diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2009-12-10 13:16:03 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2009-12-10 13:16:03 +0000 |
commit | a47070bc84c4e45ef117cf107e671a6c7aa1bc03 (patch) | |
tree | ff20a7201d778690951d590ff102defa93724dc5 | |
parent | 864a9954f2f11f85ca75e63be11507fbb7a0290c (diff) |
add an option '1' to display all cpu stats combined. helps save space
with increasing processor counts.
ok deraadt (kettenis otto)
-rw-r--r-- | usr.bin/top/display.c | 87 | ||||
-rw-r--r-- | usr.bin/top/top.1 | 8 | ||||
-rw-r--r-- | usr.bin/top/top.c | 20 |
3 files changed, 76 insertions, 39 deletions
diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c index 34a2b907bef..7858e37c968 100644 --- a/usr.bin/top/display.c +++ b/usr.bin/top/display.c @@ -1,4 +1,4 @@ -/* $OpenBSD: display.c,v 1.33 2007/11/30 10:39:01 otto Exp $ */ +/* $OpenBSD: display.c,v 1.34 2009/12/10 13:16:02 tedu Exp $ */ /* * Top users/processes display for Unix @@ -87,12 +87,8 @@ static char **procstate_names; static char **cpustate_names; static char **memory_names; -static int num_procstates; static int num_cpustates; -static int *lprocstates; -static int64_t **lcpustates; - static int *cpustate_columns; static int cpustate_total_length; @@ -103,6 +99,7 @@ int y_header; int y_idlecursor; int y_procs; extern int ncpu; +extern int combine_cpus; int Header_lines; int header_status = Yes; @@ -128,12 +125,18 @@ int display_resize(void) { int display_lines; + int cpu_lines = (combine_cpus ? 1 : ncpu); + + y_mem = 2 + cpu_lines; + y_header = 4 + cpu_lines; + y_procs = 5 + cpu_lines; + Header_lines = 5 + cpu_lines; /* calculate the current dimensions */ /* if operating in "dumb" mode, we only need one line */ display_lines = smart_terminal ? screen_length - Header_lines : 1; - y_idlecursor = y_message = 3 + ncpu; + y_idlecursor = y_message = 3 + (combine_cpus ? 1 : ncpu); if (screen_length <= y_message) y_idlecursor = y_message = screen_length - 1; @@ -154,7 +157,7 @@ display_resize(void) int display_init(struct statics * statics) { - int display_lines, *ip, i, cpu; + int display_lines, *ip, i; char **pp; if (smart_terminal) { @@ -169,32 +172,15 @@ display_init(struct statics * statics) standendp = empty; } - y_mem = 2 + ncpu; - y_header = 4 + ncpu; - y_procs = 5 + ncpu; - Header_lines = 5 + ncpu; - /* call resize to do the dirty work */ display_lines = display_resize(); /* only do the rest if we need to */ /* save pointers and allocate space for names */ procstate_names = statics->procstate_names; - num_procstates = string_count(procstate_names); - lprocstates = calloc(num_procstates, sizeof(int)); - if (lprocstates == NULL) - err(1, NULL); cpustate_names = statics->cpustate_names; num_cpustates = string_count(cpustate_names); - lcpustates = calloc(ncpu, sizeof(int64_t *)); - if (lcpustates == NULL) - err(1, NULL); - for (cpu = 0; cpu < ncpu; cpu++) { - lcpustates[cpu] = calloc(num_cpustates, sizeof(int64_t)); - if (lcpustates[cpu] == NULL) - err(1, NULL); - } cpustate_columns = calloc(num_cpustates, sizeof(int)); if (cpustate_columns == NULL) @@ -354,20 +340,61 @@ cpustates_tag(int cpu) } return (tag); } else - return ('\0'); + return ("\0"); } void i_cpustates(int64_t *ostates) { - int i, cpu, value; + int i, first, cpu; + double value; int64_t *states; - char **names = cpustate_names, *thisname; + char **names, *thisname; + + if (combine_cpus) { + static double *values; + if (!values) { + values = calloc(num_cpustates, sizeof(*values)); + if (!values) + err(1, NULL); + } + memset(values, 0, num_cpustates * sizeof(*values)); + for (cpu = 0; cpu < ncpu; cpu++) { + names = cpustate_names; + states = ostates + (CPUSTATES * cpu); + i = 0; + while ((thisname = *names++) != NULL) { + if (*thisname != '\0') { + /* retrieve the value and remember it */ + values[i++] += *states++; + } + } + } + if (screen_length > 2 || !smart_terminal) { + names = cpustate_names; + i = 0; + first = 0; + move(2, 0); + clrtoeol(); + addstrp("All CPUs: "); + while ((thisname = *names++) != NULL) { + if (*thisname != '\0') { + value = values[i++] / ncpu; + /* if percentage is >= 1000, print it as 100% */ + printwp((value >= 1000 ? "%s%4.0f%% %s" : + "%s%4.1f%% %s"), first++ == 0 ? "" : ", ", + value / 10., thisname); + } + } + putn(); + } + return; + } for (cpu = 0; cpu < ncpu; cpu++) { /* now walk thru the names and print the line */ names = cpustate_names; - i = 0; + first = 0; states = ostates + (CPUSTATES * cpu); if (screen_length > 2 + cpu || !smart_terminal) { @@ -382,8 +409,8 @@ i_cpustates(int64_t *ostates) /* if percentage is >= 1000, print it as 100% */ printwp((value >= 1000 ? "%s%4.0f%% %s" : - "%s%4.1f%% %s"), i++ == 0 ? "" : ", ", - ((float) value) / 10., thisname); + "%s%4.1f%% %s"), first++ == 0 ? "" : ", ", + value / 10., thisname); } } putn(); diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1 index 44eba1eac16..189419ca925 100644 --- a/usr.bin/top/top.1 +++ b/usr.bin/top/top.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: top.1,v 1.50 2007/11/17 09:29:59 jmc Exp $ +.\" $OpenBSD: top.1,v 1.51 2009/12/10 13:16:02 tedu Exp $ .\" .\" Copyright (c) 1997, Jason Downs. All rights reserved. .\" @@ -23,7 +23,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd $Mdocdate: November 17 2007 $ +.Dd $Mdocdate: December 10 2009 $ .Dt TOP 1 .Os .Sh NAME @@ -32,7 +32,7 @@ .Sh SYNOPSIS .Nm top .Bk -words -.Op Fl bCIinqSTu +.Op Fl 1bCIinqSTu .Op Fl d Ar count .Op Fl g Ar string .Op Fl o Ar field @@ -74,6 +74,8 @@ terminal. .Pp The options are as follows: .Bl -tag -width Ds +.It Fl 1 +Combine all CPU lines into a single line. .It Fl b Use .Em batch diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c index 1dceaca5ac5..716772c6a4e 100644 --- a/usr.bin/top/top.c +++ b/usr.bin/top/top.c @@ -1,4 +1,4 @@ -/* $OpenBSD: top.c,v 1.67 2009/07/18 06:12:41 jmc Exp $ */ +/* $OpenBSD: top.c,v 1.68 2009/12/10 13:16:02 tedu Exp $ */ /* * Top users/processes display for Unix @@ -83,6 +83,7 @@ int old_system = No; int old_threads = No; int show_args = No; pid_t hlpid = -1; +int combine_cpus = 0; #if Default_TOPN == Infinity char topn_specified = No; @@ -116,6 +117,7 @@ char topn_specified = No; #define CMD_grep 20 #define CMD_add 21 #define CMD_hl 22 +#define CMD_cpus 23 static void usage(void) @@ -123,7 +125,7 @@ usage(void) extern char *__progname; fprintf(stderr, - "usage: %s [-bCIinqSTu] [-d count] [-g string] [-o field] " + "usage: %s [-1bCIinqSTu] [-d count] [-g string] [-o field] " "[-p pid] [-s time]\n\t[-U user] [number]\n", __progname); } @@ -134,12 +136,14 @@ parseargs(int ac, char **av) char *endp; int i; - while ((i = getopt(ac, av, "STICbinqus:d:p:U:o:g:")) != -1) { + while ((i = getopt(ac, av, "1STICbinqus:d:p:U:o:g:")) != -1) { switch (i) { + case '1': + combine_cpus = 1; + break; case 'C': show_args = Yes; break; - case 'u': /* toggle uid/username display */ do_unames = !do_unames; break; @@ -516,7 +520,7 @@ rundisplay(void) int change, i; struct pollfd pfd[1]; uid_t uid; - static char command_chars[] = "\f qh?en#sdkriIuSopCTg+P"; + static char command_chars[] = "\f qh?en#sdkriIuSopCTg+P1"; /* * assume valid command unless told @@ -899,7 +903,11 @@ rundisplay(void) ps.command = NULL; /* grep */ hlpid = -1; break; - + case CMD_cpus: + combine_cpus = !combine_cpus; + max_topn = display_resize(); + reset_display(); + break; default: new_message(MT_standout, " BAD CASE IN SWITCH!"); putr(); |