aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/pstat/pstat.c
blob: 51828cbca7057c4f67672177e354b5a3047da2e4 (plain) (blame)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*-
 * Copyright (c) 1980, 1991, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 2002 Networks Associates Technologies, Inc.
 * All rights reserved.
 *
 * Portions of this software were developed for the FreeBSD Project by
 * ThinkSec AS and NAI Labs, the Security Research Division of Network
 * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
 * ("CBOSS"), as part of the DARPA CHATS research program.
 *
 * 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.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 */

#if 0
#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
static char sccsid[] = "@(#)pstat.c	8.16 (Berkeley) 5/9/95";
#endif /* not lint */
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/time.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/stdint.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/blist.h>

#include <sys/sysctl.h>
#include <vm/vm_param.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <kvm.h>
#include <libutil.h>
#include <limits.h>
#include <nlist.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

enum {
	NL_CONSTTY,
	NL_MAXFILES,
	NL_NFILES,
	NL_TTY_LIST
};

static struct nlist nl[] = {
	{ .n_name = "_constty" },
	{ .n_name = "_maxfiles" },
	{ .n_name = "_openfiles" },
	{ .n_name = "_tty_list" },
	{ .n_name = "" }
};

static int	humanflag;
static int	usenumflag;
static int	totalflag;
static int	swapflag;
static char	*nlistf;
static char	*memf;
static kvm_t	*kd;

static char	*usagestr;

static void	filemode(void);
static int	getfiles(char **, size_t *);
static void	swapmode(void);
static void	ttymode(void);
static void	ttyprt(struct xtty *);
static void	usage(void);

