summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2021-09-03 12:44:19 +1000
committerBenBE <BenBE@geshi.org>2021-09-05 18:47:07 +0200
commit556d7c03e8876eb440c9033ec9e9c533349deaf8 (patch)
tree466351a6e8fb8ac8c3e32aae15c060c5b4c22d69
parent0925c54caadbdf55c34a06590c62dfa1cfa68a93 (diff)
Add a Process_printPercentage helper routine
Replace several open-coded variants of percentage formatting. This function has been ported from Hishams old 'next' branch.
-rw-r--r--Process.c47
-rw-r--r--Process.h2
-rw-r--r--linux/LinuxProcess.c24
3 files changed, 29 insertions, 44 deletions
diff --git a/Process.c b/Process.c
index 924580d0..475d23d5 100644
--- a/Process.c
+++ b/Process.c
@@ -707,6 +707,24 @@ void Process_printLeftAlignedField(RichString* str, int attr, const char* conten
RichString_appendChr(str, attr, ' ', width + 1 - columns);
}
+void Process_printPercentage(float val, char* buffer, int n, int* attr) {
+ if (val >= 0) {
+ if (val < 99.9F) {
+ if (val < 0.05F) {
+ *attr = CRT_colors[PROCESS_SHADOW];
+ }
+ xSnprintf(buffer, n, "%4.1f ", val);
+ } else if (val < 999) {
+ xSnprintf(buffer, n, "%3d. ", (int)val);
+ } else {
+ xSnprintf(buffer, n, "%4d ", (int)val);
+ }
+ } else {
+ *attr = CRT_colors[PROCESS_SHADOW];
+ xSnprintf(buffer, n, " N/A ");
+ }
+}
+
void Process_writeField(const Process* this, RichString* str, ProcessField field) {
char buffer[256];
size_t n = sizeof(buffer);
@@ -821,34 +839,13 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
xSnprintf(buffer, n, "%4ld ", this->nlwp);
break;
- case PERCENT_CPU:
+ case PERCENT_CPU: Process_printPercentage(this->percent_cpu, buffer, n, &attr); break;
case PERCENT_NORM_CPU: {
- float cpuPercentage = this->percent_cpu;
- if (field == PERCENT_NORM_CPU) {
- cpuPercentage /= this->processList->activeCPUs;
- }
- if (cpuPercentage > 999.9F) {
- xSnprintf(buffer, n, "%4u ", (unsigned int)cpuPercentage);
- } else if (cpuPercentage > 99.9F) {
- xSnprintf(buffer, n, "%3u. ", (unsigned int)cpuPercentage);
- } else {
- if (cpuPercentage < 0.05F)
- attr = CRT_colors[PROCESS_SHADOW];
-
- xSnprintf(buffer, n, "%4.1f ", cpuPercentage);
- }
+ float cpuPercentage = this->percent_cpu / this->processList->activeCPUs;
+ Process_printPercentage(cpuPercentage, buffer, n, &attr);
break;
}
- case PERCENT_MEM:
- if (this->percent_mem > 99.9F) {
- xSnprintf(buffer, n, "100. ");
- } else {
- if (this->percent_mem < 0.05F)
- attr = CRT_colors[PROCESS_SHADOW];
-
- xSnprintf(buffer, n, "%4.1f ", this->percent_mem);
- }
- break;
+ case PERCENT_MEM: Process_printPercentage(this->percent_mem, buffer, n, &attr); break;
case PGRP: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pgrp); break;
case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pid); break;
case PPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->ppid); break;
diff --git a/Process.h b/Process.h
index c652fae9..1ba36de3 100644
--- a/Process.h
+++ b/Process.h
@@ -356,6 +356,8 @@ void Process_fillStarttimeBuffer(Process* this);
void Process_printLeftAlignedField(RichString* str, int attr, const char* content, unsigned int width);
+void Process_printPercentage(float val, char* buffer, int n, int* attr);
+
void Process_display(const Object* cast, RichString* out);
void Process_done(Process* this);
diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c
index 9434a1ed..75638e49 100644
--- a/linux/LinuxProcess.c
+++ b/linux/LinuxProcess.c
@@ -85,9 +85,9 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
[OOM] = { .name = "OOM", .title = " OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = PROCESS_FLAG_LINUX_OOM, .defaultSortDesc = true, },
[IO_PRIORITY] = { .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_LINUX_IOPRIO, },
#ifdef HAVE_DELAYACCT
- [PERCENT_CPU_DELAY] = { .name = "PERCENT_CPU_DELAY", .title = "CPUD% ", .description = "CPU delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
+ [PERCENT_CPU_DELAY] = { .name = "PERCENT_CPU_DELAY", .title = "CPUD%", .description = "CPU delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
[PERCENT_IO_DELAY] = { .name = "PERCENT_IO_DELAY", .title = "IOD% ", .description = "Block I/O delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
- [PERCENT_SWAP_DELAY] = { .name = "PERCENT_SWAP_DELAY", .title = "SWAPD% ", .description = "Swapin delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
+ [PERCENT_SWAP_DELAY] = { .name = "PERCENT_SWAP_DELAY", .title = "SWPD%", .description = "Swapin delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
#endif
[M_PSS] = { .name = "M_PSS", .title = " PSS ", .description = "proportional set size, same as M_RESIDENT but each page is divided by the number of processes sharing it", .flags = PROCESS_FLAG_LINUX_SMAPS, .defaultSortDesc = true, },
[M_SWAP] = { .name = "M_SWAP", .title = " SWAP ", .description = "Size of the process's swapped pages", .flags = PROCESS_FLAG_LINUX_SMAPS, .defaultSortDesc = true, },
@@ -189,20 +189,6 @@ bool LinuxProcess_changeAutogroupPriorityBy(Process* this, Arg delta) {
return success;
}
-#ifdef HAVE_DELAYACCT
-static void LinuxProcess_printDelay(float delay_percent, char* buffer, int n, int* attr) {
- if (isnan(delay_percent)) {
- xSnprintf(buffer, n, " N/A ");
- *attr = CRT_colors[PROCESS_SHADOW];
- } else {
- xSnprintf(buffer, n, "%4.1f ", delay_percent);
- if (delay_percent < 0.05) {
- *attr = CRT_colors[PROCESS_SHADOW];
- }
- }
-}
-#endif
-
static void LinuxProcess_writeField(const Process* this, RichString* str, ProcessField field) {
const LinuxProcess* lp = (const LinuxProcess*) this;
bool coloring = this->settings->highlightMegabytes;
@@ -281,9 +267,9 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces
break;
}
#ifdef HAVE_DELAYACCT
- case PERCENT_CPU_DELAY: LinuxProcess_printDelay(lp->cpu_delay_percent, buffer, n, &attr); break;
- case PERCENT_IO_DELAY: LinuxProcess_printDelay(lp->blkio_delay_percent, buffer, n, &attr); break;
- case PERCENT_SWAP_DELAY: LinuxProcess_printDelay(lp->swapin_delay_percent, buffer, n, &attr); break;
+ case PERCENT_CPU_DELAY: Process_printPercentage(lp->cpu_delay_percent, buffer, n, &attr); break;
+ case PERCENT_IO_DELAY: Process_printPercentage(lp->blkio_delay_percent, buffer, n, &attr); break;
+ case PERCENT_SWAP_DELAY: Process_printPercentage(lp->swapin_delay_percent, buffer, n, &attr); break;
#endif
case CTXT:
if (lp->ctxt_diff > 1000) {

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