diff --git a/AUTHORS b/AUTHORS index daea1f6..09e2e05 100644 --- a/AUTHORS +++ b/AUTHORS @@ -36,6 +36,7 @@ Maxim Vafin MongoDB Inc. Nick Hutchinson Oleksandr Sochka +Ori Livneh Paul Redmond Radoslav Yovchev Roman Lebedev diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 2ff2f2a..f727bd1 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -50,6 +50,7 @@ Matt Clarkson Maxim Vafin Nick Hutchinson Oleksandr Sochka +Ori Livneh Pascal Leroy Paul Redmond Pierre Phaneuf diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 157a717..1f072b1 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -1284,6 +1284,7 @@ struct CPUInfo { double cycles_per_second; std::vector caches; bool scaling_enabled; + std::vector load_avg; static const CPUInfo& Get(); diff --git a/src/json_reporter.cc b/src/json_reporter.cc index 6866c71..f599425 100644 --- a/src/json_reporter.cc +++ b/src/json_reporter.cc @@ -116,6 +116,12 @@ bool JSONReporter::ReportContext(const Context& context) { } indent = std::string(4, ' '); out << indent << "],\n"; + out << indent << "\"load_avg\": ["; + for (auto it = info.load_avg.begin(); it != info.load_avg.end();) { + out << *it++; + if (it != info.load_avg.end()) out << ","; + } + out << "],\n"; #if defined(NDEBUG) const char build_type[] = "release"; diff --git a/src/reporter.cc b/src/reporter.cc index fe86617..056561d 100644 --- a/src/reporter.cc +++ b/src/reporter.cc @@ -22,6 +22,7 @@ #include #include "check.h" +#include "string_util.h" namespace benchmark { @@ -54,6 +55,14 @@ void BenchmarkReporter::PrintBasicContext(std::ostream *out, Out << "\n"; } } + if (!info.load_avg.empty()) { + Out << "Load Average: "; + for (auto It = info.load_avg.begin(); It != info.load_avg.end();) { + Out << StrFormat("%.2f", *It++); + if (It != info.load_avg.end()) Out << ", "; + } + Out << "\n"; + } if (info.scaling_enabled) { Out << "***WARNING*** CPU scaling is enabled, the benchmark " diff --git a/src/sysinfo.cc b/src/sysinfo.cc index d740047..0ad300e 100644 --- a/src/sysinfo.cc +++ b/src/sysinfo.cc @@ -577,6 +577,24 @@ double GetCPUCyclesPerSecond() { return static_cast(cycleclock::Now() - start_ticks); } +std::vector GetLoadAvg() { +#if defined BENCHMARK_OS_FREEBSD || defined(BENCHMARK_OS_LINUX) || \ + defined BENCHMARK_OS_MACOSX || defined BENCHMARK_OS_NETBSD || \ + defined BENCHMARK_OS_OPENBSD + constexpr int kMaxSamples = 3; + std::vector res(kMaxSamples, 0.0); + const int nelem = getloadavg(res.data(), kMaxSamples); + if (nelem < 1) { + res.clear(); + } else { + res.resize(nelem); + } + return res; +#else + return {}; +#endif +} + } // end namespace const CPUInfo& CPUInfo::Get() { @@ -588,6 +606,7 @@ CPUInfo::CPUInfo() : num_cpus(GetNumCPUs()), cycles_per_second(GetCPUCyclesPerSecond()), caches(GetCacheSizes()), - scaling_enabled(CpuScalingEnabled(num_cpus)) {} + scaling_enabled(CpuScalingEnabled(num_cpus)), + load_avg(GetLoadAvg()) {} } // end namespace benchmark diff --git a/test/reporter_output_test.cc b/test/reporter_output_test.cc index 80314b3..8a45471 100644 --- a/test/reporter_output_test.cc +++ b/test/reporter_output_test.cc @@ -29,7 +29,8 @@ static int AddContextCases() { {"\"mhz_per_cpu\": %float,$", MR_Next}, {"\"cpu_scaling_enabled\": ", MR_Next}, {"\"caches\": \\[$", MR_Next}}); - auto const& Caches = benchmark::CPUInfo::Get().caches; + auto const& Info = benchmark::CPUInfo::Get(); + auto const& Caches = Info.caches; if (!Caches.empty()) { AddCases(TC_ConsoleErr, {{"CPU Caches:$", MR_Next}}); } @@ -46,8 +47,13 @@ static int AddContextCases() { {"\"num_sharing\": %int$", MR_Next}, {"}[,]{0,1}$", MR_Next}}); } - AddCases(TC_JSONOut, {{"],$"}}); + auto const& LoadAvg = Info.load_avg; + if (!LoadAvg.empty()) { + AddCases(TC_ConsoleErr, + {{"Load Average: (%float, ){0,2}%float$", MR_Next}}); + } + AddCases(TC_JSONOut, {{"\"load_avg\": \\[(%float,?){0,3}],$", MR_Next}}); return 0; } int dummy_register = AddContextCases();