CGR Localization
 All Classes Namespaces Files Functions Variables Macros Pages
timer.cc
1 #include "timer.h"
2 
3 double CycleTimer::cpu_period = -1.0;
4 
5 double GetCPUClockRateMHz()
6 {
7 #ifdef linux
8  static double mhz = 0.0;
9  if(mhz > 0.0) return(mhz);
10 
11  FILE *in = fopen("/proc/cpuinfo","rt");
12  const int length=64;
13  char buf[length],*s;
14 
15  if(in){
16  while(fgets(buf,length,in)){
17  if(strncmp(buf,"cpu MHz",7) == 0){
18  s = strchr(buf,':');
19  if(*s) s++;
20  mhz = atof(s);
21  }
22  }
23  // while(!feof(in) && fscanf(in,"cpu MHz%*s:%lf\n",&mhz)!=1);
24  fclose(in);
25  }
26  // printf("mhz=%f\n",mhz);
27  return(mhz);
28 #endif
29 
30 #ifdef Apertos
31  // Return constant, since supercore Aibos and the SDR are 400MHz
32  // should eventually figure out how to actually get this properly
33  return(400);
34 #endif
35 
36  // default, fail
37  return(0.0);
38 }
39 
40 double GetCPUClockPeriod()
41 {
42  static double period = 0.0;
43  if(period != 0.0) return(period);
44 
45  period = 1E-6 / GetCPUClockRateMHz();
46  return(period);
47 }