2 * Copyright (c) 1994 Christopher G. Demetriou
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
32 static const char rcsid[] =
33 "$FreeBSD: src/usr.sbin/sa/pdb.c,v 1.7 1999/08/28 01:19:53 peter Exp $";
36 #include <sys/types.h>
44 #include "pathnames.h"
46 static int check_junk __P((struct cmdinfo *));
47 static void add_ci __P((const struct cmdinfo *, struct cmdinfo *));
48 static void print_ci __P((const struct cmdinfo *, const struct cmdinfo *));
58 pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL);
67 saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDONLY, 0, DB_BTREE,
69 if (saved_pacct_db == NULL) {
70 error = errno == ENOENT ? 0 : -1;
72 warn("retrieving process accounting summary");
76 serr = DB_SEQ(saved_pacct_db, &key, &data, R_FIRST);
78 warn("retrieving process accounting summary");
83 nerr = DB_PUT(pacct_db, &key, &data, 0);
85 warn("initializing process accounting stats");
90 serr = DB_SEQ(saved_pacct_db, &key, &data, R_NEXT);
92 warn("retrieving process accounting summary");
98 closeout: if (DB_CLOSE(saved_pacct_db) < 0) {
99 warn("closing process accounting summary");
112 if (DB_CLOSE(pacct_db) < 0)
113 warn("destroying process accounting stats");
118 const struct cmdinfo *ci;
121 struct cmdinfo newci;
122 char keydata[sizeof ci->ci_comm];
125 bcopy(ci->ci_comm, &keydata, sizeof keydata);
127 key.size = strlen(keydata);
129 rv = DB_GET(pacct_db, &key, &data, 0);
131 warn("get key %s from process accounting stats", ci->ci_comm);
133 } else if (rv == 0) { /* it's there; copy whole thing */
134 /* XXX compare size if paranoid */
135 /* add the old data to the new data */
136 bcopy(data.data, &newci, data.size);
137 } else { /* it's not there; zero it and copy the key */
138 bzero(&newci, sizeof newci);
139 bcopy(key.data, newci.ci_comm, key.size);
145 data.size = sizeof newci;
146 rv = DB_PUT(pacct_db, &key, &data, 0);
148 warn("add key %s to process accounting stats", ci->ci_comm);
150 } else if (rv == 1) {
151 warnx("duplicate key %s in process accounting stats",
164 int error, serr, nerr;
166 saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
168 if (saved_pacct_db == NULL) {
169 warn("creating process accounting summary");
175 serr = DB_SEQ(pacct_db, &key, &data, R_FIRST);
177 warn("retrieving process accounting stats");
181 nerr = DB_PUT(saved_pacct_db, &key, &data, 0);
183 warn("saving process accounting summary");
188 serr = DB_SEQ(pacct_db, &key, &data, R_NEXT);
190 warn("retrieving process accounting stats");
196 if (DB_SYNC(saved_pacct_db, 0) < 0) {
197 warn("syncing process accounting summary");
200 if (DB_CLOSE(saved_pacct_db) < 0) {
201 warn("closing process accounting summary");
211 DBT key, data, ndata;
213 struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk;
216 bzero(&ci_total, sizeof ci_total);
217 strcpy(ci_total.ci_comm, "");
218 bzero(&ci_other, sizeof ci_other);
219 strcpy(ci_other.ci_comm, "***other");
220 bzero(&ci_junk, sizeof ci_junk);
221 strcpy(ci_junk.ci_comm, "**junk**");
224 * Retrieve them into new DB, sorted by appropriate key.
225 * At the same time, cull 'other' and 'junk'
227 bzero(&bti, sizeof bti);
228 bti.compare = sa_cmp;
229 output_pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
230 if (output_pacct_db == NULL) {
231 warn("couldn't sort process accounting stats");
237 rv = DB_SEQ(pacct_db, &key, &data, R_FIRST);
239 warn("retrieving process accounting stats");
241 cip = (struct cmdinfo *) data.data;
242 bcopy(cip, &ci, sizeof ci);
245 add_ci(&ci, &ci_total);
247 if (vflag && ci.ci_calls <= cutoff &&
248 (fflag || check_junk(&ci))) {
249 /* put it into **junk** */
250 add_ci(&ci, &ci_junk);
254 ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) {
255 /* put into ***other */
256 add_ci(&ci, &ci_other);
259 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
261 warn("sorting process accounting stats");
263 next: rv = DB_SEQ(pacct_db, &key, &data, R_NEXT);
265 warn("retrieving process accounting stats");
268 /* insert **junk** and ***other */
269 if (ci_junk.ci_calls != 0) {
270 data.data = &ci_junk;
271 data.size = sizeof ci_junk;
272 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
274 warn("sorting process accounting stats");
276 if (ci_other.ci_calls != 0) {
277 data.data = &ci_other;
278 data.size = sizeof ci_other;
279 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
281 warn("sorting process accounting stats");
284 /* print out the total */
285 print_ci(&ci_total, &ci_total);
287 /* print out; if reversed, print first (smallest) first */
288 rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST);
290 warn("retrieving process accounting report");
292 cip = (struct cmdinfo *) data.data;
293 bcopy(cip, &ci, sizeof ci);
295 print_ci(&ci, &ci_total);
297 rv = DB_SEQ(output_pacct_db, &data, &ndata,
298 rflag ? R_NEXT : R_PREV);
300 warn("retrieving process accounting report");
302 DB_CLOSE(output_pacct_db);
312 fprintf(stderr, "%s (%qu) -- ", cip->ci_comm, cip->ci_calls);
313 cp = fgetln(stdin, &len);
315 return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0;
319 add_ci(fromcip, tocip)
320 const struct cmdinfo *fromcip;
321 struct cmdinfo *tocip;
323 tocip->ci_calls += fromcip->ci_calls;
324 tocip->ci_etime += fromcip->ci_etime;
325 tocip->ci_utime += fromcip->ci_utime;
326 tocip->ci_stime += fromcip->ci_stime;
327 tocip->ci_mem += fromcip->ci_mem;
328 tocip->ci_io += fromcip->ci_io;
332 print_ci(cip, totalcip)
333 const struct cmdinfo *cip, *totalcip;
338 c = cip->ci_calls ? cip->ci_calls : 1;
339 t = (cip->ci_utime + cip->ci_stime) / (double) AHZ;
346 printf("%8qu ", cip->ci_calls);
350 cip->ci_calls / (double) totalcip->ci_calls);
356 printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c));
358 printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ));
362 cip->ci_etime / (double) totalcip->ci_etime);
369 printf("%11.2fcp ", t / (double) cip->ci_calls);
371 printf("%11.2fcp ", t / 60.0);
375 (cip->ci_utime + cip->ci_stime) / (double)
376 (totalcip->ci_utime + totalcip->ci_stime));
382 printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c));
384 printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ));
387 printf(" %4.2f%% ", cip->ci_utime / (double) totalcip->ci_utime);
392 printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c));
394 printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ));
397 printf(" %4.2f%% ", cip->ci_stime / (double) totalcip->ci_stime);
405 printf("%8.2fre/cp ", cip->ci_etime / (double) (cip->ci_utime + cip->ci_stime));
410 printf("%10qutio ", cip->ci_io);
412 printf("%8.0favio ", cip->ci_io / c);
415 printf("%10quk*sec ", cip->ci_mem);
417 printf("%8.0fk ", cip->ci_mem / t);
419 printf(" %s\n", cip->ci_comm);