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