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: src/usr.sbin/sa/pdb.c,v 1.14 2007/05/22 06:51:38 dds Exp $");
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
46static int check_junk(const struct cmdinfo *);
47static void add_ci(const struct cmdinfo *, struct cmdinfo *);
48static void print_ci(const struct cmdinfo *, const struct cmdinfo *);
49
50static DB	*pacct_db;
51
52/* Legacy format in AHZV1 units. */
53struct 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 */
69static int
70v1_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. */
96int
97pacct_init()
98{
99	return (db_copy_in(&pacct_db, pdb_file, "process accounting",
100	    NULL, v1_to_v2));
101}
102
103void
104pacct_destroy()
105{
106	db_destroy(pacct_db, "process accounting");
107}
108
109int
110pacct_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. */
152int
153pacct_update()
154{
155	return (db_copy_out(pacct_db, pdb_file, "process accounting",
156	    NULL));
157}
158
159void
160pacct_print()
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
215next:		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
257static int
258check_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
269static void
270add_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#ifdef __APPLE__
281static void
282print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip)
283{
284	double t, c;
285	int uflow;
286
287	c = cip->ci_calls ? cip->ci_calls : 1;
288	t = (cip->ci_utime + cip->ci_stime) / (double) AHZ;
289	if (t < 0.01) {
290		t = 0.01;
291		uflow = 1;
292	} else
293		uflow = 0;
294
295	printf("%8ju ", (uintmax_t)cip->ci_calls);
296	if (cflag) {
297		if (cip != totalcip)
298			printf(" %4.2f%%  ",
299			    cip->ci_calls / (double) totalcip->ci_calls);
300		else
301			printf(" %4s   ", "");
302	}
303
304	if (jflag)
305		printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c));
306	else
307		printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ));
308	if (cflag) {
309		if (cip != totalcip)
310			printf(" %4.2f%%  ",
311			    cip->ci_etime / (double) totalcip->ci_etime);
312		else
313			printf(" %4s   ", "");
314	}
315
316	if (!lflag) {
317		if (jflag)
318			printf("%11.2fcp ", t / (double) cip->ci_calls);
319		else
320			printf("%11.2fcp ", t / 60.0);
321		if (cflag) {
322			if (cip != totalcip)
323				printf(" %4.2f%%  ",
324				    (cip->ci_utime + cip->ci_stime) / (double)
325				    (totalcip->ci_utime + totalcip->ci_stime));
326			else
327				printf(" %4s   ", "");
328		}
329	} else {
330		if (jflag)
331			printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c));
332		else
333			printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ));
334		if (cflag) {
335			if (cip != totalcip)
336				printf(" %4.2f%%  ", cip->ci_utime / (double) totalcip->ci_utime);
337			else
338				printf(" %4s   ", "");
339		}
340		if (jflag)
341			printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c));
342		else
343			printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ));
344		if (cflag) {
345			if (cip != totalcip)
346				printf(" %4.2f%%  ", cip->ci_stime / (double) totalcip->ci_stime);
347			else
348				printf(" %4s   ", "");
349		}
350	}
351
352	if (tflag) {
353		if (!uflow)
354			printf("%8.2fre/cp ",
355			    cip->ci_etime /
356			    (double) (cip->ci_utime + cip->ci_stime));
357		else
358			printf("*ignore*      ");
359	}
360
361	if (Dflag)
362		printf("%10jutio ", (uintmax_t)cip->ci_io);
363	else
364		printf("%8.0favio ", cip->ci_io / c);
365
366	if (Kflag)
367		printf("%10juk*sec ", (uintmax_t)cip->ci_mem);
368	else
369		printf("%8.0fk ", cip->ci_mem / t);
370
371	printf("  %s\n", cip->ci_comm);
372}
373#else
374static void
375print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip)
376{
377	double t, c;
378	int uflow;
379
380	c = cip->ci_calls ? cip->ci_calls : 1;
381	t = (cip->ci_utime + cip->ci_stime) / 1000000;
382	if (t < 0.01) {
383		t = 0.01;
384		uflow = 1;
385	} else
386		uflow = 0;
387
388	printf("%8ju ", (uintmax_t)cip->ci_calls);
389	if (cflag) {
390		if (cip != totalcip)
391			printf(" %4.1f%%  ", cip->ci_calls /
392			    (double)totalcip->ci_calls * 100);
393		else
394			printf(" %4s   ", "");
395	}
396
397	if (jflag)
398		printf("%11.3fre ", cip->ci_etime / (1000000 * c));
399	else
400		printf("%11.3fre ", cip->ci_etime / (60.0 * 1000000));
401	if (cflag) {
402		if (cip != totalcip)
403			printf(" %4.1f%%  ", cip->ci_etime /
404			    totalcip->ci_etime * 100);
405		else
406			printf(" %4s   ", "");
407	}
408
409	if (!lflag) {
410		if (jflag)
411			printf("%11.3fcp ", t / (double) cip->ci_calls);
412		else
413			printf("%11.2fcp ", t / 60.0);
414		if (cflag) {
415			if (cip != totalcip)
416				printf(" %4.1f%%  ",
417				    (cip->ci_utime + cip->ci_stime) /
418				    (totalcip->ci_utime + totalcip->ci_stime) *
419				    100);
420			else
421				printf(" %4s   ", "");
422		}
423	} else {
424		if (jflag)
425			printf("%11.3fu ", cip->ci_utime / (1000000 * c));
426		else
427			printf("%11.2fu ", cip->ci_utime / (60.0 * 1000000));
428		if (cflag) {
429			if (cip != totalcip)
430				printf(" %4.1f%%  ", cip->ci_utime /
431				    (double)totalcip->ci_utime * 100);
432			else
433				printf(" %4s   ", "");
434		}
435		if (jflag)
436			printf("%11.3fs ", cip->ci_stime / (1000000 * c));
437		else
438			printf("%11.2fs ", cip->ci_stime / (60.0 * 1000000));
439		if (cflag) {
440			if (cip != totalcip)
441				printf(" %4.1f%%  ", cip->ci_stime /
442				    (double)totalcip->ci_stime * 100);
443			else
444				printf(" %4s   ", "");
445		}
446	}
447
448	if (tflag) {
449		if (!uflow)
450			printf("%8.2fre/cp ",
451			    cip->ci_etime /
452			    (cip->ci_utime + cip->ci_stime));
453		else
454			printf("*ignore*      ");
455	}
456
457	if (Dflag)
458		printf("%10.0fio ", cip->ci_io);
459	else
460		printf("%8.0favio ", cip->ci_io / c);
461
462	if (Kflag)
463		printf("%10.0fk*sec ", cip->ci_mem);
464	else
465		printf("%8.0fk ", cip->ci_mem / t);
466
467	printf("  %s\n", cip->ci_comm);
468}
469#endif