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
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
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 $
35 #include <sys/kinfo.h>
36 #include <sys/param.h>
38 #include <sys/vnode.h>
39 #include <sys/ucred.h>
41 #include <vfs/ufs/quota.h>
42 #include <vfs/ufs/inode.h>
43 #include <sys/mount.h>
45 #include <sys/namei.h>
47 #include <nfs/rpcv2.h>
48 #include <nfs/nfsproto.h>
50 #include <nfs/nfsnode.h>
51 #include <sys/ioctl.h>
54 #include <sys/blist.h>
56 #include <sys/sysctl.h>
70 # define KCORE_KINFO_WRAPPER
77 * Backoff to DECIMAL mode if humanize_number does not have FRACTIONAL mode.
80 #define HN_FRACTIONAL HN_DECIMAL
94 const size_t NL_LAST_MANDATORY = NL_NUMVNODES;
97 { "_mountlist", 0, 0, 0, 0 }, /* address of head of mount list. */
98 { "_numvnodes", 0, 0, 0, 0 },
99 { "_rc_tty", 0, 0, 0, 0 },
100 { "_nrc_tty", 0, 0, 0, 0 },
101 { "_cy_tty", 0, 0, 0, 0 },
102 { "_ncy_tty", 0, 0, 0, 0 },
103 { "_si__tty", 0, 0, 0, 0 },
104 { "_si_Nports", 0, 0, 0, 0 },
118 const char *usagestr;
124 { MNT_RDONLY, "rdonly" },
125 { MNT_SYNCHRONOUS, "sync" },
126 { MNT_NOEXEC, "noexec" },
127 { MNT_NOSUID, "nosuid" },
128 { MNT_NODEV, "nodev" },
129 { MNT_ASYNC, "async" },
130 { MNT_SUIDDIR, "suiddir" },
131 { MNT_SOFTDEP, "softdep" },
132 { MNT_NOSYMFOLLOW, "nosymfollow" },
133 { MNT_NOATIME, "noatime" },
134 { MNT_NOCLUSTERR, "noclusterread" },
135 { MNT_NOCLUSTERW, "noclusterwrite" },
136 { MNT_EXRDONLY, "exrdonly" },
137 { MNT_EXPORTED, "exported" },
138 { MNT_DEFEXPORTED, "defexported" },
139 { MNT_EXPORTANON, "exportanon" },
140 { MNT_EXKERB, "exkerb" },
141 { MNT_EXPUBLIC, "public" },
142 { MNT_LOCAL, "local" },
143 { MNT_QUOTA, "quota" },
144 { MNT_ROOTFS, "rootfs" },
145 { MNT_USER, "user" },
146 { MNT_IGNORE, "ignore" },
147 { MNT_UPDATE, "update" },
148 { MNT_DELEXPORT, "delexport" },
149 { MNT_RELOAD, "reload" },
150 { MNT_FORCE, "force" },
155 #define SVAR(var) __STRING(var) /* to force expansion */
156 #define KGET(idx, var) \
157 KGET1(idx, &var, sizeof(var), SVAR(var))
158 #define KGET1(idx, p, s, msg) \
159 KGET2(nl[idx].n_value, p, s, msg)
160 #define KGET2(addr, p, s, msg) \
161 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
162 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
163 #define KGETN(idx, var) \
164 KGET1N(idx, &var, sizeof(var), SVAR(var))
165 #define KGET1N(idx, p, s, msg) \
166 KGET2N(nl[idx].n_value, p, s, msg)
167 #define KGET2N(addr, p, s, msg) \
168 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
169 #define KGETRET(addr, p, s, msg) \
170 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
171 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
176 int getfiles(char **, int *);
178 getmnt(struct mount *);
183 void mount_print(struct mount *);
184 void nfs_header(void);
185 int nfs_print(struct vnode *);
188 void ttyprt(struct tty *, int);
189 void ttytype(struct tty *, const char *, int, int, int);
190 void ufs_header(void);
191 int ufs_print(struct vnode *);
192 static void usage(void);
193 void vnode_header(void);
194 void vnode_print(struct vnode *, struct vnode *);
195 void vnodemode(void);
197 static void Output(const char *name, int hlen,
198 struct kvm_swap *kswap, int flags);
201 main(int argc, char **argv)
204 int fileflag, ttyflag, vnodeflag;
205 char buf[_POSIX2_LINE_MAX];
208 fileflag = swapflag = ttyflag = vnodeflag = 0;
210 /* We will behave like good old swapinfo if thus invoked */
211 opts = strrchr(argv[0],'/');
216 if (!strcmp(opts,"swapinfo")) {
219 usagestr = "swapinfo [-ghkm] [-M core] [-N system]";
221 opts = "TM:N:fhiknstv";
222 usagestr = "pstat [-Tfhknst] [-M core] [-N system]";
225 while ((ch = getopt(argc, argv, opts)) != -1) {
231 if (setenv("BLOCKSIZE", "1G", 1) == -1)
232 warn("setenv: cannot set BLOCKSIZE=1K");
235 if (setenv("BLOCKSIZE", "1K", 1) == -1)
236 warn("setenv: cannot set BLOCKSIZE=1K");
239 if (setenv("BLOCKSIZE", "1M", 1) == -1)
240 warn("setenv: cannot set BLOCKSIZE=1K");
264 case 'i': /* Backward compatibility. */
265 errx(1, "vnode mode not supported");
278 * Discard setgid privileges if not the running kernel so that bad
279 * guys can't print interesting stuff from kernel memory.
281 if (nlistf != NULL || memf != NULL)
284 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
285 errx(1, "kvm_openfiles: %s", buf);
287 if (kcore_wrapper_open(nlistf, memf, buf))
288 errx(1, "kcore_open: %s", buf);
290 if ((ret = kvm_nlist(kd, nl)) != 0) {
295 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
296 for (i = 0; i < NL_LAST_MANDATORY; i++) {
297 if (!nl[i].n_value) {
299 warnx("undefined symbol: %s", nl[i].n_name);
305 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
307 if (fileflag || totalflag)
313 if (swapflag || totalflag)
321 fprintf(stderr, "usage: %s\n", usagestr);
326 struct vnode *avnode;
333 struct e_vnode *e_vnodebase, *endvnode, *evp;
335 struct mount *maddr, *mp = NULL;
338 e_vnodebase = loadvnodes(&numvnodes);
340 printf("%7d vnodes\n", numvnodes);
343 endvnode = e_vnodebase + numvnodes;
344 printf("%d active vnodes\n", numvnodes);
347 #define ST mp->mnt_stat
349 for (evp = e_vnodebase; evp < endvnode; evp++) {
351 if (vp->v_mount != maddr) {
355 if ((mp = getmnt(vp->v_mount)) == NULL)
360 if (!strcmp(ST.f_fstypename, "ufs") ||
361 !strcmp(ST.f_fstypename, "mfs"))
363 else if (!strcmp(ST.f_fstypename, "nfs"))
367 vnode_print(evp->avnode, vp);
368 if (!strcmp(ST.f_fstypename, "ufs") ||
369 !strcmp(ST.f_fstypename, "mfs"))
371 else if (!strcmp(ST.f_fstypename, "nfs"))
381 printf("ADDR TYP VFLAG USE HOLD");
385 vnode_print(struct vnode *avnode, struct vnode *vp)
396 switch (vp->v_type) {
438 if (flag & (VAGE0 | VAGE1))
444 if (flag & VONWORKLST)
447 if (flag & VINACTIVE)
449 if (flag & VRECLAIMED)
458 * Convert SYSREF ref counts into something more
459 * human readable for display.
462 printf("%8lx %s %5s %08x %4d",
463 (u_long)(void *)avnode, type, flags, refs, vp->v_auxrefs);
469 printf(" FILEID IFLAG RDEV|SZ");
473 ufs_print(struct vnode *vp)
476 struct inode inode, *ip = &inode;
477 char flagbuf[16], *flags = flagbuf;
481 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
483 if (flag & IN_ACCESS)
485 if (flag & IN_CHANGE)
487 if (flag & IN_UPDATE)
489 if (flag & IN_MODIFIED)
491 if (flag & IN_RENAME)
493 if (flag & IN_SHLOCK)
495 if (flag & IN_EXLOCK)
497 if (flag & IN_HASHED)
499 if (flag & IN_LAZYMOD)
505 printf(" %6ju %5s", (uintmax_t)ip->i_number, flagbuf);
506 type = ip->i_mode & S_IFMT;
507 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
508 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
510 major(ip->i_rdev), minor(ip->i_rdev));
512 printf(" %7s", name);
514 printf(" %7ju", (uintmax_t)ip->i_size);
521 printf(" FILEID NFLAG RDEV|SZ");
525 nfs_print(struct vnode *vp)
527 struct nfsnode nfsnode, *np = &nfsnode;
528 char flagbuf[16], *flags = flagbuf;
533 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
535 if (flag & NFLUSHWANT)
537 if (flag & NFLUSHINPROG)
539 if (flag & NLMODIFIED)
541 if (flag & NRMODIFIED)
543 if (flag & NWRITEERR)
545 if (flag & NQNFSEVICTED)
561 #define VT np->n_vattr
562 printf(" %6ju %5s", (uintmax_t)VT.va_fileid, flagbuf);
563 type = VT.va_mode & S_IFMT;
564 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
565 if (usenumflag || ((name = devname((VT.va_rmajor << 8) | VT.va_rminor, type)) == NULL))
567 VT.va_rmajor, VT.va_rminor);
569 printf(" %7s", name);
571 printf(" %7ju", (uintmax_t)np->n_size);
576 * Given a pointer to a mount structure in kernel space,
577 * read it in and return a usable pointer to it.
580 getmnt(struct mount *maddr)
589 for (mt = mhead; mt != NULL; mt = mt->next)
590 if (maddr == mt->maddr)
592 if ((mt = malloc(sizeof(struct mtab))) == NULL)
594 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
602 mount_print(struct mount *mp)
606 #define ST mp->mnt_stat
607 printf("*** MOUNT %s %s on %s", ST.f_fstypename,
608 ST.f_mntfromname, ST.f_mntonname);
609 if ((flags = mp->mnt_flag)) {
611 const char *sep = " (";
613 for (i = 0; mnt_flags[i].m_flag; i++) {
614 if (flags & mnt_flags[i].m_flag) {
615 printf("%s%s", sep, mnt_flags[i].m_name);
616 flags &= ~mnt_flags[i].m_flag;
621 printf("%sunknown_flags:%x", sep, flags);
629 loadvnodes(int *avnodes)
633 struct e_vnode *vnodebase;
639 return (kinfo_vnodes(avnodes));
643 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1)
644 err(1, "sysctl: KERN_VNODE");
645 if ((vnodebase = malloc(copysize)) == NULL)
647 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1)
648 err(1, "sysctl: KERN_VNODE");
649 if (copysize % sizeof(struct e_vnode))
650 errx(1, "vnode size mismatch");
651 *avnodes = copysize / sizeof(struct e_vnode);
657 * simulate what a running kernel does in in kinfo_vnode
660 kinfo_vnodes(int *avnodes)
662 struct mntlist mountlist;
663 struct mount *mp, mounth, *mp_next;
664 struct vnode *vp, vnode, *vp_next;
665 char *vbuf, *evbuf, *bp;
668 #define VPTRSZ sizeof(struct vnode *)
669 #define VNODESZ sizeof(struct vnode)
671 KGET(NL_NUMVNODES, numvnodes);
672 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
675 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
676 KGET(NL_MOUNTLIST, mountlist);
677 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
678 KGET2(mp, &mounth, sizeof(mounth), "mount entry");
679 mp_next = TAILQ_NEXT(&mounth, mnt_list);
680 for (vp = TAILQ_FIRST(&mounth.mnt_nvnodelist);
681 vp != NULL; vp = vp_next) {
682 KGET2(vp, &vnode, sizeof(vnode), "vnode");
683 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
684 if ((bp + VPTRSZ + VNODESZ) > evbuf)
685 /* XXX - should realloc */
686 errx(1, "no more room for vnodes");
687 memmove(bp, &vp, VPTRSZ);
689 memmove(bp, &vnode, VNODESZ);
693 if (mp == TAILQ_LAST(&mountlist, mntlist))
697 return ((struct e_vnode *)vbuf);
701 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
708 struct tty ttyb[1000];
714 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
716 len /= sizeof(ttyb[0]);
717 for (i = 0; i < len; i++) {
721 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
723 if (nl[NL_NRC_TTY].n_type != 0)
724 ttytype(tty, "rc", NL_RC_TTY, NL_NRC_TTY, 0);
725 if (nl[NL_NCY_TTY].n_type != 0)
726 ttytype(tty, "cy", NL_CY_TTY, NL_NCY_TTY, 0);
727 if (nl[NL_SI_NPORTS].n_type != 0)
728 ttytype(tty, "si", NL_SI_TTY, NL_SI_NPORTS, 1);
733 ttytype(struct tty *tty, const char *name, int type, int number, int indir)
737 struct tty **ttyaddr;
742 printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
743 if (ntty > ttyspace) {
745 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == NULL)
750 KGET2(ttyaddr, tty, (ssize_t)(ntty * sizeof(struct tty)),
753 KGET1(type, tty, (ssize_t)(ntty * sizeof(struct tty)),
757 for (tp = tty; tp < &tty[ntty]; tp++)
758 ttyprt(tp, tp - tty);
771 { TS_CONNECTED, 'c'},
780 { TS_SO_OLOWAT, 'A'},
782 #ifdef TS_SO_OCOMPLETE
783 { TS_SO_OCOMPLETE, 'a'},
788 { TS_CAR_OFLOW, 'm'},
791 { TS_CTS_OFLOW, 'o'},
794 { TS_DSR_OFLOW, 'd'},
803 #ifdef TS_CAN_BYPASS_L_RINT
804 { TS_CAN_BYPASS_L_RINT, 'l'},
816 ttyprt(struct tty *tp, int line)
820 char *name, state[20];
823 dev = (uintptr_t)tp->t_dev & 0xffff; /* XXX t_dev is really dev_t */
825 if (usenumflag || dev == 0 || (name = devname(dev, S_IFCHR)) == NULL)
826 printf("%7d ", line);
828 printf("%7s ", name);
829 printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
830 printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
831 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
833 for (i = j = 0; ttystates[i].flag; i++)
834 if (tp->t_state&ttystates[i].flag)
835 state[j++] = ttystates[i].val;
839 printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
841 if (tp->t_pgrp != NULL)
842 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
843 printf("%6d ", pgid);
844 switch (tp->t_line) {
855 printf("%d\n", tp->t_line);
863 struct kinfo_file *fp, *ofp;
865 char flagbuf[16], *fbp;
867 static const char *dtypes[] = { "???", "inode", "socket" };
869 if (kinfo_get_maxfiles(&maxfile))
870 err(1, "kinfo_get_maxfiles");
872 if (kinfo_get_openfiles(&nfile))
873 err(1, "kinfo_get_openfiles");
874 printf("%3d/%3d files\n", nfile, maxfile);
877 if (kinfo_get_files(&fp, &len))
878 err(1, "kinfo_get_files");
881 printf("%zu/%d open files\n", len, maxfile);
882 printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
883 for (; len-- > 0; fp++) {
884 if ((unsigned)fp->f_type > DTYPE_SOCKET)
886 printf("%p ", fp->f_file);
887 printf("%-8.8s", dtypes[fp->f_type]);
889 if (fp->f_flag & FREAD)
891 if (fp->f_flag & FWRITE)
893 if (fp->f_flag & FAPPEND)
895 #ifdef FSHLOCK /* currently gone */
896 if (fp->f_flag & FSHLOCK)
898 if (fp->f_flag & FEXLOCK)
901 if (fp->f_flag & FASYNC)
904 printf("%6s %3d", flagbuf, fp->f_count);
905 printf(" %3d", fp->f_msgcount);
906 printf(" %8lx", (u_long)(void *)fp->f_data);
907 if (fp->f_offset < 0)
908 printf(" %jx\n", (uintmax_t)fp->f_offset);
910 printf(" %jd\n", (intmax_t)fp->f_offset);
916 * swapmode is based on a program called swapinfo written
917 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
922 struct kvm_swap kswap[16];
928 pagesize = getpagesize();
933 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
936 #define CONVERT(v) ((long)((quad_t)(v) * pagesize / blocksize))
937 #define CONVERTB(v) ((long)((quad_t)(v) * pagesize))
943 header = getbsize(&hlen, &blocksize);
946 if (totalflag == 0) {
947 printf("%-15s %*s %8s %8s %8s %s\n",
948 "Device", hlen, header,
949 "Used", "Avail", "Capacity", "Type");
951 for (i = 0; i < n; ++i) {
952 Output(kswap[i].ksw_devname, hlen, &kswap[i], 1);
960 humanize_number(buf1, sizeof(buf1),
961 CONVERTB(kswap[n].ksw_used),
963 HN_AUTOSCALE, HN_NOSPACE |
965 humanize_number(buf2, sizeof(buf2),
966 CONVERTB(kswap[n].ksw_total),
968 HN_AUTOSCALE, HN_NOSPACE |
970 printf("%s/%s swap space\n", buf1, buf2);
972 blocksize = 1024 * 1024;
973 printf("%ldM/%ldM swap space\n",
974 CONVERT(kswap[n].ksw_used),
975 CONVERT(kswap[n].ksw_total));
978 Output("Total", hlen, &kswap[n], 0);
984 Output(const char *name, int hlen, struct kvm_swap *kswap, int flags)
991 humanize_number(buf1, 6,
992 CONVERTB(kswap->ksw_total),
995 HN_NOSPACE | HN_FRACTIONAL);
996 humanize_number(buf2, 6,
997 CONVERTB(kswap->ksw_used),
1000 HN_NOSPACE | HN_FRACTIONAL);
1001 humanize_number(buf3, 6,
1002 CONVERTB(kswap->ksw_total -
1006 HN_NOSPACE | HN_FRACTIONAL);
1008 snprintf(buf1, sizeof(buf1), "%*ld",
1010 CONVERT(kswap->ksw_total));
1011 snprintf(buf2, sizeof(buf2), "%8ld",
1012 CONVERT(kswap->ksw_used));
1013 snprintf(buf3, sizeof(buf2), "%8ld",
1014 CONVERT(kswap->ksw_total -
1017 printf("%-15s %*s ", name, hlen, buf1);
1018 printf("%8s %8s %5.0f%%",
1021 (double)kswap->ksw_used * 100.0 / (double)kswap->ksw_total);
1024 ((kswap->ksw_flags & SW_SEQUENTIAL) ?
1025 "Sequential" : "Interleaved"));