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 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/types.h> 35 #include <sys/acct.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <stdbool.h> 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <string.h> 43 #include "extern.h" 44 #include "pathnames.h" 45 46 static int check_junk(const struct cmdinfo *); 47 static void add_ci(const struct cmdinfo *, struct cmdinfo *); 48 static void print_ci(const struct cmdinfo *, const struct cmdinfo *); 49 50 static DB *pacct_db; 51 52 /* Legacy format in AHZV1 units. */ 53 struct cmdinfov1 { 54 char ci_comm[MAXCOMLEN+2]; /* command name (+ '*') */ 55 uid_t ci_uid; /* user id */ 56 u_quad_t ci_calls; /* number of calls */ 57 u_quad_t ci_etime; /* elapsed time */ 58 u_quad_t ci_utime; /* user time */ 59 u_quad_t ci_stime; /* system time */ 60 u_quad_t ci_mem; /* memory use */ 61 u_quad_t ci_io; /* number of disk i/o ops */ 62 u_int ci_flags; /* flags; see below */ 63 }; 64 65 /* 66 * Convert a v1 data record into the current version. 67 * Return 0 if OK, -1 on error, setting errno. 68 */ 69 static int 70 v1_to_v2(DBT *key __unused, DBT *data) 71 { 72 struct cmdinfov1 civ1; 73 static struct cmdinfo civ2; 74 75 if (data->size != sizeof(civ1)) { 76 errno = EFTYPE; 77 return (-1); 78 } 79 memcpy(&civ1, data->data, data->size); 80 memset(&civ2, 0, sizeof(civ2)); 81 memcpy(civ2.ci_comm, civ1.ci_comm, sizeof(civ2.ci_comm)); 82 civ2.ci_uid = civ1.ci_uid; 83 civ2.ci_calls = civ1.ci_calls; 84 civ2.ci_etime = ((double)civ1.ci_etime / AHZV1) * 1000000; 85 civ2.ci_utime = ((double)civ1.ci_utime / AHZV1) * 1000000; 86 civ2.ci_stime = ((double)civ1.ci_stime / AHZV1) * 1000000; 87 civ2.ci_mem = civ1.ci_mem; 88 civ2.ci_io = civ1.ci_io; 89 civ2.ci_flags = civ1.ci_flags; 90 data->size = sizeof(civ2); 91 data->data = &civ2; 92 return (0); 93 } 94 95 /* Copy pdb_file to in-memory pacct_db. */ 96 int 97 pacct_init(void) 98 { 99 return (db_copy_in(&pacct_db, pdb_file, "process accounting", 100 NULL, v1_to_v2)); 101 } 102 103 void 104 pacct_destroy(void) 105 { 106 db_destroy(pacct_db, "process accounting"); 107 } 108 109 int 110 pacct_add(const struct cmdinfo *ci) 111 { 112 DBT key, data; 113 struct cmdinfo newci; 114 char keydata[sizeof ci->ci_comm]; 115 int rv; 116 117 bcopy(ci->ci_comm, &keydata, sizeof keydata); 118 key.data = &keydata; 119 key.size = strlen(keydata); 120 121 rv = DB_GET(pacct_db, &key, &data, 0); 122 if (rv < 0) { 123 warn("get key %s from process accounting stats", ci->ci_comm); 124 return (-1); 125 } else if (rv == 0) { /* it's there; copy whole thing */ 126 /* XXX compare size if paranoid */ 127 /* add the old data to the new data */ 128 bcopy(data.data, &newci, data.size); 129 } else { /* it's not there; zero it and copy the key */ 130 bzero(&newci, sizeof newci); 131 bcopy(key.data, newci.ci_comm, key.size); 132 } 133 134 add_ci(ci, &newci); 135 136 data.data = &newci; 137 data.size = sizeof newci; 138 rv = DB_PUT(pacct_db, &key, &data, 0); 139 if (rv < 0) { 140 warn("add key %s to process accounting stats", ci->ci_comm); 141 return (-1); 142 } else if (rv == 1) { 143 warnx("duplicate key %s in process accounting stats", 144 ci->ci_comm); 145 return (-1); 146 } 147 148 return (0); 149 } 150 151 /* Copy in-memory pacct_db to pdb_file. */ 152 int 153 pacct_update(void) 154 { 155 return (db_copy_out(pacct_db, pdb_file, "process accounting", 156 NULL)); 157 } 158 159 void 160 pacct_print(void) 161 { 162 BTREEINFO bti; 163 DBT key, data, ndata; 164 DB *output_pacct_db; 165 struct cmdinfo *cip, ci, ci_total, ci_other, ci_junk; 166 int rv; 167 168 bzero(&ci_total, sizeof ci_total); 169 strcpy(ci_total.ci_comm, ""); 170 bzero(&ci_other, sizeof ci_other); 171 strcpy(ci_other.ci_comm, "***other"); 172 bzero(&ci_junk, sizeof ci_junk); 173 strcpy(ci_junk.ci_comm, "**junk**"); 174 175 /* 176 * Retrieve them into new DB, sorted by appropriate key. 177 * At the same time, cull 'other' and 'junk' 178 */ 179 bzero(&bti, sizeof bti); 180 bti.compare = sa_cmp; 181 output_pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti); 182 if (output_pacct_db == NULL) { 183 warn("couldn't sort process accounting stats"); 184 return; 185 } 186 187 ndata.data = NULL; 188 ndata.size = 0; 189 rv = DB_SEQ(pacct_db, &key, &data, R_FIRST); 190 if (rv < 0) 191 warn("retrieving process accounting stats"); 192 while (rv == 0) { 193 cip = (struct cmdinfo *) data.data; 194 bcopy(cip, &ci, sizeof ci); 195 196 /* add to total */ 197 add_ci(&ci, &ci_total); 198 199 if (vflag && ci.ci_calls <= cutoff && 200 (fflag || check_junk(&ci))) { 201 /* put it into **junk** */ 202 add_ci(&ci, &ci_junk); 203 goto next; 204 } 205 if (!aflag && 206 ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) { 207 /* put into ***other */ 208 add_ci(&ci, &ci_other); 209 goto next; 210 } 211 rv = DB_PUT(output_pacct_db, &data, &ndata, 0); 212 if (rv < 0) 213 warn("sorting process accounting stats"); 214 215 next: rv = DB_SEQ(pacct_db, &key, &data, R_NEXT); 216 if (rv < 0) 217 warn("retrieving process accounting stats"); 218 } 219 220 /* insert **junk** and ***other */ 221 if (ci_junk.ci_calls != 0) { 222 data.data = &ci_junk; 223 data.size = sizeof ci_junk; 224 rv = DB_PUT(output_pacct_db, &data, &ndata, 0); 225 if (rv < 0) 226 warn("sorting process accounting stats"); 227 } 228 if (ci_other.ci_calls != 0) { 229 data.data = &ci_other; 230 data.size = sizeof ci_other; 231 rv = DB_PUT(output_pacct_db, &data, &ndata, 0); 232 if (rv < 0) 233 warn("sorting process accounting stats"); 234 } 235 236 /* print out the total */ 237 print_ci(&ci_total, &ci_total); 238 239 /* print out; if reversed, print first (smallest) first */ 240 rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST); 241 if (rv < 0) 242 warn("retrieving process accounting report"); 243 while (rv == 0) { 244 cip = (struct cmdinfo *) data.data; 245 bcopy(cip, &ci, sizeof ci); 246 247 print_ci(&ci, &ci_total); 248 249 rv = DB_SEQ(output_pacct_db, &data, &ndata, 250 rflag ? R_NEXT : R_PREV); 251 if (rv < 0) 252 warn("retrieving process accounting report"); 253 } 254 DB_CLOSE(output_pacct_db); 255 } 256 257 static int 258 check_junk(const struct cmdinfo *cip) 259 { 260 char *cp; 261 size_t len; 262 263 fprintf(stderr, "%s (%ju) -- ", cip->ci_comm, (uintmax_t)cip->ci_calls); 264 cp = fgetln(stdin, &len); 265 266 return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0; 267 } 268 269 static void 270 add_ci(const struct cmdinfo *fromcip, struct cmdinfo *tocip) 271 { 272 tocip->ci_calls += fromcip->ci_calls; 273 tocip->ci_etime += fromcip->ci_etime; 274 tocip->ci_utime += fromcip->ci_utime; 275 tocip->ci_stime += fromcip->ci_stime; 276 tocip->ci_mem += fromcip->ci_mem; 277 tocip->ci_io += fromcip->ci_io; 278 } 279 280 static void 281 print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip) 282 { 283 double t, c; 284 int uflow; 285 286 c = cip->ci_calls ? cip->ci_calls : 1; 287 t = (cip->ci_utime + cip->ci_stime) / 1000000; 288 if (t < 0.01) { 289 t = 0.01; 290 uflow = 1; 291 } else 292 uflow = 0; 293 294 printf("%8ju ", (uintmax_t)cip->ci_calls); 295 if (cflag) { 296 if (cip != totalcip) 297 printf(" %4.1f%% ", cip->ci_calls / 298 (double)totalcip->ci_calls * 100); 299 else 300 printf(" %4s ", ""); 301 } 302 303 if (jflag) 304 printf("%11.3fre ", cip->ci_etime / (1000000 * c)); 305 else 306 printf("%11.3fre ", cip->ci_etime / (60.0 * 1000000)); 307 if (cflag) { 308 if (cip != totalcip) 309 printf(" %4.1f%% ", cip->ci_etime / 310 totalcip->ci_etime * 100); 311 else 312 printf(" %4s ", ""); 313 } 314 315 if (!lflag) { 316 if (jflag) 317 printf("%11.3fcp ", t / (double) cip->ci_calls); 318 else 319 printf("%11.2fcp ", t / 60.0); 320 if (cflag) { 321 if (cip != totalcip) 322 printf(" %4.1f%% ", 323 (cip->ci_utime + cip->ci_stime) / 324 (totalcip->ci_utime + totalcip->ci_stime) * 325 100); 326 else 327 printf(" %4s ", ""); 328 } 329 } else { 330 if (jflag) 331 printf("%11.3fu ", cip->ci_utime / (1000000 * c)); 332 else 333 printf("%11.2fu ", cip->ci_utime / (60.0 * 1000000)); 334 if (cflag) { 335 if (cip != totalcip) 336 printf(" %4.1f%% ", cip->ci_utime / 337 (double)totalcip->ci_utime * 100); 338 else 339 printf(" %4s ", ""); 340 } 341 if (jflag) 342 printf("%11.3fs ", cip->ci_stime / (1000000 * c)); 343 else 344 printf("%11.2fs ", cip->ci_stime / (60.0 * 1000000)); 345 if (cflag) { 346 if (cip != totalcip) 347 printf(" %4.1f%% ", cip->ci_stime / 348 (double)totalcip->ci_stime * 100); 349 else 350 printf(" %4s ", ""); 351 } 352 } 353 354 if (tflag) { 355 if (!uflow) 356 printf("%8.2fre/cp ", 357 cip->ci_etime / 358 (cip->ci_utime + cip->ci_stime)); 359 else 360 printf("*ignore* "); 361 } 362 363 if (Dflag) 364 printf("%10.0fio ", cip->ci_io); 365 else 366 printf("%8.0favio ", cip->ci_io / c); 367 368 if (Kflag) 369 printf("%10.0fk*sec ", cip->ci_mem); 370 else 371 printf("%8.0fk ", cip->ci_mem / t); 372 373 printf(" %s\n", cip->ci_comm); 374 } 375