From 368d76456bba0368457c8068937a2ba4ea81033e Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 18 Jan 2008 01:43:14 +0000 Subject: Add a -P flag to display per-cpu cpu usage stats. --- contrib/top/display.c | 86 +++++++++++++++++++++++++++++++++++++++++---------- contrib/top/layout.h | 42 +++++++++++++------------ contrib/top/machine.h | 3 ++ contrib/top/top.c | 11 ++++++- contrib/top/top.h | 4 ++- 5 files changed, 107 insertions(+), 39 deletions(-) (limited to 'contrib/top') diff --git a/contrib/top/display.c b/contrib/top/display.c index 9de8952..0b1226c 100644 --- a/contrib/top/display.c +++ b/contrib/top/display.c @@ -78,8 +78,10 @@ static int *lcpustates; static int *lmemory; static int *lswap; +static int num_cpus; static int *cpustate_columns; static int cpustate_total_length; +static int cpustates_column; static enum { OFF, ON, ERASE } header_status = ON; @@ -87,6 +89,29 @@ static int string_count(); static void summary_format(); static void line_update(); +int x_lastpid = 10; +int y_lastpid = 0; +int x_loadave = 33; +int x_loadave_nompid = 15; +int y_loadave = 0; +int x_procstate = 0; +int y_procstate = 1; +int x_brkdn = 15; +int y_brkdn = 1; +int x_mem = 5; +int y_mem = 3; +int x_swap = 6; +int y_swap = 4; +int y_message = 5; +int x_header = 0; +int y_header = 6; +int x_idlecursor = 0; +int y_idlecursor = 5; +int y_procs = 7; + +int y_cpustates = 2; +int Header_lines = 7; + int display_resize() { @@ -138,6 +163,12 @@ struct statics *statics; /* call resize to do the dirty work */ lines = display_resize(); + num_cpus = statics->ncpus; + cpustates_column = 5; /* CPU: */ + if (num_cpus != 1) + cpustates_column += 2; /* CPU 0: */ + for (i = num_cpus; i > 9; i /= 10) + cpustates_column++; /* only do the rest if we need to */ if (lines > -1) @@ -153,7 +184,7 @@ struct statics *statics; num_swap = string_count(swap_names); lswap = (int *)malloc(num_swap * sizeof(int)); num_cpustates = string_count(cpustate_names); - lcpustates = (int *)malloc(num_cpustates * sizeof(int)); + lcpustates = (int *)malloc(num_cpustates * sizeof(int) * num_cpus); cpustate_columns = (int *)malloc(num_cpustates * sizeof(int)); memory_names = statics->memory_names; @@ -365,14 +396,13 @@ int *brkdn; } } +#ifdef no_more /* * *_cpustates(states, names) - print the cpu state percentages * * Assumptions: cursor is on the PREVIOUS line */ -static int cpustates_column; - /* cpustates_tag() calculates the correct tag to use to label the line */ char *cpustates_tag() @@ -398,6 +428,7 @@ char *cpustates_tag() cpustates_column = strlen(use); return(use); } +#endif i_cpustates(states) @@ -406,11 +437,18 @@ register int *states; { register int i = 0; register int value; - register char **names = cpustate_names; + register char **names; register char *thisname; + int cpu; + +for (cpu = 0; cpu < num_cpus; cpu++) { + names = cpustate_names; /* print tag and bump lastline */ - printf("\n%s", cpustates_tag()); + if (num_cpus == 1) + printf("\nCPU: "); + else + printf("\nCPU %d: ", cpu); lastline++; /* now walk thru the names and print the line */ @@ -423,14 +461,15 @@ register int *states; /* if percentage is >= 1000, print it as 100% */ printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"), - i++ == 0 ? "" : ", ", + (i++ % num_cpustates) == 0 ? "" : ", ", ((float)value)/10., thisname); } } +} /* copy over values into "last" array */ - memcpy(lcpustates, states, num_cpustates * sizeof(int)); + memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus); } u_cpustates(states) @@ -439,14 +478,18 @@ register int *states; { register int value; - register char **names = cpustate_names; + register char **names; register char *thisname; register int *lp; register int *colp; + int cpu; - Move_to(cpustates_column, y_cpustates); - lastline = y_cpustates; - lp = lcpustates; +for (cpu = 0; cpu < num_cpus; cpu++) { + names = cpustate_names; + + Move_to(cpustates_column, y_cpustates + cpu); + lastline = y_cpustates + cpu; + lp = lcpustates + (cpu * num_cpustates); colp = cpustate_columns; /* we could be much more optimal about this */ @@ -458,8 +501,8 @@ register int *states; if (*lp != *states) { /* yes, move and change */ - Move_to(cpustates_column + *colp, y_cpustates); - lastline = y_cpustates; + Move_to(cpustates_column + *colp, y_cpustates + cpu); + lastline = y_cpustates + cpu; /* retrieve value and remember it */ value = *states; @@ -479,30 +522,39 @@ register int *states; colp++; } } +} z_cpustates() { register int i = 0; - register char **names = cpustate_names; + register char **names; register char *thisname; register int *lp; + int cpu; + +for (cpu = 0; cpu < num_cpus; cpu++) { + names = cpustate_names; /* show tag and bump lastline */ - printf("\n%s", cpustates_tag()); + if (num_cpus == 1) + printf("\nCPU: "); + else + printf("\nCPU %d: ", cpu); lastline++; while ((thisname = *names++) != NULL) { if (*thisname != '\0') { - printf("%s %% %s", i++ == 0 ? "" : ", ", thisname); + printf("%s %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname); } } +} /* fill the "last" array with all -1s, to insure correct updating */ lp = lcpustates; - i = num_cpustates; + i = num_cpustates * num_cpus; while (--i >= 0) { *lp++ = -1; diff --git a/contrib/top/layout.h b/contrib/top/layout.h index 5db383f..1b2564e 100644 --- a/contrib/top/layout.h +++ b/contrib/top/layout.h @@ -4,26 +4,28 @@ * This file defines the locations on tne screen for various parts of the * display. These definitions are used by the routines in "display.c" for * cursor addressing. + * + * $FreeBSD$ */ -#define x_lastpid 10 -#define y_lastpid 0 -#define x_loadave 33 -#define x_loadave_nompid 15 -#define y_loadave 0 -#define x_procstate 0 -#define y_procstate 1 -#define x_brkdn 15 -#define y_brkdn 1 -#define x_mem 5 -#define y_mem 3 -#define x_swap 6 -#define y_swap 4 -#define y_message 5 -#define x_header 0 -#define y_header 6 -#define x_idlecursor 0 -#define y_idlecursor 5 -#define y_procs 7 +extern int x_lastpid; /* 10 */ +extern int y_lastpid; /* 0 */ +extern int x_loadave; /* 33 */ +extern int x_loadave_nompid; /* 15 */ +extern int y_loadave; /* 0 */ +extern int x_procstate; /* 0 */ +extern int y_procstate; /* 1 */ +extern int x_brkdn; /* 15 */ +extern int y_brkdn; /* 1 */ +extern int x_mem; /* 5 */ +extern int y_mem; /* 3 */ +extern int x_swap; /* 6 */ +extern int y_swap; /* 4 */ +extern int y_message; /* 5 */ +extern int x_header; /* 0 */ +extern int y_header; /* 6 */ +extern int x_idlecursor; /* 0 */ +extern int y_idlecursor; /* 5 */ +extern int y_procs; /* 7 */ -#define y_cpustates 2 +extern int y_cpustates; /* 2 */ diff --git a/contrib/top/machine.h b/contrib/top/machine.h index 10c69ca..4584650 100644 --- a/contrib/top/machine.h +++ b/contrib/top/machine.h @@ -20,6 +20,7 @@ struct statics #ifdef ORDER char **order_names; #endif + int ncpus; }; /* @@ -43,6 +44,8 @@ struct system_info int *memory; int *swap; struct timeval boottime; + unsigned long cpumask; /* bitfield of cpu states represented */ + int ncpus; }; /* cpu_states is an array of percentages * 10. For example, diff --git a/contrib/top/top.c b/contrib/top/top.c index b38300f..3fbd3a2 100644 --- a/contrib/top/top.c +++ b/contrib/top/top.c @@ -66,6 +66,7 @@ extern char *optarg; extern int overstrike; static int fmt_flags = 0; +int pcpu_stats = No; /* signal handling routines */ sigret_t leave(); @@ -282,7 +283,7 @@ char *argv[]; optind = 1; } - while ((i = getopt(ac, av, "CSIHabijnquvs:d:U:m:o:t")) != EOF) + while ((i = getopt(ac, av, "CSIHPabijnpquvs:d:U:m:o:t")) != EOF) { switch(i) { @@ -407,6 +408,14 @@ char *argv[]; ps.jail = !ps.jail; break; + case 'P': + pcpu_stats = Yes; + break; + + case 'p': + pcpu_stats = No; + break; + default: fprintf(stderr, "Top version %s\n" diff --git a/contrib/top/top.h b/contrib/top/top.h index 2b32d10..a281957 100644 --- a/contrib/top/top.h +++ b/contrib/top/top.h @@ -11,7 +11,7 @@ #define VERSION 3 /* Number of lines of header information on the standard screen */ -#define Header_lines 7 +extern int Header_lines; /* 7 */ /* Maximum number of columns allowed for display */ #define MAX_COLS 128 @@ -45,3 +45,5 @@ enum displaymodes { DISP_CPU = 0, DISP_IO, DISP_MAX }; #define FMT_SHOWARGS 0x00000001 extern enum displaymodes displaymode; + +extern int pcpu_stats; -- cgit v1.1