Debian Codegraph @

third-party/benchmark/src/sysinfo.cc

raw
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 #if defined(_MSC_VER)
16 // FIXME: This must be defined before any other includes to disable deprecation
17 // warnings for use of codecvt from C++17. We should remove our reliance on
18 // the deprecated functionality instead.
19 #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
20 #endif
21
22 #include "internal_macros.h"
23
24 #ifdef BENCHMARK_OS_WINDOWS
25 #if !defined(WINVER) || WINVER < 0x0600
26 #undef WINVER
27 #define WINVER 0x0600
28 #endif // WINVER handling
29 #include <shlwapi.h>
30 #undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA
31 #include <versionhelpers.h>
32 #include <windows.h>
33
34 #include <codecvt>
35 #else
36 #include <fcntl.h>
37 #if !defined(BENCHMARK_OS_FUCHSIA) && !defined(BENCHMARK_OS_QURT)
38 #include <sys/resource.h>
39 #endif
40 #include <sys/time.h>
41 #include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD
42 #include <unistd.h>
43 #if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX || \
44 defined BENCHMARK_OS_NETBSD || defined BENCHMARK_OS_OPENBSD || \
45 defined BENCHMARK_OS_DRAGONFLY
46 #define BENCHMARK_HAS_SYSCTL
47 #include <sys/sysctl.h>
48 #endif
49 #endif
50 #if defined(BENCHMARK_OS_SOLARIS)
51 #include <kstat.h>
52 #include <netdb.h>
53 #endif
54 #if defined(BENCHMARK_OS_QNX)
55 #include <sys/syspage.h>
56 #endif
57 #if defined(BENCHMARK_OS_QURT)
58 #include <qurt.h>
59 #endif
60 #if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)
61 #include <pthread.h>
62 #endif
63
64 #include <algorithm>
65 #include <array>
66 #include <bitset>
67 #include <cerrno>
68 #include <climits>
69 #include <cstdint>
70 #include <cstdio>
71 #include <cstdlib>
72 #include <cstring>
73 #include <fstream>
74 #include <iostream>
75 #include <iterator>
76 #include <limits>
77 #include <locale>
78 #include <memory>
79 #include <random>
80 #include <sstream>
81 #include <utility>
82
83 #include "benchmark/benchmark.h"
84 #include "check.h"
85 #include "cycleclock.h"
86 #include "internal_macros.h"
87 #include "log.h"
88 #include "string_util.h"
89 #include "timers.h"
90
91 namespace benchmark {
92 namespace {
93
94 void PrintImp(std::ostream& out) { out << std::endl; }
95
96 template <class First, class... Rest>
97 void PrintImp(std::ostream& out, First&& f, Rest&&... rest) {
98 out << std::forward<First>(f);
99 PrintImp(out, std::forward<Rest>(rest)...);
100 }
101
102 template <class... Args>
103 BENCHMARK_NORETURN void PrintErrorAndDie(Args&&... args) {
104 PrintImp(std::cerr, std::forward<Args>(args)...);
105 std::exit(EXIT_FAILURE);
106 }
107
108 #ifdef BENCHMARK_HAS_SYSCTL
109
110 /// ValueUnion - A type used to correctly alias the byte-for-byte output of
111 /// `sysctl` with the result type it's to be interpreted as.
112 struct ValueUnion {
113 union DataT {
114 int32_t int32_value;
115 int64_t int64_value;
116 // For correct aliasing of union members from bytes.
117 char bytes[8];
118 };
119 using DataPtr = std::unique_ptr<DataT, decltype(&std::free)>;
120
121 // The size of the data union member + its trailing array size.
122 std::size_t size;
123 DataPtr buff;
124
125 public:
126 ValueUnion() : size(0), buff(nullptr, &std::free) {}
127
128 explicit ValueUnion(std::size_t buff_size)
129 : size(sizeof(DataT) + buff_size),
130 buff(::new (std::malloc(size)) DataT(), &std::free) {}
131
132 ValueUnion(ValueUnion&& other) = default;
133
134 explicit operator bool() const { return bool(buff); }
135
136 char* data() const { return buff->bytes; }
137
138 std::string GetAsString() const { return std::string(data()); }
139
140 int64_t GetAsInteger() const {
141 if (size == sizeof(buff->int32_value))
142 return buff->int32_value;
143 else if (size == sizeof(buff->int64_value))
144 return buff->int64_value;
145 BENCHMARK_UNREACHABLE();
146 }
147
148 template <class T, int N>
149 std::array<T, N> GetAsArray() {
150 const int arr_size = sizeof(T) * N;
151 BM_CHECK_LE(arr_size, size);
152 std::array<T, N> arr;
153 std::memcpy(arr.data(), data(), arr_size);
154 return arr;
155 }
156 };
157
158 ValueUnion GetSysctlImp(std::string const& name) {
159 #if defined BENCHMARK_OS_OPENBSD
160 int mib[2];
161
162 mib[0] = CTL_HW;
163 if ((name == "hw.ncpuonline") || (name == "hw.cpuspeed")) {
164 ValueUnion buff(sizeof(int));
165
166 if (name == "hw.ncpuonline") {
167 mib[1] = HW_NCPUONLINE;
168 } else {
169 mib[1] = HW_CPUSPEED;
170 }
171
172 if (sysctl(mib, 2, buff.data(), &buff.size, nullptr, 0) == -1) {
173 return ValueUnion();
174 }
175 return buff;
176 }
177 return ValueUnion();
178 #else
179 std::size_t cur_buff_size = 0;
180 if (sysctlbyname(name.c_str(), nullptr, &cur_buff_size, nullptr, 0) == -1)
181 return ValueUnion();
182
183 ValueUnion buff(cur_buff_size);
184 if (sysctlbyname(name.c_str(), buff.data(), &buff.size, nullptr, 0) == 0)
185 return buff;
186 return ValueUnion();
187 #endif
188 }
189
190 BENCHMARK_MAYBE_UNUSED
191 bool GetSysctl(std::string const& name, std::string* out) {
192 out->clear();
193 auto buff = GetSysctlImp(name);
194 if (!buff) return false;
195 out->assign(buff.data());
196 return true;
197 }
198
199 template <class Tp,
200 class = typename std::enable_if<std::is_integral<Tp>::value>::type>
201 bool GetSysctl(std::string const& name, Tp* out) {
202 *out = 0;
203 auto buff = GetSysctlImp(name);
204 if (!buff) return false;
205 *out = static_cast<Tp>(buff.GetAsInteger());
206 return true;
207 }
208
209 template <class Tp, size_t N>
210 bool GetSysctl(std::string const& name, std::array<Tp, N>* out) {
211 auto buff = GetSysctlImp(name);
212 if (!buff) return false;
213 *out = buff.GetAsArray<Tp, N>();
214 return true;
215 }
216 #endif
217
218 template <class ArgT>
219 bool ReadFromFile(std::string const& fname, ArgT* arg) {
220 *arg = ArgT();
221 std::ifstream f(fname.c_str());
222 if (!f.is_open()) return false;
223 f >> *arg;
224 return f.good();
225 }
226
227 CPUInfo::Scaling CpuScaling(int num_cpus) {
228 // We don't have a valid CPU count, so don't even bother.
229 if (num_cpus <= 0) return CPUInfo::Scaling::UNKNOWN;
230 #if defined(BENCHMARK_OS_QNX)
231 return CPUInfo::Scaling::UNKNOWN;
232 #elif !defined(BENCHMARK_OS_WINDOWS)
233 // On Linux, the CPUfreq subsystem exposes CPU information as files on the
234 // local file system. If reading the exported files fails, then we may not be
235 // running on Linux, so we silently ignore all the read errors.
236 std::string res;
237 for (int cpu = 0; cpu < num_cpus; ++cpu) {
238 std::string governor_file =
239 StrCat("/sys/devices/system/cpu/cpu", cpu, "/cpufreq/scaling_governor");
240 if (ReadFromFile(governor_file, &res) && res != "performance")
241 return CPUInfo::Scaling::ENABLED;
242 }
243 return CPUInfo::Scaling::DISABLED;
244 #else
245 return CPUInfo::Scaling::UNKNOWN;
246 #endif
247 }
248
249 int CountSetBitsInCPUMap(std::string val) {
250 auto CountBits = [](std::string part) {
251 using CPUMask = std::bitset<sizeof(std::uintptr_t) * CHAR_BIT>;
252 part = "0x" + part;
253 CPUMask mask(benchmark::stoul(part, nullptr, 16));
254 return static_cast<int>(mask.count());
255 };
256 std::size_t pos;
257 int total = 0;
258 while ((pos = val.find(',')) != std::string::npos) {
259 total += CountBits(val.substr(0, pos));
260 val = val.substr(pos + 1);
261 }
262 if (!val.empty()) {
263 total += CountBits(val);
264 }
265 return total;
266 }
267
268 BENCHMARK_MAYBE_UNUSED
269 std::vector<CPUInfo::CacheInfo> GetCacheSizesFromKVFS() {
270 std::vector<CPUInfo::CacheInfo> res;
271 std::string dir = "/sys/devices/system/cpu/cpu0/cache/";
272 int idx = 0;
273 while (true) {
274 CPUInfo::CacheInfo info;
275 std::string fpath = StrCat(dir, "index", idx++, "/");
276 std::ifstream f(StrCat(fpath, "size").c_str());
277 if (!f.is_open()) break;
278 std::string suffix;
279 f >> info.size;
280 if (f.fail())
281 PrintErrorAndDie("Failed while reading file '", fpath, "size'");
282 if (f.good()) {
283 f >> suffix;
284 if (f.bad())
285 PrintErrorAndDie(
286 "Invalid cache size format: failed to read size suffix");
287 else if (f && suffix != "K")
288 PrintErrorAndDie("Invalid cache size format: Expected bytes ", suffix);
289 else if (suffix == "K")
290 info.size *= 1024;
291 }
292 if (!ReadFromFile(StrCat(fpath, "type"), &info.type))
293 PrintErrorAndDie("Failed to read from file ", fpath, "type");
294 if (!ReadFromFile(StrCat(fpath, "level"), &info.level))
295 PrintErrorAndDie("Failed to read from file ", fpath, "level");
296 std::string map_str;
297 if (!ReadFromFile(StrCat(fpath, "shared_cpu_map"), &map_str))
298 PrintErrorAndDie("Failed to read from file ", fpath, "shared_cpu_map");
299 info.num_sharing = CountSetBitsInCPUMap(map_str);
300 res.push_back(info);
301 }
302
303 return res;
304 }
305
306 #ifdef BENCHMARK_OS_MACOSX
307 std::vector<CPUInfo::CacheInfo> GetCacheSizesMacOSX() {
308 std::vector<CPUInfo::CacheInfo> res;
309 std::array<int, 4> cache_counts{{0, 0, 0, 0}};
310 GetSysctl("hw.cacheconfig", &cache_counts);
311
312 struct {
313 std::string name;
314 std::string type;
315 int level;
316 int num_sharing;
317 } cases[] = {{"hw.l1dcachesize", "Data", 1, cache_counts[1]},
318 {"hw.l1icachesize", "Instruction", 1, cache_counts[1]},
319 {"hw.l2cachesize", "Unified", 2, cache_counts[2]},
320 {"hw.l3cachesize", "Unified", 3, cache_counts[3]}};
321 for (auto& c : cases) {
322 int val;
323 if (!GetSysctl(c.name, &val)) continue;
324 CPUInfo::CacheInfo info;
325 info.type = c.type;
326 info.level = c.level;
327 info.size = val;
328 info.num_sharing = c.num_sharing;
329 res.push_back(std::move(info));
330 }
331 return res;
332 }
333 #elif defined(BENCHMARK_OS_WINDOWS)
334 std::vector<CPUInfo::CacheInfo> GetCacheSizesWindows() {
335 std::vector<CPUInfo::CacheInfo> res;
336 DWORD buffer_size = 0;
337 using PInfo = SYSTEM_LOGICAL_PROCESSOR_INFORMATION;
338 using CInfo = CACHE_DESCRIPTOR;
339
340 using UPtr = std::unique_ptr<PInfo, decltype(&std::free)>;
341 GetLogicalProcessorInformation(nullptr, &buffer_size);
342 UPtr buff(static_cast<PInfo*>(std::malloc(buffer_size)), &std::free);
343 if (!GetLogicalProcessorInformation(buff.get(), &buffer_size))
344 PrintErrorAndDie("Failed during call to GetLogicalProcessorInformation: ",
345 GetLastError());
346
347 PInfo* it = buff.get();
348 PInfo* end = buff.get() + (buffer_size / sizeof(PInfo));
349
350 for (; it != end; ++it) {
351 if (it->Relationship != RelationCache) continue;
352 using BitSet = std::bitset<sizeof(ULONG_PTR) * CHAR_BIT>;
353 BitSet b(it->ProcessorMask);
354 // To prevent duplicates, only consider caches where CPU 0 is specified
355 if (!b.test(0)) continue;
356 const CInfo& cache = it->Cache;
357 CPUInfo::CacheInfo C;
358 C.num_sharing = static_cast<int>(b.count());
359 C.level = cache.Level;
360 C.size = cache.Size;
361 switch (cache.Type) {
362 case CacheUnified:
363 C.type = "Unified";
364 break;
365 case CacheInstruction:
366 C.type = "Instruction";
367 break;
368 case CacheData:
369 C.type = "Data";
370 break;
371 case CacheTrace:
372 C.type = "Trace";
373 break;
374 default:
375 C.type = "Unknown";
376 break;
377 }
378 res.push_back(C);
379 }
380 return res;
381 }
382 #elif BENCHMARK_OS_QNX
383 std::vector<CPUInfo::CacheInfo> GetCacheSizesQNX() {
384 std::vector<CPUInfo::CacheInfo> res;
385 struct cacheattr_entry* cache = SYSPAGE_ENTRY(cacheattr);
386 uint32_t const elsize = SYSPAGE_ELEMENT_SIZE(cacheattr);
387 int num = SYSPAGE_ENTRY_SIZE(cacheattr) / elsize;
388 for (int i = 0; i < num; ++i) {
389 CPUInfo::CacheInfo info;
390 switch (cache->flags) {
391 case CACHE_FLAG_INSTR:
392 info.type = "Instruction";
393 info.level = 1;
394 break;
395 case CACHE_FLAG_DATA:
396 info.type = "Data";
397 info.level = 1;
398 break;
399 case CACHE_FLAG_UNIFIED:
400 info.type = "Unified";
401 info.level = 2;
402 break;
403 case CACHE_FLAG_SHARED:
404 info.type = "Shared";
405 info.level = 3;
406 break;
407 default:
408 continue;
409 break;
410 }
411 info.size = cache->line_size * cache->num_lines;
412 info.num_sharing = 0;
413 res.push_back(std::move(info));
414 cache = SYSPAGE_ARRAY_ADJ_OFFSET(cacheattr, cache, elsize);
415 }
416 return res;
417 }
418 #endif
419
420 std::vector<CPUInfo::CacheInfo> GetCacheSizes() {
421 #ifdef BENCHMARK_OS_MACOSX
422 return GetCacheSizesMacOSX();
423 #elif defined(BENCHMARK_OS_WINDOWS)
424 return GetCacheSizesWindows();
425 #elif defined(BENCHMARK_OS_QNX)
426 return GetCacheSizesQNX();
427 #elif defined(BENCHMARK_OS_QURT)
428 return std::vector<CPUInfo::CacheInfo>();
429 #else
430 return GetCacheSizesFromKVFS();
431 #endif
432 }
433
434 std::string GetSystemName() {
435 #if defined(BENCHMARK_OS_WINDOWS)
436 std::string str;
437 static constexpr int COUNT = MAX_COMPUTERNAME_LENGTH + 1;
438 TCHAR hostname[COUNT] = {'\0'};
439 DWORD DWCOUNT = COUNT;
440 if (!GetComputerName(hostname, &DWCOUNT)) return std::string("");
441 #ifndef UNICODE
442 str = std::string(hostname, DWCOUNT);
443 #else
444 // `WideCharToMultiByte` returns `0` when conversion fails.
445 int len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, hostname,
446 DWCOUNT, NULL, 0, NULL, NULL);
447 str.resize(len);
448 WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, hostname, DWCOUNT, &str[0],
449 str.size(), NULL, NULL);
450 #endif
451 return str;
452 #elif defined(BENCHMARK_OS_QURT)
453 std::string str = "Hexagon DSP";
454 qurt_arch_version_t arch_version_struct;
455 if (qurt_sysenv_get_arch_version(&arch_version_struct) == QURT_EOK) {
456 str += " v";
457 str += std::to_string(arch_version_struct.arch_version);
458 }
459 return str;
460 #else
461 #ifndef HOST_NAME_MAX
462 #ifdef BENCHMARK_HAS_SYSCTL // BSD/Mac doesn't have HOST_NAME_MAX defined
463 #define HOST_NAME_MAX 64
464 #elif defined(BENCHMARK_OS_NACL)
465 #define HOST_NAME_MAX 64
466 #elif defined(BENCHMARK_OS_QNX)
467 #define HOST_NAME_MAX 154
468 #elif defined(BENCHMARK_OS_RTEMS)
469 #define HOST_NAME_MAX 256
470 #elif defined(BENCHMARK_OS_SOLARIS)
471 #define HOST_NAME_MAX MAXHOSTNAMELEN
472 #elif defined(BENCHMARK_OS_ZOS)
473 #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
474 #else
475 #pragma message("HOST_NAME_MAX not defined. using 64")
476 #define HOST_NAME_MAX 64
477 #endif
478 #endif // def HOST_NAME_MAX
479 char hostname[HOST_NAME_MAX];
480 int retVal = gethostname(hostname, HOST_NAME_MAX);
481 if (retVal != 0) return std::string("");
482 return std::string(hostname);
483 #endif // Catch-all POSIX block.
484 }
485
486 int GetNumCPUsImpl() {
487 #ifdef BENCHMARK_OS_WINDOWS
488 SYSTEM_INFO sysinfo;
489 // Use memset as opposed to = {} to avoid GCC missing initializer false
490 // positives.
491 std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
492 GetSystemInfo(&sysinfo);
493 // number of logical processors in the current group
494 return static_cast<int>(sysinfo.dwNumberOfProcessors);
495 #elif defined(BENCHMARK_OS_QNX)
496 return static_cast<int>(_syspage_ptr->num_cpu);
497 #elif defined(BENCHMARK_OS_QURT)
498 qurt_sysenv_max_hthreads_t hardware_threads;
499 if (qurt_sysenv_get_max_hw_threads(&hardware_threads) != QURT_EOK) {
500 hardware_threads.max_hthreads = 1;
501 }
502 return hardware_threads.max_hthreads;
503 #elif defined(BENCHMARK_HAS_SYSCTL)
504 int num_cpu = -1;
505 constexpr auto* hwncpu =
506 #if defined BENCHMARK_OS_MACOSX
507 "hw.logicalcpu";
508 #elif defined(HW_NCPUONLINE)
509 "hw.ncpuonline";
510 #else
511 "hw.ncpu";
512 #endif
513 if (GetSysctl(hwncpu, &num_cpu)) return num_cpu;
514 PrintErrorAndDie("Err: ", strerror(errno));
515 #elif defined(_SC_NPROCESSORS_ONLN)
516 // Returns -1 in case of a failure.
517 int num_cpu = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
518 if (num_cpu < 0) {
519 PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
520 strerror(errno));
521 }
522 return num_cpu;
523 #endif
524 BENCHMARK_UNREACHABLE();
525 }
526
527 int GetNumCPUs() {
528 int num_cpus = GetNumCPUsImpl();
529 if (num_cpus < 1) {
530 std::cerr << "Unable to extract number of CPUs.\n";
531 /* There is at least one CPU which we run on. */
532 num_cpus = 1;
533 }
534 return num_cpus;
535 }
536
537 class ThreadAffinityGuard final {
538 public:
539 ThreadAffinityGuard() : reset_affinity(SetAffinity()) {
540 if (!reset_affinity)
541 std::cerr << "***WARNING*** Failed to set thread affinity. Estimated CPU "
542 "frequency may be incorrect."
543 << std::endl;
544 }
545
546 ~ThreadAffinityGuard() {
547 if (!reset_affinity) return;
548
549 #if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)
550 int ret = pthread_setaffinity_np(self, sizeof(previous_affinity),
551 &previous_affinity);
552 if (ret == 0) return;
553 #elif defined(BENCHMARK_OS_WINDOWS_WIN32)
554 DWORD_PTR ret = SetThreadAffinityMask(self, previous_affinity);
555 if (ret != 0) return;
556 #endif // def BENCHMARK_HAS_PTHREAD_AFFINITY
557 PrintErrorAndDie("Failed to reset thread affinity");
558 }
559
560 ThreadAffinityGuard(ThreadAffinityGuard&&) = delete;
561 ThreadAffinityGuard(const ThreadAffinityGuard&) = delete;
562 ThreadAffinityGuard& operator=(ThreadAffinityGuard&&) = delete;
563 ThreadAffinityGuard& operator=(const ThreadAffinityGuard&) = delete;
564
565 private:
566 bool SetAffinity() {
567 #if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)
568 int ret;
569 self = pthread_self();
570 ret = pthread_getaffinity_np(self, sizeof(previous_affinity),
571 &previous_affinity);
572 if (ret != 0) return false;
573
574 cpu_set_t affinity;
575 memcpy(&affinity, &previous_affinity, sizeof(affinity));
576
577 bool is_first_cpu = true;
578
579 for (int i = 0; i < CPU_SETSIZE; ++i)
580 if (CPU_ISSET(i, &affinity)) {
581 if (is_first_cpu)
582 is_first_cpu = false;
583 else
584 CPU_CLR(i, &affinity);
585 }
586
587 if (is_first_cpu) return false;
588
589 ret = pthread_setaffinity_np(self, sizeof(affinity), &affinity);
590 return ret == 0;
591 #elif defined(BENCHMARK_OS_WINDOWS_WIN32)
592 self = GetCurrentThread();
593 DWORD_PTR mask = static_cast<DWORD_PTR>(1) << GetCurrentProcessorNumber();
594 previous_affinity = SetThreadAffinityMask(self, mask);
595 return previous_affinity != 0;
596 #else
597 return false;
598 #endif // def BENCHMARK_HAS_PTHREAD_AFFINITY
599 }
600
601 #if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)
602 pthread_t self;
603 cpu_set_t previous_affinity;
604 #elif defined(BENCHMARK_OS_WINDOWS_WIN32)
605 HANDLE self;
606 DWORD_PTR previous_affinity;
607 #endif // def BENCHMARK_HAS_PTHREAD_AFFINITY
608 bool reset_affinity;
609 };
610
611 double GetCPUCyclesPerSecond(CPUInfo::Scaling scaling) {
612 // Currently, scaling is only used on linux path here,
613 // suppress diagnostics about it being unused on other paths.
614 (void)scaling;
615
616 #if defined BENCHMARK_OS_LINUX || defined BENCHMARK_OS_CYGWIN
617 long freq;
618
619 // If the kernel is exporting the tsc frequency use that. There are issues
620 // where cpuinfo_max_freq cannot be relied on because the BIOS may be
621 // exporintg an invalid p-state (on x86) or p-states may be used to put the
622 // processor in a new mode (turbo mode). Essentially, those frequencies
623 // cannot always be relied upon. The same reasons apply to /proc/cpuinfo as
624 // well.
625 if (ReadFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz", &freq)
626 // If CPU scaling is disabled, use the *current* frequency.
627 // Note that we specifically don't want to read cpuinfo_cur_freq,
628 // because it is only readable by root.
629 || (scaling == CPUInfo::Scaling::DISABLED &&
630 ReadFromFile("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
631 &freq))
632 // Otherwise, if CPU scaling may be in effect, we want to use
633 // the *maximum* frequency, not whatever CPU speed some random processor
634 // happens to be using now.
635 || ReadFromFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
636 &freq)) {
637 // The value is in kHz (as the file name suggests). For example, on a
638 // 2GHz warpstation, the file contains the value "2000000".
639 return static_cast<double>(freq) * 1000.0;
640 }
641
642 const double error_value = -1;
643 double bogo_clock = error_value;
644
645 std::ifstream f("/proc/cpuinfo");
646 if (!f.is_open()) {
647 std::cerr << "failed to open /proc/cpuinfo\n";
648 return error_value;
649 }
650
651 auto StartsWithKey = [](std::string const& Value, std::string const& Key) {
652 if (Key.size() > Value.size()) return false;
653 auto Cmp = [&](char X, char Y) {
654 return std::tolower(X) == std::tolower(Y);
655 };
656 return std::equal(Key.begin(), Key.end(), Value.begin(), Cmp);
657 };
658
659 std::string ln;
660 while (std::getline(f, ln)) {
661 if (ln.empty()) continue;
662 std::size_t split_idx = ln.find(':');
663 std::string value;
664 if (split_idx != std::string::npos) value = ln.substr(split_idx + 1);
665 // When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only
666 // accept positive values. Some environments (virtual machines) report zero,
667 // which would cause infinite looping in WallTime_Init.
668 if (StartsWithKey(ln, "cpu MHz")) {
669 if (!value.empty()) {
670 double cycles_per_second = benchmark::stod(value) * 1000000.0;
671 if (cycles_per_second > 0) return cycles_per_second;
672 }
673 } else if (StartsWithKey(ln, "bogomips")) {
674 if (!value.empty()) {
675 bogo_clock = benchmark::stod(value) * 1000000.0;
676 if (bogo_clock < 0.0) bogo_clock = error_value;
677 }
678 }
679 }
680 if (f.bad()) {
681 std::cerr << "Failure reading /proc/cpuinfo\n";
682 return error_value;
683 }
684 if (!f.eof()) {
685 std::cerr << "Failed to read to end of /proc/cpuinfo\n";
686 return error_value;
687 }
688 f.close();
689 // If we found the bogomips clock, but nothing better, we'll use it (but
690 // we're not happy about it); otherwise, fallback to the rough estimation
691 // below.
692 if (bogo_clock >= 0.0) return bogo_clock;
693
694 #elif defined BENCHMARK_HAS_SYSCTL
695 constexpr auto* freqStr =
696 #if defined(BENCHMARK_OS_FREEBSD) || defined(BENCHMARK_OS_NETBSD)
697 "machdep.tsc_freq";
698 #elif defined BENCHMARK_OS_OPENBSD
699 "hw.cpuspeed";
700 #elif defined BENCHMARK_OS_DRAGONFLY
701 "hw.tsc_frequency";
702 #else
703 "hw.cpufrequency";
704 #endif
705 unsigned long long hz = 0;
706 #if defined BENCHMARK_OS_OPENBSD
707 if (GetSysctl(freqStr, &hz)) return static_cast<double>(hz * 1000000);
708 #else
709 if (GetSysctl(freqStr, &hz)) return hz;
710 #endif
711 fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n",
712 freqStr, strerror(errno));
713 fprintf(stderr,
714 "This does not affect benchmark measurements, only the "
715 "metadata output.\n");
716
717 #elif defined BENCHMARK_OS_WINDOWS_WIN32
718 // In NT, read MHz from the registry. If we fail to do so or we're in win9x
719 // then make a crude estimate.
720 DWORD data, data_size = sizeof(data);
721 if (IsWindowsXPOrGreater() &&
722 SUCCEEDED(
723 SHGetValueA(HKEY_LOCAL_MACHINE,
724 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
725 "~MHz", nullptr, &data, &data_size)))
726 return static_cast<double>(static_cast<int64_t>(data) *
727 static_cast<int64_t>(1000 * 1000)); // was mhz
728 #elif defined(BENCHMARK_OS_SOLARIS)
729 kstat_ctl_t* kc = kstat_open();
730 if (!kc) {
731 std::cerr << "failed to open /dev/kstat\n";
732 return -1;
733 }
734 kstat_t* ksp = kstat_lookup(kc, const_cast<char*>("cpu_info"), -1,
735 const_cast<char*>("cpu_info0"));
736 if (!ksp) {
737 std::cerr << "failed to lookup in /dev/kstat\n";
738 return -1;
739 }
740 if (kstat_read(kc, ksp, NULL) < 0) {
741 std::cerr << "failed to read from /dev/kstat\n";
742 return -1;
743 }
744 kstat_named_t* knp = (kstat_named_t*)kstat_data_lookup(
745 ksp, const_cast<char*>("current_clock_Hz"));
746 if (!knp) {
747 std::cerr << "failed to lookup data in /dev/kstat\n";
748 return -1;
749 }
750 if (knp->data_type != KSTAT_DATA_UINT64) {
751 std::cerr << "current_clock_Hz is of unexpected data type: "
752 << knp->data_type << "\n";
753 return -1;
754 }
755 double clock_hz = knp->value.ui64;
756 kstat_close(kc);
757 return clock_hz;
758 #elif defined(BENCHMARK_OS_QNX)
759 return static_cast<double>(
760 static_cast<int64_t>(SYSPAGE_ENTRY(cpuinfo)->speed) *
761 static_cast<int64_t>(1000 * 1000));
762 #elif defined(BENCHMARK_OS_QURT)
763 // QuRT doesn't provide any API to query Hexagon frequency.
764 return 1000000000;
765 #endif
766 // If we've fallen through, attempt to roughly estimate the CPU clock rate.
767
768 // Make sure to use the same cycle counter when starting and stopping the
769 // cycle timer. We just pin the current thread to a cpu in the previous
770 // affinity set.
771 ThreadAffinityGuard affinity_guard;
772
773 static constexpr double estimate_time_s = 1.0;
774 const double start_time = ChronoClockNow();
775 const auto start_ticks = cycleclock::Now();
776
777 // Impose load instead of calling sleep() to make sure the cycle counter
778 // works.
779 using PRNG = std::minstd_rand;
780 using Result = PRNG::result_type;
781 PRNG rng(static_cast<Result>(start_ticks));
782
783 Result state = 0;
784
785 do {
786 static constexpr size_t batch_size = 10000;
787 rng.discard(batch_size);
788 state += rng();
789
790 } while (ChronoClockNow() - start_time < estimate_time_s);
791
792 DoNotOptimize(state);
793
794 const auto end_ticks = cycleclock::Now();
795 const double end_time = ChronoClockNow();
796
797 return static_cast<double>(end_ticks - start_ticks) / (end_time - start_time);
798 // Reset the affinity of current thread when the lifetime of affinity_guard
799 // ends.
800 }
801
802 std::vector<double> GetLoadAvg() {
803 #if (defined BENCHMARK_OS_FREEBSD || defined(BENCHMARK_OS_LINUX) || \
804 defined BENCHMARK_OS_MACOSX || defined BENCHMARK_OS_NETBSD || \
805 defined BENCHMARK_OS_OPENBSD || defined BENCHMARK_OS_DRAGONFLY) && \
806 !(defined(__ANDROID__) && __ANDROID_API__ < 29)
807 static constexpr int kMaxSamples = 3;
808 std::vector<double> res(kMaxSamples, 0.0);
809 const int nelem = getloadavg(res.data(), kMaxSamples);
810 if (nelem < 1) {
811 res.clear();
812 } else {
813 res.resize(nelem);
814 }
815 return res;
816 #else
817 return {};
818 #endif
819 }
820
821 } // end namespace
822
823 const CPUInfo& CPUInfo::Get() {
824 static const CPUInfo* info = new CPUInfo();
825 return *info;
826 }
827
828 CPUInfo::CPUInfo()
829 : num_cpus(GetNumCPUs()),
830 scaling(CpuScaling(num_cpus)),
831 cycles_per_second(GetCPUCyclesPerSecond(scaling)),
832 caches(GetCacheSizes()),
833 load_avg(GetLoadAvg()) {}
834
835 const SystemInfo& SystemInfo::Get() {
836 static const SystemInfo* info = new SystemInfo();
837 return *info;
838 }
839
840 SystemInfo::SystemInfo() : name(GetSystemName()) {}
841 } // end namespace benchmark