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