workhorsy/py-cpuinfo

A module for getting CPU info with pure Python

Python

345

696 commits

updated Mar 22, 2024

See the code

README

py-cpuinfo

Latest Version License Python Version PyPI - Downloads

Py-cpuinfo gets CPU info with pure Python. Py-cpuinfo should work without any extra programs or libraries, beyond what your OS provides. It does not require any compilation(C/C++, assembly, et cetera) to use. It works with Python 3.

Example

if __name__ == '__main__':
    from cpuinfo import get_cpu_info

    for key, value in get_cpu_info().items():
        print("{0}: {1}".format(key, value))

API

'''
Returns the CPU info by using the best sources of information for your OS.
Returns the result in a dict
'''
get_cpu_info()

'''
Returns the CPU info by using the best sources of information for your OS.
Returns the result in a json string
'''
get_cpu_info_json()

Fields

keyExample valueReturn Format
"python_version""3.10.4.final.0 (64 bit)"string
"cpuinfo_version"(8, 0, 0)(int, int, int)
"cpuinfo_version_string""8.0.0"string
"hz_advertised_friendly""2.9300 GHz"string
"hz_actual_friendly""1.7330 GHz"string
"hz_advertised"(2930000000, 0)(int, int)
"hz_actual"(1733000000, 0)(int, int)
"arch""X86_64"'ARM_7', 'ARM_8', 'LOONG_32', 'LOONG_64', 'MIPS_32', 'MIPS_64', 'PPC_32', 'PPC_64', 'RISCV_32', 'RISCV_64', 'SPARC_32', 'SPARC_64', 'S390X', 'X86_32', 'X86_64'
"bits"64int
"count"4int
"l1_data_cache_size"32768int
"l1_instruction_cache_size"32768int
"l2_cache_size"262144int
"l2_cache_line_size"256int
"l2_cache_associativity"6int
"l3_cache_size"3145728int
"stepping"5int
"model"30int
"family"6int
"processor_type"0int
"flags"['acpi', 'aperfmperf', 'apic', 'arch_perfmon', 'bts', 'clflush', 'cmov', 'constant_tsc', 'cx16', 'cx8', 'de', 'ds_cpl', 'dtes64', 'dtherm', 'dts', 'ept', 'est', 'flexpriority', 'fpu', 'fxsr', 'ht', 'ida', 'lahf_lm', 'lm', 'mca', 'mce', 'mmx', 'monitor', 'msr', 'mtrr', 'nonstop_tsc', 'nopl', 'nx', 'pae', 'pat', 'pbe', 'pdcm', 'pebs', 'pge', 'pni', 'popcnt', 'pse', 'pse36', 'rdtscp', 'rep_good', 'sep', 'smx', 'ss', 'sse', 'sse2', 'sse4_1', 'sse4_2', 'ssse3', 'syscall', 'tm', 'tm2', 'tpr_shadow', 'tsc', 'vme', 'vmx', 'vnmi', 'vpid', 'xtopology', 'xtpr'][string]

Raw Fields

These fields are pulled directly from the CPU and are unverified. They may contain expected results. Other times they may contain wildly unexpected results or garbage. So it would be a bad idea to rely on them.

keyExample valueReturn Format
"vendor_id_raw""GenuineIntel"string
"hardware_raw""BCM2708"string
"brand_raw""Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz"string
"arch_string_raw""x86_64"string

Run as a script

$ python cpuinfo/cpuinfo.py

Run as a module

$ python -m cpuinfo

Run as a library

if __name__ == '__main__':
    from cpuinfo import get_cpu_info
    info = get_cpu_info()
    print(info)

Run under Pyinstaller

# NOTE: Pyinstaller may spawn infinite processes if __main__ is not used
if __name__ == '__main__':
    from cpuinfo import get_cpu_info
    from multiprocessing import freeze_support

    # NOTE: Pyinstaller also requires freeze_support
    freeze_support()
    info = get_cpu_info()
    print(info)

