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>
58 #include "pathnames.h"
60 static FILE *acct_load(const char *, int);
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;
74 const char *pdb_file = _PATH_SAVACCT;
75 const char *usrdb_file = _PATH_USRACCT;
77 static char *dfltargv[] = { NULL };
78 static int dfltargc = (sizeof dfltargv/sizeof(char *));
80 /* default to comparing by sum of user + system time */
81 cmpf_t sa_cmp = cmp_usrsys;
84 main(int argc, char **argv)
87 char pathacct[] = _PATH_ACCT;
90 dfltargv[0] = pathacct;
92 while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1)
95 /* print all commands */
99 /* sort by per-call user/system time average */
101 sa_cmp = cmp_avgusrsys;
104 /* print percentage total time */
108 /* sort by averge number of disk I/O ops */
110 sa_cmp = cmp_avgdkio;
113 /* print and sort by total disk I/O ops */
118 /* force no interactive threshold comprison */
122 /* do not read in summary file */
126 /* instead of total minutes, give sec/call */
130 /* sort by cpu-time average memory usage */
132 sa_cmp = cmp_avgcpumem;
135 /* print and sort by cpu-storage integral */
140 /* separate system and user time */
144 /* print procs and time per-user */
148 /* sort by number of calls */
152 /* specify program database summary file */
156 /* quiet; error messages only */
160 /* reverse order of sort */
164 /* merge accounting file into summaries */
168 /* report ratio of user and system times */
172 /* first, print uid and command name */
176 /* specify user database summary file */
182 cutoff = atoi(optarg);
192 /* various argument checking */
194 errx(1, "only one of -f requires -v");
196 errx(1, "only one of -a and -v may be specified");
197 /* XXX need more argument checking */
200 /* initialize tables */
201 if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
202 errx(1, "process accounting initialization failed");
203 if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
204 errx(1, "user accounting initialization failed");
212 /* for each file specified */
213 for (; argc > 0; argc--, argv++) {
215 * load the accounting data from the file.
216 * if it fails, go on to the next file.
218 f = acct_load(argv[0], sflag);
222 if (!uflag && sflag) {
224 sigset_t nmask, omask;
228 * block most signals so we aren't interrupted during
231 if (sigfillset(&nmask) == -1) {
237 (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
238 warn("couldn't set signal mask");
245 * truncate the accounting data file ASAP, to avoid
246 * losing data. don't worry about errors in updating
247 * the saved stats; better to underbill than overbill,
248 * but we want every accounting record intact.
250 if (ftruncate(fileno(f), 0) == -1) {
251 warn("couldn't truncate %s", *argv);
256 * update saved user and process accounting data.
257 * note errors for later.
259 if (pacct_update() != 0 || usracct_update() != 0)
267 (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
268 warn("couldn't restore signal mask");
275 * close the opened accounting file
277 if (fclose(f) == EOF) {
278 warn("fclose %s", *argv);
283 if (!uflag && !qflag) {
284 /* print any results we may have obtained. */
292 /* finally, deallocate databases */
293 if (sflag || (!mflag && !qflag))
295 if (sflag || (mflag && !qflag))
305 (void)fprintf(stderr,
306 "usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n");
311 acct_load(const char *pn, int wr)
322 f = fopen(pn, wr ? "r+" : "r");
324 warn("open %s %s", pn, wr ? "for read/write" : "read-only");
329 * read all we can; don't stat and open because more processes
330 * could exit, and we'd miss them
333 /* get one accounting entry and punt if there's an error */
334 rv = readrec_forward(f, &ac);
337 warn("error reading %s", pn);
343 for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
345 char c = ac.ac_comm[i];
347 if (!isascii(c) || iscntrl(c)) {
349 ci.ci_flags |= CI_UNPRINTABLE;
353 if (ac.ac_flagx & AFORK)
354 ci.ci_comm[i++] = '*';
355 ci.ci_comm[i++] = '\0';
356 ci.ci_etime = ac.ac_etime;
357 ci.ci_utime = ac.ac_utime;
358 ci.ci_stime = ac.ac_stime;
359 ci.ci_uid = ac.ac_uid;
360 ci.ci_mem = ac.ac_mem;
364 /* and enter it into the usracct and pacct databases */
365 if (sflag || (!mflag && !qflag))
367 if (sflag || (mflag && !qflag))
370 printf("%6u %12.3lf cpu %12.0lfk mem %12.0lf io %s\n",
372 (ci.ci_utime + ci.ci_stime) / 1000000,
377 /* Finally, return the file stream for possible truncation. */
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 = c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
466 n2 = 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 = c1.ci_mem / (t1 ? t1 : 1);
508 n2 = c2.ci_mem / (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));