Deleted Added
full compact
main.c (72091) main.c (99829)
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#ifndef lint
32static const char copyright[] =
33"@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
34 All rights reserved.\n";
35#endif
36
37#ifndef lint
38static const char rcsid[] =
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#ifndef lint
32static const char copyright[] =
33"@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
34 All rights reserved.\n";
35#endif
36
37#ifndef lint
38static const char rcsid[] =
39 "$FreeBSD: head/usr.sbin/sa/main.c 72091 2001-02-06 11:21:58Z asmodai $";
39 "$FreeBSD: head/usr.sbin/sa/main.c 99829 2002-07-11 22:11:20Z alfred $";
40#endif /* not lint */
41
42/*
43 * sa: system accounting
44 */
45
46#include <sys/types.h>
47#include <sys/acct.h>
48#include <ctype.h>
49#include <err.h>
50#include <fcntl.h>
51#include <signal.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
40#endif /* not lint */
41
42/*
43 * sa: system accounting
44 */
45
46#include <sys/types.h>
47#include <sys/acct.h>
48#include <ctype.h>
49#include <err.h>
50#include <fcntl.h>
51#include <signal.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 __P((char *, int));
60static u_quad_t decode_comp_t __P((comp_t));
61static int cmp_comm __P((const char *, const char *));
62static int cmp_usrsys __P((const DBT *, const DBT *));
63static int cmp_avgusrsys __P((const DBT *, const DBT *));
64static int cmp_dkio __P((const DBT *, const DBT *));
65static int cmp_avgdkio __P((const DBT *, const DBT *));
66static int cmp_cpumem __P((const DBT *, const DBT *));
67static int cmp_avgcpumem __P((const DBT *, const DBT *));
68static int cmp_calls __P((const DBT *, const DBT *));
69static void usage __P((void));
59static int acct_load(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;
70
71int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
72int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
73int cutoff = 1;
73u_quad_t cutoff = 1;
74
74
75static char *dfltargv[] = { _PATH_ACCT };
75static char *dfltargv[] = { NULL };
76static int dfltargc = (sizeof dfltargv/sizeof(char *));
77
78/* default to comparing by sum of user + system time */
79cmpf_t sa_cmp = cmp_usrsys;
80
81int
76static int dfltargc = (sizeof dfltargv/sizeof(char *));
77
78/* default to comparing by sum of user + system time */
79cmpf_t sa_cmp = cmp_usrsys;
80
81int
82main(argc, argv)
83 int argc;
84 char **argv;
82main(int argc, char **argv)
85{
86 char ch;
83{
84 char ch;
85 char pathacct[] = _PATH_ACCT;
87 int error = 0;
88
86 int error = 0;
87
88 dfltargv[0] = pathacct;
89
89 while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1)
90 switch (ch) {
91 case 'a':
92 /* print all commands */
93 aflag = 1;
94 break;
95 case 'b':
96 /* sort by per-call user/system time average */

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

234
235 /*
236 * truncate the accounting data file ASAP, to avoid
237 * losing data. don't worry about errors in updating
238 * the saved stats; better to underbill than overbill,
239 * but we want every accounting record intact.
240 */
241 if (ftruncate(fd, 0) == -1) {
90 while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1)
91 switch (ch) {
92 case 'a':
93 /* print all commands */
94 aflag = 1;
95 break;
96 case 'b':
97 /* sort by per-call user/system time average */

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

235
236 /*
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.
241 */
242 if (ftruncate(fd, 0) == -1) {
242 warn("couldn't truncate %s", argv);
243 warn("couldn't truncate %s", *argv);
243 error = 1;
244 }
245
246 /*
247 * update saved user and process accounting data.
248 * note errors for later.
249 */
250 if (pacct_update() != 0 || usracct_update() != 0)

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

261 }
262#endif /* DEBUG */
263 }
264
265 /*
266 * close the opened accounting file
267 */
268 if (close(fd) == -1) {
244 error = 1;
245 }
246
247 /*
248 * update saved user and process accounting data.
249 * note errors for later.
250 */
251 if (pacct_update() != 0 || usracct_update() != 0)

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

262 }
263#endif /* DEBUG */
264 }
265
266 /*
267 * close the opened accounting file
268 */
269 if (close(fd) == -1) {
269 warn("close %s", argv);
270 warn("close %s", *argv);
270 error = 1;
271 }
272 }
273
274 if (!uflag && !qflag) {
275 /* print any results we may have obtained. */
276 if (!mflag)
277 pacct_print();

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

321 * read all we can; don't stat and open because more processes
322 * could exit, and we'd miss them
323 */
324 while (1) {
325 /* get one accounting entry and punt if there's an error */
326 rv = read(fd, &ac, sizeof(struct acct));
327 if (rv == -1)
328 warn("error reading %s", pn);
271 error = 1;
272 }
273 }
274
275 if (!uflag && !qflag) {
276 /* print any results we may have obtained. */
277 if (!mflag)
278 pacct_print();

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

322 * read all we can; don't stat and open because more processes
323 * could exit, and we'd miss them
324 */
325 while (1) {
326 /* get one accounting entry and punt if there's an error */
327 rv = read(fd, &ac, sizeof(struct acct));
328 if (rv == -1)
329 warn("error reading %s", pn);
329 else if (rv > 0 && rv < sizeof(struct acct))
330 else if (rv > 0 && rv < (int)sizeof(struct acct))
330 warnx("short read of accounting data in %s", pn);
331 if (rv != sizeof(struct acct))
332 break;
333
334 /* decode it */
335 ci.ci_calls = 1;
331 warnx("short read of accounting data in %s", pn);
332 if (rv != sizeof(struct acct))
333 break;
334
335 /* decode it */
336 ci.ci_calls = 1;
336 for (i = 0; i < sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
337 for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
337 i++) {
338 char c = ac.ac_comm[i];
339
340 if (!isascii(c) || iscntrl(c)) {
341 ci.ci_comm[i] = '?';
342 ci.ci_flags |= CI_UNPRINTABLE;
343 } else
344 ci.ci_comm[i] = c;

--- 209 unchanged lines hidden ---
338 i++) {
339 char c = ac.ac_comm[i];
340
341 if (!isascii(c) || iscntrl(c)) {
342 ci.ci_comm[i] = '?';
343 ci.ci_flags |= CI_UNPRINTABLE;
344 } else
345 ci.ci_comm[i] = c;

--- 209 unchanged lines hidden ---