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.
34 * sa: system accounting
37 #include <sys/types.h>
50 #include "pathnames.h"
52 static FILE *acct_load(const char *, int);
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;
66 const char *pdb_file = _PATH_SAVACCT;
67 const char *usrdb_file = _PATH_USRACCT;
69 static char *dfltargv[] = { NULL };
70 static int dfltargc = (sizeof dfltargv/sizeof(char *));
72 /* default to comparing by sum of user + system time */
73 cmpf_t sa_cmp = cmp_usrsys;
76 main(int argc, char **argv)
79 char pathacct[] = _PATH_ACCT;
82 dfltargv[0] = pathacct;
84 while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1)
87 /* print all commands */
91 /* sort by per-call user/system time average */
93 sa_cmp = cmp_avgusrsys;
96 /* print percentage total time */
100 /* sort by averge number of disk I/O ops */
102 sa_cmp = cmp_avgdkio;
105 /* print and sort by total disk I/O ops */
110 /* force no interactive threshold comprison */
114 /* do not read in summary file */
118 /* instead of total minutes, give sec/call */
122 /* sort by cpu-time average memory usage */
124 sa_cmp = cmp_avgcpumem;
127 /* print and sort by cpu-storage integral */
132 /* separate system and user time */
136 /* print procs and time per-user */
140 /* sort by number of calls */
144 /* specify program database summary file */
148 /* quiet; error messages only */
152 /* reverse order of sort */
156 /* merge accounting file into summaries */
160 /* report ratio of user and system times */
164 /* first, print uid and command name */
168 /* specify user database summary file */
174 cutoff = atoi(optarg);
184 /* various argument checking */
186 errx(1, "only one of -f requires -v");
188 errx(1, "only one of -a and -v may be specified");
189 /* XXX need more argument checking */
192 /* initialize tables */
193 if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
194 errx(1, "process accounting initialization failed");
195 if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
196 errx(1, "user accounting initialization failed");
204 /* for each file specified */
205 for (; argc > 0; argc--, argv++) {
207 * load the accounting data from the file.
208 * if it fails, go on to the next file.
210 f = acct_load(argv[0], sflag);
214 if (!uflag && sflag) {
216 sigset_t nmask, omask;
220 * block most signals so we aren't interrupted during
223 if (sigfillset(&nmask) == -1) {
229 (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
230 warn("couldn't set signal mask");
237 * truncate the accounting data file ASAP, to avoid
238 * losing data. don't worry about errors in updating
239 * the saved stats; better to underbill than overbill,
240 * but we want every accounting record intact.
242 if (ftruncate(fileno(f), 0) == -1) {
243 warn("couldn't truncate %s", *argv);
248 * update saved user and process accounting data.
249 * note errors for later.
251 if (pacct_update() != 0 || usracct_update() != 0)
259 (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
260 warn("couldn't restore signal mask");
267 * close the opened accounting file
269 if (fclose(f) == EOF) {
270 warn("fclose %s", *argv);
275 if (!uflag && !qflag) {
276 /* print any results we may have obtained. */
284 /* finally, deallocate databases */
285 if (sflag || (!mflag && !qflag))
287 if (sflag || (mflag && !qflag))
297 (void)fprintf(stderr,
298 "usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n");
303 acct_load(const char *pn, int wr)
314 f = fopen(pn, wr ? "r+" : "r");
316 warn("open %s %s", pn, wr ? "for read/write" : "read-only");
321 * read all we can; don't stat and open because more processes
322 * could exit, and we'd miss them
325 /* get one accounting entry and punt if there's an error */
326 rv = readrec_forward(f, &ac);
329 warn("error reading %s", pn);
335 for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
337 char c = ac.ac_comm[i];
339 if (!isascii(c) || iscntrl(c)) {
341 ci.ci_flags |= CI_UNPRINTABLE;
345 if (ac.ac_flagx & AFORK)
346 ci.ci_comm[i++] = '*';
347 ci.ci_comm[i++] = '\0';
348 ci.ci_etime = ac.ac_etime;
349 ci.ci_utime = ac.ac_utime;
350 ci.ci_stime = ac.ac_stime;
351 ci.ci_uid = ac.ac_uid;
352 ci.ci_mem = ac.ac_mem;
356 /* and enter it into the usracct and pacct databases */
357 if (sflag || (!mflag && !qflag))
359 if (sflag || (mflag && !qflag))
362 printf("%6u %12.3lf cpu %12.0lfk mem %12.0lf io %s\n",
364 (ci.ci_utime + ci.ci_stime) / 1000000,
369 /* Finally, return the file stream for possible truncation. */
373 /* sort commands, doing the right thing in terms of reversals */
375 cmp_comm(const char *s1, const char *s2)
382 return (rflag ? rv : -rv);
385 /* sort by total user and system time */
387 cmp_usrsys(const DBT *d1, const DBT *d2)
389 struct cmdinfo c1, c2;
392 memcpy(&c1, d1->data, sizeof(c1));
393 memcpy(&c2, d2->data, sizeof(c2));
395 t1 = c1.ci_utime + c1.ci_stime;
396 t2 = c2.ci_utime + c2.ci_stime;
401 return (cmp_comm(c1.ci_comm, c2.ci_comm));
406 /* sort by average user and system time */
408 cmp_avgusrsys(const DBT *d1, const DBT *d2)
410 struct cmdinfo c1, c2;
413 memcpy(&c1, d1->data, sizeof(c1));
414 memcpy(&c2, d2->data, sizeof(c2));
416 t1 = c1.ci_utime + c1.ci_stime;
417 t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
419 t2 = c2.ci_utime + c2.ci_stime;
420 t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
425 return (cmp_comm(c1.ci_comm, c2.ci_comm));
430 /* sort by total number of disk I/O operations */
432 cmp_dkio(const DBT *d1, const DBT *d2)
434 struct cmdinfo c1, c2;
436 memcpy(&c1, d1->data, sizeof(c1));
437 memcpy(&c2, d2->data, sizeof(c2));
439 if (c1.ci_io < c2.ci_io)
441 else if (c1.ci_io == c2.ci_io)
442 return (cmp_comm(c1.ci_comm, c2.ci_comm));
447 /* sort by average number of disk I/O operations */
449 cmp_avgdkio(const DBT *d1, const DBT *d2)
451 struct cmdinfo c1, c2;
454 memcpy(&c1, d1->data, sizeof(c1));
455 memcpy(&c2, d2->data, sizeof(c2));
457 n1 = c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
458 n2 = c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
463 return (cmp_comm(c1.ci_comm, c2.ci_comm));
468 /* sort by the cpu-storage integral */
470 cmp_cpumem(const DBT *d1, const DBT *d2)
472 struct cmdinfo c1, c2;
474 memcpy(&c1, d1->data, sizeof(c1));
475 memcpy(&c2, d2->data, sizeof(c2));
477 if (c1.ci_mem < c2.ci_mem)
479 else if (c1.ci_mem == c2.ci_mem)
480 return (cmp_comm(c1.ci_comm, c2.ci_comm));
485 /* sort by the cpu-time average memory usage */
487 cmp_avgcpumem(const DBT *d1, const DBT *d2)
489 struct cmdinfo c1, c2;
493 memcpy(&c1, d1->data, sizeof(c1));
494 memcpy(&c2, d2->data, sizeof(c2));
496 t1 = c1.ci_utime + c1.ci_stime;
497 t2 = c2.ci_utime + c2.ci_stime;
499 n1 = c1.ci_mem / (t1 ? t1 : 1);
500 n2 = c2.ci_mem / (t2 ? t2 : 1);
505 return (cmp_comm(c1.ci_comm, c2.ci_comm));
510 /* sort by the number of invocations */
512 cmp_calls(const DBT *d1, const DBT *d2)
514 struct cmdinfo c1, c2;
516 memcpy(&c1, d1->data, sizeof(c1));
517 memcpy(&c2, d2->data, sizeof(c2));
519 if (c1.ci_calls < c2.ci_calls)
521 else if (c1.ci_calls == c2.ci_calls)
522 return (cmp_comm(c1.ci_comm, c2.ci_comm));