htop icon indicating copy to clipboard operation
htop copied to clipboard

Use sysconf() to determine active and existing CPUs on Linux

Open glaubitz opened this issue 1 year ago • 4 comments

Instead of enumerating the number of active and existing CPUs on Linux by iterating over the pseudo-files below /sys/devices/system/cpu/, it is better to retrieve the number of active and existing CPUs using the sysconf() function using _SC_NPROCESSORS_ONLN and _SC_NPROCESSORS_CONF which will always report the correct numbers.

Fixes #757

glaubitz avatar Dec 03 '23 21:12 glaubitz

This should be amended by a configuration option to hide offline CPUs.

glaubitz avatar Dec 03 '23 21:12 glaubitz

Instead of enumerating the number of active and existing CPUs on Linux by iterating over the pseudo-files below /sys/devices/system/cpu/, it is better to retrieve the number of active and existing CPUs using the sysconf() function using _SC_NPROCESSORS_ONLN and _SC_NPROCESSORS_CONF which will always report the correct numbers.

I would like to point out that they seem to do the same iteration anyway, as per NOTES here, but I suppose it's better than reinventing the wheel.


This is broken when applying against the 3.3.0 release, at least on one of the machines I have access to, I have not tested against master but I don't see any relevant commits.

LXD 5.19 container with 6 threads using 5.15.0-58-generic kernel(a bit odd that temps on the 4 soon-to-be-missing threads are N/A): image

+ this patch(missing 4 threads): image


LXD 5.19 container with 4 threads using 5.4.0-113-generic kernel: image

+ this patch(seemingly correct): image

C0rn3j avatar Jan 30 '24 16:01 C0rn3j

This is broken when applying against the 3.3.0 release, at least on one of the machines I have access to, I have not tested against master but I don't see any relevant commits.

If this change breaks LXD, I would argue it's a bug either in sysconf() or LXD causing the former to return an incorrect number of online CPUs.

FWIW, I am not saying that my patch is the way to go. It's just a draft and I currently don't have any better idea to improve CPU enumeration. The current enumeration method on Linux is broken, too, however.

glaubitz avatar Jan 30 '24 16:01 glaubitz

EDIT: the PR code is wrong, added a comment EDIT2: And sent my own PR as the current logic is wrong anyway

sysconf works correctly on the container, what else can I check?

[130] # cat x.c; echo; gcc x.c -o x; ./x
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <unistd.h>

long sysconf(int name);

int main(void) {
    int nproc_conf, nproc_onln;
    nproc_conf = sysconf(_SC_NPROCESSORS_CONF);
    nproc_onln = sysconf(_SC_NPROCESSORS_ONLN);
    printf("_SC: This system has %d processors configured and %d processors online.\n", nproc_conf, nproc_onln);
    printf("get_nprocs: This system has %d processors configured and %d processors available.\n", get_nprocs_conf(), get_nprocs());
    exit(EXIT_SUCCESS);
}

_SC: This system has 48 processors configured and 6 processors online.
get_nprocs: This system has 48 processors configured and 6 processors available.

C0rn3j avatar Jan 30 '24 17:01 C0rn3j