-
-
Notifications
You must be signed in to change notification settings - Fork 441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sparc64] limit cpu shown on active (online) cpus only? #757
Comments
Can you please post the output of |
PS: there's util-linux dump from a physical machine (not a VM) with all cpus online, which is not exactly my case, but could be probably useful. |
There is a very obvious bug here which is determining the amount of active CPUs on Linux using a custom implementation equivalent to The corresponding Solaris code actually does it correctly [1]: s = sysconf(_SC_NPROCESSORS_ONLN);
if (s < 1)
CRT_fatalError("Cannot get active CPU count by sysconf(_SC_NPROCESSORS_ONLN)");
if (s != super->activeCPUs) {
change = true;
hsuper->activeCPUs = s;
} while the corresponding Linux code reads out The obvious solution would be to determine the number of active CPUs using
|
This would be my suggested fix: From af53a75a5c23b1d4aec4aceadbc329997e306f15 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Sun, 3 Dec 2023 19:01:00 +0100
Subject: [PATCH] Use sysconf() to determine active and existing CPUs on Linux
---
linux/LinuxMachine.c | 64 +++++++-------------------------------------
1 file changed, 10 insertions(+), 54 deletions(-)
diff --git a/linux/LinuxMachine.c b/linux/LinuxMachine.c
index aa1ff5f8..b71502ef 100644
--- a/linux/LinuxMachine.c
+++ b/linux/LinuxMachine.c
@@ -40,9 +40,6 @@ in the source distribution for its full text.
#define O_PATH 010000000 // declare for ancient glibc versions
#endif
-/* 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
- */
static void LinuxMachine_updateCPUcount(LinuxMachine* this) {
unsigned int existing = 0, active = 0;
Machine* super = &this->super;
@@ -56,63 +53,22 @@ static void LinuxMachine_updateCPUcount(LinuxMachine* this) {
super->existingCPUs = 1;
}
- DIR* dir = opendir("/sys/devices/system/cpu");
- if (!dir)
- return;
-
unsigned int currExisting = super->existingCPUs;
- const struct dirent* entry;
- while ((entry = readdir(dir)) != NULL) {
- if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN)
- continue;
-
- if (!String_startsWith(entry->d_name, "cpu"))
- continue;
+ existing = sysconf(_SC_NPROCESSORS_CONF);
+ active = sysconf(_SC_NPROCESSORS_ONLN);
- char* endp;
- unsigned long int id = strtoul(entry->d_name + 3, &endp, 10);
- if (id == ULONG_MAX || endp == entry->d_name + 3 || *endp != '\0')
- continue;
+ this->cpuData = xReallocArrayZero(this->cpuData, currExisting ? (currExisting + 1) : 0, existing + 1, sizeof(CPUData));
+ this->cpuData[0].online = true; /* average is always "online" */
+ currExisting = existing;
-#ifdef HAVE_OPENAT
- int cpuDirFd = openat(dirfd(dir), entry->d_name, O_DIRECTORY | O_PATH | O_NOFOLLOW);
- if (cpuDirFd < 0)
- continue;
-#else
- char cpuDirFd[4096];
- xSnprintf(cpuDirFd, sizeof(cpuDirFd), "/sys/devices/system/cpu/%s", entry->d_name);
-#endif
-
- existing++;
-
- /* readdir() iterates with no specific order */
- unsigned int max = MAXIMUM(existing, id + 1);
- if (max > currExisting) {
- this->cpuData = xReallocArrayZero(this->cpuData, currExisting ? (currExisting + 1) : 0, max + /* aggregate */ 1, sizeof(CPUData));
- this->cpuData[0].online = true; /* average is always "online" */
- currExisting = max;
- }
-
- char buffer[8];
- ssize_t res = xReadfileat(cpuDirFd, "online", buffer, sizeof(buffer));
- /* If the file "online" does not exist or on failure count as active */
- if (res < 1 || buffer[0] != '0') {
- active++;
- this->cpuData[id + 1].online = true;
- } else {
- this->cpuData[id + 1].online = false;
- }
-
- Compat_openatArgClose(cpuDirFd);
+ for (unsigned int i = 0; i < existing; i++) {
+ if (i <= active)
+ this->cpuData[i].online = true;
+ else
+ this->cpuData[i].online = false;
}
- closedir(dir);
-
- // return if no CPU is found
- if (existing < 1)
- return;
-
#ifdef HAVE_SENSORS_SENSORS_H
/* When started with offline CPUs, libsensors does not monitor those,
* even when they become online. */
--
2.43.0 However, |
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 htop-dev#757
#1394 should fix the trailing offline threads not showing up as offline since 3.1.0 The threads in both people's screenshots are not marked as offline. @mator @zv-io if you can test that #1394 marks the offline CPUs as such, this should be closed in favor of #1198 after a merge. EDIT: Sorry for the double post, my intention was to edit the now-deleted comment. |
Hello!
After PR #656 htop shows all available CPUs , instead of active ones. How do i limit it to show it to only active CPUs?
For example, on sparc64 linux platform, under LDOM (read virtual machine), there's only particular list of CPUs available for LDOM. I.e. my LDOM has 8 active CPUs (allocated for VM from hypervisor):
and there's no point to show unavailable (256-8 == 248) CPUs (which would 0 load anyway).
This is how
htop
(3.0.5) was looking before:and this is how it (3.1.0-dev) looks now:
The text was updated successfully, but these errors were encountered: