CPU Information Library
The CPU Information Library, commonly referred to as cpuinfo, is a sophisticated tool designed to gather pivotal details about the host CPU, crucial for optimizing performance. This library provides a cross-platform solution compatible with various operating systems including Linux, Windows, macOS, Android, iOS, and FreeBSD, and supports architectures such as x86, x86-64, ARM, and ARM64.
Features
-
Cross-Platform Support: Ensures that cpuinfo can operate seamlessly across several major operating systems and on numerous hardware architectures.
-
Modern C/C++ Interface: This library is constructed with a modern programming interface that is thread-safe. It does not allocate memory post-initialization and does not throw exceptions, which makes it robust and reliable.
-
Instruction Set and Core Detection: It identifies which instruction sets your CPU supports, like AVX512 for x86 and ARMv8.3 for ARM architectures. It also detects detailed information about your CPU's architecture, vendor, and specific core technologies.
-
Comprehensive Cache Information: Provides details about the CPU cache, including cache type, size, line size, associativity, and the cores it shares the cache with.
-
Topology Information: Offers insights into the relationship between logical processors, cores, and CPU packages, helping in understanding how these elements interconnect and work together.
-
Production Quality: The library contains over 60 mock tests grounded on actual device data and includes work-arounds for known hardware and software kernel bugs, supporting systems with heterogeneous cores like big.LITTLE configurations.
-
Open Source License: It is available under a permissive BSD (2-Clause) license, promoting wide usage and adaptation.
Usage Examples
Here's how you can utilize cpuinfo in practice:
- To log the CPU's name:
cpuinfo_initialize();
printf("Running on %s CPU\n", cpuinfo_get_package(0)->name);
- To check if the system is using a 32-bit or 64-bit ARM architecture:
#if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64
/* 32-bit ARM-specific code here */
#endif
- To determine if the CPU supports ARM NEON:
cpuinfo_initialize();
if (cpuinfo_has_arm_neon()) {
neon_implementation(arguments);
}
- To confirm x86 AVX support:
cpuinfo_initialize();
if (cpuinfo_has_x86_avx()) {
avx_implementation(arguments);
}
Integration in Build Systems
The library can be an integral part of various build setups:
-
Pkg-config: Ensures the build environment has necessary compiler and linker flags.
-
Autotools, Meson, Bazel, CMake, Makefile: Integrate easily with these build systems to compile projects relying on cpuinfo effectively.
Exposed Information
The library reveals a wealth of CPU information including:
- Processor and microarchitecture specifics
- Instruction sets utilization
- Cache details (e.g., size, topology)
- Topology map (logical processors, cores)
Supported Environments
Cpuinfo supports Android, Linux, iOS, macOS, Windows, and FreeBSD platforms across numerous architectures such as x86, x86-64, ARM, and ARM64.
Detection Methods
The library employs various methods to detect processor details, including but not limited to leveraging CPUID for x86, /proc/cpuinfo
for ARM, and using system-specific calls like sysctlbyname
for topology and cache information.
In summary, the cpuinfo library stands as a comprehensive tool for obtaining detailed, platform-independent CPU information crucial for performance tuning and optimization tasks. Whether for desktop, mobile, or embedded systems, it provides a robust framework for accessing critical CPU data efficiently and reliably.