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.
33 static const char copyright[] =
34 "@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
35 All rights reserved.\n";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * sa: system accounting
45 #include <sys/types.h>
57 #include "pathnames.h"
59 static int acct_load(const char *, int);
60 static u_quad_t decode_comp_t(comp_t);
61 static int cmp_comm(const char *, const char *);
62 static int cmp_usrsys(const DBT *, const DBT *);
63 static int cmp_avgusrsys(const DBT *, const DBT *);
64 static int cmp_dkio(const DBT *, const DBT *);
65 static int cmp_avgdkio(const DBT *, const DBT *);
66 static int cmp_cpumem(const DBT *, const DBT *);
67 static int cmp_avgcpumem(const DBT *, const DBT *);
68 static int cmp_calls(const DBT *, const DBT *);
69 static void usage(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[] = { NULL };
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;
82 main(int argc, char **argv)
84 char pathacct[] = _PATH_ACCT;
87 dfltargv[0] = pathacct;
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");
302 acct_load(const char *pn, int wr)
312 fd = open(pn, wr ? O_RDWR : O_RDONLY, 0);
314 warn("open %s %s", pn, wr ? "for read/write" : "read-only");
319 * read all we can; don't stat and open because more processes
320 * could exit, and we'd miss them
323 /* get one accounting entry and punt if there's an error */
324 rv = read(fd, &ac, sizeof(struct acct));
326 warn("error reading %s", pn);
327 else if (rv > 0 && rv < (int)sizeof(struct acct))
328 warnx("short read of accounting data in %s", pn);
329 if (rv != sizeof(struct acct))
334 for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
336 char c = ac.ac_comm[i];
338 if (!isascii(c) || iscntrl(c)) {
340 ci.ci_flags |= CI_UNPRINTABLE;
344 if (ac.ac_flag & AFORK)
345 ci.ci_comm[i++] = '*';
346 ci.ci_comm[i++] = '\0';
347 ci.ci_etime = decode_comp_t(ac.ac_etime);
348 ci.ci_utime = decode_comp_t(ac.ac_utime);
349 ci.ci_stime = decode_comp_t(ac.ac_stime);
350 ci.ci_uid = ac.ac_uid;
351 ci.ci_mem = ac.ac_mem;
352 ci.ci_io = decode_comp_t(ac.ac_io) / AHZ;
355 /* and enter it into the usracct and pacct databases */
356 if (sflag || (!mflag && !qflag))
358 if (sflag || (mflag && !qflag))
361 printf("%6lu %12.2f cpu %12juk mem %12ju io %s\n",
363 (ci.ci_utime + ci.ci_stime) / (double) AHZ,
364 (uintmax_t)ci.ci_mem, (uintmax_t)ci.ci_io,
368 /* finally, return the file descriptor for possible truncation */
373 decode_comp_t(comp_t comp)
378 * for more info on the comp_t format, see:
379 * /usr/src/sys/kern/kern_acct.c
380 * /usr/src/sys/sys/acct.h
381 * /usr/src/usr.bin/lastcomm/lastcomm.c
383 rv = comp & 0x1fff; /* 13 bit fraction */
384 comp >>= 13; /* 3 bit base-8 exponent */
391 /* sort commands, doing the right thing in terms of reversals */
393 cmp_comm(const char *s1, const char *s2)
400 return (rflag ? rv : -rv);
403 /* sort by total user and system time */
405 cmp_usrsys(const DBT *d1, const DBT *d2)
407 struct cmdinfo c1, c2;
410 memcpy(&c1, d1->data, sizeof(c1));
411 memcpy(&c2, d2->data, sizeof(c2));
413 t1 = c1.ci_utime + c1.ci_stime;
414 t2 = c2.ci_utime + c2.ci_stime;
419 return (cmp_comm(c1.ci_comm, c2.ci_comm));
424 /* sort by average user and system time */
426 cmp_avgusrsys(const DBT *d1, const DBT *d2)
428 struct cmdinfo c1, c2;
431 memcpy(&c1, d1->data, sizeof(c1));
432 memcpy(&c2, d2->data, sizeof(c2));
434 t1 = c1.ci_utime + c1.ci_stime;
435 t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
437 t2 = c2.ci_utime + c2.ci_stime;
438 t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
443 return (cmp_comm(c1.ci_comm, c2.ci_comm));
448 /* sort by total number of disk I/O operations */
450 cmp_dkio(const DBT *d1, const DBT *d2)
452 struct cmdinfo c1, c2;
454 memcpy(&c1, d1->data, sizeof(c1));
455 memcpy(&c2, d2->data, sizeof(c2));
457 if (c1.ci_io < c2.ci_io)
459 else if (c1.ci_io == c2.ci_io)
460 return (cmp_comm(c1.ci_comm, c2.ci_comm));
465 /* sort by average number of disk I/O operations */
467 cmp_avgdkio(const DBT *d1, const DBT *d2)
469 struct cmdinfo c1, c2;
472 memcpy(&c1, d1->data, sizeof(c1));
473 memcpy(&c2, d2->data, sizeof(c2));
475 n1 = (double) c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
476 n2 = (double) c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
481 return (cmp_comm(c1.ci_comm, c2.ci_comm));
486 /* sort by the cpu-storage integral */
488 cmp_cpumem(const DBT *d1, const DBT *d2)
490 struct cmdinfo c1, c2;
492 memcpy(&c1, d1->data, sizeof(c1));
493 memcpy(&c2, d2->data, sizeof(c2));
495 if (c1.ci_mem < c2.ci_mem)
497 else if (c1.ci_mem == c2.ci_mem)
498 return (cmp_comm(c1.ci_comm, c2.ci_comm));
503 /* sort by the cpu-time average memory usage */
505 cmp_avgcpumem(const DBT *d1, const DBT *d2)
507 struct cmdinfo c1, c2;
511 memcpy(&c1, d1->data, sizeof(c1));
512 memcpy(&c2, d2->data, sizeof(c2));
514 t1 = c1.ci_utime + c1.ci_stime;
515 t2 = c2.ci_utime + c2.ci_stime;
517 n1 = (double) c1.ci_mem / (double) (t1 ? t1 : 1);
518 n2 = (double) c2.ci_mem / (double) (t2 ? t2 : 1);
523 return (cmp_comm(c1.ci_comm, c2.ci_comm));
528 /* sort by the number of invocations */
530 cmp_calls(const DBT *d1, const DBT *d2)
532 struct cmdinfo c1, c2;
534 memcpy(&c1, d1->data, sizeof(c1));
535 memcpy(&c2, d2->data, sizeof(c2));
537 if (c1.ci_calls < c2.ci_calls)
539 else if (c1.ci_calls == c2.ci_calls)
540 return (cmp_comm(c1.ci_comm, c2.ci_comm));