📄 pstat.c
字号:
if (flags & MNT_RDONLY) { (void)printf("%srdonly", comma); flags &= ~MNT_RDONLY; comma = ","; } if (flags & MNT_SYNCHRONOUS) { (void)printf("%ssynchronous", comma); flags &= ~MNT_SYNCHRONOUS; comma = ","; } if (flags & MNT_NOEXEC) { (void)printf("%snoexec", comma); flags &= ~MNT_NOEXEC; comma = ","; } if (flags & MNT_NOSUID) { (void)printf("%snosuid", comma); flags &= ~MNT_NOSUID; comma = ","; } if (flags & MNT_NODEV) { (void)printf("%snodev", comma); flags &= ~MNT_NODEV; comma = ","; } if (flags & MNT_EXPORTED) { (void)printf("%sexport", comma); flags &= ~MNT_EXPORTED; comma = ","; } if (flags & MNT_EXRDONLY) { (void)printf("%sexrdonly", comma); flags &= ~MNT_EXRDONLY; comma = ","; } if (flags & MNT_LOCAL) { (void)printf("%slocal", comma); flags &= ~MNT_LOCAL; comma = ","; } if (flags & MNT_QUOTA) { (void)printf("%squota", comma); flags &= ~MNT_QUOTA; comma = ","; } /* filesystem control flags */ if (flags & MNT_UPDATE) { (void)printf("%supdate", comma); flags &= ~MNT_UPDATE; comma = ","; } if (flags & MNT_MLOCK) { (void)printf("%slock", comma); flags &= ~MNT_MLOCK; comma = ","; } if (flags & MNT_MWAIT) { (void)printf("%swait", comma); flags &= ~MNT_MWAIT; comma = ","; } if (flags & MNT_MPBUSY) { (void)printf("%sbusy", comma); flags &= ~MNT_MPBUSY; comma = ","; } if (flags & MNT_MPWANT) { (void)printf("%swant", comma); flags &= ~MNT_MPWANT; comma = ","; } if (flags & MNT_UNMOUNT) { (void)printf("%sunmount", comma); flags &= ~MNT_UNMOUNT; comma = ","; } if (flags) (void)printf("%sunknown_flags:%x", comma, flags); (void)printf(")"); } (void)printf("\n");#undef ST}struct e_vnode *loadvnodes(avnodes) int *avnodes;{ int mib[2]; size_t copysize; struct e_vnode *vnodebase; if (memf != NULL) { /* * do it by hand */ return (kinfo_vnodes(avnodes)); } mib[0] = CTL_KERN; mib[1] = KERN_VNODE; if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) err(1, "sysctl: KERN_VNODE"); if ((vnodebase = malloc(copysize)) == NULL) err(1, NULL); if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) err(1, "sysctl: KERN_VNODE"); if (copysize % sizeof(struct e_vnode)) errx(1, "vnode size mismatch"); *avnodes = copysize / sizeof(struct e_vnode); return (vnodebase);}/* * simulate what a running kernel does in in kinfo_vnode */struct e_vnode *kinfo_vnodes(avnodes) int *avnodes;{ struct mntlist mountlist; struct mount *mp, mount; struct vnode *vp, vnode; char *vbuf, *evbuf, *bp; int num, numvnodes;#define VPTRSZ sizeof(struct vnode *)#define VNODESZ sizeof(struct vnode) KGET(V_NUMV, numvnodes); if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) err(1, NULL); bp = vbuf; evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); KGET(V_MOUNTLIST, mountlist); for (num = 0, mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) { KGET2(mp, &mount, sizeof(mount), "mount entry"); for (vp = mount.mnt_vnodelist.lh_first; vp != NULL; vp = vp->v_mntvnodes.le_next) { KGET2(vp, &vnode, sizeof(vnode), "vnode"); if ((bp + VPTRSZ + VNODESZ) > evbuf) /* XXX - should realloc */ errx(1, "no more room for vnodes"); memmove(bp, &vp, VPTRSZ); bp += VPTRSZ; memmove(bp, &vnode, VNODESZ); bp += VNODESZ; num++; } } *avnodes = num; return ((struct e_vnode *)vbuf);} char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n";int ttyspace = 128;voidttymode(){ struct tty *tty; if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL) err(1, NULL);#ifndef hp300 (void)printf("1 console\n"); KGET(SCONS, *tty); (void)printf(hdr); ttyprt(&tty[0], 0);#endif#ifdef vax if (nl[SNQD].n_type != 0) qdss(); if (nl[SNDZ].n_type != 0) ttytype(tty, "dz", SDZ, SNDZ); if (nl[SNDH].n_type != 0) ttytype(tty, "dh", SDH, SNDH); if (nl[SNDMF].n_type != 0) ttytype(tty, "dmf", SDMF, SNDMF); if (nl[SNDHU].n_type != 0) ttytype(tty, "dhu", SDHU, SNDHU); if (nl[SNDMZ].n_type != 0) ttytype(tty, "dmz", SDMZ, SNDMZ);#endif#ifdef tahoe if (nl[SNVX].n_type != 0) ttytype(tty, "vx", SVX, SNVX); if (nl[SNMP].n_type != 0) ttytype(tty, "mp", SMP, SNMP);#endif#ifdef hp300 if (nl[SNITE].n_type != 0) ttytype(tty, "ite", SITE, SNITE); if (nl[SNDCA].n_type != 0) ttytype(tty, "dca", SDCA, SNDCA); if (nl[SNDCM].n_type != 0) ttytype(tty, "dcm", SDCM, SNDCM); if (nl[SNDCL].n_type != 0) ttytype(tty, "dcl", SDCL, SNDCL);#endif#ifdef mips if (nl[SNDC].n_type != 0) ttytype(tty, "dc", SDC, SNDC);#endif if (nl[SNPTY].n_type != 0) ttytype(tty, "pty", SPTY, SNPTY);}voidttytype(tty, name, type, number) register struct tty *tty; char *name; int type, number;{ register struct tty *tp; int ntty; if (tty == NULL) return; KGET(number, ntty); (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines"); if (ntty > ttyspace) { ttyspace = ntty; if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0) err(1, NULL); } KGET1(type, tty, ntty * sizeof(struct tty), "tty structs"); (void)printf(hdr); for (tp = tty; tp < &tty[ntty]; tp++) ttyprt(tp, tp - tty);}struct { int flag; char val;} ttystates[] = { { TS_WOPEN, 'W'}, { TS_ISOPEN, 'O'}, { TS_CARR_ON, 'C'}, { TS_TIMEOUT, 'T'}, { TS_FLUSH, 'F'}, { TS_BUSY, 'B'}, { TS_ASLEEP, 'A'}, { TS_XCLUDE, 'X'}, { TS_TTSTOP, 'S'}, { TS_TBLOCK, 'K'}, { TS_ASYNC, 'Y'}, { TS_BKSL, 'D'}, { TS_ERASE, 'E'}, { TS_LNCH, 'L'}, { TS_TYPEN, 'P'}, { TS_CNTTB, 'N'}, { 0, '\0'},};voidttyprt(tp, line) register struct tty *tp; int line;{ register int i, j; pid_t pgid; char *name, state[20]; if (usenumflag || tp->t_dev == 0 || (name = devname(tp->t_dev, S_IFCHR)) == NULL) (void)printf("%7d ", line); else (void)printf("%7s ", name); (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); (void)printf("%3d %4d %3d %3d ", tp->t_outq.c_cc, tp->t_hiwat, tp->t_lowat, tp->t_column); for (i = j = 0; ttystates[i].flag; i++) if (tp->t_state&ttystates[i].flag) state[j++] = ttystates[i].val; if (j == 0) state[j++] = '-'; state[j] = '\0'; (void)printf("%-4s %6x", state, (u_long)tp->t_session & ~KERNBASE); pgid = 0; if (tp->t_pgrp != NULL) KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); (void)printf("%6d ", pgid); switch (tp->t_line) { case TTYDISC: (void)printf("term\n"); break; case TABLDISC: (void)printf("tab\n"); break; case SLIPDISC: (void)printf("slip\n"); break; default: (void)printf("%d\n", tp->t_line); break; }}voidfilemode(){ register struct file *fp; struct file *addr; char *buf, flagbuf[16], *fbp; int len, maxfile, nfile; static char *dtypes[] = { "???", "inode", "socket" }; KGET(FNL_MAXFILE, maxfile); if (totalflag) { KGET(FNL_NFILE, nfile); (void)printf("%3d/%3d files\n", nfile, maxfile); return; } if (getfiles(&buf, &len) == -1) return; /* * Getfiles returns in malloc'd memory a pointer to the first file * structure, and then an array of file structs (whose addresses are * derivable from the previous entry). */ addr = *((struct file **)buf); fp = (struct file *)(buf + sizeof(struct file *)); nfile = (len - sizeof(struct file *)) / sizeof(struct file); (void)printf("%d/%d open files\n", nfile, maxfile); (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); for (; (char *)fp < buf + len; addr = fp->f_filef, fp++) { if ((unsigned)fp->f_type > DTYPE_SOCKET) continue; (void)printf("%x ", addr); (void)printf("%-8.8s", dtypes[fp->f_type]); fbp = flagbuf; if (fp->f_flag & FREAD) *fbp++ = 'R'; if (fp->f_flag & FWRITE) *fbp++ = 'W'; if (fp->f_flag & FAPPEND) *fbp++ = 'A';#ifdef FSHLOCK /* currently gone */ if (fp->f_flag & FSHLOCK) *fbp++ = 'S'; if (fp->f_flag & FEXLOCK) *fbp++ = 'X';#endif if (fp->f_flag & FASYNC) *fbp++ = 'I'; *fbp = '\0'; (void)printf("%6s %3d", flagbuf, fp->f_count); (void)printf(" %3d", fp->f_msgcount); (void)printf(" %8.1x", fp->f_data); if (fp->f_offset < 0) (void)printf(" %qx\n", fp->f_offset); else (void)printf(" %qd\n", fp->f_offset); } free(buf);}intgetfiles(abuf, alen) char **abuf; int *alen;{ size_t len; int mib[2]; char *buf; /* * XXX * Add emulation of KINFO_FILE here. */ if (memf != NULL) errx(1, "files on dead kernel, not implemented\n"); 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) err(1, NULL); 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>. */voidswapmode(){ char *header; int hlen, nswap, nswdev, dmmax, nswapmap, niswap, niswdev; int s, e, div, i, l, avail, nfree, npfree, used; struct swdevt *sw; long blocksize, *perdev; struct map *swapmap, *kswapmap; struct mapent *mp; KGET(VM_NSWAP, nswap); KGET(VM_NSWDEV, nswdev); KGET(VM_DMMAX, dmmax); KGET(VM_NSWAPMAP, nswapmap); KGET(VM_SWAPMAP, kswapmap); /* kernel `swapmap' is a pointer */ if ((sw = malloc(nswdev * sizeof(*sw))) == NULL || (perdev = malloc(nswdev * sizeof(*perdev))) == NULL || (mp = malloc(nswapmap * sizeof(*mp))) == NULL) err(1, "malloc"); KGET1(VM_SWDEVT, sw, nswdev * sizeof(*sw), "swdevt"); KGET2((long)kswapmap, mp, nswapmap * sizeof(*mp), "swapmap"); /* Supports sequential swap */ if (nl[VM_NISWAP].n_value != 0) { KGET(VM_NISWAP, niswap); KGET(VM_NISWDEV, niswdev); } else { niswap = nswap; niswdev = nswdev; } /* First entry in map is `struct map'; rest are mapent's. */ swapmap = (struct map *)mp; if (nswapmap != swapmap->m_limit - (struct mapent *)kswapmap) errx(1, "panic: nswapmap goof"); /* Count up swap space. */ nfree = 0; memset(perdev, 0, nswdev * sizeof(*perdev)); for (mp++; mp->m_addr != 0; mp++) { s = mp->m_addr; /* start of swap region */ e = mp->m_addr + mp->m_size; /* end of region */ nfree += mp->m_size; /* * Swap space is split up among the configured disks. * * For interleaved swap devices, the first dmmax blocks * of swap space some from the first disk, the next dmmax * blocks from the next, and so on up to niswap blocks. * * Sequential swap devices follow the interleaved devices * (i.e. blocks starting at niswap) in the order in which * they appear in the swdev table. The size of each device * will be a multiple of dmmax. * * The list of free space joins adjacent free blocks, * ignoring device boundries. If we want to keep track * of this information per device, we'll just have to * extract it ourselves. We know that dmmax-sized chunks * cannot span device boundaries (interleaved or sequential) * so we loop over such chunks assigning them to devices. */ i = -1; while (s < e) { /* XXX this is inefficient */ int bound = roundup(s+1, dmmax); if (bound > e) bound = e; if (bound <= niswap) { /* Interleaved swap chunk. */ if (i == -1) i = (s / dmmax) % niswdev; perdev[i] += bound - s; if (++i >= niswdev) i = 0; } else { /* Sequential swap chunk. */ if (i < niswdev) { i = niswdev; l = niswap + sw[i].sw_nblks; } while (s >= l) { /* XXX don't die on bogus blocks */ if (i == nswdev-1) break; l += sw[++i].sw_nblks; } perdev[i] += bound - s; } s = bound; } } header = getbsize(&hlen, &blocksize); if (!totalflag) (void)printf("%-11s %*s %8s %8s %8s %s\n", "Device", hlen, header, "Used", "Avail", "Capacity", "Type"); div = blocksize / 512; avail = npfree = 0; for (i = 0; i < nswdev; i++) { int xsize, xfree; if (!totalflag) (void)printf("/dev/%-6s %*d ", devname(sw[i].sw_dev, S_IFBLK), hlen, sw[i].sw_nblks / div); /* * Don't report statistics for partitions which have not * yet been activated via swapon(8). */ if (!(sw[i].sw_flags & SW_FREED)) { if (totalflag) continue; (void)printf(" *** not available for swapping ***\n"); continue; } xsize = sw[i].sw_nblks; xfree = perdev[i]; used = xsize - xfree; npfree++; avail += xsize; if (totalflag) continue; (void)printf("%8d %8d %5.0f%% %s\n", used / div, xfree / div, (double)used / (double)xsize * 100.0, (sw[i].sw_flags & SW_SEQUENTIAL) ? "Sequential" : "Interleaved"); } /* * If only one partition has been set up via swapon(8), we don't * need to bother with totals. */ used = avail - nfree; if (totalflag) { (void)printf("%dM/%dM swap space\n", used / 2048, avail / 2048); return; } if (npfree > 1) { (void)printf("%-11s %*d %8d %8d %5.0f%%\n", "Total", hlen, avail / div, used / div, nfree / div, (double)used / (double)avail * 100.0); }}voidusage(){ (void)fprintf(stderr, "usage: pstat -Tfnstv [system] [-M core] [-N system]\n"); exit(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -