static void dovmstat(unsigned int interval, int reps) { struct vmtotal total; time_t uptime, halfuptime; struct devinfo *tmp_dinfo; size_t size; int ncpus, maxid; u_long cpumask; int rate_adj; uptime = getuptime() / 1000000000LL; halfuptime = uptime / 2; rate_adj = 1; ncpus = 1; maxid = 0; /* * If the user stops the program (control-Z) and then resumes it, * print out the header again. */ (void)signal(SIGCONT, needhdr); /* * If our standard output is a tty, then install a SIGWINCH handler * and set wresized so that our first iteration through the main * vmstat loop will peek at the terminal's current rows to find out * how many lines can fit in a screenful of output. */ if (isatty(fileno(stdout)) != 0) { wresized = 1; (void)signal(SIGWINCH, needresize); } else { wresized = 0; winlines = VMSTAT_DEFAULT_LINES; } if (kd != NULL) { if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0) kread(X_STATHZ, &hz, sizeof(hz)); if (!hz) kread(X_HZ, &hz, sizeof(hz)); } else { struct clockinfo clockrate; size = sizeof(clockrate); mysysctl("kern.clockrate", &clockrate, &size, NULL, 0); if (size != sizeof(clockrate)) errx(1, "clockrate size mismatch"); hz = clockrate.hz; } if (Pflag) { ncpus = getcpuinfo(&cpumask, &maxid); size_cp_times = sizeof(long) * (maxid + 1) * CPUSTATES; cur_cp_times = calloc(1, size_cp_times); last_cp_times = calloc(1, size_cp_times); } for (hdrcnt = 1;;) { if (!--hdrcnt) printhdr(maxid, cpumask); if (kd != NULL) { if (kvm_getcptime(kd, cur.cp_time) < 0) errx(1, "kvm_getcptime: %s", kvm_geterr(kd)); } else { size = sizeof(cur.cp_time); mysysctl("kern.cp_time", &cur.cp_time, &size, NULL, 0); if (size != sizeof(cur.cp_time)) errx(1, "cp_time size mismatch"); } if (Pflag) { size = size_cp_times; mysysctl("kern.cp_times", cur_cp_times, &size, NULL, 0); if (size != size_cp_times) errx(1, "cp_times mismatch"); } tmp_dinfo = last.dinfo; last.dinfo = cur.dinfo; cur.dinfo = tmp_dinfo; last.snap_time = cur.snap_time; /* * Here what we want to do is refresh our device stats. * getdevs() returns 1 when the device list has changed. * If the device list has changed, we want to go through * the selection process again, in case a device that we * were previously displaying has gone away. */ switch (devstat_getdevs(NULL, &cur)) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: { int retval; num_devices = cur.dinfo->numdevs; generation = cur.dinfo->generation; retval = devstat_selectdevs(&dev_select, &num_selected, &num_selections, &select_generation, generation, cur.dinfo->devices, num_devices, matches, num_matches, specified_devices, num_devices_specified, select_mode, maxshowdevs, 0); switch (retval) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: printhdr(maxid, cpumask); break; default: break; } } default: break; } fill_vmmeter(&sum); fill_vmtotal(&total); (void)printf("%1d %1d %1d", total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); #define vmstat_pgtok(a) ((a) * (sum.v_page_size >> 10)) #define rate(x) (((x) * rate_adj + halfuptime) / uptime) /* round */ if (hflag) { printf(""); prthuman(total.t_avm * (u_int64_t)sum.v_page_size, 5); printf(" "); prthuman(total.t_free * (u_int64_t)sum.v_page_size, 5); printf(" "); (void)printf("%5lu ", (unsigned long)rate(sum.v_vm_faults - osum.v_vm_faults)); } else { printf(" %7d", vmstat_pgtok(total.t_avm)); printf(" %7d ", vmstat_pgtok(total.t_free)); (void)printf("%4lu ", (unsigned long)rate(sum.v_vm_faults - osum.v_vm_faults)); } (void)printf("%3lu ", (unsigned long)rate(sum.v_reactivated - osum.v_reactivated)); (void)printf("%3lu ", (unsigned long)rate(sum.v_swapin + sum.v_vnodein - (osum.v_swapin + osum.v_vnodein))); (void)printf("%3lu ", (unsigned long)rate(sum.v_swapout + sum.v_vnodeout - (osum.v_swapout + osum.v_vnodeout))); (void)printf("%5lu ", (unsigned long)rate(sum.v_tfree - osum.v_tfree)); (void)printf("%4lu ", (unsigned long)rate(sum.v_pdpages - osum.v_pdpages)); devstats(); (void)printf("%4lu %5lu %5lu", (unsigned long)rate(sum.v_intr - osum.v_intr), (unsigned long)rate(sum.v_syscall - osum.v_syscall), (unsigned long)rate(sum.v_swtch - osum.v_swtch)); if (Pflag) pcpustats(ncpus, cpumask, maxid); else cpustats(); (void)printf("\n"); (void)fflush(stdout); if (reps >= 0 && --reps <= 0) break; osum = sum; uptime = interval; rate_adj = 1000; /* * We round upward to avoid losing low-frequency events * (i.e., >= 1 per interval but < 1 per millisecond). */ if (interval != 1) halfuptime = (uptime + 1) / 2; else halfuptime = 0; (void)usleep(interval * 1000); } }
static void dovmstat(u_int interval, int reps) { struct vmtotal total; struct devinfo *tmp_dinfo; size_t vmm_size = sizeof(vmm); size_t vms_size = sizeof(vms); size_t vmt_size = sizeof(total); int initial = 1; signal(SIGCONT, needhdr); for (hdrcnt = 1;;) { if (!--hdrcnt) printhdr(); if (kinfo_get_sched_cputime(&cp_time)) err(1, "kinfo_get_sched_cputime"); tmp_dinfo = last.dinfo; last.dinfo = cur.dinfo; cur.dinfo = tmp_dinfo; last.busy_time = cur.busy_time; /* * Here what we want to do is refresh our device stats. * getdevs() returns 1 when the device list has changed. * If the device list has changed, we want to go through * the selection process again, in case a device that we * were previously displaying has gone away. */ switch (getdevs(&cur)) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: { int retval; num_devices = cur.dinfo->numdevs; generation = cur.dinfo->generation; retval = selectdevs(&dev_select, &num_selected, &num_selections, &select_generation, generation, cur.dinfo->devices, num_devices, matches, num_matches, specified_devices, num_devices_specified, select_mode, maxshowdevs, 0); switch (retval) { case -1: errx(1, "%s", devstat_errbuf); break; case 1: printhdr(); break; default: break; } } default: break; } if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) { perror("sysctlbyname: vm.vmstats"); exit(1); } if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) { perror("sysctlbyname: vm.vmmeter"); exit(1); } if (sysctlbyname("vm.vmtotal", &total, &vmt_size, NULL, 0)) { perror("sysctlbyname: vm.vmtotal"); exit(1); } printf("%2ld %1ld %1ld", total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); #define vmstat_pgtok(a) \ (intmax_t)(((intmax_t)(a) * vms.v_page_size) >> 10) #define rate(x) \ (intmax_t)(initial ? (x) : ((intmax_t)(x) * 1000 + interval / 2) \ / interval) printf(" %7jd %6jd ", vmstat_pgtok(total.t_avm), vmstat_pgtok(total.t_free)); printf("%4ju ", rate(vmm.v_vm_faults - ovmm.v_vm_faults)); printf("%3ju ", rate(vmm.v_reactivated - ovmm.v_reactivated)); printf("%3ju ", rate(vmm.v_swapin + vmm.v_vnodein - (ovmm.v_swapin + ovmm.v_vnodein))); printf("%3ju ", rate(vmm.v_swapout + vmm.v_vnodeout - (ovmm.v_swapout + ovmm.v_vnodeout))); printf("%3ju ", rate(vmm.v_tfree - ovmm.v_tfree)); printf("%3ju ", rate(vmm.v_pdpages - ovmm.v_pdpages)); devstats(); printf("%4ju %4ju %3ju ", rate(vmm.v_intr - ovmm.v_intr), rate(vmm.v_syscall - ovmm.v_syscall), rate(vmm.v_swtch - ovmm.v_swtch)); cpustats(); printf("\n"); fflush(stdout); if (reps >= 0 && --reps <= 0) break; ovmm = vmm; usleep(interval * 1000); initial = 0; } }
/* * Print a running summary of nfs statistics. * Repeat display every interval seconds, showing statistics * collected over that interval. Assumes that interval is non-zero. * First line printed at top of screen is always cumulative. */ void sidewaysintpr(u_int interval, int clientOnly, int serverOnly) { struct nfsstats nfsstats, lastst, *nfsstatsp; struct nfsrvstats nfsrvstats, lastsrvst, *nfsrvstatsp; int hdrcnt = 1; nfsstatsp = &lastst; nfsrvstatsp = &lastsrvst; readstats(&nfsstatsp, &nfsrvstatsp, 0); if (clientOnly && !nfsstatsp) { printf("Client not present!\n"); clientOnly = 0; } if (serverOnly && !nfsrvstatsp) { printf("Server not present!\n"); serverOnly = 0; } sleep(interval); for (;;) { nfsstatsp = &nfsstats; nfsrvstatsp = &nfsrvstats; readstats(&nfsstatsp, &nfsrvstatsp, 0); if (--hdrcnt == 0) { printhdr(clientOnly, serverOnly); if (clientOnly && serverOnly) hdrcnt = 10; else hdrcnt = 20; } if (clientOnly) { printf("%s %6d %6d %6d %6d %6d %6d %6d %6d", ((clientOnly && serverOnly) ? "Client:" : ""), DELTA(attrcache_hits) + DELTA(attrcache_misses), DELTA(lookupcache_hits) + DELTA(lookupcache_misses), DELTA(biocache_readlinks), DELTA(biocache_reads), DELTA(biocache_writes), nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME], DELTA(accesscache_hits) + DELTA(accesscache_misses), DELTA(biocache_readdirs) ); if (widemode) { printf(" %s %s %s %s %s %s", sperc1(DELTA(attrcache_hits), DELTA(attrcache_misses)), sperc1(DELTA(lookupcache_hits), DELTA(lookupcache_misses)), sperc2(DELTA(biocache_reads), DELTA(read_bios)), sperc2(DELTA(biocache_writes), DELTA(write_bios)), sperc1(DELTA(accesscache_hits), DELTA(accesscache_misses)), sperc2(DELTA(biocache_readdirs), DELTA(readdir_bios)) ); } printf("\n"); lastst = nfsstats; } if (serverOnly) { printf("%s %6d %6d %6d %6d %6d %6d %6d %6d", ((clientOnly && serverOnly) ? "Server:" : ""), nfsrvstats.srvrpccnt[NFSPROC_GETATTR]-lastsrvst.srvrpccnt[NFSPROC_GETATTR], nfsrvstats.srvrpccnt[NFSPROC_LOOKUP]-lastsrvst.srvrpccnt[NFSPROC_LOOKUP], nfsrvstats.srvrpccnt[NFSPROC_READLINK]-lastsrvst.srvrpccnt[NFSPROC_READLINK], nfsrvstats.srvrpccnt[NFSPROC_READ]-lastsrvst.srvrpccnt[NFSPROC_READ], nfsrvstats.srvrpccnt[NFSPROC_WRITE]-lastsrvst.srvrpccnt[NFSPROC_WRITE], nfsrvstats.srvrpccnt[NFSPROC_RENAME]-lastsrvst.srvrpccnt[NFSPROC_RENAME], nfsrvstats.srvrpccnt[NFSPROC_ACCESS]-lastsrvst.srvrpccnt[NFSPROC_ACCESS], (nfsrvstats.srvrpccnt[NFSPROC_READDIR]-lastsrvst.srvrpccnt[NFSPROC_READDIR]) +(nfsrvstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastsrvst.srvrpccnt[NFSPROC_READDIRPLUS])); printf("\n"); lastsrvst = nfsrvstats; } fflush(stdout); sleep(interval); } /*NOTREACHED*/ }
/* * Print a running summary of nfs statistics for the experimental client and/or * server. * Repeat display every interval seconds, showing statistics * collected over that interval. Assumes that interval is non-zero. * First line printed at top of screen is always cumulative. */ void exp_sidewaysintpr(u_int interval, int clientOnly, int serverOnly) { struct ext_nfsstats nfsstats, lastst, *ext_nfsstatsp; int hdrcnt = 1; ext_nfsstatsp = &lastst; if (nfssvc(NFSSVC_GETSTATS, ext_nfsstatsp) < 0) err(1, "Can't get stats"); sleep(interval); for (;;) { ext_nfsstatsp = &nfsstats; if (nfssvc(NFSSVC_GETSTATS, ext_nfsstatsp) < 0) err(1, "Can't get stats"); if (--hdrcnt == 0) { printhdr(clientOnly, serverOnly); if (clientOnly && serverOnly) hdrcnt = 10; else hdrcnt = 20; } if (clientOnly) { printf("%s %6d %6d %6d %6d %6d %6d %6d %6d", ((clientOnly && serverOnly) ? "Client:" : ""), DELTA(attrcache_hits) + DELTA(attrcache_misses), DELTA(lookupcache_hits) + DELTA(lookupcache_misses), DELTA(biocache_readlinks), DELTA(biocache_reads), DELTA(biocache_writes), nfsstats.rpccnt[NFSPROC_RENAME] - lastst.rpccnt[NFSPROC_RENAME], DELTA(accesscache_hits) + DELTA(accesscache_misses), DELTA(biocache_readdirs) ); if (widemode) { printf(" %s %s %s %s %s %s", sperc1(DELTA(attrcache_hits), DELTA(attrcache_misses)), sperc1(DELTA(lookupcache_hits), DELTA(lookupcache_misses)), sperc2(DELTA(biocache_reads), DELTA(read_bios)), sperc2(DELTA(biocache_writes), DELTA(write_bios)), sperc1(DELTA(accesscache_hits), DELTA(accesscache_misses)), sperc2(DELTA(biocache_readdirs), DELTA(readdir_bios)) ); } printf("\n"); lastst = nfsstats; } if (serverOnly) { printf("%s %6d %6d %6d %6d %6d %6d %6d %6d", ((clientOnly && serverOnly) ? "Server:" : ""), nfsstats.srvrpccnt[NFSV4OP_GETATTR] - lastst.srvrpccnt[NFSV4OP_GETATTR], nfsstats.srvrpccnt[NFSV4OP_LOOKUP] - lastst.srvrpccnt[NFSV4OP_LOOKUP], nfsstats.srvrpccnt[NFSV4OP_READLINK] - lastst.srvrpccnt[NFSV4OP_READLINK], nfsstats.srvrpccnt[NFSV4OP_READ] - lastst.srvrpccnt[NFSV4OP_READ], nfsstats.srvrpccnt[NFSV4OP_WRITE] - lastst.srvrpccnt[NFSV4OP_WRITE], nfsstats.srvrpccnt[NFSV4OP_RENAME] - lastst.srvrpccnt[NFSV4OP_RENAME], nfsstats.srvrpccnt[NFSV4OP_ACCESS] - lastst.srvrpccnt[NFSV4OP_ACCESS], (nfsstats.srvrpccnt[NFSV4OP_READDIR] - lastst.srvrpccnt[NFSV4OP_READDIR]) + (nfsstats.srvrpccnt[NFSV4OP_READDIRPLUS] - lastst.srvrpccnt[NFSV4OP_READDIRPLUS])); printf("\n"); lastst = nfsstats; } fflush(stdout); sleep(interval); } /*NOTREACHED*/ }
void dovmstat(u_int interval, int reps) { time_t uptime, halfuptime; struct clockinfo clkinfo; struct vmtotal total; size_t size; int mib[2]; uptime = getuptime(); halfuptime = uptime / 2; (void)signal(SIGCONT, needhdr); mib[0] = CTL_KERN; mib[1] = KERN_CLOCKRATE; size = sizeof(clkinfo); if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) { warn("could not read kern.clockrate"); return; } hz = clkinfo.stathz; for (hdrcnt = 1;;) { /* Read new disk statistics */ dkreadstats(); if (!--hdrcnt || last.dk_ndrive != cur.dk_ndrive) printhdr(); if (nlistf == NULL && memf == NULL) { size = sizeof(struct uvmexp); mib[0] = CTL_VM; mib[1] = VM_UVMEXP; if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) { warn("could not get vm.uvmexp"); bzero(&uvmexp, sizeof(struct uvmexp)); } } else { kread(X_UVMEXP, &uvmexp, sizeof(struct uvmexp)); } size = sizeof(total); mib[0] = CTL_VM; mib[1] = VM_METER; if (sysctl(mib, 2, &total, &size, NULL, 0) < 0) { warn("could not read vm.vmmeter"); bzero(&total, sizeof(total)); } (void)printf(" %u %u %u ", total.t_rq - 1, total.t_dw + total.t_pw, total.t_sw); #define rate(x) ((unsigned)((((unsigned)x) + halfuptime) / uptime)) /* round */ #define pgtok(a) ((a) * ((unsigned int)uvmexp.pagesize >> 10)) (void)printf("%6u %7u ", pgtok(uvmexp.active + uvmexp.swpginuse), pgtok(uvmexp.free)); (void)printf("%4u ", rate(uvmexp.faults - ouvmexp.faults)); (void)printf("%3u ", rate(uvmexp.pdreact - ouvmexp.pdreact)); (void)printf("%3u ", rate(uvmexp.pageins - ouvmexp.pageins)); (void)printf("%3u %3u ", rate(uvmexp.pdpageouts - ouvmexp.pdpageouts), 0); (void)printf("%3u ", rate(uvmexp.pdscans - ouvmexp.pdscans)); dkstats(); (void)printf("%4u %5u %4u ", rate(uvmexp.intrs - ouvmexp.intrs), rate(uvmexp.syscalls - ouvmexp.syscalls), rate(uvmexp.swtch - ouvmexp.swtch)); cpustats(); (void)printf("\n"); (void)fflush(stdout); if (reps >= 0 && --reps <= 0) break; ouvmexp = uvmexp; uptime = interval; /* * We round upward to avoid losing low-frequency events * (i.e., >= 1 per interval but < 1 per second). */ halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2; (void)sleep(interval); } }