1 | // Copyright 2015 Google Inc. All rights reserved. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | #include <cstdlib> |
16 | #include <iostream> |
17 | #include <map> |
18 | #include <string> |
19 | #include <tuple> |
20 | #include <vector> |
21 | |
22 | #include "benchmark/benchmark.h" |
23 | #include "check.h" |
24 | #include "string_util.h" |
25 | #include "timers.h" |
26 | |
27 | namespace benchmark { |
28 | namespace internal { |
29 | extern std::map<std::string, std::string> *global_context; |
30 | } |
31 | |
32 | BenchmarkReporter::BenchmarkReporter() |
33 | : output_stream_(&std::cout), error_stream_(&std::cerr) {} |
34 | |
35 | BenchmarkReporter::~BenchmarkReporter() {} |
36 | |
37 | void BenchmarkReporter::PrintBasicContext(std::ostream *out, |
38 | Context const &context) { |
39 | BM_CHECK(out) << "cannot be null" ; |
40 | auto &Out = *out; |
41 | |
42 | Out << LocalDateTimeString() << "\n" ; |
43 | |
44 | if (context.executable_name) |
45 | Out << "Running " << context.executable_name << "\n" ; |
46 | |
47 | const CPUInfo &info = context.cpu_info; |
48 | Out << "Run on (" << info.num_cpus << " X " |
49 | << (info.cycles_per_second / 1000000.0) << " MHz CPU " |
50 | << ((info.num_cpus > 1) ? "s" : "" ) << ")\n" ; |
51 | if (info.caches.size() != 0) { |
52 | Out << "CPU Caches:\n" ; |
53 | for (auto &CInfo : info.caches) { |
54 | Out << " L" << CInfo.level << " " << CInfo.type << " " |
55 | << (CInfo.size / 1024) << " KiB" ; |
56 | if (CInfo.num_sharing != 0) |
57 | Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")" ; |
58 | Out << "\n" ; |
59 | } |
60 | } |
61 | if (!info.load_avg.empty()) { |
62 | Out << "Load Average: " ; |
63 | for (auto It = info.load_avg.begin(); It != info.load_avg.end();) { |
64 | Out << StrFormat("%.2f" , *It++); |
65 | if (It != info.load_avg.end()) Out << ", " ; |
66 | } |
67 | Out << "\n" ; |
68 | } |
69 | |
70 | if (internal::global_context != nullptr) { |
71 | for (const auto &kv : *internal::global_context) { |
72 | Out << kv.first << ": " << kv.second << "\n" ; |
73 | } |
74 | } |
75 | |
76 | if (CPUInfo::Scaling::ENABLED == info.scaling) { |
77 | Out << "***WARNING*** CPU scaling is enabled, the benchmark " |
78 | "real time measurements may be noisy and will incur extra " |
79 | "overhead.\n" ; |
80 | } |
81 | |
82 | #ifndef NDEBUG |
83 | Out << "***WARNING*** Library was built as DEBUG. Timings may be " |
84 | "affected.\n" ; |
85 | #endif |
86 | } |
87 | |
88 | // No initializer because it's already initialized to NULL. |
89 | const char *BenchmarkReporter::Context::executable_name; |
90 | |
91 | BenchmarkReporter::Context::Context() |
92 | : cpu_info(CPUInfo::Get()), sys_info(SystemInfo::Get()) {} |
93 | |
94 | std::string BenchmarkReporter::Run::benchmark_name() const { |
95 | std::string name = run_name.str(); |
96 | if (run_type == RT_Aggregate) { |
97 | name += "_" + aggregate_name; |
98 | } |
99 | return name; |
100 | } |
101 | |
102 | double BenchmarkReporter::Run::GetAdjustedRealTime() const { |
103 | double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit); |
104 | if (iterations != 0) new_time /= static_cast<double>(iterations); |
105 | return new_time; |
106 | } |
107 | |
108 | double BenchmarkReporter::Run::GetAdjustedCPUTime() const { |
109 | double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit); |
110 | if (iterations != 0) new_time /= static_cast<double>(iterations); |
111 | return new_time; |
112 | } |
113 | |
114 | } // end namespace benchmark |
115 | |