diff --git a/src/sysinfo.cc b/src/sysinfo.cc index 8e323301..f3a7ea76 100644 --- a/src/sysinfo.cc +++ b/src/sysinfo.cc @@ -20,21 +20,18 @@ namespace benchmark { namespace { +const int64_t estimate_time_ms = 1000; pthread_once_t cpuinfo_init = PTHREAD_ONCE_INIT; double cpuinfo_cycles_per_second = 1.0; int cpuinfo_num_cpus = 1; // Conservative guess -static pthread_mutex_t cputimens_mutex; +pthread_mutex_t cputimens_mutex; // Helper function estimates cycles/sec by observing cycles elapsed during // sleep(). Using small sleep time decreases accuracy significantly. -int64_t EstimateCyclesPerSecond(const int estimate_time_ms) { - CHECK(estimate_time_ms > 0); - double multiplier = 1000.0 / (double)estimate_time_ms; // scale by this much - +int64_t EstimateCyclesPerSecond() { const int64_t start_ticks = cycleclock::Now(); SleepForMilliseconds(estimate_time_ms); - const int64_t guess = int64_t(multiplier * (cycleclock::Now() - start_ticks)); - return guess; + return cycleclock::Now() - start_ticks; } // Helper function for reading an int from a file. Returns true if successful @@ -99,9 +96,9 @@ void InitializeSystemInfo() { if (fd == -1) { perror(pname); if (!saw_mhz) { - cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000); + cpuinfo_cycles_per_second = EstimateCyclesPerSecond(); } - return; // TODO: use generic tester instead? + return; } double bogo_clock = 1.0; @@ -166,7 +163,7 @@ void InitializeSystemInfo() { cpuinfo_cycles_per_second = bogo_clock; } else { // If we don't even have bogomips, we'll use the slow estimation. - cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000); + cpuinfo_cycles_per_second = EstimateCyclesPerSecond(); } } if (num_cpus == 0) { @@ -202,7 +199,7 @@ void InitializeSystemInfo() { if ( sysctlbyname(sysctl_path, &hz, &sz, NULL, 0) != 0 ) { fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n", sysctl_path, strerror(errno)); - cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000); + cpuinfo_cycles_per_second = EstimateCyclesPerSecond(); } else { cpuinfo_cycles_per_second = hz; } @@ -222,7 +219,7 @@ void InitializeSystemInfo() { "~MHz", NULL, &data, &data_size))) cpuinfo_cycles_per_second = (int64)data * (int64)(1000 * 1000); // was mhz else - cpuinfo_cycles_per_second = EstimateCyclesPerSecond(500); // TODO <500? + cpuinfo_cycles_per_second = EstimateCyclesPerSecond(); // TODO: also figure out cpuinfo_num_cpus #elif defined OS_MACOSX @@ -251,7 +248,7 @@ void InitializeSystemInfo() { #else // Generic cycles per second counter - cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000); + cpuinfo_cycles_per_second = EstimateCyclesPerSecond(); #endif } } // end namespace