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 $
32 * $DragonFly: src/usr.sbin/sa/main.c,v 1.5 2005/12/05 02:40:28 swildner Exp $
36 * sa: system accounting
39 #include <sys/types.h>
50 #include "pathnames.h"
52 static int acct_load(char *, int);
53 static u_quad_t decode_comp_t(comp_t);
54 static int cmp_comm(const char *, const char *);
55 static int cmp_usrsys(const DBT *, const DBT *);
56 static int cmp_avgusrsys(const DBT *, const DBT *);
57 static int cmp_dkio(const DBT *, const DBT *);
58 static int cmp_avgdkio(const DBT *, const DBT *);
59 static int cmp_cpumem(const DBT *, const DBT *);
60 static int cmp_avgcpumem(const DBT *, const DBT *);
61 static int cmp_calls(const DBT *, const DBT *);
62 static void usage (void);
64 int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
65 int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
68 static char *dfltargv[] = { _PATH_ACCT };
69 static int dfltargc = (sizeof dfltargv/sizeof(char *));
71 /* default to comparing by sum of user + system time */
72 cmpf_t sa_cmp = cmp_usrsys;
75 main(int argc, char **argv)
80 while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1)
83 /* print all commands */
87 /* sort by per-call user/system time average */
89 sa_cmp = cmp_avgusrsys;
92 /* print percentage total time */
96 /* sort by averge number of disk I/O ops */
101 /* print and sort by total disk I/O ops */
106 /* force no interactive threshold comprison */
110 /* do not read in summary file */
114 /* instead of total minutes, give sec/call */
118 /* sort by cpu-time average memory usage */
120 sa_cmp = cmp_avgcpumem;
123 /* print and sort by cpu-storage integral */
128 /* separate system and user time */
132 /* print procs and time per-user */
136 /* sort by number of calls */
140 /* quiet; error messages only */
144 /* reverse order of sort */
148 /* merge accounting file into summaries */
152 /* report ratio of user and system times */
156 /* first, print uid and command name */
162 cutoff = atoi(optarg);
172 /* various argument checking */
174 errx(1, "only one of -f requires -v");
176 errx(1, "only one of -a and -v may be specified");
177 /* XXX need more argument checking */
180 /* initialize tables */
181 if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
182 errx(1, "process accounting initialization failed");
183 if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
184 errx(1, "user accounting initialization failed");
192 /* for each file specified */
193 for (; argc > 0; argc--, argv++) {
197 * load the accounting data from the file.
198 * if it fails, go on to the next file.
200 fd = acct_load(argv[0], sflag);
204 if (!uflag && sflag) {
206 sigset_t nmask, omask;
210 * block most signals so we aren't interrupted during
213 if (sigfillset(&nmask) == -1) {
219 (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
220 warn("couldn't set signal mask");
227 * truncate the accounting data file ASAP, to avoid
228 * losing data. don't worry about errors in updating
229 * the saved stats; better to underbill than overbill,
230 * but we want every accounting record intact.
232 if (ftruncate(fd, 0) == -1) {
233 warn("couldn't truncate %s", argv);
238 * update saved user and process accounting data.
239 * note errors for later.
241 if (pacct_update() != 0 || usracct_update() != 0)
249 (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
250 warn("couldn't restore signal mask");
257 * close the opened accounting file
259 if (close(fd) == -1) {
260 warn("close %s", argv);
265 if (!uflag && !qflag) {
266 /* print any results we may have obtained. */
274 /* finally, deallocate databases */
275 if (sflag || (!mflag && !qflag))
277 if (sflag || (mflag && !qflag))
288 "usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n");
293 acct_load(char *pn, int wr)
303 fd = open(pn, wr ? O_RDWR : O_RDONLY, 0);
305 warn("open %s %s", pn, wr ? "for read/write" : "read-only");
310 * read all we can; don't stat and open because more processes
311 * could exit, and we'd miss them
314 /* get one accounting entry and punt if there's an error */
315 rv = read(fd, &ac, sizeof(struct acct));
317 warn("error reading %s", pn);
318 else if (rv > 0 && rv < sizeof(struct acct))
319 warnx("short read of accounting data in %s", pn);
320 if (rv != sizeof(struct acct))
325 for (i = 0; i < sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
327 char c = ac.ac_comm[i];
329 if (!isascii(c) || iscntrl(c)) {
331 ci.ci_flags |= CI_UNPRINTABLE;
335 if (ac.ac_flag & AFORK)
336 ci.ci_comm[i++] = '*';
337 ci.ci_comm[i++] = '\0';
338 ci.ci_etime = decode_comp_t(ac.ac_etime);
339 ci.ci_utime = decode_comp_t(ac.ac_utime);
340 ci.ci_stime = decode_comp_t(ac.ac_stime);
341 ci.ci_uid = ac.ac_uid;
342 ci.ci_mem = ac.ac_mem;
343 ci.ci_io = decode_comp_t(ac.ac_io) / AHZ;
346 /* and enter it into the usracct and pacct databases */
347 if (sflag || (!mflag && !qflag))
349 if (sflag || (mflag && !qflag))
352 printf("%6lu %12.2f cpu %12quk mem %12qu io %s\n",
354 (ci.ci_utime + ci.ci_stime) / (double) AHZ,
355 ci.ci_mem, ci.ci_io, ci.ci_comm);
358 /* finally, return the file descriptor for possible truncation */
363 decode_comp_t(comp_t comp)
368 * for more info on the comp_t format, see:
369 * /usr/src/sys/kern/kern_acct.c
370 * /usr/src/sys/sys/acct.h
371 * /usr/src/usr.bin/lastcomm/lastcomm.c
373 rv = comp & 0x1fff; /* 13 bit fraction */
374 comp >>= 13; /* 3 bit base-8 exponent */
381 /* sort commands, doing the right thing in terms of reversals */
383 cmp_comm(const char *s1, const char *s2)
390 return (rflag ? rv : -rv);
393 /* sort by total user and system time */
395 cmp_usrsys(const DBT *d1, const DBT *d2)
397 struct cmdinfo c1, c2;
400 memcpy(&c1, d1->data, sizeof(c1));
401 memcpy(&c2, d2->data, sizeof(c2));
403 t1 = c1.ci_utime + c1.ci_stime;
404 t2 = c2.ci_utime + c2.ci_stime;
409 return (cmp_comm(c1.ci_comm, c2.ci_comm));
414 /* sort by average user and system time */
416 cmp_avgusrsys(const DBT *d1, const DBT *d2)
418 struct cmdinfo c1, c2;
421 memcpy(&c1, d1->data, sizeof(c1));
422 memcpy(&c2, d2->data, sizeof(c2));
424 t1 = c1.ci_utime + c1.ci_stime;
425 t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
427 t2 = c2.ci_utime + c2.ci_stime;
428 t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
433 return (cmp_comm(c1.ci_comm, c2.ci_comm));
438 /* sort by total number of disk I/O operations */
440 cmp_dkio(const DBT *d1, const DBT *d2)
442 struct cmdinfo c1, c2;
444 memcpy(&c1, d1->data, sizeof(c1));
445 memcpy(&c2, d2->data, sizeof(c2));
447 if (c1.ci_io < c2.ci_io)
449 else if (c1.ci_io == c2.ci_io)
450 return (cmp_comm(c1.ci_comm, c2.ci_comm));
455 /* sort by average number of disk I/O operations */
457 cmp_avgdkio(const DBT *d1, const DBT *d2)
459 struct cmdinfo c1, c2;
462 memcpy(&c1, d1->data, sizeof(c1));
463 memcpy(&c2, d2->data, sizeof(c2));
465 n1 = (double) c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
466 n2 = (double) c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
471 return (cmp_comm(c1.ci_comm, c2.ci_comm));
476 /* sort by the cpu-storage integral */
478 cmp_cpumem(const DBT *d1, const DBT *d2)
480 struct cmdinfo c1, c2;
482 memcpy(&c1, d1->data, sizeof(c1));
483 memcpy(&c2, d2->data, sizeof(c2));
485 if (c1.ci_mem < c2.ci_mem)
487 else if (c1.ci_mem == c2.ci_mem)
488 return (cmp_comm(c1.ci_comm, c2.ci_comm));
493 /* sort by the cpu-time average memory usage */
495 cmp_avgcpumem(const DBT *d1, const DBT *d2)
497 struct cmdinfo c1, c2;
501 memcpy(&c1, d1->data, sizeof(c1));
502 memcpy(&c2, d2->data, sizeof(c2));
504 t1 = c1.ci_utime + c1.ci_stime;
505 t2 = c2.ci_utime + c2.ci_stime;
507 n1 = (double) c1.ci_mem / (double) (t1 ? t1 : 1);
508 n2 = (double) c2.ci_mem / (double) (t2 ? t2 : 1);
513 return (cmp_comm(c1.ci_comm, c2.ci_comm));
518 /* sort by the number of invocations */
520 cmp_calls(const DBT *d1, const DBT *d2)
522 struct cmdinfo c1, c2;
524 memcpy(&c1, d1->data, sizeof(c1));
525 memcpy(&c2, d2->data, sizeof(c2));
527 if (c1.ci_calls < c2.ci_calls)
529 else if (c1.ci_calls == c2.ci_calls)
530 return (cmp_comm(c1.ci_comm, c2.ci_comm));