Commit 78fa0b93 by Dominic Hamon

Merged with upstream

parents 4ce184d8 96446f2f
...@@ -4,3 +4,4 @@ Makefile ...@@ -4,3 +4,4 @@ Makefile
bin/ bin/
cmake_install.cmake cmake_install.cmake
lib/ lib/
tags
This diff is collapsed. Click to expand it.
benchmark benchmark
========= =========
[![Build Status](https://drone.io/github.com/google/benchmark/status.png)](https://drone.io/github.com/google/benchmark/latest)
A library to support the benchmarking of functions, similar to unit-tests. A library to support the benchmarking of functions, similar to unit-tests.
Discussion group: https://groups.google.com/d/forum/benchmark-discuss
Example usage: Example usage:
Define a function that executes the code to be measured a Define a function that executes the code to be measured a
specified number of times: specified number of times:
......
...@@ -145,24 +145,27 @@ BENCHMARK(BM_MultiThreaded)->Threads(4); ...@@ -145,24 +145,27 @@ BENCHMARK(BM_MultiThreaded)->Threads(4);
#include "macros.h" #include "macros.h"
namespace benchmark { namespace benchmark {
// If the --benchmarks flag is empty, do nothing. class BenchmarkReporter;
//
// Otherwise, run all benchmarks specified by the --benchmarks flag, void Initialize(int* argc, const char** argv);
// Otherwise, run all benchmarks specified by the --benchmark_filter flag,
// and exit after running the benchmarks. // and exit after running the benchmarks.
extern void RunSpecifiedBenchmarks(); void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter = nullptr);
// ------------------------------------------------------ // ------------------------------------------------------
// Routines that can be called from within a benchmark // Routines that can be called from within a benchmark
// //
// REQUIRES: a benchmark is currently executing // REQUIRES: a benchmark is currently executing
extern void SetLabel(const std::string& label); void SetLabel(const std::string& label);
// If this routine is called, peak memory allocation past this point in the // If this routine is called, peak memory allocation past this point in the
// benchmark is reported at the end of the benchmark report line. (It is // benchmark is reported at the end of the benchmark report line. (It is
// computed by running the benchmark once with a single iteration and a memory // computed by running the benchmark once with a single iteration and a memory
// tracer.) // tracer.)
extern void MemoryUsage(); // TODO(dominic)
//void MemoryUsage();
// If a particular benchmark is I/O bound, or if for some reason CPU // If a particular benchmark is I/O bound, or if for some reason CPU
// timings are not representative, call this method from within the // timings are not representative, call this method from within the
...@@ -170,7 +173,7 @@ extern void MemoryUsage(); ...@@ -170,7 +173,7 @@ extern void MemoryUsage();
// control how many iterations are run, and in the printing of // control how many iterations are run, and in the printing of
// items/second or MB/seconds values. If not called, the cpu time // items/second or MB/seconds values. If not called, the cpu time
// used by the benchmark will be used. // used by the benchmark will be used.
extern void UseRealTime(); void UseRealTime();
namespace internal { namespace internal {
class Benchmark; class Benchmark;
...@@ -290,15 +293,73 @@ class State { ...@@ -290,15 +293,73 @@ class State {
DISALLOW_COPY_AND_ASSIGN(State); DISALLOW_COPY_AND_ASSIGN(State);
}; };
// Interface for custom benchmark result printers.
// By default, benchmark reports are printed to stdout. However an application
// can control the destination of the reports by calling
// RunSpecifiedBenchmarks and passing it a custom reporter object.
// The reporter object must implement the following interface.
class BenchmarkReporter {
public:
struct Context {
int num_cpus;
double mhz_per_cpu;
//std::string cpu_info;
bool cpu_scaling_enabled;
// The number of chars in the longest benchmark name.
int name_field_width;
};
struct Run {
Run() :
thread_index(-1),
iterations(1),
real_accumulated_time(0),
cpu_accumulated_time(0),
bytes_per_second(0),
items_per_second(0),
max_heapbytes_used(0) {}
std::string benchmark_name;
std::string report_label;
int thread_index;
int64_t iterations;
double real_accumulated_time;
double cpu_accumulated_time;
// Zero if not set by benchmark.
double bytes_per_second;
double items_per_second;
// This is set to 0.0 if memory tracing is not enabled.
double max_heapbytes_used;
};
// Called once for every suite of benchmarks run.
// The parameter "context" contains information that the
// reporter may wish to use when generating its report, for example the
// platform under which the benchmarks are running. The benchmark run is
// never started if this function returns false, allowing the reporter
// to skip runs based on the context information.
virtual bool ReportContext(const Context& context) const = 0;
// Called once for each group of benchmark runs, gives information about
// cpu-time and heap memory usage during the benchmark run.
// Note that all the grouped benchmark runs should refer to the same
// benchmark, thus have the same name.
virtual void ReportRuns(const std::vector<Run>& report) const = 0;
virtual ~BenchmarkReporter() {}
};
namespace internal { namespace internal {
class BenchmarkReporter;
typedef std::function<void(State&)> BenchmarkFunction; typedef std::function<void(State&)> BenchmarkFunction;
// Run all benchmarks whose name is a partial match for the regular // Run all benchmarks whose name is a partial match for the regular
// expression in "spec". The results of benchmark runs are fed to "reporter". // expression in "spec". The results of benchmark runs are fed to "reporter".
void RunMatchingBenchmarks(const std::string& spec, void RunMatchingBenchmarks(const std::string& spec,
BenchmarkReporter* reporter); const BenchmarkReporter* reporter);
// Extract the list of benchmark names that match the specified regular // Extract the list of benchmark names that match the specified regular
// expression. // expression.
...@@ -376,8 +437,6 @@ class Benchmark { ...@@ -376,8 +437,6 @@ class Benchmark {
// Equivalent to ThreadRange(NumCPUs(), NumCPUs()) // Equivalent to ThreadRange(NumCPUs(), NumCPUs())
Benchmark* ThreadPerCpu(); Benchmark* ThreadPerCpu();
// TODO(dominic): Control whether or not real-time is used for this benchmark
// ------------------------------- // -------------------------------
// Following methods are not useful for clients // Following methods are not useful for clients
...@@ -411,100 +470,31 @@ class Benchmark { ...@@ -411,100 +470,31 @@ class Benchmark {
static void AddRange(std::vector<int>* dst, int lo, int hi, int mult); static void AddRange(std::vector<int>* dst, int lo, int hi, int mult);
static double MeasurePeakHeapMemory(const Instance& b); static double MeasurePeakHeapMemory(const Instance& b);
static void RunInstance(const Instance& b, BenchmarkReporter* br); static void RunInstance(const Instance& b, const BenchmarkReporter* br);
friend class ::benchmark::State; friend class ::benchmark::State;
friend struct ::benchmark::internal::Benchmark::Instance; friend struct ::benchmark::internal::Benchmark::Instance;
friend void ::benchmark::internal::RunMatchingBenchmarks(const std::string&, friend void ::benchmark::internal::RunMatchingBenchmarks(
BenchmarkReporter*); const std::string&, const BenchmarkReporter*);
DISALLOW_COPY_AND_ASSIGN(Benchmark); DISALLOW_COPY_AND_ASSIGN(Benchmark);
}; };
// ------------------------------------------------------ // ------------------------------------------------------
// Benchmarks reporter interface + data containers.
struct BenchmarkContextData {
int num_cpus;
double mhz_per_cpu;
// std::string cpu_info;
bool cpu_scaling_enabled;
// The number of chars in the longest benchmark name.
int name_field_width;
};
struct BenchmarkRunData {
BenchmarkRunData()
: thread_index(-1),
iterations(1),
real_accumulated_time(0),
cpu_accumulated_time(0),
bytes_per_second(0),
items_per_second(0),
max_heapbytes_used(0) {}
std::string benchmark_name;
std::string report_label;
int thread_index;
int64_t iterations;
double real_accumulated_time;
double cpu_accumulated_time;
// Zero if not set by benchmark.
double bytes_per_second;
double items_per_second;
// This is set to 0.0 if memory tracing is not enabled.
double max_heapbytes_used;
};
// Interface for custom benchmark result printers.
// By default, benchmark reports are printed to stdout. However an application
// can control the destination of the reports by calling
// RunMatchingBenchmarks and passing it a custom reporter object.
// The reporter object must implement the following interface.
class BenchmarkReporter {
public:
// Called once for every suite of benchmarks run.
// The parameter "context" contains information that the
// reporter may wish to use when generating its report, for example the
// platform under which the benchmarks are running. The benchmark run is
// never started if this function returns false, allowing the reporter
// to skip runs based on the context information.
virtual bool ReportContext(const BenchmarkContextData& context) = 0;
// Called once for each group of benchmark runs, gives information about
// cpu-time and heap memory usage during the benchmark run.
// Note that all the grouped benchmark runs should refer to the same
// benchmark, thus have the same name.
virtual void ReportRuns(const std::vector<BenchmarkRunData>& report) = 0;
virtual ~BenchmarkReporter();
};
// ------------------------------------------------------
// Internal implementation details follow; please ignore // Internal implementation details follow; please ignore
// Given a collection of reports, computes their mean and stddev.
// REQUIRES: all runs in "reports" must be from the same benchmark.
void ComputeStats(const std::vector<BenchmarkRunData>& reports,
BenchmarkRunData* mean_data, BenchmarkRunData* stddev_data);
// Simple reporter that outputs benchmark data to the console. This is the // Simple reporter that outputs benchmark data to the console. This is the
// default reporter used by RunSpecifiedBenchmarks(). // default reporter used by RunSpecifiedBenchmarks().
class ConsoleReporter : public BenchmarkReporter { class ConsoleReporter : public BenchmarkReporter {
public: public:
virtual bool ReportContext(const BenchmarkContextData& context); virtual bool ReportContext(const Context& context) const;
virtual void ReportRuns(const std::vector<BenchmarkRunData>& reports); virtual void ReportRuns(const std::vector<Run>& reports) const;
private: private:
std::string PrintMemoryUsage(double bytes); std::string PrintMemoryUsage(double bytes) const;
virtual void PrintRunData(const BenchmarkRunData& report); virtual void PrintRunData(const Run& report) const;
int name_field_width_; mutable int name_field_width_;
}; };
} // end namespace internal } // end namespace internal
void Initialize(int* argc, const char** argv);
} // end namespace benchmark } // end namespace benchmark
// ------------------------------------------------------ // ------------------------------------------------------
......
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include "benchmark/macros.h" #include "benchmark/macros.h"
#include "colorprint.h" #include "colorprint.h"
...@@ -224,14 +238,11 @@ bool CpuScalingEnabled() { ...@@ -224,14 +238,11 @@ bool CpuScalingEnabled() {
return false; return false;
} }
} // namespace // Given a collection of reports, computes their mean and stddev.
// REQUIRES: all runs in "reports" must be from the same benchmark.
namespace internal { void ComputeStats(const std::vector<BenchmarkReporter::Run>& reports,
BenchmarkReporter::Run* mean_data,
BenchmarkReporter::~BenchmarkReporter() {} BenchmarkReporter::Run* stddev_data) {
void ComputeStats(const std::vector<BenchmarkRunData>& reports,
BenchmarkRunData* mean_data, BenchmarkRunData* stddev_data) {
// Accumulators. // Accumulators.
Stat1_d real_accumulated_time_stat; Stat1_d real_accumulated_time_stat;
Stat1_d cpu_accumulated_time_stat; Stat1_d cpu_accumulated_time_stat;
...@@ -241,7 +252,7 @@ void ComputeStats(const std::vector<BenchmarkRunData>& reports, ...@@ -241,7 +252,7 @@ void ComputeStats(const std::vector<BenchmarkRunData>& reports,
Stat1MinMax_d max_heapbytes_used_stat; Stat1MinMax_d max_heapbytes_used_stat;
// Populate the accumulators. // Populate the accumulators.
for (std::vector<BenchmarkRunData>::const_iterator it = reports.begin(); for (std::vector<BenchmarkReporter::Run>::const_iterator it = reports.begin();
it != reports.end(); ++it) { it != reports.end(); ++it) {
CHECK_EQ(reports[0].benchmark_name, it->benchmark_name); CHECK_EQ(reports[0].benchmark_name, it->benchmark_name);
real_accumulated_time_stat += real_accumulated_time_stat +=
...@@ -282,16 +293,21 @@ void ComputeStats(const std::vector<BenchmarkRunData>& reports, ...@@ -282,16 +293,21 @@ void ComputeStats(const std::vector<BenchmarkRunData>& reports,
stddev_data->items_per_second = items_per_second_stat.StdDev(); stddev_data->items_per_second = items_per_second_stat.StdDev();
stddev_data->max_heapbytes_used = max_heapbytes_used_stat.StdDev(); stddev_data->max_heapbytes_used = max_heapbytes_used_stat.StdDev();
} }
} // namespace
std::string ConsoleReporter::PrintMemoryUsage(double bytes) { namespace internal {
if (!get_memory_usage || bytes < 0.0) return "";
std::string ConsoleReporter::PrintMemoryUsage(double bytes) const {
if (!get_memory_usage || bytes < 0.0)
return "";
std::stringstream ss; std::stringstream ss;
ss << " " << HumanReadableNumber(bytes) << "B peak-mem"; ss << " " << HumanReadableNumber(bytes) << "B peak-mem";
return ss.str(); return ss.str();
} }
bool ConsoleReporter::ReportContext(const BenchmarkContextData& context) { bool ConsoleReporter::ReportContext(
const BenchmarkReporter::Context& context) const {
name_field_width_ = context.name_field_width; name_field_width_ = context.name_field_width;
std::cout << "Benchmarking on " << context.num_cpus << " X " std::cout << "Benchmarking on " << context.num_cpus << " X "
...@@ -319,8 +335,9 @@ bool ConsoleReporter::ReportContext(const BenchmarkContextData& context) { ...@@ -319,8 +335,9 @@ bool ConsoleReporter::ReportContext(const BenchmarkContextData& context) {
return true; return true;
} }
void ConsoleReporter::ReportRuns(const std::vector<BenchmarkRunData>& reports) { void ConsoleReporter::ReportRuns(
for (std::vector<BenchmarkRunData>::const_iterator it = reports.begin(); const std::vector<BenchmarkReporter::Run>& reports) const {
for (std::vector<BenchmarkReporter::Run>::const_iterator it = reports.begin();
it != reports.end(); ++it) { it != reports.end(); ++it) {
CHECK_EQ(reports[0].benchmark_name, it->benchmark_name); CHECK_EQ(reports[0].benchmark_name, it->benchmark_name);
PrintRunData(*it); PrintRunData(*it);
...@@ -329,15 +346,15 @@ void ConsoleReporter::ReportRuns(const std::vector<BenchmarkRunData>& reports) { ...@@ -329,15 +346,15 @@ void ConsoleReporter::ReportRuns(const std::vector<BenchmarkRunData>& reports) {
// We don't report aggregated data if there was a single run. // We don't report aggregated data if there was a single run.
if (reports.size() < 2) return; if (reports.size() < 2) return;
BenchmarkRunData mean_data; BenchmarkReporter::Run mean_data;
BenchmarkRunData stddev_data; BenchmarkReporter::Run stddev_data;
internal::ComputeStats(reports, &mean_data, &stddev_data); ComputeStats(reports, &mean_data, &stddev_data);
PrintRunData(mean_data); PrintRunData(mean_data);
PrintRunData(stddev_data); PrintRunData(stddev_data);
} }
void ConsoleReporter::PrintRunData(const BenchmarkRunData& result) { void ConsoleReporter::PrintRunData(const BenchmarkReporter::Run& result) const {
// Format bytes per second // Format bytes per second
std::string rate; std::string rate;
if (result.bytes_per_second > 0) { if (result.bytes_per_second > 0) {
...@@ -363,13 +380,14 @@ void ConsoleReporter::PrintRunData(const BenchmarkRunData& result) { ...@@ -363,13 +380,14 @@ void ConsoleReporter::PrintRunData(const BenchmarkRunData& result) {
(result.cpu_accumulated_time * 1e9) / (result.cpu_accumulated_time * 1e9) /
(static_cast<double>(result.iterations))); (static_cast<double>(result.iterations)));
ColorPrintf(COLOR_CYAN, "%10lld", result.iterations); ColorPrintf(COLOR_CYAN, "%10lld", result.iterations);
ColorPrintf(COLOR_DEFAULT, "%*s %*s %s%s\n", ColorPrintf(COLOR_DEFAULT, "%*s %*s %s %s\n",
16, rate.c_str(), 13, rate.c_str(),
18, items.c_str(), 18, items.c_str(),
result.report_label.c_str(), result.report_label.c_str(),
PrintMemoryUsage(result.max_heapbytes_used).c_str()); PrintMemoryUsage(result.max_heapbytes_used).c_str());
} }
/* TODO(dominic)
void MemoryUsage() { void MemoryUsage() {
// if (benchmark_mc) { // if (benchmark_mc) {
// benchmark_mc->Reset(); // benchmark_mc->Reset();
...@@ -377,6 +395,7 @@ void MemoryUsage() { ...@@ -377,6 +395,7 @@ void MemoryUsage() {
get_memory_usage = true; get_memory_usage = true;
//} //}
} }
*/
void UseRealTime() { use_real_time = true; } void UseRealTime() { use_real_time = true; }
...@@ -542,7 +561,7 @@ struct State::SharedState { ...@@ -542,7 +561,7 @@ struct State::SharedState {
int stopping; // Number of threads that have entered STOPPING state int stopping; // Number of threads that have entered STOPPING state
int threads; // Number of total threads that are running concurrently int threads; // Number of total threads that are running concurrently
ThreadStats stats; ThreadStats stats;
std::vector<internal::BenchmarkRunData> runs; // accumulated runs std::vector<BenchmarkReporter::Run> runs; // accumulated runs
std::string label; std::string label;
explicit SharedState(const internal::Benchmark::Instance* b) explicit SharedState(const internal::Benchmark::Instance* b)
...@@ -766,7 +785,7 @@ void Benchmark::MeasureOverhead() { ...@@ -766,7 +785,7 @@ void Benchmark::MeasureOverhead() {
#endif #endif
} }
void Benchmark::RunInstance(const Instance& b, BenchmarkReporter* br) { void Benchmark::RunInstance(const Instance& b, const BenchmarkReporter* br) {
use_real_time = false; use_real_time = false;
running_benchmark = true; running_benchmark = true;
// get_memory_usage = FLAGS_gbenchmark_memory_usage; // get_memory_usage = FLAGS_gbenchmark_memory_usage;
...@@ -810,7 +829,7 @@ void Benchmark::RunInstance(const Instance& b, BenchmarkReporter* br) { ...@@ -810,7 +829,7 @@ void Benchmark::RunInstance(const Instance& b, BenchmarkReporter* br) {
*/ */
running_benchmark = false; running_benchmark = false;
for (internal::BenchmarkRunData& report : state.runs) { for (BenchmarkReporter::Run& report : state.runs) {
double seconds = (use_real_time ? report.real_accumulated_time double seconds = (use_real_time ? report.real_accumulated_time
: report.cpu_accumulated_time); : report.cpu_accumulated_time);
report.benchmark_name = b.name; report.benchmark_name = b.name;
...@@ -1006,8 +1025,7 @@ bool State::FinishInterval() { ...@@ -1006,8 +1025,7 @@ bool State::FinishInterval() {
return true; return true;
} }
internal::BenchmarkRunData data; BenchmarkReporter::Run data;
data.thread_index = thread_index;
data.iterations = iterations_; data.iterations = iterations_;
data.thread_index = thread_index; data.thread_index = thread_index;
...@@ -1094,8 +1112,7 @@ void* State::RunWrapper(void* arg) { ...@@ -1094,8 +1112,7 @@ void* State::RunWrapper(void* arg) {
namespace internal { namespace internal {
void RunMatchingBenchmarks(const std::string& spec, void RunMatchingBenchmarks(const std::string& spec,
BenchmarkReporter* reporter) { const BenchmarkReporter* reporter) {
CHECK(reporter != NULL);
if (spec.empty()) return; if (spec.empty()) return;
std::vector<internal::Benchmark::Instance> benchmarks; std::vector<internal::Benchmark::Instance> benchmarks;
...@@ -1121,7 +1138,7 @@ void RunMatchingBenchmarks(const std::string& spec, ...@@ -1121,7 +1138,7 @@ void RunMatchingBenchmarks(const std::string& spec,
} }
// Print header here // Print header here
BenchmarkContextData context; BenchmarkReporter::Context context;
context.num_cpus = NumCPUs(); context.num_cpus = NumCPUs();
context.mhz_per_cpu = CyclesPerSecond() / 1000000.0f; context.mhz_per_cpu = CyclesPerSecond() / 1000000.0f;
// context.cpu_info = base::CompactCPUIDInfoString(); // context.cpu_info = base::CompactCPUIDInfoString();
...@@ -1145,12 +1162,12 @@ void FindMatchingBenchmarkNames(const std::string& spec, ...@@ -1145,12 +1162,12 @@ void FindMatchingBenchmarkNames(const std::string& spec,
} // end namespace internal } // end namespace internal
void RunSpecifiedBenchmarks() { void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter /*= nullptr*/) {
std::string spec = FLAGS_benchmark_filter; std::string spec = FLAGS_benchmark_filter;
if (spec.empty() || spec == "all") if (spec.empty() || spec == "all")
spec = "."; // Regexp that matches all benchmarks spec = "."; // Regexp that matches all benchmarks
internal::ConsoleReporter default_reporter; internal::ConsoleReporter default_reporter;
internal::RunMatchingBenchmarks(spec, &default_reporter); internal::RunMatchingBenchmarks(spec, reporter == nullptr ? &default_reporter : reporter);
pthread_cond_destroy(&starting_cv); pthread_cond_destroy(&starting_cv);
pthread_mutex_destroy(&starting_mutex); pthread_mutex_destroy(&starting_mutex);
pthread_mutex_destroy(&benchmark_mutex); pthread_mutex_destroy(&benchmark_mutex);
......
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "colorprint.h" #include "colorprint.h"
#include <stdarg.h> #include <stdarg.h>
......
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "commandlineflags.h" #include "commandlineflags.h"
#include <string.h> #include <string.h>
......
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sleep.h" #include "sleep.h"
#include <time.h> #include <time.h>
......
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sysinfo.h" #include "sysinfo.h"
#include <errno.h> #include <errno.h>
...@@ -20,21 +34,18 @@ ...@@ -20,21 +34,18 @@
namespace benchmark { namespace benchmark {
namespace { namespace {
const int64_t estimate_time_ms = 1000;
pthread_once_t cpuinfo_init = PTHREAD_ONCE_INIT; pthread_once_t cpuinfo_init = PTHREAD_ONCE_INIT;
double cpuinfo_cycles_per_second = 1.0; double cpuinfo_cycles_per_second = 1.0;
int cpuinfo_num_cpus = 1; // Conservative guess 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 // Helper function estimates cycles/sec by observing cycles elapsed during
// sleep(). Using small sleep time decreases accuracy significantly. // sleep(). Using small sleep time decreases accuracy significantly.
int64_t EstimateCyclesPerSecond(const int estimate_time_ms) { int64_t EstimateCyclesPerSecond() {
CHECK(estimate_time_ms > 0);
double multiplier = 1000.0 / (double)estimate_time_ms; // scale by this much
const int64_t start_ticks = cycleclock::Now(); const int64_t start_ticks = cycleclock::Now();
SleepForMilliseconds(estimate_time_ms); SleepForMilliseconds(estimate_time_ms);
const int64_t guess = int64_t(multiplier * (cycleclock::Now() - start_ticks)); return cycleclock::Now() - start_ticks;
return guess;
} }
// Helper function for reading an int from a file. Returns true if successful // Helper function for reading an int from a file. Returns true if successful
...@@ -99,9 +110,9 @@ void InitializeSystemInfo() { ...@@ -99,9 +110,9 @@ void InitializeSystemInfo() {
if (fd == -1) { if (fd == -1) {
perror(pname); perror(pname);
if (!saw_mhz) { 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; double bogo_clock = 1.0;
...@@ -165,7 +176,7 @@ void InitializeSystemInfo() { ...@@ -165,7 +176,7 @@ void InitializeSystemInfo() {
cpuinfo_cycles_per_second = bogo_clock; cpuinfo_cycles_per_second = bogo_clock;
} else { } else {
// If we don't even have bogomips, we'll use the slow estimation. // 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) { if (num_cpus == 0) {
...@@ -201,7 +212,7 @@ void InitializeSystemInfo() { ...@@ -201,7 +212,7 @@ void InitializeSystemInfo() {
if (sysctlbyname(sysctl_path, &hz, &sz, NULL, 0) != 0) { if (sysctlbyname(sysctl_path, &hz, &sz, NULL, 0) != 0) {
fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n", fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n",
sysctl_path, strerror(errno)); sysctl_path, strerror(errno));
cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000); cpuinfo_cycles_per_second = EstimateCyclesPerSecond();
} else { } else {
cpuinfo_cycles_per_second = hz; cpuinfo_cycles_per_second = hz;
} }
...@@ -221,8 +232,8 @@ void InitializeSystemInfo() { ...@@ -221,8 +232,8 @@ void InitializeSystemInfo() {
"~MHz", NULL, &data, &data_size))) "~MHz", NULL, &data, &data_size)))
cpuinfo_cycles_per_second = (int64)data * (int64)(1000 * 1000); // was mhz cpuinfo_cycles_per_second = (int64)data * (int64)(1000 * 1000); // was mhz
else else
cpuinfo_cycles_per_second = EstimateCyclesPerSecond(500); // TODO <500? cpuinfo_cycles_per_second = EstimateCyclesPerSecond();
// TODO: also figure out cpuinfo_num_cpus // TODO: also figure out cpuinfo_num_cpus
#elif defined OS_MACOSX #elif defined OS_MACOSX
// returning "mach time units" per second. the current number of elapsed // returning "mach time units" per second. the current number of elapsed
...@@ -250,7 +261,7 @@ void InitializeSystemInfo() { ...@@ -250,7 +261,7 @@ void InitializeSystemInfo() {
#else #else
// Generic cycles per second counter // Generic cycles per second counter
cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000); cpuinfo_cycles_per_second = EstimateCyclesPerSecond();
#endif #endif
} }
} // end namespace } // end namespace
......
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "walltime.h" #include "walltime.h"
#include <stdio.h> #include <stdio.h>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment