summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Lange <DLange@git.local>2022-05-04 18:21:41 +0200
committerDaniel Lange <DLange@git.local>2022-05-04 18:21:41 +0200
commit0d53245cf94f2d1a16c87488da6e427bbc520be4 (patch)
tree24d33ec7be9366bbc4c9080e1019c0aa982bc72f
parentc7413fd6771b65388bea14ef42863444c6eaa419 (diff)
LXC: Limit CPU count to what is given in /proc/cpuinfo despite the container seeing the real host CPUs
-rw-r--r--linux/LinuxProcessList.c30
-rw-r--r--linux/Platform.h2
2 files changed, 31 insertions, 1 deletions
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;
}
diff --git a/linux/Platform.h b/linux/Platform.h
index f2c314f5..e6fa1610 100644
--- a/linux/Platform.h
+++ b/linux/Platform.h
@@ -51,6 +51,8 @@ extern const MeterClass* const Platform_meterTypes[];
bool Platform_init(void);
void Platform_done(void);
+extern bool Running_containerized;
+
void Platform_setBindings(Htop_Action* keys);
int Platform_getUptime(void);

© 2014-2024 Faster IT GmbH | imprint | privacy policy