Home | History | Annotate | Line # | Download | only in sa
pdb.c revision 1.7
      1 /* $NetBSD: pdb.c,v 1.7 2000/06/14 06:49:26 cgd Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1994 Christopher G. Demetriou
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Christopher G. Demetriou.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __RCSID("$NetBSD: pdb.c,v 1.7 2000/06/14 06:49:26 cgd Exp $");
     36 #endif
     37 
     38 #include <sys/types.h>
     39 #include <sys/acct.h>
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdio.h>
     44 #include <string.h>
     45 #include "extern.h"
     46 #include "pathnames.h"
     47 
     48 static int check_junk __P((struct cmdinfo *));
     49 static void add_ci __P((const struct cmdinfo *, struct cmdinfo *));
     50 static void print_ci __P((const struct cmdinfo *, const struct cmdinfo *));
     51 
     52 static DB	*pacct_db;
     53 
     54 int
     55 pacct_init()
     56 {
     57 	DB *saved_pacct_db;
     58 	int error;
     59 
     60 	pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL);
     61 	if (pacct_db == NULL)
     62 		return (-1);
     63 
     64 	error = 0;
     65 	if (!iflag) {
     66 		DBT key, data;
     67 		int serr, nerr;
     68 
     69 		saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDONLY, 0, DB_BTREE,
     70 		    NULL);
     71 		if (saved_pacct_db == NULL) {
     72 			error = errno == ENOENT ? 0 : -1;
     73 			if (error)
     74 				warn("retrieving process accounting summary");
     75 			goto out;
     76 		}
     77 
     78 		serr = DB_SEQ(saved_pacct_db, &key, &data, R_FIRST);
     79 		if (serr < 0) {
     80 			warn("retrieving process accounting summary");
     81 			error = -1;
     82 			goto closeout;
     83 		}
     84 		while (serr == 0) {
     85 			nerr = DB_PUT(pacct_db, &key, &data, 0);
     86 			if (nerr < 0) {
     87 				warn("initializing process accounting stats");
     88 				error = -1;
     89 				break;
     90 			}
     91 
     92 			serr = DB_SEQ(saved_pacct_db, &key, &data, R_NEXT);
     93 			if (serr < 0) {
     94 				warn("retrieving process accounting summary");
     95 				error = -1;
     96 				break;
     97 			}
     98 		}
     99 
    100 closeout:	if (DB_CLOSE(saved_pacct_db) < 0) {
    101 			warn("closing process accounting summary");
    102 			error = -1;
    103 		}
    104 	}
    105 
    106 out:	if (error != 0)
    107 		pacct_destroy();
    108 	return (error);
    109 }
    110 
    111 void
    112 pacct_destroy()
    113 {
    114 	if (DB_CLOSE(pacct_db) < 0)
    115 		warn("destroying process accounting stats");
    116 }
    117 
    118 int
    119 pacct_add(ci)
    120 	const struct cmdinfo *ci;
    121 {
    122 	DBT key, data;
    123 	struct cmdinfo newci;
    124 	char keydata[sizeof(ci->ci_comm)];
    125 	int rv;
    126 
    127 	memcpy(&keydata, ci->ci_comm, sizeof(keydata));
    128 	key.data = &keydata;
    129 	key.size = strlen(keydata);
    130 
    131 	rv = DB_GET(pacct_db, &key, &data, 0);
    132 	if (rv < 0) {
    133 		warn("get key %s from process accounting stats", ci->ci_comm);
    134 		return (-1);
    135 	} else if (rv == 0) {	/* it's there; copy whole thing */
    136 		/* XXX compare size if paranoid */
    137 		/* add the old data to the new data */
    138 		memcpy(&newci, data.data, data.size);
    139 	} else {		/* it's not there; zero it and copy the key */
    140 		memset(&newci, 0, sizeof(newci));
    141 		memcpy(newci.ci_comm, key.data, key.size);
    142 	}
    143 
    144 	add_ci(ci, &newci);
    145 
    146 	data.data = &newci;
    147 	data.size = sizeof(newci);
    148 	rv = DB_PUT(pacct_db, &key, &data, 0);
    149 	if (rv < 0) {
    150 		warn("add key %s to process accounting stats", ci->ci_comm);
    151 		return (-1);
    152 	} else if (rv == 1) {
    153 		warnx("duplicate key %s in process accounting stats",
    154 		    ci->ci_comm);
    155 		return (-1);
    156 	}
    157 
    158 	return (0);
    159 }
    160 
    161 int
    162 pacct_update()
    163 {
    164 	DB *saved_pacct_db;
    165 	DBT key, data;
    166 	int error, serr, nerr;
    167 
    168 	saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
    169 	    DB_BTREE, NULL);
    170 	if (saved_pacct_db == NULL) {
    171 		warn("creating process accounting summary");
    172 		return (-1);
    173 	}
    174 
    175 	error = 0;
    176 
    177 	serr = DB_SEQ(pacct_db, &key, &data, R_FIRST);
    178 	if (serr < 0) {
    179 		warn("retrieving process accounting stats");
    180 		error = -1;
    181 	}
    182 	while (serr == 0) {
    183 		nerr = DB_PUT(saved_pacct_db, &key, &data, 0);
    184 		if (nerr < 0) {
    185 			warn("saving process accounting summary");
    186 			error = -1;
    187 			break;
    188 		}
    189 
    190 		serr = DB_SEQ(pacct_db, &key, &data, R_NEXT);
    191 		if (serr < 0) {
    192 			warn("retrieving process accounting stats");
    193 			error = -1;
    194 			break;
    195 		}
    196 	}
    197 
    198 	if (DB_SYNC(saved_pacct_db, 0) < 0) {
    199 		warn("syncing process accounting summary");
    200 		error = -1;
    201 	}
    202 	if (DB_CLOSE(saved_pacct_db) < 0) {
    203 		warn("closing process accounting summary");
    204 		error = -1;
    205 	}
    206 	return error;
    207 }
    208 
    209 void
    210 pacct_print()
    211 {
    212 	BTREEINFO bti;
    213 	DBT key, data, ndata;
    214 	DB *output_pacct_db;
    215 	struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk;
    216 	int rv;
    217 
    218 	memset(&ci_total, 0, sizeof(ci_total));
    219 	strcpy(ci_total.ci_comm, "");
    220 	memset(&ci_other, 0, sizeof(ci_other));
    221 	strcpy(ci_other.ci_comm, "***other");
    222 	memset(&ci_junk, 0, sizeof(ci_junk));
    223 	strcpy(ci_junk.ci_comm, "**junk**");
    224 
    225 	/*
    226 	 * Retrieve them into new DB, sorted by appropriate key.
    227 	 * At the same time, cull 'other' and 'junk'
    228 	 */
    229 	memset(&bti, 0, sizeof(bti));
    230 	bti.compare = sa_cmp;
    231 	output_pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
    232 	if (output_pacct_db == NULL) {
    233 		warn("couldn't sort process accounting stats");
    234 		return;
    235 	}
    236 
    237 	ndata.data = NULL;
    238 	ndata.size = 0;
    239 	rv = DB_SEQ(pacct_db, &key, &data, R_FIRST);
    240 	if (rv < 0)
    241 		warn("retrieving process accounting stats");
    242 	while (rv == 0) {
    243 		cip = (struct cmdinfo *) data.data;
    244 		memcpy(&ci, cip, sizeof(ci));
    245 
    246 		/* add to total */
    247 		add_ci(&ci, &ci_total);
    248 
    249 		if (vflag && ci.ci_calls <= cutoff &&
    250 		    (fflag || check_junk(&ci))) {
    251 			/* put it into **junk** */
    252 			add_ci(&ci, &ci_junk);
    253 			goto next;
    254 		}
    255 		if (!aflag &&
    256 		    ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) {
    257 			/* put into ***other */
    258 			add_ci(&ci, &ci_other);
    259 			goto next;
    260 		}
    261 		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
    262 		if (rv < 0)
    263 			warn("sorting process accounting stats");
    264 
    265 next:		rv = DB_SEQ(pacct_db, &key, &data, R_NEXT);
    266 		if (rv < 0)
    267 			warn("retrieving process accounting stats");
    268 	}
    269 
    270 	/* insert **junk** and ***other */
    271 	if (ci_junk.ci_calls != 0) {
    272 		data.data = &ci_junk;
    273 		data.size = sizeof(ci_junk);
    274 		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
    275 		if (rv < 0)
    276 			warn("sorting process accounting stats");
    277 	}
    278 	if (ci_other.ci_calls != 0) {
    279 		data.data = &ci_other;
    280 		data.size = sizeof(ci_other);
    281 		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
    282 		if (rv < 0)
    283 			warn("sorting process accounting stats");
    284 	}
    285 
    286 	/* print out the total */
    287 	print_ci(&ci_total, &ci_total);
    288 
    289 	/* print out; if reversed, print first (smallest) first */
    290 	rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST);
    291 	if (rv < 0)
    292 		warn("retrieving process accounting report");
    293 	while (rv == 0) {
    294 		cip = (struct cmdinfo *) data.data;
    295 		memcpy(&ci, cip, sizeof(ci));
    296 
    297 		print_ci(&ci, &ci_total);
    298 
    299 		rv = DB_SEQ(output_pacct_db, &data, &ndata,
    300 		    rflag ? R_NEXT : R_PREV);
    301 		if (rv < 0)
    302 			warn("retrieving process accounting report");
    303 	}
    304 	DB_CLOSE(output_pacct_db);
    305 }
    306 
    307 static int
    308 check_junk(cip)
    309 	struct cmdinfo *cip;
    310 {
    311 	char *cp;
    312 	size_t len;
    313 
    314 	fprintf(stderr, "%s (%qu) -- ", cip->ci_comm,
    315 	    (unsigned long long)cip->ci_calls);
    316 	cp = fgetln(stdin, &len);
    317 
    318 	return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0;
    319 }
    320 
    321 static void
    322 add_ci(fromcip, tocip)
    323 	const struct cmdinfo *fromcip;
    324 	struct cmdinfo *tocip;
    325 {
    326 	tocip->ci_calls += fromcip->ci_calls;
    327 	tocip->ci_etime += fromcip->ci_etime;
    328 	tocip->ci_utime += fromcip->ci_utime;
    329 	tocip->ci_stime += fromcip->ci_stime;
    330 	tocip->ci_mem += fromcip->ci_mem;
    331 	tocip->ci_io += fromcip->ci_io;
    332 }
    333 
    334 static void
    335 print_ci(cip, totalcip)
    336 	const struct cmdinfo *cip, *totalcip;
    337 {
    338 	double t, c;
    339 	int uflow;
    340 
    341 	c = cip->ci_calls ? cip->ci_calls : 1;
    342 	t = (cip->ci_utime + cip->ci_stime) / (double) AHZ;
    343 	if (t < 0.01) {
    344 		t = 0.01;
    345 		uflow = 1;
    346 	} else
    347 		uflow = 0;
    348 
    349 	printf("%8qu ", (unsigned long long)cip->ci_calls);
    350 	if (cflag) {
    351 		if (cip != totalcip)
    352 			printf(" %4.2f%%  ",
    353 			    cip->ci_calls / (double) totalcip->ci_calls);
    354 		else
    355 			printf(" %4s   ", "");
    356 	}
    357 
    358 	if (jflag)
    359 		printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c));
    360 	else
    361 		printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ));
    362 	if (cflag) {
    363 		if (cip != totalcip)
    364 			printf(" %4.2f%%  ",
    365 			    cip->ci_etime / (double) totalcip->ci_etime);
    366 		else
    367 			printf(" %4s   ", "");
    368 	}
    369 
    370 	if (!lflag) {
    371 		if (jflag)
    372 			printf("%11.2fcp ", t / (double) cip->ci_calls);
    373 		else
    374 			printf("%11.2fcp ", t / 60.0);
    375 		if (cflag) {
    376 			if (cip != totalcip)
    377 				printf(" %4.2f%%  ",
    378 				    (cip->ci_utime + cip->ci_stime) / (double)
    379 				    (totalcip->ci_utime + totalcip->ci_stime));
    380 			else
    381 				printf(" %4s   ", "");
    382 		}
    383 	} else {
    384 		if (jflag)
    385 			printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c));
    386 		else
    387 			printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ));
    388 		if (cflag) {
    389 			if (cip != totalcip)
    390 				printf(" %4.2f%%  ", cip->ci_utime / (double) totalcip->ci_utime);
    391 			else
    392 				printf(" %4s   ", "");
    393 		}
    394 		if (jflag)
    395 			printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c));
    396 		else
    397 			printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ));
    398 		if (cflag) {
    399 			if (cip != totalcip)
    400 				printf(" %4.2f%%  ", cip->ci_stime / (double) totalcip->ci_stime);
    401 			else
    402 				printf(" %4s   ", "");
    403 		}
    404 	}
    405 
    406 	if (tflag) {
    407 		if (!uflow)
    408 			printf("%8.2fre/cp ", cip->ci_etime / (double) (cip->ci_utime + cip->ci_stime));
    409 		else
    410 			printf("%8s ", "*ignore*");
    411 	}
    412 
    413 	if (Dflag)
    414 		printf("%10qutio ", (unsigned long long)cip->ci_io);
    415 	else
    416 		printf("%8.0favio ", cip->ci_io / c);
    417 
    418 	if (Kflag)
    419 		printf("%10quk*sec ", (unsigned long long)cip->ci_mem);
    420 	else
    421 		printf("%8.0fk ", cip->ci_mem / t);
    422 
    423 	printf("  %s\n", cip->ci_comm);
    424 }
    425