Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 4.23 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * Copyright (c) 1980, 1986, 1991, 1993
  3.  *      The Regents of the University of California.  All rights reserved.
  4.  *
  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.  * 4. 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.
  16.  *
  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.
  28.  */
  29.  
  30. #include <sys/cdefs.h>
  31.  
  32. #include <sys/param.h>
  33. #include <sys/time.h>
  34. #include <sys/proc.h>
  35. #include <sys/uio.h>
  36. #include <sys/namei.h>
  37. #include <sys/malloc.h>
  38. #include <sys/signal.h>
  39. #include <sys/fcntl.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/resource.h>
  42. #include <sys/sysctl.h>
  43. #include <sys/time.h>
  44. #include <sys/vmmeter.h>
  45. #include <sys/pcpu.h>
  46.  
  47. #include <vm/vm_param.h>
  48.  
  49. #include <ctype.h>
  50. #include <devstat.h>
  51. #include <err.h>
  52. #include <errno.h>
  53. #include <kvm.h>
  54. #include <limits.h>
  55. #include <memstat.h>
  56. #include <nlist.h>
  57. #include <paths.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <sysexits.h>
  62. #include <time.h>
  63. #include <unistd.h>
  64. #include <libutil.h>
  65.  
  66. static kvm_t   *kd;
  67.  
  68. static void
  69. fill_pcpu(struct pcpu ***pcpup, int* maxcpup)
  70. {
  71.         struct pcpu **pcpu;
  72.        
  73.         int maxcpu, i;
  74.  
  75.         *pcpup = NULL;
  76.        
  77.         if (kd == NULL)
  78.                 return;
  79.  
  80.         maxcpu = kvm_getmaxcpu(kd);
  81.         if (maxcpu < 0)
  82.                 errx(1, "kvm_getmaxcpu: %s", kvm_geterr(kd));
  83.  
  84.         pcpu = calloc(maxcpu, sizeof(struct pcpu *));
  85.         if (pcpu == NULL)
  86.                 err(1, "calloc");
  87.  
  88.         for (i = 0; i < maxcpu; i++) {
  89.                 pcpu[i] = kvm_getpcpu(kd, i);
  90.                 if (pcpu[i] == (struct pcpu *)-1)
  91.                         errx(1, "kvm_getpcpu: %s", kvm_geterr(kd));
  92.         }
  93.  
  94.         *maxcpup = maxcpu;
  95.         *pcpup = pcpu;
  96. }
  97.  
  98. int main(void)
  99. {
  100.         char *memf, *nlistf;
  101.         char errbuf[_POSIX2_LINE_MAX];
  102.         struct pcpu **pcpu;
  103.         int maxcpu, i;
  104.  
  105.         memf = nlistf = NULL;
  106.         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
  107.         if (kd == NULL)
  108.                 errx(1, "kvm_openfiles: %s", errbuf);  
  109.  
  110.         fill_pcpu(&pcpu, &maxcpu);
  111.         for (i = 0; i < maxcpu; i++) {
  112.                 if (pcpu[i] == NULL)
  113.                         continue;
  114.  
  115.                 printf("cpu%d:\n", i);
  116. #define PRINT_VPARAM_PCPU(i, name) \
  117.                 printf(#name ":%d\n", pcpu[i]->pc_cnt.name)
  118.                 PRINT_VPARAM_PCPU(i, v_swtch);
  119.                 PRINT_VPARAM_PCPU(i, v_trap);
  120.                 PRINT_VPARAM_PCPU(i, v_syscall);
  121.                 PRINT_VPARAM_PCPU(i, v_intr);
  122.                 PRINT_VPARAM_PCPU(i, v_soft);
  123.                 PRINT_VPARAM_PCPU(i, v_vm_faults);
  124.                 PRINT_VPARAM_PCPU(i, v_cow_faults);
  125.                 PRINT_VPARAM_PCPU(i, v_cow_optim);
  126.                 PRINT_VPARAM_PCPU(i, v_zfod);
  127.                 PRINT_VPARAM_PCPU(i, v_ozfod);
  128.                 PRINT_VPARAM_PCPU(i, v_swapin);
  129.                 PRINT_VPARAM_PCPU(i, v_swapout);
  130.                 PRINT_VPARAM_PCPU(i, v_swappgsin);
  131.                 PRINT_VPARAM_PCPU(i, v_swappgsout);
  132.                 PRINT_VPARAM_PCPU(i, v_vnodein);
  133.                 PRINT_VPARAM_PCPU(i, v_vnodeout);
  134.                 PRINT_VPARAM_PCPU(i, v_vnodepgsin);
  135.                 PRINT_VPARAM_PCPU(i, v_vnodepgsout);
  136.                 PRINT_VPARAM_PCPU(i, v_intrans);
  137.                 PRINT_VPARAM_PCPU(i, v_tfree);
  138.                 PRINT_VPARAM_PCPU(i, v_forks);
  139.                 PRINT_VPARAM_PCPU(i, v_vforks);
  140.                 PRINT_VPARAM_PCPU(i, v_rforks);
  141.                 PRINT_VPARAM_PCPU(i, v_kthreads);
  142.                 PRINT_VPARAM_PCPU(i, v_forkpages);
  143.                 PRINT_VPARAM_PCPU(i, v_vforkpages);
  144.                 PRINT_VPARAM_PCPU(i, v_rforkpages);
  145.                 PRINT_VPARAM_PCPU(i, v_kthreadpages);
  146. #undef PRINT_VPARAM_PCPU
  147.                 printf("\n\n");
  148.         }
  149. }