Skip to content

Commit 7fda14a

Browse files
authored
Merge 8128e07 into 19f7d5c
2 parents 19f7d5c + 8128e07 commit 7fda14a

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

include/benchmark/benchmark.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,14 @@ struct CPUInfo {
12931293
BENCHMARK_DISALLOW_COPY_AND_ASSIGN(CPUInfo);
12941294
};
12951295

1296+
struct SystemInformation {
1297+
std::string name;
1298+
static const SystemInformation& Get();
1299+
private:
1300+
SystemInformation();
1301+
BENCHMARK_DISALLOW_COPY_AND_ASSIGN(SystemInformation);
1302+
};
1303+
12961304
// Interface for custom benchmark result printers.
12971305
// By default, benchmark reports are printed to stdout. However an application
12981306
// can control the destination of the reports by calling
@@ -1302,6 +1310,7 @@ class BenchmarkReporter {
13021310
public:
13031311
struct Context {
13041312
CPUInfo const& cpu_info;
1313+
SystemInformation const& sys_info;
13051314
// The number of chars in the longest benchmark name.
13061315
size_t name_field_width;
13071316
static const char* executable_name;

src/json_reporter.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ bool JSONReporter::ReportContext(const Context& context) {
7777
std::string walltime_value = LocalDateTimeString();
7878
out << indent << FormatKV("date", walltime_value) << ",\n";
7979

80+
out << indent << FormatKV("host_name", context.sys_info.name) << ",\n";
81+
8082
if (Context::executable_name) {
8183
// windows uses backslash for its path separator,
8284
// which must be escaped in JSON otherwise it blows up conforming JSON

src/reporter.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ void BenchmarkReporter::PrintBasicContext(std::ostream *out,
7979
// No initializer because it's already initialized to NULL.
8080
const char *BenchmarkReporter::Context::executable_name;
8181

82-
BenchmarkReporter::Context::Context() : cpu_info(CPUInfo::Get()) {}
82+
BenchmarkReporter::Context::Context()
83+
: cpu_info(CPUInfo::Get()), sys_info(SystemInformation::Get()) {}
8384

8485
std::string BenchmarkReporter::Run::benchmark_name() const {
8586
std::string name = run_name;

src/sysinfo.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,33 @@ std::vector<CPUInfo::CacheInfo> GetCacheSizes() {
366366
#endif
367367
}
368368

369+
std::string GetSystemName() {
370+
#if defined(BENCHMARK_OS_WINDOWS)
371+
std::string str;
372+
const unsigned COUNT = MAX_COMPUTERNAME_LENGTH+1;
373+
TCHAR hostname[COUNT];
374+
DWORD DWCOUNT = COUNT;
375+
if (!GetComputerName(hostname, &DWCOUNT))
376+
return std::string("Unable to Get Host Name");
377+
#ifndef UNICODE
378+
str = hostname;
379+
#else
380+
std::wstring wStr = hostname;
381+
str = std::string(wStr.begin(), wStr.end());
382+
#endif
383+
return str;
384+
#else
385+
#ifdef BENCHMARK_OS_MACOSX //Mac Doesnt have a copy for Host Name in it
386+
#define HOST_NAME_MAX 64
387+
#endif
388+
const unsigned COUNT = HOST_NAME_MAX;
389+
char hostname[COUNT];
390+
int retVal = gethostname(hostname, COUNT);
391+
if (retVal != 0) return std::string("Unable to Get Host Name");
392+
return std::string(hostname);
393+
#endif
394+
}
395+
369396
int GetNumCPUs() {
370397
#ifdef BENCHMARK_HAS_SYSCTL
371398
int NumCPU = -1;
@@ -609,4 +636,11 @@ CPUInfo::CPUInfo()
609636
scaling_enabled(CpuScalingEnabled(num_cpus)),
610637
load_avg(GetLoadAvg()) {}
611638

639+
640+
const SystemInformation& SystemInformation::Get() {
641+
static const SystemInformation* info = new SystemInformation();
642+
return *info;
643+
}
644+
645+
SystemInformation::SystemInformation() : name(GetSystemName()) {}
612646
} // end namespace benchmark

test/reporter_output_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static int AddContextCases() {
2323
{{"^\\{", MR_Default},
2424
{"\"context\":", MR_Next},
2525
{"\"date\": \"", MR_Next},
26+
{"\"host_name\":", MR_Next},
2627
{"\"executable\": \".*(/|\\\\)reporter_output_test(\\.exe)?\",",
2728
MR_Next},
2829
{"\"num_cpus\": %int,$", MR_Next},

0 commit comments

Comments
 (0)