1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino * Copyright (c) 1994 Christopher G. Demetriou
3*86d7f5d3SJohn Marino * All rights reserved.
4*86d7f5d3SJohn Marino *
5*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino * are met:
8*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino * 3. All advertising materials mentioning features or use of this software
14*86d7f5d3SJohn Marino * must display the following acknowledgement:
15*86d7f5d3SJohn Marino * This product includes software developed by Christopher G. Demetriou.
16*86d7f5d3SJohn Marino * 4. The name of the author may not be used to endorse or promote products
17*86d7f5d3SJohn Marino * derived from this software without specific prior written permission
18*86d7f5d3SJohn Marino *
19*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*86d7f5d3SJohn Marino * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*86d7f5d3SJohn Marino * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*86d7f5d3SJohn Marino * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*86d7f5d3SJohn Marino * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*86d7f5d3SJohn Marino * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*86d7f5d3SJohn Marino * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*86d7f5d3SJohn Marino * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*86d7f5d3SJohn Marino * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*86d7f5d3SJohn Marino * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*86d7f5d3SJohn Marino *
30*86d7f5d3SJohn Marino * @(#) Copyright (c) 1994 Christopher G. Demetriou All rights reserved.
31*86d7f5d3SJohn Marino * $FreeBSD: src/usr.sbin/sa/main.c,v 1.8.2.2 2001/07/19 05:20:49 kris Exp $
32*86d7f5d3SJohn Marino */
33*86d7f5d3SJohn Marino
34*86d7f5d3SJohn Marino /*
35*86d7f5d3SJohn Marino * sa: system accounting
36*86d7f5d3SJohn Marino */
37*86d7f5d3SJohn Marino
38*86d7f5d3SJohn Marino #include <sys/types.h>
39*86d7f5d3SJohn Marino #include <sys/acct.h>
40*86d7f5d3SJohn Marino #include <ctype.h>
41*86d7f5d3SJohn Marino #include <err.h>
42*86d7f5d3SJohn Marino #include <fcntl.h>
43*86d7f5d3SJohn Marino #include <signal.h>
44*86d7f5d3SJohn Marino #include <stdio.h>
45*86d7f5d3SJohn Marino #include <stdlib.h>
46*86d7f5d3SJohn Marino #include <string.h>
47*86d7f5d3SJohn Marino #include <unistd.h>
48*86d7f5d3SJohn Marino #include "extern.h"
49*86d7f5d3SJohn Marino #include "pathnames.h"
50*86d7f5d3SJohn Marino
51*86d7f5d3SJohn Marino static int acct_load(char *, int);
52*86d7f5d3SJohn Marino static u_quad_t decode_comp_t(comp_t);
53*86d7f5d3SJohn Marino static int cmp_comm(const char *, const char *);
54*86d7f5d3SJohn Marino static int cmp_usrsys(const DBT *, const DBT *);
55*86d7f5d3SJohn Marino static int cmp_avgusrsys(const DBT *, const DBT *);
56*86d7f5d3SJohn Marino static int cmp_dkio(const DBT *, const DBT *);
57*86d7f5d3SJohn Marino static int cmp_avgdkio(const DBT *, const DBT *);
58*86d7f5d3SJohn Marino static int cmp_cpumem(const DBT *, const DBT *);
59*86d7f5d3SJohn Marino static int cmp_avgcpumem(const DBT *, const DBT *);
60*86d7f5d3SJohn Marino static int cmp_calls(const DBT *, const DBT *);
61*86d7f5d3SJohn Marino static void usage (void);
62*86d7f5d3SJohn Marino
63*86d7f5d3SJohn Marino int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
64*86d7f5d3SJohn Marino int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
65*86d7f5d3SJohn Marino u_quad_t cutoff = 1;
66*86d7f5d3SJohn Marino
67*86d7f5d3SJohn Marino static char *dfltargv[] = { NULL };
68*86d7f5d3SJohn Marino static int dfltargc = (sizeof dfltargv/sizeof(char *));
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino /* default to comparing by sum of user + system time */
71*86d7f5d3SJohn Marino cmpf_t sa_cmp = cmp_usrsys;
72*86d7f5d3SJohn Marino
73*86d7f5d3SJohn Marino int
main(int argc,char ** argv)74*86d7f5d3SJohn Marino main(int argc, char **argv)
75*86d7f5d3SJohn Marino {
76*86d7f5d3SJohn Marino char ch;
77*86d7f5d3SJohn Marino char pathacct[] = _PATH_ACCT;
78*86d7f5d3SJohn Marino int error = 0;
79*86d7f5d3SJohn Marino
80*86d7f5d3SJohn Marino dfltargv[0] = pathacct;
81*86d7f5d3SJohn Marino
82*86d7f5d3SJohn Marino while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1)
83*86d7f5d3SJohn Marino switch (ch) {
84*86d7f5d3SJohn Marino case 'a':
85*86d7f5d3SJohn Marino /* print all commands */
86*86d7f5d3SJohn Marino aflag = 1;
87*86d7f5d3SJohn Marino break;
88*86d7f5d3SJohn Marino case 'b':
89*86d7f5d3SJohn Marino /* sort by per-call user/system time average */
90*86d7f5d3SJohn Marino bflag = 1;
91*86d7f5d3SJohn Marino sa_cmp = cmp_avgusrsys;
92*86d7f5d3SJohn Marino break;
93*86d7f5d3SJohn Marino case 'c':
94*86d7f5d3SJohn Marino /* print percentage total time */
95*86d7f5d3SJohn Marino cflag = 1;
96*86d7f5d3SJohn Marino break;
97*86d7f5d3SJohn Marino case 'd':
98*86d7f5d3SJohn Marino /* sort by averge number of disk I/O ops */
99*86d7f5d3SJohn Marino dflag = 1;
100*86d7f5d3SJohn Marino sa_cmp = cmp_avgdkio;
101*86d7f5d3SJohn Marino break;
102*86d7f5d3SJohn Marino case 'D':
103*86d7f5d3SJohn Marino /* print and sort by total disk I/O ops */
104*86d7f5d3SJohn Marino Dflag = 1;
105*86d7f5d3SJohn Marino sa_cmp = cmp_dkio;
106*86d7f5d3SJohn Marino break;
107*86d7f5d3SJohn Marino case 'f':
108*86d7f5d3SJohn Marino /* force no interactive threshold comprison */
109*86d7f5d3SJohn Marino fflag = 1;
110*86d7f5d3SJohn Marino break;
111*86d7f5d3SJohn Marino case 'i':
112*86d7f5d3SJohn Marino /* do not read in summary file */
113*86d7f5d3SJohn Marino iflag = 1;
114*86d7f5d3SJohn Marino break;
115*86d7f5d3SJohn Marino case 'j':
116*86d7f5d3SJohn Marino /* instead of total minutes, give sec/call */
117*86d7f5d3SJohn Marino jflag = 1;
118*86d7f5d3SJohn Marino break;
119*86d7f5d3SJohn Marino case 'k':
120*86d7f5d3SJohn Marino /* sort by cpu-time average memory usage */
121*86d7f5d3SJohn Marino kflag = 1;
122*86d7f5d3SJohn Marino sa_cmp = cmp_avgcpumem;
123*86d7f5d3SJohn Marino break;
124*86d7f5d3SJohn Marino case 'K':
125*86d7f5d3SJohn Marino /* print and sort by cpu-storage integral */
126*86d7f5d3SJohn Marino sa_cmp = cmp_cpumem;
127*86d7f5d3SJohn Marino Kflag = 1;
128*86d7f5d3SJohn Marino break;
129*86d7f5d3SJohn Marino case 'l':
130*86d7f5d3SJohn Marino /* separate system and user time */
131*86d7f5d3SJohn Marino lflag = 1;
132*86d7f5d3SJohn Marino break;
133*86d7f5d3SJohn Marino case 'm':
134*86d7f5d3SJohn Marino /* print procs and time per-user */
135*86d7f5d3SJohn Marino mflag = 1;
136*86d7f5d3SJohn Marino break;
137*86d7f5d3SJohn Marino case 'n':
138*86d7f5d3SJohn Marino /* sort by number of calls */
139*86d7f5d3SJohn Marino sa_cmp = cmp_calls;
140*86d7f5d3SJohn Marino break;
141*86d7f5d3SJohn Marino case 'q':
142*86d7f5d3SJohn Marino /* quiet; error messages only */
143*86d7f5d3SJohn Marino qflag = 1;
144*86d7f5d3SJohn Marino break;
145*86d7f5d3SJohn Marino case 'r':
146*86d7f5d3SJohn Marino /* reverse order of sort */
147*86d7f5d3SJohn Marino rflag = 1;
148*86d7f5d3SJohn Marino break;
149*86d7f5d3SJohn Marino case 's':
150*86d7f5d3SJohn Marino /* merge accounting file into summaries */
151*86d7f5d3SJohn Marino sflag = 1;
152*86d7f5d3SJohn Marino break;
153*86d7f5d3SJohn Marino case 't':
154*86d7f5d3SJohn Marino /* report ratio of user and system times */
155*86d7f5d3SJohn Marino tflag = 1;
156*86d7f5d3SJohn Marino break;
157*86d7f5d3SJohn Marino case 'u':
158*86d7f5d3SJohn Marino /* first, print uid and command name */
159*86d7f5d3SJohn Marino uflag = 1;
160*86d7f5d3SJohn Marino break;
161*86d7f5d3SJohn Marino case 'v':
162*86d7f5d3SJohn Marino /* cull junk */
163*86d7f5d3SJohn Marino vflag = 1;
164*86d7f5d3SJohn Marino cutoff = atoi(optarg);
165*86d7f5d3SJohn Marino break;
166*86d7f5d3SJohn Marino case '?':
167*86d7f5d3SJohn Marino default:
168*86d7f5d3SJohn Marino usage();
169*86d7f5d3SJohn Marino }
170*86d7f5d3SJohn Marino
171*86d7f5d3SJohn Marino argc -= optind;
172*86d7f5d3SJohn Marino argv += optind;
173*86d7f5d3SJohn Marino
174*86d7f5d3SJohn Marino /* various argument checking */
175*86d7f5d3SJohn Marino if (fflag && !vflag)
176*86d7f5d3SJohn Marino errx(1, "only one of -f requires -v");
177*86d7f5d3SJohn Marino if (fflag && aflag)
178*86d7f5d3SJohn Marino errx(1, "only one of -a and -v may be specified");
179*86d7f5d3SJohn Marino /* XXX need more argument checking */
180*86d7f5d3SJohn Marino
181*86d7f5d3SJohn Marino if (!uflag) {
182*86d7f5d3SJohn Marino /* initialize tables */
183*86d7f5d3SJohn Marino if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
184*86d7f5d3SJohn Marino errx(1, "process accounting initialization failed");
185*86d7f5d3SJohn Marino if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
186*86d7f5d3SJohn Marino errx(1, "user accounting initialization failed");
187*86d7f5d3SJohn Marino }
188*86d7f5d3SJohn Marino
189*86d7f5d3SJohn Marino if (argc == 0) {
190*86d7f5d3SJohn Marino argc = dfltargc;
191*86d7f5d3SJohn Marino argv = dfltargv;
192*86d7f5d3SJohn Marino }
193*86d7f5d3SJohn Marino
194*86d7f5d3SJohn Marino /* for each file specified */
195*86d7f5d3SJohn Marino for (; argc > 0; argc--, argv++) {
196*86d7f5d3SJohn Marino int fd;
197*86d7f5d3SJohn Marino
198*86d7f5d3SJohn Marino /*
199*86d7f5d3SJohn Marino * load the accounting data from the file.
200*86d7f5d3SJohn Marino * if it fails, go on to the next file.
201*86d7f5d3SJohn Marino */
202*86d7f5d3SJohn Marino fd = acct_load(argv[0], sflag);
203*86d7f5d3SJohn Marino if (fd < 0)
204*86d7f5d3SJohn Marino continue;
205*86d7f5d3SJohn Marino
206*86d7f5d3SJohn Marino if (!uflag && sflag) {
207*86d7f5d3SJohn Marino #ifndef DEBUG
208*86d7f5d3SJohn Marino sigset_t nmask, omask;
209*86d7f5d3SJohn Marino int unmask = 1;
210*86d7f5d3SJohn Marino
211*86d7f5d3SJohn Marino /*
212*86d7f5d3SJohn Marino * block most signals so we aren't interrupted during
213*86d7f5d3SJohn Marino * the update.
214*86d7f5d3SJohn Marino */
215*86d7f5d3SJohn Marino if (sigfillset(&nmask) == -1) {
216*86d7f5d3SJohn Marino warn("sigfillset");
217*86d7f5d3SJohn Marino unmask = 0;
218*86d7f5d3SJohn Marino error = 1;
219*86d7f5d3SJohn Marino }
220*86d7f5d3SJohn Marino if (unmask &&
221*86d7f5d3SJohn Marino (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
222*86d7f5d3SJohn Marino warn("couldn't set signal mask");
223*86d7f5d3SJohn Marino unmask = 0;
224*86d7f5d3SJohn Marino error = 1;
225*86d7f5d3SJohn Marino }
226*86d7f5d3SJohn Marino #endif /* DEBUG */
227*86d7f5d3SJohn Marino
228*86d7f5d3SJohn Marino /*
229*86d7f5d3SJohn Marino * truncate the accounting data file ASAP, to avoid
230*86d7f5d3SJohn Marino * losing data. don't worry about errors in updating
231*86d7f5d3SJohn Marino * the saved stats; better to underbill than overbill,
232*86d7f5d3SJohn Marino * but we want every accounting record intact.
233*86d7f5d3SJohn Marino */
234*86d7f5d3SJohn Marino if (ftruncate(fd, 0) == -1) {
235*86d7f5d3SJohn Marino warn("couldn't truncate %s", argv[0]);
236*86d7f5d3SJohn Marino error = 1;
237*86d7f5d3SJohn Marino }
238*86d7f5d3SJohn Marino
239*86d7f5d3SJohn Marino /*
240*86d7f5d3SJohn Marino * update saved user and process accounting data.
241*86d7f5d3SJohn Marino * note errors for later.
242*86d7f5d3SJohn Marino */
243*86d7f5d3SJohn Marino if (pacct_update() != 0 || usracct_update() != 0)
244*86d7f5d3SJohn Marino error = 1;
245*86d7f5d3SJohn Marino
246*86d7f5d3SJohn Marino #ifndef DEBUG
247*86d7f5d3SJohn Marino /*
248*86d7f5d3SJohn Marino * restore signals
249*86d7f5d3SJohn Marino */
250*86d7f5d3SJohn Marino if (unmask &&
251*86d7f5d3SJohn Marino (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
252*86d7f5d3SJohn Marino warn("couldn't restore signal mask");
253*86d7f5d3SJohn Marino error = 1;
254*86d7f5d3SJohn Marino }
255*86d7f5d3SJohn Marino #endif /* DEBUG */
256*86d7f5d3SJohn Marino }
257*86d7f5d3SJohn Marino
258*86d7f5d3SJohn Marino /*
259*86d7f5d3SJohn Marino * close the opened accounting file
260*86d7f5d3SJohn Marino */
261*86d7f5d3SJohn Marino if (close(fd) == -1) {
262*86d7f5d3SJohn Marino warn("close %s", argv[0]);
263*86d7f5d3SJohn Marino error = 1;
264*86d7f5d3SJohn Marino }
265*86d7f5d3SJohn Marino }
266*86d7f5d3SJohn Marino
267*86d7f5d3SJohn Marino if (!uflag && !qflag) {
268*86d7f5d3SJohn Marino /* print any results we may have obtained. */
269*86d7f5d3SJohn Marino if (!mflag)
270*86d7f5d3SJohn Marino pacct_print();
271*86d7f5d3SJohn Marino else
272*86d7f5d3SJohn Marino usracct_print();
273*86d7f5d3SJohn Marino }
274*86d7f5d3SJohn Marino
275*86d7f5d3SJohn Marino if (!uflag) {
276*86d7f5d3SJohn Marino /* finally, deallocate databases */
277*86d7f5d3SJohn Marino if (sflag || (!mflag && !qflag))
278*86d7f5d3SJohn Marino pacct_destroy();
279*86d7f5d3SJohn Marino if (sflag || (mflag && !qflag))
280*86d7f5d3SJohn Marino usracct_destroy();
281*86d7f5d3SJohn Marino }
282*86d7f5d3SJohn Marino
283*86d7f5d3SJohn Marino exit(error);
284*86d7f5d3SJohn Marino }
285*86d7f5d3SJohn Marino
286*86d7f5d3SJohn Marino static void
usage(void)287*86d7f5d3SJohn Marino usage(void)
288*86d7f5d3SJohn Marino {
289*86d7f5d3SJohn Marino fprintf(stderr,
290*86d7f5d3SJohn Marino "usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n");
291*86d7f5d3SJohn Marino exit(1);
292*86d7f5d3SJohn Marino }
293*86d7f5d3SJohn Marino
294*86d7f5d3SJohn Marino static int
acct_load(char * pn,int wr)295*86d7f5d3SJohn Marino acct_load(char *pn, int wr)
296*86d7f5d3SJohn Marino {
297*86d7f5d3SJohn Marino struct acct ac;
298*86d7f5d3SJohn Marino struct cmdinfo ci;
299*86d7f5d3SJohn Marino ssize_t rv;
300*86d7f5d3SJohn Marino int fd, i;
301*86d7f5d3SJohn Marino
302*86d7f5d3SJohn Marino /*
303*86d7f5d3SJohn Marino * open the file
304*86d7f5d3SJohn Marino */
305*86d7f5d3SJohn Marino fd = open(pn, wr ? O_RDWR : O_RDONLY, 0);
306*86d7f5d3SJohn Marino if (fd == -1) {
307*86d7f5d3SJohn Marino warn("open %s %s", pn, wr ? "for read/write" : "read-only");
308*86d7f5d3SJohn Marino return (-1);
309*86d7f5d3SJohn Marino }
310*86d7f5d3SJohn Marino
311*86d7f5d3SJohn Marino /*
312*86d7f5d3SJohn Marino * read all we can; don't stat and open because more processes
313*86d7f5d3SJohn Marino * could exit, and we'd miss them
314*86d7f5d3SJohn Marino */
315*86d7f5d3SJohn Marino while (1) {
316*86d7f5d3SJohn Marino /* get one accounting entry and punt if there's an error */
317*86d7f5d3SJohn Marino rv = read(fd, &ac, sizeof(struct acct));
318*86d7f5d3SJohn Marino if (rv == -1)
319*86d7f5d3SJohn Marino warn("error reading %s", pn);
320*86d7f5d3SJohn Marino else if (rv > 0 && rv < (int)sizeof(struct acct))
321*86d7f5d3SJohn Marino warnx("short read of accounting data in %s", pn);
322*86d7f5d3SJohn Marino if (rv != sizeof(struct acct))
323*86d7f5d3SJohn Marino break;
324*86d7f5d3SJohn Marino
325*86d7f5d3SJohn Marino /* decode it */
326*86d7f5d3SJohn Marino ci.ci_calls = 1;
327*86d7f5d3SJohn Marino for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
328*86d7f5d3SJohn Marino i++) {
329*86d7f5d3SJohn Marino char c = ac.ac_comm[i];
330*86d7f5d3SJohn Marino
331*86d7f5d3SJohn Marino if (!isascii(c) || iscntrl(c)) {
332*86d7f5d3SJohn Marino ci.ci_comm[i] = '?';
333*86d7f5d3SJohn Marino ci.ci_flags |= CI_UNPRINTABLE;
334*86d7f5d3SJohn Marino } else
335*86d7f5d3SJohn Marino ci.ci_comm[i] = c;
336*86d7f5d3SJohn Marino }
337*86d7f5d3SJohn Marino if (ac.ac_flag & AFORK)
338*86d7f5d3SJohn Marino ci.ci_comm[i++] = '*';
339*86d7f5d3SJohn Marino ci.ci_comm[i++] = '\0';
340*86d7f5d3SJohn Marino ci.ci_etime = decode_comp_t(ac.ac_etime);
341*86d7f5d3SJohn Marino ci.ci_utime = decode_comp_t(ac.ac_utime);
342*86d7f5d3SJohn Marino ci.ci_stime = decode_comp_t(ac.ac_stime);
343*86d7f5d3SJohn Marino ci.ci_uid = ac.ac_uid;
344*86d7f5d3SJohn Marino ci.ci_mem = ac.ac_mem;
345*86d7f5d3SJohn Marino ci.ci_io = decode_comp_t(ac.ac_io) / AHZ;
346*86d7f5d3SJohn Marino
347*86d7f5d3SJohn Marino if (!uflag) {
348*86d7f5d3SJohn Marino /* and enter it into the usracct and pacct databases */
349*86d7f5d3SJohn Marino if (sflag || (!mflag && !qflag))
350*86d7f5d3SJohn Marino pacct_add(&ci);
351*86d7f5d3SJohn Marino if (sflag || (mflag && !qflag))
352*86d7f5d3SJohn Marino usracct_add(&ci);
353*86d7f5d3SJohn Marino } else if (!qflag)
354*86d7f5d3SJohn Marino printf("%6lu %12.2f cpu %12juk mem %12ju io %s\n",
355*86d7f5d3SJohn Marino ci.ci_uid,
356*86d7f5d3SJohn Marino (ci.ci_utime + ci.ci_stime) / (double) AHZ,
357*86d7f5d3SJohn Marino (uintmax_t)ci.ci_mem, (uintmax_t)ci.ci_io,
358*86d7f5d3SJohn Marino ci.ci_comm);
359*86d7f5d3SJohn Marino }
360*86d7f5d3SJohn Marino
361*86d7f5d3SJohn Marino /* finally, return the file descriptor for possible truncation */
362*86d7f5d3SJohn Marino return (fd);
363*86d7f5d3SJohn Marino }
364*86d7f5d3SJohn Marino
365*86d7f5d3SJohn Marino static u_quad_t
decode_comp_t(comp_t comp)366*86d7f5d3SJohn Marino decode_comp_t(comp_t comp)
367*86d7f5d3SJohn Marino {
368*86d7f5d3SJohn Marino u_quad_t rv;
369*86d7f5d3SJohn Marino
370*86d7f5d3SJohn Marino /*
371*86d7f5d3SJohn Marino * for more info on the comp_t format, see:
372*86d7f5d3SJohn Marino * /usr/src/sys/kern/kern_acct.c
373*86d7f5d3SJohn Marino * /usr/src/sys/sys/acct.h
374*86d7f5d3SJohn Marino * /usr/src/usr.bin/lastcomm/lastcomm.c
375*86d7f5d3SJohn Marino */
376*86d7f5d3SJohn Marino rv = comp & 0x1fff; /* 13 bit fraction */
377*86d7f5d3SJohn Marino comp >>= 13; /* 3 bit base-8 exponent */
378*86d7f5d3SJohn Marino while (comp--)
379*86d7f5d3SJohn Marino rv <<= 3;
380*86d7f5d3SJohn Marino
381*86d7f5d3SJohn Marino return (rv);
382*86d7f5d3SJohn Marino }
383*86d7f5d3SJohn Marino
384*86d7f5d3SJohn Marino /* sort commands, doing the right thing in terms of reversals */
385*86d7f5d3SJohn Marino static int
cmp_comm(const char * s1,const char * s2)386*86d7f5d3SJohn Marino cmp_comm(const char *s1, const char *s2)
387*86d7f5d3SJohn Marino {
388*86d7f5d3SJohn Marino int rv;
389*86d7f5d3SJohn Marino
390*86d7f5d3SJohn Marino rv = strcmp(s1, s2);
391*86d7f5d3SJohn Marino if (rv == 0)
392*86d7f5d3SJohn Marino rv = -1;
393*86d7f5d3SJohn Marino return (rflag ? rv : -rv);
394*86d7f5d3SJohn Marino }
395*86d7f5d3SJohn Marino
396*86d7f5d3SJohn Marino /* sort by total user and system time */
397*86d7f5d3SJohn Marino static int
cmp_usrsys(const DBT * d1,const DBT * d2)398*86d7f5d3SJohn Marino cmp_usrsys(const DBT *d1, const DBT *d2)
399*86d7f5d3SJohn Marino {
400*86d7f5d3SJohn Marino struct cmdinfo c1, c2;
401*86d7f5d3SJohn Marino u_quad_t t1, t2;
402*86d7f5d3SJohn Marino
403*86d7f5d3SJohn Marino memcpy(&c1, d1->data, sizeof(c1));
404*86d7f5d3SJohn Marino memcpy(&c2, d2->data, sizeof(c2));
405*86d7f5d3SJohn Marino
406*86d7f5d3SJohn Marino t1 = c1.ci_utime + c1.ci_stime;
407*86d7f5d3SJohn Marino t2 = c2.ci_utime + c2.ci_stime;
408*86d7f5d3SJohn Marino
409*86d7f5d3SJohn Marino if (t1 < t2)
410*86d7f5d3SJohn Marino return -1;
411*86d7f5d3SJohn Marino else if (t1 == t2)
412*86d7f5d3SJohn Marino return (cmp_comm(c1.ci_comm, c2.ci_comm));
413*86d7f5d3SJohn Marino else
414*86d7f5d3SJohn Marino return 1;
415*86d7f5d3SJohn Marino }
416*86d7f5d3SJohn Marino
417*86d7f5d3SJohn Marino /* sort by average user and system time */
418*86d7f5d3SJohn Marino static int
cmp_avgusrsys(const DBT * d1,const DBT * d2)419*86d7f5d3SJohn Marino cmp_avgusrsys(const DBT *d1, const DBT *d2)
420*86d7f5d3SJohn Marino {
421*86d7f5d3SJohn Marino struct cmdinfo c1, c2;
422*86d7f5d3SJohn Marino double t1, t2;
423*86d7f5d3SJohn Marino
424*86d7f5d3SJohn Marino memcpy(&c1, d1->data, sizeof(c1));
425*86d7f5d3SJohn Marino memcpy(&c2, d2->data, sizeof(c2));
426*86d7f5d3SJohn Marino
427*86d7f5d3SJohn Marino t1 = c1.ci_utime + c1.ci_stime;
428*86d7f5d3SJohn Marino t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
429*86d7f5d3SJohn Marino
430*86d7f5d3SJohn Marino t2 = c2.ci_utime + c2.ci_stime;
431*86d7f5d3SJohn Marino t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
432*86d7f5d3SJohn Marino
433*86d7f5d3SJohn Marino if (t1 < t2)
434*86d7f5d3SJohn Marino return -1;
435*86d7f5d3SJohn Marino else if (t1 == t2)
436*86d7f5d3SJohn Marino return (cmp_comm(c1.ci_comm, c2.ci_comm));
437*86d7f5d3SJohn Marino else
438*86d7f5d3SJohn Marino return 1;
439*86d7f5d3SJohn Marino }
440*86d7f5d3SJohn Marino
441*86d7f5d3SJohn Marino /* sort by total number of disk I/O operations */
442*86d7f5d3SJohn Marino static int
cmp_dkio(const DBT * d1,const DBT * d2)443*86d7f5d3SJohn Marino cmp_dkio(const DBT *d1, const DBT *d2)
444*86d7f5d3SJohn Marino {
445*86d7f5d3SJohn Marino struct cmdinfo c1, c2;
446*86d7f5d3SJohn Marino
447*86d7f5d3SJohn Marino memcpy(&c1, d1->data, sizeof(c1));
448*86d7f5d3SJohn Marino memcpy(&c2, d2->data, sizeof(c2));
449*86d7f5d3SJohn Marino
450*86d7f5d3SJohn Marino if (c1.ci_io < c2.ci_io)
451*86d7f5d3SJohn Marino return -1;
452*86d7f5d3SJohn Marino else if (c1.ci_io == c2.ci_io)
453*86d7f5d3SJohn Marino return (cmp_comm(c1.ci_comm, c2.ci_comm));
454*86d7f5d3SJohn Marino else
455*86d7f5d3SJohn Marino return 1;
456*86d7f5d3SJohn Marino }
457*86d7f5d3SJohn Marino
458*86d7f5d3SJohn Marino /* sort by average number of disk I/O operations */
459*86d7f5d3SJohn Marino static int
cmp_avgdkio(const DBT * d1,const DBT * d2)460*86d7f5d3SJohn Marino cmp_avgdkio(const DBT *d1, const DBT *d2)
461*86d7f5d3SJohn Marino {
462*86d7f5d3SJohn Marino struct cmdinfo c1, c2;
463*86d7f5d3SJohn Marino double n1, n2;
464*86d7f5d3SJohn Marino
465*86d7f5d3SJohn Marino memcpy(&c1, d1->data, sizeof(c1));
466*86d7f5d3SJohn Marino memcpy(&c2, d2->data, sizeof(c2));
467*86d7f5d3SJohn Marino
468*86d7f5d3SJohn Marino n1 = (double) c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
469*86d7f5d3SJohn Marino n2 = (double) c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
470*86d7f5d3SJohn Marino
471*86d7f5d3SJohn Marino if (n1 < n2)
472*86d7f5d3SJohn Marino return -1;
473*86d7f5d3SJohn Marino else if (n1 == n2)
474*86d7f5d3SJohn Marino return (cmp_comm(c1.ci_comm, c2.ci_comm));
475*86d7f5d3SJohn Marino else
476*86d7f5d3SJohn Marino return 1;
477*86d7f5d3SJohn Marino }
478*86d7f5d3SJohn Marino
479*86d7f5d3SJohn Marino /* sort by the cpu-storage integral */
480*86d7f5d3SJohn Marino static int
cmp_cpumem(const DBT * d1,const DBT * d2)481*86d7f5d3SJohn Marino cmp_cpumem(const DBT *d1, const DBT *d2)
482*86d7f5d3SJohn Marino {
483*86d7f5d3SJohn Marino struct cmdinfo c1, c2;
484*86d7f5d3SJohn Marino
485*86d7f5d3SJohn Marino memcpy(&c1, d1->data, sizeof(c1));
486*86d7f5d3SJohn Marino memcpy(&c2, d2->data, sizeof(c2));
487*86d7f5d3SJohn Marino
488*86d7f5d3SJohn Marino if (c1.ci_mem < c2.ci_mem)
489*86d7f5d3SJohn Marino return -1;
490*86d7f5d3SJohn Marino else if (c1.ci_mem == c2.ci_mem)
491*86d7f5d3SJohn Marino return (cmp_comm(c1.ci_comm, c2.ci_comm));
492*86d7f5d3SJohn Marino else
493*86d7f5d3SJohn Marino return 1;
494*86d7f5d3SJohn Marino }
495*86d7f5d3SJohn Marino
496*86d7f5d3SJohn Marino /* sort by the cpu-time average memory usage */
497*86d7f5d3SJohn Marino static int
cmp_avgcpumem(const DBT * d1,const DBT * d2)498*86d7f5d3SJohn Marino cmp_avgcpumem(const DBT *d1, const DBT *d2)
499*86d7f5d3SJohn Marino {
500*86d7f5d3SJohn Marino struct cmdinfo c1, c2;
501*86d7f5d3SJohn Marino u_quad_t t1, t2;
502*86d7f5d3SJohn Marino double n1, n2;
503*86d7f5d3SJohn Marino
504*86d7f5d3SJohn Marino memcpy(&c1, d1->data, sizeof(c1));
505*86d7f5d3SJohn Marino memcpy(&c2, d2->data, sizeof(c2));
506*86d7f5d3SJohn Marino
507*86d7f5d3SJohn Marino t1 = c1.ci_utime + c1.ci_stime;
508*86d7f5d3SJohn Marino t2 = c2.ci_utime + c2.ci_stime;
509*86d7f5d3SJohn Marino
510*86d7f5d3SJohn Marino n1 = (double) c1.ci_mem / (double) (t1 ? t1 : 1);
511*86d7f5d3SJohn Marino n2 = (double) c2.ci_mem / (double) (t2 ? t2 : 1);
512*86d7f5d3SJohn Marino
513*86d7f5d3SJohn Marino if (n1 < n2)
514*86d7f5d3SJohn Marino return -1;
515*86d7f5d3SJohn Marino else if (n1 == n2)
516*86d7f5d3SJohn Marino return (cmp_comm(c1.ci_comm, c2.ci_comm));
517*86d7f5d3SJohn Marino else
518*86d7f5d3SJohn Marino return 1;
519*86d7f5d3SJohn Marino }
520*86d7f5d3SJohn Marino
521*86d7f5d3SJohn Marino /* sort by the number of invocations */
522*86d7f5d3SJohn Marino static int
cmp_calls(const DBT * d1,const DBT * d2)523*86d7f5d3SJohn Marino cmp_calls(const DBT *d1, const DBT *d2)
524*86d7f5d3SJohn Marino {
525*86d7f5d3SJohn Marino struct cmdinfo c1, c2;
526*86d7f5d3SJohn Marino
527*86d7f5d3SJohn Marino memcpy(&c1, d1->data, sizeof(c1));
528*86d7f5d3SJohn Marino memcpy(&c2, d2->data, sizeof(c2));
529*86d7f5d3SJohn Marino
530*86d7f5d3SJohn Marino if (c1.ci_calls < c2.ci_calls)
531*86d7f5d3SJohn Marino return -1;
532*86d7f5d3SJohn Marino else if (c1.ci_calls == c2.ci_calls)
533*86d7f5d3SJohn Marino return (cmp_comm(c1.ci_comm, c2.ci_comm));
534*86d7f5d3SJohn Marino else
535*86d7f5d3SJohn Marino return 1;
536*86d7f5d3SJohn Marino }
537