bufcache.c revision 1.9 1 /* $NetBSD: bufcache.c,v 1.9 2000/11/30 12:08:13 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.9 2000/11/30 12:08:13 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/queue.h>
48 #include <sys/sysctl.h>
49 #include <sys/time.h>
50 #include <sys/vnode.h>
51
52 #include <uvm/uvm_extern.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <kvm.h>
57 #include <nlist.h>
58 #include <paths.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "systat.h"
64 #include "extern.h"
65
66
67 /*
68 * Definitions for the buffer free lists (from sys/kern/vfs_bio.c).
69 */
70 #define BQUEUES 4 /* number of free buffer queues */
71
72 #define BQ_LOCKED 0 /* super-blocks &c */
73 #define BQ_LRU 1 /* lru, useful buffers */
74 #define BQ_AGE 2 /* rubbish */
75 #define BQ_EMPTY 3 /* buffer headers with no memory */
76
77 #define VCACHE_SIZE 50
78
79 struct vcache {
80 int vc_age;
81 struct vnode *vc_addr;
82 struct vnode vc_node;
83 };
84
85 struct ml_entry {
86 int ml_count;
87 long ml_size;
88 long ml_valid;
89 struct mount *ml_addr;
90 struct mount ml_mount;
91 LIST_ENTRY(ml_entry) ml_entries;
92 };
93
94 static struct nlist namelist[] = {
95 #define X_NBUF 0
96 { "_nbuf" },
97 #define X_BUF 1
98 { "_buf" },
99 #define X_BUFQUEUES 2
100 { "_bufqueues" },
101 #define X_BUFPAGES 3
102 { "_bufpages" },
103 { "" },
104 };
105
106 static struct vcache vcache[VCACHE_SIZE];
107 static LIST_HEAD(mount_list, ml_entry) mount_list;
108
109 static int nbuf, bufpages, bufkb;
110 static struct uvmexp_sysctl uvmexp;
111 static void *bufaddr;
112 static struct buf *buf = NULL;
113 static TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
114
115 static void vc_init(void);
116 static void ml_init(void);
117 static struct vnode *vc_lookup(struct vnode *);
118 static struct mount *ml_lookup(struct mount *, int, int);
119
120
121 WINDOW *
122 openbufcache(void)
123 {
124
125 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
126 }
127
128 void
129 closebufcache(WINDOW *w)
130 {
131
132 if (w == NULL)
133 return;
134 wclear(w);
135 wrefresh(w);
136 delwin(w);
137 ml_init(); /* Clear out mount list */
138 }
139
140 void
141 labelbufcache(void)
142 {
143 mvwprintw(wnd, 0, 0,
144 "There are %d metadata buffers using %d kBytes of memory.",
145 nbuf, bufkb);
146 wclrtoeol(wnd);
147 wmove(wnd, 1, 0);
148 wmove(wnd, 2, 0);
149 wclrtoeol(wnd);
150 mvwaddstr(wnd, 3, 0,
151 "File System Bufs used % kB in use % Bufsize kB % Util %");
152 wclrtoeol(wnd);
153 }
154
155 void
156 showbufcache(void)
157 {
158 int tbuf, i, lastrow;
159 long tvalid, tsize;
160 struct ml_entry *ml;
161
162 mvwprintw(wnd, 1, 0,
163 "There are %llu pages for vnode page cache using %llu kBytes of memory.",
164 (long long)uvmexp.vnodepages,
165 (long long) uvmexp.vnodepages * getpagesize() / 1024);
166 wclrtoeol(wnd);
167
168 tbuf = tvalid = tsize = 0;
169 lastrow = 4; /* Leave room for header. */
170 for (i = lastrow, ml = LIST_FIRST(&mount_list); ml != NULL;
171 i++, ml = LIST_NEXT(ml, ml_entries)) {
172
173 /* Display in window if enough room. */
174 if (i < getmaxy(wnd) - 2) {
175 mvwprintw(wnd, i, 0, "%-20.20s", ml->ml_addr == NULL ?
176 "NULL" : ml->ml_mount.mnt_stat.f_mntonname);
177 wprintw(wnd,
178 " %6d %3d %8ld %3ld %8ld %3ld %3ld",
179 ml->ml_count, (100 * ml->ml_count) / nbuf,
180 ml->ml_valid, (100 * ml->ml_valid) / bufkb,
181 ml->ml_size, (100 * ml->ml_size) / bufkb,
182 (100 * ml->ml_valid) / ml->ml_size);
183 wclrtoeol(wnd);
184 lastrow = i;
185 }
186
187 /* Update statistics. */
188 tbuf += ml->ml_count;
189 tvalid += ml->ml_valid;
190 tsize += ml->ml_size;
191 }
192
193 wclrtobot(wnd);
194 mvwprintw(wnd, lastrow + 2, 0,
195 "%-20s %6d %3d %8ld %3ld %8ld %3ld %3ld",
196 "Total:", tbuf, (100 * tbuf) / nbuf,
197 tvalid, (100 * tvalid) / bufkb,
198 tsize, (100 * tsize) / bufkb, (100 * tvalid) / tsize);
199 }
200
201 int
202 initbufcache(void)
203 {
204 if (namelist[X_NBUF].n_type == 0) {
205 if (kvm_nlist(kd, namelist)) {
206 nlisterr(namelist);
207 return(0);
208 }
209 if (namelist[X_NBUF].n_type == 0) {
210 error("namelist on %s failed", _PATH_UNIX);
211 return(0);
212 }
213 }
214
215 NREAD(X_NBUF, &nbuf, sizeof(nbuf));
216 NREAD(X_BUFPAGES, &bufpages, sizeof(bufpages));
217 bufkb = bufpages * sysconf(_SC_PAGESIZE) / 1024;
218
219 if ((buf = malloc(nbuf * sizeof(struct buf))) == NULL) {
220 error("malloc failed");
221 die(0);
222 }
223 NREAD(X_BUF, &bufaddr, sizeof(bufaddr));
224
225 return(1);
226 }
227
228 void
229 fetchbufcache(void)
230 {
231 int i, count, mib[2];
232 size_t size;
233 struct buf *bp;
234 struct vnode *vn;
235 struct mount *mt;
236 struct ml_entry *ml;
237
238 /* Re-read pages used for vnodes */
239 size = sizeof(uvmexp);
240 mib[0] = CTL_VM;
241 mib[1] = VM_UVMEXP2;
242 if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
243 error("can't get uvmexp: %s\n", strerror(errno));
244 memset(&uvmexp, 0, sizeof(uvmexp));
245 }
246
247 /* Re-read bufqueues lists and buffer cache headers */
248 NREAD(X_BUFQUEUES, bufqueues, sizeof(bufqueues));
249 KREAD(bufaddr, buf, sizeof(struct buf) * nbuf);
250
251 /* Initialise vnode cache and mount list. */
252 vc_init();
253 ml_init();
254 for (i = 0; i < BQUEUES; i++) {
255 for (bp = bufqueues[i].tqh_first; bp != NULL;
256 bp = bp->b_freelist.tqe_next) {
257 if (bp != NULL) {
258 bp = (struct buf *)((u_long)bp + (u_long)buf -
259 (u_long)bufaddr);
260
261 if (bp->b_vp != NULL) {
262 vn = vc_lookup(bp->b_vp);
263 if (vn == NULL)
264 errx(1,
265 "vc_lookup returns NULL!\n");
266 if (vn->v_mount != NULL)
267 mt = ml_lookup(vn->v_mount,
268 bp->b_bufsize,
269 bp->b_bcount);
270 }
271 }
272 }
273 }
274
275 /* simple sort - there's not that many entries */
276 do {
277 if ((ml = LIST_FIRST(&mount_list)) == NULL ||
278 LIST_NEXT(ml, ml_entries) == NULL)
279 break;
280
281 count = 0;
282 for (ml = LIST_FIRST(&mount_list); ml != NULL;
283 ml = LIST_NEXT(ml, ml_entries)) {
284 if (LIST_NEXT(ml, ml_entries) == NULL)
285 break;
286 if (ml->ml_count < LIST_NEXT(ml, ml_entries)->ml_count) {
287 ml = LIST_NEXT(ml, ml_entries);
288 LIST_REMOVE(ml, ml_entries);
289 LIST_INSERT_HEAD(&mount_list, ml, ml_entries);
290 count++;
291 }
292 }
293 } while (count != 0);
294 }
295
296 static void
297 vc_init(void)
298 {
299 int i;
300
301 /* vc_addr == NULL for unused cache entry. */
302 for (i = 0; i < VCACHE_SIZE; i++)
303 vcache[i].vc_addr = NULL;
304 }
305
306 static void
307 ml_init(void)
308 {
309 struct ml_entry *ml;
310
311 /* Throw out the current mount list and start again. */
312 while ((ml = LIST_FIRST(&mount_list)) != NULL) {
313 LIST_REMOVE(ml, ml_entries);
314 free(ml);
315 }
316 }
317
318
319 static struct vnode *
320 vc_lookup(struct vnode *vaddr)
321 {
322 struct vnode *ret;
323 int i, oldest, match;
324
325 ret = NULL;
326 oldest = match = 0;
327 for (i = 0; i < VCACHE_SIZE || vcache[i].vc_addr == NULL; i++) {
328 vcache[i].vc_age++;
329 if (vcache[i].vc_addr == NULL)
330 break;
331 if (vcache[i].vc_age < vcache[oldest].vc_age)
332 oldest = i;
333 if (vcache[i].vc_addr == vaddr) {
334 vcache[i].vc_age = 0;
335 match = i;
336 ret = &vcache[i].vc_node;
337 }
338 }
339
340 /* Find an entry in the cache? */
341 if (ret != NULL)
342 return(ret);
343
344 /* Go past the end of the cache? */
345 if (i >= VCACHE_SIZE)
346 i = oldest;
347
348 /* Read in new vnode and reset age counter. */
349 KREAD(vaddr, &vcache[i].vc_node, sizeof(struct vnode));
350 vcache[i].vc_addr = vaddr;
351 vcache[i].vc_age = 0;
352
353 return(&vcache[i].vc_node);
354 }
355
356 static struct mount *
357 ml_lookup(struct mount *maddr, int size, int valid)
358 {
359 struct ml_entry *ml;
360
361 for (ml = LIST_FIRST(&mount_list); ml != NULL;
362 ml = LIST_NEXT(ml, ml_entries))
363 if (ml->ml_addr == maddr) {
364 ml->ml_count++;
365 ml->ml_size += size / 1024;
366 ml->ml_valid += valid / 1024;
367 if (ml->ml_addr == NULL)
368 return(NULL);
369 else
370 return(&ml->ml_mount);
371 }
372
373 if ((ml = malloc(sizeof(struct ml_entry))) == NULL) {
374 error("out of memory");
375 die(0);
376 }
377 LIST_INSERT_HEAD(&mount_list, ml, ml_entries);
378 ml->ml_count = 1;
379 ml->ml_size = size / 1024;
380 ml->ml_valid = valid / 1024;
381 ml->ml_addr = maddr;
382 if (maddr == NULL)
383 return(NULL);
384
385 KREAD(maddr, &ml->ml_mount, sizeof(struct mount));
386 return(&ml->ml_mount);
387 }
388