Email or username:

Password:

Forgot your password?
Hector Martin

PSA: If you're reading and parsing /proc/cpuinfo for anything, you should probably stop.

The CPU information you're probably looking for is available in structured form in /sys/devices/system/cpu, no horrible text parsing required. And unlike /proc/cpuinfo, the /sys hierarchy is standardized across architectures (as much as is practical). In particular, you will not find CPU topology info in /proc/cpuinfo on non-x86 architectures.

Signed, ARM64 users who keep seeing things like "1 cpu, 1 core, 8 threads" in software written by x86 users (I'm looking at you, GeekBench).

31 comments
Irenes (many)

@marcan this is a good heads-up, thanks for that. we had vaguely heard the pieces of it a long time ago but hadn't put together the practical significance.

Efraim Flashner

@marcan
I am guilty of this. I'll have to go and check out that path in /sys.

Imikoy

@RahulVadhyar@social.treehouse.systems @marcan@social.treehouse.systems Probably existed before the better interface was added, now it can't be removed for compat. reasons

mxk

@KasTasMykolas @RahulVadhyar @marcan this. Using cat on an interactive shell is the only valid use of /proc/cpuinfo today.

Wolf480pl

@RahulVadhyar @marcan
I'm guessing for interactive viewing when the only tool you have is `cat`.

KasTas

@marcan yup, I do use at least 3 SDKs which do seem... of getting some wrong ideas from /proc/cpuinfo about the environment they are running in :|

Malcolm Herbert

@marcan ansible and puppet have had facter for ages to winkle out all of this sort of information in a standard YAML/JSON form ... facter is pretty standalone and works without the dependencies if needed - your example: facter --no-ruby -j processors

Tom Walker

@marcan This is the most Mastodon post I ever read.

Thank you!

Tobias Heider

@marcan /me looks at gnome-control-center which tries to parse "model name" from /proc/cpuinfo for its system details view...

Campbell Jones :budgie:

@marcan I'm actually writing a system info tool right now, and it *mostly* relies on /sys/devices/system/cpu for cross-architecture CPU properties. There are some properties in /proc/cpuinfo that can't be found in sysfs, but those are gated behind an architecture check at build time.

And as an addendum, it does properly report core and thread counts on ARM :)

Tammi :3

@marcan CPU model unknown on aarch64 cause the aarch64 cpuinfo implementation does not print a string heh

Nihils Pilz

@marcan Did you just assume my Unix-like OS? Your Unix clone has weird paths that my UNIX grandchild doeth not.
BYW both Neo- and screenfetch(1) are EOL. I recommend fastfetch(1) these days.
My English FOSS profile is @neb

zwangseinweisung

@marcan /proc/cpuinfo is good for a quick view. ragnar@omnia:/sys/devices/system/cpu has to much i dont want or need

Hector Martin

@zwangseinweisung If you are a human and not a piece of code, the tool you should be using is lscpu, not anything from /proc or /sys.

DrScriptt

@marcan @zwangseinweisung sure.

But /proc/cpuinfo is available a lot more places than lscpu is.

Gacha

@marcan offtopic: how do you format the code in your toot?

Qais Yousef

@marcan can you think of legitimate reasons for parsing cpuinfo/sysfs? The ones i've seen were hacks/workarounds.

Yaakov

@marcan does meminfo have a similar story or it is ok to parse?

DrScriptt

@marcan interesting.

I’ll have to explore and expirament.

Thank you for sharing.

Marcin Juszkiewicz

@marcan When all you need is quick grep through "cpu features" flags then `/proc/cpuinfo` gives you readable names.

`/sys/devices/system/cpu/modalias` gives you hex codes for which you need to have dictionary.

But yes - the rest of stuff is there and users should just use `lscpu` to get data.

Christian Horn

@hrw Depending on the circumstances, lscpu might not be possible.
I just committed support for pmda-denki to use /dev/cpu/*/msr registers as source for power consumption metrics. I need to detect the cpu type (using /proc/cpuinfo right now) to understand which MSR are available.
Using plain directories/files for cpu detection allows me to implement a fake directory option where I can also i.e. on aarch64 make MSR's appear, for QE scripts and verification.
github.com/performancecopilot/

mmu_man

@marcan and if all you need is to know how many threads to spawn to scale on cores:

sysconf(_SC_NPROCESSORS_CONF); /* processors configured */
sysconf(_SC_NPROCESSORS_ONLN); /* processors available */

is even more portable (though there are 2 different names depending on the OSes because why not).

Ben Hutchings

@marcan Indeed, many years ago I added fallback implementations of the topology functions so that the information would be available on every architecture and config

Go Up