2 * Copyright (c) 2003-2004. Hiten Pandya <hmp@backplane.com>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY HITEN PANDYA AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL HITEN PANDYA OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $DragonFly: src/test/pcpu/ncache-stats.c,v 1.1 2004/04/02 05:46:03 hmp Exp $
33 #include <sys/param.h>
34 #include <sys/nchstats.h>
35 #include <sys/sysctl.h>
43 #define _NCH_ENT(t, n, tt) \
45 for (i = 1; i <= ncpus; ++i) { \
46 printf("%-9ld%s", nch[i-1]. ## n, "\t"); \
48 printf("(%-9ld)\n", tt. ## n); \
52 * Aggregate the per-cpu counters we retrieved via sysctl(2)
53 * to give the total across the CPUs. Use a nasty trick to
54 * aggregate the counters in the structure! YYY
58 nch_cpuagg(struct nchstats *unagg, struct nchstats *ttl, int cpucnt)
61 siz = sizeof(struct nchstats);
68 /* kick hmp@ for this nasty loop! :-) */
69 for (i = 0; i < cpucnt; ++i) {
70 for (off = 0; off < siz; off += sizeof(u_long)) {
71 *(u_long *)((char *)(*(&ttl)) + off) +=
72 *(u_long *)((char *)&unagg[i] + off);
81 struct nchstats *nch, total;
82 size_t nch_len = SMP_MAXCPU * sizeof(struct nchstats);
85 nch = malloc(nch_len);
89 /* retrieve the statistics */
90 if (sysctlbyname("vfs.cache.nchstats", nch, &nch_len, NULL, 0) < 0) {
94 nch = realloc(nch, nch_len);
99 ncpus = nch_len / sizeof(struct nchstats);
102 printf("Number of processors = %d\n", ncpus);
105 kvm_nch_cpuagg(nch, &total, ncpus);
106 nchtotal = total.ncs_goodhits + total.ncs_neghits +
107 total.ncs_badhits + total.ncs_falsehits +
108 total.ncs_miss + total.ncs_long;
110 printf("VFS Name Cache Effectiveness Statistics\n");
111 printf("%9ld total name lookups\n",nchtotal);
113 printf("%-20s", "COUNTER");
115 for (i = 1; i <= ncpus; ++i) {
116 printf("%3s-%-2d%s", "CPU", i, "\t");
118 printf("\t%-9s\n", "TOTAL");
121 _NCH_ENT("goodhits", ncs_goodhits, total);
122 _NCH_ENT("neghits", ncs_neghits, total);
123 _NCH_ENT("badhits", ncs_badhits, total);
124 _NCH_ENT("falsehits", ncs_falsehits, total);
125 _NCH_ENT("misses", ncs_miss, total);
126 _NCH_ENT("longnames", ncs_long, total);
127 _NCH_ENT("passes 2", ncs_pass2, total);
128 _NCH_ENT("2-passes", ncs_2passes, total);