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