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