summaryrefslogtreecommitdiff
path: root/usr.sbin/sa/pdb.c
blob: c8e82aed26b61f3eee6703938a518de048c89431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*	$OpenBSD: pdb.c,v 1.8 2009/10/27 23:59:54 deraadt Exp $	*/
/*
 * Copyright (c) 1994 Christopher G. Demetriou
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Christopher G. Demetriou.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/acct.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include "extern.h"
#include "pathnames.h"

static int check_junk(struct cmdinfo *);
static void add_ci(const struct cmdinfo *, struct cmdinfo *);
static void print_ci(const struct cmdinfo *, const struct cmdinfo *);

static DB	*pacct_db;

int
pacct_init(void)
{
	DB *saved_pacct_db;
	int error;

	pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL);
	if (pacct_db == NULL)
		return (-1);

	error = 0;
	if (!iflag) {
		DBT key, data;
		int serr, nerr;

		saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDONLY, 0, DB_BTREE,
		    NULL);
		if (saved_pacct_db == NULL) {
			error = errno == ENOENT ? 0 : -1;
			if (error)
				warn("retrieving process accounting summary");
			goto out;
		}

		serr = DB_SEQ(saved_pacct_db, &key, &data, R_FIRST);
		if (serr < 0) {
			warn("retrieving process accounting summary");
			error = -1;
			goto closeout;
		}
		while (serr == 0) {
			nerr = DB_PUT(pacct_db, &key, &data, 0);
			if (nerr < 0) {
				warn("initializing process accounting stats");
				error = -1;
				break;
			}

			serr = DB_SEQ(saved_pacct_db, &key, &data, R_NEXT);
			if (serr < 0) {
				warn("retrieving process accounting summary");
				error = -1;
				break;
			}
		}

closeout:	if (DB_CLOSE(saved_pacct_db) < 0) {
			warn("closing process accounting summary");
			error = -1;
		}
	}

out:	if (error != 0)
		pacct_destroy();
	return (error);
}

void
pacct_destroy(void)
{
	if (DB_CLOSE(pacct_db) < 0)
		warn("destroying process accounting stats");
}

int
pacct_add(const struct cmdinfo *ci)
{
	DBT key, data;
	struct cmdinfo newci;
	char keydata[sizeof(ci->ci_comm)];
	int rv;

	memcpy(&keydata, ci->ci_comm, sizeof(keydata));
	key.data = &keydata;
	key.size = strlen(keydata);

	rv = DB_GET(pacct_db, &key, &data, 0);
	if (rv < 0) {
		warn("get key %s from process accounting stats", ci->ci_comm);
		return (-1);
	} else if (rv == 0) {	/* it's there; copy whole thing */
		/* XXX compare size if paranoid */
		/* add the old data to the new data */
		memcpy(&newci, data.data, data.size);
	} else {		/* it's not there; zero it and copy the key */
		memset(&newci, 0, sizeof(newci));
		memcpy(newci.ci_comm, key.data, key.size);
	}

	add_ci(ci, &newci);

	data.data = &newci;
	data.size = sizeof(newci);
	rv = DB_PUT(pacct_db, &key, &data, 0);
	if (rv < 0) {
		warn("add key %s to process accounting stats", ci->ci_comm);
		return (-1);
	} else if (rv == 1) {
		warnx("duplicate key %s in process accounting stats",
		    ci->ci_comm);
		return (-1);
	}

	return (0);
}

int
pacct_update(void)
{
	DB *saved_pacct_db;
	DBT key, data;
	int error, serr, nerr;

	saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
	    DB_BTREE, NULL);
	if (saved_pacct_db == NULL) {
		warn("creating process accounting summary");
		return (-1);
	}

	error = 0;

	serr = DB_SEQ(pacct_db, &key, &data, R_FIRST);
	if (serr < 0) {
		warn("retrieving process accounting stats");
		error = -1;
	}
	while (serr == 0) {
		nerr = DB_PUT(saved_pacct_db, &key, &data, 0);
		if (nerr < 0) {
			warn("saving process accounting summary");
			error = -1;
			break;
		}

		serr = DB_SEQ(pacct_db, &key, &data, R_NEXT);
		if (serr < 0) {
			warn("retrieving process accounting stats");
			error = -1;
			break;
		}
	}

	if (DB_SYNC(saved_pacct_db, 0) < 0) {
		warn("syncing process accounting summary");
		error = -1;
	}
	if (DB_CLOSE(saved_pacct_db) < 0) {
		warn("closing process accounting summary");
		error = -1;
	}
	return error;
}

