Home | History | Annotate | Line # | Download | only in sa
main.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1994 Christopher G. Demetriou
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *      This product includes software developed by Christopher G. Demetriou.
     16  1.1  cgd  * 4. The name of the author may not be used to endorse or promote products
     17  1.1  cgd  *    derived from this software without specific prior written permission
     18  1.1  cgd  *
     19  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  cgd  */
     30  1.1  cgd 
     31  1.1  cgd #ifndef LINT
     32  1.1  cgd static char copright[] =
     33  1.1  cgd "@(#) Copyright (c) 1994 Christopher G. Demetriou\n\
     34  1.1  cgd  All rights reserved.\n";
     35  1.1  cgd 
     36  1.1  cgd static char rcsid[] = "$Id: main.c,v 1.1 1994/03/24 18:41:51 cgd Exp $";
     37  1.1  cgd #endif
     38  1.1  cgd 
     39  1.1  cgd /*
     40  1.1  cgd  * sa:	system accounting
     41  1.1  cgd  */
     42  1.1  cgd 
     43  1.1  cgd #include <sys/types.h>
     44  1.1  cgd #include <sys/acct.h>
     45  1.1  cgd #include <ctype.h>
     46  1.1  cgd #include <err.h>
     47  1.1  cgd #include <fcntl.h>
     48  1.1  cgd #include <signal.h>
     49  1.1  cgd #include <stdio.h>
     50  1.1  cgd #include <stdlib.h>
     51  1.1  cgd #include <unistd.h>
     52  1.1  cgd #include "extern.h"
     53  1.1  cgd #include "pathnames.h"
     54  1.1  cgd 
     55  1.1  cgd static int	acct_load	__P((char *, int));
     56  1.1  cgd static u_quad_t	decode_comp_t	__P((comp_t));
     57  1.1  cgd static int	cmp_comm	__P((const char *, const char *));
     58  1.1  cgd static int	cmp_usrsys	__P((const DBT *, const DBT *));
     59  1.1  cgd static int	cmp_avgusrsys	__P((const DBT *, const DBT *));
     60  1.1  cgd static int	cmp_dkio	__P((const DBT *, const DBT *));
     61  1.1  cgd static int	cmp_avgdkio	__P((const DBT *, const DBT *));
     62  1.1  cgd static int	cmp_cpumem	__P((const DBT *, const DBT *));
     63  1.1  cgd static int	cmp_avgcpumem	__P((const DBT *, const DBT *));
     64  1.1  cgd static int	cmp_calls	__P((const DBT *, const DBT *));
     65  1.1  cgd 
     66  1.1  cgd int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
     67  1.1  cgd int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
     68  1.1  cgd int cutoff = 1;
     69  1.1  cgd 
     70  1.1  cgd static char	*dfltargv[] = { _PATH_ACCT };
     71  1.1  cgd static int	dfltargc = (sizeof dfltargv/sizeof(char *));
     72  1.1  cgd 
     73  1.1  cgd /* default to comparing by sum of user + system time */
     74  1.1  cgd cmpf_t   sa_cmp = cmp_usrsys;
     75  1.1  cgd 
     76  1.1  cgd int
     77  1.1  cgd main(argc, argv)
     78  1.1  cgd 	int argc;
     79  1.1  cgd 	char **argv;
     80  1.1  cgd {
     81  1.1  cgd 	char ch;
     82  1.1  cgd 	int error;
     83  1.1  cgd 
     84  1.1  cgd 	while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1)
     85  1.1  cgd 		switch (ch) {
     86  1.1  cgd 			case 'a':
     87  1.1  cgd 				/* print all commands */
     88  1.1  cgd 				aflag = 1;
     89  1.1  cgd 				break;
     90  1.1  cgd 			case 'b':
     91  1.1  cgd 				/* sort by per-call user/system time average */
     92  1.1  cgd 				bflag = 1;
     93  1.1  cgd 				sa_cmp = cmp_avgusrsys;
     94  1.1  cgd 				break;
     95  1.1  cgd 			case 'c':
     96  1.1  cgd 				/* print percentage total time */
     97  1.1  cgd 				cflag = 1;
     98  1.1  cgd 				break;
     99  1.1  cgd 			case 'd':
    100  1.1  cgd 				/* sort by averge number of disk I/O ops */
    101  1.1  cgd 				dflag = 1;
    102  1.1  cgd 				sa_cmp = cmp_avgdkio;
    103  1.1  cgd 				break;
    104  1.1  cgd 			case 'D':
    105  1.1  cgd 				/* print and sort by total disk I/O ops */
    106  1.1  cgd 				Dflag = 1;
    107  1.1  cgd 				sa_cmp = cmp_dkio;
    108  1.1  cgd 				break;
    109  1.1  cgd 			case 'f':
    110  1.1  cgd 				/* force no interactive threshold comprison */
    111  1.1  cgd 				fflag = 1;
    112  1.1  cgd 				break;
    113  1.1  cgd 			case 'i':
    114  1.1  cgd 				/* do not read in summary file */
    115  1.1  cgd 				iflag = 1;
    116  1.1  cgd 				break;
    117  1.1  cgd 			case 'j':
    118  1.1  cgd 				/* instead of total minutes, give sec/call */
    119  1.1  cgd 				jflag = 1;
    120  1.1  cgd 				break;
    121  1.1  cgd 			case 'k':
    122  1.1  cgd 				/* sort by cpu-time average memory usage */
    123  1.1  cgd 				kflag = 1;
    124  1.1  cgd 				sa_cmp = cmp_avgcpumem;
    125  1.1  cgd 				break;
    126  1.1  cgd 			case 'K':
    127  1.1  cgd 				/* print and sort by cpu-storage integral */
    128  1.1  cgd 				sa_cmp = cmp_cpumem;
    129  1.1  cgd 				Kflag = 1;
    130  1.1  cgd 				break;
    131  1.1  cgd 			case 'l':
    132  1.1  cgd 				/* seperate system and user time */
    133  1.1  cgd 				lflag = 1;
    134  1.1  cgd 				break;
    135  1.1  cgd 			case 'm':
    136  1.1  cgd 				/* print procs and time per-user */
    137  1.1  cgd 				mflag = 1;
    138  1.1  cgd 				break;
    139  1.1  cgd 			case 'n':
    140  1.1  cgd 				/* sort by number of calls */
    141  1.1  cgd 				sa_cmp = cmp_calls;
    142  1.1  cgd 				break;
    143  1.1  cgd 			case 'q':
    144  1.1  cgd 				/* quiet; error messages only */
    145  1.1  cgd 				qflag = 1;
    146  1.1  cgd 				break;
    147  1.1  cgd 			case 'r':
    148  1.1  cgd 				/* reverse order of sort */
    149  1.1  cgd 				rflag = 1;
    150  1.1  cgd 				break;
    151  1.1  cgd 			case 's':
    152  1.1  cgd 				/* merge accounting file into summaries */
    153  1.1  cgd 				sflag = 1;
    154  1.1  cgd 				break;
    155  1.1  cgd 			case 't':
    156  1.1  cgd 				/* report ratio of user and system times */
    157  1.1  cgd 				tflag = 1;
    158  1.1  cgd 				break;
    159  1.1  cgd 			case 'u':
    160  1.1  cgd 				/* first, print uid and command name */
    161  1.1  cgd 				uflag = 1;
    162  1.1  cgd 				break;
    163  1.1  cgd 			case 'v':
    164  1.1  cgd 				/* cull junk */
    165  1.1  cgd 				vflag = 1;
    166  1.1  cgd 				cutoff = atoi(optarg);
    167  1.1  cgd 				break;
    168  1.1  cgd 			case '?':
    169  1.1  cgd 	                default:
    170  1.1  cgd 				(void)fprintf(stderr,
    171  1.1  cgd 				    "usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n");
    172  1.1  cgd 				exit(1);
    173  1.1  cgd 		}
    174  1.1  cgd 
    175  1.1  cgd 	argc -= optind;
    176  1.1  cgd 	argv += optind;
    177  1.1  cgd 
    178  1.1  cgd 	/* various argument checking */
    179  1.1  cgd 	if (fflag && !vflag)
    180  1.1  cgd 		errx(1, "only one of -f requires -v");
    181  1.1  cgd 	if (fflag && aflag)
    182  1.1  cgd 		errx(1, "only one of -a and -v may be specified");
    183  1.1  cgd 	/* XXX need more argument checking */
    184  1.1  cgd 
    185  1.1  cgd 	if (!uflag) {
    186  1.1  cgd 		/* initialize tables */
    187  1.1  cgd 		if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
    188  1.1  cgd 			errx(1, "process accounting initialization failed");
    189  1.1  cgd 		if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
    190  1.1  cgd 			errx(1, "user accounting initialization failed");
    191  1.1  cgd 	}
    192  1.1  cgd 
    193  1.1  cgd 	if (argc == 0) {
    194  1.1  cgd 		argc = dfltargc;
    195  1.1  cgd 		argv = dfltargv;
    196  1.1  cgd 	}
    197  1.1  cgd 
    198  1.1  cgd 	/* for each file specified */
    199  1.1  cgd 	for (; argc > 0; argc--, argv++) {
    200  1.1  cgd 		int	fd;
    201  1.1  cgd 
    202  1.1  cgd 		/*
    203  1.1  cgd 		 * load the accounting data from the file.
    204  1.1  cgd 		 * if it fails, go on to the next file.
    205  1.1  cgd 		 */
    206  1.1  cgd 		fd = acct_load(argv[0], sflag);
    207  1.1  cgd 		if (fd < 0)
    208  1.1  cgd 			continue;
    209  1.1  cgd 
    210  1.1  cgd 		if (!uflag && sflag) {
    211  1.1  cgd #ifndef DEBUG
    212  1.1  cgd 			sigset_t nmask, omask;
    213  1.1  cgd 			int unmask = 1;
    214  1.1  cgd 
    215  1.1  cgd 			/*
    216  1.1  cgd 			 * block most signals so we aren't interrupted during
    217  1.1  cgd 			 * the update.
    218  1.1  cgd 			 */
    219  1.1  cgd 			if (sigfillset(&nmask) == -1) {
    220  1.1  cgd 				warn("sigfillset");
    221  1.1  cgd 				unmask = 0;
    222  1.1  cgd 				error = 1;
    223  1.1  cgd 			}
    224  1.1  cgd 			if (unmask &&
    225  1.1  cgd 			    (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
    226  1.1  cgd 				warn("couldn't set signal mask ");
    227  1.1  cgd 				unmask = 0;
    228  1.1  cgd 				error = 1;
    229  1.1  cgd 			}
    230  1.1  cgd #endif /* DEBUG */
    231  1.1  cgd 
    232  1.1  cgd 			/*
    233  1.1  cgd 			 * truncate the accounting data file ASAP, to avoid
    234  1.1  cgd 			 * losing data.  don't worry about errors in updating
    235  1.1  cgd 			 * the saved stats; better to underbill than overbill,
    236  1.1  cgd 			 * but we want every accounting record intact.
    237  1.1  cgd 			 */
    238  1.1  cgd 			if (ftruncate(fd, 0) == -1) {
    239  1.1  cgd 				warn("couldn't truncate %s", argv);
    240  1.1  cgd 				error = 1;
    241  1.1  cgd 			}
    242  1.1  cgd 
    243  1.1  cgd 			/*
    244  1.1  cgd 			 * update saved user and process accounting data.
    245  1.1  cgd 			 * note errors for later.
    246  1.1  cgd 			 */
    247  1.1  cgd 			if (pacct_update() != 0 || usracct_update() != 0)
    248  1.1  cgd 				error = 1;
    249  1.1  cgd 
    250  1.1  cgd #ifndef DEBUG
    251  1.1  cgd 			/*
    252  1.1  cgd 			 * restore signals
    253  1.1  cgd 			 */
    254  1.1  cgd 			if (unmask &&
    255  1.1  cgd 			    (sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
    256  1.1  cgd 				warn("couldn't restore signal mask");
    257  1.1  cgd 				error = 1;
    258  1.1  cgd 			}
    259  1.1  cgd #endif /* DEBUG */
    260  1.1  cgd 		}
    261  1.1  cgd 
    262  1.1  cgd 		/*
    263  1.1  cgd 		 * close the opened accounting file
    264  1.1  cgd 		 */
    265  1.1  cgd 		if (close(fd) == -1) {
    266  1.1  cgd 			warn("close %s", argv);
    267  1.1  cgd 			error = 1;
    268  1.1  cgd 		}
    269  1.1  cgd 	}
    270  1.1  cgd 
    271  1.1  cgd 	if (!uflag && !qflag) {
    272  1.1  cgd 		/* print any results we may have obtained. */
    273  1.1  cgd 		if (!mflag)
    274  1.1  cgd 			pacct_print();
    275  1.1  cgd 		else
    276  1.1  cgd 			usracct_print();
    277  1.1  cgd 	}
    278  1.1  cgd 
    279  1.1  cgd 	if (!uflag) {
    280  1.1  cgd 		/* finally, deallocate databases */
    281  1.1  cgd 		if (sflag || (!mflag && !qflag))
    282  1.1  cgd 			pacct_destroy();
    283  1.1  cgd 		if (sflag || (mflag && !qflag))
    284  1.1  cgd 			usracct_destroy();
    285  1.1  cgd 	}
    286  1.1  cgd 
    287  1.1  cgd 	exit(error);
    288  1.1  cgd }
    289  1.1  cgd 
    290  1.1  cgd static int
    291  1.1  cgd acct_load(pn, wr)
    292  1.1  cgd 	char *pn;
    293  1.1  cgd 	int wr;
    294  1.1  cgd {
    295  1.1  cgd 	struct acct ac;
    296  1.1  cgd 	struct cmdinfo ci;
    297  1.1  cgd 	ssize_t rv;
    298  1.1  cgd 	int fd, i;
    299  1.1  cgd 
    300  1.1  cgd 	/*
    301  1.1  cgd 	 * open the file
    302  1.1  cgd 	 */
    303  1.1  cgd 	fd = open(pn, wr ? O_RDWR : O_RDONLY, 0);
    304  1.1  cgd 	if (fd == -1) {
    305  1.1  cgd 		warn("open %s %s", pn, wr ? "for read/write" : "read-only");
    306  1.1  cgd 		return (-1);
    307  1.1  cgd 	}
    308  1.1  cgd 
    309  1.1  cgd 	/*
    310  1.1  cgd 	 * read all we can; don't stat and open because more processes
    311  1.1  cgd 	 * could exit, and we'd miss them
    312  1.1  cgd 	 */
    313  1.1  cgd 	while (1) {
    314  1.1  cgd 		/* get one accounting entry and punt if there's an error */
    315  1.1  cgd 		rv = read(fd, &ac, sizeof(struct acct));
    316  1.1  cgd 		if (rv == -1)
    317  1.1  cgd 			warn("error reading %s", pn);
    318  1.1  cgd 		else if (rv > 0 && rv < sizeof(struct acct))
    319  1.1  cgd 			warnx("short read of accounting data in %s", pn);
    320  1.1  cgd 		if (rv != sizeof(struct acct))
    321  1.1  cgd 			break;
    322  1.1  cgd 
    323  1.1  cgd 		/* decode it */
    324  1.1  cgd 		ci.ci_calls = 1;
    325  1.1  cgd 		for (i = 0; i < sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
    326  1.1  cgd 		    i++) {
    327  1.1  cgd 			char c = ac.ac_comm[i];
    328  1.1  cgd 
    329  1.1  cgd 			if (!isascii(c) || iscntrl(c)) {
    330  1.1  cgd 				ci.ci_comm[i] = '?';
    331  1.1  cgd 				ci.ci_flags |= CI_UNPRINTABLE;
    332  1.1  cgd 			} else
    333  1.1  cgd 				ci.ci_comm[i] = c;
    334  1.1  cgd 		}
    335  1.1  cgd 		if (ac.ac_flag & AFORK)
    336  1.1  cgd 			ci.ci_comm[i++] = '*';
    337  1.1  cgd 		ci.ci_comm[i++] = '\0';
    338  1.1  cgd 		ci.ci_etime = decode_comp_t(ac.ac_etime);
    339  1.1  cgd 		ci.ci_utime = decode_comp_t(ac.ac_utime);
    340  1.1  cgd 		ci.ci_stime = decode_comp_t(ac.ac_stime);
    341  1.1  cgd 		ci.ci_uid = ac.ac_uid;
    342  1.1  cgd 		ci.ci_mem = ac.ac_mem;
    343  1.1  cgd 		ci.ci_io = decode_comp_t(ac.ac_io) / AHZ;
    344  1.1  cgd 
    345  1.1  cgd 		if (!uflag) {
    346  1.1  cgd 			/* and enter it into the usracct and pacct databases */
    347  1.1  cgd 			if (sflag || (!mflag && !qflag))
    348  1.1  cgd 				pacct_add(&ci);
    349  1.1  cgd 			if (sflag || (mflag && !qflag))
    350  1.1  cgd 				usracct_add(&ci);
    351  1.1  cgd 		} else if (!qflag)
    352  1.1  cgd 			printf("%6u %12.2lf cpu %12quk mem %12qu io %s\n",
    353  1.1  cgd 			    ci.ci_uid,
    354  1.1  cgd 			    (ci.ci_utime + ci.ci_stime) / (double) AHZ,
    355  1.1  cgd 			    ci.ci_mem, ci.ci_io, ci.ci_comm);
    356  1.1  cgd 	}
    357  1.1  cgd 
    358  1.1  cgd 	/* finally, return the file descriptor for possible truncation */
    359  1.1  cgd 	return (fd);
    360  1.1  cgd }
    361  1.1  cgd 
    362  1.1  cgd static u_quad_t
    363  1.1  cgd decode_comp_t(comp)
    364  1.1  cgd 	comp_t comp;
    365  1.1  cgd {
    366  1.1  cgd 	u_quad_t rv;
    367  1.1  cgd 
    368  1.1  cgd 	/*
    369  1.1  cgd 	 * for more info on the comp_t format, see:
    370  1.1  cgd 	 *	/usr/src/sys/kern/kern_acct.c
    371  1.1  cgd 	 *	/usr/src/sys/sys/acct.h
    372  1.1  cgd 	 *	/usr/src/usr.bin/lastcomm/lastcomm.c
    373  1.1  cgd 	 */
    374  1.1  cgd 	rv = comp & 0x1fff;	/* 13 bit fraction */
    375  1.1  cgd 	comp >>= 13;		/* 3 bit base-8 exponent */
    376  1.1  cgd 	while (comp--)
    377  1.1  cgd 		rv <<= 3;
    378  1.1  cgd 
    379  1.1  cgd 	return (rv);
    380  1.1  cgd }
    381  1.1  cgd 
    382  1.1  cgd /* sort commands, doing the right thing in terms of reversals */
    383  1.1  cgd static int
    384  1.1  cgd cmp_comm(s1, s2)
    385  1.1  cgd 	const char *s1, *s2;
    386  1.1  cgd {
    387  1.1  cgd 	int rv;
    388  1.1  cgd 
    389  1.1  cgd 	rv = strcmp(s1, s2);
    390  1.1  cgd 	if (rv == 0)
    391  1.1  cgd 		rv = -1;
    392  1.1  cgd 	return (rflag ? rv : -rv);
    393  1.1  cgd }
    394  1.1  cgd 
    395  1.1  cgd /* sort by total user and system time */
    396  1.1  cgd static int
    397  1.1  cgd cmp_usrsys(d1, d2)
    398  1.1  cgd 	const DBT *d1, *d2;
    399  1.1  cgd {
    400  1.1  cgd 	struct cmdinfo *c1, *c2;
    401  1.1  cgd 	u_quad_t t1, t2;
    402  1.1  cgd 
    403  1.1  cgd 	c1 = (struct cmdinfo *) d1->data;
    404  1.1  cgd 	c2 = (struct cmdinfo *) d2->data;
    405  1.1  cgd 
    406  1.1  cgd 	t1 = c1->ci_utime + c1->ci_stime;
    407  1.1  cgd 	t2 = c2->ci_utime + c2->ci_stime;
    408  1.1  cgd 
    409  1.1  cgd 	if (t1 < t2)
    410  1.1  cgd 		return -1;
    411  1.1  cgd 	else if (t1 == t2)
    412  1.1  cgd 		return (cmp_comm(c1->ci_comm, c2->ci_comm));
    413  1.1  cgd 	else
    414  1.1  cgd 		return 1;
    415  1.1  cgd }
    416  1.1  cgd 
    417  1.1  cgd /* sort by average user and system time */
    418  1.1  cgd static int
    419  1.1  cgd cmp_avgusrsys(d1, d2)
    420  1.1  cgd 	const DBT *d1, *d2;
    421  1.1  cgd {
    422  1.1  cgd 	struct cmdinfo *c1, *c2;
    423  1.1  cgd 	double t1, t2;
    424  1.1  cgd 
    425  1.1  cgd 	c1 = (struct cmdinfo *) d1->data;
    426  1.1  cgd 	c2 = (struct cmdinfo *) d2->data;
    427  1.1  cgd 
    428  1.1  cgd 	t1 = c1->ci_utime + c1->ci_stime;
    429  1.1  cgd 	t1 /= (double) (c1->ci_calls ? c1->ci_calls : 1);
    430  1.1  cgd 
    431  1.1  cgd 	t2 = c2->ci_utime + c2->ci_stime;
    432  1.1  cgd 	t2 /= (double) (c2->ci_calls ? c2->ci_calls : 1);
    433  1.1  cgd 
    434  1.1  cgd 	if (t1 < t2)
    435  1.1  cgd 		return -1;
    436  1.1  cgd 	else if (t1 == t2)
    437  1.1  cgd 		return (cmp_comm(c1->ci_comm, c2->ci_comm));
    438  1.1  cgd 	else
    439  1.1  cgd 		return 1;
    440  1.1  cgd }
    441  1.1  cgd 
    442  1.1  cgd /* sort by total number of disk I/O operations */
    443  1.1  cgd static int
    444  1.1  cgd cmp_dkio(d1, d2)
    445  1.1  cgd 	const DBT *d1, *d2;
    446  1.1  cgd {
    447  1.1  cgd 	struct cmdinfo *c1, *c2;
    448  1.1  cgd 
    449  1.1  cgd 	c1 = (struct cmdinfo *) d1->data;
    450  1.1  cgd 	c2 = (struct cmdinfo *) d2->data;
    451  1.1  cgd 
    452  1.1  cgd 	if (c1->ci_io < c2->ci_io)
    453  1.1  cgd 		return -1;
    454  1.1  cgd 	else if (c1->ci_io == c2->ci_io)
    455  1.1  cgd 		return (cmp_comm(c1->ci_comm, c2->ci_comm));
    456  1.1  cgd 	else
    457  1.1  cgd 		return 1;
    458  1.1  cgd }
    459  1.1  cgd 
    460  1.1  cgd /* sort by average number of disk I/O operations */
    461  1.1  cgd static int
    462  1.1  cgd cmp_avgdkio(d1, d2)
    463  1.1  cgd 	const DBT *d1, *d2;
    464  1.1  cgd {
    465  1.1  cgd 	struct cmdinfo *c1, *c2;
    466  1.1  cgd 	double n1, n2;
    467  1.1  cgd 
    468  1.1  cgd 	c1 = (struct cmdinfo *) d1->data;
    469  1.1  cgd 	c2 = (struct cmdinfo *) d2->data;
    470  1.1  cgd 
    471  1.1  cgd 	n1 = (double) c1->ci_io / (double) (c1->ci_calls ? c1->ci_calls : 1);
    472  1.1  cgd 	n2 = (double) c2->ci_io / (double) (c2->ci_calls ? c2->ci_calls : 1);
    473  1.1  cgd 
    474  1.1  cgd 	if (n1 < n2)
    475  1.1  cgd 		return -1;
    476  1.1  cgd 	else if (n1 == n2)
    477  1.1  cgd 		return (cmp_comm(c1->ci_comm, c2->ci_comm));
    478  1.1  cgd 	else
    479  1.1  cgd 		return 1;
    480  1.1  cgd }
    481  1.1  cgd 
    482  1.1  cgd /* sort by the cpu-storage integral */
    483  1.1  cgd static int
    484  1.1  cgd cmp_cpumem(d1, d2)
    485  1.1  cgd 	const DBT *d1, *d2;
    486  1.1  cgd {
    487  1.1  cgd 	struct cmdinfo *c1, *c2;
    488  1.1  cgd 
    489  1.1  cgd 	c1 = (struct cmdinfo *) d1->data;
    490  1.1  cgd 	c2 = (struct cmdinfo *) d2->data;
    491  1.1  cgd 
    492  1.1  cgd 	if (c1->ci_mem < c2->ci_mem)
    493  1.1  cgd 		return -1;
    494  1.1  cgd 	else if (c1->ci_mem == c2->ci_mem)
    495  1.1  cgd 		return (cmp_comm(c1->ci_comm, c2->ci_comm));
    496  1.1  cgd 	else
    497  1.1  cgd 		return 1;
    498  1.1  cgd }
    499  1.1  cgd 
    500  1.1  cgd /* sort by the cpu-time average memory usage */
    501  1.1  cgd static int
    502  1.1  cgd cmp_avgcpumem(d1, d2)
    503  1.1  cgd 	const DBT *d1, *d2;
    504  1.1  cgd {
    505  1.1  cgd 	struct cmdinfo *c1, *c2;
    506  1.1  cgd 	u_quad_t t1, t2;
    507  1.1  cgd 	double n1, n2;
    508  1.1  cgd 
    509  1.1  cgd 	c1 = (struct cmdinfo *) d1->data;
    510  1.1  cgd 	c2 = (struct cmdinfo *) d2->data;
    511  1.1  cgd 
    512  1.1  cgd 	t1 = c1->ci_utime + c1->ci_stime;
    513  1.1  cgd 	t2 = c2->ci_utime + c2->ci_stime;
    514  1.1  cgd 
    515  1.1  cgd 	n1 = (double) c1->ci_mem / (double) (t1 ? t1 : 1);
    516  1.1  cgd 	n2 = (double) c2->ci_mem / (double) (t2 ? t2 : 1);
    517  1.1  cgd 
    518  1.1  cgd 	if (n1 < n2)
    519  1.1  cgd 		return -1;
    520  1.1  cgd 	else if (n1 == n2)
    521  1.1  cgd 		return (cmp_comm(c1->ci_comm, c2->ci_comm));
    522  1.1  cgd 	else
    523  1.1  cgd 		return 1;
    524  1.1  cgd }
    525  1.1  cgd 
    526  1.1  cgd /* sort by the number of invocations */
    527  1.1  cgd static int
    528  1.1  cgd cmp_calls(d1, d2)
    529  1.1  cgd 	const DBT *d1, *d2;
    530  1.1  cgd {
    531  1.1  cgd 	struct cmdinfo *c1, *c2;
    532  1.1  cgd 
    533  1.1  cgd 	c1 = (struct cmdinfo *) d1->data;
    534  1.1  cgd 	c2 = (struct cmdinfo *) d2->data;
    535  1.1  cgd 
    536  1.1  cgd 	if (c1->ci_calls < c2->ci_calls)
    537  1.1  cgd 		return -1;
    538  1.1  cgd 	else if (c1->ci_calls == c2->ci_calls)
    539  1.1  cgd 		return (cmp_comm(c1->ci_comm, c2->ci_comm));
    540  1.1  cgd 	else
    541  1.1  cgd 		return 1;
    542  1.1  cgd }
    543