Skip to content

Commit 5c4b578

Browse files
authored
Merge 26b2d4d into eee8b05
2 parents eee8b05 + 26b2d4d commit 5c4b578

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#include <limits>
5353
#include <memory>
5454
#include <sstream>
55+
#include <locale>
56+
#include <codecvt>
5557

5658
#include "check.h"
5759
#include "cycleclock.h"
@@ -366,6 +368,35 @@ std::vector<CPUInfo::CacheInfo> GetCacheSizes() {
366368
#endif
367369
}
368370

371+
std::string GetSystemName() {
372+
#if defined(BENCHMARK_OS_WINDOWS)
373+
std::string str;
374+
const unsigned COUNT = MAX_COMPUTERNAME_LENGTH+1;
375+
TCHAR hostname[COUNT] = {'\0'};
376+
DWORD DWCOUNT = COUNT;
377+
if (!GetComputerName(hostname, &DWCOUNT))
378+
return std::string("Unable to Get Host Name");
379+
#ifndef UNICODE
380+
str = std::string(hostname, DWCOUNT);
381+
#else
382+
//Using wstring_convert, Is deprecated in C++17
383+
using convert_type = std::codecvt_utf8<wchar_t>;
384+
std::wstring_convert<convert_type, wchar_t> converter;
385+
std::wstring wStr(hostname, DWCOUNT);
386+
str = converter.to_bytes(wStr);
387+
#endif
388+
return str;
389+
#else // defined(BENCHMARK_OS_WINDOWS)
390+
#ifdef BENCHMARK_OS_MACOSX //Mac Doesnt have HOST_NAME_MAX defined
391+
#define HOST_NAME_MAX 64
392+
#endif
393+
char hostname[HOST_NAME_MAX];
394+
int retVal = gethostname(hostname, HOST_NAME_MAX);
395+
if (retVal != 0) return std::string("Unable to Get Host Name");
396+
return std::string(hostname);
397+
#endif // Catch-all POSIX block.
398+
}
399+
369400
int GetNumCPUs() {
370401
#ifdef BENCHMARK_HAS_SYSCTL
371402
int NumCPU = -1;
@@ -609,4 +640,11 @@ CPUInfo::CPUInfo()
609640
scaling_enabled(CpuScalingEnabled(num_cpus)),
610641
load_avg(GetLoadAvg()) {}
611642

643+
644+
const SystemInformation& SystemInformation::Get() {
645+
static const SystemInformation* info = new SystemInformation();
646+
return *info;
647+
}
648+
649+
SystemInformation::SystemInformation() : name(GetSystemName()) {}
612650
} // 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)