pdb.c revision 169857
13125Sdg/* 23125Sdg * Copyright (c) 1994 Christopher G. Demetriou 33125Sdg * All rights reserved. 43125Sdg * 53125Sdg * Redistribution and use in source and binary forms, with or without 63125Sdg * modification, are permitted provided that the following conditions 73125Sdg * are met: 83125Sdg * 1. Redistributions of source code must retain the above copyright 93125Sdg * notice, this list of conditions and the following disclaimer. 103125Sdg * 2. Redistributions in binary form must reproduce the above copyright 113125Sdg * notice, this list of conditions and the following disclaimer in the 123125Sdg * documentation and/or other materials provided with the distribution. 133125Sdg * 3. All advertising materials mentioning features or use of this software 143125Sdg * must display the following acknowledgement: 153125Sdg * This product includes software developed by Christopher G. Demetriou. 163125Sdg * 4. The name of the author may not be used to endorse or promote products 173125Sdg * derived from this software without specific prior written permission 183125Sdg * 193125Sdg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 203125Sdg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 213125Sdg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 223125Sdg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 233125Sdg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 243125Sdg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 253125Sdg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 263125Sdg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 273125Sdg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 283125Sdg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 293125Sdg */ 303125Sdg 31114601Sobrien#include <sys/cdefs.h> 32114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/sa/pdb.c 169857 2007-05-22 06:51:38Z dds $"); 333125Sdg 343125Sdg#include <sys/types.h> 353125Sdg#include <sys/acct.h> 363125Sdg#include <err.h> 373125Sdg#include <errno.h> 383125Sdg#include <fcntl.h> 39169857Sdds#include <stdbool.h> 40100107Sdes#include <stdint.h> 413125Sdg#include <stdio.h> 4213558Smpp#include <string.h> 433125Sdg#include "extern.h" 443125Sdg#include "pathnames.h" 453125Sdg 46141638Sdelphijstatic int check_junk(const struct cmdinfo *); 47141638Sdelphijstatic void add_ci(const struct cmdinfo *, struct cmdinfo *); 48141638Sdelphijstatic void print_ci(const struct cmdinfo *, const struct cmdinfo *); 493125Sdg 503125Sdgstatic DB *pacct_db; 513125Sdg 52169857Sdds/* Legacy format in AHZV1 units. */ 53169857Sddsstruct cmdinfov1 { 54169857Sdds char ci_comm[MAXCOMLEN+2]; /* command name (+ '*') */ 55169857Sdds uid_t ci_uid; /* user id */ 56169857Sdds u_quad_t ci_calls; /* number of calls */ 57169857Sdds u_quad_t ci_etime; /* elapsed time */ 58169857Sdds u_quad_t ci_utime; /* user time */ 59169857Sdds u_quad_t ci_stime; /* system time */ 60169857Sdds u_quad_t ci_mem; /* memory use */ 61169857Sdds u_quad_t ci_io; /* number of disk i/o ops */ 62169857Sdds u_int ci_flags; /* flags; see below */ 63169857Sdds}; 64169857Sdds 65169857Sdds/* 66169857Sdds * Convert a v1 data record into the current version. 67169857Sdds * Return 0 if OK, -1 on error, setting errno. 68169857Sdds */ 69169857Sddsstatic int 70169857Sddsv1_to_v2(DBT *key __unused, DBT *data) 713125Sdg{ 72169857Sdds struct cmdinfov1 civ1; 73169857Sdds static struct cmdinfo civ2; 743125Sdg 75169857Sdds if (data->size != sizeof(civ1)) { 76169857Sdds errno = EFTYPE; 773125Sdg return (-1); 783125Sdg } 79169857Sdds memcpy(&civ1, data->data, data->size); 80169857Sdds memset(&civ2, 0, sizeof(civ2)); 81169857Sdds memcpy(civ2.ci_comm, civ1.ci_comm, sizeof(civ2.ci_comm)); 82169857Sdds civ2.ci_uid = civ1.ci_uid; 83169857Sdds civ2.ci_calls = civ1.ci_calls; 84169857Sdds civ2.ci_etime = ((double)civ1.ci_etime / AHZV1) * 1000000; 85169857Sdds civ2.ci_utime = ((double)civ1.ci_utime / AHZV1) * 1000000; 86169857Sdds civ2.ci_stime = ((double)civ1.ci_stime / AHZV1) * 1000000; 87169857Sdds civ2.ci_mem = civ1.ci_mem; 88169857Sdds civ2.ci_io = civ1.ci_io; 89169857Sdds civ2.ci_flags = civ1.ci_flags; 90169857Sdds data->size = sizeof(civ2); 91169857Sdds data->data = &civ2; 92169857Sdds return (0); 93169857Sdds} 943125Sdg 95169857Sdds/* Copy pdb_file to in-memory pacct_db. */ 96169857Sddsint 97169857Sddspacct_init() 98169857Sdds{ 99169857Sdds return (db_copy_in(&pacct_db, pdb_file, "process accounting", 100169857Sdds NULL, v1_to_v2)); 1013125Sdg} 1023125Sdg 1033125Sdgvoid 1043125Sdgpacct_destroy() 1053125Sdg{ 106169857Sdds db_destroy(pacct_db, "process accounting"); 1073125Sdg} 1083125Sdg 1093125Sdgint 110141638Sdelphijpacct_add(const struct cmdinfo *ci) 1113125Sdg{ 1123125Sdg DBT key, data; 1133125Sdg struct cmdinfo newci; 1143125Sdg char keydata[sizeof ci->ci_comm]; 1153125Sdg int rv; 1163125Sdg 1173125Sdg bcopy(ci->ci_comm, &keydata, sizeof keydata); 1183125Sdg key.data = &keydata; 1193125Sdg key.size = strlen(keydata); 1203125Sdg 1213125Sdg rv = DB_GET(pacct_db, &key, &data, 0); 1223125Sdg if (rv < 0) { 1233125Sdg warn("get key %s from process accounting stats", ci->ci_comm); 1243125Sdg return (-1); 1253125Sdg } else if (rv == 0) { /* it's there; copy whole thing */ 1263125Sdg /* XXX compare size if paranoid */ 1273125Sdg /* add the old data to the new data */ 1283125Sdg bcopy(data.data, &newci, data.size); 1293125Sdg } else { /* it's not there; zero it and copy the key */ 1303125Sdg bzero(&newci, sizeof newci); 1313125Sdg bcopy(key.data, newci.ci_comm, key.size); 1323125Sdg } 1338857Srgrimes 1343125Sdg add_ci(ci, &newci); 1353125Sdg 1368857Srgrimes data.data = &newci; 1373125Sdg data.size = sizeof newci; 1383125Sdg rv = DB_PUT(pacct_db, &key, &data, 0); 1393125Sdg if (rv < 0) { 1403125Sdg warn("add key %s to process accounting stats", ci->ci_comm); 1413125Sdg return (-1); 1423125Sdg } else if (rv == 1) { 1433125Sdg warnx("duplicate key %s in process accounting stats", 1443125Sdg ci->ci_comm); 1453125Sdg return (-1); 1463125Sdg } 1473125Sdg 1483125Sdg return (0); 1493125Sdg} 1503125Sdg 151169857Sdds/* Copy in-memory pacct_db to pdb_file. */ 1523125Sdgint 1533125Sdgpacct_update() 1543125Sdg{ 155169857Sdds return (db_copy_out(pacct_db, pdb_file, "process accounting", 156169857Sdds NULL)); 1573125Sdg} 1583125Sdg 1593125Sdgvoid 1603125Sdgpacct_print() 1613125Sdg{ 1623125Sdg BTREEINFO bti; 1633125Sdg DBT key, data, ndata; 1643125Sdg DB *output_pacct_db; 1653125Sdg struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk; 1663125Sdg int rv; 1673125Sdg 1683125Sdg bzero(&ci_total, sizeof ci_total); 1693125Sdg strcpy(ci_total.ci_comm, ""); 1703125Sdg bzero(&ci_other, sizeof ci_other); 1713125Sdg strcpy(ci_other.ci_comm, "***other"); 1723125Sdg bzero(&ci_junk, sizeof ci_junk); 1733125Sdg strcpy(ci_junk.ci_comm, "**junk**"); 1743125Sdg 1753125Sdg /* 1763125Sdg * Retrieve them into new DB, sorted by appropriate key. 1773125Sdg * At the same time, cull 'other' and 'junk' 1783125Sdg */ 1793125Sdg bzero(&bti, sizeof bti); 1803125Sdg bti.compare = sa_cmp; 1813125Sdg output_pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti); 1823125Sdg if (output_pacct_db == NULL) { 1833125Sdg warn("couldn't sort process accounting stats"); 1843125Sdg return; 1853125Sdg } 1863125Sdg 1873125Sdg ndata.data = NULL; 1883125Sdg ndata.size = 0; 1893125Sdg rv = DB_SEQ(pacct_db, &key, &data, R_FIRST); 1903125Sdg if (rv < 0) 1913125Sdg warn("retrieving process accounting stats"); 1923125Sdg while (rv == 0) { 1933125Sdg cip = (struct cmdinfo *) data.data; 1943125Sdg bcopy(cip, &ci, sizeof ci); 1953125Sdg 1963125Sdg /* add to total */ 1973125Sdg add_ci(&ci, &ci_total); 1983125Sdg 1993125Sdg if (vflag && ci.ci_calls <= cutoff && 2003125Sdg (fflag || check_junk(&ci))) { 2013125Sdg /* put it into **junk** */ 2023125Sdg add_ci(&ci, &ci_junk); 2033125Sdg goto next; 2043125Sdg } 2053125Sdg if (!aflag && 2063125Sdg ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) { 2073125Sdg /* put into ***other */ 2083125Sdg add_ci(&ci, &ci_other); 2093125Sdg goto next; 2103125Sdg } 2113125Sdg rv = DB_PUT(output_pacct_db, &data, &ndata, 0); 2123125Sdg if (rv < 0) 2133125Sdg warn("sorting process accounting stats"); 2143125Sdg 2153125Sdgnext: rv = DB_SEQ(pacct_db, &key, &data, R_NEXT); 2163125Sdg if (rv < 0) 2173125Sdg warn("retrieving process accounting stats"); 2183125Sdg } 2193125Sdg 2203125Sdg /* insert **junk** and ***other */ 2213125Sdg if (ci_junk.ci_calls != 0) { 2223125Sdg data.data = &ci_junk; 2233125Sdg data.size = sizeof ci_junk; 2243125Sdg rv = DB_PUT(output_pacct_db, &data, &ndata, 0); 2253125Sdg if (rv < 0) 2263125Sdg warn("sorting process accounting stats"); 2273125Sdg } 2283125Sdg if (ci_other.ci_calls != 0) { 2293125Sdg data.data = &ci_other; 2303125Sdg data.size = sizeof ci_other; 2313125Sdg rv = DB_PUT(output_pacct_db, &data, &ndata, 0); 2323125Sdg if (rv < 0) 2333125Sdg warn("sorting process accounting stats"); 2343125Sdg } 2353125Sdg 2363125Sdg /* print out the total */ 2373125Sdg print_ci(&ci_total, &ci_total); 2383125Sdg 2393125Sdg /* print out; if reversed, print first (smallest) first */ 2403125Sdg rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST); 2413125Sdg if (rv < 0) 2423125Sdg warn("retrieving process accounting report"); 2433125Sdg while (rv == 0) { 2443125Sdg cip = (struct cmdinfo *) data.data; 2453125Sdg bcopy(cip, &ci, sizeof ci); 2463125Sdg 2473125Sdg print_ci(&ci, &ci_total); 2483125Sdg 2493125Sdg rv = DB_SEQ(output_pacct_db, &data, &ndata, 2503125Sdg rflag ? R_NEXT : R_PREV); 2513125Sdg if (rv < 0) 2523125Sdg warn("retrieving process accounting report"); 2533125Sdg } 2543125Sdg DB_CLOSE(output_pacct_db); 2553125Sdg} 2563125Sdg 2573125Sdgstatic int 258141638Sdelphijcheck_junk(const struct cmdinfo *cip) 2593125Sdg{ 2603125Sdg char *cp; 2613125Sdg size_t len; 2623125Sdg 263100107Sdes fprintf(stderr, "%s (%ju) -- ", cip->ci_comm, (uintmax_t)cip->ci_calls); 2643125Sdg cp = fgetln(stdin, &len); 2653125Sdg 2663125Sdg return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0; 2673125Sdg} 2683125Sdg 2693125Sdgstatic void 270141638Sdelphijadd_ci(const struct cmdinfo *fromcip, struct cmdinfo *tocip) 2713125Sdg{ 2723125Sdg tocip->ci_calls += fromcip->ci_calls; 2733125Sdg tocip->ci_etime += fromcip->ci_etime; 2743125Sdg tocip->ci_utime += fromcip->ci_utime; 2753125Sdg tocip->ci_stime += fromcip->ci_stime; 2763125Sdg tocip->ci_mem += fromcip->ci_mem; 2773125Sdg tocip->ci_io += fromcip->ci_io; 2783125Sdg} 2793125Sdg 2803125Sdgstatic void 281141638Sdelphijprint_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip) 2823125Sdg{ 2833125Sdg double t, c; 2843125Sdg int uflow; 2853125Sdg 2863125Sdg c = cip->ci_calls ? cip->ci_calls : 1; 287169857Sdds t = (cip->ci_utime + cip->ci_stime) / 1000000; 2883125Sdg if (t < 0.01) { 2893125Sdg t = 0.01; 2903125Sdg uflow = 1; 2913125Sdg } else 2923125Sdg uflow = 0; 2933125Sdg 294100107Sdes printf("%8ju ", (uintmax_t)cip->ci_calls); 2953125Sdg if (cflag) { 2963125Sdg if (cip != totalcip) 297168456Sdds printf(" %4.1f%% ", cip->ci_calls / 298168456Sdds (double)totalcip->ci_calls * 100); 2993125Sdg else 3003125Sdg printf(" %4s ", ""); 3013125Sdg } 3023125Sdg 3033125Sdg if (jflag) 304169857Sdds printf("%11.3fre ", cip->ci_etime / (1000000 * c)); 3053125Sdg else 306169857Sdds printf("%11.3fre ", cip->ci_etime / (60.0 * 1000000)); 3073125Sdg if (cflag) { 3083125Sdg if (cip != totalcip) 309168456Sdds printf(" %4.1f%% ", cip->ci_etime / 310169857Sdds totalcip->ci_etime * 100); 3113125Sdg else 3123125Sdg printf(" %4s ", ""); 3133125Sdg } 3143125Sdg 3153125Sdg if (!lflag) { 3163125Sdg if (jflag) 317169857Sdds printf("%11.3fcp ", t / (double) cip->ci_calls); 3183125Sdg else 3193125Sdg printf("%11.2fcp ", t / 60.0); 3203125Sdg if (cflag) { 3213125Sdg if (cip != totalcip) 322168456Sdds printf(" %4.1f%% ", 323169857Sdds (cip->ci_utime + cip->ci_stime) / 324168456Sdds (totalcip->ci_utime + totalcip->ci_stime) * 325168456Sdds 100); 3263125Sdg else 3273125Sdg printf(" %4s ", ""); 3283125Sdg } 3293125Sdg } else { 3303125Sdg if (jflag) 331169857Sdds printf("%11.3fu ", cip->ci_utime / (1000000 * c)); 3323125Sdg else 333169857Sdds printf("%11.2fu ", cip->ci_utime / (60.0 * 1000000)); 3343125Sdg if (cflag) { 3353125Sdg if (cip != totalcip) 336168456Sdds printf(" %4.1f%% ", cip->ci_utime / 337168456Sdds (double)totalcip->ci_utime * 100); 3383125Sdg else 3393125Sdg printf(" %4s ", ""); 3403125Sdg } 3413125Sdg if (jflag) 342169857Sdds printf("%11.3fs ", cip->ci_stime / (1000000 * c)); 3433125Sdg else 344169857Sdds printf("%11.2fs ", cip->ci_stime / (60.0 * 1000000)); 3453125Sdg if (cflag) { 3463125Sdg if (cip != totalcip) 347168456Sdds printf(" %4.1f%% ", cip->ci_stime / 348168456Sdds (double)totalcip->ci_stime * 100); 3493125Sdg else 3503125Sdg printf(" %4s ", ""); 3513125Sdg } 3523125Sdg } 3533125Sdg 35499829Salfred if (tflag) { 3553125Sdg if (!uflow) 35699829Salfred printf("%8.2fre/cp ", 35799829Salfred cip->ci_etime / 358169857Sdds (cip->ci_utime + cip->ci_stime)); 3593125Sdg else 36013558Smpp printf("*ignore* "); 36199829Salfred } 3623125Sdg 3633125Sdg if (Dflag) 364169857Sdds printf("%10.0fio ", cip->ci_io); 3653125Sdg else 3663125Sdg printf("%8.0favio ", cip->ci_io / c); 3673125Sdg 3683125Sdg if (Kflag) 369169857Sdds printf("%10.0fk*sec ", cip->ci_mem); 3703125Sdg else 3713125Sdg printf("%8.0fk ", cip->ci_mem / t); 3723125Sdg 3733125Sdg printf(" %s\n", cip->ci_comm); 3743125Sdg} 375