df, pstat - Use HN_FRACTIONAL
[dragonfly.git] / usr.sbin / pstat / pstat.c
blob3d7ef801bd53878359666928c7e791a22c03d82d
1 /*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)pstat.c 8.16 (Berkeley) 5/9/95
31 * $FreeBSD: src/usr.sbin/pstat/pstat.c,v 1.49.2.5 2002/07/12 09:12:49 des Exp $
34 #include <sys/user.h>
35 #include <sys/kinfo.h>
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <sys/vnode.h>
39 #include <sys/ucred.h>
40 #include <sys/file.h>
41 #include <vfs/ufs/quota.h>
42 #include <vfs/ufs/inode.h>
43 #include <sys/mount.h>
44 #include <sys/uio.h>
45 #include <sys/namei.h>
46 #include <sys/stat.h>
47 #include <nfs/rpcv2.h>
48 #include <nfs/nfsproto.h>
49 #include <nfs/nfs.h>
50 #include <nfs/nfsnode.h>
51 #include <sys/ioctl.h>
52 #include <sys/ioctl_compat.h> /* XXX NTTYDISC is too well hidden */
53 #include <sys/tty.h>
54 #include <sys/conf.h>
55 #include <sys/blist.h>
57 #include <sys/sysctl.h>
59 #include <err.h>
60 #include <fcntl.h>
61 #include <kvm.h>
62 #include <limits.h>
63 #include <nlist.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <libutil.h>
70 #ifdef USE_KCORE
71 # define KCORE_KINFO_WRAPPER
72 # include <kcore.h>
73 #else
74 # include <kinfo.h>
75 #endif
78 * Backoff to DECIMAL mode if humanize_number does not have FRACTIONAL mode.
80 #ifndef HN_FRACTIONAL
81 #define HN_FRACTIONAL HN_DECIMAL
82 #endif
84 enum {
85 NL_MOUNTLIST,
86 NL_NUMVNODES,
87 NL_RC_TTY,
88 NL_NRC_TTY,
89 NL_CY_TTY,
90 NL_NCY_TTY,
91 NL_SI_TTY,
92 NL_SI_NPORTS,
95 const size_t NL_LAST_MANDATORY = NL_NUMVNODES;
97 struct nlist nl[] = {
98 { "_mountlist", 0, 0, 0, 0 }, /* address of head of mount list. */
99 { "_numvnodes", 0, 0, 0, 0 },
100 { "_rc_tty", 0, 0, 0, 0 },
101 { "_nrc_tty", 0, 0, 0, 0 },
102 { "_cy_tty", 0, 0, 0, 0 },
103 { "_ncy_tty", 0, 0, 0, 0 },
104 { "_si__tty", 0, 0, 0, 0 },
105 { "_si_Nports", 0, 0, 0, 0 },
106 { "", 0, 0, 0, 0 }
109 int usenumflag;
110 int totalflag;
111 int swapflag;
112 int humanflag;
113 long pagesize;
114 long blocksize;
115 char *nlistf = NULL;
116 char *memf = NULL;
117 kvm_t *kd;
119 const char *usagestr;
121 struct {
122 int m_flag;
123 const char *m_name;
124 } mnt_flags[] = {
125 { MNT_RDONLY, "rdonly" },
126 { MNT_SYNCHRONOUS, "sync" },
127 { MNT_NOEXEC, "noexec" },
128 { MNT_NOSUID, "nosuid" },
129 { MNT_NODEV, "nodev" },
130 { MNT_ASYNC, "async" },
131 { MNT_SUIDDIR, "suiddir" },
132 { MNT_SOFTDEP, "softdep" },
133 { MNT_NOSYMFOLLOW, "nosymfollow" },
134 { MNT_NOATIME, "noatime" },
135 { MNT_NOCLUSTERR, "noclusterread" },
136 { MNT_NOCLUSTERW, "noclusterwrite" },
137 { MNT_EXRDONLY, "exrdonly" },
138 { MNT_EXPORTED, "exported" },
139 { MNT_DEFEXPORTED, "defexported" },
140 { MNT_EXPORTANON, "exportanon" },
141 { MNT_EXKERB, "exkerb" },
142 { MNT_EXPUBLIC, "public" },
143 { MNT_LOCAL, "local" },
144 { MNT_QUOTA, "quota" },
145 { MNT_ROOTFS, "rootfs" },
146 { MNT_USER, "user" },
147 { MNT_IGNORE, "ignore" },
148 { MNT_UPDATE, "update" },
149 { MNT_DELEXPORT, "delexport" },
150 { MNT_RELOAD, "reload" },
151 { MNT_FORCE, "force" },
152 { 0, NULL }
156 #define SVAR(var) __STRING(var) /* to force expansion */
157 #define KGET(idx, var) \
158 KGET1(idx, &var, sizeof(var), SVAR(var))
159 #define KGET1(idx, p, s, msg) \
160 KGET2(nl[idx].n_value, p, s, msg)
161 #define KGET2(addr, p, s, msg) \
162 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
163 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
164 #define KGETN(idx, var) \
165 KGET1N(idx, &var, sizeof(var), SVAR(var))
166 #define KGET1N(idx, p, s, msg) \
167 KGET2N(nl[idx].n_value, p, s, msg)
168 #define KGET2N(addr, p, s, msg) \
169 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
170 #define KGETRET(addr, p, s, msg) \
171 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
172 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
173 return (0); \
176 void filemode(void);
177 int getfiles(char **, int *);
178 struct mount *
179 getmnt(struct mount *);
180 struct e_vnode *
181 kinfo_vnodes(int *);
182 struct e_vnode *
183 loadvnodes(int *);
184 void mount_print(struct mount *);
185 void nfs_header(void);
186 int nfs_print(struct vnode *);
187 void swapmode(void);
188 void ttymode(void);
189 void ttyprt(struct tty *, int);
190 void ttytype(struct tty *, const char *, int, int, int);
191 void ufs_header(void);
192 int ufs_print(struct vnode *);
193 static void usage(void);
194 void vnode_header(void);
195 void vnode_print(struct vnode *, struct vnode *);
196 void vnodemode(void);
198 static void Output(const char *name, int hlen,
199 struct kvm_swap *kswap, int flags);
202 main(int argc, char **argv)
204 int ch, ret;
205 int fileflag, ttyflag, vnodeflag;
206 char buf[_POSIX2_LINE_MAX];
207 const char *opts;
209 fileflag = swapflag = ttyflag = vnodeflag = 0;
211 /* We will behave like good old swapinfo if thus invoked */
212 opts = strrchr(argv[0],'/');
213 if (opts)
214 opts++;
215 else
216 opts = argv[0];
217 if (!strcmp(opts,"swapinfo")) {
218 swapflag = 1;
219 opts = "ghkmM:N:";
220 usagestr = "swapinfo [-ghkm] [-M core] [-N system]";
221 } else {
222 opts = "TM:N:fhiknstv";
223 usagestr = "pstat [-Tfhknst] [-M core] [-N system]";
226 while ((ch = getopt(argc, argv, opts)) != -1) {
227 switch (ch) {
228 case 'f':
229 fileflag = 1;
230 break;
231 case 'g':
232 if (setenv("BLOCKSIZE", "1G", 1) == -1)
233 warn("setenv: cannot set BLOCKSIZE=1K");
234 break;
235 case 'k':
236 if (setenv("BLOCKSIZE", "1K", 1) == -1)
237 warn("setenv: cannot set BLOCKSIZE=1K");
238 break;
239 case 'm':
240 if (setenv("BLOCKSIZE", "1M", 1) == -1)
241 warn("setenv: cannot set BLOCKSIZE=1K");
242 break;
243 case 'h':
244 humanflag = 1;
245 break;
246 case 'M':
247 memf = optarg;
248 break;
249 case 'N':
250 nlistf = optarg;
251 break;
252 case 'n':
253 usenumflag = 1;
254 break;
255 case 's':
256 ++swapflag;
257 break;
258 case 'T':
259 totalflag = 1;
260 break;
261 case 't':
262 ttyflag = 1;
263 break;
264 case 'v':
265 case 'i': /* Backward compatibility. */
266 errx(1, "vnode mode not supported");
267 #if 0
268 vnodeflag = 1;
269 break;
270 #endif
271 default:
272 usage();
275 argc -= optind;
276 argv += optind;
279 * Discard setgid privileges if not the running kernel so that bad
280 * guys can't print interesting stuff from kernel memory.
282 if (nlistf != NULL || memf != NULL)
283 setgid(getgid());
285 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
286 errx(1, "kvm_openfiles: %s", buf);
287 #ifdef USE_KCORE
288 if (kcore_wrapper_open(nlistf, memf, buf))
289 errx(1, "kcore_open: %s", buf);
290 #endif
291 if ((ret = kvm_nlist(kd, nl)) != 0) {
292 size_t i;
293 int quit = 0;
295 if (ret == -1)
296 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
297 for (i = 0; i < NL_LAST_MANDATORY; i++) {
298 if (!nl[i].n_value) {
299 quit = 1;
300 warnx("undefined symbol: %s", nl[i].n_name);
303 if (quit)
304 exit(1);
306 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
307 usage();
308 if (fileflag || totalflag)
309 filemode();
310 if (vnodeflag)
311 vnodemode();
312 if (ttyflag)
313 ttymode();
314 if (swapflag || totalflag)
315 swapmode();
316 exit (0);
319 static void
320 usage(void)
322 fprintf(stderr, "usage: %s\n", usagestr);
323 exit (1);
326 struct e_vnode {
327 struct vnode *avnode;
328 struct vnode vnode;
331 void
332 vnodemode(void)
334 struct e_vnode *e_vnodebase, *endvnode, *evp;
335 struct vnode *vp;
336 struct mount *maddr, *mp = NULL;
337 int numvnodes;
339 e_vnodebase = loadvnodes(&numvnodes);
340 if (totalflag) {
341 printf("%7d vnodes\n", numvnodes);
342 return;
344 endvnode = e_vnodebase + numvnodes;
345 printf("%d active vnodes\n", numvnodes);
348 #define ST mp->mnt_stat
349 maddr = NULL;
350 for (evp = e_vnodebase; evp < endvnode; evp++) {
351 vp = &evp->vnode;
352 if (vp->v_mount != maddr) {
354 * New filesystem
356 if ((mp = getmnt(vp->v_mount)) == NULL)
357 continue;
358 maddr = vp->v_mount;
359 mount_print(mp);
360 vnode_header();
361 if (!strcmp(ST.f_fstypename, "ufs") ||
362 !strcmp(ST.f_fstypename, "mfs"))
363 ufs_header();
364 else if (!strcmp(ST.f_fstypename, "nfs"))
365 nfs_header();
366 printf("\n");
368 vnode_print(evp->avnode, vp);
369 if (!strcmp(ST.f_fstypename, "ufs") ||
370 !strcmp(ST.f_fstypename, "mfs"))
371 ufs_print(vp);
372 else if (!strcmp(ST.f_fstypename, "nfs"))
373 nfs_print(vp);
374 printf("\n");
376 free(e_vnodebase);
379 void
380 vnode_header(void)
382 printf("ADDR TYP VFLAG USE HOLD");
385 void
386 vnode_print(struct vnode *avnode, struct vnode *vp)
388 const char *type;
389 char flags[32];
390 char *fp = flags;
391 int flag;
392 int refs;
395 * set type
397 switch (vp->v_type) {
398 case VNON:
399 type = "non"; break;
400 case VREG:
401 type = "reg"; break;
402 case VDIR:
403 type = "dir"; break;
404 case VBLK:
405 type = "blk"; break;
406 case VCHR:
407 type = "chr"; break;
408 case VLNK:
409 type = "lnk"; break;
410 case VSOCK:
411 type = "soc"; break;
412 case VFIFO:
413 type = "fif"; break;
414 case VBAD:
415 type = "bad"; break;
416 default:
417 type = "unk"; break;
420 * gather flags
422 flag = vp->v_flag;
423 if (flag & VROOT)
424 *fp++ = 'R';
425 if (flag & VTEXT)
426 *fp++ = 'T';
427 if (flag & VSYSTEM)
428 *fp++ = 'S';
429 if (flag & VISTTY)
430 *fp++ = 't';
431 #ifdef VXLOCK
432 if (flag & VXLOCK)
433 *fp++ = 'L';
434 if (flag & VXWANT)
435 *fp++ = 'W';
436 #endif
437 if (flag & VOBJBUF)
438 *fp++ = 'V';
439 if (flag & (VAGE0 | VAGE1))
440 *fp++ = 'a';
441 #ifdef VDOOMED
442 if (flag & VDOOMED)
443 *fp++ = 'D';
444 #endif
445 if (flag & VONWORKLST)
446 *fp++ = 'O';
447 #ifdef VRECLAIMED
448 if (flag & VINACTIVE)
449 *fp++ = 'I';
450 if (flag & VRECLAIMED)
451 *fp++ = 'X';
452 #endif
454 if (flag == 0)
455 *fp++ = '-';
456 *fp = '\0';
459 * Convert SYSREF ref counts into something more
460 * human readable for display.
462 refs = vp->v_refcnt;
463 printf("%8lx %s %5s %08x %4d",
464 (u_long)(void *)avnode, type, flags, refs, vp->v_auxrefs);
467 void
468 ufs_header(void)
470 printf(" FILEID IFLAG RDEV|SZ");
474 ufs_print(struct vnode *vp)
476 int flag;
477 struct inode inode, *ip = &inode;
478 char flagbuf[16], *flags = flagbuf;
479 char *name;
480 mode_t type;
482 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
483 flag = ip->i_flag;
484 if (flag & IN_ACCESS)
485 *flags++ = 'A';
486 if (flag & IN_CHANGE)
487 *flags++ = 'C';
488 if (flag & IN_UPDATE)
489 *flags++ = 'U';
490 if (flag & IN_MODIFIED)
491 *flags++ = 'M';
492 if (flag & IN_RENAME)
493 *flags++ = 'R';
494 if (flag & IN_SHLOCK)
495 *flags++ = 'S';
496 if (flag & IN_EXLOCK)
497 *flags++ = 'E';
498 if (flag & IN_HASHED)
499 *flags++ = 'H';
500 if (flag & IN_LAZYMOD)
501 *flags++ = 'L';
502 if (flag == 0)
503 *flags++ = '-';
504 *flags = '\0';
506 printf(" %6ju %5s", (uintmax_t)ip->i_number, flagbuf);
507 type = ip->i_mode & S_IFMT;
508 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
509 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
510 printf(" %2d,%-2d",
511 major(ip->i_rdev), minor(ip->i_rdev));
512 else
513 printf(" %7s", name);
514 else
515 printf(" %7ju", (uintmax_t)ip->i_size);
516 return (0);
519 void
520 nfs_header(void)
522 printf(" FILEID NFLAG RDEV|SZ");
526 nfs_print(struct vnode *vp)
528 struct nfsnode nfsnode, *np = &nfsnode;
529 char flagbuf[16], *flags = flagbuf;
530 int flag;
531 char *name;
532 mode_t type;
534 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
535 flag = np->n_flag;
536 if (flag & NFLUSHWANT)
537 *flags++ = 'W';
538 if (flag & NFLUSHINPROG)
539 *flags++ = 'P';
540 if (flag & NLMODIFIED)
541 *flags++ = 'M';
542 if (flag & NRMODIFIED)
543 *flags++ = 'R';
544 if (flag & NWRITEERR)
545 *flags++ = 'E';
546 if (flag & NQNFSEVICTED)
547 *flags++ = 'G';
548 if (flag & NACC)
549 *flags++ = 'A';
550 if (flag & NUPD)
551 *flags++ = 'U';
552 if (flag & NCHG)
553 *flags++ = 'C';
554 if (flag & NLOCKED)
555 *flags++ = 'L';
556 if (flag & NWANTED)
557 *flags++ = 'w';
558 if (flag == 0)
559 *flags++ = '-';
560 *flags = '\0';
562 #define VT np->n_vattr
563 printf(" %6ju %5s", (uintmax_t)VT.va_fileid, flagbuf);
564 type = VT.va_mode & S_IFMT;
565 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
566 if (usenumflag || ((name = devname((VT.va_rmajor << 8) | VT.va_rminor, type)) == NULL))
567 printf(" %2d,%-2d",
568 VT.va_rmajor, VT.va_rminor);
569 else
570 printf(" %7s", name);
571 else
572 printf(" %7ju", (uintmax_t)np->n_size);
573 return (0);
577 * Given a pointer to a mount structure in kernel space,
578 * read it in and return a usable pointer to it.
580 struct mount *
581 getmnt(struct mount *maddr)
583 static struct mtab {
584 struct mtab *next;
585 struct mount *maddr;
586 struct mount mount;
587 } *mhead = NULL;
588 struct mtab *mt;
590 for (mt = mhead; mt != NULL; mt = mt->next)
591 if (maddr == mt->maddr)
592 return (&mt->mount);
593 if ((mt = malloc(sizeof(struct mtab))) == NULL)
594 errx(1, "malloc");
595 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
596 mt->maddr = maddr;
597 mt->next = mhead;
598 mhead = mt;
599 return (&mt->mount);
602 void
603 mount_print(struct mount *mp)
605 int flags;
607 #define ST mp->mnt_stat
608 printf("*** MOUNT %s %s on %s", ST.f_fstypename,
609 ST.f_mntfromname, ST.f_mntonname);
610 if ((flags = mp->mnt_flag)) {
611 int i;
612 const char *sep = " (";
614 for (i = 0; mnt_flags[i].m_flag; i++) {
615 if (flags & mnt_flags[i].m_flag) {
616 printf("%s%s", sep, mnt_flags[i].m_name);
617 flags &= ~mnt_flags[i].m_flag;
618 sep = ",";
621 if (flags)
622 printf("%sunknown_flags:%x", sep, flags);
623 printf(")");
625 printf("\n");
626 #undef ST
629 struct e_vnode *
630 loadvnodes(int *avnodes)
632 int mib[2];
633 size_t copysize;
634 struct e_vnode *vnodebase;
636 if (memf != NULL) {
638 * do it by hand
640 return (kinfo_vnodes(avnodes));
642 mib[0] = CTL_KERN;
643 mib[1] = KERN_VNODE;
644 if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
645 err(1, "sysctl: KERN_VNODE");
646 if ((vnodebase = malloc(copysize)) == NULL)
647 errx(1, "malloc");
648 if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
649 err(1, "sysctl: KERN_VNODE");
650 if (copysize % sizeof(struct e_vnode))
651 errx(1, "vnode size mismatch");
652 *avnodes = copysize / sizeof(struct e_vnode);
654 return (vnodebase);
658 * simulate what a running kernel does in in kinfo_vnode
660 struct e_vnode *
661 kinfo_vnodes(int *avnodes)
663 struct mntlist mountlist;
664 struct mount *mp, mounth, *mp_next;
665 struct vnode *vp, vnode, *vp_next;
666 char *vbuf, *evbuf, *bp;
667 int num, numvnodes;
669 #define VPTRSZ sizeof(struct vnode *)
670 #define VNODESZ sizeof(struct vnode)
672 KGET(NL_NUMVNODES, numvnodes);
673 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
674 errx(1, "malloc");
675 bp = vbuf;
676 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
677 KGET(NL_MOUNTLIST, mountlist);
678 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
679 KGET2(mp, &mounth, sizeof(mounth), "mount entry");
680 mp_next = TAILQ_NEXT(&mounth, mnt_list);
681 for (vp = TAILQ_FIRST(&mounth.mnt_nvnodelist);
682 vp != NULL; vp = vp_next) {
683 KGET2(vp, &vnode, sizeof(vnode), "vnode");
684 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
685 if ((bp + VPTRSZ + VNODESZ) > evbuf)
686 /* XXX - should realloc */
687 errx(1, "no more room for vnodes");
688 memmove(bp, &vp, VPTRSZ);
689 bp += VPTRSZ;
690 memmove(bp, &vnode, VNODESZ);
691 bp += VNODESZ;
692 num++;
694 if (mp == TAILQ_LAST(&mountlist, mntlist))
695 break;
697 *avnodes = num;
698 return ((struct e_vnode *)vbuf);
701 char hdr[] =
702 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
703 int ttyspace = 128;
705 void
706 ttymode(void)
708 struct tty *tty;
709 struct tty ttyb[1000];
710 int error;
711 size_t len, i;
713 printf("%s", hdr);
714 len = sizeof(ttyb);
715 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
716 if (!error) {
717 len /= sizeof(ttyb[0]);
718 for (i = 0; i < len; i++) {
719 ttyprt(&ttyb[i], 0);
722 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
723 errx(1, "malloc");
724 if (nl[NL_NRC_TTY].n_type != 0)
725 ttytype(tty, "rc", NL_RC_TTY, NL_NRC_TTY, 0);
726 if (nl[NL_NCY_TTY].n_type != 0)
727 ttytype(tty, "cy", NL_CY_TTY, NL_NCY_TTY, 0);
728 if (nl[NL_SI_NPORTS].n_type != 0)
729 ttytype(tty, "si", NL_SI_TTY, NL_SI_NPORTS, 1);
730 free(tty);
733 void
734 ttytype(struct tty *tty, const char *name, int type, int number, int indir)
736 struct tty *tp;
737 int ntty;
738 struct tty **ttyaddr;
740 if (tty == NULL)
741 return;
742 KGET(number, ntty);
743 printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
744 if (ntty > ttyspace) {
745 ttyspace = ntty;
746 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == NULL)
747 errx(1, "realloc");
749 if (indir) {
750 KGET(type, ttyaddr);
751 KGET2(ttyaddr, tty, (ssize_t)(ntty * sizeof(struct tty)),
752 "tty structs");
753 } else {
754 KGET1(type, tty, (ssize_t)(ntty * sizeof(struct tty)),
755 "tty structs");
757 printf("%s", hdr);
758 for (tp = tty; tp < &tty[ntty]; tp++)
759 ttyprt(tp, tp - tty);
762 struct {
763 int flag;
764 char val;
765 } ttystates[] = {
766 #ifdef TS_WOPEN
767 { TS_WOPEN, 'W'},
768 #endif
769 { TS_ISOPEN, 'O'},
770 { TS_CARR_ON, 'C'},
771 #ifdef TS_CONNECTED
772 { TS_CONNECTED, 'c'},
773 #endif
774 { TS_TIMEOUT, 'T'},
775 { TS_FLUSH, 'F'},
776 { TS_BUSY, 'B'},
777 #ifdef TS_ASLEEP
778 { TS_ASLEEP, 'A'},
779 #endif
780 #ifdef TS_SO_OLOWAT
781 { TS_SO_OLOWAT, 'A'},
782 #endif
783 #ifdef TS_SO_OCOMPLETE
784 { TS_SO_OCOMPLETE, 'a'},
785 #endif
786 { TS_XCLUDE, 'X'},
787 { TS_TTSTOP, 'S'},
788 #ifdef TS_CAR_OFLOW
789 { TS_CAR_OFLOW, 'm'},
790 #endif
791 #ifdef TS_CTS_OFLOW
792 { TS_CTS_OFLOW, 'o'},
793 #endif
794 #ifdef TS_DSR_OFLOW
795 { TS_DSR_OFLOW, 'd'},
796 #endif
797 { TS_TBLOCK, 'K'},
798 { TS_ASYNC, 'Y'},
799 { TS_BKSL, 'D'},
800 { TS_ERASE, 'E'},
801 { TS_LNCH, 'L'},
802 { TS_TYPEN, 'P'},
803 { TS_CNTTB, 'N'},
804 #ifdef TS_CAN_BYPASS_L_RINT
805 { TS_CAN_BYPASS_L_RINT, 'l'},
806 #endif
807 #ifdef TS_SNOOP
808 { TS_SNOOP, 's'},
809 #endif
810 #ifdef TS_ZOMBIE
811 { TS_ZOMBIE, 'Z'},
812 #endif
813 { 0, '\0'},
816 void
817 ttyprt(struct tty *tp, int line)
819 int i, j;
820 pid_t pgid;
821 char *name, state[20];
822 dev_t dev;
824 dev = (uintptr_t)tp->t_dev & 0xffff; /* XXX t_dev is really dev_t */
826 if (usenumflag || dev == 0 || (name = devname(dev, S_IFCHR)) == NULL)
827 printf("%7d ", line);
828 else
829 printf("%7s ", name);
830 printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
831 printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
832 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
833 tp->t_column);
834 for (i = j = 0; ttystates[i].flag; i++)
835 if (tp->t_state&ttystates[i].flag)
836 state[j++] = ttystates[i].val;
837 if (j == 0)
838 state[j++] = '-';
839 state[j] = '\0';
840 printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
841 pgid = 0;
842 if (tp->t_pgrp != NULL)
843 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
844 printf("%6d ", pgid);
845 switch (tp->t_line) {
846 case TTYDISC:
847 printf("term\n");
848 break;
849 case NTTYDISC:
850 printf("ntty\n");
851 break;
852 case SLIPDISC:
853 printf("slip\n");
854 break;
855 case PPPDISC:
856 printf("ppp\n");
857 break;
858 default:
859 printf("%d\n", tp->t_line);
860 break;
864 void
865 filemode(void)
867 struct kinfo_file *fp, *ofp;
868 size_t len;
869 char flagbuf[16], *fbp;
870 int maxfile, nfile;
871 static const char *dtypes[] = { "???", "inode", "socket" };
873 if (kinfo_get_maxfiles(&maxfile))
874 err(1, "kinfo_get_maxfiles");
875 if (totalflag) {
876 if (kinfo_get_openfiles(&nfile))
877 err(1, "kinfo_get_openfiles");
878 printf("%3d/%3d files\n", nfile, maxfile);
879 return;
881 if (kinfo_get_files(&fp, &len))
882 err(1, "kinfo_get_files");
883 ofp = fp;
885 printf("%zu/%d open files\n", len, maxfile);
886 printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
887 for (; len-- > 0; fp++) {
888 if ((unsigned)fp->f_type > DTYPE_SOCKET)
889 continue;
890 printf("%p ", fp->f_file);
891 printf("%-8.8s", dtypes[fp->f_type]);
892 fbp = flagbuf;
893 if (fp->f_flag & FREAD)
894 *fbp++ = 'R';
895 if (fp->f_flag & FWRITE)
896 *fbp++ = 'W';
897 if (fp->f_flag & FAPPEND)
898 *fbp++ = 'A';
899 #ifdef FSHLOCK /* currently gone */
900 if (fp->f_flag & FSHLOCK)
901 *fbp++ = 'S';
902 if (fp->f_flag & FEXLOCK)
903 *fbp++ = 'X';
904 #endif
905 if (fp->f_flag & FASYNC)
906 *fbp++ = 'I';
907 *fbp = '\0';
908 printf("%6s %3d", flagbuf, fp->f_count);
909 printf(" %3d", fp->f_msgcount);
910 printf(" %8lx", (u_long)(void *)fp->f_data);
911 if (fp->f_offset < 0)
912 printf(" %jx\n", (uintmax_t)fp->f_offset);
913 else
914 printf(" %jd\n", (intmax_t)fp->f_offset);
916 free(ofp);
920 * swapmode is based on a program called swapinfo written
921 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
923 void
924 swapmode(void)
926 struct kvm_swap kswap[16];
927 int i;
928 int n;
929 const char *header;
930 int hlen;
932 pagesize = getpagesize();
933 n = kvm_getswapinfo(
934 kd,
935 kswap,
936 sizeof(kswap)/sizeof(kswap[0]),
937 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
940 #define CONVERT(v) ((long)((quad_t)(v) * pagesize / blocksize))
941 #define CONVERTB(v) ((long)((quad_t)(v) * pagesize))
943 if (humanflag) {
944 hlen = 9;
945 header = " Blocks";
946 } else {
947 header = getbsize(&hlen, &blocksize);
950 if (totalflag == 0) {
951 printf("%-15s %*s %8s %8s %8s %s\n",
952 "Device", hlen, header,
953 "Used", "Avail", "Capacity", "Type");
955 for (i = 0; i < n; ++i) {
956 Output(kswap[i].ksw_devname, hlen, &kswap[i], 1);
960 if (totalflag) {
961 if (humanflag) {
962 char buf1[6];
963 char buf2[6];
964 humanize_number(buf1, sizeof(buf1),
965 CONVERTB(kswap[n].ksw_used),
967 HN_AUTOSCALE, HN_NOSPACE |
968 HN_FRACTIONAL);
969 humanize_number(buf2, sizeof(buf2),
970 CONVERTB(kswap[n].ksw_total),
972 HN_AUTOSCALE, HN_NOSPACE |
973 HN_FRACTIONAL);
974 printf("%s/%s swap space\n", buf1, buf2);
975 } else {
976 blocksize = 1024 * 1024;
977 printf("%ldM/%ldM swap space\n",
978 CONVERT(kswap[n].ksw_used),
979 CONVERT(kswap[n].ksw_total));
981 } else if (n > 1) {
982 Output("Total", hlen, &kswap[n], 0);
986 static
987 void
988 Output(const char *name, int hlen, struct kvm_swap *kswap, int flags)
990 char buf1[32];
991 char buf2[32];
992 char buf3[32];
994 if (humanflag) {
995 humanize_number(buf1, 6,
996 CONVERTB(kswap->ksw_total),
998 HN_AUTOSCALE,
999 HN_NOSPACE | HN_FRACTIONAL);
1000 humanize_number(buf2, 6,
1001 CONVERTB(kswap->ksw_used),
1003 HN_AUTOSCALE,
1004 HN_NOSPACE | HN_FRACTIONAL);
1005 humanize_number(buf3, 6,
1006 CONVERTB(kswap->ksw_total -
1007 kswap->ksw_used),
1009 HN_AUTOSCALE,
1010 HN_NOSPACE | HN_FRACTIONAL);
1011 } else {
1012 snprintf(buf1, sizeof(buf1), "%*ld",
1013 hlen,
1014 CONVERT(kswap->ksw_total));
1015 snprintf(buf2, sizeof(buf2), "%8ld",
1016 CONVERT(kswap->ksw_used));
1017 snprintf(buf3, sizeof(buf2), "%8ld",
1018 CONVERT(kswap->ksw_total -
1019 kswap->ksw_used));
1021 printf("%-15s %*s ", name, hlen, buf1);
1022 printf("%8s %8s %5.0f%%",
1023 buf2,
1024 buf3,
1025 (double)kswap->ksw_used * 100.0 / (double)kswap->ksw_total);
1026 if (flags) {
1027 printf(" %s",
1028 ((kswap->ksw_flags & SW_SEQUENTIAL) ?
1029 "Sequential" : "Interleaved"));
1031 printf("\n");