Install instructions

$ python -m pip install -U py-cpuinfo

Command Line Arguments

--help: show this help message and exit
--json: Return the info in JSON format
--version: Return the version of py-cpuinfo
--trace: Traces code paths used to find CPU info to file

OS Support

OSTested and should workUntested
AndroidEverything
BSDFreeBSD, PC-BSD, TrueOSOpenBSD, NetBSD
CygwinWindows
HaikuHaiku NightlyBeOS
LinuxArch, Centos, Debian, Fedora, Gentoo, OpenSuse, Ubuntu
OS X10.8 - 10.14
SolarisOracle Solaris, OpenIndiana
WindowsXP, Vista, 7, 8, 10RT

CPU Support

  • X86 32bit and 64bit
  • Some ARM, LoongArch, MIPS, PPC, RISCV, SPARC, and S390X CPUs

These approaches are used for getting info:

  1. Windows Registry (Windows)
  2. /proc/cpuinfo (Linux)
  3. sysctl (OS X)
  4. dmesg (Unix/Linux)
  5. /var/run/dmesg.boot (BSD/Unix)
  6. isainfo and kstat (Solaris)
  7. cpufreq-info (BeagleBone)
  8. lscpu (Unix/Linux)
  9. sysinfo (Haiku)
  10. device-tree ibm features flags (Linux PPC)
  11. Querying the CPUID register (Intel X86 CPUs)

Run Test Suite

python test_suite.py

Bugs and Corrections

Please report a Bug if you suspect any of this information is wrong.

If py-cpuinfo does not work on your machine, run the script:

python tools/get_system_info.py

and create bug report with the generated "system_info.txt" file.

cpu
cpufreq-info
cpuid
cpuinfo
kstat
lscpu
python
sysctl
sysinfo

workhorsy/py-cpuinfo

A module for getting CPU info with pure Python

Python

345

696 commits

updated Mar 22, 2024

See the code

README

py-cpuinfo

Latest Version License Python Version PyPI - Downloads

Py-cpuinfo gets CPU info with pure Python. Py-cpuinfo should work without any extra programs or libraries, beyond what your OS provides. It does not require any compilation(C/C++, assembly, et cetera) to use. It works with Python 3.

Example

if __name__ == '__main__':
    from cpuinfo import get_cpu_info

    for key, value in get_cpu_info().items():
        print("{0}: {1}".format(key, value))

API

'''
Returns the CPU info by using the best sources of information for your OS.
Returns the result in a dict
'''
get_cpu_info()

'''
Returns the CPU info by using the best sources of information for your OS.
Returns the result in a json string
'''
get_cpu_info_json()

Fields

keyExample valueReturn Format
"python_version""3.10.4.final.0 (64 bit)"string
"cpuinfo_version"(8, 0, 0)(int, int, int)
"cpuinfo_version_string""8.0.0"string
"hz_advertised_friendly""2.9300 GHz"string
"hz_actual_friendly""1.7330 GHz"string
"hz_advertised"(2930000000, 0)(int, int)
"hz_actual"(1733000000, 0)(int, int)
"arch""X86_64"'ARM_7', 'ARM_8', 'LOONG_32', 'LOONG_64', 'MIPS_32', 'MIPS_64', 'PPC_32', 'PPC_64', 'RISCV_32', 'RISCV_64', 'SPARC_32', 'SPARC_64', 'S390X', 'X86_32', 'X86_64'
"bits"64int
"count"4int
"l1_data_cache_size"32768int
"l1_instruction_cache_size"32768int
"l2_cache_size"262144int
"l2_cache_line_size"256int
"l2_cache_associativity"6int
"l3_cache_size"3145728int
"stepping"5int
"model"30int
"family"6int
"processor_type"0int
"flags"['acpi', 'aperfmperf', 'apic', 'arch_perfmon', 'bts', 'clflush', 'cmov', 'constant_tsc', 'cx16', 'cx8', 'de', 'ds_cpl', 'dtes64', 'dtherm', 'dts', 'ept', 'est', 'flexpriority', 'fpu', 'fxsr', 'ht', 'ida', 'lahf_lm', 'lm', 'mca', 'mce', 'mmx', 'monitor', 'msr', 'mtrr', 'nonstop_tsc', 'nopl', 'nx', 'pae', 'pat', 'pbe', 'pdcm', 'pebs', 'pge', 'pni', 'popcnt', 'pse', 'pse36', 'rdtscp', 'rep_good', 'sep', 'smx', 'ss', 'sse', 'sse2', 'sse4_1', 'sse4_2', 'ssse3', 'syscall', 'tm', 'tm2', 'tpr_shadow', 'tsc', 'vme', 'vmx', 'vnmi', 'vpid', 'xtopology', 'xtpr'][string]

