diff options
author | Sohaib <sohaib.amhmd@gmail.com> | 2021-08-14 17:30:19 -0400 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2021-08-16 17:23:07 +0200 |
commit | fefff80631e86f0296a2621a3699b16de60b86c6 (patch) | |
tree | d63400fea86d63c7075aa828faadae39cdca75b4 /pcp | |
parent | edafa26f9e6298bcd935a2eaf9dfcec8868eff8d (diff) |
PCP: PCPMetric.[ch] Mdoule
Split the PCP Metric API (functions `Metric_*`) into their own module.
as @BenBE suggested.
Diffstat (limited to 'pcp')
-rw-r--r-- | pcp/PCPDynamicColumn.c | 14 | ||||
-rw-r--r-- | pcp/PCPDynamicColumn.h | 3 | ||||
-rw-r--r-- | pcp/PCPDynamicMeter.c | 27 | ||||
-rw-r--r-- | pcp/PCPDynamicMeter.h | 5 | ||||
-rw-r--r-- | pcp/PCPMetric.c | 177 | ||||
-rw-r--r-- | pcp/PCPMetric.h | 179 | ||||
-rw-r--r-- | pcp/PCPProcess.c | 1 | ||||
-rw-r--r-- | pcp/PCPProcess.h | 2 | ||||
-rw-r--r-- | pcp/PCPProcessList.c | 124 | ||||
-rw-r--r-- | pcp/Platform.c | 274 | ||||
-rw-r--r-- | pcp/Platform.h | 182 |
11 files changed, 525 insertions, 463 deletions
diff --git a/pcp/PCPDynamicColumn.c b/pcp/PCPDynamicColumn.c index 2848490e..141978a7 100644 --- a/pcp/PCPDynamicColumn.c +++ b/pcp/PCPDynamicColumn.c @@ -14,21 +14,21 @@ in the source distribution for its full text. #include <ctype.h> #include <dirent.h> #include <errno.h> -#include <limits.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include "CRT.h" #include "Macros.h" #include "Platform.h" #include "Process.h" +#include "ProcessList.h" #include "RichString.h" #include "XUtils.h" #include "pcp/PCPProcess.h" +#include "pcp/PCPMetric.h" static bool PCPDynamicColumn_addMetric(PCPDynamicColumns* columns, PCPDynamicColumn* column) { @@ -228,10 +228,10 @@ void PCPDynamicColumns_init(PCPDynamicColumns* columns) { void PCPDynamicColumn_writeField(PCPDynamicColumn* this, const Process* proc, RichString* str) { const PCPProcess* pp = (const PCPProcess*) proc; - unsigned int type = Metric_type(this->id); + unsigned int type = PCPMetric_type(this->id); pmAtomValue atom; - if (!Metric_instance(this->id, proc->pid, pp->offset, &atom, type)) { + if (!PCPMetric_instance(this->id, proc->pid, pp->offset, &atom, type)) { RichString_appendAscii(str, CRT_colors[METER_VALUE_ERROR], "no data"); return; } @@ -288,11 +288,11 @@ int PCPDynamicColumn_compareByKey(const PCPProcess* p1, const PCPProcess* p2, Pr const PCPDynamicColumn* column = Hashtable_get(p1->super.processList->dynamicColumns, key); size_t metric = column->id; - unsigned int type = Metric_type(metric); + unsigned int type = PCPMetric_type(metric); pmAtomValue atom1 = {0}, atom2 = {0}; - if (!Metric_instance(metric, p1->super.pid, p1->offset, &atom1, type) || - !Metric_instance(metric, p2->super.pid, p2->offset, &atom2, type)) { + if (!PCPMetric_instance(metric, p1->super.pid, p1->offset, &atom1, type) || + !PCPMetric_instance(metric, p2->super.pid, p2->offset, &atom2, type)) { if (type == PM_TYPE_STRING) { free(atom1.cp); free(atom2.cp); diff --git a/pcp/PCPDynamicColumn.h b/pcp/PCPDynamicColumn.h index 39f79358..28f3d5ca 100644 --- a/pcp/PCPDynamicColumn.h +++ b/pcp/PCPDynamicColumn.h @@ -1,7 +1,8 @@ #ifndef HEADER_PCPDynamicColumn #define HEADER_PCPDynamicColumn -#include "CRT.h" +#include <stddef.h> + #include "DynamicColumn.h" #include "Hashtable.h" #include "Process.h" diff --git a/pcp/PCPDynamicMeter.c b/pcp/PCPDynamicMeter.c index b90511ec..430f41c9 100644 --- a/pcp/PCPDynamicMeter.c +++ b/pcp/PCPDynamicMeter.c @@ -9,15 +9,22 @@ in the source distribution for its full text. #include "pcp/PCPDynamicMeter.h" -#include <math.h> - -#include "Object.h" +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <pcp/pmapi.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "Macros.h" #include "Platform.h" -#include "ProcessList.h" #include "RichString.h" -#include "Settings.h" #include "XUtils.h" +#include "pcp/PCPMetric.h" + static PCPDynamicMetric* PCPDynamicMeter_lookupMetric(PCPDynamicMeters* meters, PCPDynamicMeter* meter, const char* name) { size_t bytes = 16 + strlen(meter->super.name) + strlen(name); @@ -278,7 +285,7 @@ void PCPDynamicMeters_init(PCPDynamicMeters* meters) { void PCPDynamicMeter_enable(PCPDynamicMeter* this) { for (size_t i = 0; i < this->totalMetrics; i++) - Metric_enable(this->metrics[i].id, true); + PCPMetric_enable(this->metrics[i].id, true); } void PCPDynamicMeter_updateValues(PCPDynamicMeter* this, Meter* meter) { @@ -291,10 +298,10 @@ void PCPDynamicMeter_updateValues(PCPDynamicMeter* this, Meter* meter) { buffer[bytes++] = '/'; /* separator */ PCPDynamicMetric* metric = &this->metrics[i]; - const pmDesc* desc = Metric_desc(metric->id); + const pmDesc* desc = PCPMetric_desc(metric->id); pmAtomValue atom, raw; - if (!Metric_values(metric->id, &raw, 1, desc->type)) { + if (!PCPMetric_values(metric->id, &raw, 1, desc->type)) { bytes--; /* clear the separator */ continue; } @@ -362,11 +369,11 @@ void PCPDynamicMeter_display(PCPDynamicMeter* this, ATTR_UNUSED const Meter* met for (size_t i = 0; i < this->totalMetrics; i++) { PCPDynamicMetric* metric = &this->metrics[i]; - const pmDesc* desc = Metric_desc(metric->id); + const pmDesc* desc = PCPMetric_desc(metric->id); pmAtomValue atom, raw; char buffer[64]; - if (!Metric_values(metric->id, &raw, 1, desc->type)) + if (!PCPMetric_values(metric->id, &raw, 1, desc->type)) continue; pmUnits conv = desc->units; /* convert to canonical units */ diff --git a/pcp/PCPDynamicMeter.h b/pcp/PCPDynamicMeter.h index cfe488e5..3da7c35b 100644 --- a/pcp/PCPDynamicMeter.h +++ b/pcp/PCPDynamicMeter.h @@ -1,8 +1,13 @@ #ifndef HEADER_PCPDynamicMeter #define HEADER_PCPDynamicMeter +#include <stddef.h> + #include "CRT.h" #include "DynamicMeter.h" +#include "Hashtable.h" +#include "Meter.h" +#include "RichString.h" typedef struct PCPDynamicMetric_ { diff --git a/pcp/PCPMetric.c b/pcp/PCPMetric.c new file mode 100644 index 00000000..c8cfe716 --- /dev/null +++ b/pcp/PCPMetric.c @@ -0,0 +1,177 @@ +/* +htop - PCPMetric.c +(C) 2020-2021 htop dev team +(C) 2020-2021 Red Hat, Inc. +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + +#include "pcp/PCPMetric.h" + +#include <stddef.h> +#include <stdio.h> +#include <string.h> + +#include "XUtils.h" + +#include "pcp/Platform.h" + + +extern Platform* pcp; + +const pmDesc* PCPMetric_desc(PCPMetric metric) { + return &pcp->descs[metric]; +} + +int PCPMetric_type(PCPMetric metric) { + return pcp->descs[metric].type; +} + +pmAtomValue* PCPMetric_values(PCPMetric metric, pmAtomValue* atom, int count, int type) { + if (pcp->result == NULL) + return NULL; + + pmValueSet* vset = pcp->result->vset[metric]; + if (!vset || vset->numval <= 0) + return NULL; + + /* extract requested number of values as requested type */ + const pmDesc* desc = &pcp->descs[metric]; + for (int i = 0; i < vset->numval; i++) { + if (i == count) + break; + const pmValue* value = &vset->vlist[i]; + int sts = pmExtractValue(vset->valfmt, value, desc->type, &atom[i], type); + if (sts < 0) { + if (pmDebugOptions.appl0) + fprintf(stderr, "Error: cannot extract metric value: %s\n", + pmErrStr(sts)); + memset(&atom[i], 0, sizeof(pmAtomValue)); + } + } + return atom; +} + +int PCPMetric_instanceCount(PCPMetric metric) { + pmValueSet* vset = pcp->result->vset[metric]; + if (vset) + return vset->numval; + return 0; +} + +int PCPMetric_instanceOffset(PCPMetric metric, int inst) { + pmValueSet* vset = pcp->result->vset[metric]; + if (!vset || vset->numval <= 0) + return 0; + + /* search for optimal offset for subsequent inst lookups to begin */ + for (int i = 0; i < vset->numval; i++) { + if (inst == vset->vlist[i].inst) + return i; + } + return 0; +} + +static pmAtomValue* PCPMetric_extract(PCPMetric metric, int inst, int offset, pmValueSet* vset, pmAtomValue* atom, int type) { + + /* extract value (using requested type) of given metric instance */ + const pmDesc* desc = &pcp->descs[metric]; + const pmValue* value = &vset->vlist[offset]; + int sts = pmExtractValue(vset->valfmt, value, desc->type, atom, type); + if (sts < 0) { + if (pmDebugOptions.appl0) + fprintf(stderr, "Error: cannot extract %s instance %d value: %s\n", + pcp->names[metric], inst, pmErrStr(sts)); + memset(atom, 0, sizeof(pmAtomValue)); + } + return atom; +} + +pmAtomValue* PCPMetric_instance(PCPMetric metric, int inst, int offset, pmAtomValue* atom, int type) { + + pmValueSet* vset = pcp->result->vset[metric]; + if (!vset || vset->numval <= 0) + return NULL; + + /* fast-path using heuristic offset based on expected location */ + if (offset >= 0 && offset < vset->numval && inst == vset->vlist[offset].inst) + return PCPMetric_extract(metric, inst, offset, vset, atom, type); + + /* slow-path using a linear search for the requested instance */ + for (int i = 0; i < vset->numval; i++) { + if (inst == vset->vlist[i].inst) + return PCPMetric_extract(metric, inst, i, vset, atom, type); + } + return NULL; +} + +/* + * Iterate over a set of instances (incl PM_IN_NULL) + * returning the next instance identifier and offset. + * + * Start it off by passing offset -1 into the routine. + */ +bool PCPMetric_iterate(PCPMetric metric, int* instp, int* offsetp) { + if (!pcp->result) + return false; + + pmValueSet* vset = pcp->result->vset[metric]; + if (!vset || vset->numval <= 0) + return false; + + int offset = *offsetp; + offset = (offset < 0) ? 0 : offset + 1; + if (offset > vset->numval - 1) + return false; + + *offsetp = offset; + *instp = vset->vlist[offset].inst; + return true; +} + +/* Switch on/off a metric for value fetching (sampling) */ +void PCPMetric_enable(PCPMetric metric, bool enable) { + pcp->fetch[metric] = enable ? pcp->pmids[metric] : PM_ID_NULL; +} + +bool PCPMetric_enabled(PCPMetric metric) { + return pcp->fetch[metric] != PM_ID_NULL; +} + +void PCPMetric_enableThreads(void) { + pmValueSet* vset = xCalloc(1, sizeof(pmValueSet)); + vset->vlist[0].inst = PM_IN_NULL; + vset->vlist[0].value.lval = 1; + vset->valfmt = PM_VAL_INSITU; + vset->numval = 1; + vset->pmid = pcp->pmids[PCP_CONTROL_THREADS]; + + pmResult* result = xCalloc(1, sizeof(pmResult)); + result->vset[0] = vset; + result->numpmid = 1; + + int sts = pmStore(result); + if (sts < 0 && pmDebugOptions.appl0) + fprintf(stderr, "Error: cannot enable threads: %s\n", pmErrStr(sts)); + + pmFreeResult(result); +} + +bool PCPMetric_fetch(struct timeval* timestamp) { + if (pcp->result) { + pmFreeResult(pcp->result); + pcp->result = NULL; + } + int sts = pmFetch(pcp->totalMetrics, pcp->fetch, &pcp->result); + if (sts < 0) { + if (pmDebugOptions.appl0) + fprintf(stderr, "Error: cannot fetch metric values: %s\n", + pmErrStr(sts)); + return false; + } + if (timestamp) + *timestamp = pcp->result->timestamp; + return true; +} diff --git a/pcp/PCPMetric.h b/pcp/PCPMetric.h new file mode 100644 index 00000000..7d625035 --- /dev/null +++ b/pcp/PCPMetric.h @@ -0,0 +1,179 @@ +#ifndef HEADER_PCPMetric +#define HEADER_PCPMetric +/* +htop - PCPMetric.h +(C) 2020-2021 htop dev team +(C) 2020-2021 Red Hat, Inc. +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include <ctype.h> +#include <stdbool.h> +#include <pcp/pmapi.h> +#include <sys/time.h> + +/* use htop config.h values for these macros, not pcp values */ +#undef PACKAGE_URL +#undef PACKAGE_NAME +#undef PACKAGE_STRING +#undef PACKAGE_TARNAME +#undef PACKAGE_VERSION +#undef PACKAGE_BUGREPORT + + +typedef enum PCPMetric_ { + PCP_CONTROL_THREADS, /* proc.control.perclient.threads */ + + PCP_HINV_NCPU, /* hinv.ncpu */ + PCP_HINV_CPUCLOCK, /* hinv.cpu.clock */ + PCP_UNAME_SYSNAME, /* kernel.uname.sysname */ + PCP_UNAME_RELEASE, /* kernel.uname.release */ + PCP_UNAME_MACHINE, /* kernel.uname.machine */ + PCP_UNAME_DISTRO, /* kernel.uname.distro */ + PCP_LOAD_AVERAGE, /* kernel.all.load */ + PCP_PID_MAX, /* kernel.all.pid_max */ + PCP_UPTIME, /* kernel.all.uptime */ + PCP_BOOTTIME, /* kernel.all.boottime */ + PCP_CPU_USER, /* kernel.all.cpu.user */ + PCP_CPU_NICE, /* kernel.all.cpu.nice */ + PCP_CPU_SYSTEM, /* kernel.all.cpu.sys */ + PCP_CPU_IDLE, /* kernel.all.cpu.idle */ + PCP_CPU_IOWAIT, /* kernel.all.cpu.wait.total */ + PCP_CPU_IRQ, /* kernel.all.cpu.intr */ + PCP_CPU_SOFTIRQ, /* kernel.all.cpu.irq.soft */ + PCP_CPU_STEAL, /* kernel.all.cpu.steal */ + PCP_CPU_GUEST, /* kernel.all.cpu.guest */ + PCP_CPU_GUESTNICE, /* kernel.all.cpu.guest_nice */ + PCP_PERCPU_USER, /* kernel.percpu.cpu.user */ + PCP_PERCPU_NICE, /* kernel.percpu.cpu.nice */ + PCP_PERCPU_SYSTEM, /* kernel.percpu.cpu.sys */ + PCP_PERCPU_IDLE, /* kernel.percpu.cpu.idle */ + PCP_PERCPU_IOWAIT, /* kernel.percpu.cpu.wait.total */ + PCP_PERCPU_IRQ, /* kernel.percpu.cpu.intr */ + PCP_PERCPU_SOFTIRQ, /* kernel.percpu.cpu.irq.soft */ + PCP_PERCPU_STEAL, /* kernel.percpu.cpu.steal */ + PCP_PERCPU_GUEST, /* kernel.percpu.cpu.guest */ + PCP_PERCPU_GUESTNICE, /* kernel.percpu.cpu.guest_nice */ + PCP_MEM_TOTAL, /* mem.physmem */ + PCP_MEM_FREE, /* mem.util.free */ + PCP_MEM_BUFFERS, /* mem.util.bufmem */ + PCP_MEM_CACHED, /* mem.util.cached */ + PCP_MEM_SHARED, /* mem.util.shared */ + PCP_MEM_AVAILABLE, /* mem.util.available */ + PCP_MEM_SRECLAIM, /* mem.util.slabReclaimable */ + PCP_MEM_SWAPCACHED, /* mem.util.swapCached */ + PCP_MEM_SWAPTOTAL, /* mem.util.swapTotal */ + PCP_MEM_SWAPFREE, /* mem.util.swapFree */ + PCP_DISK_READB, /* disk.all.read_bytes */ + PCP_DISK_WRITEB, /* disk.all.write_bytes */ + PCP_DISK_ACTIVE, /* disk.all.avactive */ + PCP_NET_RECVB, /* network.all.in.bytes */ + PCP_NET_SENDB, /* network.all.out.bytes */ + PCP_NET_RECVP, /* network.all.in.packets */ + PCP_NET_SENDP, /* network.all.out.packets */ + PCP_PSI_CPUSOME, /* kernel.all.pressure.cpu.some.avg */ + PCP_PSI_IOSOME, /* kernel.all.pressure.io.some.avg */ + PCP_PSI_IOFULL, /* kernel.all.pressure.io.full.avg */ + PCP_PSI_MEMSOME, /* kernel.all.pressure.memory.some.avg */ + PCP_PSI_MEMFULL, /* kernel.all.pressure.memory.full.avg */ + PCP_ZFS_ARC_ANON_SIZE, /* zfs.arc.anon_size */ + PCP_ZFS_ARC_BONUS_SIZE, /* zfs.arc.bonus_size */ + PCP_ZFS_ARC_COMPRESSED_SIZE, /* zfs.arc.compressed_size */ + PCP_ZFS_ARC_UNCOMPRESSED_SIZE, /* zfs.arc.uncompressed_size */ + PCP_ZFS_ARC_C_MAX, /* zfs.arc.c_max */ + PCP_ZFS_ARC_DBUF_SIZE, /* zfs.arc.dbuf_size */ + PCP_ZFS_ARC_DNODE_SIZE, /* zfs.arc.dnode_size */ + PCP_ZFS_ARC_HDR_SIZE, /* zfs.arc.hdr_size */ + PCP_ZFS_ARC_MFU_SIZE, /* zfs.arc.mfu_size */ + PCP_ZFS_ARC_MRU_SIZE, /* zfs.arc.mru_size */ + PCP_ZFS_ARC_SIZE, /* zfs.arc.size */ + PCP_ZRAM_CAPACITY, /* zram.capacity */ + PCP_ZRAM_ORIGINAL, /* zram.mm_stat.data_size.original */ + PCP_ZRAM_COMPRESSED, /* zram.mm_stat.data_size.compressed */ + + PCP_PROC_PID, /* proc.psinfo.pid */ + PCP_PROC_PPID, /* proc.psinfo.ppid */ + PCP_PROC_TGID, /* proc.psinfo.tgid */ + PCP_PROC_PGRP, /* proc.psinfo.pgrp */ + PCP_PROC_SESSION, /* proc.psinfo.session */ + PCP_PROC_STATE, /* proc.psinfo.sname */ + PCP_PROC_TTY, /* proc.psinfo.tty */ + PCP_PROC_TTYPGRP, /* proc.psinfo.tty_pgrp */ + PCP_PROC_MINFLT, /* proc.psinfo.minflt */ + PCP_PROC_MAJFLT, /* proc.psinfo.maj_flt */ + PCP_PROC_CMINFLT, /* proc.psinfo.cmin_flt */ + PCP_PROC_CMAJFLT, /* proc.psinfo.cmaj_flt */ + PCP_PROC_UTIME, /* proc.psinfo.utime */ + PCP_PROC_STIME, /* proc.psinfo.stime */ + PCP_PROC_CUTIME, /* proc.psinfo.cutime */ + PCP_PROC_CSTIME, /* proc.psinfo.cstime */ + PCP_PROC_PRIORITY, /* proc.psinfo.priority */ + PCP_PROC_NICE, /* proc.psinfo.nice */ + PCP_PROC_THREADS, /* proc.psinfo.threads */ + PCP_PROC_STARTTIME, /* proc.psinfo.start_time */ + PCP_PROC_PROCESSOR, /* proc.psinfo.processor */ + PCP_PROC_CMD, /* proc.psinfo.cmd */ + PCP_PROC_PSARGS, /* proc.psinfo.psargs */ + PCP_PROC_CGROUPS, /* proc.psinfo.cgroups */ + PCP_PROC_OOMSCORE, /* proc.psinfo.oom_score */ + PCP_PROC_VCTXSW, /* proc.psinfo.vctxsw */ + PCP_PROC_NVCTXSW, /* proc.psinfo.nvctxsw */ + PCP_PROC_LABELS, /* proc.psinfo.labels */ + PCP_PROC_ENVIRON, /* proc.psinfo.environ */ + PCP_PROC_TTYNAME, /* proc.psinfo.ttyname */ + PCP_PROC_EXE, /* proc.psinfo.exe */ + PCP_PROC_CWD, /* proc.psinfo.cwd */ + + PCP_PROC_AUTOGROUP_ID, /* proc.autogroup.id */ + PCP_PROC_AUTOGROUP_NICE, /* proc.autogroup.nice */ + + PCP_PROC_ID_UID, /* proc.id.uid */ + PCP_PROC_ID_USER, /* proc.id.uid_nm */ + + PCP_PROC_IO_RCHAR, /* proc.io.rchar */ + PCP_PROC_IO_WCHAR, /* proc.io.wchar */ + PCP_PROC_IO_SYSCR, /* proc.io.syscr */ + PCP_PROC_IO_SYSCW, /* proc.io.syscw */ + PCP_PROC_IO_READB, /* proc.io.read_bytes */ + PCP_PROC_IO_WRITEB, /* proc.io.write_bytes */ + PCP_PROC_IO_CANCELLED, /* proc.io.cancelled_write_bytes */ + + PCP_PROC_MEM_SIZE, /* proc.memory.size */ + PCP_PROC_MEM_RSS, /* proc.memory.rss */ + PCP_PROC_MEM_SHARE, /* proc.memory.share */ + PCP_PROC_MEM_TEXTRS, /* proc.memory.textrss */ + PCP_PROC_MEM_LIBRS, /* proc.memory.librss */ + PCP_PROC_MEM_DATRS, /* proc.memory.datrss */ + PCP_PROC_MEM_DIRTY, /* proc.memory.dirty */ + + PCP_PROC_SMAPS_PSS, /* proc.smaps.pss */ + PCP_PROC_SMAPS_SWAP, /* proc.smaps.swap */ + PCP_PROC_SMAPS_SWAPPSS, /* proc.smaps.swappss */ + + PCP_METRIC_COUNT /* total metric count */ +} PCPMetric; + +void PCPMetric_enable(PCPMetric metric, bool enable); + +bool PCPMetric_enabled(PCPMetric metric); + +void PCPMetric_enableThreads(void); + +bool PCPMetric_fetch(struct timeval* timestamp); + +bool PCPMetric_iterate(PCPMetric metric, int* instp, int* offsetp); + +pmAtomValue* PCPMetric_values(PCPMetric metric, pmAtomValue* atom, int count, int type); + +const pmDesc* PCPMetric_desc(PCPMetric metric); + +int PCPMetric_type(PCPMetric metric); + +int PCPMetric_instanceCount(PCPMetric metric); + +int PCPMetric_instanceOffset(PCPMetric metric, int inst); + +pmAtomValue* PCPMetric_instance(PCPMetric metric, int inst, int offset, pmAtomValue* atom, int type); + +#endif diff --git a/pcp/PCPProcess.c b/pcp/PCPProcess.c index 5407a021..fa61506f 100644 --- a/pcp/PCPProcess.c +++ b/pcp/PCPProcess.c @@ -15,7 +15,6 @@ in the source distribution for its full text. #include "CRT.h" #include "Macros.h" -#include "Platform.h" #include "Process.h" #include "ProvideCurses.h" #include "RichString.h" diff --git a/pcp/PCPProcess.h b/pcp/PCPProcess.h index 3593255b..e8440971 100644 --- a/pcp/PCPProcess.h +++ b/pcp/PCPProcess.h @@ -17,8 +17,6 @@ in the source distribution for its full text. #include "Process.h" #include "Settings.h" -#include "pcp/Platform.h" - #define PROCESS_FLAG_LINUX_CGROUP 0x00000800 #define PROCESS_FLAG_LINUX_OOM 0x00001000 diff --git a/pcp/PCPProcessList.c b/pcp/PCPProcessList.c index 8e644b07..e4f9a5fd 100644 --- a/pcp/PCPProcessList.c +++ b/pcp/PCPProcessList.c @@ -11,6 +11,7 @@ in the source distribution for its full text. #include "pcp/PCPProcessList.h" +#include <assert.h> #include <limits.h> #include <math.h> #include <stdlib.h> @@ -24,12 +25,13 @@ in the source distribution for its full text. #include "Settings.h" #include "XUtils.h" +#include "pcp/PCPMetric.h" #include "pcp/PCPProcess.h" static void PCPProcessList_updateCPUcount(PCPProcessList* this) { ProcessList* pl = &(this->super); - pl->activeCPUs = Metric_instanceCount(PCP_PERCPU_SYSTEM); + pl->activeCPUs = PCPMetric_instanceCount(PCP_PERCPU_SYSTEM); unsigned int cpus = Platform_getMaxCPU(); if (cpus == pl->existingCPUs) return; @@ -54,7 +56,7 @@ static char* setUser(UsersTable* this, unsigned int uid, int pid, int offset) { return name; pmAtomValue value; - if (Metric_instance(PCP_PROC_ID_USER, pid, offset, &value, PM_TYPE_STRING)) { + if (PCPMetric_instance(PCP_PROC_ID_USER, pid, offset, &value, PM_TYPE_STRING)) { Hashtable_put(this->users, uid, value.cp); name = value.cp; } @@ -90,49 +92,49 @@ void ProcessList_delete(ProcessList* pl) { static inline long Metric_instance_s32(int metric, int pid, int offset, long fallback) { pmAtomValue value; - if (Metric_instance(metric, pid, offset, &value, PM_TYPE_32)) + if (PCPMetric_instance(metric, pid, offset, &value, PM_TYPE_32)) return value.l; return fallback; } static inline long long Metric_instance_s64(int metric, int pid, int offset, long long fallback) { pmAtomValue value; - if (Metric_instance(metric, pid, offset, &value, PM_TYPE_64)) + if (PCPMetric_instance(metric, pid, offset, &value, PM_TYPE_64)) return value.l; return fallback; } static inline unsigned long Metric_instance_u32(int metric, int pid, int offset, unsigned long fallback) { pmAtomValue value; - if (Metric_instance(metric, pid, offset, &value, PM_TYPE_U32)) + if (PCPMetric_instance(metric, pid, offset, &value, PM_TYPE_U32)) return value.ul; return fallback; } static inline unsigned long long Metric_instance_u64(int metric, int pid, int offset, unsigned long long fallback) { pmAtomValue value; - if (Metric_instance(metric, pid, offset, &value, PM_TYPE_U64)) + if (PCPMetric_instance(metric, pid, offset, &value, PM_TYPE_U64)) return value.ull; return fallback; } static inline unsigned long long Metric_instance_time(int metric, int pid, int offset) { pmAtomValue value; - if (Metric_instance(metric, pid, offset, &value, PM_TYPE_U64)) + if (PCPMetric_instance(metric, pid, offset, &value, PM_TYPE_U64)) return value.ull / 10; return 0; } static inline unsigned long long Metric_instance_ONE_K(int metric, int pid, int offset) { pmAtomValue value; - if (Metric_instance(metric, pid, offset, &value, PM_TYPE_U64)) + if (PCPMetric_instance(metric, pid, offset, &value, PM_TYPE_U64)) return value.ull / ONE_K; return ULLONG_MAX; } static inline char Metric_instance_char(int metric, int pid, int offset, char fallback) { pmAtomValue value; - if (Metric_instance(metric, pid, offset, &value, PM_TYPE_STRING)) { + if (PCPMetric_instance(metric, pid, offset, &value, PM_TYPE_STRING)) { char uchar = value.cp[0]; free(value.cp); return uchar; @@ -150,7 +152,7 @@ static void PCPProcessList_updateInfo(Process* process, int pid, int offset, cha PCPProcess* pp = (PCPProcess*) process; pmAtomValue value; - if (!Metric_instance(PCP_PROC_CMD, pid, offset, &value, PM_TYPE_STRING)) + if (!PCPMetric_instance(PCP_PROC_CMD, pid, offset, &value, PM_TYPE_STRING)) value.cp = xStrdup("<unknown>"); String_safeStrncpy(command, value.cp, commLen); free(value.cp); @@ -185,7 +187,7 @@ static void PCPProcessList_updateIO(PCPProcess* pp, int pid, int offset, unsigne pp->io_syscw = Metric_instance_u64(PCP_PROC_IO_SYSCW, pid, offset, ULLONG_MAX); pp->io_cancelled_write_bytes = Metric_instance_ONE_K(PCP_PROC_IO_CANCELLED, pid, offset); - if (Metric_instance(PCP_PROC_IO_READB, pid, offset, &value, PM_TYPE_U64)) { + if (PCPMetric_instance(PCP_PROC_IO_READB, pid, offset, &value, PM_TYPE_U64)) { unsigned long long last_read = pp->io_read_bytes; pp->io_read_bytes = value.ull / ONE_K; pp->io_rate_read_bps = ONE_K * (pp->io_read_bytes - last_read) / @@ -195,7 +197,7 @@ static void PCPProcessList_updateIO(PCPProcess* pp, int pid, int offset, unsigne pp->io_rate_read_bps = NAN; } - if (Metric_instance(PCP_PROC_IO_WRITEB, pid, offset, &value, PM_TYPE_U64)) { + if (PCPMetric_instance(PCP_PROC_IO_WRITEB, pid, offset, &value, PM_TYPE_U64)) { unsigned long long last_write = pp->io_write_bytes; pp->io_write_bytes = value.ull; pp->io_rate_write_bps = ONE_K * (pp->io_write_bytes - last_write) / @@ -237,20 +239,20 @@ static void PCPProcessList_readCtxtData(PCPProcess* pp, int pid, int offset) { pmAtomValue value; unsigned long ctxt = 0; - if (Metric_instance(PCP_PROC_VCTXSW, pid, offset, &value, PM_TYPE_U32)) + if (PCPMetric_instance(PCP_PROC_VCTXSW, pid, offset, &value, PM_TYPE_U32)) ctxt += value.ul; - if (Metric_instance(PCP_PROC_NVCTXSW, pid, offset, &value, PM_TYPE_U32)) + if (PCPMetric_instance(PCP_PROC_NVCTXSW, pid, offset, &value, PM_TYPE_U32)) ctxt += value.ul; pp->ctxt_diff = ctxt > pp->ctxt_total ? ctxt - pp->ctxt_total : 0; pp->ctxt_total = ctxt; } -static char* setString(Metric metric, int pid, int offset, char* string) { +static char* setString(PCPMetric metric, int pid, int offset, char* string) { if (string) free(string); pmAtomValue value; - if (Metric_instance(metric, pid, offset, &value, PM_TYPE_STRING)) + if (PCPMetric_instance(metric, pid, offset, &value, PM_TYPE_STRING)) string = value.cp; else string = NULL; @@ -280,7 +282,7 @@ static void PCPProcessList_updateUsername(Process* process, int pid, int offset, static void PCPProcessList_updateCmdline(Process* process, int pid, int offset, const char* comm) { pmAtomValue value; - if (!Metric_instance(PCP_PROC_PSARGS, pid, offset, &value, PM_TYPE_STRING)) { + if (!PCPMetric_instance(PCP_PROC_PSARGS, pid, offset, &value, PM_TYPE_STRING)) { if (process->state != 'Z') process->isKernelThread = true; Process_updateCmdline(process, NULL, 0, 0); @@ -314,7 +316,7 @@ static void PCPProcessList_updateCmdline(Process* process, int pid, int offset, Process_updateComm(process, comm); - if (Metric_instance(PCP_PROC_EXE, pid, offset, &value, PM_TYPE_STRING)) { + if (PCPMetric_instance(PCP_PROC_EXE, pid, offset, &value, PM_TYPE_STRING)) { Process_updateExe(process, value.cp); free(value.cp); } @@ -331,7 +333,7 @@ static bool PCPProcessList_updateProcesses(PCPProcessList* this, double period, int pid = -1, offset = -1; /* for every process ... */ - while (Metric_iterate(PCP_PROC_PID, &pid, &offset)) { + while (PCPMetric_iterate(PCP_PROC_PID, &pid, &offset)) { bool preExisting; Process* proc = ProcessList_getProcess(pl, pid, &preExisting, PCPProcess_new); @@ -372,7 +374,7 @@ static bool PCPProcessList_updateProcesses(PCPProcessList* this, double period, if ((settings->flags & PROCESS_FLAG_LINUX_SMAPS) && (Process_isKernelThread(proc) == false)) { - if (Metric_enabled(PCP_PROC_SMAPS_PSS)) + if (PCPMetric_enabled(PCP_PROC_SMAPS_PSS)) PCPProcessList_updateSmaps(pp, pid, offset); } @@ -452,30 +454,30 @@ static void PCPProcessList_updateMemoryInfo(ProcessList* super) { super->usedSwap = super->totalSwap = super->sharedMem = 0; pmAtomValue value; - if (Metric_values(PCP_MEM_TOTAL, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_TOTAL, &value, 1, PM_TYPE_U64) != NULL) super->totalMem = value.ull; - if (Metric_values(PCP_MEM_FREE, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_FREE, &value, 1, PM_TYPE_U64) != NULL) freeMem = value.ull; - if (Metric_values(PCP_MEM_BUFFERS, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_BUFFERS, &value, 1, PM_TYPE_U64) != NULL) super->buffersMem = value.ull; - if (Metric_values(PCP_MEM_SRECLAIM, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_SRECLAIM, &value, 1, PM_TYPE_U64) != NULL) sreclaimableMem = value.ull; - if (Metric_values(PCP_MEM_SHARED, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_SHARED, &value, 1, PM_TYPE_U64) != NULL) super->sharedMem = value.ull; - if (Metric_values(PCP_MEM_CACHED, &value, 1, PM_TYPE_U64) != NULL) + 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; - if (Metric_values(PCP_MEM_AVAILABLE, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_AVAILABLE, &value, 1, PM_TYPE_U64) != NULL) super->availableMem = MINIMUM(value.ull, super->totalMem); else super->availableMem = freeMem; - if (Metric_values(PCP_MEM_SWAPFREE, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_SWAPFREE, &value, 1, PM_TYPE_U64) != NULL) swapFreeMem = value.ull; - if (Metric_values(PCP_MEM_SWAPTOTAL, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_SWAPTOTAL, &value, 1, PM_TYPE_U64) != NULL) super->totalSwap = value.ull; - if (Metric_values(PCP_MEM_SWAPCACHED, &value, 1, PM_TYPE_U64) != NULL) + if (PCPMetric_values(PCP_MEM_SWAPCACHED, &value, 1, PM_TYPE_U64) != NULL) super->cachedSwap = value.ull; super->usedSwap = super->totalSwap - swapFreeMem - super->cachedSwap; } @@ -547,26 +549,26 @@ static void PCPProcessList_deriveCPUTime(pmAtomValue* values) { PCPProcessList_saveCPUTimePeriod(values, CPU_TOTAL_PERIOD, totaltime); } -static void PCPProcessList_updateAllCPUTime(PCPProcessList* this, Metric metric, CPUMetric cpumetric) +static void PCPProcessList_updateAllCPUTime(PCPProcessList* this, PCPMetric metric, CPUMetric cpumetric) { pmAtomValue* value = &this->cpu[cpumetric]; - if (Metric_values(metric, value, 1, PM_TYPE_U64) == NULL) + if (PCPMetric_values(metric, value, 1, PM_TYPE_U64) == NULL) memset(&value, 0, sizeof(pmAtomValue)); } -static void PCPProcessList_updatePerCPUTime(PCPProcessList* this, Metric metric, CPUMetric cpumetric) +static void PCPProcessList_updatePerCPUTime(PCPProcessList* this, PCPMetric metric, CPUMetric cpumetric) { int cpus = this->super.existingCPUs; - if (Metric_values(metric, this->values, cpus, PM_TYPE_U64) == NULL) + 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++) this->percpu[i][cpumetric].ull = this->values[i].ull; } -static void PCPProcessList_updatePerCPUReal(PCPProcessList* this, Metric metric, CPUMetric cpumetric) +static void PCPProcessList_updatePerCPUReal(PCPProcessList* this, PCPMetric metric, CPUMetric cpumetric) { int cpus = this->super.existingCPUs; - if (Metric_values(metric, this->values, cpus, PM_TYPE_DOUBLE) == NULL) + 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++) this->percpu[i][cpumetric].d = this->values[i].d; @@ -579,27 +581,27 @@ static inline void PCPProcessList_scanZfsArcstats(PCPProcessList* this) { pmAtomValue value; memset(&this->zfs, 0, sizeof(ZfsArcStats)); - if (Metric_values(PCP_ZFS_ARC_ANON_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_ANON_SIZE, &value, 1, PM_TYPE_U64)) this->zfs.anon = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_C_MAX, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_C_MAX, &value, 1, PM_TYPE_U64)) this->zfs.max = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_BONUS_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_BONUS_SIZE, &value, 1, PM_TYPE_U64)) bonusSize = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_DBUF_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_DBUF_SIZE, &value, 1, PM_TYPE_U64)) dbufSize = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_DNODE_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_DNODE_SIZE, &value, 1, PM_TYPE_U64)) dnodeSize = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_COMPRESSED_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_COMPRESSED_SIZE, &value, 1, PM_TYPE_U64)) this->zfs.compressed = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_UNCOMPRESSED_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_UNCOMPRESSED_SIZE, &value, 1, PM_TYPE_U64)) this->zfs.uncompressed = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_HDR_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_HDR_SIZE, &value, 1, PM_TYPE_U64)) this->zfs.header = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_MFU_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_MFU_SIZE, &value, 1, PM_TYPE_U64)) this->zfs.MFU = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_MRU_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_MRU_SIZE, &value, 1, PM_TYPE_U64)) this->zfs.MRU = value.ull / ONE_K; - if (Metric_values(PCP_ZFS_ARC_SIZE, &value, 1, PM_TYPE_U64)) + if (PCPMetric_values(PCP_ZFS_ARC_SIZE, &value, 1, PM_TYPE_U64)) this->zfs.size = value.ull / ONE_K; this->zfs.other = (dbufSize + dnodeSize + bonusSize) / ONE_K; @@ -651,34 +653,34 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { bool enabled = !pauseProcessUpdate; bool flagged = settings->showCPUFrequency; - Metric_enabl |