Skip to main content

cpuid Pythonic API

Project description

cpuid

PyPI version License: MIT Python Support

High-level Pythonic API for x86 CPU identification and feature detection

A user-friendly Python library that automatically identifies your CPU vendor, model, microarchitecture, and supported instruction sets. Built on top of cpuid-native for direct hardware access.

⚠️ x86/x64 ONLY: This library depends on the CPUID instruction which is exclusive to x86/x64 processors. It will NOT work on ARM processors (Apple Silicon M1/M2/M3, Raspberry Pi, AWS Graviton, etc.). Installation or execution on ARM will fail because the underlying cpuid-native extension cannot be built on ARM architectures.

Features

  • Automatic CPU identification - Detects Intel, AMD, and VIA processors
  • Microarchitecture detection - Identifies specific CPU generations (Sandy Bridge, Haswell, Zen, etc.)
  • Instruction set detection - Checks support for SSE, AVX, AVX2, BMI, and more
  • Zero configuration - Just import and use
  • Cross-platform - Linux, macOS, and Windows support
  • Python 3.7+ - Full support through Python 3.13

Installation

pip install cpuid

Quick Start

import cpuid

# Get CPU vendor
vendor = cpuid.cpu_vendor()
print(f"Vendor: {vendor}")  # e.g., "GenuineIntel" or "AuthenticAMD"

# Get CPU name/brand
name = cpuid.cpu_name()
print(f"CPU: {name}")  # e.g., "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz"

# Detect microarchitecture
arch, suffix = cpuid.cpu_microarchitecture()
print(f"Microarchitecture: {arch}{suffix}")  # e.g., "kabylake" or "zen2"

# Check for specific instruction sets
eax, ebx, ecx, edx = cpuid.cpuid(1)
has_sse2 = bool(edx & (1 << 26))
has_avx = bool(ecx & (1 << 28))
print(f"SSE2: {has_sse2}, AVX: {has_avx}")

Real-World Example

import cpuid

def detect_cpu_capabilities():
    """Detect CPU and print comprehensive information."""

    vendor = cpuid.cpu_vendor()
    name = cpuid.cpu_name()
    arch, suffix = cpuid.cpu_microarchitecture()

    print("=" * 60)
    print(f"Vendor: {vendor}")
    print(f"Model:  {name}")
    print(f"Arch:   {arch}{suffix}")
    print("=" * 60)

    # Check vector instruction support
    _, _, ecx, edx = cpuid.cpuid(1)

    features = {
        "SSE": bool(edx & (1 << 25)),
        "SSE2": bool(edx & (1 << 26)),
        "SSE3": bool(ecx & (1 << 0)),
        "SSSE3": bool(ecx & (1 << 9)),
        "SSE4.1": bool(ecx & (1 << 19)),
        "SSE4.2": bool(ecx & (1 << 20)),
        "AVX": bool(ecx & (1 << 28)),
    }

    # Check AVX2 and BMI
    _, ebx, _, _ = cpuid.cpuid_count(7, 0)
    features["AVX2"] = bool(ebx & (1 << 5))
    features["BMI1"] = bool(ebx & (1 << 3))
    features["BMI2"] = bool(ebx & (1 << 8))

    print("\nVector Instructions:")
    for name, supported in features.items():
        status = "✓" if supported else "✗"
        print(f"  {status} {name}")

if __name__ == "__main__":
    detect_cpu_capabilities()

Output example:

============================================================
Vendor: GenuineIntel
Model:  Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Arch:   kabylake
============================================================

Vector Instructions:
  ✓ SSE
  ✓ SSE2
  ✓ SSE3
  ✓ SSSE3
  ✓ SSE4.1
  ✓ SSE4.2
  ✓ AVX
  ✓ AVX2
  ✓ BMI1
  ✓ BMI2

API Reference

High-Level Functions

cpu_vendor() -> str

Returns the CPU vendor string (e.g., "GenuineIntel", "AuthenticAMD", "CentaurHauls").

cpu_name() -> str

Returns the full CPU brand string (e.g., "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz").

cpu_microarchitecture() -> Tuple[str, str]

Returns a tuple of (architecture, suffix) where:

  • architecture: Detected microarchitecture (e.g., "haswell", "skylake", "zen2")
  • suffix: Additional flags like "noavx" if AVX is disabled

Intel architectures: pentium, pentium2, pentium3, pentium4, core2, nehalem, westmere, sandybridge, ivybridge, haswell, broadwell, skylake, kabylake, atom, silvermont, goldmont

AMD architectures: k5, k6, athlon, k8, k10, bobcat, bulldozer, piledriver, steamroller, excavator, jaguar

Low-Level Functions

cpuid(leaf: int) -> Tuple[int, int, int, int]

Executes CPUID instruction and returns (eax, ebx, ecx, edx) register values.

cpuid_count(leaf: int, subleaf: int) -> Tuple[int, int, int, int]

Executes CPUID with both leaf and subleaf, returns (eax, ebx, ecx, edx).

xgetbv(ctr: int) -> int

Reads extended control register (typically ctr=0 for XCR0).

Supported Architectures

Intel

  • Legacy: Pentium, Pentium MMX, Pentium Pro, Pentium II/III/4, Pentium M
  • Core: Core 2, Nehalem, Westmere, Sandy Bridge, Ivy Bridge
  • Modern: Haswell, Broadwell, Skylake, Kaby Lake, Coffee Lake
  • Low-power: Atom, Silvermont, Goldmont
  • HPC: Knights Landing (Xeon Phi)

AMD

  • Legacy: K5, K6, Athlon
  • 64-bit: K8, K10
  • Bulldozer family: Bulldozer, Piledriver, Steamroller, Excavator
  • Modern: Bobcat, Jaguar, Zen (through microarchitecture detection)

VIA

  • C3, C7, Nano

Use Cases

  • Performance optimization - Select optimal algorithms based on CPU features
  • System requirements - Check if CPU supports required instruction sets
  • Benchmarking - Categorize results by CPU architecture
  • Cross-platform development - Adapt code paths for different CPUs
  • System monitoring - Display detailed CPU information

Low-Level Access

Need direct access to CPUID registers? Use cpuid-native:

import cpuid_native

success, eax, ebx, ecx, edx = cpuid_native.get_cpuid(1)
tsc, aux = cpuid_native.rdtscp()
xcr0 = cpuid_native.xgetbv(0)

Building from Source

git clone https://github.com/fpelliccioni/cpuid-py.git
cd cpuid-py
pip install -e .

License

MIT License - see LICENSE file for details.

Credits

Created and maintained by Fernando Pelliccioni

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpuid-0.1.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cpuid-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file cpuid-0.1.1.tar.gz.

File metadata

  • Download URL: cpuid-0.1.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cpuid-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fd5fd35fa29ede70de64235c38d0e824290606769b536a4fdf6c5951a989f38a
MD5 b3a94fe1c0505ff3101515a65218bd0e
BLAKE2b-256 515569e3eb791b409b5c251491516f4f87b27301c0646055bc4347668ed4ffbb

See more details on using hashes here.

File details

Details for the file cpuid-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cpuid-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cpuid-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b47e6d66bdd9f32edbeef918a9936d24a4ab102dd90a10992f218fdfe1fe00b4
MD5 62a1cc1424dfc5c7654a044a47706ed3
BLAKE2b-256 bf35fc1d0ffe657b3cb9424c18549950e5e1fdf612500d54091d9b29132d6368

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page