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