User: Password:
|
|
Subscribe / Log in / New account

Breaking userspace

Breaking userspace

Posted Jan 29, 2013 13:12 UTC (Tue) by eru (subscriber, #2753)
In reply to: There are real reasons to question systemd by nix
Parent article: Poettering: The Biggest Myths

I recently wrote some code that wants to know how many cores or processors there are in the Linux system, so it can estimate how many parallel threads it makes sense to set up. I did this by scanning "/proc/cpuinfo". Is this now in danger of breaking at any time, because it relies on "something the kernel exports other than syscalls?"

In that case I would like to know of some alternate, more stable way to get this information - and preferably without adding dependencies on some heavy-duty libraries or middleware, since the program currently depends on nothing but its own code, the standard C library, and pthreads.


(Log in to post comments)

Breaking userspace

Posted Jan 29, 2013 13:45 UTC (Tue) by cortana (subscriber, #24596) [Link]

Breaking userspace

Posted Jan 29, 2013 13:53 UTC (Tue) by khim (subscriber, #9252) [Link]

What's wrong with a standard C library?

Breaking userspace

Posted Jan 29, 2013 16:20 UTC (Tue) by nix (subscriber, #2304) [Link]

IIRC, that's actually never worked on all multi-CPU architectures: it just happens that x86 and x86_64 report one stanza per CPU.

Breaking userspace

Posted Jan 29, 2013 16:54 UTC (Tue) by cesarb (subscriber, #6266) [Link]

/sys/devices/system/cpu seems better, and looks the same at least on this desktop (x86-64) and my phone (arm).

Breaking userspace

Posted Jan 29, 2013 19:01 UTC (Tue) by nix (subscriber, #2304) [Link]

But that's /sys, which is also rather more mutable than the syscall interface.

getconf _NPROCESSORS_CONF (or _NPROCESSORS_ONLN) is the thing to use (or, from a program, sysconf(_SC_NPROCESSORS_CONF) et seq. This is in glibc, so has a stability guarantee that is worth something. Let it figure out where to go for the answer.

Breaking userspace

Posted Jan 29, 2013 20:10 UTC (Tue) by eru (subscriber, #2753) [Link]

Thanks nix, and khim, that looks like what I need.

Breaking userspace

Posted Jan 29, 2013 16:29 UTC (Tue) by cesarb (subscriber, #6266) [Link]

Copying an old Stack Overflow answer of mine:

The definitive resource for /sys is Documentation/sysfs-rules.txt. The definitive resource for /proc/sys is Documentation/sysctl/. The definitive resource for the rest of /proc appears to be Documentation/filesystems/proc.txt. The rest of the Documentation/ directory in the Linux kernel source has other interesting information. In particular, Documentation/ABI/ mentions the stability of each interface.

(Unfortunately for your use case, I found nothing about /proc/cpuinfo in these files, and I know that the output is very different depending on the architecture, so it is probably not stable. The /sys/devices/system/cpu directory has some of that information, however, including how many cores there are, and should be the same for all architectures, so I would recommend looking there first and using /proc/cpuinfo as a fallback.)


Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds