Subject: change to top (or top change 8)
To: None <tech-userlevel@netbsd.org>
From: Chris Gilbert <chris@dokein.co.uk>
List: tech-userlevel
Date: 02/25/2003 01:05:19
This is a multi-part message in MIME format.

--Multipart_Tue__25_Feb_2003_01:05:19_+0000_08234000
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

Hi,

Would people be interested in me applying the attached diff to give more
detail on the memory usage in top, or rather it's more explicit, with
the Memory line now adding up to the total amount of available memory,
and the memory usage saying how much is used for what, my top now shows
output such as:

load averages:  0.39,  0.38,  0.36                                    
00:59:01
79 processes:  1 runnable, 77 sleeping, 1 on processor
CPU states:  5.9% user,  0.0% nice,  4.4% system,  0.0% interrupt, 89.7%
idle
Memory: 121M Act, 50M Inact, 1184K Wired, 14M Free
Memory usage: 86M Anon, 31M Exec, 68M File, 24K Rsvd
Swap: 641M Total, 181M Used, 461M Free

  PID USERNAME PRI NICE   SIZE   RES STATE      TIME   WCPU    CPU
COMMAND
  217 root       2    0    37M   23M RUN      423:45  3.86%  3.86%
XFree86
 2073 chris      2    0  1632K   10M select     0:21  2.00%  2.00%
kdeinit
  315 chris      2    4  3588K 9964K select   163:24  0.49%  0.49%
kdeinit
  307 chris      2    4  2416K 5952K select    13:59  0.05%  0.05%
kdeinit

Note that you also get paging inserted between Wired and Free if you're
paging something out (which is always handy to know on a low memory,
heavily loaded system)

However is it worth losing 1 line of processes?  Or shall I just keep it
in my own tree, rather than inflict on everyone 8)

Chris

PS Those wondering why, it's because a while back someone asked why
doesn't the memory line add up and make sense...

--Multipart_Tue__25_Feb_2003_01:05:19_+0000_08234000
Content-Type: text/plain;
 name="top.diff"
Content-Disposition: attachment;
 filename="top.diff"
Content-Transfer-Encoding: 7bit

? top.diff
Index: display.c
===================================================================
RCS file: /cvsroot/src/usr.bin/top/display.c,v
retrieving revision 1.8
diff -u -p -r1.8 display.c
--- display.c	2002/07/16 00:40:51	1.8
+++ display.c	2003/02/25 00:49:13
@@ -41,7 +41,7 @@
  *        have minimal (or nonexistent) terminal capabilities.
  *
  *        The routines are called in this order:  *_loadave, i_timeofday,
- *        *_procstates, *_cpustates, *_memory, *_message, *_header,
+ *        *_procstates, *_cpustates, *_memory, *_memusage, *_message, *_header,
  *        *_process, u_endscreen.
  */
 
@@ -81,16 +81,19 @@ char *screenbuf = NULL;
 static char **procstate_names;
 static char **cpustate_names;
 static char **memory_names;
+static char **memory_usage;
 static char **swap_names;
 
 static int num_procstates;
 static int num_cpustates;
 static int num_memory;
+static int num_memory_usage;
 static int num_swap;
 
 static int *lprocstates;
 static int *lcpustates;
 static int *lmemory;
+static int *lmemory_usage;
 static int *lswap;
 
 static int *cpustate_columns;
@@ -168,7 +171,12 @@ struct statics *statics;
 	memory_names = statics->memory_names;
 	num_memory = string_count(memory_names);
 	lmemory = (int *)malloc(num_memory * sizeof(int));
+	
+	memory_usage = statics->memory_usage;
+	num_memory_usage = string_count(memory_usage);
+	lmemory_usage = (int *)malloc(num_memory_usage * sizeof(int));
 
+
 	swap_names = statics->swap_names;
 	num_swap = string_count(swap_names);
 	lswap = (int *)malloc(num_swap * sizeof(int));
@@ -565,6 +573,43 @@ int *stats;
     summary_format(new, stats, memory_names);
     line_update(memory_buffer, new, x_mem, y_mem);
 }
+
+/*
+ *  *_memusage(stats) - print "Memory usage: " followed by where memory is used.
+ *
+ *  Assumptions:  cursor is on "lastline"
+ *                for i_memory ONLY: cursor is on the previous line
+ */
+
+char memory_usage_buffer[MAX_COLS];
+
+void
+i_memusage(stats)
+
+int *stats;
+
+{
+    fputs("\nMemory usage: ", stdout);
+    lastline++;
+
+    /* format and print the memory summary */
+    summary_format(memory_usage_buffer, stats, memory_usage);
+    fputs(memory_usage_buffer, stdout);
+}
+
+void
+u_memusage(stats)
+
+int *stats;
+
+{
+    static char new[MAX_COLS];
+
+    /* format the new line */
+    summary_format(new, stats, memory_usage);
+    line_update(memory_usage_buffer, new, x_musage, y_musage);
+}
+
 
 /*  
  *  *_swap(stats) - print "Swap: " followed by the memory summary string
Index: display.h
===================================================================
RCS file: /cvsroot/src/usr.bin/top/display.h,v
retrieving revision 1.6
diff -u -p -r1.6 display.h
--- display.h	2002/07/16 00:40:51	1.6
+++ display.h	2003/02/25 00:49:13
@@ -49,6 +49,8 @@ void u_cpustates __P((int *));
 void z_cpustates __P((void));
 void i_memory __P((int *));
 void u_memory __P((int *));
+void i_memusage __P((int *));
+void u_memusage __P((int *));
 void i_swap __P((int *));
 void u_swap __P((int *));
 void i_message __P((void));
Index: layout.h
===================================================================
RCS file: /cvsroot/src/usr.bin/top/layout.h,v
retrieving revision 1.4
diff -u -p -r1.4 layout.h
--- layout.h	2002/07/16 00:40:51	1.4
+++ layout.h	2003/02/25 00:49:13
@@ -46,14 +46,16 @@
 #define  x_brkdn	15
 #define  y_brkdn	1
 #define  x_mem		8
-#define  y_mem		3
+#define  y_mem		3	
+#define  x_musage	14
+#define  y_musage	4
 #define  x_swap 	6
-#define  y_swap 	4
-#define  y_message	5
+#define  y_swap 	5	
+#define  y_message	6
 #define  x_header	0
-#define  y_header	6
+#define  y_header	7
 #define  x_idlecursor	0
-#define  y_idlecursor	5
-#define  y_procs	7
+#define  y_idlecursor	6
+#define  y_procs	8
 
 #define  y_cpustates	2
Index: machine.h
===================================================================
RCS file: /cvsroot/src/usr.bin/top/machine.h,v
retrieving revision 1.7
diff -u -p -r1.7 machine.h
--- machine.h	2002/07/16 00:40:51	1.7
+++ machine.h	2003/02/25 00:49:13
@@ -42,6 +42,7 @@ struct statics
     char **procstate_names;
     char **cpustate_names;
     char **memory_names;
+    char **memory_usage;
     char **swap_names;
 #ifdef ORDER
     char **order_names;
@@ -67,6 +68,7 @@ struct system_info
     int    *procstates;
     int    *cpustates;
     int    *memory;
+    int    *memusage;
     int    *swap;
 };
 
Index: top.c
===================================================================
RCS file: /cvsroot/src/usr.bin/top/top.c,v
retrieving revision 1.15
diff -u -p -r1.15 top.c
--- top.c	2002/09/23 12:48:05	1.15
+++ top.c	2003/02/25 00:49:14
@@ -93,6 +93,7 @@ void (*d_loadave) __P((int, double *)) =
 void (*d_procstates) __P((int, int *)) = i_procstates;
 void (*d_cpustates) __P((int *)) = i_cpustates;
 void (*d_memory) __P((int *)) = i_memory;
+void (*d_memusage) __P((int *)) = i_memusage;
 void (*d_swap) __P((int *)) = i_swap;
 void (*d_message) __P((void)) = i_message;
 void (*d_header) __P((char *)) = i_header;
@@ -543,6 +544,9 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o f
 	/* display memory stats */
 	(*d_memory)(system_info.memory);
 
+	/* display memory usage */
+	(*d_memusage)(system_info.memusage);
+
 	/* display swap stats */
 	(*d_swap)(system_info.swap); 
 
