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