2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1994 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 static const char copyright[] =
36 "@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
37 All rights reserved.\n";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * sa: system accounting
47 #include <sys/types.h>
60 #include "pathnames.h"
62 static FILE *acct_load(const char *, int);
63 static int cmp_comm(const char *, const char *);
64 static int cmp_usrsys(const DBT *, const DBT *);
65 static int cmp_avgusrsys(const DBT *, const DBT *);
66 static int cmp_dkio(const DBT *, const DBT *);
67 static int cmp_avgdkio(const DBT *, const DBT *);
68 static int cmp_cpumem(const DBT *, const DBT *);
69 static int cmp_avgcpumem(const DBT *, const DBT *);
70 static int cmp_calls(const DBT *, const DBT *);
71 static void usage(void);
73 int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
74 int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
76 const char *pdb_file = _PATH_SAVACCT;
77 const char *usrdb_file = _PATH_USRACCT;
79 static char *dfltargv[] = { NULL };
80 static int dfltargc = (sizeof dfltargv/sizeof(char *));
82 /* default to comparing by sum of user + system time */
83 cmpf_t sa_cmp = cmp_usrsys;
86 main(int argc, char **argv)
89 char pathacct[] = _PATH_ACCT;
92 dfltargv[0] = pathacct;
94 while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1)
97 /* print all commands */
101 /* sort by per-call user/system time average */
103 sa_cmp = cmp_avgusrsys;
106 /* print percentage total time */
110 /* sort by averge number of disk I/O ops */
112 sa_cmp = cmp_avgdkio;
115 /* print and sort by total disk I/O ops */
120 /* force no interactive threshold comprison */
124 /* do not read in summary file */
128 /* instead of total minutes, give sec/call */
132 /* sort by cpu-time average memory usage */
134 sa_cmp = cmp_avgcpumem;
137 /* print and sort by cpu-storage integral */
142 /* separate system and user time */
146 /* print procs and time per-user */
150 /* sort by number of calls */
154 /* specify program database summary file */
158 /* quiet; error messages only */
162 /* reverse order of sort */
166 /* merge accounting file into summaries */
170 /* report ratio of user and system times */
174 /* first, print uid and command name */
178 /* specify user database summary file */
184 cutoff = atoi(optarg);
194 /* various argument checking */
196 errx(1, "only one of -f requires -v");
198 errx(1, "only one of -a and -v may be specified");
199 /* XXX need more argument checking */
202 /* initialize tables */
203 if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
204 errx(1, "process accounting initialization failed");
205 if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
206 errx(1, "user accounting initialization failed");
214 /* for each file specified */
215 for (; argc > 0; argc--, argv++) {
217 * load the accounting data from the file.
218 * if it fails, go on to the next file.
220 f = acct_load(argv[0], sflag);
224 if (!uflag && sflag) {
226 sigset_t nmask, omask;
230 * block most signals so we aren't interrupted during
233 if (sigfillset(&nmask) == -1) {
239 (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
240 warn("couldn't set signal mask");
247 * truncate the accounting data file ASAP, to avoid
248 * losing data. don't worry about errors in updating
249 * the saved stats; better to underbill than overbill,
250 * but we want every accounting record intact.
252 if (ftruncate(fileno(f), 0) == -1) {
253 warn("couldn't truncate %s", *argv);
258 * update saved user and process accounting data.
259 * note errors for later.
261 if (pacct_update() != 0 || usracct_update() != 0)
269 (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
270 warn("couldn't restore signal mask");
277 * close the opened accounting file
279 if (fclose(f) == EOF) {
280 warn("fclose %s", *argv);
285 if (!uflag && !qflag) {
286 /* print any results we may have obtained. */
294 /* finally, deallocate databases */
295 if (sflag || (!mflag && !qflag))
297 if (sflag || (mflag && !qflag))
307 (void)fprintf(stderr,
308 "usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n");
313 acct_load(const char *pn, int wr)
324 f = fopen(pn, wr ? "r+" : "r");
326 warn("open %s %s", pn, wr ? "for read/write" : "read-only");
331 * read all we can; don't stat and open because more processes
332 * could exit, and we'd miss them
335 /* get one accounting entry and punt if there's an error */
336 rv = readrec_forward(f, &ac);
339 warn("error reading %s", pn);
345 for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
347 char c = ac.ac_comm[i];
349 if (!isascii(c) || iscntrl(c)) {
351 ci.ci_flags |= CI_UNPRINTABLE;
355 if (ac.ac_flagx & AFORK)
356 ci.ci_comm[i++] = '*';
357 ci.ci_comm[i++] = '\0';
358 ci.ci_etime = ac.ac_etime;
359 ci.ci_utime = ac.ac_utime;
360 ci.ci_stime = ac.ac_stime;
361 ci.ci_uid = ac.ac_uid;
362 ci.ci_mem = ac.ac_mem;
366 /* and enter it into the usracct and pacct databases */
367 if (sflag || (!mflag && !qflag))
369 if (sflag || (mflag && !qflag))
372 printf("%6u %12.3lf cpu %12.0lfk mem %12.0lf io %s\n",
374 (ci.ci_utime + ci.ci_stime) / 1000000,
379 /* Finally, return the file stream for possible truncation. */
383 /* sort commands, doing the right thing in terms of reversals */
385 cmp_comm(const char *s1, const char *s2)
392 return (rflag ? rv : -rv);
395 /* sort by total user and system time */
397 cmp_usrsys(const DBT *d1, const DBT *d2)
399 struct cmdinfo c1, c2;
402 memcpy(&c1, d1->data, sizeof(c1));
403 memcpy(&c2, d2->data, sizeof(c2));
405 t1 = c1.ci_utime + c1.ci_stime;
406 t2 = c2.ci_utime + c2.ci_stime;
411 return (cmp_comm(c1.ci_comm, c2.ci_comm));
416 /* sort by average user and system time */
418 cmp_avgusrsys(const DBT *d1, const DBT *d2)
420 struct cmdinfo c1, c2;
423 memcpy(&c1, d1->data, sizeof(c1));
424 memcpy(&c2, d2->data, sizeof(c2));
426 t1 = c1.ci_utime + c1.ci_stime;
427 t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
429 t2 = c2.ci_utime + c2.ci_stime;
430 t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
435 return (cmp_comm(c1.ci_comm, c2.ci_comm));
440 /* sort by total number of disk I/O operations */
442 cmp_dkio(const DBT *d1, const DBT *d2)
444 struct cmdinfo c1, c2;
446 memcpy(&c1, d1->data, sizeof(c1));
447 memcpy(&c2, d2->data, sizeof(c2));
449 if (c1.ci_io < c2.ci_io)
451 else if (c1.ci_io == c2.ci_io)
452 return (cmp_comm(c1.ci_comm, c2.ci_comm));
457 /* sort by average number of disk I/O operations */
459 cmp_avgdkio(const DBT *d1, const DBT *d2)
461 struct cmdinfo c1, c2;
464 memcpy(&c1, d1->data, sizeof(c1));
465 memcpy(&c2, d2->data, sizeof(c2));
467 n1 = c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
468 n2 = c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
473 return (cmp_comm(c1.ci_comm, c2.ci_comm));
478 /* sort by the cpu-storage integral */
480 cmp_cpumem(const DBT *d1, const DBT *d2)
482 struct cmdinfo c1, c2;
484 memcpy(&c1, d1->data, sizeof(c1));
485 memcpy(&c2, d2->data, sizeof(c2));
487 if (c1.ci_mem < c2.ci_mem)
489 else if (c1.ci_mem == c2.ci_mem)
490 return (cmp_comm(c1.ci_comm, c2.ci_comm));
495 /* sort by the cpu-time average memory usage */
497 cmp_avgcpumem(const DBT *d1, const DBT *d2)
499 struct cmdinfo c1, c2;
503 memcpy(&c1, d1->data, sizeof(c1));
504 memcpy(&c2, d2->data, sizeof(c2));
506 t1 = c1.ci_utime + c1.ci_stime;
507 t2 = c2.ci_utime + c2.ci_stime;
509 n1 = c1.ci_mem / (t1 ? t1 : 1);
510 n2 = c2.ci_mem / (t2 ? t2 : 1);
515 return (cmp_comm(c1.ci_comm, c2.ci_comm));
520 /* sort by the number of invocations */
522 cmp_calls(const DBT *d1, const DBT *d2)
524 struct cmdinfo c1, c2;
526 memcpy(&c1, d1->data, sizeof(c1));
527 memcpy(&c2, d2->data, sizeof(c2));
529 if (c1.ci_calls < c2.ci_calls)
531 else if (c1.ci_calls == c2.ci_calls)
532 return (cmp_comm(c1.ci_comm, c2.ci_comm));