summaryrefslogtreecommitdiffstats
path: root/pcp
diff options
context:
space:
mode:
authorNathan Scott <nathans@redhat.com>2023-05-02 09:02:22 +1000
committerNathan Scott <nathans@redhat.com>2023-05-08 13:06:07 +1000
commit0bdade1b6cb40c5bd374a93ac0489058a7421bb5 (patch)
tree0e0225f7dbf6867402c5ed3481a705d01941f42e /pcp
parente4ebe18b67c366d367231a1123b057c82018cf5b (diff)
Introduce Machine class for host-specific info (split from ProcessList)
First stage in sanitizing the process list structure so that htop can support other types of lists too (cgroups, filesystems, ...), in the not-too-distant future. This introduces struct Machine for system-wide information while keeping process-list information in ProcessList (now much less). Next step is to propogate this separation into each platform, to match these core changes.
Diffstat (limited to 'pcp')
-rw-r--r--pcp/PCPDynamicColumn.c2
-rw-r--r--pcp/PCPProcess.c6
-rw-r--r--pcp/PCPProcess.h4
-rw-r--r--pcp/PCPProcessList.c86
-rw-r--r--pcp/PCPProcessList.h8
-rw-r--r--pcp/Platform.c34
6 files changed, 81 insertions, 59 deletions
diff --git a/pcp/PCPDynamicColumn.c b/pcp/PCPDynamicColumn.c
index 8c35fc10..23b896e6 100644
--- a/pcp/PCPDynamicColumn.c
+++ b/pcp/PCPDynamicColumn.c
@@ -304,7 +304,7 @@ void PCPDynamicColumn_writeField(PCPDynamicColumn* this, const Process* proc, Ri
}
int PCPDynamicColumn_compareByKey(const PCPProcess* p1, const PCPProcess* p2, ProcessField key) {
- const PCPDynamicColumn* column = Hashtable_get(p1->super.settings->dynamicColumns, key);
+ const PCPDynamicColumn* column = Hashtable_get(p1->super.host->settings->dynamicColumns, key);
if (!column)
return -1;
diff --git a/pcp/PCPProcess.c b/pcp/PCPProcess.c
index b8b87cab..cefd0ac3 100644
--- a/pcp/PCPProcess.c
+++ b/pcp/PCPProcess.c
@@ -88,10 +88,10 @@ const ProcessFieldData Process_fields[] = {
[AUTOGROUP_NICE] = { .name = "AUTOGROUP_NICE", .title = " ANI", .description = "Nice value (the higher the value, the more other processes take priority) associated with the process autogroup", .flags = PROCESS_FLAG_LINUX_AUTOGROUP, },
};
-Process* PCPProcess_new(const Settings* settings) {
+Process* PCPProcess_new(const Machine* host) {
PCPProcess* this = xCalloc(1, sizeof(PCPProcess));
Object_setClass(this, Class(PCPProcess));
- Process_init(&this->super, settings);
+ Process_init(&this->super, host);
return &this->super;
}
@@ -113,7 +113,7 @@ static void PCPProcess_printDelay(float delay_percent, char* buffer, int n) {
static void PCPProcess_writeField(const Process* this, RichString* str, ProcessField field) {
const PCPProcess* pp = (const PCPProcess*) this;
- bool coloring = this->settings->highlightMegabytes;
+ bool coloring = this->host->settings->highlightMegabytes;
char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR];
int n = sizeof(buffer) - 1;
diff --git a/pcp/PCPProcess.h b/pcp/PCPProcess.h
index 46ba07fe..be33111e 100644
--- a/pcp/PCPProcess.h
+++ b/pcp/PCPProcess.h
@@ -13,9 +13,9 @@ in the source distribution for its full text.
#include <stdbool.h>
+#include "Machine.h"
#include "Object.h"
#include "Process.h"
-#include "Settings.h"
#define PROCESS_FLAG_LINUX_CGROUP 0x00000800
@@ -93,7 +93,7 @@ extern const ProcessFieldData Process_fields[LAST_PROCESSFIELD];
extern const ProcessClass PCPProcess_class;
-Process* PCPProcess_new(const Settings* settings);
+Process* PCPProcess_new(const Machine* host);
void Process_delete(Object* cast);
diff --git a/pcp/PCPProcessList.c b/pcp/PCPProcessList.c
index a5eb64d6..0e345fc0 100644
--- a/pcp/PCPProcessList.c
+++ b/pcp/PCPProcessList.c
@@ -19,6 +19,7 @@ in the source distribution for its full text.
#include <sys/time.h>
#include "Macros.h"
+#include "Machine.h"
#include "Object.h"
#include "Platform.h"
#include "Process.h"
@@ -30,16 +31,16 @@ in the source distribution for its full text.
static void PCPProcessList_updateCPUcount(PCPProcessList* this) {
- ProcessList* pl = &(this->super);
- pl->activeCPUs = PCPMetric_instanceCount(PCP_PERCPU_SYSTEM);
+ Machine* host = this->super.host;
+ host->activeCPUs = PCPMetric_instanceCount(PCP_PERCPU_SYSTEM);
unsigned int cpus = Platform_getMaxCPU();
- if (cpus == pl->existingCPUs)
+ if (cpus == host->existingCPUs)
return;
if (cpus == 0)
- cpus = pl->activeCPUs;
+ cpus = host->activeCPUs;
if (cpus <= 1)
- cpus = pl->activeCPUs = 1;
- pl->existingCPUs = cpus;
+ cpus = host->activeCPUs = 1;
+ host->existingCPUs = cpus;
free(this->percpu);
free(this->values);
@@ -63,11 +64,11 @@ static char* setUser(UsersTable* this, unsigned int uid, int pid, int offset) {
return name;
}
-ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId) {
+ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList) {
PCPProcessList* this = xCalloc(1, sizeof(PCPProcessList));
ProcessList* super = &(this->super);
- ProcessList_init(super, Class(PCPProcess), usersTable, pidMatchList, userId);
+ ProcessList_init(super, Class(PCPProcess), host, pidMatchList);
struct timeval timestamp;
gettimeofday(&timestamp, NULL);
@@ -83,7 +84,7 @@ void ProcessList_delete(ProcessList* pl) {
PCPProcessList* this = (PCPProcessList*) pl;
ProcessList_done(pl);
free(this->values);
- for (unsigned int i = 0; i < pl->existingCPUs; i++)
+ for (unsigned int i = 0; i < pl->host->existingCPUs; i++)
free(this->percpu[i]);
free(this->percpu);
free(this->cpu);
@@ -341,7 +342,9 @@ static void PCPProcessList_updateCmdline(Process* process, int pid, int offset,
static bool PCPProcessList_updateProcesses(PCPProcessList* this, double period, struct timeval* tv) {
ProcessList* pl = (ProcessList*) this;
- const Settings* settings = pl->settings;
+
+ Machine* host = pl->host;
+ const Settings* settings = host->settings;
bool hideKernelThreads = settings->hideKernelThreads;
bool hideUserlandThreads = settings->hideUserlandThreads;
@@ -406,11 +409,11 @@ static bool PCPProcessList_updateProcesses(PCPProcessList* this, double period,
float percent_cpu = (pp->utime + pp->stime - lasttimes) / period * 100.0;
proc->percent_cpu = isnan(percent_cpu) ?
- 0.0 : CLAMP(percent_cpu, 0.0, pl->activeCPUs * 100.0);
- proc->percent_mem = proc->m_resident / (double)pl->totalMem * 100.0;
+ 0.0 : CLAMP(percent_cpu, 0.0, host->activeCPUs * 100.0);
+ proc->percent_mem = proc->m_resident / (double)host->totalMem * 100.0;
Process_updateCPUFieldWidths(proc->percent_cpu);
- PCPProcessList_updateUsername(proc, pid, offset, pl->usersTable);
+ PCPProcessList_updateUsername(proc, pid, offset, host->usersTable);
if (!preExisting) {
PCPProcessList_updateCmdline(proc, pid, offset, command);
@@ -465,39 +468,40 @@ static bool PCPProcessList_updateProcesses(PCPProcessList* this, double period,
}
static void PCPProcessList_updateMemoryInfo(ProcessList* super) {
+ Machine* host = super->host;
unsigned long long int freeMem = 0;
unsigned long long int swapFreeMem = 0;
unsigned long long int sreclaimableMem = 0;
- super->totalMem = super->usedMem = super->cachedMem = 0;
- super->usedSwap = super->totalSwap = super->sharedMem = 0;
+ host->totalMem = host->usedMem = host->cachedMem = 0;
+ host->usedSwap = host->totalSwap = host->sharedMem = 0;
pmAtomValue value;
if (PCPMetric_values(PCP_MEM_TOTAL, &value, 1, PM_TYPE_U64) != NULL)
- super->totalMem = value.ull;
+ host->totalMem = value.ull;
if (PCPMetric_values(PCP_MEM_FREE, &value, 1, PM_TYPE_U64) != NULL)
freeMem = value.ull;
if (PCPMetric_values(PCP_MEM_BUFFERS, &value, 1, PM_TYPE_U64) != NULL)
- super->buffersMem = value.ull;
+ host->buffersMem = value.ull;
if (PCPMetric_values(PCP_MEM_SRECLAIM, &value, 1, PM_TYPE_U64) != NULL)
sreclaimableMem = value.ull;
if (PCPMetric_values(PCP_MEM_SHARED, &value, 1, PM_TYPE_U64) != NULL)
- super->sharedMem = value.ull;
+ host->sharedMem = value.ull;
if (PCPMetric_values(PCP_MEM_CACHED, &value, 1, PM_TYPE_U64) != NULL)
- super->cachedMem = value.ull + sreclaimableMem - super->sharedMem;
- const memory_t usedDiff = freeMem + super->cachedMem + sreclaimableMem + super->buffersMem;
- super->usedMem = (super->totalMem >= usedDiff) ?
- super->totalMem - usedDiff : super->totalMem - freeMem;
+ host->cachedMem = value.ull + sreclaimableMem - host->sharedMem;
+ const memory_t usedDiff = freeMem + host->cachedMem + sreclaimableMem + host->buffersMem;
+ host->usedMem = (host->totalMem >= usedDiff) ?
+ host->totalMem - usedDiff : host->totalMem - freeMem;
if (PCPMetric_values(PCP_MEM_AVAILABLE, &value, 1, PM_TYPE_U64) != NULL)
- super->availableMem = MINIMUM(value.ull, super->totalMem);
+ host->availableMem = MINIMUM(value.ull, host->totalMem);
else
- super->availableMem = freeMem;
+ host->availableMem = freeMem;
if (PCPMetric_values(PCP_MEM_SWAPFREE, &value, 1, PM_TYPE_U64) != NULL)
swapFreeMem = value.ull;
if (PCPMetric_values(PCP_MEM_SWAPTOTAL, &value, 1, PM_TYPE_U64) != NULL)
- super->totalSwap = value.ull;
+ host->totalSwap = value.ull;
if (PCPMetric_values(PCP_MEM_SWAPCACHED, &value, 1, PM_TYPE_U64) != NULL)
- super->cachedSwap = value.ull;
- super->usedSwap = super->totalSwap - swapFreeMem - super->cachedSwap;
+ host->cachedSwap = value.ull;
+ host->usedSwap = host->totalSwap - swapFreeMem - host->cachedSwap;
}
/* make copies of previously sampled values to avoid overwrite */
@@ -576,7 +580,7 @@ static void PCPProcessList_updateAllCPUTime(PCPProcessList* this, PCPMetric metr
static void PCPProcessList_updatePerCPUTime(PCPProcessList* this, PCPMetric metric, CPUMetric cpumetric)
{
- int cpus = this->super.existingCPUs;
+ int cpus = this->super.host->existingCPUs;
if (PCPMetric_values(metric, this->values, cpus, PM_TYPE_U64) == NULL)
memset(this->values, 0, cpus * sizeof(pmAtomValue));
for (int i = 0; i < cpus; i++)
@@ -585,7 +589,7 @@ static void PCPProcessList_updatePerCPUTime(PCPProcessList* this, PCPMetric metr
static void PCPProcessList_updatePerCPUReal(PCPProcessList* this, PCPMetric metric, CPUMetric cpumetric)
{
- int cpus = this->super.existingCPUs;
+ int cpus = this->super.host->existingCPUs;
if (PCPMetric_values(metric, this->values, cpus, PM_TYPE_DOUBLE) == NULL)
memset(this->values, 0, cpus * sizeof(pmAtomValue));
for (int i = 0; i < cpus; i++)
@@ -630,6 +634,7 @@ static inline void PCPProcessList_scanZfsArcstats(PCPProcessList* this) {
}
static void PCPProcessList_updateHeader(ProcessList* super, const Settings* settings) {
+ Machine* host = super->host;
PCPProcessList_updateMemoryInfo(super);
PCPProcessList* this = (PCPProcessList*) super;
@@ -647,7 +652,7 @@ static void PCPProcessList_updateHeader(ProcessList* super, const Settings* sett
PCPProcessList_updateAllCPUTime(this, PCP_CPU_GUEST, CPU_GUEST_TIME);
PCPProcessList_deriveCPUTime(this->cpu);
- for (unsigned int i = 0; i < super->existingCPUs; i++)
+ for (unsigned int i = 0; i < host->existingCPUs; i++)
PCPProcessList_backupCPUTime(this->percpu[i]);
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_USER, CPU_USER_TIME);
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_NICE, CPU_NICE_TIME);
@@ -658,7 +663,7 @@ static void PCPProcessList_updateHeader(ProcessList* super, const Settings* sett
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_SOFTIRQ, CPU_SOFTIRQ_TIME);
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_STEAL, CPU_STEAL_TIME);
PCPProcessList_updatePerCPUTime(this, PCP_PERCPU_GUEST, CPU_GUEST_TIME);
- for (unsigned int i = 0; i < super->existingCPUs; i++)
+ for (unsigned int i = 0; i < host->existingCPUs; i++)
PCPProcessList_deriveCPUTime(this->percpu[i]);
if (settings->showCPUFrequency)
@@ -669,7 +674,8 @@ static void PCPProcessList_updateHeader(ProcessList* super, const Settings* sett
void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
PCPProcessList* this = (PCPProcessList*) super;
- const Settings* settings = super->settings;
+ Machine* host = super->host;
+ const Settings* settings = host->settings;
bool enabled = !pauseProcessUpdate;
bool flagged = settings->showCPUFrequency;
@@ -716,9 +722,19 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
PCPProcessList_updateProcesses(this, period, &timestamp);
}
-bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id) {
- assert(id < super->existingCPUs);
- (void) super;
+Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
+ Machine* this = xCalloc(1, sizeof(Machine));
+ Machine_init(this, usersTable, userId);
+ return this;
+}
+
+void Machine_delete(Machine* host) {
+ free(host);
+}
+
+bool Machine_isCPUonline(const Machine* host, unsigned int id) {
+ assert(id < host->existingCPUs);
+ (void) host;
pmAtomValue value;
if (PCPMetric_instance(PCP_PERCPU_SYSTEM, id, id, &value, PM_TYPE_U32))
diff --git a/pcp/PCPProcessList.h b/pcp/PCPProcessList.h
index 90e9939c..9bce9cd7 100644
--- a/pcp/PCPProcessList.h
+++ b/pcp/PCPProcessList.h
@@ -63,12 +63,16 @@ typedef struct PCPProcessList_ {
ZfsArcStats zfs;
} PCPProcessList;
-ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, uid_t userId);
+ProcessList* ProcessList_new(Machine* host, Hashtable* pidMatchList);
void ProcessList_delete(ProcessList* pl);
void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate);
-bool ProcessList_isCPUonline(const ProcessList* super, unsigned int id);
+Machine* Machine_new(UsersTable* usersTable, uid_t userId);
+
+bool Machine_isCPUonline(const Machine* host, unsigned int id);
+
+void Machine_delete(Machine* host);
#endif
diff --git a/pcp/Platform.c b/pcp/Platform.c
index 87de2c2f..181898aa 100644
--- a/pcp/Platform.c
+++ b/pcp/Platform.c
@@ -485,6 +485,7 @@ long long Platform_getBootTime(void) {
}
static double Platform_setOneCPUValues(Meter* this, pmAtomValue* values) {
+ Settings* settings = this->host->settings;
unsigned long long value = values[CPU_TOTAL_PERIOD].ull;
double total = (double) (value == 0 ? 1 : value);
@@ -493,7 +494,7 @@ static double Platform_setOneCPUValues(Meter* this, pmAtomValue* values) {
double* v = this->values;
v[CPU_METER_NICE] = values[CPU_NICE_PERIOD].ull / total * 100.0;
v[CPU_METER_NORMAL] = values[CPU_USER_PERIOD].ull / total * 100.0;
- if (this->pl->settings->detailedCPUTime) {
+ if (settings->detailedCPUTime) {
v[CPU_METER_KERNEL] = values[CPU_SYSTEM_PERIOD].ull / total * 100.0;
v[CPU_METER_IRQ] = values[CPU_IRQ_PERIOD].ull / total * 100.0;
v[CPU_METER_SOFTIRQ] = values[CPU_SOFTIRQ_PERIOD].ull / total * 100.0;
@@ -502,7 +503,7 @@ static double Platform_setOneCPUValues(Meter* this, pmAtomValue* values) {
v[CPU_METER_IOWAIT] = values[CPU_IOWAIT_PERIOD].ull / total * 100.0;
this->curItems = 8;
percent = v[CPU_METER_NICE] + v[CPU_METER_NORMAL] + v[CPU_METER_KERNEL] + v[CPU_METER_IRQ] + v[CPU_METER_SOFTIRQ];
- if (this->pl->settings->accountGuestInCPUMeter) {
+ if (settings->accountGuestInCPUMeter) {
percent += v[CPU_METER_STEAL] + v[CPU_METER_GUEST];
}
} else {
@@ -523,23 +524,24 @@ static double Platform_setOneCPUValues(Meter* this, pmAtomValue* values) {
}
double Platform_setCPUValues(Meter* this, int cpu) {
- const PCPProcessList* pl = (const PCPProcessList*) this->pl;
+ const PCPProcessList* pl = (const PCPProcessList*) this->host->pl;
if (cpu <= 0) /* use aggregate values */
return Platform_setOneCPUValues(this, pl->cpu);
return Platform_setOneCPUValues(this, pl->percpu[cpu - 1]);
}
void Platform_setMemoryValues(Meter* this) {
- const ProcessList* pl = this->pl;
+ const Machine* host = this->host;
+ const ProcessList* pl = host->pl;
const PCPProcessList* ppl = (const PCPProcessList*) pl;
- this->total = pl->totalMem;
- this->values[MEMORY_METER_USED] = pl->usedMem;
- this->values[MEMORY_METER_BUFFERS] = pl->buffersMem;
- this->values[MEMORY_METER_SHARED] = pl->sharedMem;
+ this->total = host->totalMem;
+ this->values[MEMORY_METER_USED] = host->usedMem;
+ this->values[MEMORY_METER_BUFFERS] = host->buffersMem;
+ this->values[MEMORY_METER_SHARED] = host->sharedMem;
// this->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
- this->values[MEMORY_METER_CACHE] = pl->cachedMem;
- this->values[MEMORY_METER_AVAILABLE] = pl->availableMem;
+ this->values[MEMORY_METER_CACHE] = host->cachedMem;
+ this->values[MEMORY_METER_AVAILABLE] = host->availableMem;
if (ppl->zfs.enabled != 0) {
// ZFS does not shrink below the value of zfs_arc_min.
@@ -553,10 +555,10 @@ void Platform_setMemoryValues(Meter* this) {
}
void Platform_setSwapValues(Meter* this) {
- const ProcessList* pl = this->pl;
- this->total = pl->totalSwap;
- this->values[SWAP_METER_USED] = pl->usedSwap;
- this->values[SWAP_METER_CACHE] = pl->cachedSwap;
+ const Machine* host = this->host;
+ this->total = host->totalSwap;
+ this->values[SWAP_METER_USED] = host->usedSwap;
+ this->values[SWAP_METER_CACHE] = host->cachedSwap;
// this->values[SWAP_METER_FRONTSWAP] = "pages that are accounted to swap but stored elsewhere, like frontswap on linux"
}
@@ -593,13 +595,13 @@ void Platform_setZramValues(Meter* this) {
}
void Platform_setZfsArcValues(Meter* this) {
- const PCPProcessList* ppl = (const PCPProcessList*) this->pl;
+ const PCPProcessList* ppl = (const PCPProcessList*) this->host->pl;
ZfsArcMeter_readStats(this, &(ppl->zfs));
}
void Platform_setZfsCompressedArcValues(Meter* this) {
- const PCPProcessList* ppl = (const PCPProcessList*) this->pl;
+ const PCPProcessList* ppl = (const PCPProcessList*) this->host->pl;
ZfsCompressedArcMeter_readStats(this, &(ppl->zfs));
}

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