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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
43 #include "pathnames.h"
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 *));
57 pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL);
66 saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDONLY, 0, DB_BTREE,
68 if (saved_pacct_db == NULL) {
69 error = errno == ENOENT ? 0 : -1;
71 warn("retrieving process accounting summary");
75 serr = DB_SEQ(saved_pacct_db, &key, &data, R_FIRST);
77 warn("retrieving process accounting summary");
82 nerr = DB_PUT(pacct_db, &key, &data, 0);
84 warn("initializing process accounting stats");
89 serr = DB_SEQ(saved_pacct_db, &key, &data, R_NEXT);
91 warn("retrieving process accounting summary");
97 closeout: if (DB_CLOSE(saved_pacct_db) < 0) {
98 warn("closing process accounting summary");
111 if (DB_CLOSE(pacct_db) < 0)
112 warn("destroying process accounting stats");
117 const struct cmdinfo *ci;
120 struct cmdinfo newci;
121 char keydata[sizeof ci->ci_comm];
124 bcopy(ci->ci_comm, &keydata, sizeof keydata);
126 key.size = strlen(keydata);
128 rv = DB_GET(pacct_db, &key, &data, 0);
130 warn("get key %s from process accounting stats", ci->ci_comm);
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);
144 data.size = sizeof newci;
145 rv = DB_PUT(pacct_db, &key, &data, 0);
147 warn("add key %s to process accounting stats", ci->ci_comm);
149 } else if (rv == 1) {
150 warnx("duplicate key %s in process accounting stats",
163 int error, serr, nerr;
165 saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
167 if (saved_pacct_db == NULL) {
168 warn("creating process accounting summary");
174 serr = DB_SEQ(pacct_db, &key, &data, R_FIRST);
176 warn("retrieving process accounting stats");
180 nerr = DB_PUT(saved_pacct_db, &key, &data, 0);
182 warn("saving process accounting summary");
187 serr = DB_SEQ(pacct_db, &key, &data, R_NEXT);
189 warn("retrieving process accounting stats");
195 if (DB_SYNC(saved_pacct_db, 0) < 0) {
196 warn("syncing process accounting summary");
199 if (DB_CLOSE(saved_pacct_db) < 0) {
200 warn("closing process accounting summary");
210 DBT key, data, ndata;
212 struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk;
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**");
223 * Retrieve them into new DB, sorted by appropriate key.
224 * At the same time, cull 'other' and 'junk'
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");
236 rv = DB_SEQ(pacct_db, &key, &data, R_FIRST);
238 warn("retrieving process accounting stats");
240 cip = (struct cmdinfo *) data.data;
241 bcopy(cip, &ci, sizeof ci);
244 add_ci(&ci, &ci_total);
246 if (vflag && ci.ci_calls <= cutoff &&
247 (fflag || check_junk(&ci))) {
248 /* put it into **junk** */
249 add_ci(&ci, &ci_junk);
253 ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) {
254 /* put into ***other */
255 add_ci(&ci, &ci_other);
258 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
260 warn("sorting process accounting stats");
262 next: rv = DB_SEQ(pacct_db, &key, &data, R_NEXT);
264 warn("retrieving process accounting stats");
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);
273 warn("sorting process accounting stats");
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);
280 warn("sorting process accounting stats");
283 /* print out the total */
284 print_ci(&ci_total, &ci_total);
286 /* print out; if reversed, print first (smallest) first */
287 rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST);
289 warn("retrieving process accounting report");
291 cip = (struct cmdinfo *) data.data;
292 bcopy(cip, &ci, sizeof ci);
294 print_ci(&ci, &ci_total);
296 rv = DB_SEQ(output_pacct_db, &data, &ndata,
297 rflag ? R_NEXT : R_PREV);
299 warn("retrieving process accounting report");
301 DB_CLOSE(output_pacct_db);
311 fprintf(stderr, "%s (%ju) -- ", cip->ci_comm, (uintmax_t)cip->ci_calls);
312 cp = fgetln(stdin, &len);
314 return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0;
318 add_ci(fromcip, tocip)
319 const struct cmdinfo *fromcip;
320 struct cmdinfo *tocip;
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;
331 print_ci(cip, totalcip)
332 const struct cmdinfo *cip, *totalcip;
337 c = cip->ci_calls ? cip->ci_calls : 1;
338 t = (cip->ci_utime + cip->ci_stime) / (double) AHZ;
345 printf("%8ju ", (uintmax_t)cip->ci_calls);
349 cip->ci_calls / (double) totalcip->ci_calls);
355 printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c));
357 printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ));
361 cip->ci_etime / (double) totalcip->ci_etime);
368 printf("%11.2fcp ", t / (double) cip->ci_calls);
370 printf("%11.2fcp ", t / 60.0);
374 (cip->ci_utime + cip->ci_stime) / (double)
375 (totalcip->ci_utime + totalcip->ci_stime));
381 printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c));
383 printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ));
386 printf(" %4.2f%% ", cip->ci_utime / (double) totalcip->ci_utime);
391 printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c));
393 printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ));
396 printf(" %4.2f%% ", cip->ci_stime / (double) totalcip->ci_stime);
404 printf("%8.2fre/cp ",
406 (double) (cip->ci_utime + cip->ci_stime));
412 printf("%10jutio ", (uintmax_t)cip->ci_io);
414 printf("%8.0favio ", cip->ci_io / c);
417 printf("%10juk*sec ", (uintmax_t)cip->ci_mem);
419 printf("%8.0fk ", cip->ci_mem / t);
421 printf(" %s\n", cip->ci_comm);