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