Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <stdarg.h>
00008 #include <stdlib.h>
00009 #include "rgc_utils.h"
00010
00011 int debug_printf(char *fmt, ...)
00012 {
00013 va_list args;
00014 char buf[128];
00015
00016 va_start(args, fmt);
00017 vsprintf(buf, fmt, args);
00018 va_end(args);
00019 fprintf(stderr, "[ %s ]\n", buf);
00020
00021 return 0;
00022 }
00023
00024
00025 void hoge(){}
00026
00027 #define __USE_RDTSC
00028 #ifdef __USE_RDTSC
00029 #define CPU_CLOCK 1700
00030
00031 static int cpu_clock;
00032
00033 int read_cpuinfo()
00034 {
00035 #define BUF_SIZE 100
00036 FILE *fp;
00037 char buf[BUF_SIZE];
00038 int i;
00039 if ((fp = fopen("/proc/cpuinfo", "r")) == NULL) {
00040 fprintf(stderr, "Can't open /proc/cpuinfo\n");
00041 return -1;
00042 }
00043 while(fgets(buf, BUF_SIZE, fp) != NULL) {
00044 if (strncmp(buf, "cpu MHz", 7) )
00045 continue;
00046 for (i = 0; i < BUF_SIZE; i++) {
00047 if (buf[i] == ':') break;
00048 }
00049 if (i != BUF_SIZE) {
00050 sscanf(&buf[i+1], "%d", &cpu_clock);
00051 if (cpu_clock <= 0)
00052 return -1;
00053 else
00054 fprintf(stderr, "CPU CLOCK is %d\n", cpu_clock);
00055 }
00056 }
00057 return cpu_clock;
00058 }
00059
00060 typedef union {
00061 unsigned long long val;
00062 struct {
00063 unsigned long eax;
00064 unsigned long edx;
00065 } reg;
00066 } time_stamp_t;
00067
00068 #define rdtsc(tsc)\
00069 __asm__ __volatile__("rdtsc"\
00070 : "=a" (tsc.reg.eax), "=d" (tsc.reg.edx)\
00071 )
00072
00073
00074
00075 static time_stamp_t now, start;
00076
00077 static void reset_utime(){
00078 start.val = 0ULL;
00079 }
00080
00081 unsigned current_utime(void){
00082 rdtsc(now);
00083 return (now.val - start.val)/cpu_clock;
00084 }
00085
00086 #else
00087
00088 unsigned current_utime(void){
00089 struct timeval t;
00090 if(gettimeofday( &t, 0 ) == -1)
00091 return 0;
00092 return (t.tv_sec * 1000000 + t.tv_usec);
00093 }
00094
00095 #endif
00096
00097 #ifdef __PAPI
00098
00099
00100
00101
00102
00103 #include <papi.h>
00104
00105
00106 static int Events[2] = { PAPI_L2_TCH, PAPI_L2_TCA};
00107
00108 static long_long values[2];
00109
00110 static void handle_error(int code) {
00111 fprintf(stderr, "PAPI: an error occurred.\n");
00112 exit(code);
00113 }
00114
00115 void papi_init() {
00116 int num_hwcntrs = 0;
00117 num_hwcntrs = PAPI_num_counters();
00118
00119
00120 if (num_hwcntrs < 0 )
00121 handle_error(1);
00122
00123
00124 if (num_hwcntrs == 0)
00125 fprintf(stderr, "Info: This machine does not provide hardware counters." );
00126
00127 if (num_hwcntrs > 2)
00128 num_hwcntrs = 2;
00129
00130
00131 if (PAPI_start_counters(Events, num_hwcntrs) != PAPI_OK)
00132 handle_error(3);
00133 }
00134
00135 void papi_print_counters(){
00136 PAPI_read_counters(values, 2);
00137
00138 fprintf(stderr, "PAPI: %lf, %lf\n", (double)values[0], (double)values[1]);
00139
00140 #endif
00141
00142 void init_utils()
00143 {
00144 if (read_cpuinfo() < 0) {
00145 cpu_clock = CPU_CLOCK;
00146 }
00147 reset_utime();
00148
00149 #ifdef __PAPI
00150 papi_init();
00151 #endif
00152 }
00153
00154
00155
00156
00157