From 881b50021de621c0a04f6dce1820773085891d23 Mon Sep 17 00:00:00 2001 From: seb Date: Sat, 2 Mar 2013 14:34:54 +0100 Subject: [PATCH] pidstat can now display the username of the tasks being monitored. 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 | 2 ++ man/pidstat.1 | 55 +++++++++++++++++++++++++++++++++++++++++++-------- pidstat.c | 24 +++++++++++++++++----- pidstat.h | 10 +++++++++- 4 files changed, 77 insertions(+), 14 deletions(-) diff --git a/CHANGES b/CHANGES index 07af587..ceb6f82 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,8 @@ xxxx/xx/xx: Version 10.1.4 - Sebastien Godard (sysstat 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. diff --git a/man/pidstat.1 b/man/pidstat.1 index f93f67e..fa50547 100644 --- a/man/pidstat.1 +++ b/man/pidstat.1 @@ -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. diff --git a/pidstat.c b/pidstat.c index 0e08558..63f1ac6 100644 --- a/pidstat.c +++ b/pidstat.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -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 ] [ -p { [,...] | 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; diff --git a/pidstat.h b/pidstat.h index dd090a1..76262ca 100644 --- 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 @@ -78,7 +80,13 @@ #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"); \ } \ -- 2.40.0