2 * Copyright (c) 1994 Christopher G. Demetriou
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Christopher G. Demetriou.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 static const char copyright[] =
33 "@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
34 All rights reserved.\n";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.sbin/sa/main.c,v 1.8.2.2 2001/07/19 05:20:49 kris Exp $";
43 * sa: system accounting
46 #include <sys/types.h>
57 #include "pathnames.h"
59 static int acct_load __P((char *, int));
60 static u_quad_t decode_comp_t __P((comp_t));
61 static int cmp_comm __P((const char *, const char *));
62 static int cmp_usrsys __P((const DBT *, const DBT *));
63 static int cmp_avgusrsys __P((const DBT *, const DBT *));
64 static int cmp_dkio __P((const DBT *, const DBT *));
65 static int cmp_avgdkio __P((const DBT *, const DBT *));
66 static int cmp_cpumem __P((const DBT *, const DBT *));
67 static int cmp_avgcpumem __P((const DBT *, const DBT *));
68 static int cmp_calls __P((const DBT *, const DBT *));
69 static void usage __P((void));
71 int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
72 int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
75 static char *dfltargv[] = { _PATH_ACCT };
76 static int dfltargc = (sizeof dfltargv/sizeof(char *));
78 /* default to comparing by sum of user + system time */
79 cmpf_t sa_cmp = cmp_usrsys;
89 while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1)
92 /* print all commands */
96 /* sort by per-call user/system time average */
98 sa_cmp = cmp_avgusrsys;
101 /* print percentage total time */
105 /* sort by averge number of disk I/O ops */
107 sa_cmp = cmp_avgdkio;
110 /* print and sort by total disk I/O ops */
115 /* force no interactive threshold comprison */
119 /* do not read in summary file */
123 /* instead of total minutes, give sec/call */
127 /* sort by cpu-time average memory usage */
129 sa_cmp = cmp_avgcpumem;
132 /* print and sort by cpu-storage integral */
137 /* separate system and user time */
141 /* print procs and time per-user */
145 /* sort by number of calls */
149 /* quiet; error messages only */
153 /* reverse order of sort */
157 /* merge accounting file into summaries */
161 /* report ratio of user and system times */
165 /* first, print uid and command name */
171 cutoff = atoi(optarg);
181 /* various argument checking */
183 errx(1, "only one of -f requires -v");
185 errx(1, "only one of -a and -v may be specified");
186 /* XXX need more argument checking */
189 /* initialize tables */
190 if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
191 errx(1, "process accounting initialization failed");
192 if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
193 errx(1, "user accounting initialization failed");
201 /* for each file specified */
202 for (; argc > 0; argc--, argv++) {
206 * load the accounting data from the file.
207 * if it fails, go on to the next file.
209 fd = acct_load(argv[0], sflag);
213 if (!uflag && sflag) {
215 sigset_t nmask, omask;
219 * block most signals so we aren't interrupted during
222 if (sigfillset(&nmask) == -1) {
228 (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
229 warn("couldn't set signal mask");
236 * truncate the accounting data file ASAP, to avoid
237 * losing data. don't worry about errors in updating
238 * the saved stats; better to underbill than overbill,
239 * but we want every accounting record intact.
241 if (ftruncate(fd, 0) == -1) {
242 warn("couldn't truncate %s", argv);
247 * update saved user and process accounting data.
248 * note errors for later.
250 if (pacct_update() != 0 || usracct_update() != 0)
258 (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
259 warn("couldn't restore signal mask");
266 * close the opened accounting file
268 if (close(fd) == -1) {
269 warn("close %s", argv);
274 if (!uflag && !qflag) {
275 /* print any results we may have obtained. */
283 /* finally, deallocate databases */
284 if (sflag || (!mflag && !qflag))
286 if (sflag || (mflag && !qflag))
296 (void)fprintf(stderr,
297 "usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n");
314 fd = open(pn, wr ? O_RDWR : O_RDONLY, 0);
316 warn("open %s %s", pn, wr ? "for read/write" : "read-only");
321 * read all we can; don't stat and open because more processes
322 * could exit, and we'd miss them
325 /* get one accounting entry and punt if there's an error */
326 rv = read(fd, &ac, sizeof(struct acct));
328 warn("error reading %s", pn);
329 else if (rv > 0 && rv < sizeof(struct acct))
330 warnx("short read of accounting data in %s", pn);
331 if (rv != sizeof(struct acct))
336 for (i = 0; i < sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
338 char c = ac.ac_comm[i];
340 if (!isascii(c) || iscntrl(c)) {
342 ci.ci_flags |= CI_UNPRINTABLE;
346 if (ac.ac_flag & AFORK)
347 ci.ci_comm[i++] = '*';
348 ci.ci_comm[i++] = '\0';
349 ci.ci_etime = decode_comp_t(ac.ac_etime);
350 ci.ci_utime = decode_comp_t(ac.ac_utime);
351 ci.ci_stime = decode_comp_t(ac.ac_stime);
352 ci.ci_uid = ac.ac_uid;
353 ci.ci_mem = ac.ac_mem;
354 ci.ci_io = decode_comp_t(ac.ac_io) / AHZ;
357 /* and enter it into the usracct and pacct databases */
358 if (sflag || (!mflag && !qflag))
360 if (sflag || (mflag && !qflag))
363 printf("%6lu %12.2f cpu %12quk mem %12qu io %s\n",
365 (ci.ci_utime + ci.ci_stime) / (double) AHZ,
366 ci.ci_mem, ci.ci_io, ci.ci_comm);
369 /* finally, return the file descriptor for possible truncation */
380 * for more info on the comp_t format, see:
381 * /usr/src/sys/kern/kern_acct.c
382 * /usr/src/sys/sys/acct.h
383 * /usr/src/usr.bin/lastcomm/lastcomm.c
385 rv = comp & 0x1fff; /* 13 bit fraction */
386 comp >>= 13; /* 3 bit base-8 exponent */
393 /* sort commands, doing the right thing in terms of reversals */
403 return (rflag ? rv : -rv);
406 /* sort by total user and system time */
411 struct cmdinfo c1, c2;
414 memcpy(&c1, d1->data, sizeof(c1));
415 memcpy(&c2, d2->data, sizeof(c2));
417 t1 = c1.ci_utime + c1.ci_stime;
418 t2 = c2.ci_utime + c2.ci_stime;
423 return (cmp_comm(c1.ci_comm, c2.ci_comm));
428 /* sort by average user and system time */
430 cmp_avgusrsys(d1, d2)
433 struct cmdinfo c1, c2;
436 memcpy(&c1, d1->data, sizeof(c1));
437 memcpy(&c2, d2->data, sizeof(c2));
439 t1 = c1.ci_utime + c1.ci_stime;
440 t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
442 t2 = c2.ci_utime + c2.ci_stime;
443 t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
448 return (cmp_comm(c1.ci_comm, c2.ci_comm));
453 /* sort by total number of disk I/O operations */
458 struct cmdinfo c1, c2;
460 memcpy(&c1, d1->data, sizeof(c1));
461 memcpy(&c2, d2->data, sizeof(c2));
463 if (c1.ci_io < c2.ci_io)
465 else if (c1.ci_io == c2.ci_io)
466 return (cmp_comm(c1.ci_comm, c2.ci_comm));
471 /* sort by average number of disk I/O operations */
476 struct cmdinfo c1, c2;
479 memcpy(&c1, d1->data, sizeof(c1));
480 memcpy(&c2, d2->data, sizeof(c2));
482 n1 = (double) c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
483 n2 = (double) c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
488 return (cmp_comm(c1.ci_comm, c2.ci_comm));
493 /* sort by the cpu-storage integral */
498 struct cmdinfo c1, c2;
500 memcpy(&c1, d1->data, sizeof(c1));
501 memcpy(&c2, d2->data, sizeof(c2));
503 if (c1.ci_mem < c2.ci_mem)
505 else if (c1.ci_mem == c2.ci_mem)
506 return (cmp_comm(c1.ci_comm, c2.ci_comm));
511 /* sort by the cpu-time average memory usage */
513 cmp_avgcpumem(d1, d2)
516 struct cmdinfo c1, c2;
520 memcpy(&c1, d1->data, sizeof(c1));
521 memcpy(&c2, d2->data, sizeof(c2));
523 t1 = c1.ci_utime + c1.ci_stime;
524 t2 = c2.ci_utime + c2.ci_stime;
526 n1 = (double) c1.ci_mem / (double) (t1 ? t1 : 1);
527 n2 = (double) c2.ci_mem / (double) (t2 ? t2 : 1);
532 return (cmp_comm(c1.ci_comm, c2.ci_comm));
537 /* sort by the number of invocations */
542 struct cmdinfo c1, c2;
544 memcpy(&c1, d1->data, sizeof(c1));
545 memcpy(&c2, d2->data, sizeof(c2));
547 if (c1.ci_calls < c2.ci_calls)
549 else if (c1.ci_calls == c2.ci_calls)
550 return (cmp_comm(c1.ci_comm, c2.ci_comm));