1/*
2 * Copyright (c) 1994 Christopher G. Demetriou
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
18 *
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.
29 */
30
31#if 0
32#ifndef lint
33static const char copyright[] =
34"@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
35 All rights reserved.\n";
36#endif
37#endif
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: src/usr.sbin/sa/main.c,v 1.18 2007/05/22 06:51:38 dds Exp $");
40
41/*
42 * sa:	system accounting
43 */
44
45#include <sys/types.h>
46#include <sys/acct.h>
47#include <ctype.h>
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <signal.h>
52#include <stdint.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57#include "extern.h"
58#include "pathnames.h"
59
60static FILE	*acct_load(const char *, int);
61#ifdef __APPLE__
62static u_quad_t	decode_comp_t(comp_t);
63#endif
64static int	 cmp_comm(const char *, const char *);
65static int	 cmp_usrsys(const DBT *, const DBT *);
66static int	 cmp_avgusrsys(const DBT *, const DBT *);
67static int	 cmp_dkio(const DBT *, const DBT *);
68static int	 cmp_avgdkio(const DBT *, const DBT *);
69static int	 cmp_cpumem(const DBT *, const DBT *);
70static int	 cmp_avgcpumem(const DBT *, const DBT *);
71static int	 cmp_calls(const DBT *, const DBT *);
72static void	 usage(void);
73
74int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
75int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
76u_quad_t cutoff = 1;
77const char *pdb_file = _PATH_SAVACCT;
78const char *usrdb_file = _PATH_USRACCT;
79
80static char	*dfltargv[] = { NULL };
81static int	dfltargc = (sizeof dfltargv/sizeof(char *));
82
83/* default to comparing by sum of user + system time */
84cmpf_t   sa_cmp = cmp_usrsys;
85
86int
87main(int argc, char **argv)
88{
89	FILE *f;
90	char pathacct[] = _PATH_ACCT;
91	int ch, error = 0;
92
93	dfltargv[0] = pathacct;
94
95	while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1)
96		switch (ch) {
97			case 'a':
98				/* print all commands */
99				aflag = 1;
100				break;
101			case 'b':
102				/* sort by per-call user/system time average */
103				bflag = 1;
104				sa_cmp = cmp_avgusrsys;
105				break;
106			case 'c':
107				/* print percentage total time */
108				cflag = 1;
109				break;
110			case 'd':
111				/* sort by averge number of disk I/O ops */
112				dflag = 1;
113				sa_cmp = cmp_avgdkio;
114				break;
115			case 'D':
116				/* print and sort by total disk I/O ops */
117				Dflag = 1;
118				sa_cmp = cmp_dkio;
119				break;
120			case 'f':
121				/* force no interactive threshold comprison */
122				fflag = 1;
123				break;
124			case 'i':
125				/* do not read in summary file */
126				iflag = 1;
127				break;
128			case 'j':
129				/* instead of total minutes, give sec/call */
130				jflag = 1;
131				break;
132			case 'k':
133				/* sort by cpu-time average memory usage */
134				kflag = 1;
135				sa_cmp = cmp_avgcpumem;
136				break;
137			case 'K':
138				/* print and sort by cpu-storage integral */
139				sa_cmp = cmp_cpumem;
140				Kflag = 1;
141				break;
142			case 'l':
143				/* separate system and user time */
144				lflag = 1;
145				break;
146			case 'm':
147				/* print procs and time per-user */
148				mflag = 1;
149				break;
150			case 'n':
151				/* sort by number of calls */
152				sa_cmp = cmp_calls;
153				break;
154			case 'P':
155				/* specify program database summary file */
156				pdb_file = optarg;
157				break;
158			case 'q':
159				/* quiet; error messages only */
160				qflag = 1;
161				break;
162			case 'r':
163				/* reverse order of sort */
164				rflag = 1;
165				break;
166			case 's':
167				/* merge accounting file into summaries */
168				sflag = 1;
169				break;
170			case 't':
171				/* report ratio of user and system times */
172				tflag = 1;
173				break;
174			case 'u':
175				/* first, print uid and command name */
176				uflag = 1;
177				break;
178			case 'U':
179				/* specify user database summary file */
180				usrdb_file = optarg;
181				break;
182			case 'v':
183				/* cull junk */
184				vflag = 1;
185				cutoff = atoi(optarg);
186				break;
187			case '?':
188	                default:
189				usage();
190		}
191
192	argc -= optind;
193	argv += optind;
194
195	/* various argument checking */
196	if (fflag && !vflag)
197		errx(1, "only one of -f requires -v");
198	if (fflag && aflag)
199		errx(1, "only one of -a and -v may be specified");
200	/* XXX need more argument checking */
201
202	if (!uflag) {
203		/* initialize tables */
204		if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
205			errx(1, "process accounting initialization failed");
206		if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
207			errx(1, "user accounting initialization failed");
208	}
209
210	if (argc == 0) {
211		argc = dfltargc;
212		argv = dfltargv;
213	}
214
215	/* for each file specified */
216	for (; argc > 0; argc--, argv++) {
217		/*
218		 * load the accounting data from the file.
219		 * if it fails, go on to the next file.
220		 */
221		f = acct_load(argv[0], sflag);
222		if (f == NULL)
223			continue;
224
225		if (!uflag && sflag) {
226#ifndef DEBUG
227			sigset_t nmask, omask;
228			int unmask = 1;
229
230			/*
231			 * block most signals so we aren't interrupted during
232			 * the update.
233			 */
234			if (sigfillset(&nmask) == -1) {
235				warn("sigfillset");
236				unmask = 0;
237				error = 1;
238			}
239			if (unmask &&
240			    (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
241				warn("couldn't set signal mask");
242				unmask = 0;
243				error = 1;
244			}
245#endif /* DEBUG */
246
247			/*
248			 * truncate the accounting data file ASAP, to avoid
249			 * losing data.  don't worry about errors in updating
250			 * the saved stats; better to underbill than overbill,
251			 * but we want every accounting record intact.
252			 */
253			if (ftruncate(fileno(f), 0) == -1) {
254				warn("couldn't truncate %s", *argv);
255				error = 1;
256			}
257
258			/*
259			 * update saved user and process accounting data.
260			 * note errors for later.
261			 */
262			if (pacct_update() != 0 || usracct_update() != 0)
263				error = 1;
264
265#ifndef DEBUG
266			/*
267			 * restore signals
268			 */
269			if (unmask &&
270			    (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
271				warn("couldn't restore signal mask");
272				error = 1;
273			}
274#endif /* DEBUG */
275		}
276
277		/*
278		 * close the opened accounting file
279		 */
280		if (fclose(f) == EOF) {
281			warn("fclose %s", *argv);
282			error = 1;
283		}
284	}
285
286	if (!uflag && !qflag) {
287		/* print any results we may have obtained. */
288		if (!mflag)
289			pacct_print();
290		else
291			usracct_print();
292	}
293
294	if (!uflag) {
295		/* finally, deallocate databases */
296		if (sflag || (!mflag && !qflag))
297			pacct_destroy();
298		if (sflag || (mflag && !qflag))
299			usracct_destroy();
300	}
301
302	exit(error);
303}
304
305static void
306usage()
307{
308	(void)fprintf(stderr,
309		"usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n");
310	exit(1);
311}
312
313static FILE *
314acct_load(const char *pn, int wr)
315{
316#ifdef __APPLE__
317	struct acct ac;
318#else
319	struct acctv2 ac;
320#endif
321	struct cmdinfo ci;
322	ssize_t rv;
323	FILE *f;
324	int i;
325
326	/*
327	 * open the file
328	 */
329	f = fopen(pn, wr ? "r+" : "r");
330	if (f == NULL) {
331		warn("open %s %s", pn, wr ? "for read/write" : "read-only");
332		return (NULL);
333	}
334
335	/*
336	 * read all we can; don't stat and open because more processes
337	 * could exit, and we'd miss them
338	 */
339	while (1) {
340		/* get one accounting entry and punt if there's an error */
341#ifdef __APPLE__
342		rv = read(fileno(f), &ac, sizeof(struct acct));
343		if (rv == -1)
344			warn("error reading %s", pn);
345		else if (rv > 0 && rv < (int)sizeof(struct acct))
346			warnx("short read of accounting data in %s", pn);
347		if (rv != sizeof(struct acct))
348			break;
349#else
350		rv = readrec_forward(f, &ac);
351		if (rv != 1) {
352			if (rv == EOF)
353				warn("error reading %s", pn);
354			break;
355		}
356#endif
357
358		/* decode it */
359		ci.ci_calls = 1;
360		for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
361		    i++) {
362			char c = ac.ac_comm[i];
363
364			if (!isascii(c) || iscntrl(c)) {
365				ci.ci_comm[i] = '?';
366				ci.ci_flags |= CI_UNPRINTABLE;
367			} else
368				ci.ci_comm[i] = c;
369		}
370#ifdef __APPLE__
371		if (ac.ac_flag & AFORK)
372			ci.ci_comm[i++] = '*';
373		ci.ci_comm[i++] = '\0';
374		ci.ci_etime = decode_comp_t(ac.ac_etime);
375		ci.ci_utime = decode_comp_t(ac.ac_utime);
376		ci.ci_stime = decode_comp_t(ac.ac_stime);
377		ci.ci_uid = ac.ac_uid;
378		ci.ci_mem = ac.ac_mem;
379		ci.ci_io = decode_comp_t(ac.ac_io) / AHZ;
380#else
381		if (ac.ac_flagx & AFORK)
382			ci.ci_comm[i++] = '*';
383		ci.ci_comm[i++] = '\0';
384		ci.ci_etime = ac.ac_etime;
385		ci.ci_utime = ac.ac_utime;
386		ci.ci_stime = ac.ac_stime;
387		ci.ci_uid = ac.ac_uid;
388		ci.ci_mem = ac.ac_mem;
389		ci.ci_io = ac.ac_io;
390#endif
391
392		if (!uflag) {
393			/* and enter it into the usracct and pacct databases */
394			if (sflag || (!mflag && !qflag))
395				pacct_add(&ci);
396			if (sflag || (mflag && !qflag))
397				usracct_add(&ci);
398		} else if (!qflag)
399#ifdef __APPLE__
400			printf("%6u %12.2f cpu %12juk mem %12ju io %s\n",
401			    ci.ci_uid,
402			    (ci.ci_utime + ci.ci_stime) / (double) AHZ,
403			    (uintmax_t)ci.ci_mem, (uintmax_t)ci.ci_io,
404			    ci.ci_comm);
405#else
406			printf("%6u %12.3lf cpu %12.0lfk mem %12.0lf io %s\n",
407			    ci.ci_uid,
408			    (ci.ci_utime + ci.ci_stime) / 1000000,
409			    ci.ci_mem, ci.ci_io,
410			    ci.ci_comm);
411#endif
412	}
413
414	/* Finally, return the file stream for possible truncation. */
415	return (f);
416}
417
418#ifdef __APPLE__
419static u_quad_t decode_comp_t(comp_t comp)
420{
421	u_quad_t rv;
422
423	/*
424	 * for more info on the comp_t format, see:
425	 *	/usr/src/sys/kern/kern_acct.c
426	 *	/usr/src/sys/sys/acct.h
427	 *	/usr/src/usr.bin/lastcomm/lastcomm.c
428	 */
429	rv = comp & 0x1fff;	/* 13 bit fraction */
430	comp >>= 13;		/* 3 bit base-8 exponent */
431	while (comp--)
432		rv <<= 3;
433
434	return (rv);
435}
436#endif
437
438/* sort commands, doing the right thing in terms of reversals */
439static int
440cmp_comm(const char *s1, const char *s2)
441{
442	int rv;
443
444	rv = strcmp(s1, s2);
445	if (rv == 0)
446		rv = -1;
447	return (rflag ? rv : -rv);
448}
449
450/* sort by total user and system time */
451static int
452cmp_usrsys(const DBT *d1, const DBT *d2)
453{
454	struct cmdinfo c1, c2;
455#ifdef __APPLE__
456	u_quad_t t1, t2;
457#else
458	double t1, t2;
459#endif
460
461	memcpy(&c1, d1->data, sizeof(c1));
462	memcpy(&c2, d2->data, sizeof(c2));
463
464	t1 = c1.ci_utime + c1.ci_stime;
465	t2 = c2.ci_utime + c2.ci_stime;
466
467	if (t1 < t2)
468		return -1;
469	else if (t1 == t2)
470		return (cmp_comm(c1.ci_comm, c2.ci_comm));
471	else
472		return 1;
473}
474
475/* sort by average user and system time */
476static int
477cmp_avgusrsys(const DBT *d1, const DBT *d2)
478{
479	struct cmdinfo c1, c2;
480	double t1, t2;
481
482	memcpy(&c1, d1->data, sizeof(c1));
483	memcpy(&c2, d2->data, sizeof(c2));
484
485	t1 = c1.ci_utime + c1.ci_stime;
486	t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
487
488	t2 = c2.ci_utime + c2.ci_stime;
489	t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
490
491	if (t1 < t2)
492		return -1;
493	else if (t1 == t2)
494		return (cmp_comm(c1.ci_comm, c2.ci_comm));
495	else
496		return 1;
497}
498
499/* sort by total number of disk I/O operations */
500static int
501cmp_dkio(const DBT *d1, const DBT *d2)
502{
503	struct cmdinfo c1, c2;
504
505	memcpy(&c1, d1->data, sizeof(c1));
506	memcpy(&c2, d2->data, sizeof(c2));
507
508	if (c1.ci_io < c2.ci_io)
509		return -1;
510	else if (c1.ci_io == c2.ci_io)
511		return (cmp_comm(c1.ci_comm, c2.ci_comm));
512	else
513		return 1;
514}
515
516/* sort by average number of disk I/O operations */
517static int
518cmp_avgdkio(const DBT *d1, const DBT *d2)
519{
520	struct cmdinfo c1, c2;
521	double n1, n2;
522
523	memcpy(&c1, d1->data, sizeof(c1));
524	memcpy(&c2, d2->data, sizeof(c2));
525
526#ifdef __APPLE__
527	n1 = (double) c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
528	n2 = (double) c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
529#else
530	n1 = c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
531	n2 = c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
532#endif
533
534	if (n1 < n2)
535		return -1;
536	else if (n1 == n2)
537		return (cmp_comm(c1.ci_comm, c2.ci_comm));
538	else
539		return 1;
540}
541
542/* sort by the cpu-storage integral */
543static int
544cmp_cpumem(const DBT *d1, const DBT *d2)
545{
546	struct cmdinfo c1, c2;
547
548	memcpy(&c1, d1->data, sizeof(c1));
549	memcpy(&c2, d2->data, sizeof(c2));
550
551	if (c1.ci_mem < c2.ci_mem)
552		return -1;
553	else if (c1.ci_mem == c2.ci_mem)
554		return (cmp_comm(c1.ci_comm, c2.ci_comm));
555	else
556		return 1;
557}
558
559/* sort by the cpu-time average memory usage */
560static int
561cmp_avgcpumem(const DBT *d1, const DBT *d2)
562{
563	struct cmdinfo c1, c2;
564#ifdef __APPLE__
565	u_quad_t t1, t2;
566#else
567	double t1, t2;
568#endif
569	double n1, n2;
570
571	memcpy(&c1, d1->data, sizeof(c1));
572	memcpy(&c2, d2->data, sizeof(c2));
573
574	t1 = c1.ci_utime + c1.ci_stime;
575	t2 = c2.ci_utime + c2.ci_stime;
576
577#ifdef __APPLE__
578	n1 = (double) c1.ci_mem / (double) (t1 ? t1 : 1);
579	n2 = (double) c2.ci_mem / (double) (t2 ? t2 : 1);
580#else
581	n1 = c1.ci_mem / (t1 ? t1 : 1);
582	n2 = c2.ci_mem / (t2 ? t2 : 1);
583#endif
584
585	if (n1 < n2)
586		return -1;
587	else if (n1 == n2)
588		return (cmp_comm(c1.ci_comm, c2.ci_comm));
589	else
590		return 1;
591}
592
593/* sort by the number of invocations */
594static int
595cmp_calls(const DBT *d1, const DBT *d2)
596{
597	struct cmdinfo c1, c2;
598
599	memcpy(&c1, d1->data, sizeof(c1));
600	memcpy(&c2, d2->data, sizeof(c2));
601
602	if (c1.ci_calls < c2.ci_calls)
603		return -1;
604	else if (c1.ci_calls == c2.ci_calls)
605		return (cmp_comm(c1.ci_comm, c2.ci_comm));
606	else
607		return 1;
608}
609