Deleted Added
sdiff udiff text old ( 169670 ) new ( 169857 )
full compact
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

--- 22 unchanged lines hidden (view full) ---

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: head/usr.sbin/sa/main.c 169670 2007-05-18 12:36:10Z dds $");
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 <fcntl.h>
50#include <signal.h>
51#include <stdint.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
56#include "extern.h"
57#include "pathnames.h"
58
59static int acct_load(const char *, int);
60static u_quad_t decode_comp_t(comp_t);
61static int cmp_comm(const char *, const char *);
62static int cmp_usrsys(const DBT *, const DBT *);
63static int cmp_avgusrsys(const DBT *, const DBT *);
64static int cmp_dkio(const DBT *, const DBT *);
65static int cmp_avgdkio(const DBT *, const DBT *);
66static int cmp_cpumem(const DBT *, const DBT *);
67static int cmp_avgcpumem(const DBT *, const DBT *);
68static int cmp_calls(const DBT *, const DBT *);
69static void usage(void);
70
71int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
72int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
73u_quad_t cutoff = 1;
74const char *pdb_file = _PATH_SAVACCT;
75const char *usrdb_file = _PATH_USRACCT;
76
77static char *dfltargv[] = { NULL };
78static int dfltargc = (sizeof dfltargv/sizeof(char *));
79
80/* default to comparing by sum of user + system time */
81cmpf_t sa_cmp = cmp_usrsys;
82
83int
84main(int argc, char **argv)
85{
86 char pathacct[] = _PATH_ACCT;
87 int ch, error = 0;
88
89 dfltargv[0] = pathacct;
90
91 while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1)
92 switch (ch) {
93 case 'a':

--- 111 unchanged lines hidden (view full) ---

205
206 if (argc == 0) {
207 argc = dfltargc;
208 argv = dfltargv;
209 }
210
211 /* for each file specified */
212 for (; argc > 0; argc--, argv++) {
213 int fd;
214
215 /*
216 * load the accounting data from the file.
217 * if it fails, go on to the next file.
218 */
219 fd = acct_load(argv[0], sflag);
220 if (fd < 0)
221 continue;
222
223 if (!uflag && sflag) {
224#ifndef DEBUG
225 sigset_t nmask, omask;
226 int unmask = 1;
227
228 /*

--- 14 unchanged lines hidden (view full) ---

243#endif /* DEBUG */
244
245 /*
246 * truncate the accounting data file ASAP, to avoid
247 * losing data. don't worry about errors in updating
248 * the saved stats; better to underbill than overbill,
249 * but we want every accounting record intact.
250 */
251 if (ftruncate(fd, 0) == -1) {
252 warn("couldn't truncate %s", *argv);
253 error = 1;
254 }
255
256 /*
257 * update saved user and process accounting data.
258 * note errors for later.
259 */

--- 10 unchanged lines hidden (view full) ---

270 error = 1;
271 }
272#endif /* DEBUG */
273 }
274
275 /*
276 * close the opened accounting file
277 */
278 if (close(fd) == -1) {
279 warn("close %s", *argv);
280 error = 1;
281 }
282 }
283
284 if (!uflag && !qflag) {
285 /* print any results we may have obtained. */
286 if (!mflag)
287 pacct_print();

--- 15 unchanged lines hidden (view full) ---

303static void
304usage()
305{
306 (void)fprintf(stderr,
307 "usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n");
308 exit(1);
309}
310
311static int
312acct_load(const char *pn, int wr)
313{
314 struct acct ac;
315 struct cmdinfo ci;
316 ssize_t rv;
317 int fd, i;
318
319 /*
320 * open the file
321 */
322 fd = open(pn, wr ? O_RDWR : O_RDONLY, 0);
323 if (fd == -1) {
324 warn("open %s %s", pn, wr ? "for read/write" : "read-only");
325 return (-1);
326 }
327
328 /*
329 * read all we can; don't stat and open because more processes
330 * could exit, and we'd miss them
331 */
332 while (1) {
333 /* get one accounting entry and punt if there's an error */
334 rv = read(fd, &ac, sizeof(struct acct));
335 if (rv == -1)
336 warn("error reading %s", pn);
337 else if (rv > 0 && rv < (int)sizeof(struct acct))
338 warnx("short read of accounting data in %s", pn);
339 if (rv != sizeof(struct acct))
340 break;
341
342 /* decode it */
343 ci.ci_calls = 1;
344 for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
345 i++) {
346 char c = ac.ac_comm[i];
347
348 if (!isascii(c) || iscntrl(c)) {
349 ci.ci_comm[i] = '?';
350 ci.ci_flags |= CI_UNPRINTABLE;
351 } else
352 ci.ci_comm[i] = c;
353 }
354 if (ac.ac_flag & AFORK)
355 ci.ci_comm[i++] = '*';
356 ci.ci_comm[i++] = '\0';
357 ci.ci_etime = decode_comp_t(ac.ac_etime);
358 ci.ci_utime = decode_comp_t(ac.ac_utime);
359 ci.ci_stime = decode_comp_t(ac.ac_stime);
360 ci.ci_uid = ac.ac_uid;
361 ci.ci_mem = ac.ac_mem;
362 ci.ci_io = decode_comp_t(ac.ac_io) / AHZ;
363
364 if (!uflag) {
365 /* and enter it into the usracct and pacct databases */
366 if (sflag || (!mflag && !qflag))
367 pacct_add(&ci);
368 if (sflag || (mflag && !qflag))
369 usracct_add(&ci);
370 } else if (!qflag)
371 printf("%6lu %12.2f cpu %12juk mem %12ju io %s\n",
372 ci.ci_uid,
373 (ci.ci_utime + ci.ci_stime) / (double) AHZ,
374 (uintmax_t)ci.ci_mem, (uintmax_t)ci.ci_io,
375 ci.ci_comm);
376 }
377
378 /* finally, return the file descriptor for possible truncation */
379 return (fd);
380}
381
382static u_quad_t
383decode_comp_t(comp_t comp)
384{
385 u_quad_t rv;
386
387 /*
388 * for more info on the comp_t format, see:
389 * /usr/src/sys/kern/kern_acct.c
390 * /usr/src/sys/sys/acct.h
391 * /usr/src/usr.bin/lastcomm/lastcomm.c
392 */
393 rv = comp & 0x1fff; /* 13 bit fraction */
394 comp >>= 13; /* 3 bit base-8 exponent */
395 while (comp--)
396 rv <<= 3;
397
398 return (rv);
399}
400
401/* sort commands, doing the right thing in terms of reversals */
402static int
403cmp_comm(const char *s1, const char *s2)
404{
405 int rv;
406
407 rv = strcmp(s1, s2);
408 if (rv == 0)
409 rv = -1;
410 return (rflag ? rv : -rv);
411}
412
413/* sort by total user and system time */
414static int
415cmp_usrsys(const DBT *d1, const DBT *d2)
416{
417 struct cmdinfo c1, c2;
418 u_quad_t t1, t2;
419
420 memcpy(&c1, d1->data, sizeof(c1));
421 memcpy(&c2, d2->data, sizeof(c2));
422
423 t1 = c1.ci_utime + c1.ci_stime;
424 t2 = c2.ci_utime + c2.ci_stime;
425
426 if (t1 < t2)

--- 50 unchanged lines hidden (view full) ---

477cmp_avgdkio(const DBT *d1, const DBT *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}

--- 15 unchanged lines hidden (view full) ---

510 return 1;
511}
512
513/* sort by the cpu-time average memory usage */
514static int
515cmp_avgcpumem(const DBT *d1, const DBT *d2)
516{
517 struct cmdinfo c1, c2;
518 u_quad_t t1, t2;
519 double n1, n2;
520
521 memcpy(&c1, d1->data, sizeof(c1));
522 memcpy(&c2, d2->data, sizeof(c2));
523
524 t1 = c1.ci_utime + c1.ci_stime;
525 t2 = c2.ci_utime + c2.ci_stime;
526
527 n1 = (double) c1.ci_mem / (double) (t1 ? t1 : 1);
528 n2 = (double) c2.ci_mem / (double) (t2 ? t2 : 1);
529
530 if (n1 < n2)
531 return -1;
532 else if (n1 == n2)
533 return (cmp_comm(c1.ci_comm, c2.ci_comm));
534 else
535 return 1;
536}

--- 17 unchanged lines hidden ---