blob: 87dcfb4666197b1390c47480550069d50d2aaf9f [file] [log] [blame]
Eric Fiselier5cfd6bc2015-03-09 16:15:05 -04001// Copyright 2015 Google Inc. All rights reserved.
Dominic Hamon78fa0b92014-01-09 12:16:51 -08002//
Dominic Hamon3fb82682014-01-09 08:01:34 -08003// 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
Dominic Hamon78fa0b92014-01-09 12:16:51 -08006//
Dominic Hamon3fb82682014-01-09 08:01:34 -08007// http://www.apache.org/licenses/LICENSE-2.0
Dominic Hamon78fa0b92014-01-09 12:16:51 -08008//
Dominic Hamon3fb82682014-01-09 08:01:34 -08009// 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
Matt Clarksondd613912015-03-27 13:41:50 +000015#include "internal_macros.h"
Dominic Hamon403f3542013-12-18 16:55:45 -080016
Anton Danielssonba141ac2015-10-05 13:58:35 +020017#ifdef BENCHMARK_OS_WINDOWS
Martin Storsjö52613072018-09-18 11:42:20 +030018#include <shlwapi.h>
Wink Saville61497232018-03-07 03:20:06 -080019#undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA
Martin Storsjö52613072018-09-18 11:42:20 +030020#include <versionhelpers.h>
21#include <windows.h>
Dominic Hamonfcef4fb2021-11-10 16:04:32 +000022
Jatin Chaudhary47a5f772018-12-11 16:53:02 +053023#include <codecvt>
Matt Clarksondd613912015-03-27 13:41:50 +000024#else
Dominic Hamon403f3542013-12-18 16:55:45 -080025#include <fcntl.h>
Ian McKellar6ecf8a82018-02-14 13:17:12 -080026#ifndef BENCHMARK_OS_FUCHSIA
Dominic Hamon403f3542013-12-18 16:55:45 -080027#include <sys/resource.h>
Ian McKellar6ecf8a82018-02-14 13:17:12 -080028#endif
Dominic Hamon403f3542013-12-18 16:55:45 -080029#include <sys/time.h>
Dominic Hamon332f6772016-10-07 11:35:03 -070030#include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD
Dominic Hamon403f3542013-12-18 16:55:45 -080031#include <unistd.h>
Eric27e0b432017-11-22 09:33:52 -070032#if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX || \
Michael Neumannaf729112020-10-12 22:41:49 +020033 defined BENCHMARK_OS_NETBSD || defined BENCHMARK_OS_OPENBSD || \
34 defined BENCHMARK_OS_DRAGONFLY
Eric27e0b432017-11-22 09:33:52 -070035#define BENCHMARK_HAS_SYSCTL
Anton Danielsson0d35f5f2015-10-05 10:24:12 +020036#include <sys/sysctl.h>
37#endif
Matt Clarksondd613912015-03-27 13:41:50 +000038#endif
alekseyshl47df49e2018-03-02 03:53:58 -080039#if defined(BENCHMARK_OS_SOLARIS)
40#include <kstat.h>
41#endif
Jilin Zhoud205ead2019-02-28 05:42:44 -050042#if defined(BENCHMARK_OS_QNX)
43#include <sys/syspage.h>
44#endif
Dominic Hamon403f3542013-12-18 16:55:45 -080045
Eric27e0b432017-11-22 09:33:52 -070046#include <algorithm>
Eric11dc3682017-11-26 13:33:01 -070047#include <array>
48#include <bitset>
Eric Fiseliera187aa02015-03-06 17:01:05 -050049#include <cerrno>
Eric11dc3682017-11-26 13:33:01 -070050#include <climits>
Eric Fiseliera187aa02015-03-06 17:01:05 -050051#include <cstdint>
Dominic Hamon332f6772016-10-07 11:35:03 -070052#include <cstdio>
Eric Fiseliera187aa02015-03-06 17:01:05 -050053#include <cstdlib>
54#include <cstring>
Eric27e0b432017-11-22 09:33:52 -070055#include <fstream>
Dominic Hamon403f3542013-12-18 16:55:45 -080056#include <iostream>
Eric27e0b432017-11-22 09:33:52 -070057#include <iterator>
Dominic Hamon403f3542013-12-18 16:55:45 -080058#include <limits>
Dominic Hamonfcef4fb2021-11-10 16:04:32 +000059#include <locale>
Eric27e0b432017-11-22 09:33:52 -070060#include <memory>
61#include <sstream>
Alexander Enaldiev99010112020-07-28 14:46:07 +030062#include <utility>
Dominic Hamon403f3542013-12-18 16:55:45 -080063
Dominic Hamona3b5e442014-10-31 21:54:55 -070064#include "check.h"
Dominic Hamon403f3542013-12-18 16:55:45 -080065#include "cycleclock.h"
Eric Fiseliera187aa02015-03-06 17:01:05 -050066#include "internal_macros.h"
Eric Fiselier7a767012015-03-12 18:03:33 -040067#include "log.h"
Dominic Hamon403f3542013-12-18 16:55:45 -080068#include "sleep.h"
Eric Fiseliera3308c62015-03-26 14:26:07 -040069#include "string_util.h"
Dominic Hamon403f3542013-12-18 16:55:45 -080070
Dominic Hamone390e4e2013-12-19 16:18:09 -080071namespace benchmark {
Dominic Hamon403f3542013-12-18 16:55:45 -080072namespace {
Dominic Hamon403f3542013-12-18 16:55:45 -080073
Eric27e0b432017-11-22 09:33:52 -070074void PrintImp(std::ostream& out) { out << std::endl; }
Lei Xu3460bf12014-07-24 23:57:09 -070075
Eric27e0b432017-11-22 09:33:52 -070076template <class First, class... Rest>
77void PrintImp(std::ostream& out, First&& f, Rest&&... rest) {
78 out << std::forward<First>(f);
79 PrintImp(out, std::forward<Rest>(rest)...);
80}
81
82template <class... Args>
83BENCHMARK_NORETURN void PrintErrorAndDie(Args&&... args) {
84 PrintImp(std::cerr, std::forward<Args>(args)...);
85 std::exit(EXIT_FAILURE);
86}
87
88#ifdef BENCHMARK_HAS_SYSCTL
89
Eric27e0b432017-11-22 09:33:52 -070090/// ValueUnion - A type used to correctly alias the byte-for-byte output of
91/// `sysctl` with the result type it's to be interpreted as.
92struct ValueUnion {
93 union DataT {
94 uint32_t uint32_value;
95 uint64_t uint64_value;
Victor Costan95a14352017-11-30 08:05:38 -080096 // For correct aliasing of union members from bytes.
97 char bytes[8];
Eric27e0b432017-11-22 09:33:52 -070098 };
99 using DataPtr = std::unique_ptr<DataT, decltype(&std::free)>;
100
101 // The size of the data union member + its trailing array size.
102 size_t Size;
103 DataPtr Buff;
104
105 public:
106 ValueUnion() : Size(0), Buff(nullptr, &std::free) {}
107
108 explicit ValueUnion(size_t BuffSize)
109 : Size(sizeof(DataT) + BuffSize),
110 Buff(::new (std::malloc(Size)) DataT(), &std::free) {}
111
112 ValueUnion(ValueUnion&& other) = default;
113
114 explicit operator bool() const { return bool(Buff); }
115
116 char* data() const { return Buff->bytes; }
117
118 std::string GetAsString() const { return std::string(data()); }
119
120 int64_t GetAsInteger() const {
121 if (Size == sizeof(Buff->uint32_value))
122 return static_cast<int32_t>(Buff->uint32_value);
123 else if (Size == sizeof(Buff->uint64_value))
124 return static_cast<int64_t>(Buff->uint64_value);
125 BENCHMARK_UNREACHABLE();
126 }
127
128 uint64_t GetAsUnsigned() const {
129 if (Size == sizeof(Buff->uint32_value))
130 return Buff->uint32_value;
131 else if (Size == sizeof(Buff->uint64_value))
132 return Buff->uint64_value;
133 BENCHMARK_UNREACHABLE();
134 }
Eric11dc3682017-11-26 13:33:01 -0700135
136 template <class T, int N>
137 std::array<T, N> GetAsArray() {
138 const int ArrSize = sizeof(T) * N;
Dominic Hamon6a5bf082021-06-24 18:21:59 +0100139 BM_CHECK_LE(ArrSize, Size);
Eric11dc3682017-11-26 13:33:01 -0700140 std::array<T, N> Arr;
141 std::memcpy(Arr.data(), data(), ArrSize);
142 return Arr;
143 }
Eric27e0b432017-11-22 09:33:52 -0700144};
145
Eric27e0b432017-11-22 09:33:52 -0700146ValueUnion GetSysctlImp(std::string const& Name) {
Nan Xiaoea5551e2018-05-02 18:26:43 +0800147#if defined BENCHMARK_OS_OPENBSD
148 int mib[2];
149
150 mib[0] = CTL_HW;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000151 if ((Name == "hw.ncpu") || (Name == "hw.cpuspeed")) {
Nan Xiaoea5551e2018-05-02 18:26:43 +0800152 ValueUnion buff(sizeof(int));
153
154 if (Name == "hw.ncpu") {
155 mib[1] = HW_NCPU;
156 } else {
157 mib[1] = HW_CPUSPEED;
158 }
159
160 if (sysctl(mib, 2, buff.data(), &buff.Size, nullptr, 0) == -1) {
161 return ValueUnion();
162 }
163 return buff;
164 }
165 return ValueUnion();
166#else
Eric27e0b432017-11-22 09:33:52 -0700167 size_t CurBuffSize = 0;
168 if (sysctlbyname(Name.c_str(), nullptr, &CurBuffSize, nullptr, 0) == -1)
169 return ValueUnion();
170
171 ValueUnion buff(CurBuffSize);
172 if (sysctlbyname(Name.c_str(), buff.data(), &buff.Size, nullptr, 0) == 0)
173 return buff;
174 return ValueUnion();
Nan Xiaoea5551e2018-05-02 18:26:43 +0800175#endif
Eric27e0b432017-11-22 09:33:52 -0700176}
177
178BENCHMARK_MAYBE_UNUSED
179bool GetSysctl(std::string const& Name, std::string* Out) {
180 Out->clear();
181 auto Buff = GetSysctlImp(Name);
182 if (!Buff) return false;
183 Out->assign(Buff.data());
184 return true;
185}
186
187template <class Tp,
188 class = typename std::enable_if<std::is_integral<Tp>::value>::type>
189bool GetSysctl(std::string const& Name, Tp* Out) {
190 *Out = 0;
191 auto Buff = GetSysctlImp(Name);
192 if (!Buff) return false;
193 *Out = static_cast<Tp>(Buff.GetAsUnsigned());
194 return true;
Dominic Hamon403f3542013-12-18 16:55:45 -0800195}
Eric11dc3682017-11-26 13:33:01 -0700196
197template <class Tp, size_t N>
198bool GetSysctl(std::string const& Name, std::array<Tp, N>* Out) {
199 auto Buff = GetSysctlImp(Name);
200 if (!Buff) return false;
201 *Out = Buff.GetAsArray<Tp, N>();
202 return true;
Victor Costan95a14352017-11-30 08:05:38 -0800203}
Dominic Hamone2633b92014-01-16 09:13:18 -0800204#endif
Dominic Hamon403f3542013-12-18 16:55:45 -0800205
Eric27e0b432017-11-22 09:33:52 -0700206template <class ArgT>
207bool ReadFromFile(std::string const& fname, ArgT* arg) {
208 *arg = ArgT();
209 std::ifstream f(fname.c_str());
210 if (!f.is_open()) return false;
211 f >> *arg;
212 return f.good();
213}
214
Alexander Enaldiev99010112020-07-28 14:46:07 +0300215CPUInfo::Scaling CpuScaling(int num_cpus) {
Eric27e0b432017-11-22 09:33:52 -0700216 // We don't have a valid CPU count, so don't even bother.
Alexander Enaldiev99010112020-07-28 14:46:07 +0300217 if (num_cpus <= 0) return CPUInfo::Scaling::UNKNOWN;
Nico Weber8fd49d62021-08-19 09:09:49 -0400218#if defined(BENCHMARK_OS_QNX)
Alexander Enaldiev99010112020-07-28 14:46:07 +0300219 return CPUInfo::Scaling::UNKNOWN;
Nico Weber8fd49d62021-08-19 09:09:49 -0400220#elif !defined(BENCHMARK_OS_WINDOWS)
Eric27e0b432017-11-22 09:33:52 -0700221 // On Linux, the CPUfreq subsystem exposes CPU information as files on the
222 // local file system. If reading the exported files fails, then we may not be
223 // running on Linux, so we silently ignore all the read errors.
224 std::string res;
225 for (int cpu = 0; cpu < num_cpus; ++cpu) {
226 std::string governor_file =
227 StrCat("/sys/devices/system/cpu/cpu", cpu, "/cpufreq/scaling_governor");
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000228 if (ReadFromFile(governor_file, &res) && res != "performance")
229 return CPUInfo::Scaling::ENABLED;
Eric27e0b432017-11-22 09:33:52 -0700230 }
Alexander Enaldiev99010112020-07-28 14:46:07 +0300231 return CPUInfo::Scaling::DISABLED;
Nico Weber8fd49d62021-08-19 09:09:49 -0400232#else
Alexander Enaldiev99010112020-07-28 14:46:07 +0300233 return CPUInfo::Scaling::UNKNOWN;
Nico Weber8fd49d62021-08-19 09:09:49 -0400234#endif
Eric27e0b432017-11-22 09:33:52 -0700235}
236
Eric11dc3682017-11-26 13:33:01 -0700237int CountSetBitsInCPUMap(std::string Val) {
238 auto CountBits = [](std::string Part) {
239 using CPUMask = std::bitset<sizeof(std::uintptr_t) * CHAR_BIT>;
240 Part = "0x" + Part;
Marat Dukhan7fb3c562018-06-05 03:36:26 -0700241 CPUMask Mask(benchmark::stoul(Part, nullptr, 16));
Eric11dc3682017-11-26 13:33:01 -0700242 return static_cast<int>(Mask.count());
243 };
244 size_t Pos;
245 int total = 0;
246 while ((Pos = Val.find(',')) != std::string::npos) {
247 total += CountBits(Val.substr(0, Pos));
248 Val = Val.substr(Pos + 1);
249 }
250 if (!Val.empty()) {
251 total += CountBits(Val);
252 }
253 return total;
254}
255
Eric27e0b432017-11-22 09:33:52 -0700256BENCHMARK_MAYBE_UNUSED
257std::vector<CPUInfo::CacheInfo> GetCacheSizesFromKVFS() {
258 std::vector<CPUInfo::CacheInfo> res;
259 std::string dir = "/sys/devices/system/cpu/cpu0/cache/";
260 int Idx = 0;
261 while (true) {
262 CPUInfo::CacheInfo info;
263 std::string FPath = StrCat(dir, "index", Idx++, "/");
264 std::ifstream f(StrCat(FPath, "size").c_str());
265 if (!f.is_open()) break;
266 std::string suffix;
267 f >> info.size;
268 if (f.fail())
269 PrintErrorAndDie("Failed while reading file '", FPath, "size'");
270 if (f.good()) {
271 f >> suffix;
272 if (f.bad())
273 PrintErrorAndDie(
274 "Invalid cache size format: failed to read size suffix");
275 else if (f && suffix != "K")
276 PrintErrorAndDie("Invalid cache size format: Expected bytes ", suffix);
277 else if (suffix == "K")
Roman Lebedev36711942019-12-02 12:29:16 +0300278 info.size *= 1024;
Dominic Hamon403f3542013-12-18 16:55:45 -0800279 }
Eric27e0b432017-11-22 09:33:52 -0700280 if (!ReadFromFile(StrCat(FPath, "type"), &info.type))
281 PrintErrorAndDie("Failed to read from file ", FPath, "type");
282 if (!ReadFromFile(StrCat(FPath, "level"), &info.level))
283 PrintErrorAndDie("Failed to read from file ", FPath, "level");
Eric11dc3682017-11-26 13:33:01 -0700284 std::string map_str;
285 if (!ReadFromFile(StrCat(FPath, "shared_cpu_map"), &map_str))
286 PrintErrorAndDie("Failed to read from file ", FPath, "shared_cpu_map");
287 info.num_sharing = CountSetBitsInCPUMap(map_str);
Eric27e0b432017-11-22 09:33:52 -0700288 res.push_back(info);
Dominic Hamon403f3542013-12-18 16:55:45 -0800289 }
Eric27e0b432017-11-22 09:33:52 -0700290
291 return res;
292}
293
294#ifdef BENCHMARK_OS_MACOSX
295std::vector<CPUInfo::CacheInfo> GetCacheSizesMacOSX() {
296 std::vector<CPUInfo::CacheInfo> res;
Eric11dc3682017-11-26 13:33:01 -0700297 std::array<uint64_t, 4> CacheCounts{{0, 0, 0, 0}};
298 GetSysctl("hw.cacheconfig", &CacheCounts);
299
Eric27e0b432017-11-22 09:33:52 -0700300 struct {
301 std::string name;
302 std::string type;
303 int level;
Roman Lebedevf0901412018-09-05 14:20:18 +0300304 uint64_t num_sharing;
Eric11dc3682017-11-26 13:33:01 -0700305 } Cases[] = {{"hw.l1dcachesize", "Data", 1, CacheCounts[1]},
306 {"hw.l1icachesize", "Instruction", 1, CacheCounts[1]},
307 {"hw.l2cachesize", "Unified", 2, CacheCounts[2]},
308 {"hw.l3cachesize", "Unified", 3, CacheCounts[3]}};
Eric27e0b432017-11-22 09:33:52 -0700309 for (auto& C : Cases) {
310 int val;
311 if (!GetSysctl(C.name, &val)) continue;
312 CPUInfo::CacheInfo info;
313 info.type = C.type;
314 info.level = C.level;
315 info.size = val;
Eric11dc3682017-11-26 13:33:01 -0700316 info.num_sharing = static_cast<int>(C.num_sharing);
Eric27e0b432017-11-22 09:33:52 -0700317 res.push_back(std::move(info));
318 }
319 return res;
Dominic Hamon403f3542013-12-18 16:55:45 -0800320}
Eric11dc3682017-11-26 13:33:01 -0700321#elif defined(BENCHMARK_OS_WINDOWS)
322std::vector<CPUInfo::CacheInfo> GetCacheSizesWindows() {
323 std::vector<CPUInfo::CacheInfo> res;
324 DWORD buffer_size = 0;
325 using PInfo = SYSTEM_LOGICAL_PROCESSOR_INFORMATION;
326 using CInfo = CACHE_DESCRIPTOR;
327
328 using UPtr = std::unique_ptr<PInfo, decltype(&std::free)>;
329 GetLogicalProcessorInformation(nullptr, &buffer_size);
330 UPtr buff((PInfo*)malloc(buffer_size), &std::free);
331 if (!GetLogicalProcessorInformation(buff.get(), &buffer_size))
332 PrintErrorAndDie("Failed during call to GetLogicalProcessorInformation: ",
333 GetLastError());
334
335 PInfo* it = buff.get();
336 PInfo* end = buff.get() + (buffer_size / sizeof(PInfo));
337
338 for (; it != end; ++it) {
339 if (it->Relationship != RelationCache) continue;
340 using BitSet = std::bitset<sizeof(ULONG_PTR) * CHAR_BIT>;
341 BitSet B(it->ProcessorMask);
342 // To prevent duplicates, only consider caches where CPU 0 is specified
343 if (!B.test(0)) continue;
344 CInfo* Cache = &it->Cache;
345 CPUInfo::CacheInfo C;
oskidan4fe02062018-01-19 19:17:01 +0200346 C.num_sharing = static_cast<int>(B.count());
Eric11dc3682017-11-26 13:33:01 -0700347 C.level = Cache->Level;
348 C.size = Cache->Size;
Martin Storsjöb0006722021-12-09 11:24:54 +0200349 C.type = "Unknown";
Eric11dc3682017-11-26 13:33:01 -0700350 switch (Cache->Type) {
351 case CacheUnified:
352 C.type = "Unified";
353 break;
354 case CacheInstruction:
355 C.type = "Instruction";
356 break;
357 case CacheData:
358 C.type = "Data";
359 break;
360 case CacheTrace:
361 C.type = "Trace";
362 break;
Eric11dc3682017-11-26 13:33:01 -0700363 }
364 res.push_back(C);
365 }
366 return res;
367}
Jilin Zhoud205ead2019-02-28 05:42:44 -0500368#elif BENCHMARK_OS_QNX
369std::vector<CPUInfo::CacheInfo> GetCacheSizesQNX() {
370 std::vector<CPUInfo::CacheInfo> res;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000371 struct cacheattr_entry* cache = SYSPAGE_ENTRY(cacheattr);
Jilin Zhoud205ead2019-02-28 05:42:44 -0500372 uint32_t const elsize = SYSPAGE_ELEMENT_SIZE(cacheattr);
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000373 int num = SYSPAGE_ENTRY_SIZE(cacheattr) / elsize;
374 for (int i = 0; i < num; ++i) {
Jilin Zhoud205ead2019-02-28 05:42:44 -0500375 CPUInfo::CacheInfo info;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000376 switch (cache->flags) {
377 case CACHE_FLAG_INSTR:
Jilin Zhoud205ead2019-02-28 05:42:44 -0500378 info.type = "Instruction";
379 info.level = 1;
380 break;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000381 case CACHE_FLAG_DATA:
Jilin Zhoud205ead2019-02-28 05:42:44 -0500382 info.type = "Data";
383 info.level = 1;
384 break;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000385 case CACHE_FLAG_UNIFIED:
Jilin Zhoud205ead2019-02-28 05:42:44 -0500386 info.type = "Unified";
387 info.level = 2;
Dominic Hamon1302d2c2020-07-30 09:51:48 +0100388 break;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000389 case CACHE_FLAG_SHARED:
Jilin Zhoud205ead2019-02-28 05:42:44 -0500390 info.type = "Shared";
391 info.level = 3;
Dominic Hamon1302d2c2020-07-30 09:51:48 +0100392 break;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000393 default:
Jilin Zhoud205ead2019-02-28 05:42:44 -0500394 continue;
395 break;
396 }
397 info.size = cache->line_size * cache->num_lines;
398 info.num_sharing = 0;
399 res.push_back(std::move(info));
400 cache = SYSPAGE_ARRAY_ADJ_OFFSET(cacheattr, cache, elsize);
401 }
402 return res;
403}
Dominic Hamone2633b92014-01-16 09:13:18 -0800404#endif
Dominic Hamon403f3542013-12-18 16:55:45 -0800405
Eric27e0b432017-11-22 09:33:52 -0700406std::vector<CPUInfo::CacheInfo> GetCacheSizes() {
407#ifdef BENCHMARK_OS_MACOSX
408 return GetCacheSizesMacOSX();
Eric11dc3682017-11-26 13:33:01 -0700409#elif defined(BENCHMARK_OS_WINDOWS)
410 return GetCacheSizesWindows();
Jilin Zhoud205ead2019-02-28 05:42:44 -0500411#elif defined(BENCHMARK_OS_QNX)
412 return GetCacheSizesQNX();
Eric27e0b432017-11-22 09:33:52 -0700413#else
414 return GetCacheSizesFromKVFS();
Eric Fiselier03c44852016-10-07 22:54:06 -0600415#endif
Eric27e0b432017-11-22 09:33:52 -0700416}
Eric Fiselier03c44852016-10-07 22:54:06 -0600417
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530418std::string GetSystemName() {
419#if defined(BENCHMARK_OS_WINDOWS)
420 std::string str;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000421 const unsigned COUNT = MAX_COMPUTERNAME_LENGTH + 1;
422 TCHAR hostname[COUNT] = {'\0'};
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530423 DWORD DWCOUNT = COUNT;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000424 if (!GetComputerName(hostname, &DWCOUNT)) return std::string("");
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530425#ifndef UNICODE
426 str = std::string(hostname, DWCOUNT);
427#else
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000428 // Using wstring_convert, Is deprecated in C++17
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530429 using convert_type = std::codecvt_utf8<wchar_t>;
430 std::wstring_convert<convert_type, wchar_t> converter;
431 std::wstring wStr(hostname, DWCOUNT);
432 str = converter.to_bytes(wStr);
433#endif
434 return str;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000435#else // defined(BENCHMARK_OS_WINDOWS)
Geoffrey Martin-Noble74118742019-09-23 02:38:34 -0700436#ifndef HOST_NAME_MAX
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000437#ifdef BENCHMARK_HAS_SYSCTL // BSD/Mac Doesnt have HOST_NAME_MAX defined
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530438#define HOST_NAME_MAX 64
Geoffrey Martin-Noble74118742019-09-23 02:38:34 -0700439#elif defined(BENCHMARK_OS_NACL)
440#define HOST_NAME_MAX 64
Jilin Zhou0ae233a2019-02-19 08:05:55 -0500441#elif defined(BENCHMARK_OS_QNX)
442#define HOST_NAME_MAX 154
Geoffrey Martin-Noble74118742019-09-23 02:38:34 -0700443#elif defined(BENCHMARK_OS_RTEMS)
444#define HOST_NAME_MAX 256
445#else
Dominic Hamon2b093322021-08-24 15:08:15 +0100446#pragma message("HOST_NAME_MAX not defined. using 64")
Geoffrey Martin-Noble74118742019-09-23 02:38:34 -0700447#define HOST_NAME_MAX 64
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530448#endif
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000449#endif // def HOST_NAME_MAX
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530450 char hostname[HOST_NAME_MAX];
451 int retVal = gethostname(hostname, HOST_NAME_MAX);
452 if (retVal != 0) return std::string("");
453 return std::string(hostname);
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000454#endif // Catch-all POSIX block.
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530455}
456
Eric27e0b432017-11-22 09:33:52 -0700457int GetNumCPUs() {
458#ifdef BENCHMARK_HAS_SYSCTL
459 int NumCPU = -1;
460 if (GetSysctl("hw.ncpu", &NumCPU)) return NumCPU;
461 fprintf(stderr, "Err: %s\n", strerror(errno));
462 std::exit(EXIT_FAILURE);
463#elif defined(BENCHMARK_OS_WINDOWS)
464 SYSTEM_INFO sysinfo;
465 // Use memset as opposed to = {} to avoid GCC missing initializer false
466 // positives.
467 std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
468 GetSystemInfo(&sysinfo);
469 return sysinfo.dwNumberOfProcessors; // number of logical
470 // processors in the current
471 // group
alekseyshl47df49e2018-03-02 03:53:58 -0800472#elif defined(BENCHMARK_OS_SOLARIS)
473 // Returns -1 in case of a failure.
474 int NumCPU = sysconf(_SC_NPROCESSORS_ONLN);
475 if (NumCPU < 0) {
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000476 fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed with error: %s\n",
alekseyshl47df49e2018-03-02 03:53:58 -0800477 strerror(errno));
478 }
479 return NumCPU;
Jilin Zhoud205ead2019-02-28 05:42:44 -0500480#elif defined(BENCHMARK_OS_QNX)
481 return static_cast<int>(_syspage_ptr->num_cpu);
Eric27e0b432017-11-22 09:33:52 -0700482#else
483 int NumCPUs = 0;
484 int MaxID = -1;
485 std::ifstream f("/proc/cpuinfo");
486 if (!f.is_open()) {
487 std::cerr << "failed to open /proc/cpuinfo\n";
488 return -1;
489 }
490 const std::string Key = "processor";
491 std::string ln;
492 while (std::getline(f, ln)) {
493 if (ln.empty()) continue;
494 size_t SplitIdx = ln.find(':');
495 std::string value;
Anton Gladkyc6193af2018-10-21 10:01:42 +0200496#if defined(__s390__)
497 // s390 has another format in /proc/cpuinfo
498 // it needs to be parsed differently
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000499 if (SplitIdx != std::string::npos)
500 value = ln.substr(Key.size() + 1, SplitIdx - Key.size() - 1);
Anton Gladkyc6193af2018-10-21 10:01:42 +0200501#else
Eric27e0b432017-11-22 09:33:52 -0700502 if (SplitIdx != std::string::npos) value = ln.substr(SplitIdx + 1);
Anton Gladkyc6193af2018-10-21 10:01:42 +0200503#endif
Eric27e0b432017-11-22 09:33:52 -0700504 if (ln.size() >= Key.size() && ln.compare(0, Key.size(), Key) == 0) {
505 NumCPUs++;
506 if (!value.empty()) {
Marat Dukhan7fb3c562018-06-05 03:36:26 -0700507 int CurID = benchmark::stoi(value);
Eric27e0b432017-11-22 09:33:52 -0700508 MaxID = std::max(CurID, MaxID);
509 }
510 }
511 }
512 if (f.bad()) {
513 std::cerr << "Failure reading /proc/cpuinfo\n";
514 return -1;
515 }
516 if (!f.eof()) {
517 std::cerr << "Failed to read to end of /proc/cpuinfo\n";
518 return -1;
519 }
520 f.close();
521
522 if ((MaxID + 1) != NumCPUs) {
523 fprintf(stderr,
524 "CPU ID assignments in /proc/cpuinfo seem messed up."
525 " This is usually caused by a bad BIOS.\n");
526 }
527 return NumCPUs;
528#endif
529 BENCHMARK_UNREACHABLE();
530}
531
Roman Lebedevc05843a2021-04-23 14:33:22 +0300532double GetCPUCyclesPerSecond(CPUInfo::Scaling scaling) {
533 // Currently, scaling is only used on linux path here,
534 // suppress diagnostics about it being unused on other paths.
535 (void)scaling;
536
Anton Danielssonba141ac2015-10-05 13:58:35 +0200537#if defined BENCHMARK_OS_LINUX || defined BENCHMARK_OS_CYGWIN
Dominic Hamon3968ff42015-02-18 10:07:45 -0800538 long freq;
Dominic Hamon403f3542013-12-18 16:55:45 -0800539
540 // If the kernel is exporting the tsc frequency use that. There are issues
541 // where cpuinfo_max_freq cannot be relied on because the BIOS may be
542 // exporintg an invalid p-state (on x86) or p-states may be used to put the
543 // processor in a new mode (turbo mode). Essentially, those frequencies
544 // cannot always be relied upon. The same reasons apply to /proc/cpuinfo as
545 // well.
Eric27e0b432017-11-22 09:33:52 -0700546 if (ReadFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz", &freq)
Mircea Trofin94f845e2021-06-28 09:07:54 -0700547 // If CPU scaling is disabled, use the *current* frequency.
Roman Lebedevc05843a2021-04-23 14:33:22 +0300548 // Note that we specifically don't want to read cpuinfo_cur_freq,
549 // because it is only readable by root.
550 || (scaling == CPUInfo::Scaling::DISABLED &&
551 ReadFromFile("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
552 &freq))
553 // Otherwise, if CPU scaling may be in effect, we want to use
554 // the *maximum* frequency, not whatever CPU speed some random processor
555 // happens to be using now.
Eric27e0b432017-11-22 09:33:52 -0700556 || ReadFromFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
557 &freq)) {
Dominic Hamon4ce184d2014-01-09 12:12:11 -0800558 // The value is in kHz (as the file name suggests). For example, on a
559 // 2GHz warpstation, the file contains the value "2000000".
Eric27e0b432017-11-22 09:33:52 -0700560 return freq * 1000.0;
Dominic Hamon403f3542013-12-18 16:55:45 -0800561 }
562
Eric27e0b432017-11-22 09:33:52 -0700563 const double error_value = -1;
564 double bogo_clock = error_value;
565
566 std::ifstream f("/proc/cpuinfo");
567 if (!f.is_open()) {
568 std::cerr << "failed to open /proc/cpuinfo\n";
569 return error_value;
Dominic Hamon403f3542013-12-18 16:55:45 -0800570 }
571
Eric27e0b432017-11-22 09:33:52 -0700572 auto startsWithKey = [](std::string const& Value, std::string const& Key) {
573 if (Key.size() > Value.size()) return false;
574 auto Cmp = [&](char X, char Y) {
575 return std::tolower(X) == std::tolower(Y);
576 };
577 return std::equal(Key.begin(), Key.end(), Value.begin(), Cmp);
578 };
Dominic Hamon403f3542013-12-18 16:55:45 -0800579
Eric27e0b432017-11-22 09:33:52 -0700580 std::string ln;
581 while (std::getline(f, ln)) {
582 if (ln.empty()) continue;
583 size_t SplitIdx = ln.find(':');
584 std::string value;
585 if (SplitIdx != std::string::npos) value = ln.substr(SplitIdx + 1);
Dominic Hamon403f3542013-12-18 16:55:45 -0800586 // When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only
Wink Saville69a52cf2018-03-06 03:44:25 -0800587 // accept positive values. Some environments (virtual machines) report zero,
Dominic Hamon403f3542013-12-18 16:55:45 -0800588 // which would cause infinite looping in WallTime_Init.
Eric27e0b432017-11-22 09:33:52 -0700589 if (startsWithKey(ln, "cpu MHz")) {
590 if (!value.empty()) {
Marat Dukhan7fb3c562018-06-05 03:36:26 -0700591 double cycles_per_second = benchmark::stod(value) * 1000000.0;
Eric27e0b432017-11-22 09:33:52 -0700592 if (cycles_per_second > 0) return cycles_per_second;
Dominic Hamon403f3542013-12-18 16:55:45 -0800593 }
Eric27e0b432017-11-22 09:33:52 -0700594 } else if (startsWithKey(ln, "bogomips")) {
595 if (!value.empty()) {
Marat Dukhan7fb3c562018-06-05 03:36:26 -0700596 bogo_clock = benchmark::stod(value) * 1000000.0;
Eric27e0b432017-11-22 09:33:52 -0700597 if (bogo_clock < 0.0) bogo_clock = error_value;
Dominic Hamon403f3542013-12-18 16:55:45 -0800598 }
Dominic Hamon403f3542013-12-18 16:55:45 -0800599 }
600 }
Eric27e0b432017-11-22 09:33:52 -0700601 if (f.bad()) {
602 std::cerr << "Failure reading /proc/cpuinfo\n";
603 return error_value;
Dominic Hamon403f3542013-12-18 16:55:45 -0800604 }
Eric27e0b432017-11-22 09:33:52 -0700605 if (!f.eof()) {
606 std::cerr << "Failed to read to end of /proc/cpuinfo\n";
607 return error_value;
608 }
609 f.close();
610 // If we found the bogomips clock, but nothing better, we'll use it (but
611 // we're not happy about it); otherwise, fallback to the rough estimation
612 // below.
613 if (bogo_clock >= 0.0) return bogo_clock;
Dominic Hamon403f3542013-12-18 16:55:45 -0800614
Eric27e0b432017-11-22 09:33:52 -0700615#elif defined BENCHMARK_HAS_SYSCTL
616 constexpr auto* FreqStr =
617#if defined(BENCHMARK_OS_FREEBSD) || defined(BENCHMARK_OS_NETBSD)
618 "machdep.tsc_freq";
Nan Xiaoea5551e2018-05-02 18:26:43 +0800619#elif defined BENCHMARK_OS_OPENBSD
620 "hw.cpuspeed";
Michael Neumannaf729112020-10-12 22:41:49 +0200621#elif defined BENCHMARK_OS_DRAGONFLY
622 "hw.tsc_frequency";
Dominic Hamon403f3542013-12-18 16:55:45 -0800623#else
Eric27e0b432017-11-22 09:33:52 -0700624 "hw.cpufrequency";
Dominic Hamon403f3542013-12-18 16:55:45 -0800625#endif
Eric27e0b432017-11-22 09:33:52 -0700626 unsigned long long hz = 0;
Nan Xiaoea5551e2018-05-02 18:26:43 +0800627#if defined BENCHMARK_OS_OPENBSD
628 if (GetSysctl(FreqStr, &hz)) return hz * 1000000;
629#else
Eric27e0b432017-11-22 09:33:52 -0700630 if (GetSysctl(FreqStr, &hz)) return hz;
Nan Xiaoea5551e2018-05-02 18:26:43 +0800631#endif
Eric27e0b432017-11-22 09:33:52 -0700632 fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n",
633 FreqStr, strerror(errno));
634
Anton Danielssonba141ac2015-10-05 13:58:35 +0200635#elif defined BENCHMARK_OS_WINDOWS
Dominic Hamon403f3542013-12-18 16:55:45 -0800636 // In NT, read MHz from the registry. If we fail to do so or we're in win9x
637 // then make a crude estimate.
Dominic Hamon403f3542013-12-18 16:55:45 -0800638 DWORD data, data_size = sizeof(data);
Anton Danielssonfcf4e992015-10-05 14:51:56 +0200639 if (IsWindowsXPOrGreater() &&
Dominic Hamon4ce184d2014-01-09 12:12:11 -0800640 SUCCEEDED(
641 SHGetValueA(HKEY_LOCAL_MACHINE,
642 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
Eric Fiselier18098172015-03-12 19:32:50 -0400643 "~MHz", nullptr, &data, &data_size)))
Eric27e0b432017-11-22 09:33:52 -0700644 return static_cast<double>((int64_t)data *
645 (int64_t)(1000 * 1000)); // was mhz
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000646#elif defined(BENCHMARK_OS_SOLARIS)
647 kstat_ctl_t* kc = kstat_open();
alekseyshl47df49e2018-03-02 03:53:58 -0800648 if (!kc) {
649 std::cerr << "failed to open /dev/kstat\n";
650 return -1;
651 }
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000652 kstat_t* ksp = kstat_lookup(kc, (char*)"cpu_info", -1, (char*)"cpu_info0");
alekseyshl47df49e2018-03-02 03:53:58 -0800653 if (!ksp) {
654 std::cerr << "failed to lookup in /dev/kstat\n";
655 return -1;
656 }
657 if (kstat_read(kc, ksp, NULL) < 0) {
658 std::cerr << "failed to read from /dev/kstat\n";
659 return -1;
660 }
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000661 kstat_named_t* knp =
alekseyshl47df49e2018-03-02 03:53:58 -0800662 (kstat_named_t*)kstat_data_lookup(ksp, (char*)"current_clock_Hz");
663 if (!knp) {
664 std::cerr << "failed to lookup data in /dev/kstat\n";
665 return -1;
666 }
667 if (knp->data_type != KSTAT_DATA_UINT64) {
668 std::cerr << "current_clock_Hz is of unexpected data type: "
669 << knp->data_type << "\n";
670 return -1;
671 }
672 double clock_hz = knp->value.ui64;
673 kstat_close(kc);
674 return clock_hz;
Dominic Hamonfcef4fb2021-11-10 16:04:32 +0000675#elif defined(BENCHMARK_OS_QNX)
Jilin Zhoud205ead2019-02-28 05:42:44 -0500676 return static_cast<double>((int64_t)(SYSPAGE_ENTRY(cpuinfo)->speed) *
677 (int64_t)(1000 * 1000));
Dominic Hamon403f3542013-12-18 16:55:45 -0800678#endif
Eric27e0b432017-11-22 09:33:52 -0700679 // If we've fallen through, attempt to roughly estimate the CPU clock rate.
680 const int estimate_time_ms = 1000;
681 const auto start_ticks = cycleclock::Now();
682 SleepForMilliseconds(estimate_time_ms);
683 return static_cast<double>(cycleclock::Now() - start_ticks);
Dominic Hamon403f3542013-12-18 16:55:45 -0800684}
Ericcba945e2016-09-02 21:34:34 -0600685
Ori Livnehda9ec3d2018-07-09 00:17:44 -0400686std::vector<double> GetLoadAvg() {
Michael Neumannaf729112020-10-12 22:41:49 +0200687#if (defined BENCHMARK_OS_FREEBSD || defined(BENCHMARK_OS_LINUX) || \
688 defined BENCHMARK_OS_MACOSX || defined BENCHMARK_OS_NETBSD || \
689 defined BENCHMARK_OS_OPENBSD || defined BENCHMARK_OS_DRAGONFLY) && \
690 !defined(__ANDROID__)
Ori Livnehda9ec3d2018-07-09 00:17:44 -0400691 constexpr int kMaxSamples = 3;
692 std::vector<double> res(kMaxSamples, 0.0);
693 const int nelem = getloadavg(res.data(), kMaxSamples);
694 if (nelem < 1) {
695 res.clear();
696 } else {
697 res.resize(nelem);
698 }
699 return res;
700#else
701 return {};
702#endif
703}
704
Dominic Hamon403f3542013-12-18 16:55:45 -0800705} // end namespace
706
Eric27e0b432017-11-22 09:33:52 -0700707const CPUInfo& CPUInfo::Get() {
708 static const CPUInfo* info = new CPUInfo();
709 return *info;
Dominic Hamon403f3542013-12-18 16:55:45 -0800710}
711
Eric27e0b432017-11-22 09:33:52 -0700712CPUInfo::CPUInfo()
713 : num_cpus(GetNumCPUs()),
Alexander Enaldiev99010112020-07-28 14:46:07 +0300714 scaling(CpuScaling(num_cpus)),
Roman Lebedevc05843a2021-04-23 14:33:22 +0300715 cycles_per_second(GetCPUCyclesPerSecond(scaling)),
716 caches(GetCacheSizes()),
Ori Livnehda9ec3d2018-07-09 00:17:44 -0400717 load_avg(GetLoadAvg()) {}
Eric Fiseliera3308c62015-03-26 14:26:07 -0400718
Jatin Chaudhary47a5f772018-12-11 16:53:02 +0530719const SystemInfo& SystemInfo::Get() {
720 static const SystemInfo* info = new SystemInfo();
721 return *info;
722}
723
724SystemInfo::SystemInfo() : name(GetSystemName()) {}
Dominic Hamone390e4e2013-12-19 16:18:09 -0800725} // end namespace benchmark