diff options
author | Daniel Lange <DLange@git.local> | 2022-05-17 19:10:40 +0200 |
---|---|---|
committer | Daniel Lange <DLange@git.local> | 2022-05-17 19:10:40 +0200 |
commit | 87793b85552de1a67eb7a779c745ed02d04322bf (patch) | |
tree | 57c0614baddb15a52972cf0c32cbf26264f2dd0c /linux/LinuxProcessList.c | |
parent | fe7f238e2ccac3b98d36a7a11f938064478a717d (diff) | |
parent | 33973f7e40b6b24e219f5c9d19b205f6f9ddeed4 (diff) |
Merge branch 'lxc-cpu-count-fix' of fasterit/htop
Diffstat (limited to 'linux/LinuxProcessList.c')
-rw-r--r-- | linux/LinuxProcessList.c | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 14f3824a..31302e61 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -166,6 +166,28 @@ static void LinuxProcessList_initNetlinkSocket(LinuxProcessList* this) { #endif +static unsigned int scanAvailableCPUsFromCPUinfo(LinuxProcessList* this) { + FILE* file = fopen(PROCCPUINFOFILE, "r"); + if (file == NULL) + return this->super.existingCPUs; + + unsigned int availableCPUs = 0; + + while (!feof(file)) { + char buffer[PROC_LINE_LENGTH]; + + if (fgets(buffer, PROC_LINE_LENGTH, file) == NULL) + break; + + if (String_startsWith(buffer, "processor")) + availableCPUs++; + } + + fclose(file); + + return availableCPUs ? availableCPUs : 1; +} + static void LinuxProcessList_updateCPUcount(ProcessList* super) { /* Similar to get_nprocs_conf(3) / _SC_NPROCESSORS_CONF * https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/getsysstats.c;hb=HEAD @@ -240,6 +262,12 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { if (existing < 1) return; + if (Running_containerized) { + /* LXC munges /proc/cpuinfo but not the /sys/devices/system/cpu/ files, + * so limit the visible CPUs to what the guest has been configured to see: */ + currExisting = active = scanAvailableCPUsFromCPUinfo(this); + } + #ifdef HAVE_SENSORS_SENSORS_H /* When started with offline CPUs, libsensors does not monitor those, * even when they become online. */ @@ -248,7 +276,7 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { #endif super->activeCPUs = active; - assert(existing == currExisting); + assert(Running_containerized || (existing == currExisting)); super->existingCPUs = currExisting; } |