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.
30 * $FreeBSD: src/usr.sbin/sa/pdb.c,v 1.7 1999/08/28 01:19:53 peter Exp $
31 * $DragonFly: src/usr.sbin/sa/pdb.c,v 1.4 2005/12/05 02:40:28 swildner Exp $
34 #include <sys/types.h>
42 #include "pathnames.h"
44 static int check_junk(struct cmdinfo
*);
45 static void add_ci(const struct cmdinfo
*, struct cmdinfo
*);
46 static void print_ci(const struct cmdinfo
*, const struct cmdinfo
*);
56 pacct_db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, NULL
);
65 saved_pacct_db
= dbopen(_PATH_SAVACCT
, O_RDONLY
, 0, DB_BTREE
,
67 if (saved_pacct_db
== NULL
) {
68 error
= errno
== ENOENT
? 0 : -1;
70 warn("retrieving process accounting summary");
74 serr
= DB_SEQ(saved_pacct_db
, &key
, &data
, R_FIRST
);
76 warn("retrieving process accounting summary");
81 nerr
= DB_PUT(pacct_db
, &key
, &data
, 0);
83 warn("initializing process accounting stats");
88 serr
= DB_SEQ(saved_pacct_db
, &key
, &data
, R_NEXT
);
90 warn("retrieving process accounting summary");
96 closeout
: if (DB_CLOSE(saved_pacct_db
) < 0) {
97 warn("closing process accounting summary");
110 if (DB_CLOSE(pacct_db
) < 0)
111 warn("destroying process accounting stats");
115 pacct_add(const struct cmdinfo
*ci
)
118 struct cmdinfo newci
;
119 char keydata
[sizeof ci
->ci_comm
];
122 bcopy(ci
->ci_comm
, &keydata
, sizeof keydata
);
124 key
.size
= strlen(keydata
);
126 rv
= DB_GET(pacct_db
, &key
, &data
, 0);
128 warn("get key %s from process accounting stats", ci
->ci_comm
);
130 } else if (rv
== 0) { /* it's there; copy whole thing */
131 /* XXX compare size if paranoid */
132 /* add the old data to the new data */
133 bcopy(data
.data
, &newci
, data
.size
);
134 } else { /* it's not there; zero it and copy the key */
135 bzero(&newci
, sizeof newci
);
136 bcopy(key
.data
, newci
.ci_comm
, key
.size
);
142 data
.size
= sizeof newci
;
143 rv
= DB_PUT(pacct_db
, &key
, &data
, 0);
145 warn("add key %s to process accounting stats", ci
->ci_comm
);
147 } else if (rv
== 1) {
148 warnx("duplicate key %s in process accounting stats",
161 int error
, serr
, nerr
;
163 saved_pacct_db
= dbopen(_PATH_SAVACCT
, O_RDWR
|O_CREAT
|O_TRUNC
, 0644,
165 if (saved_pacct_db
== NULL
) {
166 warn("creating process accounting summary");
172 serr
= DB_SEQ(pacct_db
, &key
, &data
, R_FIRST
);
174 warn("retrieving process accounting stats");
178 nerr
= DB_PUT(saved_pacct_db
, &key
, &data
, 0);
180 warn("saving process accounting summary");
185 serr
= DB_SEQ(pacct_db
, &key
, &data
, R_NEXT
);
187 warn("retrieving process accounting stats");
193 if (DB_SYNC(saved_pacct_db
, 0) < 0) {
194 warn("syncing process accounting summary");
197 if (DB_CLOSE(saved_pacct_db
) < 0) {
198 warn("closing process accounting summary");
208 DBT key
, data
, ndata
;
210 struct cmdinfo
*cip
, ci
, ci_total
, ci_other
, ci_junk
;
213 bzero(&ci_total
, sizeof ci_total
);
214 strcpy(ci_total
.ci_comm
, "");
215 bzero(&ci_other
, sizeof ci_other
);
216 strcpy(ci_other
.ci_comm
, "***other");
217 bzero(&ci_junk
, sizeof ci_junk
);
218 strcpy(ci_junk
.ci_comm
, "**junk**");
221 * Retrieve them into new DB, sorted by appropriate key.
222 * At the same time, cull 'other' and 'junk'
224 bzero(&bti
, sizeof bti
);
225 bti
.compare
= sa_cmp
;
226 output_pacct_db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, &bti
);
227 if (output_pacct_db
== NULL
) {
228 warn("couldn't sort process accounting stats");
234 rv
= DB_SEQ(pacct_db
, &key
, &data
, R_FIRST
);
236 warn("retrieving process accounting stats");
238 cip
= (struct cmdinfo
*) data
.data
;
239 bcopy(cip
, &ci
, sizeof ci
);
242 add_ci(&ci
, &ci_total
);
244 if (vflag
&& ci
.ci_calls
<= cutoff
&&
245 (fflag
|| check_junk(&ci
))) {
246 /* put it into **junk** */
247 add_ci(&ci
, &ci_junk
);
251 ((ci
.ci_flags
& CI_UNPRINTABLE
) != 0 || ci
.ci_calls
<= 1)) {
252 /* put into ***other */
253 add_ci(&ci
, &ci_other
);
256 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
258 warn("sorting process accounting stats");
260 next
: rv
= DB_SEQ(pacct_db
, &key
, &data
, R_NEXT
);
262 warn("retrieving process accounting stats");
265 /* insert **junk** and ***other */
266 if (ci_junk
.ci_calls
!= 0) {
267 data
.data
= &ci_junk
;
268 data
.size
= sizeof ci_junk
;
269 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
271 warn("sorting process accounting stats");
273 if (ci_other
.ci_calls
!= 0) {
274 data
.data
= &ci_other
;
275 data
.size
= sizeof ci_other
;
276 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
278 warn("sorting process accounting stats");
281 /* print out the total */
282 print_ci(&ci_total
, &ci_total
);
284 /* print out; if reversed, print first (smallest) first */
285 rv
= DB_SEQ(output_pacct_db
, &data
, &ndata
, rflag
? R_FIRST
: R_LAST
);
287 warn("retrieving process accounting report");
289 cip
= (struct cmdinfo
*) data
.data
;
290 bcopy(cip
, &ci
, sizeof ci
);
292 print_ci(&ci
, &ci_total
);
294 rv
= DB_SEQ(output_pacct_db
, &data
, &ndata
,
295 rflag
? R_NEXT
: R_PREV
);
297 warn("retrieving process accounting report");
299 DB_CLOSE(output_pacct_db
);
303 check_junk(struct cmdinfo
*cip
)
308 fprintf(stderr
, "%s (%qu) -- ", cip
->ci_comm
, cip
->ci_calls
);
309 cp
= fgetln(stdin
, &len
);
311 return (cp
&& (cp
[0] == 'y' || cp
[0] == 'Y')) ? 1 : 0;
315 add_ci(const struct cmdinfo
*fromcip
, struct cmdinfo
*tocip
)
317 tocip
->ci_calls
+= fromcip
->ci_calls
;
318 tocip
->ci_etime
+= fromcip
->ci_etime
;
319 tocip
->ci_utime
+= fromcip
->ci_utime
;
320 tocip
->ci_stime
+= fromcip
->ci_stime
;
321 tocip
->ci_mem
+= fromcip
->ci_mem
;
322 tocip
->ci_io
+= fromcip
->ci_io
;
326 print_ci(const struct cmdinfo
*cip
, const struct cmdinfo
*totalcip
)
331 c
= cip
->ci_calls
? cip
->ci_calls
: 1;
332 t
= (cip
->ci_utime
+ cip
->ci_stime
) / (double) AHZ
;
339 printf("%8qu ", cip
->ci_calls
);
343 cip
->ci_calls
/ (double) totalcip
->ci_calls
);
349 printf("%11.2fre ", cip
->ci_etime
/ (double) (AHZ
* c
));
351 printf("%11.2fre ", cip
->ci_etime
/ (60.0 * AHZ
));
355 cip
->ci_etime
/ (double) totalcip
->ci_etime
);
362 printf("%11.2fcp ", t
/ (double) cip
->ci_calls
);
364 printf("%11.2fcp ", t
/ 60.0);
368 (cip
->ci_utime
+ cip
->ci_stime
) / (double)
369 (totalcip
->ci_utime
+ totalcip
->ci_stime
));
375 printf("%11.2fu ", cip
->ci_utime
/ (double) (AHZ
* c
));
377 printf("%11.2fu ", cip
->ci_utime
/ (60.0 * AHZ
));
380 printf(" %4.2f%% ", cip
->ci_utime
/ (double) totalcip
->ci_utime
);
385 printf("%11.2fs ", cip
->ci_stime
/ (double) (AHZ
* c
));
387 printf("%11.2fs ", cip
->ci_stime
/ (60.0 * AHZ
));
390 printf(" %4.2f%% ", cip
->ci_stime
/ (double) totalcip
->ci_stime
);
398 printf("%8.2fre/cp ", cip
->ci_etime
/ (double) (cip
->ci_utime
+ cip
->ci_stime
));
403 printf("%10qutio ", cip
->ci_io
);
405 printf("%8.0favio ", cip
->ci_io
/ c
);
408 printf("%10quk*sec ", cip
->ci_mem
);
410 printf("%8.0fk ", cip
->ci_mem
/ t
);
412 printf(" %s\n", cip
->ci_comm
);