]>
git.saurik.com Git - apple/system_cmds.git/blob - sa.tproj/pdb.c
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.
32 static const char rcsid
[] =
33 "$FreeBSD: src/usr.sbin/sa/pdb.c,v 1.9 2002/07/15 16:05:15 des Exp $";
36 #include <sys/types.h>
45 #include "pathnames.h"
47 static int check_junk
__P((struct cmdinfo
*));
48 static void add_ci
__P((const struct cmdinfo
*, struct cmdinfo
*));
49 static void print_ci
__P((const struct cmdinfo
*, const struct cmdinfo
*));
59 pacct_db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, NULL
);
68 saved_pacct_db
= dbopen(_PATH_SAVACCT
, O_RDONLY
, 0, DB_BTREE
,
70 if (saved_pacct_db
== NULL
) {
71 error
= errno
== ENOENT
? 0 : -1;
73 warn("retrieving process accounting summary");
77 serr
= DB_SEQ(saved_pacct_db
, &key
, &data
, R_FIRST
);
79 warn("retrieving process accounting summary");
84 nerr
= DB_PUT(pacct_db
, &key
, &data
, 0);
86 warn("initializing process accounting stats");
91 serr
= DB_SEQ(saved_pacct_db
, &key
, &data
, R_NEXT
);
93 warn("retrieving process accounting summary");
99 closeout
: if (DB_CLOSE(saved_pacct_db
) < 0) {
100 warn("closing process accounting summary");
113 if (DB_CLOSE(pacct_db
) < 0)
114 warn("destroying process accounting stats");
119 const struct cmdinfo
*ci
;
122 struct cmdinfo newci
;
123 char keydata
[sizeof ci
->ci_comm
];
126 bcopy(ci
->ci_comm
, &keydata
, sizeof keydata
);
128 key
.size
= strlen(keydata
);
130 rv
= DB_GET(pacct_db
, &key
, &data
, 0);
132 warn("get key %s from process accounting stats", ci
->ci_comm
);
134 } else if (rv
== 0) { /* it's there; copy whole thing */
135 /* XXX compare size if paranoid */
136 /* add the old data to the new data */
137 bcopy(data
.data
, &newci
, data
.size
);
138 } else { /* it's not there; zero it and copy the key */
139 bzero(&newci
, sizeof newci
);
140 bcopy(key
.data
, newci
.ci_comm
, key
.size
);
146 data
.size
= sizeof newci
;
147 rv
= DB_PUT(pacct_db
, &key
, &data
, 0);
149 warn("add key %s to process accounting stats", ci
->ci_comm
);
151 } else if (rv
== 1) {
152 warnx("duplicate key %s in process accounting stats",
165 int error
, serr
, nerr
;
167 saved_pacct_db
= dbopen(_PATH_SAVACCT
, O_RDWR
|O_CREAT
|O_TRUNC
, 0644,
169 if (saved_pacct_db
== NULL
) {
170 warn("creating process accounting summary");
176 serr
= DB_SEQ(pacct_db
, &key
, &data
, R_FIRST
);
178 warn("retrieving process accounting stats");
182 nerr
= DB_PUT(saved_pacct_db
, &key
, &data
, 0);
184 warn("saving process accounting summary");
189 serr
= DB_SEQ(pacct_db
, &key
, &data
, R_NEXT
);
191 warn("retrieving process accounting stats");
197 if (DB_SYNC(saved_pacct_db
, 0) < 0) {
198 warn("syncing process accounting summary");
201 if (DB_CLOSE(saved_pacct_db
) < 0) {
202 warn("closing process accounting summary");
212 DBT key
, data
, ndata
;
214 struct cmdinfo
*cip
, ci
, ci_total
, ci_other
, ci_junk
;
217 bzero(&ci_total
, sizeof ci_total
);
218 strcpy(ci_total
.ci_comm
, "");
219 bzero(&ci_other
, sizeof ci_other
);
220 strcpy(ci_other
.ci_comm
, "***other");
221 bzero(&ci_junk
, sizeof ci_junk
);
222 strcpy(ci_junk
.ci_comm
, "**junk**");
225 * Retrieve them into new DB, sorted by appropriate key.
226 * At the same time, cull 'other' and 'junk'
228 bzero(&bti
, sizeof bti
);
229 bti
.compare
= sa_cmp
;
230 output_pacct_db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, &bti
);
231 if (output_pacct_db
== NULL
) {
232 warn("couldn't sort process accounting stats");
238 rv
= DB_SEQ(pacct_db
, &key
, &data
, R_FIRST
);
240 warn("retrieving process accounting stats");
242 cip
= (struct cmdinfo
*) data
.data
;
243 bcopy(cip
, &ci
, sizeof ci
);
246 add_ci(&ci
, &ci_total
);
248 if (vflag
&& ci
.ci_calls
<= cutoff
&&
249 (fflag
|| check_junk(&ci
))) {
250 /* put it into **junk** */
251 add_ci(&ci
, &ci_junk
);
255 ((ci
.ci_flags
& CI_UNPRINTABLE
) != 0 || ci
.ci_calls
<= 1)) {
256 /* put into ***other */
257 add_ci(&ci
, &ci_other
);
260 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
262 warn("sorting process accounting stats");
264 next
: rv
= DB_SEQ(pacct_db
, &key
, &data
, R_NEXT
);
266 warn("retrieving process accounting stats");
269 /* insert **junk** and ***other */
270 if (ci_junk
.ci_calls
!= 0) {
271 data
.data
= &ci_junk
;
272 data
.size
= sizeof ci_junk
;
273 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
275 warn("sorting process accounting stats");
277 if (ci_other
.ci_calls
!= 0) {
278 data
.data
= &ci_other
;
279 data
.size
= sizeof ci_other
;
280 rv
= DB_PUT(output_pacct_db
, &data
, &ndata
, 0);
282 warn("sorting process accounting stats");
285 /* print out the total */
286 print_ci(&ci_total
, &ci_total
);
288 /* print out; if reversed, print first (smallest) first */
289 rv
= DB_SEQ(output_pacct_db
, &data
, &ndata
, rflag
? R_FIRST
: R_LAST
);
291 warn("retrieving process accounting report");
293 cip
= (struct cmdinfo
*) data
.data
;
294 bcopy(cip
, &ci
, sizeof ci
);
296 print_ci(&ci
, &ci_total
);
298 rv
= DB_SEQ(output_pacct_db
, &data
, &ndata
,
299 rflag
? R_NEXT
: R_PREV
);
301 warn("retrieving process accounting report");
303 DB_CLOSE(output_pacct_db
);
313 fprintf(stderr
, "%s (%ju) -- ", cip
->ci_comm
, (uintmax_t)cip
->ci_calls
);
314 cp
= fgetln(stdin
, &len
);
316 return (cp
&& (cp
[0] == 'y' || cp
[0] == 'Y')) ? 1 : 0;
320 add_ci(fromcip
, tocip
)
321 const struct cmdinfo
*fromcip
;
322 struct cmdinfo
*tocip
;
324 tocip
->ci_calls
+= fromcip
->ci_calls
;
325 tocip
->ci_etime
+= fromcip
->ci_etime
;
326 tocip
->ci_utime
+= fromcip
->ci_utime
;
327 tocip
->ci_stime
+= fromcip
->ci_stime
;
328 tocip
->ci_mem
+= fromcip
->ci_mem
;
329 tocip
->ci_io
+= fromcip
->ci_io
;
333 print_ci(cip
, totalcip
)
334 const struct cmdinfo
*cip
, *totalcip
;
339 c
= cip
->ci_calls
? cip
->ci_calls
: 1;
340 t
= (cip
->ci_utime
+ cip
->ci_stime
) / (double) AHZ
;
347 printf("%8ju ", (uintmax_t)cip
->ci_calls
);
351 cip
->ci_calls
/ (double) totalcip
->ci_calls
);
357 printf("%11.2fre ", cip
->ci_etime
/ (double) (AHZ
* c
));
359 printf("%11.2fre ", cip
->ci_etime
/ (60.0 * AHZ
));
363 cip
->ci_etime
/ (double) totalcip
->ci_etime
);
370 printf("%11.2fcp ", t
/ (double) cip
->ci_calls
);
372 printf("%11.2fcp ", t
/ 60.0);
376 (cip
->ci_utime
+ cip
->ci_stime
) / (double)
377 (totalcip
->ci_utime
+ totalcip
->ci_stime
));
383 printf("%11.2fu ", cip
->ci_utime
/ (double) (AHZ
* c
));
385 printf("%11.2fu ", cip
->ci_utime
/ (60.0 * AHZ
));
388 printf(" %4.2f%% ", cip
->ci_utime
/ (double) totalcip
->ci_utime
);
393 printf("%11.2fs ", cip
->ci_stime
/ (double) (AHZ
* c
));
395 printf("%11.2fs ", cip
->ci_stime
/ (60.0 * AHZ
));
398 printf(" %4.2f%% ", cip
->ci_stime
/ (double) totalcip
->ci_stime
);
406 printf("%8.2fre/cp ",
408 (double) (cip
->ci_utime
+ cip
->ci_stime
));
414 printf("%10jutio ", (uintmax_t)cip
->ci_io
);
416 printf("%8.0favio ", cip
->ci_io
/ c
);
419 printf("%10juk*sec ", (uintmax_t)cip
->ci_mem
);
421 printf("%8.0fk ", cip
->ci_mem
/ t
);
423 printf(" %s\n", cip
->ci_comm
);