bufcache.c revision 1.18 1 /* $NetBSD: bufcache.c,v 1.18 2004/11/23 03:11:33 simonb Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: bufcache.c,v 1.18 2004/11/23 03:11:33 simonb Exp $");
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/buf.h>
46 #include <sys/mount.h>
47 #include <sys/sysctl.h>
48 #include <sys/vnode.h>
49
50 #include <uvm/uvm_extern.h>
51
52 #include <err.h>
53 #include <errno.h>
54 #include <inttypes.h>
55 #include <kvm.h>
56 #include <math.h>
57 #include <nlist.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 #include <miscfs/specfs/specdev.h>
63
64 #include "systat.h"
65 #include "extern.h"
66
67 #define VCACHE_SIZE 50
68 #define PAGEINFO_ROWS 5
69
70 struct vcache {
71 int vc_age;
72 struct vnode *vc_addr;
73 struct vnode vc_node;
74 };
75
76 struct ml_entry {
77 u_int ml_count;
78 u_long ml_size;
79 u_long ml_valid;
80 struct mount *ml_addr;
81 LIST_ENTRY(ml_entry) ml_entries;
82 struct mount ml_mount;
83 };
84
85 static struct nlist namelist[] = {
86 #define X_BUFMEM 0
87 { "_bufmem" },
88 { "" },
89 };
90
91 static struct vcache vcache[VCACHE_SIZE];
92 static LIST_HEAD(mount_list, ml_entry) mount_list;
93
94 static u_long bufmem;
95 static u_int nbuf, pgwidth, kbwidth;
96 static struct uvmexp_sysctl uvmexp;
97
98 static void vc_init(void);
99 static void ml_init(void);
100 static struct vnode *vc_lookup(struct vnode *);
101 static struct mount *ml_lookup(struct mount *, int, int);
102 static void fetchuvmexp(void);
103
104
105 WINDOW *
106 openbufcache(void)
107 {
108
109 return (subwin(stdscr, -1, 0, 5, 0));
110 }
111
112 void
113 closebufcache(WINDOW *w)
114 {
115
116 if (w == NULL)
117 return;
118 wclear(w);
119 wrefresh(w);
120 delwin(w);
121 ml_init(); /* Clear out mount list */
122 }
123
124 void
125 labelbufcache(void)
126 {
127 int i;
128
129 for (i = 0; i <= PAGEINFO_ROWS; i++) {
130 wmove(wnd, i, 0);
131 wclrtoeol(wnd);
132 }
133 mvwaddstr(wnd, PAGEINFO_ROWS + 1, 0, "File System Bufs used"
134 " % kB in use % Bufsize kB % Util %");
135 wclrtoeol(wnd);
136 }
137
138 void
139 showbufcache(void)
140 {
141 int tbuf, i, lastrow;
142 double tvalid, tsize;
143 struct ml_entry *ml;
144
145 NREAD(X_BUFMEM, &bufmem, sizeof(bufmem));
146
147 mvwprintw(wnd, 0, 0,
148 " %*d metadata buffers using %*ld kBytes of "
149 "memory (%2.0f%%).",
150 pgwidth, nbuf, kbwidth, bufmem / 1024,
151 ((bufmem * 100.0) + 0.5) / getpagesize() / uvmexp.npages);
152 wclrtoeol(wnd);
153 mvwprintw(wnd, 1, 0,
154 " %*" PRIu64 " pages for cached file data using %*"
155 PRIu64 " kBytes of memory (%2.0f%%).",
156 pgwidth, uvmexp.filepages,
157 kbwidth, uvmexp.filepages * getpagesize() / 1024,
158 (uvmexp.filepages * 100 + 0.5) / uvmexp.npages);
159 wclrtoeol(wnd);
160 mvwprintw(wnd, 2, 0,
161 " %*" PRIu64 " pages for executables using %*"
162 PRIu64 " kBytes of memory (%2.0f%%).",
163 pgwidth, uvmexp.execpages,
164 kbwidth, uvmexp.execpages * getpagesize() / 1024,
165 (uvmexp.execpages * 100 + 0.5) / uvmexp.npages);
166 wclrtoeol(wnd);
167 mvwprintw(wnd, 3, 0,
168 " %*" PRIu64 " pages for anon (non-file) data %*"
169 PRIu64 " kBytes of memory (%2.0f%%).",
170 pgwidth, uvmexp.anonpages,
171 kbwidth, uvmexp.anonpages * getpagesize() / 1024,
172 (uvmexp.anonpages * 100 + 0.5) / uvmexp.npages);
173 wclrtoeol(wnd);
174 mvwprintw(wnd, 4, 0,
175 " %*" PRIu64 " free pages %*"
176 PRIu64 " kBytes of memory (%2.0f%%).",
177 pgwidth, uvmexp.free,
178 kbwidth, uvmexp.free * getpagesize() / 1024,
179 (uvmexp.free * 100 + 0.5) / uvmexp.npages);
180 wclrtoeol(wnd);
181
182 if (nbuf == 0 || bufmem == 0) {
183 wclrtobot(wnd);
184 return;
185 }
186
187 tbuf = 0;
188 tvalid = tsize = 0;
189 lastrow = PAGEINFO_ROWS + 2; /* Leave room for header. */
190 for (i = lastrow, ml = LIST_FIRST(&mount_list); ml != NULL;
191 i++, ml = LIST_NEXT(ml, ml_entries)) {
192
193 int c = ml->ml_count;
194 double v = ml->ml_valid;
195 double s = ml->ml_size;
196
197 /* Display in window if enough room. */
198 if (i < getmaxy(wnd) - 2) {
199 mvwprintw(wnd, i, 0, "%-20.20s", ml->ml_addr == NULL ?
200 "NULL" : ml->ml_mount.mnt_stat.f_mntonname);
201 wprintw(wnd,
202 " %6d %3d %8ld %3.0f %8ld %3.0f %3.0f",
203 c, (100 * c) / nbuf,
204 (long)(v/1024), 100 * v / bufmem,
205 (long)(s/1024), 100 * s / bufmem,
206 100 * v / s);
207 wclrtoeol(wnd);
208 lastrow = i;
209 }
210
211 /* Update statistics. */
212 tbuf += c;
213 tvalid += v;
214 tsize += s;
215 }
216
217 wclrtobot(wnd);
218 mvwprintw(wnd, lastrow + 2, 0,
219 "%-20s %6d %3d %8ld %3.0f %8ld %3.0f %3.0f",
220 "Total:", tbuf, (100 * tbuf) / nbuf,
221 (long)(tvalid/1024), 100 * tvalid / bufmem,
222 (long)(tsize/1024), 100 * tsize / bufmem,
223 tsize != 0 ? ((100 * tvalid) / tsize) : 0);
224 }
225
226 int
227 initbufcache(void)
228 {
229 if (namelist[0].n_type == 0) {
230 if (kvm_nlist(kd, namelist)) {
231 nlisterr(namelist);
232 return(0);
233 }
234 }
235
236 fetchuvmexp();
237 pgwidth = (int)(floor(log10((double)uvmexp.npages)) + 1);
238 kbwidth = (int)(floor(log10(uvmexp.npages * getpagesize() / 1024.0)) +
239 1);
240
241 return(1);
242 }
243
244 static void
245 fetchuvmexp(void)
246 {
247 int mib[2];
248 size_t size;
249
250 /* Re-read pages used for vnodes & executables */
251 size = sizeof(uvmexp);
252 mib[0] = CTL_VM;
253 mib[1] = VM_UVMEXP2;
254 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
255 error("can't get uvmexp: %s\n", strerror(errno));
256 memset(&uvmexp, 0, sizeof(uvmexp));
257 }
258 }
259
260 void
261 fetchbufcache(void)
262 {
263 int count;
264 struct buf_sysctl *bp, *buffers;
265 struct vnode *vn;
266 struct mount *mt;
267 struct ml_entry *ml;
268 int mib[6];
269 size_t size;
270 int extraslop = 0;
271
272 /* Re-read pages used for vnodes & executables */
273 fetchuvmexp();
274
275 /* Initialise vnode cache and mount list. */
276 vc_init();
277 ml_init();
278
279 /* Get metadata buffers */
280 size = 0;
281 buffers = NULL;
282 mib[0] = CTL_KERN;
283 mib[1] = KERN_BUF;
284 mib[2] = KERN_BUF_ALL;
285 mib[3] = KERN_BUF_ALL;
286 mib[4] = (int)sizeof(struct buf_sysctl);
287 mib[5] = INT_MAX; /* we want them all */
288 again:
289 if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) {
290 error("can't get buffers size: %s\n", strerror(errno));
291 return;
292 }
293 if (size == 0)
294 return;
295
296 size += extraslop * sizeof(struct buf_sysctl);
297 buffers = malloc(size);
298 if (buffers == NULL) {
299 error("can't allocate buffers: %s\n", strerror(errno));
300 return;
301 }
302 if (sysctl(mib, 6, buffers, &size, NULL, 0) < 0) {
303 free(buffers);
304 if (extraslop == 0) {
305 extraslop = 100;
306 goto again;
307 }
308 error("can't get buffers: %s\n", strerror(errno));
309 return;
310 }
311
312 nbuf = size / sizeof(struct buf_sysctl);
313 for (bp = buffers; bp < buffers + nbuf; bp++) {
314 if (UINT64TOPTR(bp->b_vp) != NULL) {
315 struct mount *mp;
316 vn = vc_lookup(UINT64TOPTR(bp->b_vp));
317 if (vn == NULL)
318 break;
319
320 mp = vn->v_mount;
321 /*
322 * References to mounted-on vnodes should be
323 * counted towards the mounted filesystem.
324 */
325 if (vn->v_type == VBLK && vn->v_specinfo != NULL) {
326 struct specinfo sp;
327 if (!KREAD(vn->v_specinfo, &sp, sizeof(sp)))
328 continue;
329 if (sp.si_mountpoint)
330 mp = sp.si_mountpoint;
331 }
332 if (mp != NULL)
333 mt = ml_lookup(mp,
334 bp->b_bufsize,
335 bp->b_bcount);
336 }
337 }
338
339 /* simple sort - there's not that many entries */
340 do {
341 if ((ml = LIST_FIRST(&mount_list)) == NULL ||
342 LIST_NEXT(ml, ml_entries) == NULL)
343 break;
344
345 count = 0;
346 for (ml = LIST_FIRST(&mount_list); ml != NULL;
347 ml = LIST_NEXT(ml, ml_entries)) {
348 if (LIST_NEXT(ml, ml_entries) == NULL)
349 break;
350 if (ml->ml_count < LIST_NEXT(ml, ml_entries)->ml_count) {
351 ml = LIST_NEXT(ml, ml_entries);
352 LIST_REMOVE(ml, ml_entries);
353 LIST_INSERT_HEAD(&mount_list, ml, ml_entries);
354 count++;
355 }
356 }
357 } while (count != 0);
358
359 free(buffers);
360 }
361
362 static void
363 vc_init(void)
364 {
365 int i;
366
367 /* vc_addr == NULL for unused cache entry. */
368 for (i = 0; i < VCACHE_SIZE; i++)
369 vcache[i].vc_addr = NULL;
370 }
371
372 static void
373 ml_init(void)
374 {
375 struct ml_entry *ml;
376
377 /* Throw out the current mount list and start again. */
378 while ((ml = LIST_FIRST(&mount_list)) != NULL) {
379 LIST_REMOVE(ml, ml_entries);
380 free(ml);
381 }
382 }
383
384
385 static struct vnode *
386 vc_lookup(struct vnode *vaddr)
387 {
388 struct vnode *ret;
389 size_t i, oldest;
390
391 ret = NULL;
392 oldest = 0;
393 for (i = 0; i < VCACHE_SIZE; i++) {
394 if (vcache[i].vc_addr == NULL)
395 break;
396 vcache[i].vc_age++;
397 if (vcache[i].vc_age < vcache[oldest].vc_age)
398 oldest = i;
399 if (vcache[i].vc_addr == vaddr) {
400 vcache[i].vc_age = 0;
401 ret = &vcache[i].vc_node;
402 }
403 }
404
405 /* Find an entry in the cache? */
406 if (ret != NULL)
407 return(ret);
408
409 /* Go past the end of the cache? */
410 if (i >= VCACHE_SIZE)
411 i = oldest;
412
413 /* Read in new vnode and reset age counter. */
414 if (KREAD(vaddr, &vcache[i].vc_node, sizeof(struct vnode)) == 0)
415 return NULL;
416 vcache[i].vc_addr = vaddr;
417 vcache[i].vc_age = 0;
418
419 return(&vcache[i].vc_node);
420 }
421
422 static struct mount *
423 ml_lookup(struct mount *maddr, int size, int valid)
424 {
425 struct ml_entry *ml;
426
427 for (ml = LIST_FIRST(&mount_list); ml != NULL;
428 ml = LIST_NEXT(ml, ml_entries))
429 if (ml->ml_addr == maddr) {
430 ml->ml_count++;
431 ml->ml_size += size;
432 ml->ml_valid += valid;
433 if (ml->ml_addr == NULL)
434 return(NULL);
435 else
436 return(&ml->ml_mount);
437 }
438
439 if ((ml = malloc(sizeof(struct ml_entry))) == NULL) {
440 error("out of memory");
441 die(0);
442 }
443 LIST_INSERT_HEAD(&mount_list, ml, ml_entries);
444 ml->ml_count = 1;
445 ml->ml_size = size;
446 ml->ml_valid = valid;
447 ml->ml_addr = maddr;
448 if (maddr == NULL)
449 return(NULL);
450
451 KREAD(maddr, &ml->ml_mount, sizeof(struct mount));
452 return(&ml->ml_mount);
453 }
454