From 3306a8a299e3b5170060a4f3551d52d929086ff2 Mon Sep 17 00:00:00 2001 From: rafan Date: Tue, 17 Apr 2007 03:12:39 +0000 Subject: - Add a new 'j' switch and runtime option to toggle display jail id for each process. - While I'm here, keep help message sorted by keys PR: 98489, 98975 Submitted by: clsung Approved by: delphij (mentor) MFC after: 2 weeks --- usr.bin/top/machine.c | 57 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 10 deletions(-) (limited to 'usr.bin/top') diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index 7d973b8..083055f 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -95,26 +95,26 @@ struct handle { */ static char io_header[] = - " PID %-*.*s VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND"; + " PID%s %-*.*s VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND"; #define io_Proc_format \ - "%5d %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s" + "%5d%s %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s" static char smp_header_thr[] = - " PID %-*.*s THR PRI NICE SIZE RES STATE C TIME %6s COMMAND"; + " PID%s %-*.*s THR PRI NICE SIZE RES STATE C TIME %6s COMMAND"; static char smp_header[] = - " PID %-*.*s " "PRI NICE SIZE RES STATE C TIME %6s COMMAND"; + " PID%s %-*.*s " "PRI NICE SIZE RES STATE C TIME %6s COMMAND"; #define smp_Proc_format \ - "%5d %-*.*s %s%3d %4s%7s %6s %-6.6s %1x%7s %5.2f%% %.*s" + "%5d%s %-*.*s %s%3d %4s%7s %6s %-6.6s %1x%7s %5.2f%% %.*s" static char up_header_thr[] = - " PID %-*.*s THR PRI NICE SIZE RES STATE TIME %6s COMMAND"; + " PID%s %-*.*s THR PRI NICE SIZE RES STATE TIME %6s COMMAND"; static char up_header[] = - " PID %-*.*s " "PRI NICE SIZE RES STATE TIME %6s COMMAND"; + " PID%s %-*.*s " "PRI NICE SIZE RES STATE TIME %6s COMMAND"; #define up_Proc_format \ - "%5d %-*.*s %s%3d %4s%7s %6s %-6.6s%.0d%7s %5.2f%% %.*s" + "%5d%s %-*.*s %s%3d %4s%7s %6s %-6.6s%.0d%7s %5.2f%% %.*s" /* process state names for the "STATE" column of the display */ @@ -211,10 +211,12 @@ long percentages(); */ char *ordernames[] = { "cpu", "size", "res", "time", "pri", "threads", - "total", "read", "write", "fault", "vcsw", "ivcsw", NULL + "total", "read", "write", "fault", "vcsw", "ivcsw", + "jid", NULL }; #endif +static int compare_jid(const void *a, const void *b); static int compare_pid(const void *a, const void *b); static const char *format_nice(const struct kinfo_proc *pp); static void getsysctl(const char *name, void *ptr, size_t len); @@ -300,12 +302,14 @@ format_header(char *uname_field) (ps.thread ? smp_header : smp_header_thr) : (ps.thread ? up_header : up_header_thr); snprintf(Header, sizeof(Header), prehead, + ps.jail ? " JID" : "", namelength, namelength, uname_field, ps.wcpu ? "WCPU" : "CPU"); break; case DISP_IO: prehead = io_header; snprintf(Header, sizeof(Header), prehead, + ps.jail ? " JID" : "", namelength, namelength, uname_field); break; } @@ -661,7 +665,7 @@ format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) int state; struct rusage ru, *rup; long p_tot, s_tot; - char *proc_fmt, thr_buf[6]; + char *proc_fmt, thr_buf[6], jid_buf[6]; char *cmdbuf = NULL; char **args; @@ -785,6 +789,12 @@ format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) free(argbuf); } + if (ps.jail == 0) + jid_buf[0] = '\0'; + else + snprintf(jid_buf, sizeof(jid_buf), " %*d", + sizeof(jid_buf) - 3, pp->ki_jid); + if (displaymode == DISP_IO) { oldp = get_old_proc(pp); if (oldp != NULL) { @@ -804,6 +814,7 @@ format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) sprintf(fmt, io_Proc_format, pp->ki_pid, + jid_buf, namelength, namelength, (*get_userid)(pp->ki_ruid), rup->ru_nvcsw, rup->ru_nivcsw, @@ -831,6 +842,7 @@ format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) sprintf(fmt, proc_fmt, pp->ki_pid, + jid_buf, namelength, namelength, (*get_userid)(pp->ki_ruid), thr_buf, pp->ki_pri.pri_level - PZERO, @@ -985,6 +997,12 @@ static int sorted_state[] = { return (diff > 0 ? 1 : -1); \ } while (0) +#define ORDERKEY_JID(a, b) do { \ + int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \ + if (diff != 0) \ + return (diff > 0 ? 1 : -1); \ +} while (0) + /* compare_cpu - the comparison function for sorting by cpu percentage */ int @@ -1032,6 +1050,7 @@ int (*compares[])() = { compare_iofault, compare_vcsw, compare_ivcsw, + compare_jid, NULL }; @@ -1124,6 +1143,24 @@ compare_threads(void *arg1, void *arg2) return (0); } + +/* compare_jid - the comparison function for sorting by jid */ +static int +compare_jid(const void *arg1, const void *arg2) +{ + struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; + struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; + + ORDERKEY_JID(p1, p2); + ORDERKEY_PCTCPU(p1, p2); + ORDERKEY_CPTICKS(p1, p2); + ORDERKEY_STATE(p1, p2); + ORDERKEY_PRIO(p1, p2); + ORDERKEY_RSSIZE(p1, p2); + ORDERKEY_MEM(p1, p2); + + return (0); +} #endif /* ORDER */ /* assorted comparison functions for sorting by i/o */ -- cgit v1.1