]> granicus.if.org Git - procps-ng/commitdiff
library: add <pids> TIME_ALL/TIME_ELAPSED needed by ps
authorJim Warner <james.warner@comcast.net>
Wed, 30 Sep 2015 05:00:00 +0000 (00:00 -0500)
committerCraig Small <csmall@enc.com.au>
Mon, 5 Oct 2015 10:42:07 +0000 (21:42 +1100)
Shoot, here's yet another bow to ps needs. But it's ok
because it makes a lot of sense. Rather than force all
users into their own calculations do but it once here.

As an aside this need arose during ps testing when the
sorts were using TIME_START or TICS_ALL. That was just
fine for almost every need except 'etime' plus 'time'.

That 'etime' was sorting the opposite of what's wanted
when using TIME_START (of course) while 'time' yielded
some weird ordering because TICS_ALL was too granular.

Signed-off-by: Jim Warner <james.warner@comcast.net>
proc/pids.c
proc/pids.h

index 9d0a802118abef8d9ef87d9bad39d2ec1cb34235..015656fabed213b441f7dcc6e88398498026b318 100644 (file)
@@ -36,6 +36,8 @@
 #include <sys/types.h>
 
 #include <proc/pids.h>
+#include <proc/sysinfo.h>
+#include <proc/uptime.h>
 #include "procps-private.h"
 
 #include "devname.h"                   // and a few headers for our
@@ -81,6 +83,8 @@ struct procps_pidsinfo {
     PROCTAB *PT;                       // the old library essential interface
     struct pids_counts counts;         // counts for 'procps_pids_stacks_fill'
     struct pids_reap reaped;           // counts + stacks for 'procps_pids_reap'
+    unsigned long hertz;               // for TIME_ALL & TIME_ELAPSED calculations
+    unsigned long long boot_seconds;   // for TIME_ELAPSED calculation
 };
 
 
@@ -207,6 +211,9 @@ REG_set(TICS_SYSTEM,      ull_int, stime)
 REG_set(TICS_SYSTEM_C,    ull_int, cstime)
 REG_set(TICS_USER,        ull_int, utime)
 REG_set(TICS_USER_C,      ull_int, cutime)
+setDECL(TIME_ALL)     { R->result.ull_int = (P->utime + P->stime) / I->hertz; }
+setDECL(TIME_ELAPSED) { R->result.ull_int = (I->boot_seconds >= (P->start_time / I->hertz)) ? I->boot_seconds - (P->start_time / I->hertz) : 0; }
+
 REG_set(TIME_START,       ull_int, start_time)
 REG_set(TTY,              s_int,   tty)
 setDECL(TTY_NAME)     { char buf[64]; (void)I; dev_to_tty(buf, sizeof(buf), P->tty, P->tid, ABBREV_DEV); R->result.str = strdup(buf); }
@@ -454,6 +461,8 @@ static struct {
     { RS(TICS_SYSTEM_C),     f_stat,     NULL,      QS(ull_int),  0       },
     { RS(TICS_USER),         f_stat,     NULL,      QS(ull_int),  0       },
     { RS(TICS_USER_C),       f_stat,     NULL,      QS(ull_int),  0       },
+    { RS(TIME_ALL),          f_stat,     NULL,      QS(ull_int),  0       },
+    { RS(TIME_ELAPSED),      f_stat,     NULL,      QS(ull_int),  0       },
     { RS(TIME_START),        f_stat,     NULL,      QS(ull_int),  0       },
     { RS(TTY),               f_stat,     NULL,      QS(s_int),    0       },
     { RS(TTY_NAME),          f_stat,     FF(str),   QS(strvers),  0       },
@@ -1011,6 +1020,7 @@ PROCPS_EXPORT int procps_pids_new (
         enum pids_item *items)
 {
     struct procps_pidsinfo *p;
+    double uptime_secs;
     int pgsz;
 
     if (info == NULL || *info != NULL)
@@ -1041,6 +1051,10 @@ PROCPS_EXPORT int procps_pids_new (
 
     config_history(p);
 
+    p->hertz = procps_hertz_get();
+    procps_uptime(&uptime_secs, NULL);
+    p->boot_seconds = (unsigned long)uptime_secs;
+
     p->refcount = 1;
     *info = p;
     return 0;
index 55be7f836976aa9222e4ef74bdd96c7ca30be725..3aec9b23c32e0c29c48972afcd6686156d0cb4c4 100644 (file)
@@ -120,6 +120,8 @@ enum pids_item {
     PROCPS_PIDS_TICS_SYSTEM_C,         // ull_int
     PROCPS_PIDS_TICS_USER,             // ull_int
     PROCPS_PIDS_TICS_USER_C,           // ull_int
+    PROCPS_PIDS_TIME_ALL,              // ull_int
+    PROCPS_PIDS_TIME_ELAPSED,          // ull_int
     PROCPS_PIDS_TIME_START,            // ull_int
     PROCPS_PIDS_TTY,                   // s_int
     PROCPS_PIDS_TTY_NAME,              // str