Raw Fields

These fields are pulled directly from the CPU and are unverified. They may contain expected results. Other times they may contain wildly unexpected results or garbage. So it would be a bad idea to rely on them.

keyExample valueReturn Format
"vendor_id_raw""GenuineIntel"string
"hardware_raw""BCM2708"string
"brand_raw""Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz"string
"arch_string_raw""x86_64"string

Run as a script

$ python cpuinfo/cpuinfo.py

Run as a module

$ python -m cpuinfo

Run as a library

if __name__ == '__main__':
    from cpuinfo import get_cpu_info
    info = get_cpu_info()
    print(info)

Run under Pyinstaller

# NOTE: Pyinstaller may spawn infinite processes if __main__ is not used
if __name__ == '__main__':
    from cpuinfo import get_cpu_info
    from multiprocessing import freeze_support

    # NOTE: Pyinstaller also requires freeze_support
    freeze_support()
    info = get_cpu_info()
    print(info)

Install instructions

$ python -m pip install -U py-cpuinfo

Command Line Arguments

--help: show this help message and exit
--json: Return the info in JSON format
--version: Return the version of py-cpuinfo
--trace: Traces code paths used to find CPU info to file

OS Support

OSTested and should workUntested
AndroidEverything
BSDFreeBSD, PC-BSD, TrueOSOpenBSD, NetBSD
CygwinWindows
HaikuHaiku NightlyBeOS
LinuxArch, Centos, Debian, Fedora, Gentoo, OpenSuse, Ubuntu
OS X10.8 - 10.14
SolarisOracle Solaris, OpenIndiana
WindowsXP, Vista, 7, 8, 10RT

CPU Support

  • X86 32bit and 64bit
  • Some ARM, LoongArch, MIPS, PPC, RISCV, SPARC, and S390X CPUs

These approaches are used for getting info:

  1. Windows Registry (Windows)
  2. /proc/cpuinfo (Linux)
  3. sysctl (OS X)
  4. dmesg (Unix/Linux)
  5. /var/run/dmesg.boot (BSD/Unix)
  6. isainfo and kstat (Solaris)
  7. cpufreq-info (BeagleBone)
  8. lscpu (Unix/Linux)
  9. sysinfo (Haiku)
  10. device-tree ibm features flags (Linux PPC)
  11. Querying the CPUID register (Intel X86 CPUs)

Run Test Suite

python test_suite.py

Bugs and Corrections

Please report a Bug if you suspect any of this information is wrong.

If py-cpuinfo does not work on your machine, run the script:

python tools/get_system_info.py

and create bug report with the generated "system_info.txt" file.

cpu
cpufreq-info
cpuid
cpuinfo
kstat
lscpu
python
sysctl
sysinfo

Significant stargazers

Niklas Fiekas

654 followers · starred Apr 2016

Maratyszcza

777 followers · starred Jun 2015

jpmag

78 followers · starred Nov 2020

Patrick Zhang

3 followers · starred Jun 2019