1# CPU INFOrmation library 2 3[](https://github.com/pytorch/cpuinfo/blob/master/LICENSE) 4[](https://travis-ci.org/pytorch/cpuinfo) 5[](https://ci.appveyor.com/project/MaratDukhan/cpuinfo/branch/master) 6 7cpuinfo is a library to detect essential for performance optimization information about host CPU. 8 9## Features 10 11- **Cross-platform** availability: 12 - Linux, Windows, macOS, Android, and iOS operating systems 13 - x86, x86-64, ARM, and ARM64 architectures 14- Modern **C/C++ interface** 15 - Thread-safe 16 - No memory allocation after initialization 17 - No exceptions thrown 18- Detection of **supported instruction sets**, up to AVX512 (x86) and ARMv8.3 extensions 19- Detection of SoC and core information: 20 - **Processor (SoC) name** 21 - Vendor and **microarchitecture** for each CPU core 22 - ID (**MIDR** on ARM, **CPUID** leaf 1 EAX value on x86) for each CPU core 23- Detection of **cache information**: 24 - Cache type (instruction/data/unified), size and line size 25 - Cache associativity 26 - Cores and logical processors (hyper-threads) sharing the cache 27- Detection of **topology information** (relative between logical processors, cores, and processor packages) 28- Well-tested **production-quality** code: 29 - 60+ mock tests based on data from real devices 30 - Includes work-arounds for common bugs in hardware and OS kernels 31 - Supports systems with heterogenous cores, such as **big.LITTLE** and Max.Med.Min 32- Permissive **open-source** license (Simplified BSD) 33 34## Examples 35 36Log processor name: 37 38```c 39cpuinfo_initialize(); 40printf("Running on %s CPU\n", cpuinfo_get_package(0)->name); 41``` 42 43Detect if target is a 32-bit or 64-bit ARM system: 44 45```c 46#if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64 47 /* 32-bit ARM-specific code here */ 48#endif 49``` 50 51Check if the host CPU supports ARM NEON 52 53```c 54cpuinfo_initialize(); 55if (cpuinfo_has_arm_neon()) { 56 neon_implementation(arguments); 57} 58``` 59 60Check if the host CPU supports x86 AVX 61 62```c 63cpuinfo_initialize(); 64if (cpuinfo_has_x86_avx()) { 65 avx_implementation(arguments); 66} 67``` 68 69Check if the thread runs on a Cortex-A53 core 70 71```c 72cpuinfo_initialize(); 73switch (cpuinfo_get_current_core()->uarch) { 74 case cpuinfo_uarch_cortex_a53: 75 cortex_a53_implementation(arguments); 76 break; 77 default: 78 generic_implementation(arguments); 79 break; 80} 81``` 82 83Get the size of level 1 data cache on the fastest core in the processor (e.g. big core in big.LITTLE ARM systems): 84 85```c 86cpuinfo_initialize(); 87const size_t l1_size = cpuinfo_get_processor(0)->cache.l1d->size; 88``` 89 90Pin thread to cores sharing L2 cache with the current core (Linux or Android) 91 92```c 93cpuinfo_initialize(); 94cpu_set_t cpu_set; 95CPU_ZERO(&cpu_set); 96const struct cpuinfo_cache* current_l2 = cpuinfo_get_current_processor()->cache.l2; 97for (uint32_t i = 0; i < current_l2->processor_count; i++) { 98 CPU_SET(cpuinfo_get_processor(current_l2->processor_start + i)->linux_id, &cpu_set); 99} 100pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set); 101``` 102 103## Use via pkg-config 104 105If you would like to provide your project's build environment with the necessary compiler and linker flags in a portable manner, the library by default when built enables `CPUINFO_BUILD_PKG_CONFIG` and will generate a [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/) manifest (_libcpuinfo.pc_). Here are several examples of how to use it: 106 107### Command Line 108 109If you used your distro's package manager to install the library, you can verify that it is available to your build environment like so: 110 111```console 112$ pkg-config --cflags --libs libcpuinfo 113-I/usr/include/x86_64-linux-gnu/ -L/lib/x86_64-linux-gnu/ -lcpuinfo 114``` 115 116If you have installed the library from source into a non-standard prefix, pkg-config may need help finding it: 117 118```console 119$ PKG_CONFIG_PATH="/home/me/projects/cpuinfo/prefix/lib/pkgconfig/:$PKG_CONFIG_PATH" pkg-config --cflags --libs libcpuinfo 120-I/home/me/projects/cpuinfo/prefix/include -L/home/me/projects/cpuinfo/prefix/lib -lcpuinfo 121``` 122 123### GNU Autotools 124 125To [use](https://autotools.io/pkgconfig/pkg_check_modules.html) with the GNU Autotools include the following snippet in your project's `configure.ac`: 126 127```makefile 128# CPU INFOrmation library... 129PKG_CHECK_MODULES( 130 [libcpuinfo], [libcpuinfo], [], 131 [AC_MSG_ERROR([libcpuinfo missing...])]) 132YOURPROJECT_CXXFLAGS="$YOURPROJECT_CXXFLAGS $libcpuinfo_CFLAGS" 133YOURPROJECT_LIBS="$YOURPROJECT_LIBS $libcpuinfo_LIBS" 134``` 135 136### Meson 137 138To use with Meson you just need to add `dependency('libcpuinfo')` as a dependency for your executable. 139 140```meson 141project( 142 'MyCpuInfoProject', 143 'cpp', 144 meson_version: '>=0.55.0' 145) 146 147executable( 148 'MyCpuInfoExecutable', 149 sources: 'main.cpp', 150 dependencies: dependency('libcpuinfo') 151) 152``` 153 154### Bazel 155 156This project can be built using [Bazel](https://bazel.build/install). 157 158You can also use this library as a dependency to your Bazel project. Add to the `WORKSPACE` file: 159 160```python 161load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") 162 163git_repository( 164 name = "org_pytorch_cpuinfo", 165 branch = "master", 166 remote = "https://github.com/Vertexwahn/cpuinfo.git", 167) 168``` 169 170And to your `BUILD` file: 171 172```python 173cc_binary( 174 name = "cpuinfo_test", 175 srcs = [ 176 # ... 177 ], 178 deps = [ 179 "@org_pytorch_cpuinfo//:cpuinfo", 180 ], 181) 182``` 183 184### CMake 185 186To use with CMake use the [FindPkgConfig](https://cmake.org/cmake/help/latest/module/FindPkgConfig.html) module. Here is an example: 187 188```cmake 189cmake_minimum_required(VERSION 3.6) 190project("MyCpuInfoProject") 191 192find_package(PkgConfig) 193pkg_check_modules(CpuInfo REQUIRED IMPORTED_TARGET libcpuinfo) 194 195add_executable(${PROJECT_NAME} main.cpp) 196target_link_libraries(${PROJECT_NAME} PkgConfig::CpuInfo) 197``` 198 199### Makefile 200 201To use within a vanilla makefile, you can call pkg-config directly to supply compiler and linker flags using shell substitution. 202 203```makefile 204CFLAGS=-g3 -Wall -Wextra -Werror ... 205LDFLAGS=-lfoo ... 206... 207CFLAGS+= $(pkg-config --cflags libcpuinfo) 208LDFLAGS+= $(pkg-config --libs libcpuinfo) 209``` 210 211## Exposed information 212- [x] Processor (SoC) name 213- [x] Microarchitecture 214- [x] Usable instruction sets 215- [ ] CPU frequency 216- [x] Cache 217 - [x] Size 218 - [x] Associativity 219 - [x] Line size 220 - [x] Number of partitions 221 - [x] Flags (unified, inclusive, complex hash function) 222 - [x] Topology (logical processors that share this cache level) 223- [ ] TLB 224 - [ ] Number of entries 225 - [ ] Associativity 226 - [ ] Covered page types (instruction, data) 227 - [ ] Covered page sizes 228- [x] Topology information 229 - [x] Logical processors 230 - [x] Cores 231 - [x] Packages (sockets) 232 233## Supported environments: 234- [x] Android 235 - [x] x86 ABI 236 - [x] x86_64 ABI 237 - [x] armeabi ABI 238 - [x] armeabiv7-a ABI 239 - [x] arm64-v8a ABI 240 - [ ] ~~mips ABI~~ 241 - [ ] ~~mips64 ABI~~ 242- [x] Linux 243 - [x] x86 244 - [x] x86-64 245 - [x] 32-bit ARM (ARMv5T and later) 246 - [x] ARM64 247 - [ ] PowerPC64 248- [x] iOS 249 - [x] x86 (iPhone simulator) 250 - [x] x86-64 (iPhone simulator) 251 - [x] ARMv7 252 - [x] ARM64 253- [x] macOS 254 - [x] x86 255 - [x] x86-64 256 - [x] ARM64 (Apple silicon) 257- [x] Windows 258 - [x] x86 259 - [x] x86-64 260 - [x] arm64 261 262## Methods 263 264- Processor (SoC) name detection 265 - [x] Using CPUID leaves 0x80000002–0x80000004 on x86/x86-64 266 - [x] Using `/proc/cpuinfo` on ARM 267 - [x] Using `ro.chipname`, `ro.board.platform`, `ro.product.board`, `ro.mediatek.platform`, `ro.arch` properties (Android) 268 - [ ] Using kernel log (`dmesg`) on ARM Linux 269 - [x] Using Windows registry on ARM64 Windows 270- Vendor and microarchitecture detection 271 - [x] Intel-designed x86/x86-64 cores (up to Sunny Cove, Goldmont Plus, and Knights Mill) 272 - [x] AMD-designed x86/x86-64 cores (up to Puma/Jaguar and Zen 2) 273 - [ ] VIA-designed x86/x86-64 cores 274 - [ ] Other x86 cores (DM&P, RDC, Transmeta, Cyrix, Rise) 275 - [x] ARM-designed ARM cores (up to Cortex-A55, Cortex-A77, and Neoverse E1/N1/V1/N2) 276 - [x] Qualcomm-designed ARM cores (Scorpion, Krait, and Kryo) 277 - [x] Nvidia-designed ARM cores (Denver and Carmel) 278 - [x] Samsung-designed ARM cores (Exynos) 279 - [x] Intel-designed ARM cores (XScale up to 3rd-gen) 280 - [x] Apple-designed ARM cores (up to Lightning and Thunder) 281 - [x] Cavium-designed ARM cores (ThunderX) 282 - [x] AppliedMicro-designed ARM cores (X-Gene) 283- Instruction set detection 284 - [x] Using CPUID (x86/x86-64) 285 - [x] Using `/proc/cpuinfo` on 32-bit ARM EABI (Linux) 286 - [x] Using microarchitecture heuristics on (32-bit ARM) 287 - [x] Using `FPSID` and `WCID` registers (32-bit ARM) 288 - [x] Using `getauxval` (Linux/ARM) 289 - [x] Using `/proc/self/auxv` (Android/ARM) 290 - [ ] Using instruction probing on ARM (Linux) 291 - [ ] Using CPUID registers on ARM64 (Linux) 292 - [x] Using IsProcessorFeaturePresent on ARM64 Windows 293- Cache detection 294 - [x] Using CPUID leaf 0x00000002 (x86/x86-64) 295 - [x] Using CPUID leaf 0x00000004 (non-AMD x86/x86-64) 296 - [ ] Using CPUID leaves 0x80000005-0x80000006 (AMD x86/x86-64) 297 - [x] Using CPUID leaf 0x8000001D (AMD x86/x86-64) 298 - [x] Using `/proc/cpuinfo` (Linux/pre-ARMv7) 299 - [x] Using microarchitecture heuristics (ARM) 300 - [x] Using chipset name (ARM) 301 - [x] Using `sysctlbyname` (Mach) 302 - [x] Using sysfs `typology` directories (ARM/Linux) 303 - [ ] Using sysfs `cache` directories (Linux) 304 - [x] Using `GetLogicalProcessorInformationEx` on ARM64 Windows 305- TLB detection 306 - [x] Using CPUID leaf 0x00000002 (x86/x86-64) 307 - [ ] Using CPUID leaves 0x80000005-0x80000006 and 0x80000019 (AMD x86/x86-64) 308 - [x] Using microarchitecture heuristics (ARM) 309- Topology detection 310 - [x] Using CPUID leaf 0x00000001 on x86/x86-64 (legacy APIC ID) 311 - [x] Using CPUID leaf 0x0000000B on x86/x86-64 (Intel APIC ID) 312 - [ ] Using CPUID leaf 0x8000001E on x86/x86-64 (AMD APIC ID) 313 - [x] Using `/proc/cpuinfo` (Linux) 314 - [x] Using `host_info` (Mach) 315 - [x] Using `GetLogicalProcessorInformationEx` (Windows) 316 - [x] Using sysfs (Linux) 317 - [x] Using chipset name (ARM/Linux) 318 319