Skip to content

Commit 556d7c0

Browse files
hishamhmBenBE
authored andcommitted
Add a Process_printPercentage helper routine
Replace several open-coded variants of percentage formatting. This function has been ported from Hishams old 'next' branch.
1 parent 0925c54 commit 556d7c0

File tree

3 files changed

+29
-44
lines changed

3 files changed

+29
-44
lines changed

Process.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,24 @@ void Process_printLeftAlignedField(RichString* str, int attr, const char* conten
707707
RichString_appendChr(str, attr, ' ', width + 1 - columns);
708708
}
709709

710+
void Process_printPercentage(float val, char* buffer, int n, int* attr) {
711+
if (val >= 0) {
712+
if (val < 99.9F) {
713+
if (val < 0.05F) {
714+
*attr = CRT_colors[PROCESS_SHADOW];
715+
}
716+
xSnprintf(buffer, n, "%4.1f ", val);
717+
} else if (val < 999) {
718+
xSnprintf(buffer, n, "%3d. ", (int)val);
719+
} else {
720+
xSnprintf(buffer, n, "%4d ", (int)val);
721+
}
722+
} else {
723+
*attr = CRT_colors[PROCESS_SHADOW];
724+
xSnprintf(buffer, n, " N/A ");
725+
}
726+
}
727+
710728
void Process_writeField(const Process* this, RichString* str, ProcessField field) {
711729
char buffer[256];
712730
size_t n = sizeof(buffer);
@@ -821,34 +839,13 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field
821839

822840
xSnprintf(buffer, n, "%4ld ", this->nlwp);
823841
break;
824-
case PERCENT_CPU:
842+
case PERCENT_CPU: Process_printPercentage(this->percent_cpu, buffer, n, &attr); break;
825843
case PERCENT_NORM_CPU: {
826-
float cpuPercentage = this->percent_cpu;
827-
if (field == PERCENT_NORM_CPU) {
828-
cpuPercentage /= this->processList->activeCPUs;
829-
}
830-
if (cpuPercentage > 999.9F) {
831-
xSnprintf(buffer, n, "%4u ", (unsigned int)cpuPercentage);
832-
} else if (cpuPercentage > 99.9F) {
833-
xSnprintf(buffer, n, "%3u. ", (unsigned int)cpuPercentage);
834-
} else {
835-
if (cpuPercentage < 0.05F)
836-
attr = CRT_colors[PROCESS_SHADOW];
837-
838-
xSnprintf(buffer, n, "%4.1f ", cpuPercentage);
839-
}
844+
float cpuPercentage = this->percent_cpu / this->processList->activeCPUs;
845+
Process_printPercentage(cpuPercentage, buffer, n, &attr);
840846
break;
841847
}
842-
case PERCENT_MEM:
843-
if (this->percent_mem > 99.9F) {
844-
xSnprintf(buffer, n, "100. ");
845-
} else {
846-
if (this->percent_mem < 0.05F)
847-
attr = CRT_colors[PROCESS_SHADOW];
848-
849-
xSnprintf(buffer, n, "%4.1f ", this->percent_mem);
850-
}
851-
break;
848+
case PERCENT_MEM: Process_printPercentage(this->percent_mem, buffer, n, &attr); break;
852849
case PGRP: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pgrp); break;
853850
case PID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->pid); break;
854851
case PPID: xSnprintf(buffer, n, "%*d ", Process_pidDigits, this->ppid); break;

Process.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ void Process_fillStarttimeBuffer(Process* this);
356356

357357
void Process_printLeftAlignedField(RichString* str, int attr, const char* content, unsigned int width);
358358

359+
void Process_printPercentage(float val, char* buffer, int n, int* attr);
360+
359361
void Process_display(const Object* cast, RichString* out);
360362

361363
void Process_done(Process* this);

linux/LinuxProcess.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = {
8585
[OOM] = { .name = "OOM", .title = " OOM ", .description = "OOM (Out-of-Memory) killer score", .flags = PROCESS_FLAG_LINUX_OOM, .defaultSortDesc = true, },
8686
[IO_PRIORITY] = { .name = "IO_PRIORITY", .title = "IO ", .description = "I/O priority", .flags = PROCESS_FLAG_LINUX_IOPRIO, },
8787
#ifdef HAVE_DELAYACCT
88-
[PERCENT_CPU_DELAY] = { .name = "PERCENT_CPU_DELAY", .title = "CPUD% ", .description = "CPU delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
88+
[PERCENT_CPU_DELAY] = { .name = "PERCENT_CPU_DELAY", .title = "CPUD%", .description = "CPU delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
8989
[PERCENT_IO_DELAY] = { .name = "PERCENT_IO_DELAY", .title = "IOD% ", .description = "Block I/O delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
90-
[PERCENT_SWAP_DELAY] = { .name = "PERCENT_SWAP_DELAY", .title = "SWAPD% ", .description = "Swapin delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
90+
[PERCENT_SWAP_DELAY] = { .name = "PERCENT_SWAP_DELAY", .title = "SWPD%", .description = "Swapin delay %", .flags = PROCESS_FLAG_LINUX_DELAYACCT, .defaultSortDesc = true, },
9191
#endif
9292
[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, },
9393
[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) {
189189
return success;
190190
}
191191

192-
#ifdef HAVE_DELAYACCT
193-
static void LinuxProcess_printDelay(float delay_percent, char* buffer, int n, int* attr) {
194-
if (isnan(delay_percent)) {
195-
xSnprintf(buffer, n, " N/A ");
196-
*attr = CRT_colors[PROCESS_SHADOW];
197-
} else {
198-
xSnprintf(buffer, n, "%4.1f ", delay_percent);
199-
if (delay_percent < 0.05) {
200-
*attr = CRT_colors[PROCESS_SHADOW];
201-
}
202-
}
203-
}
204-
#endif
205-
206192
static void LinuxProcess_writeField(const Process* this, RichString* str, ProcessField field) {
207193
const LinuxProcess* lp = (const LinuxProcess*) this;
208194
bool coloring = this->settings->highlightMegabytes;
@@ -281,9 +267,9 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces
281267
break;
282268
}
283269
#ifdef HAVE_DELAYACCT
284-
case PERCENT_CPU_DELAY: LinuxProcess_printDelay(lp->cpu_delay_percent, buffer, n, &attr); break;
285-
case PERCENT_IO_DELAY: LinuxProcess_printDelay(lp->blkio_delay_percent, buffer, n, &attr); break;
286-
case PERCENT_SWAP_DELAY: LinuxProcess_printDelay(lp->swapin_delay_percent, buffer, n, &attr); break;
270+
case PERCENT_CPU_DELAY: Process_printPercentage(lp->cpu_delay_percent, buffer, n, &attr); break;
271+
case PERCENT_IO_DELAY: Process_printPercentage(lp->blkio_delay_percent, buffer, n, &attr); break;
272+
case PERCENT_SWAP_DELAY: Process_printPercentage(lp->swapin_delay_percent, buffer, n, &attr); break;
287273
#endif
288274
case CTXT:
289275
if (lp->ctxt_diff > 1000) {

0 commit comments

Comments
 (0)