1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file CpuInfoUtil.cc is from Apache Arrow as of 2023-03-21
*/
#include "CpuInfoUtil.hh"
#ifdef __APPLE__
#include <sys/sysctl.h>
#endif
#ifndef _MSC_VER
#include <unistd.h>
#endif
#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#include <intrin.h>
#endif
#include <algorithm>
#include <array>
#include <bitset>
#include <cstdint>
#include <fstream>
#include <optional>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include "orc/Exceptions.hh"
#undef CPUINFO_ARCH_X86
#undef CPUINFO_ARCH_ARM
#undef CPUINFO_ARCH_PPC
#if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
#define CPUINFO_ARCH_X86
#ifndef ORC_HAVE_RUNTIME_AVX512
#define UNUSED(x) (void)(x)
#endif
#elif defined(_M_ARM64) || defined(__aarch64__) || defined(__arm64__)
#define CPUINFO_ARCH_ARM
#elif defined(__PPC64__) || defined(__PPC64LE__) || defined(__ppc64__) || defined(__powerpc64__)
#define CPUINFO_ARCH_PPC
#endif
namespace orc {
namespace {
constexpr int kCacheLevels = static_cast<int>(CpuInfo::CacheLevel::Last) + 1;
//============================== OS Dependent ==============================//
#if defined(_WIN32)
//------------------------------ WINDOWS ------------------------------//
void OsRetrieveCacheSize(std::array<int64_t, kCacheLevels>* cache_sizes) {
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = nullptr;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer_position = nullptr;
DWORD buffer_size = 0;
size_t offset = 0;
typedef BOOL(WINAPI * GetLogicalProcessorInformationFuncPointer)(void*, void*);
GetLogicalProcessorInformationFuncPointer func_pointer =
(GetLogicalProcessorInformationFuncPointer)GetProcAddress(
GetModuleHandle("kernel32"), "GetLogicalProcessorInformation");
if (!func_pointer) {
throw ParseError("Failed to find procedure GetLogicalProcessorInformation");
}
// Get buffer size
if (func_pointer(buffer, &buffer_size) && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
throw ParseError("Failed to get size of processor information buffer");
}
buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(buffer_size);
if (!buffer) {
return;
}
if (!func_pointer(buffer, &buffer_size)) {
free(buffer);
throw ParseError("Failed to get processor information");
}
buffer_position = buffer;
while (offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= buffer_size) {
if (RelationCache == buffer_position->Relationship) {
PCACHE_DESCRIPTOR cache = &buffer_position->Cache;
if (cache->Level >= 1 && cache->Level <= kCacheLevels) {
const int64_t current = (*cache_sizes)[cache->Level - 1];
(*cache_sizes)[cache->Level - 1] = std::max<int64_t>(current, cache->Size);
}
}
offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
buffer_position++;
}
free(buffer);
}
#if defined(CPUINFO_ARCH_X86)
// On x86, get CPU features by cpuid, https://en.wikipedia.org/wiki/CPUID
#if defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 5
void __cpuidex(int CPUInfo[4], int function_id, int subfunction_id) {
__asm__ __volatile__("cpuid"
: "=a"(CPUInfo[0]), "=b"(CPUInfo[1]), "=c"(CPUInfo[2]), "=d"(CPUInfo[3])
: "a"(function_id), "c"(subfunction_id));
}
int64_t _xgetbv(int xcr) {
int out = 0;
__asm__ __volatile__("xgetbv" : "=a"(out) : "c"(xcr) : "%edx");
return out;
}
#endif // MINGW
void OsRetrieveCpuInfo(int64_t* hardware_flags, CpuInfo::Vendor* vendor,
std::string* model_name) {
int register_EAX_id = 1;
int highest_valid_id = 0;
int highest_extended_valid_id = 0;
std::bitset<32> features_ECX;
std::array<int, 4> cpu_info;
// Get highest valid id
__cpuid(cpu_info.data(), 0);
highest_valid_id = cpu_info[0];
// HEX of "GenuineIntel": 47656E75 696E6549 6E74656C
// HEX of "AuthenticAMD": 41757468 656E7469 63414D44
if (cpu_info[1] == 0x756e6547 && cpu_info[3] == 0x49656e69 && cpu_info[2] == 0x6c65746e) {
*vendor = CpuInfo::Vendor::Intel;
} else if (cpu_info[1] == 0x68747541 && cpu_info[3] == 0x69746e65 &&
cpu_info[2] == 0x444d4163) {
*vendor = CpuInfo::Vendor::AMD;
}
if (highest_valid_id <= register_EAX_id) {
return;
}
// EAX=1: Processor Info and Feature Bits
__cpuidex(cpu_info.data(), register_EAX_id, 0);
features_ECX = cpu_info[2];
// Get highest extended id
__cpuid(cpu_info.data(), 0x80000000);
highest_extended_valid_id = cpu_info[0];
// Retrieve CPU model name
if (highest_extended_valid_id >= static_cast<int>(0x80000004)) {
model_name->clear();
for (int i = 0x80000002; i <= static_cast<int>(0x80000004); ++i) {
__cpuidex(cpu_info.data(), i, 0);
*model_name += std::string(reinterpret_cast<char*>(cpu_info.data()), sizeof(cpu_info));
}
}
bool zmm_enabled = false;
if (features_ECX[27]) { // OSXSAVE
// Query if the OS supports saving ZMM registers when switching contexts
int64_t xcr0 = _xgetbv(0);
zmm_enabled = (xcr0 & 0xE0) == 0xE0;
}
if (features_ECX[9]) *hardware_flags |= CpuInfo::SSSE3;
if (features_ECX[19]) *hardware_flags |= CpuInfo::SSE4_1;
if (features_ECX[20]) *hardware_flags |= CpuInfo::SSE4_2;
if (features_ECX[23]) *hardware_flags |= CpuInfo::POPCNT;
if (features_ECX[28]) *hardware_flags |= CpuInfo::AVX;
// cpuid with EAX=7, ECX=0: Extended Features
register_EAX_id = 7;
if (highest_valid_id > register_EAX_id) {
__cpuidex(cpu_info.data(), register_EAX_id, 0);
std::bitset<32> features_EBX = cpu_info[1];
if (features_EBX[3]) *hardware_flags |= CpuInfo::BMI1;
if (features_EBX[5]) *hardware_flags |= CpuInfo::AVX2;
if (features_EBX[8]) *hardware_flags |= CpuInfo::BMI2;
if (zmm_enabled) {
if (features_EBX[16]) *hardware_flags |= CpuInfo::AVX512F;
if (features_EBX[17]) *hardware_flags |= CpuInfo::AVX512DQ;
if (features_EBX[28]) *hardware_flags |= CpuInfo::AVX512CD;
if (features_EBX[30]) *hardware_flags |= CpuInfo::AVX512BW;
if (features_EBX[31]) *hardware_flags |= CpuInfo::AVX512VL;
}
}
}
#elif defined(CPUINFO_ARCH_ARM)
// Windows on Arm
void OsRetrieveCpuInfo(int64_t* hardware_flags, CpuInfo::Vendor* vendor,
std::string* model_name) {
*hardware_flags |= CpuInfo::ASIMD;
// TODO: vendor, model_name
}
#endif
#elif defined(__APPLE__)
//------------------------------ MACOS ------------------------------//
std::optional<int64_t> IntegerSysCtlByName(const char* name) {
size_t len = sizeof(int64_t);
int64_t data = 0;
if (sysctlbyname(name, &data, &len, nullptr, 0) == 0) {
return data;
}
// ENOENT is the official errno value for non-existing sysctl's,
// but EINVAL and ENOTSUP have been seen in the wild.
if (errno != ENOENT && errno != EINVAL && errno != ENOTSUP) {
std::ostringstream ss;
ss << "sysctlbyname failed for '" << name << "'";
throw ParseError(ss.str());
}
return std::nullopt;
}
void OsRetrieveCacheSize(std::array<int64_t, kCacheLevels>* cache_sizes) {
static_assert(kCacheLevels >= 3, "");
auto c = IntegerSysCtlByName("hw.l1dcachesize");
if (c.has_value()) {
(*cache_sizes)[0] = *c;
}
c = IntegerSysCtlByName("hw.l2cachesize");
if (c.has_value()) {
(*cache_sizes)[1] = *c;
}
c = IntegerSysCtlByName("hw.l3cachesize");
if (c.has_value()) {
(*cache_sizes)[2] = *c;
}
}
void OsRetrieveCpuInfo(int64_t* hardware_flags, CpuInfo::Vendor* vendor,
std::string* model_name) {
// hardware_flags
struct SysCtlCpuFeature {
const char* name;
int64_t flag;
};
std::vector<SysCtlCpuFeature> features = {
#if defined(CPUINFO_ARCH_X86)
{"hw.optional.sse4_2",
CpuInfo::SSSE3 | CpuInfo::SSE4_1 | CpuInfo::SSE4_2 | CpuInfo::POPCNT},
{"hw.optional.avx1_0", CpuInfo::AVX},
{"hw.optional.avx2_0", CpuInfo::AVX2},
{"hw.optional.bmi1", CpuInfo::BMI1},
{"hw.optional.bmi2", CpuInfo::BMI2},
{"hw.optional.avx512f", CpuInfo::AVX512F},
{"hw.optional.avx512cd", CpuInfo::AVX512CD},
{"hw.optional.avx512dq", CpuInfo::AVX512DQ},
{"hw.optional.avx512bw", CpuInfo::AVX512BW},
{"hw.optional.avx512vl", CpuInfo::AVX512VL},
#elif defined(CPUINFO_ARCH_ARM)
// ARM64 (note that this is exposed under Rosetta as well)
{"hw.optional.neon", CpuInfo::ASIMD},
#endif
};
for (const auto& feature : features) {
auto v = IntegerSysCtlByName(feature.name);
if (v.value_or(0)) {
*hardware_flags |= feature.flag;
}
}
// TODO: vendor, model_name
*vendor = CpuInfo::Vendor::Unknown;
*model_name = "Unknown";
}
#else
//------------------------------ LINUX ------------------------------//
// Get cache size, return 0 on error
int64_t LinuxGetCacheSize(int level) {
// get cache size by sysconf()
#ifdef _SC_LEVEL1_DCACHE_SIZE
const int kCacheSizeConf[] = {
_SC_LEVEL1_DCACHE_SIZE,
_SC_LEVEL2_CACHE_SIZE,
_SC_LEVEL3_CACHE_SIZE,
};
static_assert(sizeof(kCacheSizeConf) / sizeof(kCacheSizeConf[0]) == kCacheLevels, "");
errno = 0;
const int64_t cache_size = sysconf(kCacheSizeConf[level]);
if (errno == 0 && cache_size > 0) {
return cache_size;
}
#endif
// get cache size from sysfs if sysconf() fails or not supported
const char* kCacheSizeSysfs[] = {
"/sys/devices/system/cpu/cpu0/cache/index0/size", // l1d (index1 is l1i)
"/sys/devices/system/cpu/cpu0/cache/index2/size", // l2
"/sys/devices/system/cpu/cpu0/cache/index3/size", // l3
};
static_assert(sizeof(kCacheSizeSysfs) / sizeof(kCacheSizeSysfs[0]) == kCacheLevels, "");
std::ifstream cacheinfo(kCacheSizeSysfs[level], std::ios::in);
if (!cacheinfo) {
return 0;
}
// cacheinfo is one line like: 65536, 64K, 1M, etc.
uint64_t size = 0;
char unit = '\0';
cacheinfo >> size >> unit;
if (unit == 'K') {
size <<= 10;
} else if (unit == 'M') {
size <<= 20;
} else if (unit == 'G') {
size <<= 30;
} else if (unit != '\0') {
return 0;
}
return static_cast<int64_t>(size);
}
// Helper function to parse for hardware flags from /proc/cpuinfo
// values contains a list of space-separated flags. check to see if the flags we
// care about are present.
// Returns a bitmap of flags.
int64_t LinuxParseCpuFlags(const std::string& values) {
const struct {
std::string name;
int64_t flag;
} flag_mappings[] = {
#if defined(CPUINFO_ARCH_X86)
{"ssse3", CpuInfo::SSSE3},
{"sse4_1", CpuInfo::SSE4_1},
{"sse4_2", CpuInfo::SSE4_2},
{"popcnt", CpuInfo::POPCNT},
{"avx", CpuInfo::AVX},
{"avx2", CpuInfo::AVX2},
{"avx512f", CpuInfo::AVX512F},
{"avx512cd", CpuInfo::AVX512CD},
{"avx512vl", CpuInfo::AVX512VL},
{"avx512dq", CpuInfo::AVX512DQ},
{"avx512bw", CpuInfo::AVX512BW},
{"bmi1", CpuInfo::BMI1},
{"bmi2", CpuInfo::BMI2},
#elif defined(CPUINFO_ARCH_ARM)
{"asimd", CpuInfo::ASIMD},
#endif
};
const int64_t num_flags = sizeof(flag_mappings) / sizeof(flag_mappings[0]);
int64_t flags = 0;
for (int i = 0; i < num_flags; ++i) {
if (values.find(flag_mappings[i].name) != std::string::npos) {
flags |= flag_mappings[i].flag;
}
}
return flags;
}
void OsRetrieveCacheSize(std::array<int64_t, kCacheLevels>* cache_sizes) {
for (int i = 0; i < kCacheLevels; ++i) {
const int64_t cache_size = LinuxGetCacheSize(i);
if (cache_size > 0) {
(*cache_sizes)[i] = cache_size;
}
}
}
static constexpr bool IsWhitespace(char c) {
return c == ' ' || c == '\t';
}
std::string TrimString(std::string value) {
size_t ltrim_chars = 0;
while (ltrim_chars < value.size() && IsWhitespace(value[ltrim_chars])) {
++ltrim_chars;
}
value.erase(0, ltrim_chars);
size_t rtrim_chars = 0;
while (rtrim_chars < value.size() && IsWhitespace(value[value.size() - 1 - rtrim_chars])) {
++rtrim_chars;
}
value.erase(value.size() - rtrim_chars, rtrim_chars);
return value;
}
// Read from /proc/cpuinfo
void OsRetrieveCpuInfo(int64_t* hardware_flags, CpuInfo::Vendor* vendor,
std::string* model_name) {
std::ifstream cpuinfo("/proc/cpuinfo", std::ios::in);
while (cpuinfo) {
std::string line;
std::getline(cpuinfo, line);
const size_t colon = line.find(':');
if (colon != std::string::npos) {
const std::string name = TrimString(line.substr(0, colon - 1));
const std::string value = TrimString(line.substr(colon + 1, std::string::npos));
if (name.compare("flags") == 0 || name.compare("Features") == 0) {
*hardware_flags |= LinuxParseCpuFlags(value);
} else if (name.compare("model name") == 0) {
*model_name = value;
} else if (name.compare("vendor_id") == 0) {
if (value.compare("GenuineIntel") == 0) {
*vendor = CpuInfo::Vendor::Intel;
} else if (value.compare("AuthenticAMD") == 0) {
*vendor = CpuInfo::Vendor::AMD;
}
}
}
}
}
#endif // WINDOWS, MACOS, LINUX
//============================== Arch Dependent ==============================//
#if defined(CPUINFO_ARCH_X86)
//------------------------------ X86_64 ------------------------------//
bool ArchParseUserSimdLevel(const std::string& simd_level, int64_t* hardware_flags) {
enum {
USER_SIMD_NONE,
USER_SIMD_AVX512,
USER_SIMD_MAX,
};
int level = USER_SIMD_MAX;
// Parse the level
if (simd_level == "AVX512") {
level = USER_SIMD_AVX512;
} else if (simd_level == "NONE") {
level = USER_SIMD_NONE;
} else {
return false;
}
// Disable feature as the level
if (level < USER_SIMD_AVX512) {
*hardware_flags &= ~CpuInfo::AVX512;
}
return true;
}
void ArchVerifyCpuRequirements(const CpuInfo* ci) {
#if defined(ORC_HAVE_RUNTIME_AVX512)
if (!ci->isDetected(CpuInfo::AVX512)) {
throw ParseError("CPU does not support the Supplemental AVX512 instruction set");
}
#else
UNUSED(ci);
#endif
}
#elif defined(CPUINFO_ARCH_ARM)
//------------------------------ AARCH64 ------------------------------//
bool ArchParseUserSimdLevel(const std::string& simd_level, int64_t* hardware_flags) {
if (simd_level == "NONE") {
*hardware_flags &= ~CpuInfo::ASIMD;
return true;
}
return false;
}
void ArchVerifyCpuRequirements(const CpuInfo* ci) {
if (!ci->isDetected(CpuInfo::ASIMD)) {
throw ParseError("CPU does not support the Armv8 Neon instruction set");
}
}
#else
//------------------------------ PPC, ... ------------------------------//
bool ArchParseUserSimdLevel(const std::string& simd_level, int64_t* hardware_flags) {
return true;
}
void ArchVerifyCpuRequirements(const CpuInfo* ci) {}
#endif // X86, ARM, PPC
} // namespace
struct CpuInfo::Impl {
int64_t hardware_flags = 0;
int numCores = 0;
int64_t original_hardware_flags = 0;
Vendor vendor = Vendor::Unknown;
std::string model_name = "Unknown";
std::array<int64_t, kCacheLevels> cache_sizes{};
Impl() {
OsRetrieveCacheSize(&cache_sizes);
OsRetrieveCpuInfo(&hardware_flags, &vendor, &model_name);
original_hardware_flags = hardware_flags;
numCores = std::max(static_cast<int>(std::thread::hardware_concurrency()), 1);
// parse user simd level
const auto maybe_env_var = std::getenv("ORC_USER_SIMD_LEVEL");
std::string userSimdLevel = maybe_env_var == nullptr ? "NONE" : std::string(maybe_env_var);
std::transform(userSimdLevel.begin(), userSimdLevel.end(), userSimdLevel.begin(),
[](unsigned char c) { return std::toupper(c); });
if (!ArchParseUserSimdLevel(userSimdLevel, &hardware_flags)) {
throw ParseError("Invalid value for ORC_USER_SIMD_LEVEL: " + userSimdLevel);
}
}
};
CpuInfo::~CpuInfo() = default;
CpuInfo::CpuInfo() : impl_(new Impl) {}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#endif
const CpuInfo* CpuInfo::getInstance() {
static CpuInfo cpu_info;
return &cpu_info;
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
int64_t CpuInfo::hardwareFlags() const {
return impl_->hardware_flags;
}
int CpuInfo::numCores() const {
return impl_->numCores <= 0 ? 1 : impl_->numCores;
}
CpuInfo::Vendor CpuInfo::vendor() const {
return impl_->vendor;
}
const std::string& CpuInfo::modelName() const {
return impl_->model_name;
}
int64_t CpuInfo::cacheSize(CacheLevel level) const {
constexpr int64_t kDefaultCacheSizes[] = {
32 * 1024, // Level 1: 32K
256 * 1024, // Level 2: 256K
3072 * 1024, // Level 3: 3M
};
static_assert(sizeof(kDefaultCacheSizes) / sizeof(kDefaultCacheSizes[0]) == kCacheLevels, "");
static_assert(static_cast<int>(CacheLevel::L1) == 0, "");
const int i = static_cast<int>(level);
if (impl_->cache_sizes[i] > 0) return impl_->cache_sizes[i];
if (i == 0) return kDefaultCacheSizes[0];
// l3 may be not available, return maximum of l2 or default size
return std::max(kDefaultCacheSizes[i], impl_->cache_sizes[i - 1]);
}
bool CpuInfo::isSupported(int64_t flags) const {
return (impl_->hardware_flags & flags) == flags;
}
bool CpuInfo::isDetected(int64_t flags) const {
return (impl_->original_hardware_flags & flags) == flags;
}
void CpuInfo::verifyCpuRequirements() const {
return ArchVerifyCpuRequirements(this);
}
} // namespace orc
#undef CPUINFO_ARCH_X86
#undef CPUINFO_ARCH_ARM
#undef CPUINFO_ARCH_PPC
|