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 "benchmark/benchmark.h" |
16 | #include "timers.h" |
17 | |
18 | #include <cstdlib> |
19 | |
20 | #include <iostream> |
21 | #include <map> |
22 | #include <string> |
23 | #include <tuple> |
24 | #include <vector> |
25 | |
26 | #include "check.h" |
27 | #include "string_util.h" |
28 | |
29 | namespace benchmark { |
30 | namespace internal { |
31 | extern std::map<std::string, std::string>* global_context; |
32 | } |
33 | |
34 | BenchmarkReporter::BenchmarkReporter() |
35 | : output_stream_(&std::cout), error_stream_(&std::cerr) {} |
36 | |
37 | BenchmarkReporter::~BenchmarkReporter() {} |
38 | |
39 | void BenchmarkReporter::PrintBasicContext(std::ostream *out, |
40 | Context const &context) { |
41 | CHECK(out) << "cannot be null" ; |
42 | auto &Out = *out; |
43 | |
44 | Out << LocalDateTimeString() << "\n" ; |
45 | |
46 | if (context.executable_name) |
47 | Out << "Running " << context.executable_name << "\n" ; |
48 | |
49 | const CPUInfo &info = context.cpu_info; |
50 | Out << "Run on (" << info.num_cpus << " X " |
51 | << (info.cycles_per_second / 1000000.0) << " MHz CPU " |
52 | << ((info.num_cpus > 1) ? "s" : "" ) << ")\n" ; |
53 | if (info.caches.size() != 0) { |
54 | Out << "CPU Caches:\n" ; |
55 | for (auto &CInfo : info.caches) { |
56 | Out << " L" << CInfo.level << " " << CInfo.type << " " |
57 | << (CInfo.size / 1024) << " KiB" ; |
58 | if (CInfo.num_sharing != 0) |
59 | Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")" ; |
60 | Out << "\n" ; |
61 | } |
62 | } |
63 | if (!info.load_avg.empty()) { |
64 | Out << "Load Average: " ; |
65 | for (auto It = info.load_avg.begin(); It != info.load_avg.end();) { |
66 | Out << StrFormat("%.2f" , *It++); |
67 | if (It != info.load_avg.end()) Out << ", " ; |
68 | } |
69 | Out << "\n" ; |
70 | } |
71 | |
72 | if (internal::global_context != nullptr) { |
73 | for (const auto& kv: *internal::global_context) { |
74 | Out << kv.first << ": " << kv.second << "\n" ; |
75 | } |
76 | } |
77 | |
78 | if (CPUInfo::Scaling::ENABLED == info.scaling) { |
79 | Out << "***WARNING*** CPU scaling is enabled, the benchmark " |
80 | "real time measurements may be noisy and will incur extra " |
81 | "overhead.\n" ; |
82 | } |
83 | |
84 | #ifndef NDEBUG |
85 | Out << "***WARNING*** Library was built as DEBUG. Timings may be " |
86 | "affected.\n" ; |
87 | #endif |
88 | } |
89 | |
90 | // No initializer because it's already initialized to NULL. |
91 | const char *BenchmarkReporter::Context::executable_name; |
92 | |
93 | BenchmarkReporter::Context::Context() |
94 | : cpu_info(CPUInfo::Get()), sys_info(SystemInfo::Get()) {} |
95 | |
96 | std::string BenchmarkReporter::Run::benchmark_name() const { |
97 | std::string name = run_name.str(); |
98 | if (run_type == RT_Aggregate) { |
99 | name += "_" + aggregate_name; |
100 | } |
101 | return name; |
102 | } |
103 | |
104 | double BenchmarkReporter::Run::GetAdjustedRealTime() const { |
105 | double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit); |
106 | if (iterations != 0) new_time /= static_cast<double>(iterations); |
107 | return new_time; |
108 | } |
109 | |
110 | double BenchmarkReporter::Run::GetAdjustedCPUTime() const { |
111 | double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit); |
112 | if (iterations != 0) new_time /= static_cast<double>(iterations); |
113 | return new_time; |
114 | } |
115 | |
116 | } // end namespace benchmark |
117 | |