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.3 2003/11/03 19:31:43 eirikn 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");
116 const struct cmdinfo
*ci
;
119 struct cmdinfo newci
;
120 char keydata
[sizeof ci
->ci_comm
];
123 bcopy(ci
->ci_comm
, &keydata
, sizeof keydata
);
125 key
.size
= strlen(keydata
);
127 rv
= DB_GET(pacct_db
, &key
, &data
, 0);
129 warn("get key %s from process accounting stats", ci
->ci_comm
);
131 } else if (rv
== 0) { /* it's there; copy whole thing */
132 /* XXX compare size if paranoid */
133 /* add the old data to the new data */
134 bcopy(data
.data
, &newci
, data
.size
);
135 } else { /* it's not there; zero it and copy the key */
136 bzero(&newci
, sizeof newci
);
137 bcopy(key
.data
, newci
.ci_comm
, key
.size
);
143 data
.size
= sizeof newci
;
144 rv
= DB_PUT(pacct_db
, &key
, &data
, 0);
146 warn("add key %s to process accounting stats", ci
->ci_comm
);
148 } else if (rv
== 1) {
149 warnx("duplicate key %s in process accounting stats",
162 int error
, serr
, nerr
;
164 saved_pacct_db
= dbopen(_PATH_SAVACCT
, O_RDWR
|O_CREAT
|O_TRUNC
, 0644,
166 if (saved_pacct_db
== NULL
) {
167 warn("creating process accounting summary");
173 serr
= DB_SEQ(pacct_db
, &key
, &data
, R_FIRST
);
175 warn("retrieving process accounting stats");
179 nerr
= DB_PUT(saved_pacct_db
, &key
, &data
, 0);
181 warn("saving process accounting summary");
186 serr
= DB_SEQ(pacct_db
, &key
, &data
, R_NEXT
);
188 warn("retrieving process accounting stats");
194 if (DB_SYNC(saved_pacct_db
, 0) < 0) {
195 warn("syncing process accounting summary");
198 if (DB_CLOSE(saved_pacct_db
) < 0) {
199 warn("closing process accounting summary");
209 DBT key
, data
, ndata
;
211 struct cmdinfo
*cip
, ci
, ci_total
, ci_other
, ci_junk
;
214 bzero(&ci_total
, sizeof ci_total
);
215 strcpy(ci_total
.ci_comm
, "");
216 bzero(&ci_other
, sizeof ci_other
);
217 strcpy(ci_other
.ci_comm
, "***other");
218 bzero(&ci_junk
, sizeof ci_junk
);
219 strcpy(ci_junk
.ci_comm
, "**junk**");
222 * Retrieve them into new DB, sorted by appropriate key.
223 * At the same time, cull 'other' and 'junk'
225 bzero(&bti
, sizeof bti
);
226 bti
.compare
= sa_cmp
;
227 output_pacct_db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, &bti
);
228 if (output_pacct_db
== NULL
) {
229 warn("couldn't sort process accounting stats");
235 rv
= DB_SEQ(pacct_db
, &key
, &data
, R_FIRST
);
237 warn("retrieving process accounting stats");
239 cip
= (struct cmdinfo
*) data
.data
;
240 bcopy(cip
, &ci
, sizeof ci
);
243 add_ci(&ci
, &ci_total
);
245 if (vflag
&& ci
.ci_calls
<= cutoff
&&
246 (fflag
|| check_junk(&ci
))) {
247 /* put it into **junk** */
248 add_ci(&ci
, &ci_junk
);
252 ((ci
.ci_flags
& CI_UNPRINTABLE
) != 0 || ci
.ci_calls
<= 1)) {
253 /* put into ***other */
254 add_ci(&ci
, &ci_other
);
257 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
259 warn("sorting process accounting stats");
261 next
: rv
= DB_SEQ(pacct_db
, &key
, &data
, R_NEXT
);
263 warn("retrieving process accounting stats");
266 /* insert **junk** and ***other */
267 if (ci_junk
.ci_calls
!= 0) {
268 data
.data
= &ci_junk
;
269 data
.size
= sizeof ci_junk
;
270 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
272 warn("sorting process accounting stats");
274 if (ci_other
.ci_calls
!= 0) {
275 data
.data
= &ci_other
;
276 data
.size
= sizeof ci_other
;
277 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
279 warn("sorting process accounting stats");
282 /* print out the total */
283 print_ci(&ci_total
, &ci_total
);
285 /* print out; if reversed, print first (smallest) first */
286 rv
= DB_SEQ(output_pacct_db
, &data
, &ndata
, rflag
? R_FIRST
: R_LAST
);
288 warn("retrieving process accounting report");
290 cip
= (struct cmdinfo
*) data
.data
;
291 bcopy(cip
, &ci
, sizeof ci
);
293 print_ci(&ci
, &ci_total
);
295 rv
= DB_SEQ(output_pacct_db
, &data
, &ndata
,
296 rflag
? R_NEXT
: R_PREV
);
298 warn("retrieving process accounting report");
300 DB_CLOSE(output_pacct_db
);
310 fprintf(stderr
, "%s (%qu) -- ", cip
->ci_comm
, cip
->ci_calls
);
311 cp
= fgetln(stdin
, &len
);
313 return (cp
&& (cp
[0] == 'y' || cp
[0] == 'Y')) ? 1 : 0;
317 add_ci(fromcip
, tocip
)
318 const struct cmdinfo
*fromcip
;
319 struct cmdinfo
*tocip
;
321 tocip
->ci_calls
+= fromcip
->ci_calls
;
322 tocip
->ci_etime
+= fromcip
->ci_etime
;
323 tocip
->ci_utime
+= fromcip
->ci_utime
;
324 tocip
->ci_stime
+= fromcip
->ci_stime
;
325 tocip
->ci_mem
+= fromcip
->ci_mem
;
326 tocip
->ci_io
+= fromcip
->ci_io
;
330 print_ci(cip
, totalcip
)
331 const struct cmdinfo
*cip
, *totalcip
;
336 c
= cip
->ci_calls
? cip
->ci_calls
: 1;
337 t
= (cip
->ci_utime
+ cip
->ci_stime
) / (double) AHZ
;
344 printf("%8qu ", cip
->ci_calls
);
348 cip
->ci_calls
/ (double) totalcip
->ci_calls
);
354 printf("%11.2fre ", cip
->ci_etime
/ (double) (AHZ
* c
));
356 printf("%11.2fre ", cip
->ci_etime
/ (60.0 * AHZ
));
360 cip
->ci_etime
/ (double) totalcip
->ci_etime
);
367 printf("%11.2fcp ", t
/ (double) cip
->ci_calls
);
369 printf("%11.2fcp ", t
/ 60.0);
373 (cip
->ci_utime
+ cip
->ci_stime
) / (double)
374 (totalcip
->ci_utime
+ totalcip
->ci_stime
));
380 printf("%11.2fu ", cip
->ci_utime
/ (double) (AHZ
* c
));
382 printf("%11.2fu ", cip
->ci_utime
/ (60.0 * AHZ
));
385 printf(" %4.2f%% ", cip
->ci_utime
/ (double) totalcip
->ci_utime
);
390 printf("%11.2fs ", cip
->ci_stime
/ (double) (AHZ
* c
));
392 printf("%11.2fs ", cip
->ci_stime
/ (60.0 * AHZ
));
395 printf(" %4.2f%% ", cip
->ci_stime
/ (double) totalcip
->ci_stime
);
403 printf("%8.2fre/cp ", cip
->ci_etime
/ (double) (cip
->ci_utime
+ cip
->ci_stime
));
408 printf("%10qutio ", cip
->ci_io
);
410 printf("%8.0favio ", cip
->ci_io
/ c
);
413 printf("%10quk*sec ", cip
->ci_mem
);
415 printf("%8.0fk ", cip
->ci_mem
/ t
);
417 printf(" %s\n", cip
->ci_comm
);