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 <tuple>
22 #include <vector>
23 
24 #include "check.h"
25 
26 namespace benchmark {
27 
BenchmarkReporter()28 BenchmarkReporter::BenchmarkReporter()
29     : output_stream_(&std::cout), error_stream_(&std::cerr) {}
30 
~BenchmarkReporter()31 BenchmarkReporter::~BenchmarkReporter() {}
32 
PrintBasicContext(std::ostream * out,Context const & context)33 void BenchmarkReporter::PrintBasicContext(std::ostream *out,
34                                           Context const &context) {
35   CHECK(out) << "cannot be null";
36   auto &Out = *out;
37 
38   Out << LocalDateTimeString() << "\n";
39 
40   if (context.executable_name)
41     Out << "Running " << context.executable_name << "\n";
42 
43   const CPUInfo &info = context.cpu_info;
44   Out << "Run on (" << info.num_cpus << " X "
45       << (info.cycles_per_second / 1000000.0) << " MHz CPU "
46       << ((info.num_cpus > 1) ? "s" : "") << ")\n";
47   if (info.caches.size() != 0) {
48     Out << "CPU Caches:\n";
49     for (auto &CInfo : info.caches) {
50       Out << "  L" << CInfo.level << " " << CInfo.type << " "
51           << (CInfo.size / 1000) << "K";
52       if (CInfo.num_sharing != 0)
53         Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")";
54       Out << "\n";
55     }
56   }
57 
58   if (info.scaling_enabled) {
59     Out << "***WARNING*** CPU scaling is enabled, the benchmark "
60            "real time measurements may be noisy and will incur extra "
61            "overhead.\n";
62   }
63 
64 #ifndef NDEBUG
65   Out << "***WARNING*** Library was built as DEBUG. Timings may be "
66          "affected.\n";
67 #endif
68 }
69 
70 // No initializer because it's already initialized to NULL.
71 const char* BenchmarkReporter::Context::executable_name;
72 
Context()73 BenchmarkReporter::Context::Context() : cpu_info(CPUInfo::Get()) {}
74 
GetAdjustedRealTime() const75 double BenchmarkReporter::Run::GetAdjustedRealTime() const {
76   double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit);
77   if (iterations != 0) new_time /= static_cast<double>(iterations);
78   return new_time;
79 }
80 
GetAdjustedCPUTime() const81 double BenchmarkReporter::Run::GetAdjustedCPUTime() const {
82   double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit);
83   if (iterations != 0) new_time /= static_cast<double>(iterations);
84   return new_time;
85 }
86 
87 }  // end namespace benchmark
88