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 $
33 #include <sys/types.h>
41 #include "pathnames.h"
43 static int check_junk(struct cmdinfo
*);
44 static void add_ci(const struct cmdinfo
*, struct cmdinfo
*);
45 static void print_ci(const struct cmdinfo
*, const struct cmdinfo
*);
55 pacct_db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, NULL
);
64 saved_pacct_db
= dbopen(_PATH_SAVACCT
, O_RDONLY
, 0, DB_BTREE
,
66 if (saved_pacct_db
== NULL
) {
67 error
= errno
== ENOENT
? 0 : -1;
69 warn("retrieving process accounting summary");
73 serr
= DB_SEQ(saved_pacct_db
, &key
, &data
, R_FIRST
);
75 warn("retrieving process accounting summary");
80 nerr
= DB_PUT(pacct_db
, &key
, &data
, 0);
82 warn("initializing process accounting stats");
87 serr
= DB_SEQ(saved_pacct_db
, &key
, &data
, R_NEXT
);
89 warn("retrieving process accounting summary");
95 closeout
: if (DB_CLOSE(saved_pacct_db
) < 0) {
96 warn("closing process accounting summary");
109 if (DB_CLOSE(pacct_db
) < 0)
110 warn("destroying process accounting stats");
114 pacct_add(const struct cmdinfo
*ci
)
117 struct cmdinfo newci
;
118 char keydata
[sizeof ci
->ci_comm
];
121 bcopy(ci
->ci_comm
, &keydata
, sizeof keydata
);
123 key
.size
= strlen(keydata
);
125 rv
= DB_GET(pacct_db
, &key
, &data
, 0);
127 warn("get key %s from process accounting stats", ci
->ci_comm
);
129 } else if (rv
== 0) { /* it's there; copy whole thing */
130 /* XXX compare size if paranoid */
131 /* add the old data to the new data */
132 bcopy(data
.data
, &newci
, data
.size
);
133 } else { /* it's not there; zero it and copy the key */
134 bzero(&newci
, sizeof newci
);
135 bcopy(key
.data
, newci
.ci_comm
, key
.size
);
141 data
.size
= sizeof newci
;
142 rv
= DB_PUT(pacct_db
, &key
, &data
, 0);
144 warn("add key %s to process accounting stats", ci
->ci_comm
);
146 } else if (rv
== 1) {
147 warnx("duplicate key %s in process accounting stats",
160 int error
, serr
, nerr
;
162 saved_pacct_db
= dbopen(_PATH_SAVACCT
, O_RDWR
|O_CREAT
|O_TRUNC
, 0644,
164 if (saved_pacct_db
== NULL
) {
165 warn("creating process accounting summary");
171 serr
= DB_SEQ(pacct_db
, &key
, &data
, R_FIRST
);
173 warn("retrieving process accounting stats");
177 nerr
= DB_PUT(saved_pacct_db
, &key
, &data
, 0);
179 warn("saving process accounting summary");
184 serr
= DB_SEQ(pacct_db
, &key
, &data
, R_NEXT
);
186 warn("retrieving process accounting stats");
192 if (DB_SYNC(saved_pacct_db
, 0) < 0) {
193 warn("syncing process accounting summary");
196 if (DB_CLOSE(saved_pacct_db
) < 0) {
197 warn("closing process accounting summary");
207 DBT key
, data
, ndata
;
209 struct cmdinfo
*cip
, ci
, ci_total
, ci_other
, ci_junk
;
212 bzero(&ci_total
, sizeof ci_total
);
213 strcpy(ci_total
.ci_comm
, "");
214 bzero(&ci_other
, sizeof ci_other
);
215 strcpy(ci_other
.ci_comm
, "***other");
216 bzero(&ci_junk
, sizeof ci_junk
);
217 strcpy(ci_junk
.ci_comm
, "**junk**");
220 * Retrieve them into new DB, sorted by appropriate key.
221 * At the same time, cull 'other' and 'junk'
223 bzero(&bti
, sizeof bti
);
224 bti
.compare
= sa_cmp
;
225 output_pacct_db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, &bti
);
226 if (output_pacct_db
== NULL
) {
227 warn("couldn't sort process accounting stats");
233 rv
= DB_SEQ(pacct_db
, &key
, &data
, R_FIRST
);
235 warn("retrieving process accounting stats");
237 cip
= (struct cmdinfo
*) data
.data
;
238 bcopy(cip
, &ci
, sizeof ci
);
241 add_ci(&ci
, &ci_total
);
243 if (vflag
&& ci
.ci_calls
<= cutoff
&&
244 (fflag
|| check_junk(&ci
))) {
245 /* put it into **junk** */
246 add_ci(&ci
, &ci_junk
);
250 ((ci
.ci_flags
& CI_UNPRINTABLE
) != 0 || ci
.ci_calls
<= 1)) {
251 /* put into ***other */
252 add_ci(&ci
, &ci_other
);
255 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
257 warn("sorting process accounting stats");
259 next
: rv
= DB_SEQ(pacct_db
, &key
, &data
, R_NEXT
);
261 warn("retrieving process accounting stats");
264 /* insert **junk** and ***other */
265 if (ci_junk
.ci_calls
!= 0) {
266 data
.data
= &ci_junk
;
267 data
.size
= sizeof ci_junk
;
268 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
270 warn("sorting process accounting stats");
272 if (ci_other
.ci_calls
!= 0) {
273 data
.data
= &ci_other
;
274 data
.size
= sizeof ci_other
;
275 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
277 warn("sorting process accounting stats");
280 /* print out the total */
281 print_ci(&ci_total
, &ci_total
);
283 /* print out; if reversed, print first (smallest) first */
284 rv
= DB_SEQ(output_pacct_db
, &data
, &ndata
, rflag
? R_FIRST
: R_LAST
);
286 warn("retrieving process accounting report");
288 cip
= (struct cmdinfo
*) data
.data
;
289 bcopy(cip
, &ci
, sizeof ci
);
291 print_ci(&ci
, &ci_total
);
293 rv
= DB_SEQ(output_pacct_db
, &data
, &ndata
,
294 rflag
? R_NEXT
: R_PREV
);
296 warn("retrieving process accounting report");
298 DB_CLOSE(output_pacct_db
);
302 check_junk(struct cmdinfo
*cip
)
307 fprintf(stderr
, "%s (%ju) -- ", cip
->ci_comm
,
308 (uintmax_t)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("%8ju ", (uintmax_t)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
));
404 printf("%10jutio ", (uintmax_t)cip
->ci_io
);
406 printf("%8.0favio ", cip
->ci_io
/ c
);
409 printf("%10juk*sec ", (uintmax_t)cip
->ci_mem
);
411 printf("%8.0fk ", cip
->ci_mem
/ t
);
413 printf(" %s\n", cip
->ci_comm
);