xref: /dragonfly/test/pcpu/ncache-stats.c (revision e8364298)
1 /*
2  * Copyright (c) 2003, 2004 Hiten Pandya <hmp@backplane.com>.
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following
12  *    disclaimer.
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
16  *    distribution.
17  *
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.
29  *
30  * $DragonFly: src/test/pcpu/ncache-stats.c,v 1.2 2004/06/28 02:57:11 drhodus Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/nchstats.h>
35 #include <sys/sysctl.h>
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <strings.h>
40 #include <err.h>
41 #include <kvm.h>
42 
43 #define _NCH_ENT(t, n, tt)                       \
44     printf("%-20s", t);                          \
45     for (i = 1; i <= ncpus; ++i) {               \
46         printf("%-9ld%s", nch[i-1]. ## n, "\t"); \
47 		if (i == ncpus)                          \
48 			printf("(%-9ld)\n", tt. ## n);       \
49 	}                                            \
50 
51 /*
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
55  */
56 #if 0
57 static void
58 nch_cpuagg(struct nchstats *unagg, struct nchstats *ttl, int cpucnt)
59 {
60 	int i, off, siz;
61 	siz = sizeof(struct nchstats);
62 
63 	if (!unagg && !ttl)
64 		return;
65 
66 	bzero(ttl, siz);
67 
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);
73 		}
74 	}
75 }
76 #endif
77 
78 int main(void)
79 {
80 	int i, ncpus;
81 	struct nchstats *nch, total;
82 	size_t nch_len = SMP_MAXCPU * sizeof(struct nchstats);
83 	u_long nchtotal;
84 
85 	nch = malloc(nch_len);
86 	if (!nch)
87 		exit(-1);
88 
89 	/* retrieve the statistics */
90 	if (sysctlbyname("vfs.cache.nchstats", nch, &nch_len, NULL, 0) < 0) {
91 		warn("sysctl");
92 		exit(-1);
93 	} else {
94 		nch = realloc(nch, nch_len);
95 		if (!nch)
96 			exit(-1);
97 	}
98 
99 	ncpus = nch_len / sizeof(struct nchstats);
100 
101 #if defined(DEBUG)
102 	printf("Number of processors = %d\n", ncpus);
103 #endif
104 
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;
109 
110 	printf("VFS Name Cache Effectiveness Statistics\n");
111 	printf("%9ld total name lookups\n",nchtotal);
112 
113 	printf("%-20s", "COUNTER");
114 
115 	for (i = 1; i <= ncpus; ++i) {
116 		printf("%3s-%-2d%s", "CPU", i, "\t");
117 		if (i == ncpus)
118 			printf("\t%-9s\n", "TOTAL");
119 	}
120 
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);
129 
130 	free(nch);
131 
132 	return 0;
133 }
134