Skip to content

Commit b171791

Browse files
committed
Merge branch 'atdt-report_loadavg'
2 parents bb15a4e + b5082bb commit b171791

File tree

7 files changed

+46
-3
lines changed

7 files changed

+46
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Maxim Vafin <maxvafin@gmail.com>
3636
MongoDB Inc.
3737
Nick Hutchinson <nshutchinson@gmail.com>
3838
Oleksandr Sochka <sasha.sochka@gmail.com>
39+
Ori Livneh <ori.livneh@gmail.com>
3940
Paul Redmond <paul.redmond@gmail.com>
4041
Radoslav Yovchev <radoslav.tm@gmail.com>
4142
Roman Lebedev <lebedev.ri@gmail.com>

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Matt Clarkson <mattyclarkson@gmail.com>
5050
Maxim Vafin <maxvafin@gmail.com>
5151
Nick Hutchinson <nshutchinson@gmail.com>
5252
Oleksandr Sochka <sasha.sochka@gmail.com>
53+
Ori Livneh <ori.livneh@gmail.com>
5354
Pascal Leroy <phl@google.com>
5455
Paul Redmond <paul.redmond@gmail.com>
5556
Pierre Phaneuf <pphaneuf@google.com>

include/benchmark/benchmark.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ struct CPUInfo {
12841284
double cycles_per_second;
12851285
std::vector<CacheInfo> caches;
12861286
bool scaling_enabled;
1287+
std::vector<double> load_avg;
12871288

12881289
static const CPUInfo& Get();
12891290

src/json_reporter.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ bool JSONReporter::ReportContext(const Context& context) {
116116
}
117117
indent = std::string(4, ' ');
118118
out << indent << "],\n";
119+
out << indent << "\"load_avg\": [";
120+
for (auto it = info.load_avg.begin(); it != info.load_avg.end();) {
121+
out << *it++;
122+
if (it != info.load_avg.end()) out << ",";
123+
}
124+
out << "],\n";
119125

120126
#if defined(NDEBUG)
121127
const char build_type[] = "release";

src/reporter.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include "check.h"
25+
#include "string_util.h"
2526

2627
namespace benchmark {
2728

@@ -54,6 +55,14 @@ void BenchmarkReporter::PrintBasicContext(std::ostream *out,
5455
Out << "\n";
5556
}
5657
}
58+
if (!info.load_avg.empty()) {
59+
Out << "Load Average: ";
60+
for (auto It = info.load_avg.begin(); It != info.load_avg.end();) {
61+
Out << StrFormat("%.2f", *It++);
62+
if (It != info.load_avg.end()) Out << ", ";
63+
}
64+
Out << "\n";
65+
}
5766

5867
if (info.scaling_enabled) {
5968
Out << "***WARNING*** CPU scaling is enabled, the benchmark "

src/sysinfo.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,24 @@ double GetCPUCyclesPerSecond() {
577577
return static_cast<double>(cycleclock::Now() - start_ticks);
578578
}
579579

580+
std::vector<double> GetLoadAvg() {
581+
#if defined BENCHMARK_OS_FREEBSD || defined(BENCHMARK_OS_LINUX) || \
582+
defined BENCHMARK_OS_MACOSX || defined BENCHMARK_OS_NETBSD || \
583+
defined BENCHMARK_OS_OPENBSD
584+
constexpr int kMaxSamples = 3;
585+
std::vector<double> res(kMaxSamples, 0.0);
586+
const int nelem = getloadavg(res.data(), kMaxSamples);
587+
if (nelem < 1) {
588+
res.clear();
589+
} else {
590+
res.resize(nelem);
591+
}
592+
return res;
593+
#else
594+
return {};
595+
#endif
596+
}
597+
580598
} // end namespace
581599

582600
const CPUInfo& CPUInfo::Get() {
@@ -588,6 +606,7 @@ CPUInfo::CPUInfo()
588606
: num_cpus(GetNumCPUs()),
589607
cycles_per_second(GetCPUCyclesPerSecond()),
590608
caches(GetCacheSizes()),
591-
scaling_enabled(CpuScalingEnabled(num_cpus)) {}
609+
scaling_enabled(CpuScalingEnabled(num_cpus)),
610+
load_avg(GetLoadAvg()) {}
592611

593612
} // end namespace benchmark

test/reporter_output_test.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ static int AddContextCases() {
2929
{"\"mhz_per_cpu\": %float,$", MR_Next},
3030
{"\"cpu_scaling_enabled\": ", MR_Next},
3131
{"\"caches\": \\[$", MR_Next}});
32-
auto const& Caches = benchmark::CPUInfo::Get().caches;
32+
auto const& Info = benchmark::CPUInfo::Get();
33+
auto const& Caches = Info.caches;
3334
if (!Caches.empty()) {
3435
AddCases(TC_ConsoleErr, {{"CPU Caches:$", MR_Next}});
3536
}
@@ -46,8 +47,13 @@ static int AddContextCases() {
4647
{"\"num_sharing\": %int$", MR_Next},
4748
{"}[,]{0,1}$", MR_Next}});
4849
}
49-
5050
AddCases(TC_JSONOut, {{"],$"}});
51+
auto const& LoadAvg = Info.load_avg;
52+
if (!LoadAvg.empty()) {
53+
AddCases(TC_ConsoleErr,
54+
{{"Load Average: (%float, ){0,2}%float$", MR_Next}});
55+
}
56+
AddCases(TC_JSONOut, {{"\"load_avg\": \\[(%float,?){0,3}],$", MR_Next}});
5157
return 0;
5258
}
5359
int dummy_register = AddContextCases();

0 commit comments

Comments
 (0)