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