From 0d53245cf94f2d1a16c87488da6e427bbc520be4 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Wed, 4 May 2022 18:21:41 +0200 Subject: LXC: Limit CPU count to what is given in /proc/cpuinfo despite the container seeing the real host CPUs --- linux/LinuxProcessList.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'linux/LinuxProcessList.c') diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 5e18f6d3..cc60ee8b 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 = 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; } -- cgit v1.2.3 From 33973f7e40b6b24e219f5c9d19b205f6f9ddeed4 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Sat, 7 May 2022 16:05:11 +0200 Subject: Limit active CPU count under LXC as well Fixes the initial htoprc not being configured to amount for the host CPU count --- linux/LinuxProcessList.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'linux/LinuxProcessList.c') diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index cc60ee8b..06644887 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -265,7 +265,7 @@ static void LinuxProcessList_updateCPUcount(ProcessList* super) { 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 = scanAvailableCPUsFromCPUinfo(this); + currExisting = active = scanAvailableCPUsFromCPUinfo(this); } #ifdef HAVE_SENSORS_SENSORS_H -- cgit v1.2.3