int
main(int argc, char *argv[])
{
	int ch, i, quit, ret;
	int fileflag, ttyflag;
	char buf[_POSIX2_LINE_MAX],*opts;

	fileflag = swapflag = ttyflag = 0;

	/* We will behave like good old swapinfo if thus invoked */
	opts = strrchr(argv[0], '/');
	if (opts)
		opts++;
	else
		opts = argv[0];
	if (!strcmp(opts, "swapinfo")) {
		swapflag = 1;
		opts = "ghkmM:N:";
		usagestr = "swapinfo [-ghkm] [-M core [-N system]]";
	} else {
		opts = "TM:N:fghkmnst";
		usagestr = "pstat [-Tfghkmnst] [-M core [-N system]]";
	}

	while ((ch = getopt(argc, argv, opts)) != -1)
		switch (ch) {
		case 'f':
			fileflag = 1;
			break;
		case 'g':
			setenv("BLOCKSIZE", "1G", 1);
			break;
		case 'h':
			humanflag = 1;
			break;
		case 'k':
			setenv("BLOCKSIZE", "1K", 1);
			break;
		case 'm':
			setenv("BLOCKSIZE", "1M", 1);
			break;
		case 'M':
			memf = optarg;
			break;
		case 'N':
			nlistf = optarg;
			break;
		case 'n':
			usenumflag = 1;
			break;
		case 's':
			++swapflag;
			break;
		case 'T':
			totalflag = 1;
			break;
		case 't':
			ttyflag = 1;
			break;
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (memf != NULL) {
		kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
		if (kd == NULL)
			errx(1, "kvm_openfiles: %s", buf);
		if ((ret = kvm_nlist(kd, nl)) != 0) {
			if (ret == -1)
				errx(1, "kvm_nlist: %s", kvm_geterr(kd));
			quit = 0;
			for (i = 0; nl[i].n_name[0] != '\0'; ++i)
				if (nl[i].n_value == 0) {
					quit = 1;
					warnx("undefined symbol: %s",
					    nl[i].n_name);
				}
			if (quit)
				exit(1);
		}
	}
	if (!(fileflag | ttyflag | swapflag | totalflag))
		usage();
	if (fileflag || totalflag)
		filemode();
	if (ttyflag)
		ttymode();
	if (swapflag || totalflag)
		swapmode();
	exit (0);
}

static void
usage(void)
{
	fprintf(stderr, "usage: %s\n", usagestr);
	exit (1);
}

static const char fhdr32[] =
  "   LOC   TYPE   FLG  CNT MSG   DATA        OFFSET\n";
/* c0000000 ------ RWAI 123 123 c0000000 1000000000000000 */

static const char fhdr64[] =
  "       LOC       TYPE   FLG  CNT MSG       DATA            OFFSET\n";
/* c000000000000000 ------ RWAI 123 123 c000000000000000 1000000000000000 */

static const char hdr[] =
"      LINE   INQ  CAN  LIN  LOW  OUTQ  USE  LOW   COL  SESS  PGID STATE\n";

static void
ttymode_kvm(void)
{
	TAILQ_HEAD(, tty) tl;
	struct tty *tp, tty;
	struct xtty xt;

	(void)printf("%s", hdr);
	bzero(&xt, sizeof xt);
	xt.xt_size = sizeof xt;
	if (kvm_read(kd, nl[NL_TTY_LIST].n_value, &tl, sizeof tl) != sizeof tl)
		errx(1, "kvm_read(): %s", kvm_geterr(kd));
	tp = TAILQ_FIRST(&tl);
	while (tp != NULL) {
		if (kvm_read(kd, (u_long)tp, &tty, sizeof tty) != sizeof tty)
			errx(1, "kvm_read(): %s", kvm_geterr(kd));
		xt.xt_insize = tty.t_inq.ti_nblocks * TTYINQ_DATASIZE;
		xt.xt_incc = tty.t_inq.ti_linestart - tty.t_inq.ti_begin;
		xt.xt_inlc = tty.t_inq.ti_end - tty.t_inq.ti_linestart;
		xt.xt_inlow = tty.t_inlow;
		xt.xt_outsize = tty.t_outq.to_nblocks * TTYOUTQ_DATASIZE;
		xt.xt_outcc = tty.t_outq.to_end - tty.t_outq.to_begin;
		xt.xt_outlow = tty.t_outlow;
		xt.xt_column = tty.t_column;
		/* xt.xt_pgid = ... */
		/* xt.xt_sid = ... */
		xt.xt_flags = tty.t_flags;
		xt.xt_dev = NODEV;
		ttyprt(&xt);
		tp = TAILQ_NEXT(&tty, t_list);
	}
}

static void
ttymode_sysctl(void)
{
	struct xtty *xt, *end;
	void *xttys;
	size_t len;

	(void)printf("%s", hdr);
	if ((xttys = malloc(len = sizeof *xt)) == NULL)
		err(1, "malloc()");
	while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) {
		if (errno != ENOMEM)
			err(1, "sysctlbyname()");
		len *= 2;
		if ((xttys = realloc(xttys, len)) == NULL)
			err(1, "realloc()");
	}
	if (len > 0) {
		end = (struct xtty *)((char *)xttys + len);
		for (xt = xttys; xt < end; xt++)
			ttyprt(xt);
	}
}

static void
ttymode(void)
{

	if (kd != NULL)
		ttymode_kvm();
	else
		ttymode_sysctl();
}

static struct {
	int flag;
	char val;
} ttystates[] = {
#if 0
	{ TF_NOPREFIX,	'N' },
#endif
	{ TF_INITLOCK,	'I' },
	{ TF_CALLOUT,	'C' },

	/* Keep these together -> 'Oi' and 'Oo'. */
	{ TF_OPENED,	'O' },
	{ TF_OPENED_IN,	'i' },
	{ TF_OPENED_OUT,'o' },

	{ TF_GONE,	'G' },
	{ TF_OPENCLOSE,	'B' },
	{ TF_ASYNC,	'Y' },
	{ TF_LITERAL,	'L' },

	/* Keep these together -> 'Hi' and 'Ho'. */
	{ TF_HIWAT,	'H' },
	{ TF_HIWAT_IN,	'i' },
	{ TF_HIWAT_OUT,	'o' },

	{ TF_STOPPED,	'S' },
	{ TF_EXCLUDE,	'X' },
	{ TF_BYPASS,	'l' },
	{ TF_ZOMBIE,	'Z' },

	{ 0,	       '\0' },
};

