summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorgad <gad@FreeBSD.org>2004-06-20 21:25:10 +0000
committergad <gad@FreeBSD.org>2004-06-20 21:25:10 +0000
commit45865285541caea17e34a97f58f91c98d990ceb2 (patch)
treea145b6bece79fccf10f74f0b4a9d40fc22b3eabd /bin
parent0d2c5309add400692dc414bfd73e76d49fb9d111 (diff)
downloadFreeBSD-src-45865285541caea17e34a97f58f91c98d990ceb2.zip
FreeBSD-src-45865285541caea17e34a97f58f91c98d990ceb2.tar.gz
Have the main() routine calculate %CPU and (if needed) memory information
when copying per-process info before starting to sort the list. This way, sort-by-CPU or sort-by-memory will only calculate values once-per-process, instead of twice-per-comparison. Also take advantage of this to simplify the pscomp() routine.
Diffstat (limited to 'bin')
-rw-r--r--bin/ps/ps.c65
-rw-r--r--bin/ps/ps.h2
2 files changed, 31 insertions, 36 deletions
diff --git a/bin/ps/ps.c b/bin/ps/ps.c
index 58bad10..98e20f4 100644
--- a/bin/ps/ps.c
+++ b/bin/ps/ps.c
@@ -166,6 +166,7 @@ main(int argc, char *argv[])
struct listinfo gidlist, pgrplist, pidlist;
struct listinfo ruidlist, sesslist, ttylist, uidlist;
struct kinfo_proc *kp;
+ KINFO *next_KINFO;
struct varent *vent;
struct winsize ws;
const char *nlistf, *memf;
@@ -569,10 +570,15 @@ main(int argc, char *argv[])
continue;
keepit:
- kinfo[nkept].ki_p = kp;
+ next_KINFO = &kinfo[nkept];
+ next_KINFO->ki_p = kp;
+ next_KINFO->ki_pcpu = getpcpu(next_KINFO);
+ if (sortby == SORTMEM)
+ next_KINFO->ki_memsize = kp->ki_tsize +
+ kp->ki_dsize + kp->ki_ssize;
if (needuser)
- saveuser(&kinfo[nkept]);
- dynsizevars(&kinfo[nkept]);
+ saveuser(next_KINFO);
+ dynsizevars(next_KINFO);
nkept++;
}
}
@@ -989,53 +995,40 @@ saveuser(KINFO *ki)
}
}
+/* A macro used to improve the readability of pscomp(). */
+#define DIFF_RETURN(a, b, field) do { \
+ if ((a)->field != (b)->field) \
+ return (((a)->field < (b)->field) ? -1 : 1); \
+} while (0)
+
static int
pscomp(const void *a, const void *b)
{
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);
- }
+ if (sortby == SORTCPU)
+ DIFF_RETURN(kb, ka, ki_pcpu);
+ if (sortby == SORTMEM)
+ DIFF_RETURN(kb, ka, ki_memsize);
/*
* 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);
+ if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) {
+ if (ka->ki_p->ki_tdev == NODEV)
+ return (-1);
+ if (kb->ki_p->ki_tdev == NODEV)
+ return (1);
+ DIFF_RETURN(ka, kb, ki_p->ki_tdev);
+ }
+
+ DIFF_RETURN(ka, kb, ki_p->ki_pid);
return (0);
}
+#undef DIFF_RETURN
/*
* ICK (all for getopt), would rather hide the ugliness
diff --git a/bin/ps/ps.h b/bin/ps/ps.h
index 46d017c..817719d 100644
--- a/bin/ps/ps.h
+++ b/bin/ps/ps.h
@@ -38,6 +38,8 @@ typedef struct kinfo {
char *ki_args; /* exec args */
char *ki_env; /* environment */
int ki_valid; /* 1 => uarea stuff valid */
+ double ki_pcpu; /* calculated in main() */
+ segsz_t ki_memsize; /* calculated in main() */
} KINFO;
/* Variables. */
OpenPOWER on IntegriCloud