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.
30 * @(#) Copyright (c) 1994 Christopher G. Demetriou All rights reserved.
31 * $FreeBSD: src/usr.sbin/sa/main.c,v 1.8.2.2 2001/07/19 05:20:49 kris Exp $
35 * sa: system accounting
38 #include <sys/types.h>
49 #include "pathnames.h"
51 static int acct_load(char *, int);
52 static u_quad_t decode_comp_t(comp_t);
53 static int cmp_comm(const char *, const char *);
54 static int cmp_usrsys(const DBT *, const DBT *);
55 static int cmp_avgusrsys(const DBT *, const DBT *);
56 static int cmp_dkio(const DBT *, const DBT *);
57 static int cmp_avgdkio(const DBT *, const DBT *);
58 static int cmp_cpumem(const DBT *, const DBT *);
59 static int cmp_avgcpumem(const DBT *, const DBT *);
60 static int cmp_calls(const DBT *, const DBT *);
61 static void usage (void);
63 int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
64 int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
67 static char *dfltargv[] = { NULL };
68 static int dfltargc = (sizeof dfltargv/sizeof(char *));
70 /* default to comparing by sum of user + system time */
71 cmpf_t sa_cmp = cmp_usrsys;
74 main(int argc, char **argv)
77 char pathacct[] = _PATH_ACCT;
80 dfltargv[0] = pathacct;
82 while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1)
85 /* print all commands */
89 /* sort by per-call user/system time average */
91 sa_cmp = cmp_avgusrsys;
94 /* print percentage total time */
98 /* sort by averge number of disk I/O ops */
100 sa_cmp = cmp_avgdkio;
103 /* print and sort by total disk I/O ops */
108 /* force no interactive threshold comprison */
112 /* do not read in summary file */
116 /* instead of total minutes, give sec/call */
120 /* sort by cpu-time average memory usage */
122 sa_cmp = cmp_avgcpumem;
125 /* print and sort by cpu-storage integral */
130 /* separate system and user time */
134 /* print procs and time per-user */
138 /* sort by number of calls */
142 /* quiet; error messages only */
146 /* reverse order of sort */
150 /* merge accounting file into summaries */
154 /* report ratio of user and system times */
158 /* first, print uid and command name */
164 cutoff = atoi(optarg);
174 /* various argument checking */
176 errx(1, "only one of -f requires -v");
178 errx(1, "only one of -a and -v may be specified");
179 /* XXX need more argument checking */
182 /* initialize tables */
183 if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
184 errx(1, "process accounting initialization failed");
185 if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
186 errx(1, "user accounting initialization failed");
194 /* for each file specified */
195 for (; argc > 0; argc--, argv++) {
199 * load the accounting data from the file.
200 * if it fails, go on to the next file.
202 fd = acct_load(argv[0], sflag);
206 if (!uflag && sflag) {
208 sigset_t nmask, omask;
212 * block most signals so we aren't interrupted during
215 if (sigfillset(&nmask) == -1) {
221 (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
222 warn("couldn't set signal mask");
229 * truncate the accounting data file ASAP, to avoid
230 * losing data. don't worry about errors in updating
231 * the saved stats; better to underbill than overbill,
232 * but we want every accounting record intact.
234 if (ftruncate(fd, 0) == -1) {
235 warn("couldn't truncate %s", argv[0]);
240 * update saved user and process accounting data.
241 * note errors for later.
243 if (pacct_update() != 0 || usracct_update() != 0)
251 (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
252 warn("couldn't restore signal mask");
259 * close the opened accounting file
261 if (close(fd) == -1) {
262 warn("close %s", argv[0]);
267 if (!uflag && !qflag) {
268 /* print any results we may have obtained. */
276 /* finally, deallocate databases */
277 if (sflag || (!mflag && !qflag))
279 if (sflag || (mflag && !qflag))
290 "usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n");
295 acct_load(char *pn, int wr)
305 fd = open(pn, wr ? O_RDWR : O_RDONLY, 0);
307 warn("open %s %s", pn, wr ? "for read/write" : "read-only");
312 * read all we can; don't stat and open because more processes
313 * could exit, and we'd miss them
316 /* get one accounting entry and punt if there's an error */
317 rv = read(fd, &ac, sizeof(struct acct));
319 warn("error reading %s", pn);
320 else if (rv > 0 && rv < (int)sizeof(struct acct))
321 warnx("short read of accounting data in %s", pn);
322 if (rv != sizeof(struct acct))
327 for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
329 char c = ac.ac_comm[i];
331 if (!isascii(c) || iscntrl(c)) {
333 ci.ci_flags |= CI_UNPRINTABLE;
337 if (ac.ac_flag & AFORK)
338 ci.ci_comm[i++] = '*';
339 ci.ci_comm[i++] = '\0';
340 ci.ci_etime = decode_comp_t(ac.ac_etime);
341 ci.ci_utime = decode_comp_t(ac.ac_utime);
342 ci.ci_stime = decode_comp_t(ac.ac_stime);
343 ci.ci_uid = ac.ac_uid;
344 ci.ci_mem = ac.ac_mem;
345 ci.ci_io = decode_comp_t(ac.ac_io) / AHZ;
348 /* and enter it into the usracct and pacct databases */
349 if (sflag || (!mflag && !qflag))
351 if (sflag || (mflag && !qflag))
354 printf("%6lu %12.2f cpu %12juk mem %12ju io %s\n",
356 (ci.ci_utime + ci.ci_stime) / (double) AHZ,
357 (uintmax_t)ci.ci_mem, (uintmax_t)ci.ci_io,
361 /* finally, return the file descriptor for possible truncation */
366 decode_comp_t(comp_t comp)
371 * for more info on the comp_t format, see:
372 * /usr/src/sys/kern/kern_acct.c
373 * /usr/src/sys/sys/acct.h
374 * /usr/src/usr.bin/lastcomm/lastcomm.c
376 rv = comp & 0x1fff; /* 13 bit fraction */
377 comp >>= 13; /* 3 bit base-8 exponent */
384 /* sort commands, doing the right thing in terms of reversals */
386 cmp_comm(const char *s1, const char *s2)
393 return (rflag ? rv : -rv);
396 /* sort by total user and system time */
398 cmp_usrsys(const DBT *d1, const DBT *d2)
400 struct cmdinfo c1, c2;
403 memcpy(&c1, d1->data, sizeof(c1));
404 memcpy(&c2, d2->data, sizeof(c2));
406 t1 = c1.ci_utime + c1.ci_stime;
407 t2 = c2.ci_utime + c2.ci_stime;
412 return (cmp_comm(c1.ci_comm, c2.ci_comm));
417 /* sort by average user and system time */
419 cmp_avgusrsys(const DBT *d1, const DBT *d2)
421 struct cmdinfo c1, c2;
424 memcpy(&c1, d1->data, sizeof(c1));
425 memcpy(&c2, d2->data, sizeof(c2));
427 t1 = c1.ci_utime + c1.ci_stime;
428 t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
430 t2 = c2.ci_utime + c2.ci_stime;
431 t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
436 return (cmp_comm(c1.ci_comm, c2.ci_comm));
441 /* sort by total number of disk I/O operations */
443 cmp_dkio(const DBT *d1, const DBT *d2)
445 struct cmdinfo c1, c2;
447 memcpy(&c1, d1->data, sizeof(c1));
448 memcpy(&c2, d2->data, sizeof(c2));
450 if (c1.ci_io < c2.ci_io)
452 else if (c1.ci_io == c2.ci_io)
453 return (cmp_comm(c1.ci_comm, c2.ci_comm));
458 /* sort by average number of disk I/O operations */
460 cmp_avgdkio(const DBT *d1, const DBT *d2)
462 struct cmdinfo c1, c2;
465 memcpy(&c1, d1->data, sizeof(c1));
466 memcpy(&c2, d2->data, sizeof(c2));
468 n1 = (double) c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
469 n2 = (double) c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
474 return (cmp_comm(c1.ci_comm, c2.ci_comm));
479 /* sort by the cpu-storage integral */
481 cmp_cpumem(const DBT *d1, const DBT *d2)
483 struct cmdinfo c1, c2;
485 memcpy(&c1, d1->data, sizeof(c1));
486 memcpy(&c2, d2->data, sizeof(c2));
488 if (c1.ci_mem < c2.ci_mem)
490 else if (c1.ci_mem == c2.ci_mem)
491 return (cmp_comm(c1.ci_comm, c2.ci_comm));
496 /* sort by the cpu-time average memory usage */
498 cmp_avgcpumem(const DBT *d1, const DBT *d2)
500 struct cmdinfo c1, c2;
504 memcpy(&c1, d1->data, sizeof(c1));
505 memcpy(&c2, d2->data, sizeof(c2));
507 t1 = c1.ci_utime + c1.ci_stime;
508 t2 = c2.ci_utime + c2.ci_stime;
510 n1 = (double) c1.ci_mem / (double) (t1 ? t1 : 1);
511 n2 = (double) c2.ci_mem / (double) (t2 ? t2 : 1);
516 return (cmp_comm(c1.ci_comm, c2.ci_comm));
521 /* sort by the number of invocations */
523 cmp_calls(const DBT *d1, const DBT *d2)
525 struct cmdinfo c1, c2;
527 memcpy(&c1, d1->data, sizeof(c1));
528 memcpy(&c2, d2->data, sizeof(c2));
530 if (c1.ci_calls < c2.ci_calls)
532 else if (c1.ci_calls == c2.ci_calls)
533 return (cmp_comm(c1.ci_comm, c2.ci_comm));