1 #ifndef _SYRAH_CYCLE_TIMER_H_ 2 #define _SYRAH_CYCLE_TIMER_H_ 5 #if defined(__x86_64__) 6 #include <sys/sysctl.h> 9 #include <mach/mach_time.h> 10 #endif // __x86_64__ or not 40 typedef unsigned long long SysClock;
45 static SysClock currentTicks() {
46 #if defined(__APPLE__) && !defined(__x86_64__) 47 return mach_absolute_time();
50 QueryPerformanceCounter(&qwTime);
51 return qwTime.QuadPart;
52 #elif defined(__x86_64__) 54 asm volatile(
"rdtsc" :
"=a"(a),
"=d"(d));
55 return static_cast<unsigned long long>(a) |
56 (static_cast<unsigned long long>(d) << 32);
57 #elif defined(__ARM_NEON__) && 0 // mrc requires superuser. 59 asm volatile(
"mrc p15, 0, %0, c9, c13, 0" :
"=r"(val));
63 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &spec);
64 return CycleTimer::SysClock(static_cast<float>(spec.tv_sec) * 1e9 +
65 static_cast<float>(spec.tv_nsec));
73 static double currentSeconds() {
return currentTicks() * secondsPerTick(); }
77 static double ticksPerSecond() {
return 1.0 / secondsPerTick(); }
79 static const char* tickUnits() {
80 #if defined(__APPLE__) && !defined(__x86_64__) 82 #elif defined(__WIN32__) || defined(__x86_64__) 91 static double secondsPerTick() {
92 static bool initialized =
false;
93 static double secondsPerTick_val;
94 if (initialized)
return secondsPerTick_val;
95 #if defined(__APPLE__) 97 int args[] = {CTL_HW, HW_CPU_FREQ};
99 size_t len =
sizeof(Hz);
100 if (sysctl(args, 2, &Hz, &len, NULL, 0) != 0) {
101 fprintf(stderr,
"Failed to initialize secondsPerTick_val!\n");
104 secondsPerTick_val = 1.0 / (double)Hz;
106 mach_timebase_info_data_t time_info;
107 mach_timebase_info(&time_info);
110 secondsPerTick_val = (1e-9 *
static_cast<double>(time_info.numer)) /
111 static_cast<double>(time_info.denom);
112 #endif // x86_64 or not 113 #elif defined(_WIN32) 114 LARGE_INTEGER qwTicksPerSec;
115 QueryPerformanceFrequency(&qwTicksPerSec);
116 secondsPerTick_val = 1.0 /
static_cast<double>(qwTicksPerSec.QuadPart);
118 FILE* fp = fopen(
"/proc/cpuinfo",
"r");
122 "CycleTimer::resetScale failed: couldn't find /proc/cpuinfo.");
126 secondsPerTick_val = 1e-9;
127 while (!feof(fp) && fgets(input, 1024, fp)) {
131 if (strstr(input,
"model name")) {
132 char* at_sign = strstr(input,
"@");
134 char* after_at = at_sign + 1;
135 char* GHz_str = strstr(after_at,
"GHz");
136 char* MHz_str = strstr(after_at,
"MHz");
139 if (1 == sscanf(after_at,
"%f", &GHz)) {
141 secondsPerTick_val = 1e-9f / GHz;
144 }
else if (MHz_str) {
146 if (1 == sscanf(after_at,
"%f", &MHz)) {
148 secondsPerTick_val = 1e-6f / GHz;
153 }
else if (1 == sscanf(input,
"cpu MHz : %f", &MHz)) {
155 secondsPerTick_val = 1e-6f / MHz;
163 return secondsPerTick_val;
168 static double msPerTick() {
return secondsPerTick() * 1000.0; }
175 #endif // #ifndef _SYRAH_CYCLE_TIMER_H_ Definition: database.cpp:36
Definition: cycle_timer.h:38