static void
ttyprt(struct xtty *xt)
{
	int i, j;
	char *name;

	if (xt->xt_size != sizeof *xt)
		errx(1, "struct xtty size mismatch");
	if (usenumflag || xt->xt_dev == 0 ||
	   (name = devname(xt->xt_dev, S_IFCHR)) == NULL)
		printf("%5d,%4d ", major(xt->xt_dev), minor(xt->xt_dev));
	else
		printf("%10s ", name);
	printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ",
	    xt->xt_insize, xt->xt_incc, xt->xt_inlc,
	    (xt->xt_insize - xt->xt_inlow), xt->xt_outsize,
	    xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow),
	    xt->xt_column, xt->xt_sid, xt->xt_pgid);
	for (i = j = 0; ttystates[i].flag; i++)
		if (xt->xt_flags & ttystates[i].flag) {
			putchar(ttystates[i].val);
			j++;
		}
	if (j == 0)
		putchar('-');
	putchar('\n');
}

static void
filemode(void)
{
	struct xfile *fp;
	char *buf, flagbuf[16], *fbp;
	int maxf, openf;
	size_t len;
	static char *dtypes[] = { "???", "inode", "socket", "pipe",
	    "fifo", "kqueue", "crypto" };
	int i;
	int wid;

	if (kd != NULL) {
		if (kvm_read(kd, nl[NL_MAXFILES].n_value,
			&maxf, sizeof maxf) != sizeof maxf ||
		    kvm_read(kd, nl[NL_NFILES].n_value,
			&openf, sizeof openf) != sizeof openf)
			errx(1, "kvm_read(): %s", kvm_geterr(kd));
	} else {
		len = sizeof(int);
		if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 ||
		    sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1)
			err(1, "sysctlbyname()");
	}

	if (totalflag) {
		(void)printf("%3d/%3d files\n", openf, maxf);
		return;
	}
	if (getfiles(&buf, &len) == -1)
		return;
	openf = len / sizeof *fp;

	(void)printf("%d/%d open files\n", openf, maxf);
	printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64);
	wid = (int)sizeof(uintptr_t) * 2;
	for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) {
		if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0]))
			continue;
		(void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file);
		(void)printf(" %-6.6s", dtypes[fp->xf_type]);
		fbp = flagbuf;
		if (fp->xf_flag & FREAD)
			*fbp++ = 'R';
		if (fp->xf_flag & FWRITE)
			*fbp++ = 'W';
		if (fp->xf_flag & FAPPEND)
			*fbp++ = 'A';
		if (fp->xf_flag & FASYNC)
			*fbp++ = 'I';
		*fbp = '\0';
		(void)printf(" %4s %3d", flagbuf, fp->xf_count);
		(void)printf(" %3d", fp->xf_msgcount);
		(void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data);
		(void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2,
		    (uintmax_t)fp->xf_offset);
	}
	free(buf);
}

static int
getfiles(char **abuf, size_t *alen)
{
	size_t len;
	int mib[2];
	char *buf;

	/*
	 * XXX
	 * Add emulation of KINFO_FILE here.
	 */
	if (kd != NULL)
		errx(1, "files on dead kernel, not implemented");

	mib[0] = CTL_KERN;
	mib[1] = KERN_FILE;
	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
		warn("sysctl: KERN_FILE");
		return (-1);
	}
	if ((buf = malloc(len)) == NULL)
		errx(1, "malloc");
	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
		warn("sysctl: KERN_FILE");
		return (-1);
	}
	*abuf = buf;
	*alen = len;
	return (0);
}

