summaryrefslogtreecommitdiff
path: root/usr.bin/top
diff options
context:
space:
mode:
authorTed Unangst <tedu@cvs.openbsd.org>2010-01-29 00:36:10 +0000
committerTed Unangst <tedu@cvs.openbsd.org>2010-01-29 00:36:10 +0000
commit31e032aabd2861a8d88cb4902ba845f7b566eb10 (patch)
treece156a99cc20a756ea0e346a288857de12701799 /usr.bin/top
parentd9bb336cfa276e2e2e44fdb6a25888de7d21bef3 (diff)
Allow sorting by command and pid. Also, allow partial matches with strncmp.
ok jmc otto
Diffstat (limited to 'usr.bin/top')
-rw-r--r--usr.bin/top/display.c4
-rw-r--r--usr.bin/top/machine.c59
-rw-r--r--usr.bin/top/top.18
-rw-r--r--usr.bin/top/top.c4
-rw-r--r--usr.bin/top/utils.c4
5 files changed, 68 insertions, 11 deletions
diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c
index 9a57a894640..7b2005c775f 100644
--- a/usr.bin/top/display.c
+++ b/usr.bin/top/display.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: display.c,v 1.35 2009/12/22 15:06:56 jmc Exp $ */
+/* $OpenBSD: display.c,v 1.36 2010/01/29 00:36:09 tedu Exp $ */
/*
* Top users/processes display for Unix
@@ -770,7 +770,7 @@ show_help(void)
"I | i - toggle the display of idle processes\n"
"k [-sig] pid - send signal `-sig' to process `pid'\n"
"n|# count - show `count' processes\n"
- "o field - specify sort order (size, res, cpu, time, pri)\n"
+ "o field - specify sort order (size, res, cpu, time, pri, pid, command)\n"
"P pid - highlight process `pid' (P+ switches highlighting off)\n"
"p pid - display process by `pid' (p+ selects all processes)\n"
"q - quit\n"
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c
index f2720636a53..3a44faf2e10 100644
--- a/usr.bin/top/machine.c
+++ b/usr.bin/top/machine.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: machine.c,v 1.64 2009/04/28 21:24:41 deraadt Exp $ */
+/* $OpenBSD: machine.c,v 1.65 2010/01/29 00:36:09 tedu Exp $ */
/*-
* Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com>
@@ -118,7 +118,7 @@ char *memorynames[] = {
/* these are names given to allowed sorting orders -- first is default */
char *ordernames[] = {
- "cpu", "size", "res", "time", "pri", NULL
+ "cpu", "size", "res", "time", "pri", "pid", "command", NULL
};
/* these are for keeping track of the proc array */
@@ -539,6 +539,10 @@ static unsigned char sorted_state[] =
if ((result = p2->p_vm_rssize - p1->p_vm_rssize) == 0)
#define ORDERKEY_MEM \
if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0)
+#define ORDERKEY_PID \
+ if ((result = p1->p_pid - p2->p_pid) == 0)
+#define ORDERKEY_CMD \
+ if ((result = strcmp(p1->p_comm, p2->p_comm)) == 0)
/* compare_cpu - the comparison function for sorting by cpu percentage */
static int
@@ -660,12 +664,63 @@ compare_prio(const void *v1, const void *v2)
return (result);
}
+static int
+compare_pid(const void *v1, const void *v2)
+{
+ struct proc **pp1 = (struct proc **) v1;
+ struct proc **pp2 = (struct proc **) v2;
+ struct kinfo_proc2 *p1, *p2;
+ pctcpu lresult;
+ int result;
+
+ /* remove one level of indirection */
+ p1 = *(struct kinfo_proc2 **) pp1;
+ p2 = *(struct kinfo_proc2 **) pp2;
+
+ ORDERKEY_PID
+ ORDERKEY_PCTCPU
+ ORDERKEY_CPUTIME
+ ORDERKEY_STATE
+ ORDERKEY_PRIO
+ ORDERKEY_RSSIZE
+ ORDERKEY_MEM
+ ;
+ return (result);
+}
+
+static int
+compare_cmd(const void *v1, const void *v2)
+{
+ struct proc **pp1 = (struct proc **) v1;
+ struct proc **pp2 = (struct proc **) v2;
+ struct kinfo_proc2 *p1, *p2;
+ pctcpu lresult;
+ int result;
+
+ /* remove one level of indirection */
+ p1 = *(struct kinfo_proc2 **) pp1;
+ p2 = *(struct kinfo_proc2 **) pp2;
+
+ ORDERKEY_CMD
+ ORDERKEY_PCTCPU
+ ORDERKEY_CPUTIME
+ ORDERKEY_STATE
+ ORDERKEY_PRIO
+ ORDERKEY_RSSIZE
+ ORDERKEY_MEM
+ ;
+ return (result);
+}
+
+
int (*proc_compares[])(const void *, const void *) = {
compare_cpu,
compare_size,
compare_res,
compare_time,
compare_prio,
+ compare_pid,
+ compare_cmd,
NULL
};
diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1
index 438b5c783b8..5b5c1093e0a 100644
--- a/usr.bin/top/top.1
+++ b/usr.bin/top/top.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: top.1,v 1.53 2009/12/22 18:14:23 jmc Exp $
+.\" $OpenBSD: top.1,v 1.54 2010/01/29 00:36:09 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: December 22 2009 $
+.Dd $Mdocdate: January 29 2010 $
.Dt TOP 1
.Os
.Sh NAME
@@ -144,8 +144,10 @@ supports
.Ar size ,
.Ar res ,
.Ar time ,
+.Ar pri ,
+.Ar pid ,
and
-.Ar pri .
+.Ar command .
.It Fl p Ar pid
Show only the process
.Ar pid .
diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c
index 716772c6a4e..679752a1fc7 100644
--- a/usr.bin/top/top.c
+++ b/usr.bin/top/top.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: top.c,v 1.68 2009/12/10 13:16:02 tedu Exp $ */
+/* $OpenBSD: top.c,v 1.69 2010/01/29 00:36:09 tedu Exp $ */
/*
* Top users/processes display for Unix
@@ -322,7 +322,7 @@ main(int argc, char *argv[])
if (order_name != NULL) {
if ((order_index = string_index(order_name,
statics.order_names)) == -1) {
- char **pp, msg[80];
+ char **pp, msg[512];
snprintf(msg, sizeof(msg),
"'%s' is not a recognized sorting order",
diff --git a/usr.bin/top/utils.c b/usr.bin/top/utils.c
index 8b17855abc6..4a1f2a7e4ef 100644
--- a/usr.bin/top/utils.c
+++ b/usr.bin/top/utils.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: utils.c,v 1.21 2007/10/01 09:15:44 otto Exp $ */
+/* $OpenBSD: utils.c,v 1.22 2010/01/29 00:36:09 tedu Exp $ */
/*
* Top users/processes display for Unix
@@ -125,7 +125,7 @@ string_index(char *string, char **array)
int i = 0;
while (*array != NULL) {
- if (strcmp(string, *array) == 0)
+ if (strncmp(string, *array, strlen(string)) == 0)
return (i);
array++;
i++;