2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1994 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
46 #include "pathnames.h"
48 static int check_junk(const struct cmdinfo *);
49 static void add_ci(const struct cmdinfo *, struct cmdinfo *);
50 static void print_ci(const struct cmdinfo *, const struct cmdinfo *);
54 /* Legacy format in AHZV1 units. */
56 char ci_comm[MAXCOMLEN+2]; /* command name (+ '*') */
57 uid_t ci_uid; /* user id */
58 u_quad_t ci_calls; /* number of calls */
59 u_quad_t ci_etime; /* elapsed time */
60 u_quad_t ci_utime; /* user time */
61 u_quad_t ci_stime; /* system time */
62 u_quad_t ci_mem; /* memory use */
63 u_quad_t ci_io; /* number of disk i/o ops */
64 u_int ci_flags; /* flags; see below */
68 * Convert a v1 data record into the current version.
69 * Return 0 if OK, -1 on error, setting errno.
72 v1_to_v2(DBT *key __unused, DBT *data)
74 struct cmdinfov1 civ1;
75 static struct cmdinfo civ2;
77 if (data->size != sizeof(civ1)) {
81 memcpy(&civ1, data->data, data->size);
82 memset(&civ2, 0, sizeof(civ2));
83 memcpy(civ2.ci_comm, civ1.ci_comm, sizeof(civ2.ci_comm));
84 civ2.ci_uid = civ1.ci_uid;
85 civ2.ci_calls = civ1.ci_calls;
86 civ2.ci_etime = ((double)civ1.ci_etime / AHZV1) * 1000000;
87 civ2.ci_utime = ((double)civ1.ci_utime / AHZV1) * 1000000;
88 civ2.ci_stime = ((double)civ1.ci_stime / AHZV1) * 1000000;
89 civ2.ci_mem = civ1.ci_mem;
90 civ2.ci_io = civ1.ci_io;
91 civ2.ci_flags = civ1.ci_flags;
92 data->size = sizeof(civ2);
97 /* Copy pdb_file to in-memory pacct_db. */
101 return (db_copy_in(&pacct_db, pdb_file, "process accounting",
108 db_destroy(pacct_db, "process accounting");
112 pacct_add(const struct cmdinfo *ci)
115 struct cmdinfo newci;
116 char keydata[sizeof ci->ci_comm];
119 bcopy(ci->ci_comm, &keydata, sizeof keydata);
121 key.size = strlen(keydata);
123 rv = DB_GET(pacct_db, &key, &data, 0);
125 warn("get key %s from process accounting stats", ci->ci_comm);
127 } else if (rv == 0) { /* it's there; copy whole thing */
128 /* XXX compare size if paranoid */
129 /* add the old data to the new data */
130 bcopy(data.data, &newci, data.size);
131 } else { /* it's not there; zero it and copy the key */
132 bzero(&newci, sizeof newci);
133 bcopy(key.data, newci.ci_comm, key.size);
139 data.size = sizeof newci;
140 rv = DB_PUT(pacct_db, &key, &data, 0);
142 warn("add key %s to process accounting stats", ci->ci_comm);
144 } else if (rv == 1) {
145 warnx("duplicate key %s in process accounting stats",
153 /* Copy in-memory pacct_db to pdb_file. */
157 return (db_copy_out(pacct_db, pdb_file, "process accounting",
165 DBT key, data, ndata;
167 struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk;
170 bzero(&ci_total, sizeof ci_total);
171 strcpy(ci_total.ci_comm, "");
172 bzero(&ci_other, sizeof ci_other);
173 strcpy(ci_other.ci_comm, "***other");
174 bzero(&ci_junk, sizeof ci_junk);
175 strcpy(ci_junk.ci_comm, "**junk**");
178 * Retrieve them into new DB, sorted by appropriate key.
179 * At the same time, cull 'other' and 'junk'
181 bzero(&bti, sizeof bti);
182 bti.compare = sa_cmp;
183 output_pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
184 if (output_pacct_db == NULL) {
185 warn("couldn't sort process accounting stats");
191 rv = DB_SEQ(pacct_db, &key, &data, R_FIRST);
193 warn("retrieving process accounting stats");
195 cip = (struct cmdinfo *) data.data;
196 bcopy(cip, &ci, sizeof ci);
199 add_ci(&ci, &ci_total);
201 if (vflag && ci.ci_calls <= cutoff &&
202 (fflag || check_junk(&ci))) {
203 /* put it into **junk** */
204 add_ci(&ci, &ci_junk);
208 ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) {
209 /* put into ***other */
210 add_ci(&ci, &ci_other);
213 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
215 warn("sorting process accounting stats");
217 next: rv = DB_SEQ(pacct_db, &key, &data, R_NEXT);
219 warn("retrieving process accounting stats");
222 /* insert **junk** and ***other */
223 if (ci_junk.ci_calls != 0) {
224 data.data = &ci_junk;
225 data.size = sizeof ci_junk;
226 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
228 warn("sorting process accounting stats");
230 if (ci_other.ci_calls != 0) {
231 data.data = &ci_other;
232 data.size = sizeof ci_other;
233 rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
235 warn("sorting process accounting stats");
238 /* print out the total */
239 print_ci(&ci_total, &ci_total);
241 /* print out; if reversed, print first (smallest) first */
242 rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST);
244 warn("retrieving process accounting report");
246 cip = (struct cmdinfo *) data.data;
247 bcopy(cip, &ci, sizeof ci);
249 print_ci(&ci, &ci_total);
251 rv = DB_SEQ(output_pacct_db, &data, &ndata,
252 rflag ? R_NEXT : R_PREV);
254 warn("retrieving process accounting report");
256 DB_CLOSE(output_pacct_db);
260 check_junk(const struct cmdinfo *cip)
265 fprintf(stderr, "%s (%ju) -- ", cip->ci_comm, (uintmax_t)cip->ci_calls);
266 cp = fgetln(stdin, &len);
268 return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0;
272 add_ci(const struct cmdinfo *fromcip, struct cmdinfo *tocip)
274 tocip->ci_calls += fromcip->ci_calls;
275 tocip->ci_etime += fromcip->ci_etime;
276 tocip->ci_utime += fromcip->ci_utime;
277 tocip->ci_stime += fromcip->ci_stime;
278 tocip->ci_mem += fromcip->ci_mem;
279 tocip->ci_io += fromcip->ci_io;
283 print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip)
288 c = cip->ci_calls ? cip->ci_calls : 1;
289 t = (cip->ci_utime + cip->ci_stime) / 1000000;
296 printf("%8ju ", (uintmax_t)cip->ci_calls);
299 printf(" %4.1f%% ", cip->ci_calls /
300 (double)totalcip->ci_calls * 100);
306 printf("%11.3fre ", cip->ci_etime / (1000000 * c));
308 printf("%11.3fre ", cip->ci_etime / (60.0 * 1000000));
311 printf(" %4.1f%% ", cip->ci_etime /
312 totalcip->ci_etime * 100);
319 printf("%11.3fcp ", t / (double) cip->ci_calls);
321 printf("%11.2fcp ", t / 60.0);
325 (cip->ci_utime + cip->ci_stime) /
326 (totalcip->ci_utime + totalcip->ci_stime) *
333 printf("%11.3fu ", cip->ci_utime / (1000000 * c));
335 printf("%11.2fu ", cip->ci_utime / (60.0 * 1000000));
338 printf(" %4.1f%% ", cip->ci_utime /
339 (double)totalcip->ci_utime * 100);
344 printf("%11.3fs ", cip->ci_stime / (1000000 * c));
346 printf("%11.2fs ", cip->ci_stime / (60.0 * 1000000));
349 printf(" %4.1f%% ", cip->ci_stime /
350 (double)totalcip->ci_stime * 100);
358 printf("%8.2fre/cp ",
360 (cip->ci_utime + cip->ci_stime));
366 printf("%10.0fio ", cip->ci_io);
368 printf("%8.0favio ", cip->ci_io / c);
371 printf("%10.0fk*sec ", cip->ci_mem);
373 printf("%8.0fk ", cip->ci_mem / t);
375 printf(" %s\n", cip->ci_comm);