1 /* $OpenBSD: pdb.c,v 1.7 2003/06/28 20:22:21 deraadt Exp $ */
2 /*
3 * Copyright (c) 1994 Christopher G. Demetriou
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef LINT
33 static char rcsid[] = "$Id: pdb.c,v 1.7 2003/06/28 20:22:21 deraadt 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(struct cmdinfo *);
47 static void add_ci(const struct cmdinfo *, struct cmdinfo *);
48 static void print_ci(const struct cmdinfo *, const struct cmdinfo *);
49
50 static DB *pacct_db;
51
52 int
pacct_init(void)53 pacct_init(void)
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
pacct_destroy(void)110 pacct_destroy(void)
111 {
112 if (DB_CLOSE(pacct_db) < 0)
113 warn("destroying process accounting stats");
114 }
115
116 int
pacct_add(const struct cmdinfo * ci)117 pacct_add(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 memcpy(&keydata, ci->ci_comm, 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 memcpy(&newci, data.data, data.size);
136 } else { /* it's not there; zero it and copy the key */
137 memset(&newci, 0, sizeof(newci));
138 memcpy(newci.ci_comm, key.data, 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
pacct_update(void)159 pacct_update(void)
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
pacct_print(void)207 pacct_print(void)
208 {
209 BTREEINFO bti;
210 DBT key, data, ndata;
211 DB *output_pacct_db;
212 struct cmdinfo ci, ci_total, ci_other, ci_junk;
213 int rv;
214
215 memset(&ci_total, 0, sizeof(ci_total));
216 strlcpy(ci_total.ci_comm, "", sizeof ci_total.ci_comm);
217 memset(&ci_other, 0, sizeof(ci_other));
218 strlcpy(ci_other.ci_comm, "***other", sizeof ci_other.ci_comm);
219 memset(&ci_junk, 0, sizeof(ci_junk));
220 strlcpy(ci_junk.ci_comm, "**junk**", sizeof ci_junk.ci_comm);
221
222 /*
223 * Retrieve them into new DB, sorted by appropriate key.
224 * At the same time, cull 'other' and 'junk'
225 */
226 memset(&bti, 0, 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 memcpy(&ci, data.data, 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 memcpy(&ci, data.data, sizeof(ci));
291
292 print_ci(&ci, &ci_total);
293
294 rv = DB_SEQ(output_pacct_db, &data, &ndata,
295 rflag ? R_NEXT : R_PREV);
296 if (rv < 0)
297 warn("retrieving process accounting report");
298 }
299 DB_CLOSE(output_pacct_db);
300 }
301
302 static int
check_junk(struct cmdinfo * cip)303 check_junk(struct cmdinfo *cip)
304 {
305 char *cp;
306 size_t len;
307
308 fprintf(stderr, "%s (%qu) -- ", cip->ci_comm, cip->ci_calls);
309 cp = fgetln(stdin, &len);
310
311 return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0;
312 }
313
314 static void
add_ci(const struct cmdinfo * fromcip,struct cmdinfo * tocip)315 add_ci(const struct cmdinfo *fromcip, struct cmdinfo *tocip)
316 {
317 tocip->ci_calls += fromcip->ci_calls;
318 tocip->ci_etime += fromcip->ci_etime;
319 tocip->ci_utime += fromcip->ci_utime;
320 tocip->ci_stime += fromcip->ci_stime;
321 tocip->ci_mem += fromcip->ci_mem;
322 tocip->ci_io += fromcip->ci_io;
323 }
324
325 static void
print_ci(const struct cmdinfo * cip,const struct cmdinfo * totalcip)326 print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip)
327 {
328 double t, c;
329 int uflow;
330
331 c = cip->ci_calls ? cip->ci_calls : 1;
332 t = (cip->ci_utime + cip->ci_stime) / (double) AHZ;
333 if (t < 0.01) {
334 t = 0.01;
335 uflow = 1;
336 } else
337 uflow = 0;
338
339 printf("%8qu ", cip->ci_calls);
340 if (cflag) {
341 if (cip != totalcip)
342 printf(" %4.2f%% ",
343 cip->ci_calls / (double) totalcip->ci_calls);
344 else
345 printf(" %4s ", "");
346 }
347
348 if (jflag)
349 printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c));
350 else
351 printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ));
352 if (cflag) {
353 if (cip != totalcip)
354 printf(" %4.2f%% ",
355 cip->ci_etime / (double) totalcip->ci_etime);
356 else
357 printf(" %4s ", "");
358 }
359
360 if (!lflag) {
361 if (jflag)
362 printf("%11.2fcp ", t / (double) cip->ci_calls);
363 else
364 printf("%11.2fcp ", t / 60.0);
365 if (cflag) {
366 if (cip != totalcip)
367 printf(" %4.2f%% ",
368 (cip->ci_utime + cip->ci_stime) / (double)
369 (totalcip->ci_utime + totalcip->ci_stime));
370 else
371 printf(" %4s ", "");
372 }
373 } else {
374 if (jflag)
375 printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c));
376 else
377 printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ));
378 if (cflag) {
379 if (cip != totalcip)
380 printf(" %4.2f%% ", cip->ci_utime / (double) totalcip->ci_utime);
381 else
382 printf(" %4s ", "");
383 }
384 if (jflag)
385 printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c));
386 else
387 printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ));
388 if (cflag) {
389 if (cip != totalcip)
390 printf(" %4.2f%% ", cip->ci_stime / (double) totalcip->ci_stime);
391 else
392 printf(" %4s ", "");
393 }
394 }
395
396 if (tflag) {
397 if (!uflow)
398 printf("%8.2fre/cp ", cip->ci_etime / (double) (cip->ci_utime + cip->ci_stime));
399 else
400 printf("%8s ", "*ignore*");
401 }
402
403 if (Dflag)
404 printf("%10qutio ", cip->ci_io);
405 else
406 printf("%8.0favio ", cip->ci_io / c);
407
408 if (Kflag)
409 printf("%10quk*sec ", cip->ci_mem);
410 else
411 printf("%8.0fk ", cip->ci_mem / t);
412
413 printf(" %s\n", cip->ci_comm);
414 }
415