2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technologies, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char copyright
[] =
40 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid
[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
55 #include <sys/stdint.h>
56 #include <sys/ioctl.h>
58 #include <sys/blist.h>
60 #include <sys/sysctl.h>
61 #include <vm/vm_param.h>
87 { NL_CONSTTY
, "_constty" },
88 { NL_MAXFILES
, "_maxfiles" },
89 { NL_NFILES
, "_openfiles" },
90 { NL_TTY_LIST
, "_tty_list" },
93 #define NNAMES (sizeof(namelist) / sizeof(*namelist))
94 static struct nlist nl
[NNAMES
];
97 static int usenumflag
;
104 static const char *usagestr
;
106 static void filemode(void);
107 static int getfiles(struct xfile
**, size_t *);
108 static void swapmode(void);
109 static void ttymode(void);
110 static void ttyprt(struct xtty
*);
111 static void usage(void);
114 main(int argc
, char *argv
[])
117 int fileflag
, ttyflag
;
119 char buf
[_POSIX2_LINE_MAX
];
122 fileflag
= swapflag
= ttyflag
= 0;
124 /* We will behave like good old swapinfo if thus invoked */
125 opts
= strrchr(argv
[0], '/');
130 if (!strcmp(opts
, "swapinfo")) {
133 usagestr
= "swapinfo [-ghkm] [-M core [-N system]]";
135 opts
= "TM:N:fghkmnst";
136 usagestr
= "pstat [-Tfghkmnst] [-M core [-N system]]";
139 while ((ch
= getopt(argc
, argv
, opts
)) != -1)
145 setenv("BLOCKSIZE", "1G", 1);
151 setenv("BLOCKSIZE", "1K", 1);
154 setenv("BLOCKSIZE", "1M", 1);
181 * Initialize symbol names list.
183 for (i
= 0; i
< NNAMES
; i
++)
184 nl
[namelist
[i
].order
].n_name
= strdup(namelist
[i
].name
);
187 kd
= kvm_openfiles(nlistf
, memf
, NULL
, O_RDONLY
, buf
);
189 errx(1, "kvm_openfiles: %s", buf
);
190 if ((ret
= kvm_nlist(kd
, nl
)) != 0) {
192 errx(1, "kvm_nlist: %s", kvm_geterr(kd
));
194 for (i
= 0; nl
[i
].n_name
[0] != '\0'; ++i
)
195 if (nl
[i
].n_value
== 0) {
197 warnx("undefined symbol: %s",
204 if (!(fileflag
| ttyflag
| swapflag
| totalflag
))
206 if (fileflag
|| totalflag
)
210 if (swapflag
|| totalflag
)
218 fprintf(stderr
, "usage: %s\n", usagestr
);
222 static const char fhdr32
[] =
223 " LOC TYPE FLG CNT MSG DATA OFFSET\n";
224 /* c0000000 ------ RWAI 123 123 c0000000 1000000000000000 */
226 static const char fhdr64
[] =
227 " LOC TYPE FLG CNT MSG DATA OFFSET\n";
228 /* c000000000000000 ------ RWAI 123 123 c000000000000000 1000000000000000 */
230 static const char hdr
[] =
231 " LINE INQ CAN LIN LOW OUTQ USE LOW COL SESS PGID STATE\n";
236 TAILQ_HEAD(, tty
) tl
;
240 (void)printf("%s", hdr
);
241 bzero(&xt
, sizeof xt
);
242 xt
.xt_size
= sizeof xt
;
243 if (kvm_read(kd
, nl
[NL_TTY_LIST
].n_value
, &tl
, sizeof tl
) != sizeof tl
)
244 errx(1, "kvm_read(): %s", kvm_geterr(kd
));
245 tp
= TAILQ_FIRST(&tl
);
247 if (kvm_read(kd
, (u_long
)tp
, &tty
, sizeof tty
) != sizeof tty
)
248 errx(1, "kvm_read(): %s", kvm_geterr(kd
));
249 xt
.xt_insize
= tty
.t_inq
.ti_nblocks
* TTYINQ_DATASIZE
;
250 xt
.xt_incc
= tty
.t_inq
.ti_linestart
- tty
.t_inq
.ti_begin
;
251 xt
.xt_inlc
= tty
.t_inq
.ti_end
- tty
.t_inq
.ti_linestart
;
252 xt
.xt_inlow
= tty
.t_inlow
;
253 xt
.xt_outsize
= tty
.t_outq
.to_nblocks
* TTYOUTQ_DATASIZE
;
254 xt
.xt_outcc
= tty
.t_outq
.to_end
- tty
.t_outq
.to_begin
;
255 xt
.xt_outlow
= tty
.t_outlow
;
256 xt
.xt_column
= tty
.t_column
;
257 /* xt.xt_pgid = ... */
258 /* xt.xt_sid = ... */
259 xt
.xt_flags
= tty
.t_flags
;
262 tp
= TAILQ_NEXT(&tty
, t_list
);
273 (void)printf("%s", hdr
);
274 if ((xttys
= malloc(len
= sizeof(*xttys
))) == NULL
)
276 while (sysctlbyname("kern.ttys", xttys
, &len
, 0, 0) == -1) {
278 err(1, "sysctlbyname()");
280 if ((xttys
= realloc(xttys
, len
)) == NULL
)
283 n
= len
/ sizeof(*xttys
);
284 for (i
= 0; i
< n
; i
++)
303 { TF_NOPREFIX
, 'N' },
305 { TF_INITLOCK
, 'I' },
308 /* Keep these together -> 'Oi' and 'Oo'. */
310 { TF_OPENED_IN
, 'i' },
311 { TF_OPENED_OUT
, 'o' },
312 { TF_OPENED_CONS
, 'c' },
315 { TF_OPENCLOSE
, 'B' },
319 /* Keep these together -> 'Hi' and 'Ho'. */
321 { TF_HIWAT_IN
, 'i' },
322 { TF_HIWAT_OUT
, 'o' },
330 /* Keep these together -> 'bi' and 'bo'. */
333 { TF_BUSY_OUT
, 'o' },
339 ttyprt(struct xtty
*xt
)
344 if (xt
->xt_size
!= sizeof *xt
)
345 errx(1, "struct xtty size mismatch");
346 if (usenumflag
|| xt
->xt_dev
== 0 ||
347 (name
= devname(xt
->xt_dev
, S_IFCHR
)) == NULL
)
348 printf("%#10jx ", (uintmax_t)xt
->xt_dev
);
350 printf("%10s ", name
);
351 printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ",
352 xt
->xt_insize
, xt
->xt_incc
, xt
->xt_inlc
,
353 (xt
->xt_insize
- xt
->xt_inlow
), xt
->xt_outsize
,
354 xt
->xt_outcc
, (xt
->xt_outsize
- xt
->xt_outlow
),
355 MIN(xt
->xt_column
, 99999), xt
->xt_sid
, xt
->xt_pgid
);
356 for (i
= j
= 0; ttystates
[i
].flag
; i
++)
357 if (xt
->xt_flags
& ttystates
[i
].flag
) {
358 putchar(ttystates
[i
].val
);
369 struct xfile
*fp
, *buf
;
370 char flagbuf
[16], *fbp
;
373 static char const * const dtypes
[] = { "???", "inode", "socket",
374 "pipe", "fifo", "kqueue", "crypto" };
379 if (kvm_read(kd
, nl
[NL_MAXFILES
].n_value
,
380 &maxf
, sizeof maxf
) != sizeof maxf
||
381 kvm_read(kd
, nl
[NL_NFILES
].n_value
,
382 &openf
, sizeof openf
) != sizeof openf
)
383 errx(1, "kvm_read(): %s", kvm_geterr(kd
));
386 if (sysctlbyname("kern.maxfiles", &maxf
, &len
, 0, 0) == -1 ||
387 sysctlbyname("kern.openfiles", &openf
, &len
, 0, 0) == -1)
388 err(1, "sysctlbyname()");
392 (void)printf("%3d/%3d files\n", openf
, maxf
);
395 if (getfiles(&buf
, &len
) == -1)
397 openf
= len
/ sizeof *fp
;
399 (void)printf("%d/%d open files\n", openf
, maxf
);
400 printf(sizeof(uintptr_t) == 4 ? fhdr32
: fhdr64
);
401 wid
= (int)sizeof(uintptr_t) * 2;
402 for (fp
= (struct xfile
*)buf
, i
= 0; i
< openf
; ++fp
, ++i
) {
403 if ((size_t)fp
->xf_type
>= sizeof(dtypes
) / sizeof(dtypes
[0]))
405 (void)printf("%*jx", wid
, (uintmax_t)(uintptr_t)fp
->xf_file
);
406 (void)printf(" %-6.6s", dtypes
[fp
->xf_type
]);
408 if (fp
->xf_flag
& FREAD
)
410 if (fp
->xf_flag
& FWRITE
)
412 if (fp
->xf_flag
& FAPPEND
)
414 if (fp
->xf_flag
& FASYNC
)
417 (void)printf(" %4s %3d", flagbuf
, fp
->xf_count
);
418 (void)printf(" %3d", fp
->xf_msgcount
);
419 (void)printf(" %*jx", wid
, (uintmax_t)(uintptr_t)fp
->xf_data
);
420 (void)printf(" %*jx\n", (int)sizeof(fp
->xf_offset
) * 2,
421 (uintmax_t)fp
->xf_offset
);
427 getfiles(struct xfile
**abuf
, size_t *alen
)
435 * Add emulation of KINFO_FILE here.
438 errx(1, "files on dead kernel, not implemented");
442 if (sysctl(mib
, 2, NULL
, &len
, NULL
, 0) == -1) {
443 warn("sysctl: KERN_FILE");
446 if ((buf
= malloc(len
)) == NULL
)
448 if (sysctl(mib
, 2, buf
, &len
, NULL
, 0) == -1) {
449 warn("sysctl: KERN_FILE");
458 * swapmode is based on a program called swapinfo written
459 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
462 #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize)
463 #define CONVERT_BLOCKS(v) ((int64_t)(v) * pagesize)
464 static struct kvm_swap swtot
;
468 print_swap_header(void)
474 header
= getbsize(&hlen
, &blocksize
);
476 (void)printf("%-15s %*s %8s %8s %8s\n",
477 "Device", hlen
, header
,
478 "Used", "Avail", "Capacity");
482 print_swap_line(const char *swdevname
, intmax_t nblks
, intmax_t bused
,
483 intmax_t bavail
, float bpercent
)
490 pagesize
= getpagesize();
491 getbsize(&hlen
, &blocksize
);
493 printf("%-15s %*jd ", swdevname
, hlen
, CONVERT(nblks
));
495 humanize_number(usedbuf
, sizeof(usedbuf
),
496 CONVERT_BLOCKS(bused
), "",
497 HN_AUTOSCALE
, HN_B
| HN_NOSPACE
| HN_DECIMAL
);
498 humanize_number(availbuf
, sizeof(availbuf
),
499 CONVERT_BLOCKS(bavail
), "",
500 HN_AUTOSCALE
, HN_B
| HN_NOSPACE
| HN_DECIMAL
);
501 printf("%8s %8s %5.0f%%\n", usedbuf
, availbuf
, bpercent
);
503 printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused
),
504 (intmax_t)CONVERT(bavail
), bpercent
);
509 print_swap(struct kvm_swap
*ksw
)
512 swtot
.ksw_total
+= ksw
->ksw_total
;
513 swtot
.ksw_used
+= ksw
->ksw_used
;
516 print_swap_line(ksw
->ksw_devname
, ksw
->ksw_total
,
517 ksw
->ksw_used
, ksw
->ksw_total
- ksw
->ksw_used
,
518 (ksw
->ksw_used
* 100.0) / ksw
->ksw_total
);
522 print_swap_total(void)
527 pagesize
= getpagesize();
528 getbsize(&hlen
, &blocksize
);
530 blocksize
= 1024 * 1024;
531 (void)printf("%jdM/%jdM swap space\n",
532 CONVERT(swtot
.ksw_used
), CONVERT(swtot
.ksw_total
));
533 } else if (nswdev
> 1) {
534 print_swap_line("Total", swtot
.ksw_total
, swtot
.ksw_used
,
535 swtot
.ksw_total
- swtot
.ksw_used
,
536 (swtot
.ksw_used
* 100.0) / swtot
.ksw_total
);
543 struct kvm_swap kswap
[16];
546 n
= kvm_getswapinfo(kd
, kswap
, sizeof kswap
/ sizeof kswap
[0],
550 for (i
= 0; i
< n
; ++i
)
551 print_swap(&kswap
[i
]);
556 swapmode_sysctl(void)
560 size_t mibsize
, size
;
564 mibsize
= sizeof mib
/ sizeof mib
[0];
565 if (sysctlnametomib("vm.swap_info", mib
, &mibsize
) == -1)
566 err(1, "sysctlnametomib()");
570 if (sysctl(mib
, mibsize
+ 1, &xsw
, &size
, NULL
, 0) == -1)
572 if (xsw
.xsw_version
!= XSWDEV_VERSION
)
573 errx(1, "xswdev version mismatch");
574 if (xsw
.xsw_dev
== NODEV
)
575 snprintf(ksw
.ksw_devname
, sizeof ksw
.ksw_devname
,
578 snprintf(ksw
.ksw_devname
, sizeof ksw
.ksw_devname
,
579 "/dev/%s", devname(xsw
.xsw_dev
, S_IFCHR
));
580 ksw
.ksw_used
= xsw
.xsw_used
;
581 ksw
.ksw_total
= xsw
.xsw_nblks
;
582 ksw
.ksw_flags
= xsw
.xsw_flags
;