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 "internal_macros.h"
16
17 #ifdef BENCHMARK_OS_WINDOWS
18 #include <shlwapi.h>
19 #undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA
20 #include <versionhelpers.h>
21 #include <windows.h>
22 #include <codecvt>
23 #else
24 #include <fcntl.h>
25 #ifndef BENCHMARK_OS_FUCHSIA
26 #include <sys/resource.h>
27 #endif
28 #include <sys/time.h>
29 #include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD
30 #include <unistd.h>
31 #if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX || \
32 defined BENCHMARK_OS_NETBSD || defined BENCHMARK_OS_OPENBSD || \
33 defined BENCHMARK_OS_DRAGONFLY
34 #define BENCHMARK_HAS_SYSCTL
35 #include <sys/sysctl.h>
36 #endif
37 #endif
38 #if defined(BENCHMARK_OS_SOLARIS)
39 #include <kstat.h>
40 #endif
41 #if defined(BENCHMARK_OS_QNX)
42 #include <sys/syspage.h>
43 #endif
44
45 #include <algorithm>
46 #include <array>
47 #include <bitset>
48 #include <cerrno>
49 #include <climits>
50 #include <cstdint>
51 #include <cstdio>
52 #include <cstdlib>
53 #include <cstring>
54 #include <fstream>
55 #include <iostream>
56 #include <iterator>
57 #include <limits>
58 #include <memory>
59 #include <sstream>
60 #include <locale>
61 #include <utility>
62
63 #include "check.h"
64 #include "cycleclock.h"
65 #include "internal_macros.h"
66 #include "log.h"
67 #include "sleep.h"
68 #include "string_util.h"
69
70 namespace benchmark {
71 namespace {
72
PrintImp(std::ostream & out)73 void PrintImp(std::ostream& out) { out << std::endl; }
74
75 template <class First, class... Rest>
PrintImp(std::ostream & out,First && f,Rest &&...rest)76 void PrintImp(std::ostream& out, First&& f, Rest&&... rest) {
77 out << std::forward<First>(f);
78 PrintImp(out, std::forward<Rest>(rest)...);
79 }
80
81 template <class... Args>
PrintErrorAndDie(Args &&...args)82 BENCHMARK_NORETURN void PrintErrorAndDie(Args&&... args) {
83 PrintImp(std::cerr, std::forward<Args>(args)...);
84 std::exit(EXIT_FAILURE);
85 }
86
87 #ifdef BENCHMARK_HAS_SYSCTL
88
89 /// ValueUnion - A type used to correctly alias the byte-for-byte output of
90 /// `sysctl` with the result type it's to be interpreted as.
91 struct ValueUnion {
92 union DataT {
93 uint32_t uint32_value;
94 uint64_t uint64_value;
95 // For correct aliasing of union members from bytes.
96 char bytes[8];
97 };
98 using DataPtr = std::unique_ptr<DataT, decltype(&std::free)>;
99
100 // The size of the data union member + its trailing array size.
101 size_t Size;
102 DataPtr Buff;
103
104 public:
ValueUnionbenchmark::__anonbc32a9500111::ValueUnion105 ValueUnion() : Size(0), Buff(nullptr, &std::free) {}
106
ValueUnionbenchmark::__anonbc32a9500111::ValueUnion107 explicit ValueUnion(size_t BuffSize)
108 : Size(sizeof(DataT) + BuffSize),
109 Buff(::new (std::malloc(Size)) DataT(), &std::free) {}
110
111 ValueUnion(ValueUnion&& other) = default;
112
operator boolbenchmark::__anonbc32a9500111::ValueUnion113 explicit operator bool() const { return bool(Buff); }
114
databenchmark::__anonbc32a9500111::ValueUnion115 char* data() const { return Buff->bytes; }
116
GetAsStringbenchmark::__anonbc32a9500111::ValueUnion117 std::string GetAsString() const { return std::string(data()); }
118
GetAsIntegerbenchmark::__anonbc32a9500111::ValueUnion119 int64_t GetAsInteger() const {
120 if (Size == sizeof(Buff->uint32_value))
121 return static_cast<int32_t>(Buff->uint32_value);
122 else if (Size == sizeof(Buff->uint64_value))
123 return static_cast<int64_t>(Buff->uint64_value);
124 BENCHMARK_UNREACHABLE();
125 }
126
GetAsUnsignedbenchmark::__anonbc32a9500111::ValueUnion127 uint64_t GetAsUnsigned() const {
128 if (Size == sizeof(Buff->uint32_value))
129 return Buff->uint32_value;
130 else if (Size == sizeof(Buff->uint64_value))
131 return Buff->uint64_value;
132 BENCHMARK_UNREACHABLE();
133 }
134
135 template <class T, int N>
GetAsArraybenchmark::__anonbc32a9500111::ValueUnion136 std::array<T, N> GetAsArray() {
137 const int ArrSize = sizeof(T) * N;
138 CHECK_LE(ArrSize, Size);
139 std::array<T, N> Arr;
140 std::memcpy(Arr.data(), data(), ArrSize);
141 return Arr;
142 }
143 };
144
GetSysctlImp(std::string const & Name)145 ValueUnion GetSysctlImp(std::string const& Name) {
146 #if defined BENCHMARK_OS_OPENBSD
147 int mib[2];
148
149 mib[0] = CTL_HW;
150 if ((Name == "hw.ncpu") || (Name == "hw.cpuspeed")){
151 ValueUnion buff(sizeof(int));
152
153 if (Name == "hw.ncpu") {
154 mib[1] = HW_NCPU;
155 } else {
156 mib[1] = HW_CPUSPEED;
157 }
158
159 if (sysctl(mib, 2, buff.data(), &buff.Size, nullptr, 0) == -1) {
160 return ValueUnion();
161 }
162 return buff;
163 }
164 return ValueUnion();
165 #else
166 size_t CurBuffSize = 0;
167 if (sysctlbyname(Name.c_str(), nullptr, &CurBuffSize, nullptr, 0) == -1)
168 return ValueUnion();
169
170 ValueUnion buff(CurBuffSize);
171 if (sysctlbyname(Name.c_str(), buff.data(), &buff.Size, nullptr, 0) == 0)
172 return buff;
173 return ValueUnion();
174 #endif
175 }
176
177 BENCHMARK_MAYBE_UNUSED
GetSysctl(std::string const & Name,std::string * Out)178 bool GetSysctl(std::string const& Name, std::string* Out) {
179 Out->clear();
180 auto Buff = GetSysctlImp(Name);
181 if (!Buff) return false;
182 Out->assign(Buff.data());
183 return true;
184 }
185
186 template <class Tp,
187 class = typename std::enable_if<std::is_integral<Tp>::value>::type>
GetSysctl(std::string const & Name,Tp * Out)188 bool GetSysctl(std::string const& Name, Tp* Out) {
189 *Out = 0;
190 auto Buff = GetSysctlImp(Name);
191 if (!Buff) return false;
192 *Out = static_cast<Tp>(Buff.GetAsUnsigned());
193 return true;
194 }
195
196 template <class Tp, size_t N>
GetSysctl(std::string const & Name,std::array<Tp,N> * Out)197 bool GetSysctl(std::string const& Name, std::array<Tp, N>* Out) {
198 auto Buff = GetSysctlImp(Name);
199 if (!Buff) return false;
200 *Out = Buff.GetAsArray<Tp, N>();
201 return true;
202 }
203 #endif
204
205 template <class ArgT>
ReadFromFile(std::string const & fname,ArgT * arg)206 bool ReadFromFile(std::string const& fname, ArgT* arg) {
207 *arg = ArgT();
208 std::ifstream f(fname.c_str());
209 if (!f.is_open()) return false;
210 f >> *arg;
211 return f.good();
212 }
213
CpuScaling(int num_cpus)214 CPUInfo::Scaling CpuScaling(int num_cpus) {
215 // We don't have a valid CPU count, so don't even bother.
216 if (num_cpus <= 0) return CPUInfo::Scaling::UNKNOWN;
217 #ifdef BENCHMARK_OS_QNX
218 return CPUInfo::Scaling::UNKNOWN;
219 #endif
220 #ifndef BENCHMARK_OS_WINDOWS
221 // 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");
228 if (ReadFromFile(governor_file, &res) && res != "performance") return CPUInfo::Scaling::ENABLED;
229 }
230 return CPUInfo::Scaling::DISABLED;
231 #endif
232 return CPUInfo::Scaling::UNKNOWN;
233 }
234
CountSetBitsInCPUMap(std::string Val)235 int CountSetBitsInCPUMap(std::string Val) {
236 auto CountBits = [](std::string Part) {
237 using CPUMask = std::bitset<sizeof(std::uintptr_t) * CHAR_BIT>;
238 Part = "0x" + Part;
239 CPUMask Mask(benchmark::stoul(Part, nullptr, 16));
240 return static_cast<int>(Mask.count());
241 };
242 size_t Pos;
243 int total = 0;
244 while ((Pos = Val.find(',')) != std::string::npos) {
245 total += CountBits(Val.substr(0, Pos));
246 Val = Val.substr(Pos + 1);
247 }
248 if (!Val.empty()) {
249 total += CountBits(Val);
250 }
251 return total;
252 }
253
254 BENCHMARK_MAYBE_UNUSED
GetCacheSizesFromKVFS()255 std::vector<CPUInfo::CacheInfo> GetCacheSizesFromKVFS() {
256 std::vector<CPUInfo::CacheInfo> res;
257 std::string dir = "/sys/devices/system/cpu/cpu0/cache/";
258 int Idx = 0;
259 while (true) {
260 CPUInfo::CacheInfo info;
261 std::string FPath = StrCat(dir, "index", Idx++, "/");
262 std::ifstream f(StrCat(FPath, "size").c_str());
263 if (!f.is_open()) break;
264 std::string suffix;
265 f >> info.size;
266 if (f.fail())
267 PrintErrorAndDie("Failed while reading file '", FPath, "size'");
268 if (f.good()) {
269 f >> suffix;
270 if (f.bad())
271 PrintErrorAndDie(
272 "Invalid cache size format: failed to read size suffix");
273 else if (f && suffix != "K")
274 PrintErrorAndDie("Invalid cache size format: Expected bytes ", suffix);
275 else if (suffix == "K")
276 info.size *= 1024;
277 }
278 if (!ReadFromFile(StrCat(FPath, "type"), &info.type))
279 PrintErrorAndDie("Failed to read from file ", FPath, "type");
280 if (!ReadFromFile(StrCat(FPath, "level"), &info.level))
281 PrintErrorAndDie("Failed to read from file ", FPath, "level");
282 std::string map_str;
283 if (!ReadFromFile(StrCat(FPath, "shared_cpu_map"), &map_str))
284 PrintErrorAndDie("Failed to read from file ", FPath, "shared_cpu_map");
285 info.num_sharing = CountSetBitsInCPUMap(map_str);
286 res.push_back(info);
287 }
288
289 return res;
290 }
291
292 #ifdef BENCHMARK_OS_MACOSX
GetCacheSizesMacOSX()293 std::vector<CPUInfo::CacheInfo> GetCacheSizesMacOSX() {
294 std::vector<CPUInfo::CacheInfo> res;
295 std::array<uint64_t, 4> CacheCounts{{0, 0, 0, 0}};
296 GetSysctl("hw.cacheconfig", &CacheCounts);
297
298 struct {
299 std::string name;
300 std::string type;
301 int level;
302 uint64_t num_sharing;
303 } Cases[] = {{"hw.l1dcachesize", "Data", 1, CacheCounts[1]},
304 {"hw.l1icachesize", "Instruction", 1, CacheCounts[1]},
305 {"hw.l2cachesize", "Unified", 2, CacheCounts[2]},
306 {"hw.l3cachesize", "Unified", 3, CacheCounts[3]}};
307 for (auto& C : Cases) {
308 int val;
309 if (!GetSysctl(C.name, &val)) continue;
310 CPUInfo::CacheInfo info;
311 info.type = C.type;
312 info.level = C.level;
313 info.size = val;
314 info.num_sharing = static_cast<int>(C.num_sharing);
315 res.push_back(std::move(info));
316 }
317 return res;
318 }
319 #elif defined(BENCHMARK_OS_WINDOWS)
GetCacheSizesWindows()320 std::vector<CPUInfo::CacheInfo> GetCacheSizesWindows() {
321 std::vector<CPUInfo::CacheInfo> res;
322 DWORD buffer_size = 0;
323 using PInfo = SYSTEM_LOGICAL_PROCESSOR_INFORMATION;
324 using CInfo = CACHE_DESCRIPTOR;
325
326 using UPtr = std::unique_ptr<PInfo, decltype(&std::free)>;
327 GetLogicalProcessorInformation(nullptr, &buffer_size);
328 UPtr buff((PInfo*)malloc(buffer_size), &std::free);
329 if (!GetLogicalProcessorInformation(buff.get(), &buffer_size))
330 PrintErrorAndDie("Failed during call to GetLogicalProcessorInformation: ",
331 GetLastError());
332
333 PInfo* it = buff.get();
334 PInfo* end = buff.get() + (buffer_size / sizeof(PInfo));
335
336 for (; it != end; ++it) {
337 if (it->Relationship != RelationCache) continue;
338 using BitSet = std::bitset<sizeof(ULONG_PTR) * CHAR_BIT>;
339 BitSet B(it->ProcessorMask);
340 // To prevent duplicates, only consider caches where CPU 0 is specified
341 if (!B.test(0)) continue;
342 CInfo* Cache = &it->Cache;
343 CPUInfo::CacheInfo C;
344 C.num_sharing = static_cast<int>(B.count());
345 C.level = Cache->Level;
346 C.size = Cache->Size;
347 switch (Cache->Type) {
348 case CacheUnified:
349 C.type = "Unified";
350 break;
351 case CacheInstruction:
352 C.type = "Instruction";
353 break;
354 case CacheData:
355 C.type = "Data";
356 break;
357 case CacheTrace:
358 C.type = "Trace";
359 break;
360 default:
361 C.type = "Unknown";
362 break;
363 }
364 res.push_back(C);
365 }
366 return res;
367 }
368 #elif BENCHMARK_OS_QNX
GetCacheSizesQNX()369 std::vector<CPUInfo::CacheInfo> GetCacheSizesQNX() {
370 std::vector<CPUInfo::CacheInfo> res;
371 struct cacheattr_entry *cache = SYSPAGE_ENTRY(cacheattr);
372 uint32_t const elsize = SYSPAGE_ELEMENT_SIZE(cacheattr);
373 int num = SYSPAGE_ENTRY_SIZE(cacheattr) / elsize ;
374 for(int i = 0; i < num; ++i ) {
375 CPUInfo::CacheInfo info;
376 switch (cache->flags){
377 case CACHE_FLAG_INSTR :
378 info.type = "Instruction";
379 info.level = 1;
380 break;
381 case CACHE_FLAG_DATA :
382 info.type = "Data";
383 info.level = 1;
384 break;
385 case CACHE_FLAG_UNIFIED :
386 info.type = "Unified";
387 info.level = 2;
388 break;
389 case CACHE_FLAG_SHARED :
390 info.type = "Shared";
391 info.level = 3;
392 break;
393 default :
394 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 }
404 #endif
405
GetCacheSizes()406 std::vector<CPUInfo::CacheInfo> GetCacheSizes() {
407 #ifdef BENCHMARK_OS_MACOSX
408 return GetCacheSizesMacOSX();
409 #elif defined(BENCHMARK_OS_WINDOWS)
410 return GetCacheSizesWindows();
411 #elif defined(BENCHMARK_OS_QNX)
412 return GetCacheSizesQNX();
413 #else
414 return GetCacheSizesFromKVFS();
415 #endif
416 }
417
GetSystemName()418 std::string GetSystemName() {
419 #if defined(BENCHMARK_OS_WINDOWS)
420 std::string str;
421 const unsigned COUNT = MAX_COMPUTERNAME_LENGTH+1;
422 TCHAR hostname[COUNT] = {'\0'};
423 DWORD DWCOUNT = COUNT;
424 if (!GetComputerName(hostname, &DWCOUNT))
425 return std::string("");
426 #ifndef UNICODE
427 str = std::string(hostname, DWCOUNT);
428 #else
429 //Using wstring_convert, Is deprecated in C++17
430 using convert_type = std::codecvt_utf8<wchar_t>;
431 std::wstring_convert<convert_type, wchar_t> converter;
432 std::wstring wStr(hostname, DWCOUNT);
433 str = converter.to_bytes(wStr);
434 #endif
435 return str;
436 #else // defined(BENCHMARK_OS_WINDOWS)
437 #ifndef HOST_NAME_MAX
438 #ifdef BENCHMARK_HAS_SYSCTL // BSD/Mac Doesnt have HOST_NAME_MAX defined
439 #define HOST_NAME_MAX 64
440 #elif defined(BENCHMARK_OS_NACL)
441 #define HOST_NAME_MAX 64
442 #elif defined(BENCHMARK_OS_QNX)
443 #define HOST_NAME_MAX 154
444 #elif defined(BENCHMARK_OS_RTEMS)
445 #define HOST_NAME_MAX 256
446 #else
447 #warning "HOST_NAME_MAX not defined. using 64"
448 #define HOST_NAME_MAX 64
449 #endif
450 #endif // def HOST_NAME_MAX
451 char hostname[HOST_NAME_MAX];
452 int retVal = gethostname(hostname, HOST_NAME_MAX);
453 if (retVal != 0) return std::string("");
454 return std::string(hostname);
455 #endif // Catch-all POSIX block.
456 }
457
GetNumCPUs()458 int GetNumCPUs() {
459 #ifdef BENCHMARK_HAS_SYSCTL
460 int NumCPU = -1;
461 if (GetSysctl("hw.ncpu", &NumCPU)) return NumCPU;
462 fprintf(stderr, "Err: %s\n", strerror(errno));
463 std::exit(EXIT_FAILURE);
464 #elif defined(BENCHMARK_OS_WINDOWS)
465 SYSTEM_INFO sysinfo;
466 // Use memset as opposed to = {} to avoid GCC missing initializer false
467 // positives.
468 std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
469 GetSystemInfo(&sysinfo);
470 return sysinfo.dwNumberOfProcessors; // number of logical
471 // processors in the current
472 // group
473 #elif defined(BENCHMARK_OS_SOLARIS)
474 // Returns -1 in case of a failure.
475 int NumCPU = sysconf(_SC_NPROCESSORS_ONLN);
476 if (NumCPU < 0) {
477 fprintf(stderr,
478 "sysconf(_SC_NPROCESSORS_ONLN) failed with error: %s\n",
479 strerror(errno));
480 }
481 return NumCPU;
482 #elif defined(BENCHMARK_OS_QNX)
483 return static_cast<int>(_syspage_ptr->num_cpu);
484 #else
485 int NumCPUs = 0;
486 int MaxID = -1;
487 std::ifstream f("/proc/cpuinfo");
488 if (!f.is_open()) {
489 std::cerr << "failed to open /proc/cpuinfo\n";
490 return -1;
491 }
492 const std::string Key = "processor";
493 std::string ln;
494 while (std::getline(f, ln)) {
495 if (ln.empty()) continue;
496 size_t SplitIdx = ln.find(':');
497 std::string value;
498 #if defined(__s390__)
499 // s390 has another format in /proc/cpuinfo
500 // it needs to be parsed differently
501 if (SplitIdx != std::string::npos) value = ln.substr(Key.size()+1,SplitIdx-Key.size()-1);
502 #else
503 if (SplitIdx != std::string::npos) value = ln.substr(SplitIdx + 1);
504 #endif
505 if (ln.size() >= Key.size() && ln.compare(0, Key.size(), Key) == 0) {
506 NumCPUs++;
507 if (!value.empty()) {
508 int CurID = benchmark::stoi(value);
509 MaxID = std::max(CurID, MaxID);
510 }
511 }
512 }
513 if (f.bad()) {
514 std::cerr << "Failure reading /proc/cpuinfo\n";
515 return -1;
516 }
517 if (!f.eof()) {
518 std::cerr << "Failed to read to end of /proc/cpuinfo\n";
519 return -1;
520 }
521 f.close();
522
523 if ((MaxID + 1) != NumCPUs) {
524 fprintf(stderr,
525 "CPU ID assignments in /proc/cpuinfo seem messed up."
526 " This is usually caused by a bad BIOS.\n");
527 }
528 return NumCPUs;
529 #endif
530 BENCHMARK_UNREACHABLE();
531 }
532
GetCPUCyclesPerSecond()533 double GetCPUCyclesPerSecond() {
534 #if defined BENCHMARK_OS_LINUX || defined BENCHMARK_OS_CYGWIN
535 long freq;
536
537 // If the kernel is exporting the tsc frequency use that. There are issues
538 // where cpuinfo_max_freq cannot be relied on because the BIOS may be
539 // exporintg an invalid p-state (on x86) or p-states may be used to put the
540 // processor in a new mode (turbo mode). Essentially, those frequencies
541 // cannot always be relied upon. The same reasons apply to /proc/cpuinfo as
542 // well.
543 if (ReadFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz", &freq)
544 // If CPU scaling is in effect, we want to use the *maximum* frequency,
545 // not whatever CPU speed some random processor happens to be using now.
546 || ReadFromFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
547 &freq)) {
548 // The value is in kHz (as the file name suggests). For example, on a
549 // 2GHz warpstation, the file contains the value "2000000".
550 return freq * 1000.0;
551 }
552
553 const double error_value = -1;
554 double bogo_clock = error_value;
555
556 std::ifstream f("/proc/cpuinfo");
557 if (!f.is_open()) {
558 std::cerr << "failed to open /proc/cpuinfo\n";
559 return error_value;
560 }
561
562 auto startsWithKey = [](std::string const& Value, std::string const& Key) {
563 if (Key.size() > Value.size()) return false;
564 auto Cmp = [&](char X, char Y) {
565 return std::tolower(X) == std::tolower(Y);
566 };
567 return std::equal(Key.begin(), Key.end(), Value.begin(), Cmp);
568 };
569
570 std::string ln;
571 while (std::getline(f, ln)) {
572 if (ln.empty()) continue;
573 size_t SplitIdx = ln.find(':');
574 std::string value;
575 if (SplitIdx != std::string::npos) value = ln.substr(SplitIdx + 1);
576 // When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only
577 // accept positive values. Some environments (virtual machines) report zero,
578 // which would cause infinite looping in WallTime_Init.
579 if (startsWithKey(ln, "cpu MHz")) {
580 if (!value.empty()) {
581 double cycles_per_second = benchmark::stod(value) * 1000000.0;
582 if (cycles_per_second > 0) return cycles_per_second;
583 }
584 } else if (startsWithKey(ln, "bogomips")) {
585 if (!value.empty()) {
586 bogo_clock = benchmark::stod(value) * 1000000.0;
587 if (bogo_clock < 0.0) bogo_clock = error_value;
588 }
589 }
590 }
591 if (f.bad()) {
592 std::cerr << "Failure reading /proc/cpuinfo\n";
593 return error_value;
594 }
595 if (!f.eof()) {
596 std::cerr << "Failed to read to end of /proc/cpuinfo\n";
597 return error_value;
598 }
599 f.close();
600 // If we found the bogomips clock, but nothing better, we'll use it (but
601 // we're not happy about it); otherwise, fallback to the rough estimation
602 // below.
603 if (bogo_clock >= 0.0) return bogo_clock;
604
605 #elif defined BENCHMARK_HAS_SYSCTL
606 constexpr auto* FreqStr =
607 #if defined(BENCHMARK_OS_FREEBSD) || defined(BENCHMARK_OS_NETBSD)
608 "machdep.tsc_freq";
609 #elif defined BENCHMARK_OS_OPENBSD
610 "hw.cpuspeed";
611 #elif defined BENCHMARK_OS_DRAGONFLY
612 "hw.tsc_frequency";
613 #else
614 "hw.cpufrequency";
615 #endif
616 unsigned long long hz = 0;
617 #if defined BENCHMARK_OS_OPENBSD
618 if (GetSysctl(FreqStr, &hz)) return hz * 1000000;
619 #else
620 if (GetSysctl(FreqStr, &hz)) return hz;
621 #endif
622 fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n",
623 FreqStr, strerror(errno));
624
625 #elif defined BENCHMARK_OS_WINDOWS
626 // In NT, read MHz from the registry. If we fail to do so or we're in win9x
627 // then make a crude estimate.
628 DWORD data, data_size = sizeof(data);
629 if (IsWindowsXPOrGreater() &&
630 SUCCEEDED(
631 SHGetValueA(HKEY_LOCAL_MACHINE,
632 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
633 "~MHz", nullptr, &data, &data_size)))
634 return static_cast<double>((int64_t)data *
635 (int64_t)(1000 * 1000)); // was mhz
636 #elif defined (BENCHMARK_OS_SOLARIS)
637 kstat_ctl_t *kc = kstat_open();
638 if (!kc) {
639 std::cerr << "failed to open /dev/kstat\n";
640 return -1;
641 }
642 kstat_t *ksp = kstat_lookup(kc, (char*)"cpu_info", -1, (char*)"cpu_info0");
643 if (!ksp) {
644 std::cerr << "failed to lookup in /dev/kstat\n";
645 return -1;
646 }
647 if (kstat_read(kc, ksp, NULL) < 0) {
648 std::cerr << "failed to read from /dev/kstat\n";
649 return -1;
650 }
651 kstat_named_t *knp =
652 (kstat_named_t*)kstat_data_lookup(ksp, (char*)"current_clock_Hz");
653 if (!knp) {
654 std::cerr << "failed to lookup data in /dev/kstat\n";
655 return -1;
656 }
657 if (knp->data_type != KSTAT_DATA_UINT64) {
658 std::cerr << "current_clock_Hz is of unexpected data type: "
659 << knp->data_type << "\n";
660 return -1;
661 }
662 double clock_hz = knp->value.ui64;
663 kstat_close(kc);
664 return clock_hz;
665 #elif defined (BENCHMARK_OS_QNX)
666 return static_cast<double>((int64_t)(SYSPAGE_ENTRY(cpuinfo)->speed) *
667 (int64_t)(1000 * 1000));
668 #endif
669 // If we've fallen through, attempt to roughly estimate the CPU clock rate.
670 const int estimate_time_ms = 1000;
671 const auto start_ticks = cycleclock::Now();
672 SleepForMilliseconds(estimate_time_ms);
673 return static_cast<double>(cycleclock::Now() - start_ticks);
674 }
675
GetLoadAvg()676 std::vector<double> GetLoadAvg() {
677 #if (defined BENCHMARK_OS_FREEBSD || defined(BENCHMARK_OS_LINUX) || \
678 defined BENCHMARK_OS_MACOSX || defined BENCHMARK_OS_NETBSD || \
679 defined BENCHMARK_OS_OPENBSD || defined BENCHMARK_OS_DRAGONFLY) && \
680 !defined(__ANDROID__)
681 constexpr int kMaxSamples = 3;
682 std::vector<double> res(kMaxSamples, 0.0);
683 const int nelem = getloadavg(res.data(), kMaxSamples);
684 if (nelem < 1) {
685 res.clear();
686 } else {
687 res.resize(nelem);
688 }
689 return res;
690 #else
691 return {};
692 #endif
693 }
694
695 } // end namespace
696
Get()697 const CPUInfo& CPUInfo::Get() {
698 static const CPUInfo* info = new CPUInfo();
699 return *info;
700 }
701
CPUInfo()702 CPUInfo::CPUInfo()
703 : num_cpus(GetNumCPUs()),
704 cycles_per_second(GetCPUCyclesPerSecond()),
705 caches(GetCacheSizes()),
706 scaling(CpuScaling(num_cpus)),
707 load_avg(GetLoadAvg()) {}
708
709
Get()710 const SystemInfo& SystemInfo::Get() {
711 static const SystemInfo* info = new SystemInfo();
712 return *info;
713 }
714
SystemInfo()715 SystemInfo::SystemInfo() : name(GetSystemName()) {}
716 } // end namespace benchmark
717