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