Skip to content

Commit f70f5b3

Browse files
authored
Merge 3f44492 into 19f7d5c
2 parents 19f7d5c + 3f44492 commit f70f5b3

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

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/reporter.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ void BenchmarkReporter::PrintBasicContext(std::ostream *out,
3838

3939
Out << LocalDateTimeString() << "\n";
4040

41+
const SystemInformation &sys_info = context.sys_info;
42+
Out << "System Name "<< sys_info.name << "\n";
43+
4144
if (context.executable_name)
4245
Out << "Running " << context.executable_name << "\n";
4346

@@ -79,7 +82,7 @@ void BenchmarkReporter::PrintBasicContext(std::ostream *out,
7982
// No initializer because it's already initialized to NULL.
8083
const char *BenchmarkReporter::Context::executable_name;
8184

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

8487
std::string BenchmarkReporter::Run::benchmark_name() const {
8588
std::string name = run_name;

src/sysinfo.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,30 @@ 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 = 32767;
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+
const unsigned COUNT = 64;
386+
char hostname[COUNT];
387+
int retVal = gethostname(hostname, COUNT);
388+
if (retVal != 0) return std::string("Unable to Get Host Name");
389+
return std::string(hostname);
390+
#endif
391+
}
392+
369393
int GetNumCPUs() {
370394
#ifdef BENCHMARK_HAS_SYSCTL
371395
int NumCPU = -1;
@@ -609,4 +633,11 @@ CPUInfo::CPUInfo()
609633
scaling_enabled(CpuScalingEnabled(num_cpus)),
610634
load_avg(GetLoadAvg()) {}
611635

636+
637+
const SystemInformation& SystemInformation::Get() {
638+
static const SystemInformation* info = new SystemInformation();
639+
return *info;
640+
}
641+
642+
SystemInformation::SystemInformation() : name(GetSystemName()) {}
612643
} // end namespace benchmark

test/reporter_output_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static int AddContextCases() {
1616
AddCases(TC_ConsoleErr,
1717
{
1818
{"%int[-/]%int[-/]%int %int:%int:%int$", MR_Default},
19+
{"System Name", MR_Next},
1920
{"Running .*/reporter_output_test(\\.exe)?$", MR_Next},
2021
{"Run on \\(%int X %float MHz CPU s?\\)", MR_Next},
2122
});

0 commit comments

Comments
 (0)