rgc_utils.c
Go to the documentation of this file.
1 /*
2  * 2003-
3  * rgc_utils.c : R.Hanai
4  */
5 
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <stdlib.h>
9 #include "rgc_utils.h"
10 
11 int debug_printf(char *fmt, ...)
12 {
13  va_list args;
14  char buf[128];
15 
16  va_start(args, fmt);
17  vsprintf(buf, fmt, args);
18  va_end(args);
19  fprintf(stderr, "[ %s ]\n", buf);
20 
21  return 0;
22 }
23 
24 /* a dummy function for break points */
25 void hoge(){}
26 
27 #define __USE_RDTSC
28 #ifdef __USE_RDTSC
29 #define CPU_CLOCK 1700 /* 1.7GHz -> microsec */
30 
31 static int cpu_clock;
32 
34 {
35 #define BUF_SIZE 100
36  FILE *fp;
37  char buf[BUF_SIZE];
38  int i;
39  if ((fp = fopen("/proc/cpuinfo", "r")) == NULL) {
40  fprintf(stderr, "Can't open /proc/cpuinfo\n");
41  return -1;
42  }
43  while(fgets(buf, BUF_SIZE, fp) != NULL) {
44  if (strncmp(buf, "cpu MHz", 7) )
45  continue;
46  for (i = 0; i < BUF_SIZE; i++) {
47  if (buf[i] == ':') break;
48  }
49  if (i != BUF_SIZE) {
50  sscanf(&buf[i+1], "%d", &cpu_clock);
51  if (cpu_clock <= 0)
52  return -1;
53  else
54  fprintf(stderr, "CPU CLOCK is %d\n", cpu_clock);
55  }
56  }
57  return cpu_clock;
58 }
59 
60 typedef union {
61  unsigned long long val;
62  struct {
63  unsigned long eax;
64  unsigned long edx;
65  } reg;
66 } time_stamp_t;
67 
68 #define rdtsc(tsc)\
69 __asm__ __volatile__("rdtsc"\
70  : "=a" (tsc.reg.eax), "=d" (tsc.reg.edx)\
71  )
72 // : /* no input */\
73 // : "eax", "edx") // <-- Is this O.K.?
74 
76 
77 static void reset_utime(){
78  start.val = 0ULL;
79 }
80 
81 unsigned current_utime(void){
82  rdtsc(now);
83  return (now.val - start.val)/cpu_clock;
84 }
85 
86 #else /* __USE_RDTSC */
87 
88 unsigned current_utime(void){
89  struct timeval t;
90  if(gettimeofday( &t, 0 ) == -1)
91  return 0;
92  return (t.tv_sec * 1000000 + t.tv_usec);
93 }
94 
95 #endif /* __USE_RDTSC */
96 
97 #ifdef __PAPI
98 /*
99  * Compile command is as follows.
100  * gcc -I/usr/local/papi/include papitest.c /usr/local/papi/lib/libpapi.a
101  */
102 
103 #include <papi.h>
104 
105 /* event name: See "papiStdEventDefs.h" */
106 static int Events[2] = { PAPI_L2_TCH, PAPI_L2_TCA};
107 
108 static long_long values[2]; /* "long_long" is defined as "long long" */
109 
110 static void handle_error(int code) {
111  fprintf(stderr, "PAPI: an error occurred.\n");
112  exit(code);
113 }
114 
115 void papi_init() {
116  int num_hwcntrs = 0;
117  num_hwcntrs = PAPI_num_counters();
118 
119  /* The installation does not support PAPI */
120  if (num_hwcntrs < 0 )
121  handle_error(1);
122 
123  /* The installation supports PAPI, but has no counters */
124  if (num_hwcntrs == 0)
125  fprintf(stderr, "Info: This machine does not provide hardware counters." );
126 
127  if (num_hwcntrs > 2)
128  num_hwcntrs = 2;
129 
130  /* Start counting events */
131  if (PAPI_start_counters(Events, num_hwcntrs) != PAPI_OK)
132  handle_error(3);
133 }
134 
135 void papi_print_counters(){
136  PAPI_read_counters(values, 2);
137  // PAPI_accum_counters(values, 2);
138  fprintf(stderr, "PAPI: %lf, %lf\n", (double)values[0], (double)values[1]);
139 
140 #endif
141 
143 {
144  if (read_cpuinfo() < 0) {
146  }
147  reset_utime();
148 
149 #ifdef __PAPI
150  papi_init();
151 #endif
152 }
153 
154 /*
155  * live object maps for debbuging
156  */
157 
time_stamp_t::val
unsigned long long val
Definition: rgc_utils.c:61
time_stamp_t::edx
unsigned long edx
Definition: rgc_utils.c:64
buf
static char buf[CHAR_SIZE]
Definition: helpsub.c:23
time_stamp_t
Definition: rgc_utils.c:60
CPU_CLOCK
#define CPU_CLOCK
Definition: rgc_utils.c:29
now
static time_stamp_t now
Definition: rgc_utils.c:75
reset_utime
static void reset_utime()
Definition: rgc_utils.c:77
read_cpuinfo
int read_cpuinfo()
Definition: rgc_utils.c:33
hoge
void hoge()
Definition: rgc_utils.c:25
NULL
#define NULL
Definition: transargv.c:8
rgc_utils.h
current_utime
unsigned current_utime(void)
Definition: rgc_utils.c:81
reg
#define reg
Definition: eustags.c:122
values
long long values[]
start
static time_stamp_t start
Definition: rgc_utils.c:75
rdtsc
#define rdtsc(tsc)
Definition: rgc_utils.c:68
BUF_SIZE
#define BUF_SIZE
time_stamp_t::eax
unsigned long eax
Definition: rgc_utils.c:63
code
Definition: eus.h:230
cpu_clock
static int cpu_clock
Definition: rgc_utils.c:31
debug_printf
int debug_printf(char *fmt,...)
Definition: rgc_utils.c:11
init_utils
void init_utils()
Definition: rgc_utils.c:142


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 15 2023 02:06:43