]> granicus.if.org Git - sysstat/commitdiff
pidstat: Display values in human readable format
authorSebastien GODARD <sysstat@users.noreply.github.com>
Thu, 17 Nov 2016 10:43:26 +0000 (11:43 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Thu, 17 Nov 2016 10:43:26 +0000 (11:43 +0100)
Add new "--human" option.
Used to display sizes in human readable format.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
pidstat.c
pidstat.h

index 9677addeaa163db8d5642938275d024ef001e017..5412ddee5045411c720085a660341b2bc8e03510 100644 (file)
--- a/pidstat.c
+++ b/pidstat.c
@@ -89,7 +89,7 @@ void usage(char *progname)
 
        fprintf(stderr, _("Options are:\n"
                          "[ -d ] [ -h ] [ -I ] [ -l ] [ -R ] [ -r ] [ -s ] [ -t ] [ -U [ <username> ] ]\n"
-                         "[ -u ] [ -V ] [ -v ] [ -w ] [ -C <command> ] [ -G <process_name> ]\n"
+                         "[ -u ] [ -V ] [ -v ] [ -w ] [ -C <command> ] [ -G <process_name> ] [ --human ]\n"
                          "[ -p { <pid> [,...] | SELF | ALL } ] [ -T { TASK | CHILD | ALL } ]\n"));
        exit(1);
 }
