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 33 static 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$"); 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 59 static int acct_load(char *, int); 60 static u_quad_t decode_comp_t(comp_t); 61 static int cmp_comm(const char *, const char *); 62 static int cmp_usrsys(const DBT *, const DBT *); 63 static int cmp_avgusrsys(const DBT *, const DBT *); 64 static int cmp_dkio(const DBT *, const DBT *); 65 static int cmp_avgdkio(const DBT *, const DBT *); 66 static int cmp_cpumem(const DBT *, const DBT *); 67 static int cmp_avgcpumem(const DBT *, const DBT *); 68 static int cmp_calls(const DBT *, const DBT *); 69 static void usage(void); 70 71 int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag; 72 int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag; 73 u_quad_t cutoff = 1; 74 75 static char *dfltargv[] = { NULL }; 76 static int dfltargc = (sizeof dfltargv/sizeof(char *)); 77 78 /* default to comparing by sum of user + system time */ 79 cmpf_t sa_cmp = cmp_usrsys; 80 81 int 82 main(int argc, char **argv) 83 { 84 char pathacct[] = _PATH_ACCT; 85 int ch, error = 0; 86 87 dfltargv[0] = pathacct; 88 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 */ 97 bflag = 1; 98 sa_cmp = cmp_avgusrsys; 99 break; 100 case 'c': 101 /* print percentage total time */ 102 cflag = 1; 103 break; 104 case 'd': 105 /* sort by averge number of disk I/O ops */ 106 dflag = 1; 107 sa_cmp = cmp_avgdkio; 108 break; 109 case 'D': 110 /* print and sort by total disk I/O ops */ 111 Dflag = 1; 112 sa_cmp = cmp_dkio; 113 break; 114 case 'f': 115 /* force no interactive threshold comprison */ 116 fflag = 1; 117 break; 118 case 'i': 119 /* do not read in summary file */ 120 iflag = 1; 121 break; 122 case 'j': 123 /* instead of total minutes, give sec/call */ 124 jflag = 1; 125 break; 126 case 'k': 127 /* sort by cpu-time average memory usage */ 128 kflag = 1; 129 sa_cmp = cmp_avgcpumem; 130 break; 131 case 'K': 132 /* print and sort by cpu-storage integral */ 133 sa_cmp = cmp_cpumem; 134 Kflag = 1; 135 break; 136 case 'l': 137 /* separate system and user time */ 138 lflag = 1; 139 break; 140 case 'm': 141 /* print procs and time per-user */ 142 mflag = 1; 143 break; 144 case 'n': 145 /* sort by number of calls */ 146 sa_cmp = cmp_calls; 147 break; 148 case 'q': 149 /* quiet; error messages only */ 150 qflag = 1; 151 break; 152 case 'r': 153 /* reverse order of sort */ 154 rflag = 1; 155 break; 156 case 's': 157 /* merge accounting file into summaries */ 158 sflag = 1; 159 break; 160 case 't': 161 /* report ratio of user and system times */ 162 tflag = 1; 163 break; 164 case 'u': 165 /* first, print uid and command name */ 166 uflag = 1; 167 break; 168 case 'v': 169 /* cull junk */ 170 vflag = 1; 171 cutoff = atoi(optarg); 172 break; 173 case '?': 174 default: 175 usage(); 176 } 177 178 argc -= optind; 179 argv += optind; 180 181 /* various argument checking */ 182 if (fflag && !vflag) 183 errx(1, "only one of -f requires -v"); 184 if (fflag && aflag) 185 errx(1, "only one of -a and -v may be specified"); 186 /* XXX need more argument checking */ 187 188 if (!uflag) { 189 /* initialize tables */ 190 if ((sflag || (!mflag && !qflag)) && pacct_init() != 0) 191 errx(1, "process accounting initialization failed"); 192 if ((sflag || (mflag && !qflag)) && usracct_init() != 0) 193 errx(1, "user accounting initialization failed"); 194 } 195 196 if (argc == 0) { 197 argc = dfltargc; 198 argv = dfltargv; 199 } 200 201 /* for each file specified */ 202 for (; argc > 0; argc--, argv++) { 203 int fd; 204 205 /* 206 * load the accounting data from the file. 207 * if it fails, go on to the next file. 208 */ 209 fd = acct_load(argv[0], sflag); 210 if (fd < 0) 211 continue; 212 213 if (!uflag && sflag) { 214 #ifndef DEBUG 215 sigset_t nmask, omask; 216 int unmask = 1; 217 218 /* 219 * block most signals so we aren't interrupted during 220 * the update. 221 */ 222 if (sigfillset(&nmask) == -1) { 223 warn("sigfillset"); 224 unmask = 0; 225 error = 1; 226 } 227 if (unmask && 228 (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) { 229 warn("couldn't set signal mask"); 230 unmask = 0; 231 error = 1; 232 } 233 #endif /* DEBUG */ 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) { 242 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) 251 error = 1; 252 253 #ifndef DEBUG 254 /* 255 * restore signals 256 */ 257 if (unmask && 258 (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) { 259 warn("couldn't restore signal mask"); 260 error = 1; 261 } 262 #endif /* DEBUG */ 263 } 264 265 /* 266 * close the opened accounting file 267 */ 268 if (close(fd) == -1) { 269 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(); 278 else 279 usracct_print(); 280 } 281 282 if (!uflag) { 283 /* finally, deallocate databases */ 284 if (sflag || (!mflag && !qflag)) 285 pacct_destroy(); 286 if (sflag || (mflag && !qflag)) 287 usracct_destroy(); 288 } 289 290 exit(error); 291 } 292 293 static void 294 usage() 295 { 296 (void)fprintf(stderr, 297 "usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n"); 298 exit(1); 299 } 300 301 static int 302 acct_load(pn, wr) 303 char *pn; 304 int wr; 305 { 306 struct acct ac; 307 struct cmdinfo ci; 308 ssize_t rv; 309 int fd, i; 310 311 /* 312 * open the file 313 */ 314 fd = open(pn, wr ? O_RDWR : O_RDONLY, 0); 315 if (fd == -1) { 316 warn("open %s %s", pn, wr ? "for read/write" : "read-only"); 317 return (-1); 318 } 319 320 /* 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); 329 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; 336 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; 345 } 346 if (ac.ac_flag & AFORK) 347 ci.ci_comm[i++] = '*'; 348 ci.ci_comm[i++] = '\0'; 349 ci.ci_etime = decode_comp_t(ac.ac_etime); 350 ci.ci_utime = decode_comp_t(ac.ac_utime); 351 ci.ci_stime = decode_comp_t(ac.ac_stime); 352 ci.ci_uid = ac.ac_uid; 353 ci.ci_mem = ac.ac_mem; 354 ci.ci_io = decode_comp_t(ac.ac_io) / AHZ; 355 356 if (!uflag) { 357 /* and enter it into the usracct and pacct databases */ 358 if (sflag || (!mflag && !qflag)) 359 pacct_add(&ci); 360 if (sflag || (mflag && !qflag)) 361 usracct_add(&ci); 362 } else if (!qflag) 363 printf("%6lu %12.2f cpu %12juk mem %12ju io %s\n", 364 ci.ci_uid, 365 (ci.ci_utime + ci.ci_stime) / (double) AHZ, 366 (uintmax_t)ci.ci_mem, (uintmax_t)ci.ci_io, 367 ci.ci_comm); 368 } 369 370 /* finally, return the file descriptor for possible truncation */ 371 return (fd); 372 } 373 374 static u_quad_t 375 decode_comp_t(comp) 376 comp_t comp; 377 { 378 u_quad_t rv; 379 380 /* 381 * for more info on the comp_t format, see: 382 * /usr/src/sys/kern/kern_acct.c 383 * /usr/src/sys/sys/acct.h 384 * /usr/src/usr.bin/lastcomm/lastcomm.c 385 */ 386 rv = comp & 0x1fff; /* 13 bit fraction */ 387 comp >>= 13; /* 3 bit base-8 exponent */ 388 while (comp--) 389 rv <<= 3; 390 391 return (rv); 392 } 393 394 /* sort commands, doing the right thing in terms of reversals */ 395 static int 396 cmp_comm(s1, s2) 397 const char *s1, *s2; 398 { 399 int rv; 400 401 rv = strcmp(s1, s2); 402 if (rv == 0) 403 rv = -1; 404 return (rflag ? rv : -rv); 405 } 406 407 /* sort by total user and system time */ 408 static int 409 cmp_usrsys(d1, d2) 410 const DBT *d1, *d2; 411 { 412 struct cmdinfo c1, c2; 413 u_quad_t t1, t2; 414 415 memcpy(&c1, d1->data, sizeof(c1)); 416 memcpy(&c2, d2->data, sizeof(c2)); 417 418 t1 = c1.ci_utime + c1.ci_stime; 419 t2 = c2.ci_utime + c2.ci_stime; 420 421 if (t1 < t2) 422 return -1; 423 else if (t1 == t2) 424 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 425 else 426 return 1; 427 } 428 429 /* sort by average user and system time */ 430 static int 431 cmp_avgusrsys(d1, d2) 432 const DBT *d1, *d2; 433 { 434 struct cmdinfo c1, c2; 435 double t1, t2; 436 437 memcpy(&c1, d1->data, sizeof(c1)); 438 memcpy(&c2, d2->data, sizeof(c2)); 439 440 t1 = c1.ci_utime + c1.ci_stime; 441 t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1); 442 443 t2 = c2.ci_utime + c2.ci_stime; 444 t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1); 445 446 if (t1 < t2) 447 return -1; 448 else if (t1 == t2) 449 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 450 else 451 return 1; 452 } 453 454 /* sort by total number of disk I/O operations */ 455 static int 456 cmp_dkio(d1, d2) 457 const DBT *d1, *d2; 458 { 459 struct cmdinfo c1, c2; 460 461 memcpy(&c1, d1->data, sizeof(c1)); 462 memcpy(&c2, d2->data, sizeof(c2)); 463 464 if (c1.ci_io < c2.ci_io) 465 return -1; 466 else if (c1.ci_io == c2.ci_io) 467 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 468 else 469 return 1; 470 } 471 472 /* sort by average number of disk I/O operations */ 473 static int 474 cmp_avgdkio(d1, d2) 475 const DBT *d1, *d2; 476 { 477 struct cmdinfo c1, c2; 478 double n1, n2; 479 480 memcpy(&c1, d1->data, sizeof(c1)); 481 memcpy(&c2, d2->data, sizeof(c2)); 482 483 n1 = (double) c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1); 484 n2 = (double) c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1); 485 486 if (n1 < n2) 487 return -1; 488 else if (n1 == n2) 489 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 490 else 491 return 1; 492 } 493 494 /* sort by the cpu-storage integral */ 495 static int 496 cmp_cpumem(d1, d2) 497 const DBT *d1, *d2; 498 { 499 struct cmdinfo c1, c2; 500 501 memcpy(&c1, d1->data, sizeof(c1)); 502 memcpy(&c2, d2->data, sizeof(c2)); 503 504 if (c1.ci_mem < c2.ci_mem) 505 return -1; 506 else if (c1.ci_mem == c2.ci_mem) 507 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 508 else 509 return 1; 510 } 511 512 /* sort by the cpu-time average memory usage */ 513 static int 514 cmp_avgcpumem(d1, d2) 515 const DBT *d1, *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 } 537 538 /* sort by the number of invocations */ 539 static int 540 cmp_calls(d1, d2) 541 const DBT *d1, *d2; 542 { 543 struct cmdinfo c1, c2; 544 545 memcpy(&c1, d1->data, sizeof(c1)); 546 memcpy(&c2, d2->data, sizeof(c2)); 547 548 if (c1.ci_calls < c2.ci_calls) 549 return -1; 550 else if (c1.ci_calls == c2.ci_calls) 551 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 552 else 553 return 1; 554 } 555