Skip to content

Commit

Permalink
Merge branch 'atdt-report_loadavg'
Browse files Browse the repository at this point in the history
  • Loading branch information
dmah42 committed Nov 13, 2018
2 parents bb15a4e + b5082bb commit b171791
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Maxim Vafin <maxvafin@gmail.com>
MongoDB Inc.
Nick Hutchinson <nshutchinson@gmail.com>
Oleksandr Sochka <sasha.sochka@gmail.com>
Ori Livneh <ori.livneh@gmail.com>
Paul Redmond <paul.redmond@gmail.com>
Radoslav Yovchev <radoslav.tm@gmail.com>
Roman Lebedev <lebedev.ri@gmail.com>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Matt Clarkson <mattyclarkson@gmail.com>
Maxim Vafin <maxvafin@gmail.com>
Nick Hutchinson <nshutchinson@gmail.com>
Oleksandr Sochka <sasha.sochka@gmail.com>
Ori Livneh <ori.livneh@gmail.com>
Pascal Leroy <phl@google.com>
Paul Redmond <paul.redmond@gmail.com>
Pierre Phaneuf <pphaneuf@google.com>
Expand Down
1 change: 1 addition & 0 deletions include/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,7 @@ struct CPUInfo {
double cycles_per_second;
std::vector<CacheInfo> caches;
bool scaling_enabled;
std::vector<double> load_avg;

static const CPUInfo& Get();

Expand Down
6 changes: 6 additions & 0 deletions src/json_reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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++;

This comment has been minimized.

Copy link
@LebedevRI

LebedevRI Nov 18, 2018

Collaborator

I don't think this is correct.
It should probably be using FormatKV().

This comment has been minimized.

Copy link
@dmah42

dmah42 Nov 19, 2018

Author Member

There could be a FormatKV that takes a vector, but managing the indent (and applying it more generally to the case of the cache above) would be fairly complex.

This is reasonable and readable... Unless I misunderstood what you thought was wrong with it.

This comment has been minimized.

Copy link
@LebedevRI

LebedevRI Nov 19, 2018

Collaborator

Ooh, i see, FormatKV() can only output the whole field..
Ok then i guess.

if (it != info.load_avg.end()) out << ",";
}
out << "],\n";

#if defined(NDEBUG)
const char build_type[] = "release";
Expand Down
9 changes: 9 additions & 0 deletions src/reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <vector>

#include "check.h"
#include "string_util.h"

namespace benchmark {

Expand Down Expand Up @@ -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 "
Expand Down
21 changes: 20 additions & 1 deletion src/sysinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,24 @@ double GetCPUCyclesPerSecond() {
return static_cast<double>(cycleclock::Now() - start_ticks);
}

std::vector<double> 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<double> 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() {
Expand All @@ -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
10 changes: 8 additions & 2 deletions test/reporter_output_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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}});
}
Expand All @@ -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();
Expand Down

0 comments on commit b171791

Please sign in to comment.