]> granicus.if.org Git - sysstat/commitdiff
pidstat can now display the username of the tasks being monitored.
authorseb <seb@kluane.home>
Sat, 2 Mar 2013 13:34:54 +0000 (14:34 +0100)
committerseb <seb@kluane.home>
Sat, 2 Mar 2013 13:34:54 +0000 (14:34 +0100)
A new option (-U) has been added to pidstat: This option is used
to display the real user name of the tasks being monitored instead
of the UID. pidstat manual page has been updated.

CHANGES
man/pidstat.1
pidstat.c
pidstat.h

diff --git a/CHANGES b/CHANGES
index 07af58752a18781cc5a53b804b6b067c65d70f44..ceb6f82ab270d7a979d3c62b6d85404ef51f5e8b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -8,6 +8,8 @@ xxxx/xx/xx: Version 10.1.4 - Sebastien Godard (sysstat <at> orange.fr)
        * sar now stops and displays its average stats when it receives
          SIGINT (crtl/c).
        * pidstat now displays task's UID for all tasks.
+       * pidstat: -U option added. This option tells pidstat to display
+         the username of the task instead of its UID.
        * pidstat manual page updated.
        * FAQ updated.
 
index f93f67e092f58854a6c7f615ce3a3a4fd9884d6f..fa5054731ccc16affa32f340a161a0395835ec81 100644 (file)
@@ -2,7 +2,7 @@
 .SH NAME
 pidstat \- Report statistics for Linux tasks.
 .SH SYNOPSIS
-.B pidstat [ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -u ] [ -V ]
+.B pidstat [ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -U ] [ -u ] [ -V ]
 .B [ -w ] [ -C
 .I comm
 .B ] [ -p {
@@ -64,7 +64,7 @@ Display only tasks whose command name includes the string
 This string can be a regular expression.
 .IP -d
 Report I/O statistics (kernels 2.6.20 and later only).
-The following values are displayed:
+The following values may be displayed:
 
 .B UID
 .RS
@@ -72,6 +72,11 @@ The following values are displayed:
 The real user identification number of the task being monitored.
 .RE
 
+.B USER
+.RS
+The real user name of the task being monitored.
+.RE
+
 .B PID
 .RS
 The identification number of the task being monitored.
@@ -127,7 +132,7 @@ tasks managed by the system.
 Report page faults and memory utilization.
 
 When reporting statistics for individual tasks,
-the following values are displayed:
+the following values may be displayed:
 
 .B UID
 .RS
@@ -135,6 +140,11 @@ the following values are displayed:
 The real user identification number of the task being monitored.
 .RE
 
+.B USER
+.RS
+The real user name of the task being monitored.
+.RE
+
 .B PID
 .RS
 The identification number of the task being monitored.
@@ -174,7 +184,7 @@ The command name of the task.
 .RE
 
 When reporting global statistics for tasks and all their children,
-the following values are displayed:
+the following values may be displayed:
 
 .B UID
 .RS
@@ -182,6 +192,12 @@ The real user identification number of the task which is being monitored
 together with its children.
 .RE
 
+.B USER
+.RS
+The real user name of the task which is being monitored
+together with its children.
+.RE
+
 .B PID
 .RS
 The identification number of the task which is being monitored
@@ -208,7 +224,7 @@ together with its children.
 .RE
 .IP -s
 Report stack utilization.
-The following values are displayed:
+The following values may be displayed:
 
 .B UID
 .RS
@@ -216,6 +232,11 @@ The following values are displayed:
 The real user identification number of the task being monitored.
 .RE
 
+.B USER
+.RS
+The real user name of the task being monitored.
+.RE
+
 .B PID
 .RS
 The identification number of the task being monitored.
@@ -278,7 +299,7 @@ The identification number of the thread being monitored.
 Report CPU utilization.
 
 When reporting statistics for individual tasks,
-the following values are displayed:
+the following values may be displayed:
 
 .B UID
 .RS
@@ -286,6 +307,11 @@ the following values are displayed:
 The real user identification number of the task being monitored.
 .RE
 
+.B USER
+.RS
+The real user name of the task being monitored.
+.RE
+
 .B PID
 .RS
 The identification number of the task being monitored.
@@ -330,7 +356,7 @@ The command name of the task.
 .RE
 
 When reporting global statistics for tasks and all their children,
-the following values are displayed:
+the following values may be displayed:
 
 .B UID
 .RS
@@ -338,6 +364,12 @@ The real user identification number of the task which is being monitored
 together with its children.
 .RE
 
+.B USER
+.RS
+The real user name of the task which is being monitored
+together with its children.
+.RE
+
 .B PID
 .RS
 The identification number of the task which is being monitored
@@ -373,11 +405,13 @@ The command name of the task which is being monitored
 together with its children.
 .RE
 .RE
+.IP -U
+Display the real user name of the tasks being monitored instead of the UID.
 .IP -V
 Print version number then exit.
 .IP -w
 Report task switching activity (kernels 2.6.23 and later only).
-The following values are displayed:
+The following values may be displayed:
 
 .B UID
 .RS
@@ -385,6 +419,11 @@ The following values are displayed:
 The real user identification number of the task being monitored.
 .RE
 
+.B USER
+.RS
+The real user name of the task being monitored.
+.RE
+
 .B PID
 .RS
 The identification number of the task being monitored.
index 0e0855838b0221b23b4e82ef9d8b192e3771883c..63f1ac6c1a3cbc1a28c025b82816e5ccf8f1a030 100644 (file)
--- a/pidstat.c
+++ b/pidstat.c
@@ -27,6 +27,7 @@
 #include <dirent.h>
 #include <ctype.h>
 #include <sys/types.h>
+#include <pwd.h>
 #include <sys/utsname.h>
 #include <regex.h>
 
@@ -79,7 +80,7 @@ void usage(char *progname)
                progname);
 
        fprintf(stderr, _("Options are:\n"
-                         "[ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -u ] [ -V ]\n"
+                         "[ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -U ] [ -u ] [ -V ]\n"
                          "[ -w ] [ -C <command> ] [ -p { <pid> [,...] | SELF | ALL } ]\n"
                          "[ -T { TASK | CHILD | ALL } ]\n"));
        exit(1);
@@ -1053,7 +1054,7 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity,
 
 /*
  ***************************************************************************
- * Display PID and TID.
+ * Display UID/username, PID and TID.
  *
  * IN:
  * @pst                Current process statistics.
@@ -1063,7 +1064,15 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity,
 void __print_line_id(struct pid_stats *pst, char c)
 {
        char format[32];
+       struct passwd *pwdent;
 
+       if (DISPLAY_USERNAME(pidflag) && ((pwdent = getpwuid(pst->uid)) != NULL)) {
+               printf(" %8s", pwdent->pw_name);
+       }
+       else {
+               printf(" %5d", pst->uid);
+       }
+       
        if (DISPLAY_TID(pidflag)) {
                
                if (pst->tgid) {
@@ -1093,7 +1102,7 @@ void __print_line_id(struct pid_stats *pst, char c)
  */
 void print_line_id(char *timestamp, struct pid_stats *pst)
 {
-       printf("%-11s %5d", timestamp, pst->uid);
+       printf("%-11s", timestamp);
        
        __print_line_id(pst, '-');
 }
@@ -1149,7 +1158,7 @@ int write_pid_task_all_stats(int prev, int curr, int dis,
                                       &pstc, &pstp) <= 0)
                        continue;
 
