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