cpuinfo
CPU INFOrmation library (x86/x86-64/ARM/ARM64, Linux/Windows/Android/macOS/iOS)
Top Related Projects
A cross platform C99 library to get cpu features at runtime.
A microbenchmark support library
Quantized Neural Network PACKage - mobile-optimized implementation of quantized neural network operators
Compiler for Neural Network hardware accelerators
oneAPI Deep Neural Network Library (oneDNN)
The Compute Library is a set of computer vision and machine learning functions optimised for both Arm CPUs and GPUs using SIMD technologies.
Quick Overview
cpuinfo is a library for detecting and reporting CPU features and characteristics. It provides detailed information about CPU capabilities, including instruction set support, cache sizes, and core counts. The library is designed to be cross-platform and works on various architectures, including x86, ARM, and MIPS.
Pros
- Cross-platform support for multiple CPU architectures
- Detailed and accurate CPU feature detection
- Lightweight and easy to integrate into existing projects
- Actively maintained and regularly updated
Cons
- Limited documentation for some advanced features
- May require additional configuration for certain platforms
- Performance impact when used extensively in real-time applications
- Some reported issues with specific CPU models or configurations
Code Examples
- Initializing and getting basic CPU information:
#include <cpuinfo.h>
int main() {
if (!cpuinfo_initialize()) {
fprintf(stderr, "Failed to initialize cpuinfo\n");
return 1;
}
printf("Number of processors: %zu\n", cpuinfo_get_processors_count());
printf("Number of cores: %zu\n", cpuinfo_get_cores_count());
printf("Number of clusters: %zu\n", cpuinfo_get_clusters_count());
cpuinfo_deinitialize();
return 0;
}
- Checking for specific CPU features:
#include <cpuinfo.h>
int main() {
cpuinfo_initialize();
if (cpuinfo_has_x86_avx()) {
printf("CPU supports AVX\n");
}
if (cpuinfo_has_arm_neon()) {
printf("CPU supports NEON\n");
}
cpuinfo_deinitialize();
return 0;
}
- Getting detailed cache information:
#include <cpuinfo.h>
int main() {
cpuinfo_initialize();
const struct cpuinfo_cache* l1i = cpuinfo_get_l1i_cache(0);
if (l1i != NULL) {
printf("L1 instruction cache size: %zu\n", l1i->size);
printf("L1 instruction cache line size: %zu\n", l1i->line_size);
}
const struct cpuinfo_cache* l1d = cpuinfo_get_l1d_cache(0);
if (l1d != NULL) {
printf("L1 data cache size: %zu\n", l1d->size);
printf("L1 data cache line size: %zu\n", l1d->line_size);
}
cpuinfo_deinitialize();
return 0;
}
Getting Started
To use cpuinfo in your project, follow these steps:
-
Clone the repository:
git clone https://github.com/pytorch/cpuinfo.git
-
Add the following to your CMakeLists.txt:
add_subdirectory(cpuinfo) target_link_libraries(your_target PRIVATE cpuinfo)
-
Include the header in your C/C++ code:
#include <cpuinfo.h>
-
Initialize cpuinfo at the beginning of your program:
if (!cpuinfo_initialize()) { fprintf(stderr, "Failed to initialize cpuinfo\n"); return 1; }
-
Use cpuinfo functions as needed, and don't forget to deinitialize:
cpuinfo_deinitialize();
Competitor Comparisons
A cross platform C99 library to get cpu features at runtime.
Pros of cpu_features
- Broader platform support, including Android and iOS
- More comprehensive CPU feature detection, including cache sizes and topology
- Better documentation and examples
Cons of cpu_features
- Less integration with deep learning frameworks
- Smaller community and fewer contributors
- Less frequent updates and releases
Code Comparison
cpuinfo:
#include <cpuinfo.h>
if (cpuinfo_initialize()) {
printf("Number of cores: %d\n", cpuinfo_get_cores_count());
}
cpu_features:
#include "cpu_features_macros.h"
#include "cpuinfo_x86.h"
X86Info info = GetX86Info();
printf("AVX support: %d\n", info.features.avx);
Summary
Both cpuinfo and cpu_features provide CPU information and feature detection. cpuinfo is more focused on deep learning applications and has better integration with frameworks like PyTorch. cpu_features offers broader platform support and more comprehensive CPU information but has a smaller community. The choice between them depends on the specific use case and required features.
A microbenchmark support library
Pros of benchmark
- Focused on performance benchmarking, providing a comprehensive suite of tools for measuring code execution time
- Supports a wide range of platforms and compilers, offering greater flexibility for developers
- Actively maintained with frequent updates and a large community of contributors
Cons of benchmark
- Limited to benchmarking functionality, lacking the detailed CPU information provided by cpuinfo
- May have a steeper learning curve for users new to benchmarking tools
- Requires more setup and configuration compared to cpuinfo's straightforward CPU detection
Code Comparison
cpuinfo:
#include <cpuinfo.h>
if (cpuinfo_initialize()) {
printf("Number of cores: %d\n", cpuinfo_get_cores_count());
}
benchmark:
#include <benchmark/benchmark.h>
static void BM_SomeFunction(benchmark::State& state) {
for (auto _ : state) {
// Code to benchmark
}
}
BENCHMARK(BM_SomeFunction);
Summary
While cpuinfo focuses on providing detailed CPU information, benchmark is designed specifically for performance measurement. cpuinfo offers simpler CPU detection, while benchmark provides more comprehensive tools for measuring code execution time across various platforms.
Quantized Neural Network PACKage - mobile-optimized implementation of quantized neural network operators
Pros of QNNPACK
- Specialized for quantized neural network operations on mobile and embedded platforms
- Optimized for ARM NEON and x86 SSE2/AVX2 architectures
- Provides high-performance implementations of common CNN operators
Cons of QNNPACK
- More focused scope, primarily for quantized neural networks
- May require more setup and integration for general-purpose use
- Less comprehensive CPU feature detection compared to cpuinfo
Code Comparison
QNNPACK (operator implementation):
void q8gemm_ukernel_4x8__neon(
size_t mr, size_t nr, size_t k,
const uint8_t* restrict a,
size_t a_stride,
const uint8_t* restrict b,
const int32_t* restrict bias,
uint8_t* restrict c,
size_t c_stride,
const union qnnp_conv_quantization_params quantization_params[restrict static 1])
cpuinfo (CPU feature detection):
bool cpuinfo_arm_linux_detect_impl(void) {
struct cpuinfo_arm_linux_processor* arm_linux_processors = NULL;
uint32_t processors_count = 0;
struct cpuinfo_processor* processors = NULL;
struct cpuinfo_core* cores = NULL;
struct cpuinfo_cluster* clusters = NULL;
Compiler for Neural Network hardware accelerators
Pros of glow
- Designed for neural network compilation and optimization
- Supports multiple hardware targets (CPU, GPU, accelerators)
- Provides a complete compiler infrastructure for machine learning
Cons of glow
- More complex and larger codebase
- Steeper learning curve for contributors
- Requires more dependencies and setup
Code comparison
glow:
void BundleSaver::saveConstant(const Constant *C) {
auto payload = C->getPayload();
auto *data = payload.getUnsafePtr();
size_t size = payload.getSizeInBytes();
auto alignment = payload.getAlignment();
saveConstantWeights(C->getName(), data, size, alignment);
}
cpuinfo:
void cpuinfo_x86_linux_init(void) {
struct cpu_id_t id;
struct cpuinfo_x86_model_info_t model_info;
cpuid(&id);
parse_cpu_model_info(&id, &model_info);
// ...
}
Key differences
cpuinfo focuses on CPU feature detection and information gathering, while glow is a comprehensive neural network compiler. cpuinfo is more lightweight and specialized, making it easier to integrate for CPU-specific tasks. glow offers broader machine learning capabilities but requires more resources and setup time.
oneAPI Deep Neural Network Library (oneDNN)
Pros of oneDNN
- Broader scope: Provides optimized deep learning primitives for multiple architectures (CPU, GPU, FPGA)
- Higher-level functionality: Offers complete deep learning building blocks like convolutions and RNNs
- Active development: More frequent updates and larger contributor base
Cons of oneDNN
- Steeper learning curve: More complex API due to its broader functionality
- Larger codebase: Requires more resources to integrate and maintain
- Less focused: Not specialized for CPU information retrieval like cpuinfo
Code Comparison
cpuinfo (CPU detection):
struct cpuinfo_processor* processors = cpuinfo_get_processors();
if (processors[0].core->vendor == cpuinfo_vendor_intel) {
// Intel-specific code
}
oneDNN (Creating a convolution primitive):
auto conv_desc = convolution_forward::desc(prop_kind::forward,
algorithm::convolution_direct, src_md, weights_md, dst_md,
strides, padding_l, padding_r);
auto conv_prim_desc = convolution_forward::primitive_desc(conv_desc, eng);
auto conv = convolution_forward(conv_prim_desc);
The Compute Library is a set of computer vision and machine learning functions optimised for both Arm CPUs and GPUs using SIMD technologies.
Pros of ComputeLibrary
- Comprehensive ARM-specific optimizations for various compute tasks
- Supports a wide range of ARM architectures and GPUs
- Includes ready-to-use kernels for common machine learning operations
Cons of ComputeLibrary
- Limited to ARM architectures, less portable than cpuinfo
- More complex to integrate and use compared to cpuinfo's focused approach
- Larger codebase and potentially steeper learning curve
Code Comparison
cpuinfo:
struct cpuinfo_processor {
uint32_t linux_id;
uint32_t smt_id;
uint32_t core;
uint32_t package;
uint32_t cache_l1d;
uint32_t cache_l1i;
uint32_t cache_l2;
uint32_t cache_l3;
};
ComputeLibrary:
class NEGEMMInterleave4x4Kernel : public INEKernel
{
public:
const char *name() const override
{
return "NEGEMMInterleave4x4Kernel";
}
void run(const Window &window, cl::CommandQueue &queue) override;
};
The code snippets demonstrate cpuinfo's focus on CPU information structures, while ComputeLibrary provides optimized kernels for specific compute operations on ARM architectures.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
CPU INFOrmation library
cpuinfo is a library to detect essential for performance optimization information about host CPU.
Features
- Cross-platform availability:
- Linux, Windows, macOS, Android, iOS and FreeBSD operating systems
- x86, x86-64, ARM, and ARM64 architectures
- Modern C/C++ interface
- Thread-safe
- No memory allocation after initialization
- No exceptions thrown
- Detection of supported instruction sets, up to AVX512 (x86) and ARMv8.3 extensions
- Detection of SoC and core information:
- Processor (SoC) name
- Vendor and microarchitecture for each CPU core
- ID (MIDR on ARM, CPUID leaf 1 EAX value on x86) for each CPU core
- Detection of cache information:
- Cache type (instruction/data/unified), size and line size
- Cache associativity
- Cores and logical processors (hyper-threads) sharing the cache
- Detection of topology information (relative between logical processors, cores, and processor packages)
- Well-tested production-quality code:
- 60+ mock tests based on data from real devices
- Includes work-arounds for common bugs in hardware and OS kernels
- Supports systems with heterogenous cores, such as big.LITTLE and Max.Med.Min
- Permissive open-source license (Simplified BSD)
Examples
Log processor name:
cpuinfo_initialize();
printf("Running on %s CPU\n", cpuinfo_get_package(0)->name);
Detect if target is a 32-bit or 64-bit ARM system:
#if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64
/* 32-bit ARM-specific code here */
#endif
Check if the host CPU supports ARM NEON
cpuinfo_initialize();
if (cpuinfo_has_arm_neon()) {
neon_implementation(arguments);
}
Check if the host CPU supports x86 AVX
cpuinfo_initialize();
if (cpuinfo_has_x86_avx()) {
avx_implementation(arguments);
}
Check if the thread runs on a Cortex-A53 core
cpuinfo_initialize();
switch (cpuinfo_get_current_core()->uarch) {
case cpuinfo_uarch_cortex_a53:
cortex_a53_implementation(arguments);
break;
default:
generic_implementation(arguments);
break;
}
Get the size of level 1 data cache on the fastest core in the processor (e.g. big core in big.LITTLE ARM systems):
cpuinfo_initialize();
const size_t l1_size = cpuinfo_get_processor(0)->cache.l1d->size;
Pin thread to cores sharing L2 cache with the current core (Linux or Android)
cpuinfo_initialize();
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
const struct cpuinfo_cache* current_l2 = cpuinfo_get_current_processor()->cache.l2;
for (uint32_t i = 0; i < current_l2->processor_count; i++) {
CPU_SET(cpuinfo_get_processor(current_l2->processor_start + i)->linux_id, &cpu_set);
}
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set);
Use via pkg-config
If 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 manifest (libcpuinfo.pc). Here are several examples of how to use it:
Command Line
If you used your distro's package manager to install the library, you can verify that it is available to your build environment like so:
$ pkg-config --cflags --libs libcpuinfo
-I/usr/include/x86_64-linux-gnu/ -L/lib/x86_64-linux-gnu/ -lcpuinfo
If you have installed the library from source into a non-standard prefix, pkg-config may need help finding it:
$ PKG_CONFIG_PATH="/home/me/projects/cpuinfo/prefix/lib/pkgconfig/:$PKG_CONFIG_PATH" pkg-config --cflags --libs libcpuinfo
-I/home/me/projects/cpuinfo/prefix/include -L/home/me/projects/cpuinfo/prefix/lib -lcpuinfo
GNU Autotools
To use with the GNU Autotools include the following snippet in your project's configure.ac
:
# CPU INFOrmation library...
PKG_CHECK_MODULES(
[libcpuinfo], [libcpuinfo], [],
[AC_MSG_ERROR([libcpuinfo missing...])])
YOURPROJECT_CXXFLAGS="$YOURPROJECT_CXXFLAGS $libcpuinfo_CFLAGS"
YOURPROJECT_LIBS="$YOURPROJECT_LIBS $libcpuinfo_LIBS"
Meson
To use with Meson you just need to add dependency('libcpuinfo')
as a dependency for your executable.
project(
'MyCpuInfoProject',
'cpp',
meson_version: '>=0.55.0'
)
executable(
'MyCpuInfoExecutable',
sources: 'main.cpp',
dependencies: dependency('libcpuinfo')
)
Bazel
This project can be built using Bazel.
You can also use this library as a dependency to your Bazel project. Add to the WORKSPACE
file:
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "org_pytorch_cpuinfo",
branch = "master",
remote = "https://github.com/Vertexwahn/cpuinfo.git",
)
And to your BUILD
file:
cc_binary(
name = "cpuinfo_test",
srcs = [
# ...
],
deps = [
"@org_pytorch_cpuinfo//:cpuinfo",
],
)
CMake
To use with CMake use the FindPkgConfig module. Here is an example:
cmake_minimum_required(VERSION 3.6)
project("MyCpuInfoProject")
find_package(PkgConfig)
pkg_check_modules(CpuInfo REQUIRED IMPORTED_TARGET libcpuinfo)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PkgConfig::CpuInfo)
Makefile
To use within a vanilla makefile, you can call pkg-config directly to supply compiler and linker flags using shell substitution.
CFLAGS=-g3 -Wall -Wextra -Werror ...
LDFLAGS=-lfoo ...
...
CFLAGS+= $(pkg-config --cflags libcpuinfo)
LDFLAGS+= $(pkg-config --libs libcpuinfo)
Exposed information
- Processor (SoC) name
- Microarchitecture
- Usable instruction sets
- CPU frequency
- Cache
- Size
- Associativity
- Line size
- Number of partitions
- Flags (unified, inclusive, complex hash function)
- Topology (logical processors that share this cache level)
- TLB
- Number of entries
- Associativity
- Covered page types (instruction, data)
- Covered page sizes
- Topology information
- Logical processors
- Cores
- Packages (sockets)
Supported environments:
- Android
- x86 ABI
- x86_64 ABI
- armeabi ABI
- armeabiv7-a ABI
- arm64-v8a ABI
-
mips ABI -
mips64 ABI
- Linux
- x86
- x86-64
- 32-bit ARM (ARMv5T and later)
- ARM64
- PowerPC64
- iOS
- x86 (iPhone simulator)
- x86-64 (iPhone simulator)
- ARMv7
- ARM64
- macOS
- x86
- x86-64
- ARM64 (Apple silicon)
- Windows
- x86
- x86-64
- arm64
- FreeBSD
- x86-64
Methods
- Processor (SoC) name detection
- Using CPUID leaves 0x80000002â0x80000004 on x86/x86-64
- Using
/proc/cpuinfo
on ARM - Using
ro.chipname
,ro.board.platform
,ro.product.board
,ro.mediatek.platform
,ro.arch
properties (Android) - Using kernel log (
dmesg
) on ARM Linux - Using Windows registry on ARM64 Windows
- Vendor and microarchitecture detection
- Intel-designed x86/x86-64 cores (up to Sunny Cove, Goldmont Plus, and Knights Mill)
- AMD-designed x86/x86-64 cores (up to Puma/Jaguar and Zen 2)
- VIA-designed x86/x86-64 cores
- Other x86 cores (DM&P, RDC, Transmeta, Cyrix, Rise)
- ARM-designed ARM cores (up to Cortex-A55, Cortex-A77, and Neoverse E1/V1/N2/V2)
- Qualcomm-designed ARM cores (Scorpion, Krait, and Kryo)
- Nvidia-designed ARM cores (Denver and Carmel)
- Samsung-designed ARM cores (Exynos)
- Intel-designed ARM cores (XScale up to 3rd-gen)
- Apple-designed ARM cores (up to Lightning and Thunder)
- Cavium-designed ARM cores (ThunderX)
- AppliedMicro-designed ARM cores (X-Gene)
- Instruction set detection
- Using CPUID (x86/x86-64)
- Using
/proc/cpuinfo
on 32-bit ARM EABI (Linux) - Using microarchitecture heuristics on (32-bit ARM)
- Using
FPSID
andWCID
registers (32-bit ARM) - Using
getauxval
(Linux/ARM) - Using
/proc/self/auxv
(Android/ARM) - Using instruction probing on ARM (Linux)
- Using CPUID registers on ARM64 (Linux)
- Using IsProcessorFeaturePresent on ARM64 Windows
- Cache detection
- Using CPUID leaf 0x00000002 (x86/x86-64)
- Using CPUID leaf 0x00000004 (non-AMD x86/x86-64)
- Using CPUID leaves 0x80000005-0x80000006 (AMD x86/x86-64)
- Using CPUID leaf 0x8000001D (AMD x86/x86-64)
- Using
/proc/cpuinfo
(Linux/pre-ARMv7) - Using microarchitecture heuristics (ARM)
- Using chipset name (ARM)
- Using
sysctlbyname
(Mach) - Using sysfs
typology
directories (ARM/Linux) - Using sysfs
cache
directories (Linux) - Using
GetLogicalProcessorInformationEx
on ARM64 Windows
- TLB detection
- Using CPUID leaf 0x00000002 (x86/x86-64)
- Using CPUID leaves 0x80000005-0x80000006 and 0x80000019 (AMD x86/x86-64)
- Using microarchitecture heuristics (ARM)
- Topology detection
- Using CPUID leaf 0x00000001 on x86/x86-64 (legacy APIC ID)
- Using CPUID leaf 0x0000000B on x86/x86-64 (Intel APIC ID)
- Using CPUID leaf 0x8000001E on x86/x86-64 (AMD APIC ID)
- Using
/proc/cpuinfo
(Linux) - Using
host_info
(Mach) - Using
GetLogicalProcessorInformationEx
(Windows) - Using sysfs (Linux)
- Using chipset name (ARM/Linux)
Top Related Projects
A cross platform C99 library to get cpu features at runtime.
A microbenchmark support library
Quantized Neural Network PACKage - mobile-optimized implementation of quantized neural network operators
Compiler for Neural Network hardware accelerators
oneAPI Deep Neural Network Library (oneDNN)
The Compute Library is a set of computer vision and machine learning functions optimised for both Arm CPUs and GPUs using SIMD technologies.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot