RFC: Sorting in top -m io

From: Giorgos Keramidas <keramida_at_freebsd.org>
Date: Wed, 7 Jul 2004 15:47:51 +0300
Hi all...

The new -m io option of top is cool, but sorting is broken when the 'io'
mode is enabled.  The following patch adds sorting capabilities to the 'io'
mode too and makes sure that hitting 'h' for help prints the right list of
sorting keywords :)

The only thing I didn't add a sorting capability for is "percent", because
I'm not sure I know all it takes to do this.

What do you all think of this patch to top(1)?

%%
Index: contrib/top/commands.c
===================================================================
RCS file: /home/ncvs/src/contrib/top/commands.c,v
retrieving revision 1.6
diff -u -r1.6 commands.c
--- contrib/top/commands.c	17 Jul 2003 23:56:40 -0000	1.6
+++ contrib/top/commands.c	7 Jul 2004 12:40:42 -0000
_at__at_ -76,8 +76,12 _at__at_
 k       - kill processes; send a signal to a list of processes\n\
 n or #  - change number of processes to display\n", stdout);
 #ifdef ORDER
-	fputs("\
+	if (displaymode == DISP_CPU)
+		fputs("\
 o       - specify sort order (pri, size, res, cpu, time)\n", stdout);
+	else
+		fputs("\
+o       - specify sort order (read, write, fault, total)\n", stdout);
 #endif
 	fputs("\
 r       - renice a process\n\
Index: contrib/top/top.c
===================================================================
RCS file: /home/ncvs/src/contrib/top/top.c,v
retrieving revision 1.12
diff -u -r1.12 top.c
--- contrib/top/top.c	5 Jul 2004 12:19:53 -0000	1.12
+++ contrib/top/top.c	7 Jul 2004 12:11:01 -0000
_at__at_ -96,10 +96,11 _at__at_
 
 #ifdef ORDER
 extern int (*proc_compares[])();
+extern int (*io_compares[])();
 #else
 extern int proc_compare();
-#endif
 extern int io_compare();
+#endif
 time_t time();
 
 caddr_t get_process_info();
_at__at_ -567,15 +568,17 _at__at_
 	/* get the current stats */
 	get_system_info(&system_info);
 
-	if (displaymode == DISP_CPU) {
 #ifdef ORDER
+	if (displaymode == DISP_CPU)
 		compare = proc_compares[order_index];
+	else
+		compare = io_compares[order_index];
 #else
+	if (displaymode == DISP_CPU)
 		compare = proc_compare;
-#endif
-	} else {
+	else
 		compare = io_compare;
-	}
+#endif
 
 	/* get the current set of processes */
 	processes =
Index: usr.bin/top/machine.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/top/machine.c,v
retrieving revision 1.63
diff -u -r1.63 machine.c
--- usr.bin/top/machine.c	5 Jul 2004 14:55:58 -0000	1.63
+++ usr.bin/top/machine.c	7 Jul 2004 12:37:42 -0000
_at__at_ -205,10 +205,16 _at__at_
 long percentages();
 
 #ifdef ORDER
-/* sorting orders. first is default */
-char *ordernames[] = {
+/*
+ * Sorting orders.  One vector per display mode.
+ * The first element is the default for each mode.
+ */
+char *proc_ordernames[] = {
 	"cpu", "size", "res", "time", "pri", NULL
 };
+char *io_ordernames[] = {
+	"total", "read", "write", "fault", NULL
+};
 #endif
 
 int
_at__at_ -265,7 +271,15 _at__at_
 	statics->memory_names = memorynames;
 	statics->swap_names = swapnames;
 #ifdef ORDER
-	statics->order_names = ordernames;
+	switch (displaymode) {
+	case DISP_IO:
+		statics->order_names = io_ordernames;
+		break;
+	case DISP_CPU:
+	default:
+		statics->order_names = proc_ordernames;
+		break;
+	}
 #endif
 
 	/* all done! */
_at__at_ -917,14 +931,74 _at__at_
 }
 #endif
 
+/* compare_io - the comparison function for sorting by total io */
+
 int
+#ifdef ORDER
+compare_iototal(void *arg1, void *arg2)
+#else
 io_compare(void *arg1, void *arg2)
+#endif
 {
 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
 
 	return (get_io_total(p2) - get_io_total(p1));
 }
+
+#ifdef ORDER
+/* io compare routines */
+int compare_ioread(), compare_iowrite(), compare_iofault();
+
+int (*io_compares[])() = {
+	compare_iototal,
+	compare_ioread,
+	compare_iowrite,
+	compare_iofault,
+	NULL
+};
+
+int
+compare_ioread(void *arg1, void *arg2)
+{
+	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
+	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
+	long dummy, inp1, inp2;
+
+	(void) get_io_stats(p1, &inp1, &dummy, &dummy);
+	(void) get_io_stats(p2, &inp2, &dummy, &dummy);
+
+	return (inp2 - inp1);
+}
+
+int
+compare_iowrite(void *arg1, void *arg2)
+{
+	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
+	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
+	long dummy, oup1, oup2;
+
+	(void) get_io_stats(p1, &dummy, &oup1, &dummy);
+	(void) get_io_stats(p2, &dummy, &oup2, &dummy);
+
+	return (oup2 - oup1);
+}
+
+int
+compare_iofault(void *arg1, void *arg2)
+{
+	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
+	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
+	long dummy, flp1, flp2;
+
+	(void) get_io_stats(p1, &dummy, &dummy, &flp1);
+	(void) get_io_stats(p2, &dummy, &dummy, &flp2);
+
+	return (flp2 - flp1);
+}
+
+#endif /* ORDER */
+
 /*
  * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
  *		the process does not exist.
%%
Received on Thu Jul 08 2004 - 10:45:26 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:00 UTC