aboutsummaryrefslogtreecommitdiff
path: root/bin/ps/ps.c
diff options
context:
space:
mode:
authorGarance A Drosehn <gad@FreeBSD.org>2004-03-30 01:45:23 +0000
committerGarance A Drosehn <gad@FreeBSD.org>2004-03-30 01:45:23 +0000
commit5bd7b1f3c002b5c6b228dd52e3e3950d1e402fa4 (patch)
treef7f749f18392113e82728db8618c65bc862e8a34 /bin/ps/ps.c
parent653681fb30fea37af2d8c9b9f9f7dd06a2f9542b (diff)
downloadsrc-5bd7b1f3c002b5c6b228dd52e3e3950d1e402fa4.tar.gz
src-5bd7b1f3c002b5c6b228dd52e3e3950d1e402fa4.zip
Replace pscomp() with a cleaner version, mostly written by bde (*).
This corrects a problem of lost-precision for `-r' (sort-by-CPU). Also, for sort-by-CPU and sort-by-memory, any processes which have the same value CPU or MEMORY are now sorted by TTY and then (if needed) by pid. (* - I just added the NODEV checks, after doing some testing of my own) Submitted by: bde MFC after: 1 week
Notes
Notes: svn path=/head/; revision=127596
Diffstat (limited to 'bin/ps/ps.c')
-rw-r--r--bin/ps/ps.c57
1 files changed, 43 insertions, 14 deletions
diff --git a/bin/ps/ps.c b/bin/ps/ps.c
index 183c0febdda1..7db9dce4c2dc 100644
--- a/bin/ps/ps.c
+++ b/bin/ps/ps.c
@@ -1024,20 +1024,49 @@ saveuser(KINFO *ki)
static int
pscomp(const void *a, const void *b)
{
- int i;
-#define VSIZE(k) ((k)->ki_p->ki_dsize + (k)->ki_p->ki_ssize + \
- (k)->ki_p->ki_tsize)
-
- if (sortby == SORTCPU)
- return (getpcpu((const KINFO *)b) - getpcpu((const KINFO *)a));
- if (sortby == SORTMEM)
- return (VSIZE((const KINFO *)b) - VSIZE((const KINFO *)a));
- i = (int)((const KINFO *)a)->ki_p->ki_tdev -
- (int)((const KINFO *)b)->ki_p->ki_tdev;
- if (i == 0)
- i = ((const KINFO *)a)->ki_p->ki_pid -
- ((const KINFO *)b)->ki_p->ki_pid;
- return (i);
+ const KINFO *ka, *kb;
+ double cpua, cpub;
+ segsz_t sizea, sizeb;
+
+ ka = a;
+ kb = b;
+ /* SORTCPU and SORTMEM are sorted in descending order. */
+ if (sortby == SORTCPU) {
+ cpua = getpcpu(ka);
+ cpub = getpcpu(kb);
+ if (cpua < cpub)
+ return (1);
+ if (cpua > cpub)
+ return (-1);
+ }
+ if (sortby == SORTMEM) {
+ sizea = ka->ki_p->ki_tsize + ka->ki_p->ki_dsize +
+ ka->ki_p->ki_ssize;
+ sizeb = kb->ki_p->ki_tsize + kb->ki_p->ki_dsize +
+ kb->ki_p->ki_ssize;
+ if (sizea < sizeb)
+ return (1);
+ if (sizea > sizeb)
+ return (-1);
+ }
+ /*
+ * TTY's are sorted in ascending order, except that all NODEV
+ * processes come before all processes with a device.
+ */
+ if (ka->ki_p->ki_tdev == NODEV && kb->ki_p->ki_tdev != NODEV)
+ return (-1);
+ if (ka->ki_p->ki_tdev != NODEV && kb->ki_p->ki_tdev == NODEV)
+ return (1);
+ if (ka->ki_p->ki_tdev < kb->ki_p->ki_tdev)
+ return (-1);
+ if (ka->ki_p->ki_tdev > kb->ki_p->ki_tdev)
+ return (1);
+ /* PID's are sorted in ascending order. */
+ if (ka->ki_p->ki_pid < kb->ki_p->ki_pid)
+ return (-1);
+ if (ka->ki_p->ki_pid > kb->ki_p->ki_pid)
+ return (1);
+ return (0);
}
/*