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