From 2ac72e2e801e015532b555ae0f2272c8ecef2c8b Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Fri, 4 Mar 2022 00:00:00 -0600 Subject: [PATCH] ps: exploit the library addition for 'cpu utilization' This just parallels the top program by adding that new library PIDS_UTILIZATION item to the ps repertoire. It should be noted, however, that the new %CUU field is a little redundant. I mean, ps already has 4 such fields implemented identified as: '%cpu', 'c', 'cp' & 'pcpu'. Oh well, at least the newest one offers a little value added in the form of extra precision. We'll follow the top lead and display results in the form of: '##.###'. Signed-off-by: Jim Warner --- ps/common.h | 1 + ps/global.c | 1 + ps/output.c | 12 ++++++++++++ ps/ps.1 | 9 +++++++++ 4 files changed, 23 insertions(+) diff --git a/ps/common.h b/ps/common.h index 9395590d..4d7e34cd 100644 --- a/ps/common.h +++ b/ps/common.h @@ -159,6 +159,7 @@ makEXT(TICS_BEGAN) makEXT(TTY) makEXT(TTY_NAME) makEXT(TTY_NUMBER) +makEXT(UTILIZATION) makEXT(VM_DATA) makEXT(VM_RSS_LOCKED) makEXT(VM_RSS) diff --git a/ps/global.c b/ps/global.c index 09a1b265..82ced68a 100644 --- a/ps/global.c +++ b/ps/global.c @@ -145,6 +145,7 @@ makREL(TICS_BEGAN) makREL(TTY) makREL(TTY_NAME) makREL(TTY_NUMBER) +makREL(UTILIZATION) makREL(VM_DATA) makREL(VM_RSS_LOCKED) makREL(VM_RSS) diff --git a/ps/output.c b/ps/output.c index fd2caf62..de5daf70 100644 --- a/ps/output.c +++ b/ps/output.c @@ -1339,6 +1339,17 @@ setREL1(EXE) return max_rightward-rightward; } +/* %cpu utilization over task lifetime */ +static int pr_utilization(char *restrict const outbuf, const proc_t *restrict const pp){ +double cu; +setREL1(UTILIZATION) + cu = rSv(UTILIZATION, real, pp); + /* this check is really just for us (the ps program) since we will be very + short lived and the library might reflect 100% or even more utilization */ + if (cu > 99.0) cu = 99.999; + return snprintf(outbuf, COLWID, "%#.3f", cu); +} + /************************* Systemd stuff ********************************/ static int pr_sd_unit(char *restrict const outbuf, const proc_t *restrict const pp){ setREL1(SD_UNIT) @@ -1626,6 +1637,7 @@ static const format_struct format_array[] = { /* {"ctid", "CTID", pr_nop, PIDS_noop, 5, SUN, ET|RIGHT}, // resource contracts? {"cursig", "CURSIG", pr_nop, PIDS_noop, 6, DEC, AN|RIGHT}, {"cutime", "-", pr_nop, PIDS_TICS_USER_C, 1, LNX, AN|RIGHT}, +{"cuu", "%CUU", pr_utilization, PIDS_UTILIZATION, 6, XXX, AN|RIGHT}, {"cwd", "CWD", pr_nop, PIDS_noop, 3, LNX, AN|LEFT}, {"drs", "DRS", pr_drs, PIDS_VSIZE_PGS, 5, LNX, PO|RIGHT}, {"dsiz", "DSIZ", pr_dsiz, PIDS_VSIZE_PGS, 4, LNX, PO|RIGHT}, diff --git a/ps/ps.1 b/ps/ps.1 index ab02b0ad..a6275fd3 100644 --- a/ps/ps.1 +++ b/ps/ps.1 @@ -1211,6 +1211,15 @@ cumulative CPU time in seconds (alias .BR times ). T} +cuu %CUU T{ +The CPU utilization of a process in an extended "##.###" format. +(see also +.BR %cpu , +.BR c , +.BR cp , +.BR pcpu ). +T} + drs DRS T{ data resident set size, the amount of physical memory devoted to other than executable code.