void
pacct_print(void)
{
	BTREEINFO bti;
	DBT key, data, ndata;
	DB *output_pacct_db;
	struct cmdinfo ci, ci_total, ci_other, ci_junk;
	int rv;

	memset(&ci_total, 0, sizeof(ci_total));
	strlcpy(ci_total.ci_comm, "", sizeof ci_total.ci_comm);
	memset(&ci_other, 0, sizeof(ci_other));
	strlcpy(ci_other.ci_comm, "***other", sizeof ci_other.ci_comm);
	memset(&ci_junk, 0, sizeof(ci_junk));
	strlcpy(ci_junk.ci_comm, "**junk**", sizeof ci_junk.ci_comm);

	/*
	 * Retrieve them into new DB, sorted by appropriate key.
	 * At the same time, cull 'other' and 'junk'
	 */
	memset(&bti, 0, sizeof(bti));
	bti.compare = sa_cmp;
	output_pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
	if (output_pacct_db == NULL) {
		warn("couldn't sort process accounting stats");
		return;
	}

	ndata.data = NULL;
	ndata.size = 0;
	rv = DB_SEQ(pacct_db, &key, &data, R_FIRST);
	if (rv < 0)
		warn("retrieving process accounting stats");
	while (rv == 0) {
		memcpy(&ci, data.data, sizeof(ci));

		/* add to total */
		add_ci(&ci, &ci_total);

		if (vflag && ci.ci_calls <= cutoff &&
		    (fflag || check_junk(&ci))) {
			/* put it into **junk** */
			add_ci(&ci, &ci_junk);
			goto next;
		}
		if (!aflag &&
		    ((ci.ci_flags & CI_UNPRINTABLE) != 0 || ci.ci_calls <= 1)) {
			/* put into ***other */
			add_ci(&ci, &ci_other);
			goto next;
		}
		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
		if (rv < 0)
			warn("sorting process accounting stats");

next:		rv = DB_SEQ(pacct_db, &key, &data, R_NEXT);
		if (rv < 0)
			warn("retrieving process accounting stats");
	}

	/* insert **junk** and ***other */
	if (ci_junk.ci_calls != 0) {
		data.data = &ci_junk;
		data.size = sizeof(ci_junk);
		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
		if (rv < 0)
			warn("sorting process accounting stats");
	}
	if (ci_other.ci_calls != 0) {
		data.data = &ci_other;
		data.size = sizeof(ci_other);
		rv = DB_PUT(output_pacct_db, &data, &ndata, 0);
		if (rv < 0)
			warn("sorting process accounting stats");
	}

	/* print out the total */
	print_ci(&ci_total, &ci_total);

	/* print out; if reversed, print first (smallest) first */
	rv = DB_SEQ(output_pacct_db, &data, &ndata, rflag ? R_FIRST : R_LAST);
	if (rv < 0)
		warn("retrieving process accounting report");
	while (rv == 0) {
		memcpy(&ci, data.data, sizeof(ci));

		print_ci(&ci, &ci_total);

		rv = DB_SEQ(output_pacct_db, &data, &ndata,
		    rflag ? R_NEXT : R_PREV);
		if (rv < 0)
			warn("retrieving process accounting report");
	}
	DB_CLOSE(output_pacct_db);
}

static int
check_junk(struct cmdinfo *cip)
{
	char *cp;
	size_t len;

	fprintf(stderr, "%s (%qu) -- ", cip->ci_comm, cip->ci_calls);
	cp = fgetln(stdin, &len);

	return (cp && (cp[0] == 'y' || cp[0] == 'Y')) ? 1 : 0;
}

static void
add_ci(const struct cmdinfo *fromcip, struct cmdinfo *tocip)
{
	tocip->ci_calls += fromcip->ci_calls;
	tocip->ci_etime += fromcip->ci_etime;
	tocip->ci_utime += fromcip->ci_utime;
	tocip->ci_stime += fromcip->ci_stime;
	tocip->ci_mem += fromcip->ci_mem;
	tocip->ci_io += fromcip->ci_io;
}

static void
print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip)
{
	double t, c;
	int uflow;

	c = cip->ci_calls ? cip->ci_calls : 1;
	t = (cip->ci_utime + cip->ci_stime) / (double) AHZ;
	if (t < 0.01) {
		t = 0.01;
		uflow = 1;
	} else
		uflow = 0;

	printf("%8qu ", cip->ci_calls);
	if (cflag) {
		if (cip != totalcip)
			printf(" %4.2f%%  ",
			    cip->ci_calls / (double) totalcip->ci_calls);
		else
			printf(" %4s   ", "");
	}

	if (jflag)
		printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c));
	else
		printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ));
	if (cflag) {
		if (cip != totalcip)
			printf(" %4.2f%%  ",
			    cip->ci_etime / (double) totalcip->ci_etime);
		else
			printf(" %4s   ", "");
	}

	if (!lflag) {
		if (jflag)
			printf("%11.2fcp ", t / (double) cip->ci_calls);
		else
			printf("%11.2fcp ", t / 60.0);
		if (cflag) {
			if (cip != totalcip)
				printf(" %4.2f%%  ",
				    (cip->ci_utime + cip->ci_stime) / (double)
				    (totalcip->ci_utime + totalcip->ci_stime));
			else
				printf(" %4s   ", "");
		}
	} else {
		if (jflag)
			printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c));
		else
			printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ));
		if (cflag) {
			if (cip != totalcip)
				printf(" %4.2f%%  ", cip->ci_utime / (double) totalcip->ci_utime);
			else
				printf(" %4s   ", "");
		}
		if (jflag)
			printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c));
		else
			printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ));
		if (cflag) {
			if (cip != totalcip)
				printf(" %4.2f%%  ", cip->ci_stime / (double) totalcip->ci_stime);
			else
				printf(" %4s   ", "");
		}
	}

	if (tflag) {
		if (!uflow)
			printf("%8.2fre/cp ", cip->ci_etime / (double) (cip->ci_utime + cip->ci_stime));
		else
			printf("%8s ", "*ignore*");
	}

	if (Dflag)
		printf("%10qutio ", cip->ci_io);
	else
		printf("%8.0favio ", cip->ci_io / c);

	if (Kflag)
		printf("%10quk*sec ", cip->ci_mem);
	else
		printf("%8.0fk ", cip->ci_mem / t);

	printf("  %s\n", cip->ci_comm);
}