1 /* 2 * Copyright (c) 1994 Christopher G. Demetriou 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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 18 * 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. 29 */ 30 31 #ifndef LINT 32 static char rcsid[] = "$Id: pdb.c,v 1.1.1.1 1994/09/26 21:22:56 davidg Exp $"; 33 #endif 34 35 #include <sys/types.h> 36 #include <sys/acct.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <stdio.h> 41 #include "extern.h" 42 #include "pathnames.h" 43 44 static int check_junk __P((struct cmdinfo *)); 45 static void add_ci __P((const struct cmdinfo *, struct cmdinfo *)); 46 static void print_ci __P((const struct cmdinfo *, const struct cmdinfo *)); 47 48 static DB *pacct_db; 49 50 int 51 pacct_init() 52 { 53 DB *saved_pacct_db; 54 int error; 55 56 pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL); 57 if (pacct_db == NULL) 58 return (-1); 59 60 error = 0; 61 if (!iflag) { 62 DBT key, data; 63 int serr, nerr; 64 65 saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDONLY, 0, DB_BTREE, 66 NULL); 67 if (saved_pacct_db == NULL) { 68 error = errno == ENOENT ? 0 : -1; 69 if (error) 70 warn("retrieving process accounting summary"); 71 goto out; 72 } 73 74 serr = DB_SEQ(saved_pacct_db, &key, &data, R_FIRST); 75 if (serr < 0) { 76 warn("retrieving process accounting summary"); 77 error = -1; 78 goto closeout; 79 } 80 while (serr == 0) { 81 nerr = DB_PUT(pacct_db, &key, &data, 0); 82 if (nerr < 0) { 83 warn("initializing process accounting stats"); 84 error = -1; 85 break; 86 } 87 88 serr = DB_SEQ(saved_pacct_db, &key, &data, R_NEXT); 89 if (serr < 0) { 90 warn("retrieving process accounting summary"); 91 error = -1; 92 break; 93 } 94 } 95 96 closeout: if (DB_CLOSE(saved_pacct_db) < 0) { 97 warn("closing process accounting summary"); 98 error = -1; 99 } 100 } 101 102 out: if (error != 0) 103 pacct_destroy(); 104 return (error); 105 } 106 107 void 108 pacct_destroy() 109 { 110 if (DB_CLOSE(pacct_db) < 0) 111 warn("destroying process accounting stats"); 112 } 113 114 int 115 pacct_add(ci) 116 const struct cmdinfo *ci; 117 { 118 DBT key, data; 119 struct cmdinfo newci; 120 char keydata[sizeof ci->ci_comm]; 121 int rv; 122 123 bcopy(ci->ci_comm, &keydata, sizeof keydata); 124 key.data = &keydata; 125 key.size = strlen(keydata); 126 127 rv = DB_GET(pacct_db, &key, &data, 0); 128 if (rv < 0) { 129 warn("get key %s from process accounting stats", ci->ci_comm); 130 return (-1); 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); 138 } 139 140 add_ci(ci, &newci); 141 142 data.data = &newci; 143 data.size = sizeof newci; 144 rv = DB_PUT(pacct_db, &key, &data, 0); 145 if (rv < 0) { 146 warn("add key %s to process accounting stats", ci->ci_comm); 147 return (-1); 148 } else if (rv == 1) { 149 warnx("duplicate key %s in process accounting stats", 150 ci->ci_comm); 151 return (-1); 152 } 153 154 return (0); 155 } 156 157 int 158 pacct_update() 159 { 160 DB *saved_pacct_db; 161 DBT key, data; 162 int error, serr, nerr; 163 164 saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDWR|O_CREAT|O_TRUNC, 0644, 165 DB_BTREE, NULL); 166 if (saved_pacct_db == NULL) { 167 warn("creating process accounting summary"); 168 return (-1); 169 } 170 171 error = 0; 172 173 serr = DB_SEQ(pacct_db, &key, &data, R_FIRST); 174 if (serr < 0) { 175 warn("retrieving process accounting stats"); 176 error = -1; 177 } 178 while (serr == 0) { 179 nerr = DB_PUT(saved_pacct_db, &key, &data, 0); 180 if (nerr < 0) { 181 warn("saving process accounting summary"); 182 error = -1; 183 break; 184 } 185 186 serr = DB_SEQ(pacct_db, &key, &data, R_NEXT); 187 if (serr < 0) { 188 warn("retrieving process accounting stats"); 189 error = -1; 190 break; 191 } 192 } 193 194 if (DB_SYNC(saved_pacct_db, 0) < 0) { 195 warn("syncing process accounting summary"); 196 error = -1; 197 } 198 if (DB_CLOSE(saved_pacct_db) < 0) { 199 warn("closing process accounting summary"); 200 error = -1; 201 } 202 return error; 203 } 204 205 void 206 pacct_print() 207 { 208 BTREEINFO bti; 209 DBT key, data, ndata; 210 DB *output_pacct_db; 211 struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk; 212 int rv; 213 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**"); 220 221 /* 222 * Retrieve them into new DB, sorted by appropriate key. 223 * At the same time, cull 'other' and 'junk' 224 */ 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"); 230 return; 231 } 232 233 ndata.data = NULL; 234 ndata.size = 0; 235 rv = DB_SEQ(pacct_db, &key, &data, R_FIRST); 236 if (rv < 0) 237 warn("retrieving process accounting stats"); 238 while (rv == 0) { 239 cip = (struct cmdinfo *) data.data; 240 bcopy(cip, &ci, sizeof ci); 241 242 /* add to total */ 243 add_ci(&ci, &ci_total); 244 245 if (vflag && ci.ci_calls <= cutoff && 246 (fflag || check_junk(&ci))) { 247 /* put it into **junk** */ 248 add_ci(&ci, &ci_junk); 249 goto next; 250 } 251 if (!aflag && 252 ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) { 253 /* put into ***other */ 254 add_ci(&ci, &ci_other); 255 goto next; 256 } 257 rv = DB_PUT(output_pacct_db, &data, &ndata, 0); 258 if (rv < 0) 259 warn("sorting process accounting stats"); 260 261 next: rv = DB_SEQ(pacct_db, &key, &data, R_NEXT); 262 if (rv < 0) 263 warn("retrieving process accounting stats"); 264 } 265 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); 271 if (rv < 0) 272 warn("sorting process accounting stats"); 273 } 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); 278 if (rv < 0) 279 warn("sorting process accounting stats"); 280 } 281 282 /* print out the total */ 283 print_ci(&ci_total, &ci_total); 284 285 /* print out; if reversed, print first (smallest) first */ 286 rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST); 287 if (rv < 0) 288 warn("retrieving process accounting report"); 289 while (rv == 0) { 290 cip = (struct cmdinfo *) data.data; 291 bcopy(cip, &ci, sizeof ci); 292 293 print_ci(&ci, &ci_total); 294 295 rv = DB_SEQ(output_pacct_db, &data, &ndata, 296 rflag ? R_NEXT : R_PREV); 297 if (rv < 0) 298 warn("retrieving process accounting report"); 299 } 300 DB_CLOSE(output_pacct_db); 301 } 302 303 static int 304 check_junk(cip) 305 struct cmdinfo *cip; 306 { 307 char *cp; 308 size_t len; 309 310 fprintf(stderr, "%s (%qu) -- ", cip->ci_comm, cip->ci_calls); 311 cp = fgetln(stdin, &len); 312 313 return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0; 314 } 315 316 static void 317 add_ci(fromcip, tocip) 318 const struct cmdinfo *fromcip; 319 struct cmdinfo *tocip; 320 { 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; 327 } 328 329 static void 330 print_ci(cip, totalcip) 331 const struct cmdinfo *cip, *totalcip; 332 { 333 double t, c; 334 int uflow; 335 336 c = cip->ci_calls ? cip->ci_calls : 1; 337 t = (cip->ci_utime + cip->ci_stime) / (double) AHZ; 338 if (t < 0.01) { 339 t = 0.01; 340 uflow = 1; 341 } else 342 uflow = 0; 343 344 printf("%8qu ", cip->ci_calls); 345 if (cflag) { 346 if (cip != totalcip) 347 printf(" %4.2f%% ", 348 cip->ci_calls / (double) totalcip->ci_calls); 349 else 350 printf(" %4s ", ""); 351 } 352 353 if (jflag) 354 printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c)); 355 else 356 printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ)); 357 if (cflag) { 358 if (cip != totalcip) 359 printf(" %4.2f%% ", 360 cip->ci_etime / (double) totalcip->ci_etime); 361 else 362 printf(" %4s ", ""); 363 } 364 365 if (!lflag) { 366 if (jflag) 367 printf("%11.2fcp ", t / (double) cip->ci_calls); 368 else 369 printf("%11.2fcp ", t / 60.0); 370 if (cflag) { 371 if (cip != totalcip) 372 printf(" %4.2f%% ", 373 (cip->ci_utime + cip->ci_stime) / (double) 374 (totalcip->ci_utime + totalcip->ci_stime)); 375 else 376 printf(" %4s ", ""); 377 } 378 } else { 379 if (jflag) 380 printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c)); 381 else 382 printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ)); 383 if (cflag) { 384 if (cip != totalcip) 385 printf(" %4.2f%% ", cip->ci_utime / (double) totalcip->ci_utime); 386 else 387 printf(" %4s ", ""); 388 } 389 if (jflag) 390 printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c)); 391 else 392 printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ)); 393 if (cflag) { 394 if (cip != totalcip) 395 printf(" %4.2f%% ", cip->ci_stime / (double) totalcip->ci_stime); 396 else 397 printf(" %4s ", ""); 398 } 399 } 400 401 if (tflag) 402 if (!uflow) 403 printf("%8.2fre/cp ", cip->ci_etime / (double) (cip->ci_utime + cip->ci_stime)); 404 else 405 printf("%8 ", "*ignore*"); 406 407 if (Dflag) 408 printf("%10qutio ", cip->ci_io); 409 else 410 printf("%8.0favio ", cip->ci_io / c); 411 412 if (Kflag) 413 printf("%10quk*sec ", cip->ci_mem); 414 else 415 printf("%8.0fk ", cip->ci_mem / t); 416 417 printf(" %s\n", cip->ci_comm); 418 } 419