-               printf("%11ld %5d", (long) time(NULL), pstc->uid);
+               printf("%11ld", (long) time(NULL));
                __print_line_id(pstc, '0');
 
                if (DISPLAY_CPU(actflag)) {
@@ -1254,7 +1263,7 @@ int write_pid_child_all_stats(int prev, int curr, int dis,
                                       &pstc, &pstp) <= 0)
                        continue;
 
-               printf("%11ld %5d", (long) time(NULL), pstc->uid);
+               printf("%11ld", (long) time(NULL));
                __print_line_id(pstc, '0');
 
                if (DISPLAY_CPU(actflag)) {
@@ -2130,6 +2139,11 @@ int main(int argc, char **argv)
                                        pidflag |= P_D_TID;
                                        break;
 
+                               case 'U':
+                                       /* Display username instead of UID */
+                                       pidflag |= P_D_USERNAME;
+                                       break;
+
                                case 'u':
                                        /* Display CPU usage */
                                        actflag |= P_A_CPU;
index dd090a10c97c9e67dcb4cbbf1919c5f4a85459fe..76262caa4f68bea3bb3532cebf5fe34b4131d866 100644 (file)
--- a/pidstat.h
+++ b/pidstat.h
@@ -47,6 +47,7 @@
 #define P_D_TID                0x020
 #define P_D_ONELINE    0x040
 #define P_D_CMDLINE    0x080
+#define P_D_USERNAME   0x100
 
 #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)
@@ -56,6 +57,7 @@
 #define DISPLAY_TID(m)         (((m) & P_D_TID) == P_D_TID)
 #define DISPLAY_ONELINE(m)     (((m) & P_D_ONELINE) == P_D_ONELINE)
 #define DISPLAY_CMDLINE(m)     (((m) & P_D_CMDLINE) == P_D_CMDLINE)
+#define DISPLAY_USERNAME(m)    (((m) & P_D_USERNAME) == P_D_USERNAME)
 
 #define F_NO_PID_IO    0x01
 
 #define TASK_SMAP      "/proc/%u/task/%u/smaps"
 
 #define PRINT_ID_HDR(_timestamp_, _flag_)      do {                                            \
-                                                       printf("\n%-11s   UID", _timestamp_);   \
+                                                       printf("\n%-11s", _timestamp_); \
+                                                       if (DISPLAY_USERNAME(_flag_)) {         \
+                                                               printf("     USER");            \
+                                                       }                                       \
+                                                       else {                                  \
+                                                               printf("   UID");               \
+                                                       }                                       \
                                                        if (DISPLAY_TID(_flag_)) {              \
                                                                printf("      TGID       TID"); \
                                                        }                                       \