/*
 * swapmode is based on a program called swapinfo written
 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
 */

#define CONVERT(v)	((int64_t)(v) * pagesize / blocksize)
static struct kvm_swap swtot;
static int nswdev;

static void
print_swap_header(void)
{
	int hlen;
	long blocksize;
	const char *header;

	header = getbsize(&hlen, &blocksize);
	if (totalflag == 0)
		(void)printf("%-15s %*s %8s %8s %8s\n",
		    "Device", hlen, header,
		    "Used", "Avail", "Capacity");
}

static void
print_swap_line(const char *devname, intmax_t nblks, intmax_t bused,
    intmax_t bavail, float bpercent)
{
	char usedbuf[5];
	char availbuf[5];
	int hlen, pagesize;
	long blocksize;

	pagesize = getpagesize();
	getbsize(&hlen, &blocksize);

	printf("%-15s %*jd ", devname, hlen, CONVERT(nblks));
	if (humanflag) {
		humanize_number(usedbuf, sizeof(usedbuf),
		    CONVERT(blocksize * bused), "",
		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
		humanize_number(availbuf, sizeof(availbuf),
		    CONVERT(blocksize * bavail), "",
		    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
		printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent);
	} else {
		printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused),
		    (intmax_t)CONVERT(bavail), bpercent);
	}
}

static void
print_swap(struct kvm_swap *ksw)
{

	swtot.ksw_total += ksw->ksw_total;
	swtot.ksw_used += ksw->ksw_used;
	++nswdev;
	if (totalflag == 0)
		print_swap_line(ksw->ksw_devname, ksw->ksw_total,
		    ksw->ksw_used, ksw->ksw_total - ksw->ksw_used,
		    (ksw->ksw_used * 100.0) / ksw->ksw_total);
}

static void
print_swap_total(void)
{
	int hlen, pagesize;
	long blocksize;

	pagesize = getpagesize();
	getbsize(&hlen, &blocksize);
	if (totalflag) {
		blocksize = 1024 * 1024;
		(void)printf("%jdM/%jdM swap space\n",
		    CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total));
	} else if (nswdev > 1) {
		print_swap_line("Total", swtot.ksw_total, swtot.ksw_used,
		    swtot.ksw_total - swtot.ksw_used,
		    (swtot.ksw_used * 100.0) / swtot.ksw_total);
	}
}

static void
swapmode_kvm(void)
{
	struct kvm_swap kswap[16];
	int i, n;

	n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0],
	    SWIF_DEV_PREFIX);

	print_swap_header();
	for (i = 0; i < n; ++i)
		print_swap(&kswap[i]);
	print_swap_total();
}

static void
swapmode_sysctl(void)
{
	struct kvm_swap ksw;
	struct xswdev xsw;
	size_t mibsize, size;
	int mib[16], n;

	print_swap_header();
	mibsize = sizeof mib / sizeof mib[0];
	if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
		err(1, "sysctlnametomib()");
	for (n = 0; ; ++n) {
		mib[mibsize] = n;
		size = sizeof xsw;
		if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
			break;
		if (xsw.xsw_version != XSWDEV_VERSION)
			errx(1, "xswdev version mismatch");
		if (xsw.xsw_dev == NODEV)
			snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname,
			    "<NFSfile>");
		else
			snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname,
			    "/dev/%s", devname(xsw.xsw_dev, S_IFCHR));
		ksw.ksw_used = xsw.xsw_used;
		ksw.ksw_total = xsw.xsw_nblks;
		ksw.ksw_flags = xsw.xsw_flags;
		print_swap(&ksw);
	}
	if (errno != ENOENT)
		err(1, "sysctl()");
	print_swap_total();
}

static void
swapmode(void)
{
	if (kd != NULL)
		swapmode_kvm();
	else
		swapmode_sysctl();
}