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