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