]> granicus.if.org Git - procps-ng/commitdiff
ps: exploit the library addition for 'cpu utilization'
authorJim Warner <james.warner@comcast.net>
Fri, 4 Mar 2022 06:00:00 +0000 (00:00 -0600)
committerCraig Small <csmall@dropbear.xyz>
Sun, 6 Mar 2022 03:52:34 +0000 (14:52 +1100)
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 <james.warner@comcast.net>
ps/common.h
ps/global.c
ps/output.c
ps/ps.1

index 9395590dfbd20067493fdbff67e8bcbb8d4c9113..4d7e34cd47cd8a040e2b69a7c434eee1ec6fa1b2 100644 (file)
@@ -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)
index 09a1b2650450c0762ef71a39adbaec22e7f63d4b..82ced68a210b0cca37765f3ea9f9a0f2497ff874 100644 (file)
@@ -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)
index fd2caf62a7949f030b847a44740d2d2f63170acd..de5daf701e043e1035c14ef05a3fe2762e95999b 100644 (file)
@@ -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 ab02b0adb9c7b2a27957a980d0882c6db5f2919e..a6275fd379144b79c075b5afb2792479bad1b192 100644 (file)
--- 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.