@@ -1396,7 +1396,7 @@ int write_pid_task_all_stats(int prev, int curr, int dis,
                        cprintf_f(-1, 2, 9, 2,
                                  S_VALUE(pstp->minflt, pstc->minflt, itv),
                                  S_VALUE(pstp->majflt, pstc->majflt, itv));
-                       cprintf_u64(-1, 2, 7,
+                       cprintf_u64(DISPLAY_UNIT(pidflag) ? 2 : -1, 2, 7,
                                    (unsigned long long) pstc->vsz,
                                    (unsigned long long) pstc->rss);
                        cprintf_pc(1, 6, 2,
@@ -1404,7 +1404,7 @@ int write_pid_task_all_stats(int prev, int curr, int dis,
                }
 
                if (DISPLAY_STACK(actflag)) {
-                       cprintf_u64(-1, 2, 7,
+                       cprintf_u64(DISPLAY_UNIT(pidflag) ? 2 : -1, 2, 7,
                                    (unsigned long long) pstc->stack_size,
                                    (unsigned long long) pstc->stack_ref);
                }
@@ -1412,11 +1412,19 @@ int write_pid_task_all_stats(int prev, int curr, int dis,
                if (DISPLAY_IO(actflag)) {
                        if (!NO_PID_IO(pstc->flags))
                        {
-                               cprintf_f(-1, 3, 9, 2,
-                                         S_VALUE(pstp->read_bytes,  pstc->read_bytes, itv)  / 1024,
-                                         S_VALUE(pstp->write_bytes, pstc->write_bytes, itv) / 1024,
-                                         S_VALUE(pstp->cancelled_write_bytes,
-                                                 pstc->cancelled_write_bytes, itv) / 1024);
+                               double rbytes, wbytes, cbytes;
+
+                               rbytes = S_VALUE(pstp->read_bytes,  pstc->read_bytes, itv);
+                               wbytes = S_VALUE(pstp->write_bytes, pstc->write_bytes, itv);
+                               cbytes = S_VALUE(pstp->cancelled_write_bytes,
+                                                pstc->cancelled_write_bytes, itv);
+                               if (!DISPLAY_UNIT(pidflag)) {
+                                       rbytes /= 1024;
+                                       wbytes /= 1024;
+                                       cbytes /= 1024;
+                               }
+                               cprintf_f(DISPLAY_UNIT(pidflag) ? 1 : -1, 3, 9, 2,
+                                         rbytes, wbytes, cbytes);
                        }
                        else {
                                /*
@@ -1747,7 +1755,7 @@ int write_pid_task_memory_stats(int prev, int curr, int dis, int disp_avg,
                          S_VALUE(pstp->majflt, pstc->majflt, itv));
 
                if (disp_avg) {
-                       cprintf_f(-1, 2, 7, 0,
+                       cprintf_f(DISPLAY_UNIT(pidflag) ? 2 : -1, 2, 7, 0,
                                  (double) pstc->total_vsz / pstc->rt_asum_count,
                                  (double) pstc->total_rss / pstc->rt_asum_count);
 
@@ -1757,7 +1765,7 @@ int write_pid_task_memory_stats(int prev, int curr, int dis, int disp_avg,
                                   : 0.0);
                }
                else {
-                       cprintf_u64(-1, 2, 7,
+                       cprintf_u64(DISPLAY_UNIT(pidflag) ? 2 : -1, 2, 7,
                                    (unsigned long long) pstc->vsz,
                                    (unsigned long long) pstc->rss);
 
@@ -1897,12 +1905,12 @@ int write_pid_stack_stats(int prev, int curr, int dis, int disp_avg,
                print_line_id(curr_string, pstc);
 
                if (disp_avg) {
-                       cprintf_f(-1, 2, 7, 0,
+                       cprintf_f(DISPLAY_UNIT(pidflag) ? 2 : -1, 2, 7, 0,
                                  (double) pstc->total_stack_size / pstc->sk_asum_count,
                                  (double) pstc->total_stack_ref  / pstc->sk_asum_count);
                }
                else {
-                       cprintf_u64(-1, 2, 7,
+                       cprintf_u64(DISPLAY_UNIT(pidflag) ? 2 : -1, 2, 7,
                                    (unsigned long long) pstc->stack_size,
                                    (unsigned long long) pstc->stack_ref);
                }
@@ -1944,6 +1952,7 @@ int write_pid_io_stats(int prev, int curr, int dis, int disp_avg,
        char dstr[32];
        unsigned int p;
        int rc, again = 0;
+       double rbytes, wbytes, cbytes;
 
        if (dis) {
                PRINT_ID_HDR(prev_string, pidflag);
@@ -1968,11 +1977,17 @@ int write_pid_io_stats(int prev, int curr, int dis, int disp_avg,
 
                print_line_id(curr_string, pstc);
                if (!NO_PID_IO(pstc->flags)) {
-                       cprintf_f(-1, 3, 9, 2,
-                                 S_VALUE(pstp->read_bytes,  pstc->read_bytes, itv)  / 1024,
-                                 S_VALUE(pstp->write_bytes, pstc->write_bytes, itv) / 1024,
-                                 S_VALUE(pstp->cancelled_write_bytes,
-                                         pstc->cancelled_write_bytes, itv) / 1024);
+                       rbytes = S_VALUE(pstp->read_bytes,  pstc->read_bytes, itv);
+                       wbytes = S_VALUE(pstp->write_bytes, pstc->write_bytes, itv);
+                       cbytes = S_VALUE(pstp->cancelled_write_bytes,
+                                        pstc->cancelled_write_bytes, itv);
+                       if (!DISPLAY_UNIT(pidflag)) {
+                               rbytes /= 1024;
+                               wbytes /= 1024;
+                               cbytes /= 1024;
+                       }
+                       cprintf_f(DISPLAY_UNIT(pidflag) ? 1 : -1, 3, 9, 2,
+                                 rbytes, wbytes, cbytes);
                }
                else {
                        /* I/O file not readable (permission denied or file non existent) */
@@ -2579,6 +2594,11 @@ int main(int argc, char **argv)
                        }
                }
 
+               else if (!strcmp(argv[opt], "--human")) {
+                       pidflag |= P_D_UNIT;
+                       opt++;
+               }
+
                else if (!strcmp(argv[opt], "-T")) {
                        if (argv[++opt]) {
                                if (tskflag) {
index 778d18ba5ac44a0e2719003b0b53dd2b2cf5559b..f58ee0c0e9674423001834b22d9b27742e6b4203 100644 (file)
--- a/pidstat.h
+++ b/pidstat.h
@@ -69,6 +69,7 @@
 #define P_D_USERNAME   0x100
 #define P_F_USERSTR    0x200
 #define P_F_PROCSTR    0x400
+#define P_D_UNIT       0x800
 
 #define DISPLAY_PID(m)         (((m) & P_D_PID) == P_D_PID)
 #define DISPLAY_ALL_PID(m)     (((m) & P_D_ALL_PID) == P_D_ALL_PID)
@@ -81,6 +82,7 @@
 #define DISPLAY_USERNAME(m)    (((m) & P_D_USERNAME) == P_D_USERNAME)
 #define USER_STRING(m)         (((m) & P_F_USERSTR) == P_F_USERSTR)
 #define PROCESS_STRING(m)      (((m) & P_F_PROCSTR) == P_F_PROCSTR)
+#define DISPLAY_UNIT(m)                (((m) & P_D_UNIT) == P_D_UNIT)
 
 /* Per-process flags */
 #define F_NO_PID_IO    0x01