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 169857 2007-05-22 06:51:38Z 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 <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); 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 FILE *f; |
87 char pathacct[] = _PATH_ACCT; 88 int ch, error = 0; 89 90 dfltargv[0] = pathacct; 91 92 while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1) 93 switch (ch) { 94 case 'a': --- 111 unchanged lines hidden (view full) --- 206 207 if (argc == 0) { 208 argc = dfltargc; 209 argv = dfltargv; 210 } 211 212 /* for each file specified */ 213 for (; argc > 0; argc--, argv++) { |
214 /* 215 * load the accounting data from the file. 216 * if it fails, go on to the next file. 217 */ |
218 f = acct_load(argv[0], sflag); 219 if (f == NULL) |
220 continue; 221 222 if (!uflag && sflag) { 223#ifndef DEBUG 224 sigset_t nmask, omask; 225 int unmask = 1; 226 227 /* --- 14 unchanged lines hidden (view full) --- 242#endif /* DEBUG */ 243 244 /* 245 * truncate the accounting data file ASAP, to avoid 246 * losing data. don't worry about errors in updating 247 * the saved stats; better to underbill than overbill, 248 * but we want every accounting record intact. 249 */ |
250 if (ftruncate(fileno(f), 0) == -1) { |
251 warn("couldn't truncate %s", *argv); 252 error = 1; 253 } 254 255 /* 256 * update saved user and process accounting data. 257 * note errors for later. 258 */ --- 10 unchanged lines hidden (view full) --- 269 error = 1; 270 } 271#endif /* DEBUG */ 272 } 273 274 /* 275 * close the opened accounting file 276 */ |
277 if (fclose(f) == EOF) { 278 warn("fclose %s", *argv); |
279 error = 1; 280 } 281 } 282 283 if (!uflag && !qflag) { 284 /* print any results we may have obtained. */ 285 if (!mflag) 286 pacct_print(); --- 15 unchanged lines hidden (view full) --- 302static void 303usage() 304{ 305 (void)fprintf(stderr, 306 "usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n"); 307 exit(1); 308} 309 |
310static FILE * |
311acct_load(const char *pn, int wr) 312{ |
313 struct acctv2 ac; |
314 struct cmdinfo ci; 315 ssize_t rv; |
316 FILE *f; 317 int i; |
318 319 /* 320 * open the file 321 */ |
322 f = fopen(pn, wr ? "r+" : "r"); 323 if (f == NULL) { |
324 warn("open %s %s", pn, wr ? "for read/write" : "read-only"); |
325 return (NULL); |
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 = readrec_forward(f, &ac); 335 if (rv != 1) { 336 if (rv == EOF) 337 warn("error reading %s", pn); |
338 break; |
339 } |
340 341 /* decode it */ 342 ci.ci_calls = 1; 343 for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0'; 344 i++) { 345 char c = ac.ac_comm[i]; 346 347 if (!isascii(c) || iscntrl(c)) { 348 ci.ci_comm[i] = '?'; 349 ci.ci_flags |= CI_UNPRINTABLE; 350 } else 351 ci.ci_comm[i] = c; 352 } |
353 if (ac.ac_flagx & AFORK) |
354 ci.ci_comm[i++] = '*'; 355 ci.ci_comm[i++] = '\0'; |
356 ci.ci_etime = ac.ac_etime; 357 ci.ci_utime = ac.ac_utime; 358 ci.ci_stime = ac.ac_stime; |
359 ci.ci_uid = ac.ac_uid; 360 ci.ci_mem = ac.ac_mem; |
361 ci.ci_io = ac.ac_io; |
362 363 if (!uflag) { 364 /* and enter it into the usracct and pacct databases */ 365 if (sflag || (!mflag && !qflag)) 366 pacct_add(&ci); 367 if (sflag || (mflag && !qflag)) 368 usracct_add(&ci); 369 } else if (!qflag) |
370 printf("%6u %12.3lf cpu %12.0lfk mem %12.0lf io %s\n", |
371 ci.ci_uid, |
372 (ci.ci_utime + ci.ci_stime) / 1000000, 373 ci.ci_mem, ci.ci_io, |
374 ci.ci_comm); 375 } 376 |
377 /* Finally, return the file stream for possible truncation. */ 378 return (f); |
379} 380 |
381/* sort commands, doing the right thing in terms of reversals */ 382static int 383cmp_comm(const char *s1, const char *s2) 384{ 385 int rv; 386 387 rv = strcmp(s1, s2); 388 if (rv == 0) 389 rv = -1; 390 return (rflag ? rv : -rv); 391} 392 393/* sort by total user and system time */ 394static int 395cmp_usrsys(const DBT *d1, const DBT *d2) 396{ 397 struct cmdinfo c1, c2; |
398 double t1, t2; |
399 400 memcpy(&c1, d1->data, sizeof(c1)); 401 memcpy(&c2, d2->data, sizeof(c2)); 402 403 t1 = c1.ci_utime + c1.ci_stime; 404 t2 = c2.ci_utime + c2.ci_stime; 405 406 if (t1 < t2) --- 50 unchanged lines hidden (view full) --- 457cmp_avgdkio(const DBT *d1, const DBT *d2) 458{ 459 struct cmdinfo c1, c2; 460 double n1, n2; 461 462 memcpy(&c1, d1->data, sizeof(c1)); 463 memcpy(&c2, d2->data, sizeof(c2)); 464 |
465 n1 = c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1); 466 n2 = c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1); |
467 468 if (n1 < n2) 469 return -1; 470 else if (n1 == n2) 471 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 472 else 473 return 1; 474} --- 15 unchanged lines hidden (view full) --- 490 return 1; 491} 492 493/* sort by the cpu-time average memory usage */ 494static int 495cmp_avgcpumem(const DBT *d1, const DBT *d2) 496{ 497 struct cmdinfo c1, c2; |
498 double t1, t2; |
499 double n1, n2; 500 501 memcpy(&c1, d1->data, sizeof(c1)); 502 memcpy(&c2, d2->data, sizeof(c2)); 503 504 t1 = c1.ci_utime + c1.ci_stime; 505 t2 = c2.ci_utime + c2.ci_stime; 506 |
507 n1 = c1.ci_mem / (t1 ? t1 : 1); 508 n2 = c2.ci_mem / (t2 ? t2 : 1); |
509 510 if (n1 < n2) 511 return -1; 512 else if (n1 == n2) 513 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 514 else 515 return 1; 516} --- 17 unchanged lines hidden --- |