blob: 076bc31a2eccc2adf9a47764a5ca75fd2b0aa6a6 [file] [log] [blame]
Eric Fiselierb08d8b12016-07-19 23:07:031// 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 Fiselierb08d8b12016-07-19 23:07:0315#include <cstdlib>
Eric Fiselierb08d8b12016-07-19 23:07:0316#include <iostream>
Louis Dionne5208ec52021-07-12 17:15:3417#include <map>
18#include <string>
Eric Fiselierb08d8b12016-07-19 23:07:0319#include <tuple>
Eric Fiselierfbc9ff22016-11-05 00:30:2720#include <vector>
Eric Fiselierb08d8b12016-07-19 23:07:0321
Mircea Trofina2907702021-12-14 00:02:0222#include "benchmark/benchmark.h"
Eric Fiselierb08d8b12016-07-19 23:07:0323#include "check.h"
Eric Fiselier782a15a2018-11-15 19:22:5324#include "string_util.h"
Mircea Trofina2907702021-12-14 00:02:0225#include "timers.h"
Eric Fiselierb08d8b12016-07-19 23:07:0326
27namespace benchmark {
Eric Fiselierb08d8b12016-07-19 23:07:0328
29BenchmarkReporter::BenchmarkReporter()
Eric Fiselierfbc9ff22016-11-05 00:30:2730 : output_stream_(&std::cout), error_stream_(&std::cerr) {}
Eric Fiselierb08d8b12016-07-19 23:07:0331
Eric Fiselierfbc9ff22016-11-05 00:30:2732BenchmarkReporter::~BenchmarkReporter() {}
Eric Fiselierb08d8b12016-07-19 23:07:0333
Eric Fiselier19039762018-01-18 04:23:0134void BenchmarkReporter::PrintBasicContext(std::ostream *out,
Eric Fiselierb08d8b12016-07-19 23:07:0335 Context const &context) {
Mircea Trofina2907702021-12-14 00:02:0236 BM_CHECK(out) << "cannot be null";
Eric Fiselier19039762018-01-18 04:23:0137 auto &Out = *out;
Eric Fiselierb08d8b12016-07-19 23:07:0338
Mircea Trofina5b79712024-03-04 22:11:3039#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 Fiselierb08d8b12016-07-19 23:07:0342 Out << LocalDateTimeString() << "\n";
Mircea Trofina5b79712024-03-04 22:11:3043#endif
Eric Fiselierb08d8b12016-07-19 23:07:0344
Eric Fiselierfcafd3e2018-07-10 04:02:0045 if (context.executable_name)
46 Out << "Running " << context.executable_name << "\n";
47
Eric Fiselier19039762018-01-18 04:23:0148 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 Dionne5208ec52021-07-12 17:15:3456 << (CInfo.size / 1024) << " KiB";
Eric Fiselier19039762018-01-18 04:23:0157 if (CInfo.num_sharing != 0)
58 Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")";
59 Out << "\n";
60 }
61 }
Eric Fiselier782a15a2018-11-15 19:22:5362 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 Fiselier19039762018-01-18 04:23:0170
Mircea Trofina5b79712024-03-04 22:11:3071 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 Dionne5208ec52021-07-12 17:15:3476 Out << kv.first << ": " << kv.second << "\n";
77 }
78 }
79
80 if (CPUInfo::Scaling::ENABLED == info.scaling) {
Eric Fiselierb08d8b12016-07-19 23:07:0381 Out << "***WARNING*** CPU scaling is enabled, the benchmark "
Eric Fiselierfbc9ff22016-11-05 00:30:2782 "real time measurements may be noisy and will incur extra "
83 "overhead.\n";
Eric Fiselierb08d8b12016-07-19 23:07:0384 }
85
86#ifndef NDEBUG
87 Out << "***WARNING*** Library was built as DEBUG. Timings may be "
Eric Fiselierfbc9ff22016-11-05 00:30:2788 "affected.\n";
Eric Fiselierb08d8b12016-07-19 23:07:0389#endif
90}
91
Eric Fiselierfcafd3e2018-07-10 04:02:0092// No initializer because it's already initialized to NULL.
93const char *BenchmarkReporter::Context::executable_name;
94
Eric Fiselier7a91ac42018-12-14 03:37:1395BenchmarkReporter::Context::Context()
96 : cpu_info(CPUInfo::Get()), sys_info(SystemInfo::Get()) {}
Eric Fiselier19039762018-01-18 04:23:0197
Eric Fiselier782a15a2018-11-15 19:22:5398std::string BenchmarkReporter::Run::benchmark_name() const {
Louis Dionne5208ec52021-07-12 17:15:3499 std::string name = run_name.str();
Eric Fiselier782a15a2018-11-15 19:22:53100 if (run_type == RT_Aggregate) {
101 name += "_" + aggregate_name;
102 }
103 return name;
104}
105
Eric Fiselierb08d8b12016-07-19 23:07:03106double BenchmarkReporter::Run::GetAdjustedRealTime() const {
107 double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit);
Eric Fiselierfbc9ff22016-11-05 00:30:27108 if (iterations != 0) new_time /= static_cast<double>(iterations);
Eric Fiselierb08d8b12016-07-19 23:07:03109 return new_time;
110}
111
112double BenchmarkReporter::Run::GetAdjustedCPUTime() const {
113 double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit);
Eric Fiselierfbc9ff22016-11-05 00:30:27114 if (iterations != 0) new_time /= static_cast<double>(iterations);
Eric Fiselierb08d8b12016-07-19 23:07:03115 return new_time;
116}
117
Eric Fiselierfbc9ff22016-11-05 00:30:27118} // end namespace benchmark