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>
44 #include "pathnames.h"
46 static int check_junk(const struct cmdinfo *);
47 static void add_ci(const struct cmdinfo *, struct cmdinfo *);
48 static void print_ci(const struct cmdinfo *, const struct cmdinfo *);
52 /* Legacy format in AHZV1 units. */
54 char ci_comm[MAXCOMLEN+2]; /* command name (+ '*') */
55 uid_t ci_uid; /* user id */
56 u_quad_t ci_calls; /* number of calls */
57 u_quad_t ci_etime; /* elapsed time */
58 u_quad_t ci_utime; /* user time */
59 u_quad_t ci_stime; /* system time */
60 u_quad_t ci_mem; /* memory use */
61 u_quad_t ci_io; /* number of disk i/o ops */
62 u_int ci_flags; /* flags; see below */
66 * Convert a v1 data record into the current version.
67 * Return 0 if OK, -1 on error, setting errno.
70 v1_to_v2(DBT *key __unused, DBT *data)
72 struct cmdinfov1 civ1;
73 static struct cmdinfo civ2;
75 if (data->size != sizeof(civ1)) {
79 memcpy(&civ1, data->data, data->size);
80 memset(&civ2, 0, sizeof(civ2));
81 memcpy(civ2.ci_comm, civ1.ci_comm, sizeof(civ2.ci_comm));
82 civ2.ci_uid = civ1.ci_uid;
83 civ2.ci_calls = civ1.ci_calls;
84 civ2.ci_etime = ((double)civ1.ci_etime / AHZV1) * 1000000;
85 civ2.ci_utime = ((double)civ1.ci_utime / AHZV1) * 1000000;
86 civ2.ci_stime = ((double)civ1.ci_stime / AHZV1) * 1000000;
87 civ2.ci_mem = civ1.ci_mem;
88 civ2.ci_io = civ1.ci_io;
89 civ2.ci_flags = civ1.ci_flags;
90 data->size = sizeof(civ2);
95 /* Copy pdb_file to in-memory pacct_db. */
99 return (db_copy_in(&pacct_db, pdb_file, "process accounting",
106 db_destroy(pacct_db, "process accounting");
110 pacct_add(const struct cmdinfo *ci)
113 struct cmdinfo newci;
114 char keydata[sizeof ci->ci_comm];
117 bcopy(ci->ci_comm, &keydata, sizeof keydata);
119 key.size = strlen(keydata);
121 rv = DB_GET(pacct_db, &key, &data, 0);
123 warn("get key %s from process accounting stats", ci->ci_comm);
125 } else if (rv == 0) { /* it's there; copy whole thing */
126 /* XXX compare size if paranoid */
127 /* add the old data to the new data */
128 bcopy(data.data, &newci, data.size);
129 } else { /* it's not there; zero it and copy the key */
130 bzero(&newci, sizeof newci);
131 bcopy(key.data, newci.ci_comm, key.size);
137 data.size = sizeof newci;
138 rv = DB_PUT(pacct_db, &key, &data, 0);
140 warn("add key %s to process accounting stats", ci->ci_comm);
142 } else if (rv == 1) {
143 warnx("duplicate key %s in process accounting stats",
151 /* Copy in-memory pacct_db to pdb_file. */
155 return (db_copy_out(pacct_db, pdb_file, "process accounting",
163 DBT key, data, ndata;
165 struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk;
168 bzero(&ci_total, sizeof ci_total);
169 strcpy(ci_total.ci_comm, "");
170 bzero(&ci_other, sizeof ci_other);
171 strcpy(ci_other.ci_comm, "***other");
172 bzero(&ci_junk, sizeof ci_junk);
173 strcpy(ci_junk.ci_comm, "**junk**");
176 * Retrieve them into new DB, sorted by appropriate key.
177 * At the same time, cull 'other' and 'junk'
179 bzero(&bti, sizeof bti);
180 bti.compare = sa_cmp;
181 output_pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
182 if (output_pacct_db == NULL) {
183 warn("couldn't sort process accounting stats");
189 rv = DB_SEQ(pacct_db, &key, &data, R_FIRST);
191 warn("retrieving process accounting stats");
193 cip = (struct cmdinfo *) data.data;
194 bcopy(cip, &ci, sizeof ci);
197 add_ci(&ci, &ci_total);
199 if (vflag && ci.ci_calls <= cutoff &&
200 (fflag || check_junk(&ci))) {
201 /* put it into **junk** */
202 add_ci(&ci, &ci_junk);
206 ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) {
207 /* put into ***other */
208 add_ci(&ci, &ci_other);
211 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
213 warn("sorting process accounting stats");
215 next: rv = DB_SEQ(pacct_db, &key, &data, R_NEXT);
217 warn("retrieving process accounting stats");
220 /* insert **junk** and ***other */
221 if (ci_junk.ci_calls != 0) {
222 data.data = &ci_junk;
223 data.size = sizeof ci_junk;
224 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
226 warn("sorting process accounting stats");
228 if (ci_other.ci_calls != 0) {
229 data.data = &ci_other;
230 data.size = sizeof ci_other;
231 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
233 warn("sorting process accounting stats");
236 /* print out the total */
237 print_ci(&ci_total, &ci_total);
239 /* print out; if reversed, print first (smallest) first */
240 rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST);
242 warn("retrieving process accounting report");
244 cip = (struct cmdinfo *) data.data;
245 bcopy(cip, &ci, sizeof ci);
247 print_ci(&ci, &ci_total);
249 rv = DB_SEQ(output_pacct_db, &data, &ndata,
250 rflag ? R_NEXT : R_PREV);
252 warn("retrieving process accounting report");
254 DB_CLOSE(output_pacct_db);
258 check_junk(const struct cmdinfo *cip)
263 fprintf(stderr, "%s (%ju) -- ", cip->ci_comm, (uintmax_t)cip->ci_calls);
264 cp = fgetln(stdin, &len);
266 return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0;
270 add_ci(const struct cmdinfo *fromcip, struct cmdinfo *tocip)
272 tocip->ci_calls += fromcip->ci_calls;
273 tocip->ci_etime += fromcip->ci_etime;
274 tocip->ci_utime += fromcip->ci_utime;
275 tocip->ci_stime += fromcip->ci_stime;
276 tocip->ci_mem += fromcip->ci_mem;
277 tocip->ci_io += fromcip->ci_io;
281 print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip)
286 c = cip->ci_calls ? cip->ci_calls : 1;
287 t = (cip->ci_utime + cip->ci_stime) / 1000000;
294 printf("%8ju ", (uintmax_t)cip->ci_calls);
297 printf(" %4.1f%% ", cip->ci_calls /
298 (double)totalcip->ci_calls * 100);
304 printf("%11.3fre ", cip->ci_etime / (1000000 * c));
306 printf("%11.3fre ", cip->ci_etime / (60.0 * 1000000));
309 printf(" %4.1f%% ", cip->ci_etime /
310 totalcip->ci_etime * 100);
317 printf("%11.3fcp ", t / (double) cip->ci_calls);
319 printf("%11.2fcp ", t / 60.0);
323 (cip->ci_utime + cip->ci_stime) /
324 (totalcip->ci_utime + totalcip->ci_stime) *
331 printf("%11.3fu ", cip->ci_utime / (1000000 * c));
333 printf("%11.2fu ", cip->ci_utime / (60.0 * 1000000));
336 printf(" %4.1f%% ", cip->ci_utime /
337 (double)totalcip->ci_utime * 100);
342 printf("%11.3fs ", cip->ci_stime / (1000000 * c));
344 printf("%11.2fs ", cip->ci_stime / (60.0 * 1000000));
347 printf(" %4.1f%% ", cip->ci_stime /
348 (double)totalcip->ci_stime * 100);
356 printf("%8.2fre/cp ",
358 (cip->ci_utime + cip->ci_stime));
364 printf("%10.0fio ", cip->ci_io);
366 printf("%8.0favio ", cip->ci_io / c);
369 printf("%10.0fk*sec ", cip->ci_mem);
371 printf("%8.0fk ", cip->ci_mem / t);
373 printf(" %s\n", cip->ci_comm);