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 141638 2005-02-10 12:43:16Z delphij $"); |
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 *); --- 226 unchanged lines hidden (view full) --- 294usage() 295{ 296 (void)fprintf(stderr, 297 "usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n"); 298 exit(1); 299} 300 301static int |
302acct_load(const char *pn, int wr) |
303{ 304 struct acct ac; 305 struct cmdinfo ci; 306 ssize_t rv; 307 int fd, i; 308 309 /* 310 * open the file --- 74 unchanged lines hidden (view full) --- 385 while (comp--) 386 rv <<= 3; 387 388 return (rv); 389} 390 391/* sort commands, doing the right thing in terms of reversals */ 392static int |
393cmp_comm(const char *s1, const char *s2) |
394{ 395 int rv; 396 397 rv = strcmp(s1, s2); 398 if (rv == 0) 399 rv = -1; 400 return (rflag ? rv : -rv); 401} 402 403/* sort by total user and system time */ 404static int |
405cmp_usrsys(const DBT *d1, const DBT *d2) |
406{ 407 struct cmdinfo c1, c2; 408 u_quad_t t1, t2; 409 410 memcpy(&c1, d1->data, sizeof(c1)); 411 memcpy(&c2, d2->data, sizeof(c2)); 412 413 t1 = c1.ci_utime + c1.ci_stime; --- 4 unchanged lines hidden (view full) --- 418 else if (t1 == t2) 419 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 420 else 421 return 1; 422} 423 424/* sort by average user and system time */ 425static int |
426cmp_avgusrsys(const DBT *d1, const DBT *d2) |
427{ 428 struct cmdinfo c1, c2; 429 double t1, t2; 430 431 memcpy(&c1, d1->data, sizeof(c1)); 432 memcpy(&c2, d2->data, sizeof(c2)); 433 434 t1 = c1.ci_utime + c1.ci_stime; --- 7 unchanged lines hidden (view full) --- 442 else if (t1 == t2) 443 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 444 else 445 return 1; 446} 447 448/* sort by total number of disk I/O operations */ 449static int |
450cmp_dkio(const DBT *d1, const DBT *d2) |
451{ 452 struct cmdinfo c1, c2; 453 454 memcpy(&c1, d1->data, sizeof(c1)); 455 memcpy(&c2, d2->data, sizeof(c2)); 456 457 if (c1.ci_io < c2.ci_io) 458 return -1; 459 else if (c1.ci_io == c2.ci_io) 460 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 461 else 462 return 1; 463} 464 465/* sort by average number of disk I/O operations */ 466static int |
467cmp_avgdkio(const DBT *d1, const DBT *d2) |
468{ 469 struct cmdinfo c1, c2; 470 double n1, n2; 471 472 memcpy(&c1, d1->data, sizeof(c1)); 473 memcpy(&c2, d2->data, sizeof(c2)); 474 475 n1 = (double) c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1); --- 4 unchanged lines hidden (view full) --- 480 else if (n1 == n2) 481 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 482 else 483 return 1; 484} 485 486/* sort by the cpu-storage integral */ 487static int |
488cmp_cpumem(const DBT *d1, const DBT *d2) |
489{ 490 struct cmdinfo c1, c2; 491 492 memcpy(&c1, d1->data, sizeof(c1)); 493 memcpy(&c2, d2->data, sizeof(c2)); 494 495 if (c1.ci_mem < c2.ci_mem) 496 return -1; 497 else if (c1.ci_mem == c2.ci_mem) 498 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 499 else 500 return 1; 501} 502 503/* sort by the cpu-time average memory usage */ 504static int |
505cmp_avgcpumem(const DBT *d1, const DBT *d2) |
506{ 507 struct cmdinfo c1, c2; 508 u_quad_t t1, t2; 509 double n1, n2; 510 511 memcpy(&c1, d1->data, sizeof(c1)); 512 memcpy(&c2, d2->data, sizeof(c2)); 513 --- 8 unchanged lines hidden (view full) --- 522 else if (n1 == n2) 523 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 524 else 525 return 1; 526} 527 528/* sort by the number of invocations */ 529static int |
530cmp_calls(const DBT *d1, const DBT *d2) |
531{ 532 struct cmdinfo c1, c2; 533 534 memcpy(&c1, d1->data, sizeof(c1)); 535 memcpy(&c2, d2->data, sizeof(c2)); 536 537 if (c1.ci_calls < c2.ci_calls) 538 return -1; 539 else if (c1.ci_calls == c2.ci_calls) 540 return (cmp_comm(c1.ci_comm, c2.ci_comm)); 541 else 542 return 1; 543} |