Index: procstat.c =================================================================== --- procstat.c (revision 245225) +++ procstat.c (working copy) @@ -39,8 +39,8 @@ #include "procstat.h" -static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, lflag, sflag; -static int tflag, vflag, xflag; +static int aflag, bflag, cflag, eflag, fflag, iflag, jflag, kflag, lflag, rflag; +static int sflag, tflag, vflag, xflag; int hflag, nflag, Cflag; static void @@ -50,7 +50,8 @@ fprintf(stderr, "usage: procstat [-h] [-C] [-M core] [-N system] " "[-w interval] \n"); fprintf(stderr, " [-b | -c | -e | -f | -i | -j | -k | " - "-l | -s | -t | -v | -x] [-a | pid ...]\n"); + "-l | -r | -s | -t | -v | -x]\n"); + fprintf(stderr, " [-a | pid ...]\n"); exit(EX_USAGE); } @@ -74,6 +75,8 @@ procstat_kstack(kipp, kflag); else if (lflag) procstat_rlimit(kipp); + else if (rflag) + procstat_rusage(kipp); else if (sflag) procstat_cred(kipp); else if (tflag) @@ -125,7 +128,7 @@ interval = 0; memf = nlistf = NULL; - while ((ch = getopt(argc, argv, "CN:M:abcefijklhstvw:x")) != -1) { + while ((ch = getopt(argc, argv, "CN:M:abcefijklhrstvw:x")) != -1) { switch (ch) { case 'C': Cflag++; @@ -181,6 +184,10 @@ hflag++; break; + case 'r': + rflag++; + break; + case 's': sflag++; break; @@ -216,8 +223,8 @@ argv += optind; /* We require that either 0 or 1 mode flags be set. */ - tmp = bflag + cflag + eflag + fflag + (kflag ? 1 : 0) + lflag + sflag + - tflag + vflag + xflag; + tmp = bflag + cflag + eflag + fflag + (kflag ? 1 : 0) + lflag + rflag + + sflag + tflag + vflag + xflag; if (!(tmp == 0 || tmp == 1)) usage(); Index: procstat.h =================================================================== --- procstat.h (revision 245225) +++ procstat.h (working copy) @@ -43,6 +43,7 @@ void procstat_files(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_kstack(struct kinfo_proc *kipp, int kflag); void procstat_rlimit(struct kinfo_proc *kipp); +void procstat_rusage(struct kinfo_proc *kipp); void procstat_sigs(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_threads(struct kinfo_proc *kipp); void procstat_threads_sigs(struct procstat *prstat, struct kinfo_proc *kipp); Index: procstat_rusage.c =================================================================== --- procstat_rusage.c (revision 0) +++ procstat_rusage.c (revision 0) @@ -0,0 +1,119 @@ +/*- + * Copyright (c) 2012 Advanced Computing Technologies LLC + * Written by: John H. Baldwin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include +#include +#include +#include + +#include "procstat.h" + +struct { + const char *ri_name; + bool ri_humanize; +} rusage_info[] = { + { "maximum RSS", true }, + { "integral shared memory", true }, + { "integral unshared data", true }, + { "integral unshared stack", true }, + { "page reclaims", false }, + { "page faults", false }, + { "swaps", false }, + { "block reads", false }, + { "block writes", false }, + { "messages sent", false }, + { "messages received", false }, + { "signals received", false }, + { "voluntary context switches", false }, + { "involuntary context switches", false } +}; + +/* xxx days hh:mm:ss.uuuuuu */ +static const char * +format_time(struct timeval *tv) +{ + static char buffer[32]; + int days, hours, minutes, seconds, used; + + minutes = tv->tv_sec / 60; + seconds = tv->tv_sec % 60; + hours = minutes / 60; + minutes %= 60; + days = hours / 24; + hours %= 24; + used = 0; + if (days == 1) + used += snprintf(buffer, sizeof(buffer), "1 day "); + else if (days > 0) + used += snprintf(buffer, sizeof(buffer), "%u days ", days); + + snprintf(buffer + used, sizeof(buffer) - used, "%02u:%02u:%02u.%06u ", + hours, minutes, seconds, (unsigned int)tv->tv_usec); + return (buffer); +} + +static const char * +format_value(long value, bool humanize) +{ + static char buffer[14]; + + if (humanize) + humanize_number(buffer, sizeof(buffer), value, "", + HN_AUTOSCALE, HN_DECIMAL | HN_B); + else + snprintf(buffer, sizeof(buffer), "%ld ", value); + return (buffer); +} + +void +procstat_rusage(struct kinfo_proc *kipp) +{ + long *lp; + unsigned int i; + + if (!hflag) + printf("%5s %-16s %-32s %14s\n", + "PID", "COMM", "TYPE", "VALUE "); + + printf("%5d %-16s %-14s %32s\n", kipp->ki_pid, kipp->ki_comm, + "user time", format_time(&kipp->ki_rusage.ru_utime)); + printf("%5d %-16s %-14s %32s\n", kipp->ki_pid, kipp->ki_comm, + "system time", format_time(&kipp->ki_rusage.ru_stime)); + lp = &kipp->ki_rusage.ru_maxrss; + for (i = 0; i < nitems(rusage_info); i++) { + printf("%5d %-16s %-32s %14s\n", kipp->ki_pid, kipp->ki_comm, + rusage_info[i].ri_name, format_value(*lp, + rusage_info[i].ri_humanize)); + lp++; + } +} Property changes on: procstat_rusage.c ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + FreeBSD=%H Added: svn:eol-style + native Index: procstat.1 =================================================================== --- procstat.1 (revision 245225) +++ procstat.1 (working copy) @@ -37,7 +37,7 @@ .Op Fl n .Op Fl C .Op Fl w Ar interval -.Op Fl b | c | e | f | i | j | k | l | s | t | v | x +.Op Fl b | c | e | f | i | j | k | l | r | s | t | v | x .Op Fl a | Ar pid ... .Sh DESCRIPTION The @@ -71,6 +71,8 @@ printed. .It Fl l Display resource limits for the process. +.It Fl r +Display resource usage information for the process. .It Fl s Display security credential information for the process. .It Fl t Index: Makefile =================================================================== --- Makefile (revision 245225) +++ Makefile (working copy) @@ -11,6 +11,7 @@ procstat_files.c \ procstat_kstack.c \ procstat_rlimit.c \ + procstat_rusage.c \ procstat_sigs.c \ procstat_threads.c \ procstat_vm.c