Home | History | Annotate | Line # | Download | only in sa
pdb.c revision 1.6
      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 #include <sys/cdefs.h>
     32 #ifndef lint
     33 __RCSID("$NetBSD: pdb.c,v 1.6 1998/08/27 20:31:01 ross Exp $");
     34 #endif
     35 
     36 #include <sys/types.h>
     37 #include <sys/acct.h>
     38 #include <err.h>
     39 #include <errno.h>
     40 #include <fcntl.h>
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include "extern.h"
     44 #include "pathnames.h"
     45 
     46 static int check_junk __P((struct cmdinfo *));
     47 static void add_ci __P((const struct cmdinfo *, struct cmdinfo *));
     48 static void print_ci __P((const struct cmdinfo *, const struct cmdinfo *));
     49 
     50 static DB	*pacct_db;
     51 
     52 int
     53 pacct_init()
     54 {
     55 	DB *saved_pacct_db;
     56 	int error;
     57 
     58 	pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL);
     59 	if (pacct_db == NULL)
     60 		return (-1);
     61 
     62 	error = 0;
     63 	if (!iflag) {
     64 		DBT key, data;
     65 		int serr, nerr;
     66 
     67 		saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDONLY, 0, DB_BTREE,
     68 		    NULL);
     69 		if (saved_pacct_db == NULL) {
     70 			error = errno == ENOENT ? 0 : -1;
     71 			if (error)
     72 				warn("retrieving process accounting summary");
     73 			goto out;
     74 		}
     75 
     76 		serr = DB_SEQ(saved_pacct_db, &key, &data, R_FIRST);
     77 		if (serr < 0) {
     78 			warn("retrieving process accounting summary");
     79 			error = -1;
     80 			goto closeout;
     81 		}
     82 		while (serr == 0) {
     83 			nerr = DB_PUT(pacct_db, &key, &data, 0);
     84 			if (nerr < 0) {
     85 				warn("initializing process accounting stats");
     86 				error = -1;
     87 				break;
     88 			}
     89 
     90 			serr = DB_SEQ(saved_pacct_db, &key, &data, R_NEXT);
     91 			if (serr < 0) {
     92 				warn("retrieving process accounting summary");
     93 				error = -1;
     94 				break;
     95 			}
     96 		}
     97 
     98 closeout:	if (DB_CLOSE(saved_pacct_db) < 0) {
     99 			warn("closing process accounting summary");
    100 			error = -1;
    101 		}
    102 	}
    103 
    104 out:	if (error != 0)
    105 		pacct_destroy();
    106 	return (error);
    107 }
    108 
    109 void
    110 pacct_destroy()
    111 {
    112 	if (DB_CLOSE(pacct_db) < 0)
    113 		warn("destroying process accounting stats");
    114 }
    115 
    116 int
    117 pacct_add(ci)
    118 	const struct cmdinfo *ci;
    119 {
    120 	DBT key, data;
    121 	struct cmdinfo newci;
    122 	char keydata[sizeof(ci->ci_comm)];
    123 	int rv;
    124 
    125 	memcpy(&keydata, ci->ci_comm, sizeof(keydata));
    126 	key.data = &keydata;
    127 	key.size = strlen(keydata);
    128 
    129 	rv = DB_GET(pacct_db, &key, &data, 0);
    130 	if (rv < 0) {
    131 		warn("get key %s from process accounting stats", ci->ci_comm);
    132 		return (-1);
    133 	} else if (rv == 0) {	/* it's there; copy whole thing */
    134 		/* XXX compare size if paranoid */
    135 		/* add the old data to the new data */
    136 		memcpy(&newci, data.data, data.size);
    137 	} else {		/* it's not there; zero it and copy the key */
    138 		memset(&newci, 0, sizeof(newci));
    139 		memcpy(newci.ci_comm, key.data, key.size);
    140 	}
    141 
    142 	add_ci(ci, &newci);
    143 
    144 	data.data = &newci;
    145 	data.size = sizeof(newci);
    146 	rv = DB_PUT(pacct_db, &key, &data, 0);
    147 	if (rv < 0) {
    148 		warn("add key %s to process accounting stats", ci->ci_comm);
    149 		return (-1);
    150 	} else if (rv == 1) {
    151 		warnx("duplicate key %s in process accounting stats",
    152 		    ci->ci_comm);
    153 		return (-1);
    154 	}
    155 
    156 	return (0);
    157 }
    158 
    159 int
    160 pacct_update()
    161 {
    162 	DB *saved_pacct_db;
    163 	DBT key, data;
    164 	int error, serr, nerr;
    165 
    166 	saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
    167 	    DB_BTREE, NULL);
    168 	if (saved_pacct_db == NULL) {
    169 		warn("creating process accounting summary");
    170 		return (-1);
    171 	}
    172 
    173 	error = 0;
    174 
    175 	serr = DB_SEQ(pacct_db, &key, &data, R_FIRST);
    176 	if (serr < 0) {
    177 		warn("retrieving process accounting stats");
    178 		error = -1;
    179 	}
    180 	while (serr == 0) {
    181 		nerr = DB_PUT(saved_pacct_db, &key, &data, 0);
    182 		if (nerr < 0) {
    183 			warn("saving process accounting summary");
    184 			error = -1;
    185 			break;
    186 		}
    187 
    188 		serr = DB_SEQ(pacct_db, &key, &data, R_NEXT);
    189 		if (serr < 0) {
    190 			warn("retrieving process accounting stats");
    191 			error = -1;
    192 			break;
    193 		}
    194 	}
    195 
    196 	if (DB_SYNC(saved_pacct_db, 0) < 0) {
    197 		warn("syncing process accounting summary");
    198 		error = -1;
    199 	}
    200 	if (DB_CLOSE(saved_pacct_db) < 0) {
    201 		warn("closing process accounting summary");
    202 		error = -1;
    203 	}
    204 	return error;
    205 }
    206 
    207 void
    208 pacct_print()
    209 {
    210 	BTREEINFO bti;
    211 	DBT key, data, ndata;
    212 	DB *output_pacct_db;
    213 	struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk;
    214 	int rv;
    215 
    216 	memset(&ci_total, 0, sizeof(ci_total));
    217 	strcpy(ci_total.ci_comm, "");
    218 	memset(&ci_other, 0, sizeof(ci_other));
    219 	strcpy(ci_other.ci_comm, "***other");
    220 	memset(&ci_junk, 0, sizeof(ci_junk));
    221 	strcpy(ci_junk.ci_comm, "**junk**");
    222 
    223 	/*
    224 	 * Retrieve them into new DB, sorted by appropriate key.
    225 	 * At the same time, cull 'other' and 'junk'
    226 	 */
    227 	memset(&bti, 0, sizeof(bti));
    228 	bti.compare = sa_cmp;
    229 	output_pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
    230 	if (output_pacct_db == NULL) {
    231 		warn("couldn't sort process accounting stats");
    232 		return;
    233 	}
    234 
    235 	ndata.data = NULL;
    236 	ndata.size = 0;
    237 	rv = DB_SEQ(pacct_db, &key, &data, R_FIRST);
    238 	if (rv < 0)
    239 		warn("retrieving process accounting stats");
    240 	while (rv == 0) {
    241 		cip = (struct cmdinfo *) data.data;
    242 		memcpy(&ci, cip, sizeof(ci));
    243 
    244 		/* add to total */
    245 		add_ci(&ci, &ci_total);
    246 
    247 		if (vflag && ci.ci_calls <= cutoff &&
    248 		    (fflag || check_junk(&ci))) {
    249 			/* put it into **junk** */
    250 			add_ci(&ci, &ci_junk);
    251 			goto next;
    252 		}
    253 		if (!aflag &&
    254 		    ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) {
    255 			/* put into ***other */
    256 			add_ci(&ci, &ci_other);
    257 			goto next;
    258 		}
    259 		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
    260 		if (rv < 0)
    261 			warn("sorting process accounting stats");
    262 
    263 next:		rv = DB_SEQ(pacct_db, &key, &data, R_NEXT);
    264 		if (rv < 0)
    265 			warn("retrieving process accounting stats");
    266 	}
    267 
    268 	/* insert **junk** and ***other */
    269 	if (ci_junk.ci_calls != 0) {
    270 		data.data = &ci_junk;
    271 		data.size = sizeof(ci_junk);
    272 		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
    273 		if (rv < 0)
    274 			warn("sorting process accounting stats");
    275 	}
    276 	if (ci_other.ci_calls != 0) {
    277 		data.data = &ci_other;
    278 		data.size = sizeof(ci_other);
    279 		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
    280 		if (rv < 0)
    281 			warn("sorting process accounting stats");
    282 	}
    283 
    284 	/* print out the total */
    285 	print_ci(&ci_total, &ci_total);
    286 
    287 	/* print out; if reversed, print first (smallest) first */
    288 	rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST);
    289 	if (rv < 0)
    290 		warn("retrieving process accounting report");
    291 	while (rv == 0) {
    292 		cip = (struct cmdinfo *) data.data;
    293 		memcpy(&ci, cip, sizeof(ci));
    294 
    295 		print_ci(&ci, &ci_total);
    296 
    297 		rv = DB_SEQ(output_pacct_db, &data, &ndata,
    298 		    rflag ? R_NEXT : R_PREV);
    299 		if (rv < 0)
    300 			warn("retrieving process accounting report");
    301 	}
    302 	DB_CLOSE(output_pacct_db);
    303 }
    304 
    305 static int
    306 check_junk(cip)
    307 	struct cmdinfo *cip;
    308 {
    309 	char *cp;
    310 	size_t len;
    311 
    312 	fprintf(stderr, "%s (%qu) -- ", cip->ci_comm,
    313 	    (unsigned long long)cip->ci_calls);
    314 	cp = fgetln(stdin, &len);
    315 
    316 	return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0;
    317 }
    318 
    319 static void
    320 add_ci(fromcip, tocip)
    321 	const struct cmdinfo *fromcip;
    322 	struct cmdinfo *tocip;
    323 {
    324 	tocip->ci_calls += fromcip->ci_calls;
    325 	tocip->ci_etime += fromcip->ci_etime;
    326 	tocip->ci_utime += fromcip->ci_utime;
    327 	tocip->ci_stime += fromcip->ci_stime;
    328 	tocip->ci_mem += fromcip->ci_mem;
    329 	tocip->ci_io += fromcip->ci_io;
    330 }
    331 
    332 static void
    333 print_ci(cip, totalcip)
    334 	const struct cmdinfo *cip, *totalcip;
    335 {
    336 	double t, c;
    337 	int uflow;
    338 
    339 	c = cip->ci_calls ? cip->ci_calls : 1;
    340 	t = (cip->ci_utime + cip->ci_stime) / (double) AHZ;
    341 	if (t < 0.01) {
    342 		t = 0.01;
    343 		uflow = 1;
    344 	} else
    345 		uflow = 0;
    346 
    347 	printf("%8qu ", (unsigned long long)cip->ci_calls);
    348 	if (cflag) {
    349 		if (cip != totalcip)
    350 			printf(" %4.2f%%  ",
    351 			    cip->ci_calls / (double) totalcip->ci_calls);
    352 		else
    353 			printf(" %4s   ", "");
    354 	}
    355 
    356 	if (jflag)
    357 		printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c));
    358 	else
    359 		printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ));
    360 	if (cflag) {
    361 		if (cip != totalcip)
    362 			printf(" %4.2f%%  ",
    363 			    cip->ci_etime / (double) totalcip->ci_etime);
    364 		else
    365 			printf(" %4s   ", "");
    366 	}
    367 
    368 	if (!lflag) {
    369 		if (jflag)
    370 			printf("%11.2fcp ", t / (double) cip->ci_calls);
    371 		else
    372 			printf("%11.2fcp ", t / 60.0);
    373 		if (cflag) {
    374 			if (cip != totalcip)
    375 				printf(" %4.2f%%  ",
    376 				    (cip->ci_utime + cip->ci_stime) / (double)
    377 				    (totalcip->ci_utime + totalcip->ci_stime));
    378 			else
    379 				printf(" %4s   ", "");
    380 		}
    381 	} else {
    382 		if (jflag)
    383 			printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c));
    384 		else
    385 			printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ));
    386 		if (cflag) {
    387 			if (cip != totalcip)
    388 				printf(" %4.2f%%  ", cip->ci_utime / (double) totalcip->ci_utime);
    389 			else
    390 				printf(" %4s   ", "");
    391 		}
    392 		if (jflag)
    393 			printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c));
    394 		else
    395 			printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ));
    396 		if (cflag) {
    397 			if (cip != totalcip)
    398 				printf(" %4.2f%%  ", cip->ci_stime / (double) totalcip->ci_stime);
    399 			else
    400 				printf(" %4s   ", "");
    401 		}
    402 	}
    403 
    404 	if (tflag) {
    405 		if (!uflow)
    406 			printf("%8.2fre/cp ", cip->ci_etime / (double) (cip->ci_utime + cip->ci_stime));
    407 		else
    408 			printf("%8s ", "*ignore*");
    409 	}
    410 
    411 	if (Dflag)
    412 		printf("%10qutio ", (unsigned long long)cip->ci_io);
    413 	else
    414 		printf("%8.0favio ", cip->ci_io / c);
    415 
    416 	if (Kflag)
    417 		printf("%10quk*sec ", (unsigned long long)cip->ci_mem);
    418 	else
    419 		printf("%8.0fk ", cip->ci_mem / t);
    420 
    421 	printf("  %s\n", cip->ci_comm);
    422 }
    423