xref: /freebsd/usr.bin/systat/zarc.c (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * Copyright (c) 2014
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  * 3. 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 __FBSDID("$FreeBSD$");
32 
33 #include <sys/types.h>
34 #include <sys/sysctl.h>
35 
36 #include <string.h>
37 
38 #include "systat.h"
39 #include "extern.h"
40 
41 struct zfield{
42 	uint64_t arcstats;
43 	uint64_t arcstats_demand_data;
44 	uint64_t arcstats_demand_metadata;
45 	uint64_t arcstats_prefetch_data;
46 	uint64_t arcstats_prefetch_metadata;
47 	uint64_t zfetchstats;
48 	uint64_t arcstats_l2;
49 	uint64_t vdev_cache_stats;
50 };
51 
52 static struct zarcstats {
53 	struct zfield hits;
54 	struct zfield misses;
55 } curstat, initstat, oldstat;
56 
57 static void
58 getinfo(struct zarcstats *ls);
59 
60 WINDOW *
61 openzarc(void)
62 {
63 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
64 }
65 
66 void
67 closezarc(WINDOW *w)
68 {
69 	if (w == NULL)
70 		return;
71 	wclear(w);
72 	wrefresh(w);
73 	delwin(w);
74 }
75 
76 void
77 labelzarc(void)
78 {
79 	wmove(wnd, 0, 0); wclrtoeol(wnd);
80 	mvwprintw(wnd, 0, 31+1, "%4.4s %7.7s %7.7s %12.12s %12.12s",
81 		"rate", "hits", "misses", "total hits", "total misses");
82 #define L(row, str) mvwprintw(wnd, row, 5, str); \
83 	mvwprintw(wnd, row, 31, ":"); \
84 	mvwprintw(wnd, row, 31+4, "%%")
85 	L(1, "arcstats");
86 	L(2, "arcstats.demand_data");
87 	L(3, "arcstats.demand_metadata");
88 	L(4, "arcstats.prefetch_data");
89 	L(5, "arcstats.prefetch_metadata");
90 	L(6, "zfetchstats");
91 	L(7, "arcstats.l2");
92 	L(8, "vdev_cache_stats");
93 #undef L
94 }
95 
96 static int calc(uint64_t hits, uint64_t misses)
97 {
98     if( hits )
99 	return 100 * hits / ( hits + misses );
100     else
101 	return 0;
102 }
103 
104 static void
105 domode(struct zarcstats *delta, struct zarcstats *rate)
106 {
107 #define DO(stat) \
108 	delta->hits.stat = (curstat.hits.stat - oldstat.hits.stat); \
109 	delta->misses.stat = (curstat.misses.stat - oldstat.misses.stat); \
110 	rate->hits.stat = calc(delta->hits.stat, delta->misses.stat)
111 	DO(arcstats);
112 	DO(arcstats_demand_data);
113 	DO(arcstats_demand_metadata);
114 	DO(arcstats_prefetch_data);
115 	DO(arcstats_prefetch_metadata);
116 	DO(zfetchstats);
117 	DO(arcstats_l2);
118 	DO(vdev_cache_stats);
119 	DO(arcstats);
120 	DO(arcstats_demand_data);
121 	DO(arcstats_demand_metadata);
122 	DO(arcstats_prefetch_data);
123 	DO(arcstats_prefetch_metadata);
124 	DO(zfetchstats);
125 	DO(arcstats_l2);
126 	DO(vdev_cache_stats);
127 #undef DO
128 }
129 
130 void
131 showzarc(void)
132 {
133 	struct zarcstats delta, rate;
134 
135 	memset(&delta, 0, sizeof delta);
136 	memset(&rate, 0, sizeof rate);
137 
138 	domode(&delta, &rate);
139 
140 #define DO(stat, row, col, fmt) \
141 	mvwprintw(wnd, row, col, fmt, stat)
142 #define	R(row, stat) DO(rate.hits.stat, row, 31+1, "%3lu")
143 #define	H(row, stat) DO(delta.hits.stat, row, 31+1+5, "%7lu"); \
144 	DO(curstat.hits.stat, row, 31+1+5+8+8, "%12lu")
145 #define	M(row, stat) DO(delta.misses.stat, row, 31+1+5+8, "%7lu"); \
146 	DO(curstat.misses.stat, row, 31+1+5+8+8+13, "%12lu")
147 #define	E(row, stat) R(row, stat); H(row, stat); M(row, stat);
148 	E(1, arcstats);
149 	E(2, arcstats_demand_data);
150 	E(3, arcstats_demand_metadata);
151 	E(4, arcstats_prefetch_data);
152 	E(5, arcstats_prefetch_metadata);
153 	E(6, zfetchstats);
154 	E(7, arcstats_l2);
155 	E(8, vdev_cache_stats);
156 #undef DO
157 #undef E
158 #undef M
159 #undef H
160 #undef R
161 }
162 
163 int
164 initzarc(void)
165 {
166 	getinfo(&initstat);
167 	curstat = oldstat = initstat;
168 	return 1;
169 }
170 
171 void
172 resetzarc(void)
173 {
174 	initzarc();
175 }
176 
177 static void
178 getinfo(struct zarcstats *ls)
179 {
180 	size_t size = sizeof( ls->hits.arcstats );
181 	if ( sysctlbyname("kstat.zfs.misc.arcstats.hits",
182 		&ls->hits.arcstats, &size, NULL, 0 ) != 0 )
183 		return;
184 	GETSYSCTL("kstat.zfs.misc.arcstats.misses",
185 		ls->misses.arcstats);
186 	GETSYSCTL("kstat.zfs.misc.arcstats.demand_data_hits",
187 		ls->hits.arcstats_demand_data);
188 	GETSYSCTL("kstat.zfs.misc.arcstats.demand_data_misses",
189 		ls->misses.arcstats_demand_data);
190 	GETSYSCTL("kstat.zfs.misc.arcstats.demand_metadata_hits",
191 		ls->hits.arcstats_demand_metadata);
192 	GETSYSCTL("kstat.zfs.misc.arcstats.demand_metadata_misses",
193 		ls->misses.arcstats_demand_metadata);
194 	GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_data_hits",
195 		ls->hits.arcstats_prefetch_data);
196 	GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_data_misses",
197 		ls->misses.arcstats_prefetch_data);
198 	GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_metadata_hits",
199 		ls->hits.arcstats_prefetch_metadata);
200 	GETSYSCTL("kstat.zfs.misc.arcstats.prefetch_metadata_misses",
201 		ls->misses.arcstats_prefetch_metadata);
202 	GETSYSCTL("kstat.zfs.misc.zfetchstats.hits",
203 		ls->hits.zfetchstats);
204 	GETSYSCTL("kstat.zfs.misc.zfetchstats.misses",
205 		ls->misses.zfetchstats);
206 	GETSYSCTL("kstat.zfs.misc.arcstats.l2_hits",
207 		ls->hits.arcstats_l2);
208 	GETSYSCTL("kstat.zfs.misc.arcstats.l2_misses",
209 		ls->misses.arcstats_l2);
210 	GETSYSCTL("kstat.zfs.misc.vdev_cache_stats.hits",
211 		ls->hits.vdev_cache_stats);
212 	GETSYSCTL("kstat.zfs.misc.vdev_cache_stats.misses",
213 		ls->misses.vdev_cache_stats);
214 }
215 
216 void
217 fetchzarc(void)
218 {
219 	oldstat = curstat;
220 	getinfo(&curstat);
221 }
222