@@ -606,6 +610,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o f
 		    d_procstates = u_procstates;
 		    d_cpustates = u_cpustates;
 		    d_memory = u_memory;
+		    d_memusage = u_memusage;
 		    d_swap = u_swap;
 		    d_message = u_message;
 		    d_header = u_header;
@@ -914,6 +919,7 @@ reset_display()
     d_procstates = i_procstates;
     d_cpustates  = i_cpustates;
     d_memory     = i_memory;
+    d_memusage   = i_memusage;
     d_swap       = i_swap;
     d_message	 = i_message;
     d_header	 = i_header;
Index: top.h
===================================================================
RCS file: /cvsroot/src/usr.bin/top/top.h,v
retrieving revision 1.5
diff -u -p -r1.5 top.h
--- top.h	2002/07/16 00:40:51	1.5
+++ top.h	2003/02/25 00:49:14
@@ -38,7 +38,7 @@
 #define VERSION		3
 
 /* Number of lines of header information on the standard screen */
-#define Header_lines	7
+#define Header_lines	8
 
 /* Maximum number of columns allowed for display */
 #define MAX_COLS	128
Index: machine/m_netbsd15.c
===================================================================
RCS file: /cvsroot/src/usr.bin/top/machine/m_netbsd15.c,v
retrieving revision 1.17
diff -u -p -r1.17 m_netbsd15.c
--- machine/m_netbsd15.c	2003/01/18 10:55:52	1.17
+++ machine/m_netbsd15.c	2003/02/25 00:49:15
@@ -135,13 +135,18 @@ char *cpustatenames[] = {
 
 /* these are for detailing the memory statistics */
 
-int memory_stats[7];
+int memory_stats[6];
 char *memorynames[] = {
-	"K Act, ", "K Inact, ", "K Wired, ", "K Exec, ", "K File, ",
-	"K Free, ",
+	"K Act, ", "K Inact, ", "K Wired, ", "K Paging, ", "K Free, ",
 	NULL
 };
 
+int memory_usage[5];
+char *memusagenames[] = {
+	"K Anon, ", "K Exec, ", "K File, ", "K Rsvd, ",
+	NULL
+};  
+
 int swap_stats[4];
 char *swapnames[] = {
 	"K Total, ", "K Used, ", "K Free, ",
@@ -248,6 +253,7 @@ machine_init(statics)
 	statics->procstate_names = procstatenames;
 	statics->cpustate_names = cpustatenames;
 	statics->memory_names = memorynames;
+	statics->memory_usage = memusagenames;
 	statics->swap_names = swapnames;
 	statics->order_names = ordernames;
 
@@ -314,9 +320,13 @@ get_system_info(si)
 	memory_stats[0] = pagetok(uvmexp.active);
 	memory_stats[1] = pagetok(uvmexp.inactive);
 	memory_stats[2] = pagetok(uvmexp.wired);
-	memory_stats[3] = pagetok(uvmexp.execpages);
-	memory_stats[4] = pagetok(uvmexp.filepages);
-	memory_stats[5] = pagetok(uvmexp.free);
+	memory_stats[3] = pagetok(uvmexp.paging);
+	memory_stats[4] = pagetok(uvmexp.free);
+	memory_usage[0] = pagetok(uvmexp.anonpages);
+	memory_usage[1] = pagetok(uvmexp.execpages);
+	memory_usage[2] = pagetok(uvmexp.filepages);
+	memory_usage[3] = pagetok(uvmexp.zeropages +
+		uvmexp.reserve_pagedaemon + uvmexp.reserve_kernel);
 
 	swap_stats[0] = swap_stats[1] = swap_stats[2] = 0;
 
@@ -361,12 +371,14 @@ get_system_info(si)
 	if (seporig)
 		free(seporig);
 
-	memory_stats[6] = -1;
+	memory_stats[5] = -1;
+	memory_usage[4] = -1;
 	swap_stats[3] = -1;
 
 	/* set arrays and strings */
 	si->cpustates = cpu_states;
 	si->memory = memory_stats;
+	si->memusage = memory_usage;
 	si->swap = swap_stats;
 	si->last_pid = -1;
 }

--Multipart_Tue__25_Feb_2003_01:05:19_+0000_08234000--