Skip to content

Instantly share code, notes, and snippets.

@syuu1228
Created August 7, 2011 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syuu1228/1129995 to your computer and use it in GitHub Desktop.
Save syuu1228/1129995 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 1980, 1986, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/namei.h>
#include <sys/malloc.h>
#include <sys/signal.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/vmmeter.h>
#include <sys/pcpu.h>
#include <vm/vm_param.h>
#include <ctype.h>
#include <devstat.h>
#include <err.h>
#include <errno.h>
#include <kvm.h>
#include <limits.h>
#include <memstat.h>
#include <nlist.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>
#include <libutil.h>
static int mysysctl(const char *, void *, size_t *, void *, size_t);
static int printintrname(int **);
int
main(int argc, char *argv[])
{
unsigned long *intrcnt_start, *intrcnt_end;
int nintr, i;
size_t intrcntlen;
int sec;
int *intrmask;
if (argc < 2) {
printf("%s [sec]\n", argv[0]);
exit(1);
}
sec = atoi(argv[1]);
nintr = printintrname(&intrmask);
intrcnt_start = malloc(nintr * sizeof(unsigned long));
if (!intrcnt_start)
err(1, "malloc");
intrcnt_end = malloc(nintr * sizeof(unsigned long));
if (!intrcnt_end)
err(1, "malloc");
intrcntlen = nintr * sizeof(unsigned long);
while(1) {
if (mysysctl("hw.intrcnt", intrcnt_start, &intrcntlen, NULL, 0))
err(1, "hw.intrcnt");
sleep(sec);
if (mysysctl("hw.intrcnt", intrcnt_end, &intrcntlen, NULL, 0))
err(1, "hw.intrcnt");
for (i = 0; i < nintr; i++, intrcnt_start++, intrcnt_end++) {
if (intrmask[i])
(void)printf("%lu,", *intrcnt_end - *intrcnt_start);
}
printf("\n");
}
exit(0);
}
static int
mysysctl(const char *name, void *oldp, size_t *oldlenp,
void *newp, size_t newlen)
{
int error;
error = sysctlbyname(name, oldp, oldlenp, newp, newlen);
if (error != 0 && errno != ENOMEM)
err(1, "sysctl(%s)", name);
return (error);
}
static int
printintrname(int **intrmaskp)
{
unsigned long *intrcnt;
size_t clen, inamlen, intrcntlen, istrnamlen;
unsigned int i, nintr;
char *intrname, *tintrname;
int *intrmask;
for (intrcnt = NULL, intrcntlen = 1024; ; intrcntlen *= 2) {
if ((intrcnt = reallocf(intrcnt, intrcntlen)) == NULL)
err(1, "reallocf()");
if (mysysctl("hw.intrcnt",
intrcnt, &intrcntlen, NULL, 0) == 0)
break;
}
for (intrname = NULL, inamlen = 1024; ; inamlen *= 2) {
if ((intrname = reallocf(intrname, inamlen)) == NULL)
err(1, "reallocf()");
if (mysysctl("hw.intrnames",
intrname, &inamlen, NULL, 0) == 0)
break;
}
nintr = intrcntlen / sizeof(unsigned long);
intrmask = calloc(nintr, sizeof(int));
tintrname = intrname;
istrnamlen = 0;
for (i = 0; i < nintr; i++) {
clen = strlen(tintrname);
if (clen > istrnamlen)
istrnamlen = clen;
tintrname += clen + 1;
}
for (i = 0; i < nintr; i++, intrcnt++) {
if (intrname[0] != '\0' && (*intrcnt != 0)) {
(void)printf("%-*s,", (int)istrnamlen, intrname);
intrmask[i] = 1;
}else
intrmask[i] = 0;
intrname += strlen(intrname) + 1;
}
printf("\n");
*intrmaskp = intrmask;
return nintr;
}
/*
* Copyright (c) 1980, 1986, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/namei.h>
#include <sys/malloc.h>
#include <sys/signal.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/vmmeter.h>
#include <sys/pcpu.h>
#include <vm/vm_param.h>
#include <ctype.h>
#include <devstat.h>
#include <err.h>
#include <errno.h>
#include <kvm.h>
#include <limits.h>
#include <memstat.h>
#include <nlist.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>
#include <libutil.h>
static kvm_t *kd;
#define PRINT_VPARAM_NAME(name) \
printf(#name",")
#define PRINT_VPARAM_PCPU(i, name) \
printf("%d,", pcpu_end[i]->pc_cnt.name - pcpu_start[i]->pc_cnt.name)
static void
fill_pcpu(struct pcpu **pcpu, int maxcpu)
{
int i;
for (i = 0; i < maxcpu; i++) {
pcpu[i] = kvm_getpcpu(kd, i);
if (pcpu[i] == (struct pcpu *)-1)
errx(1, "kvm_getpcpu: %s", kvm_geterr(kd));
}
}
int main(int argc, char **argv)
{
char *memf, *nlistf;
char errbuf[_POSIX2_LINE_MAX];
struct pcpu **pcpu_start, **pcpu_end;
int maxcpu, i;
int sec;
if (argc < 2) {
printf("%s [sec]\n", argv[0]);
exit(1);
}
sec = atoi(argv[1]);
memf = nlistf = NULL;
kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
if (kd == NULL)
errx(1, "kvm_openfiles: %s", errbuf);
maxcpu = kvm_getmaxcpu(kd);
if (maxcpu < 0)
errx(1, "kvm_getmaxcpu: %s", kvm_geterr(kd));
pcpu_start = calloc(maxcpu, sizeof(struct pcpu *));
if (pcpu_start == NULL)
err(1, "calloc");
pcpu_end = calloc(maxcpu, sizeof(struct pcpu *));
if (pcpu_end == NULL)
err(1, "calloc");
printf("cpu,");
PRINT_VPARAM_NAME(v_swtch);
PRINT_VPARAM_NAME(v_trap);
PRINT_VPARAM_NAME(v_syscall);
PRINT_VPARAM_NAME(v_intr);
PRINT_VPARAM_NAME(v_soft);
PRINT_VPARAM_NAME(v_vm_faults);
PRINT_VPARAM_NAME(v_cow_faults);
PRINT_VPARAM_NAME(v_cow_optim);
PRINT_VPARAM_NAME(v_zfod);
PRINT_VPARAM_NAME(v_ozfod);
PRINT_VPARAM_NAME(v_swapin);
PRINT_VPARAM_NAME(v_swapout);
PRINT_VPARAM_NAME(v_swappgsin);
PRINT_VPARAM_NAME(v_swappgsout);
PRINT_VPARAM_NAME(v_vnodein);
PRINT_VPARAM_NAME(v_vnodeout);
PRINT_VPARAM_NAME(v_vnodepgsin);
PRINT_VPARAM_NAME(v_vnodepgsout);
PRINT_VPARAM_NAME(v_intrans);
PRINT_VPARAM_NAME(v_tfree);
PRINT_VPARAM_NAME(v_forks);
PRINT_VPARAM_NAME(v_vforks);
PRINT_VPARAM_NAME(v_rforks);
PRINT_VPARAM_NAME(v_kthreads);
PRINT_VPARAM_NAME(v_forkpages);
PRINT_VPARAM_NAME(v_vforkpages);
PRINT_VPARAM_NAME(v_rforkpages);
PRINT_VPARAM_NAME(v_kthreadpages);
printf("\n");
while(1) {
fill_pcpu(pcpu_start, maxcpu);
sleep(sec);
fill_pcpu(pcpu_end, maxcpu);
for (i = 0; i < maxcpu; i++) {
if (pcpu_start[i] == NULL || pcpu_end[i] == NULL)
continue;
printf("%d,", i);
PRINT_VPARAM_PCPU(i, v_swtch);
PRINT_VPARAM_PCPU(i, v_trap);
PRINT_VPARAM_PCPU(i, v_syscall);
PRINT_VPARAM_PCPU(i, v_intr);
PRINT_VPARAM_PCPU(i, v_soft);
PRINT_VPARAM_PCPU(i, v_vm_faults);
PRINT_VPARAM_PCPU(i, v_cow_faults);
PRINT_VPARAM_PCPU(i, v_cow_optim);
PRINT_VPARAM_PCPU(i, v_zfod);
PRINT_VPARAM_PCPU(i, v_ozfod);
PRINT_VPARAM_PCPU(i, v_swapin);
PRINT_VPARAM_PCPU(i, v_swapout);
PRINT_VPARAM_PCPU(i, v_swappgsin);
PRINT_VPARAM_PCPU(i, v_swappgsout);
PRINT_VPARAM_PCPU(i, v_vnodein);
PRINT_VPARAM_PCPU(i, v_vnodeout);
PRINT_VPARAM_PCPU(i, v_vnodepgsin);
PRINT_VPARAM_PCPU(i, v_vnodepgsout);
PRINT_VPARAM_PCPU(i, v_intrans);
PRINT_VPARAM_PCPU(i, v_tfree);
PRINT_VPARAM_PCPU(i, v_forks);
PRINT_VPARAM_PCPU(i, v_vforks);
PRINT_VPARAM_PCPU(i, v_rforks);
PRINT_VPARAM_PCPU(i, v_kthreads);
PRINT_VPARAM_PCPU(i, v_forkpages);
PRINT_VPARAM_PCPU(i, v_vforkpages);
PRINT_VPARAM_PCPU(i, v_rforkpages);
PRINT_VPARAM_PCPU(i, v_kthreadpages);
printf("\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment