blob: 73064b97ba2209a9c848bc198f36d5337af14d54 [file] [log] [blame]
Eric Fiselierb08d8b12016-07-19 23:07:03 +00001// 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:03 +000015#include "internal_macros.h"
16
17#ifdef BENCHMARK_OS_WINDOWS
18#include <Shlwapi.h>
Eric Fiselierfcafd3e2018-07-10 04:02:00 +000019#undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA
Eric Fiselierb08d8b12016-07-19 23:07:03 +000020#include <VersionHelpers.h>
Eric Fiselierfbc9ff22016-11-05 00:30:27 +000021#include <Windows.h>
Eric Fiselierb08d8b12016-07-19 23:07:03 +000022#else
23#include <fcntl.h>
Eric Fiselierfcafd3e2018-07-10 04:02:00 +000024#ifndef BENCHMARK_OS_FUCHSIA
Eric Fiselierb08d8b12016-07-19 23:07:03 +000025#include <sys/resource.h>
Eric Fiselierfcafd3e2018-07-10 04:02:00 +000026#endif
Eric Fiselierb08d8b12016-07-19 23:07:03 +000027#include <sys/time.h>
Eric Fiselierfbc9ff22016-11-05 00:30:27 +000028#include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD
Eric Fiselierb08d8b12016-07-19 23:07:03 +000029#include <unistd.h>
Eric Fiselier19039762018-01-18 04:23:01 +000030#if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX || \
Eric Fiselierfcafd3e2018-07-10 04:02:00 +000031 defined BENCHMARK_OS_NETBSD || defined BENCHMARK_OS_OPENBSD
Eric Fiselier19039762018-01-18 04:23:01 +000032#define BENCHMARK_HAS_SYSCTL
Eric Fiselierb08d8b12016-07-19 23:07:03 +000033#include <sys/sysctl.h>
34#endif
35#endif
Eric Fiselierfcafd3e2018-07-10 04:02:00 +000036#if defined(BENCHMARK_OS_SOLARIS)
37#include <kstat.h>
38#endif
Eric Fiselierb08d8b12016-07-19 23:07:03 +000039
Eric Fiselier19039762018-01-18 04:23:01 +000040#include <algorithm>
41#include <array>
42#include <bitset>
Eric Fiselierb08d8b12016-07-19 23:07:03 +000043#include <cerrno>
Eric Fiselier19039762018-01-18 04:23:01 +000044#include <climits>
Eric Fiselierb08d8b12016-07-19 23:07:03 +000045#include <cstdint>
Eric Fiselierfbc9ff22016-11-05 00:30:27 +000046#include <cstdio>
Eric Fiselierb08d8b12016-07-19 23:07:03 +000047#include <cstdlib>
48#include <cstring>
Eric Fiselier19039762018-01-18 04:23:01 +000049#include <fstream>
Eric Fiselierb08d8b12016-07-19 23:07:03 +000050#include <iostream>
Eric Fiselier19039762018-01-18 04:23:01 +000051#include <iterator>
Eric Fiselierb08d8b12016-07-19 23:07:03 +000052#include <limits>
Eric Fiselier19039762018-01-18 04:23:01 +000053#include <memory>
54#include <sstream>
Eric Fiselierb08d8b12016-07-19 23:07:03 +000055
Eric Fiselierb08d8b12016-07-19 23:07:03 +000056#include "check.h"
57#include "cycleclock.h"
58#include "internal_macros.h"
59#include "log.h"
60#include "sleep.h"
61#include "string_util.h"
62
63namespace benchmark {
64namespace {
Eric Fiselierb08d8b12016-07-19 23:07:03 +000065
Eric Fiselier19039762018-01-18 04:23:01 +000066void PrintImp(std::ostream& out) { out << std::endl; }
Eric Fiselierb08d8b12016-07-19 23:07:03 +000067
Eric Fiselier19039762018-01-18 04:23:01 +000068template <class First, class... Rest>
69void PrintImp(std::ostream& out, First&& f, Rest&&... rest) {
70 out << std::forward<First>(f);
71 PrintImp(out, std::forward<Rest>(rest)...);
72}
73
74template <class... Args>
75BENCHMARK_NORETURN void PrintErrorAndDie(Args&&... args) {
76 PrintImp(std::cerr, std::forward<Args>(args)...);
77 std::exit(EXIT_FAILURE);
78}
79
80#ifdef BENCHMARK_HAS_SYSCTL
81
82/// ValueUnion - A type used to correctly alias the byte-for-byte output of
83/// `sysctl` with the result type it's to be interpreted as.
84struct ValueUnion {
85 union DataT {
86 uint32_t uint32_value;
87 uint64_t uint64_value;
88 // For correct aliasing of union members from bytes.
89 char bytes[8];
90 };
91 using DataPtr = std::unique_ptr<DataT, decltype(&std::free)>;
92
93 // The size of the data union member + its trailing array size.
94 size_t Size;
95 DataPtr Buff;
96
97 public:
98 ValueUnion() : Size(0), Buff(nullptr, &std::free) {}
99
100 explicit ValueUnion(size_t BuffSize)
101 : Size(sizeof(DataT) + BuffSize),
102 Buff(::new (std::malloc(Size)) DataT(), &std::free) {}
103
104 ValueUnion(ValueUnion&& other) = default;
105
106 explicit operator bool() const { return bool(Buff); }
107
108 char* data() const { return Buff->bytes; }
109
110 std::string GetAsString() const { return std::string(data()); }
111
112 int64_t GetAsInteger() const {
113 if (Size == sizeof(Buff->uint32_value))
114 return static_cast<int32_t>(Buff->uint32_value);
115 else if (Size == sizeof(Buff->uint64_value))
116 return static_cast<int64_t>(Buff->uint64_value);
117 BENCHMARK_UNREACHABLE();
118 }
119
120 uint64_t GetAsUnsigned() const {
121 if (Size == sizeof(Buff->uint32_value))
122 return Buff->uint32_value;
123 else if (Size == sizeof(Buff->uint64_value))
124 return Buff->uint64_value;
125 BENCHMARK_UNREACHABLE();
126 }
127
128 template <class T, int N>
129 std::array<T, N> GetAsArray() {
130 const int ArrSize = sizeof(T) * N;
131 CHECK_LE(ArrSize, Size);
132 std::array<T, N> Arr;
133 std::memcpy(Arr.data(), data(), ArrSize);
134 return Arr;
135 }
136};
137
138ValueUnion GetSysctlImp(std::string const& Name) {
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000139#if defined BENCHMARK_OS_OPENBSD
140 int mib[2];
141
142 mib[0] = CTL_HW;
143 if ((Name == "hw.ncpu") || (Name == "hw.cpuspeed")){
144 ValueUnion buff(sizeof(int));
145
146 if (Name == "hw.ncpu") {
147 mib[1] = HW_NCPU;
148 } else {
149 mib[1] = HW_CPUSPEED;
150 }
151
152 if (sysctl(mib, 2, buff.data(), &buff.Size, nullptr, 0) == -1) {
153 return ValueUnion();
154 }
155 return buff;
156 }
157 return ValueUnion();
158#else
Eric Fiselier19039762018-01-18 04:23:01 +0000159 size_t CurBuffSize = 0;
160 if (sysctlbyname(Name.c_str(), nullptr, &CurBuffSize, nullptr, 0) == -1)
161 return ValueUnion();
162
163 ValueUnion buff(CurBuffSize);
164 if (sysctlbyname(Name.c_str(), buff.data(), &buff.Size, nullptr, 0) == 0)
165 return buff;
166 return ValueUnion();
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000167#endif
Eric Fiselier19039762018-01-18 04:23:01 +0000168}
169
170BENCHMARK_MAYBE_UNUSED
171bool GetSysctl(std::string const& Name, std::string* Out) {
172 Out->clear();
173 auto Buff = GetSysctlImp(Name);
174 if (!Buff) return false;
175 Out->assign(Buff.data());
176 return true;
177}
178
179template <class Tp,
180 class = typename std::enable_if<std::is_integral<Tp>::value>::type>
181bool GetSysctl(std::string const& Name, Tp* Out) {
182 *Out = 0;
183 auto Buff = GetSysctlImp(Name);
184 if (!Buff) return false;
185 *Out = static_cast<Tp>(Buff.GetAsUnsigned());
186 return true;
187}
188
189template <class Tp, size_t N>
190bool GetSysctl(std::string const& Name, std::array<Tp, N>* Out) {
191 auto Buff = GetSysctlImp(Name);
192 if (!Buff) return false;
193 *Out = Buff.GetAsArray<Tp, N>();
194 return true;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000195}
196#endif
197
Eric Fiselier19039762018-01-18 04:23:01 +0000198template <class ArgT>
199bool ReadFromFile(std::string const& fname, ArgT* arg) {
200 *arg = ArgT();
201 std::ifstream f(fname.c_str());
202 if (!f.is_open()) return false;
203 f >> *arg;
204 return f.good();
205}
206
207bool CpuScalingEnabled(int num_cpus) {
208 // We don't have a valid CPU count, so don't even bother.
209 if (num_cpus <= 0) return false;
210#ifndef BENCHMARK_OS_WINDOWS
211 // On Linux, the CPUfreq subsystem exposes CPU information as files on the
212 // local file system. If reading the exported files fails, then we may not be
213 // running on Linux, so we silently ignore all the read errors.
214 std::string res;
215 for (int cpu = 0; cpu < num_cpus; ++cpu) {
216 std::string governor_file =
217 StrCat("/sys/devices/system/cpu/cpu", cpu, "/cpufreq/scaling_governor");
218 if (ReadFromFile(governor_file, &res) && res != "performance") return true;
219 }
220#endif
221 return false;
222}
223
224int CountSetBitsInCPUMap(std::string Val) {
225 auto CountBits = [](std::string Part) {
226 using CPUMask = std::bitset<sizeof(std::uintptr_t) * CHAR_BIT>;
227 Part = "0x" + Part;
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000228 CPUMask Mask(benchmark::stoul(Part, nullptr, 16));
Eric Fiselier19039762018-01-18 04:23:01 +0000229 return static_cast<int>(Mask.count());
230 };
231 size_t Pos;
232 int total = 0;
233 while ((Pos = Val.find(',')) != std::string::npos) {
234 total += CountBits(Val.substr(0, Pos));
235 Val = Val.substr(Pos + 1);
236 }
237 if (!Val.empty()) {
238 total += CountBits(Val);
239 }
240 return total;
241}
242
243BENCHMARK_MAYBE_UNUSED
244std::vector<CPUInfo::CacheInfo> GetCacheSizesFromKVFS() {
245 std::vector<CPUInfo::CacheInfo> res;
246 std::string dir = "/sys/devices/system/cpu/cpu0/cache/";
247 int Idx = 0;
248 while (true) {
249 CPUInfo::CacheInfo info;
250 std::string FPath = StrCat(dir, "index", Idx++, "/");
251 std::ifstream f(StrCat(FPath, "size").c_str());
252 if (!f.is_open()) break;
253 std::string suffix;
254 f >> info.size;
255 if (f.fail())
256 PrintErrorAndDie("Failed while reading file '", FPath, "size'");
257 if (f.good()) {
258 f >> suffix;
259 if (f.bad())
260 PrintErrorAndDie(
261 "Invalid cache size format: failed to read size suffix");
262 else if (f && suffix != "K")
263 PrintErrorAndDie("Invalid cache size format: Expected bytes ", suffix);
264 else if (suffix == "K")
265 info.size *= 1000;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000266 }
Eric Fiselier19039762018-01-18 04:23:01 +0000267 if (!ReadFromFile(StrCat(FPath, "type"), &info.type))
268 PrintErrorAndDie("Failed to read from file ", FPath, "type");
269 if (!ReadFromFile(StrCat(FPath, "level"), &info.level))
270 PrintErrorAndDie("Failed to read from file ", FPath, "level");
271 std::string map_str;
272 if (!ReadFromFile(StrCat(FPath, "shared_cpu_map"), &map_str))
273 PrintErrorAndDie("Failed to read from file ", FPath, "shared_cpu_map");
274 info.num_sharing = CountSetBitsInCPUMap(map_str);
275 res.push_back(info);
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000276 }
Eric Fiselier19039762018-01-18 04:23:01 +0000277
278 return res;
279}
280
281#ifdef BENCHMARK_OS_MACOSX
282std::vector<CPUInfo::CacheInfo> GetCacheSizesMacOSX() {
283 std::vector<CPUInfo::CacheInfo> res;
284 std::array<uint64_t, 4> CacheCounts{{0, 0, 0, 0}};
285 GetSysctl("hw.cacheconfig", &CacheCounts);
286
287 struct {
288 std::string name;
289 std::string type;
290 int level;
291 size_t num_sharing;
292 } Cases[] = {{"hw.l1dcachesize", "Data", 1, CacheCounts[1]},
293 {"hw.l1icachesize", "Instruction", 1, CacheCounts[1]},
294 {"hw.l2cachesize", "Unified", 2, CacheCounts[2]},
295 {"hw.l3cachesize", "Unified", 3, CacheCounts[3]}};
296 for (auto& C : Cases) {
297 int val;
298 if (!GetSysctl(C.name, &val)) continue;
299 CPUInfo::CacheInfo info;
300 info.type = C.type;
301 info.level = C.level;
302 info.size = val;
303 info.num_sharing = static_cast<int>(C.num_sharing);
304 res.push_back(std::move(info));
305 }
306 return res;
307}
308#elif defined(BENCHMARK_OS_WINDOWS)
309std::vector<CPUInfo::CacheInfo> GetCacheSizesWindows() {
310 std::vector<CPUInfo::CacheInfo> res;
311 DWORD buffer_size = 0;
312 using PInfo = SYSTEM_LOGICAL_PROCESSOR_INFORMATION;
313 using CInfo = CACHE_DESCRIPTOR;
314
315 using UPtr = std::unique_ptr<PInfo, decltype(&std::free)>;
316 GetLogicalProcessorInformation(nullptr, &buffer_size);
317 UPtr buff((PInfo*)malloc(buffer_size), &std::free);
318 if (!GetLogicalProcessorInformation(buff.get(), &buffer_size))
319 PrintErrorAndDie("Failed during call to GetLogicalProcessorInformation: ",
320 GetLastError());
321
322 PInfo* it = buff.get();
323 PInfo* end = buff.get() + (buffer_size / sizeof(PInfo));
324
325 for (; it != end; ++it) {
326 if (it->Relationship != RelationCache) continue;
327 using BitSet = std::bitset<sizeof(ULONG_PTR) * CHAR_BIT>;
328 BitSet B(it->ProcessorMask);
329 // To prevent duplicates, only consider caches where CPU 0 is specified
330 if (!B.test(0)) continue;
331 CInfo* Cache = &it->Cache;
332 CPUInfo::CacheInfo C;
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000333 C.num_sharing = static_cast<int>(B.count());
Eric Fiselier19039762018-01-18 04:23:01 +0000334 C.level = Cache->Level;
335 C.size = Cache->Size;
336 switch (Cache->Type) {
337 case CacheUnified:
338 C.type = "Unified";
339 break;
340 case CacheInstruction:
341 C.type = "Instruction";
342 break;
343 case CacheData:
344 C.type = "Data";
345 break;
346 case CacheTrace:
347 C.type = "Trace";
348 break;
349 default:
350 C.type = "Unknown";
351 break;
352 }
353 res.push_back(C);
354 }
355 return res;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000356}
357#endif
358
Eric Fiselier19039762018-01-18 04:23:01 +0000359std::vector<CPUInfo::CacheInfo> GetCacheSizes() {
360#ifdef BENCHMARK_OS_MACOSX
361 return GetCacheSizesMacOSX();
362#elif defined(BENCHMARK_OS_WINDOWS)
363 return GetCacheSizesWindows();
364#else
365 return GetCacheSizesFromKVFS();
Eric Fiselierfbc9ff22016-11-05 00:30:27 +0000366#endif
Eric Fiselier19039762018-01-18 04:23:01 +0000367}
Eric Fiselierfbc9ff22016-11-05 00:30:27 +0000368
Eric Fiselier19039762018-01-18 04:23:01 +0000369int GetNumCPUs() {
370#ifdef BENCHMARK_HAS_SYSCTL
371 int NumCPU = -1;
372 if (GetSysctl("hw.ncpu", &NumCPU)) return NumCPU;
373 fprintf(stderr, "Err: %s\n", strerror(errno));
374 std::exit(EXIT_FAILURE);
375#elif defined(BENCHMARK_OS_WINDOWS)
376 SYSTEM_INFO sysinfo;
377 // Use memset as opposed to = {} to avoid GCC missing initializer false
378 // positives.
379 std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
380 GetSystemInfo(&sysinfo);
381 return sysinfo.dwNumberOfProcessors; // number of logical
382 // processors in the current
383 // group
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000384#elif defined(BENCHMARK_OS_SOLARIS)
385 // Returns -1 in case of a failure.
386 int NumCPU = sysconf(_SC_NPROCESSORS_ONLN);
387 if (NumCPU < 0) {
388 fprintf(stderr,
389 "sysconf(_SC_NPROCESSORS_ONLN) failed with error: %s\n",
390 strerror(errno));
391 }
392 return NumCPU;
Eric Fiselier19039762018-01-18 04:23:01 +0000393#else
394 int NumCPUs = 0;
395 int MaxID = -1;
396 std::ifstream f("/proc/cpuinfo");
397 if (!f.is_open()) {
398 std::cerr << "failed to open /proc/cpuinfo\n";
399 return -1;
400 }
401 const std::string Key = "processor";
402 std::string ln;
403 while (std::getline(f, ln)) {
404 if (ln.empty()) continue;
405 size_t SplitIdx = ln.find(':');
406 std::string value;
407 if (SplitIdx != std::string::npos) value = ln.substr(SplitIdx + 1);
408 if (ln.size() >= Key.size() && ln.compare(0, Key.size(), Key) == 0) {
409 NumCPUs++;
410 if (!value.empty()) {
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000411 int CurID = benchmark::stoi(value);
Eric Fiselier19039762018-01-18 04:23:01 +0000412 MaxID = std::max(CurID, MaxID);
413 }
414 }
415 }
416 if (f.bad()) {
417 std::cerr << "Failure reading /proc/cpuinfo\n";
418 return -1;
419 }
420 if (!f.eof()) {
421 std::cerr << "Failed to read to end of /proc/cpuinfo\n";
422 return -1;
423 }
424 f.close();
425
426 if ((MaxID + 1) != NumCPUs) {
427 fprintf(stderr,
428 "CPU ID assignments in /proc/cpuinfo seem messed up."
429 " This is usually caused by a bad BIOS.\n");
430 }
431 return NumCPUs;
432#endif
433 BENCHMARK_UNREACHABLE();
434}
435
436double GetCPUCyclesPerSecond() {
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000437#if defined BENCHMARK_OS_LINUX || defined BENCHMARK_OS_CYGWIN
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000438 long freq;
439
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000440 // If the kernel is exporting the tsc frequency use that. There are issues
441 // where cpuinfo_max_freq cannot be relied on because the BIOS may be
442 // exporintg an invalid p-state (on x86) or p-states may be used to put the
443 // processor in a new mode (turbo mode). Essentially, those frequencies
444 // cannot always be relied upon. The same reasons apply to /proc/cpuinfo as
445 // well.
Eric Fiselier19039762018-01-18 04:23:01 +0000446 if (ReadFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz", &freq)
447 // If CPU scaling is in effect, we want to use the *maximum* frequency,
448 // not whatever CPU speed some random processor happens to be using now.
449 || ReadFromFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
450 &freq)) {
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000451 // The value is in kHz (as the file name suggests). For example, on a
452 // 2GHz warpstation, the file contains the value "2000000".
Eric Fiselier19039762018-01-18 04:23:01 +0000453 return freq * 1000.0;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000454 }
455
Eric Fiselier19039762018-01-18 04:23:01 +0000456 const double error_value = -1;
457 double bogo_clock = error_value;
458
459 std::ifstream f("/proc/cpuinfo");
460 if (!f.is_open()) {
461 std::cerr << "failed to open /proc/cpuinfo\n";
462 return error_value;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000463 }
464
Eric Fiselier19039762018-01-18 04:23:01 +0000465 auto startsWithKey = [](std::string const& Value, std::string const& Key) {
466 if (Key.size() > Value.size()) return false;
467 auto Cmp = [&](char X, char Y) {
468 return std::tolower(X) == std::tolower(Y);
469 };
470 return std::equal(Key.begin(), Key.end(), Value.begin(), Cmp);
471 };
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000472
Eric Fiselier19039762018-01-18 04:23:01 +0000473 std::string ln;
474 while (std::getline(f, ln)) {
475 if (ln.empty()) continue;
476 size_t SplitIdx = ln.find(':');
477 std::string value;
478 if (SplitIdx != std::string::npos) value = ln.substr(SplitIdx + 1);
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000479 // When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000480 // accept positive values. Some environments (virtual machines) report zero,
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000481 // which would cause infinite looping in WallTime_Init.
Eric Fiselier19039762018-01-18 04:23:01 +0000482 if (startsWithKey(ln, "cpu MHz")) {
483 if (!value.empty()) {
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000484 double cycles_per_second = benchmark::stod(value) * 1000000.0;
Eric Fiselier19039762018-01-18 04:23:01 +0000485 if (cycles_per_second > 0) return cycles_per_second;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000486 }
Eric Fiselier19039762018-01-18 04:23:01 +0000487 } else if (startsWithKey(ln, "bogomips")) {
488 if (!value.empty()) {
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000489 bogo_clock = benchmark::stod(value) * 1000000.0;
Eric Fiselier19039762018-01-18 04:23:01 +0000490 if (bogo_clock < 0.0) bogo_clock = error_value;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000491 }
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000492 }
493 }
Eric Fiselier19039762018-01-18 04:23:01 +0000494 if (f.bad()) {
495 std::cerr << "Failure reading /proc/cpuinfo\n";
496 return error_value;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000497 }
Eric Fiselier19039762018-01-18 04:23:01 +0000498 if (!f.eof()) {
499 std::cerr << "Failed to read to end of /proc/cpuinfo\n";
500 return error_value;
501 }
502 f.close();
503 // If we found the bogomips clock, but nothing better, we'll use it (but
504 // we're not happy about it); otherwise, fallback to the rough estimation
505 // below.
506 if (bogo_clock >= 0.0) return bogo_clock;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000507
Eric Fiselier19039762018-01-18 04:23:01 +0000508#elif defined BENCHMARK_HAS_SYSCTL
509 constexpr auto* FreqStr =
510#if defined(BENCHMARK_OS_FREEBSD) || defined(BENCHMARK_OS_NETBSD)
511 "machdep.tsc_freq";
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000512#elif defined BENCHMARK_OS_OPENBSD
513 "hw.cpuspeed";
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000514#else
Eric Fiselier19039762018-01-18 04:23:01 +0000515 "hw.cpufrequency";
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000516#endif
Eric Fiselier19039762018-01-18 04:23:01 +0000517 unsigned long long hz = 0;
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000518#if defined BENCHMARK_OS_OPENBSD
519 if (GetSysctl(FreqStr, &hz)) return hz * 1000000;
520#else
Eric Fiselier19039762018-01-18 04:23:01 +0000521 if (GetSysctl(FreqStr, &hz)) return hz;
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000522#endif
Eric Fiselier19039762018-01-18 04:23:01 +0000523 fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n",
524 FreqStr, strerror(errno));
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000525
526#elif defined BENCHMARK_OS_WINDOWS
527 // In NT, read MHz from the registry. If we fail to do so or we're in win9x
528 // then make a crude estimate.
529 DWORD data, data_size = sizeof(data);
530 if (IsWindowsXPOrGreater() &&
531 SUCCEEDED(
532 SHGetValueA(HKEY_LOCAL_MACHINE,
533 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
534 "~MHz", nullptr, &data, &data_size)))
Eric Fiselier19039762018-01-18 04:23:01 +0000535 return static_cast<double>((int64_t)data *
536 (int64_t)(1000 * 1000)); // was mhz
Eric Fiselierfcafd3e2018-07-10 04:02:00 +0000537#elif defined (BENCHMARK_OS_SOLARIS)
538 kstat_ctl_t *kc = kstat_open();
539 if (!kc) {
540 std::cerr << "failed to open /dev/kstat\n";
541 return -1;
542 }
543 kstat_t *ksp = kstat_lookup(kc, (char*)"cpu_info", -1, (char*)"cpu_info0");
544 if (!ksp) {
545 std::cerr << "failed to lookup in /dev/kstat\n";
546 return -1;
547 }
548 if (kstat_read(kc, ksp, NULL) < 0) {
549 std::cerr << "failed to read from /dev/kstat\n";
550 return -1;
551 }
552 kstat_named_t *knp =
553 (kstat_named_t*)kstat_data_lookup(ksp, (char*)"current_clock_Hz");
554 if (!knp) {
555 std::cerr << "failed to lookup data in /dev/kstat\n";
556 return -1;
557 }
558 if (knp->data_type != KSTAT_DATA_UINT64) {
559 std::cerr << "current_clock_Hz is of unexpected data type: "
560 << knp->data_type << "\n";
561 return -1;
562 }
563 double clock_hz = knp->value.ui64;
564 kstat_close(kc);
565 return clock_hz;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000566#endif
Eric Fiselier19039762018-01-18 04:23:01 +0000567 // If we've fallen through, attempt to roughly estimate the CPU clock rate.
568 const int estimate_time_ms = 1000;
569 const auto start_ticks = cycleclock::Now();
570 SleepForMilliseconds(estimate_time_ms);
571 return static_cast<double>(cycleclock::Now() - start_ticks);
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000572}
Eric Fiselierfbc9ff22016-11-05 00:30:27 +0000573
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000574} // end namespace
575
Eric Fiselier19039762018-01-18 04:23:01 +0000576const CPUInfo& CPUInfo::Get() {
577 static const CPUInfo* info = new CPUInfo();
578 return *info;
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000579}
580
Eric Fiselier19039762018-01-18 04:23:01 +0000581CPUInfo::CPUInfo()
582 : num_cpus(GetNumCPUs()),
583 cycles_per_second(GetCPUCyclesPerSecond()),
584 caches(GetCacheSizes()),
585 scaling_enabled(CpuScalingEnabled(num_cpus)) {}
Eric Fiselierb08d8b12016-07-19 23:07:03 +0000586
587} // end namespace benchmark