rcache.c revision 1.6 1 1.6 lukem /* $NetBSD: rcache.c,v 1.6 2001/05/27 14:17:57 lukem Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*-
4 1.1 bouyer * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 bouyer * All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1 bouyer * by Martin J. Laubach <mjl (at) emsi.priv.at> and
9 1.1 bouyer * Manuel Bouyer <Manuel.Bouyer (at) lip6.fr>.
10 1.1 bouyer *
11 1.1 bouyer * Redistribution and use in source and binary forms, with or without
12 1.1 bouyer * modification, are permitted provided that the following conditions
13 1.1 bouyer * are met:
14 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
15 1.1 bouyer * notice, this list of conditions and the following disclaimer.
16 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
18 1.1 bouyer * documentation and/or other materials provided with the distribution.
19 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
20 1.1 bouyer * must display the following acknowledgement:
21 1.1 bouyer * This product includes software developed by the NetBSD
22 1.1 bouyer * Foundation, Inc. and its contributors.
23 1.1 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 bouyer * contributors may be used to endorse or promote products derived
25 1.1 bouyer * from this software without specific prior written permission.
26 1.1 bouyer *
27 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 bouyer * POSSIBILITY OF SUCH DAMAGE.
38 1.1 bouyer */
39 1.1 bouyer /*-----------------------------------------------------------------------*/
40 1.1 bouyer #include <sys/types.h>
41 1.1 bouyer #include <sys/uio.h>
42 1.1 bouyer #include <sys/mman.h>
43 1.1 bouyer #include <sys/param.h>
44 1.1 bouyer #include <sys/sysctl.h>
45 1.1 bouyer #include <ufs/ufs/dinode.h>
46 1.1 bouyer
47 1.1 bouyer #include <stdio.h>
48 1.1 bouyer #include <stdlib.h>
49 1.1 bouyer #include <unistd.h>
50 1.1 bouyer #include <fcntl.h>
51 1.1 bouyer #include <errno.h>
52 1.1 bouyer #include <string.h>
53 1.1 bouyer
54 1.1 bouyer #include "dump.h"
55 1.1 bouyer
56 1.1 bouyer /*-----------------------------------------------------------------------*/
57 1.1 bouyer #define MAXCACHEBUFS 512 /* max 512 buffers */
58 1.1 bouyer #define MAXMEMPART 6 /* max 15% of the user mem */
59 1.1 bouyer
60 1.1 bouyer /*-----------------------------------------------------------------------*/
61 1.1 bouyer struct cheader {
62 1.1 bouyer volatile size_t count;
63 1.1 bouyer };
64 1.1 bouyer
65 1.1 bouyer struct cdesc {
66 1.1 bouyer volatile daddr_t blkstart;
67 1.1 bouyer volatile daddr_t blkend;/* start + nblksread */
68 1.1 bouyer volatile daddr_t blocksRead;
69 1.1 bouyer volatile size_t time;
70 1.1 bouyer #ifdef DIAGNOSTICS
71 1.1 bouyer volatile pid_t owner;
72 1.1 bouyer #endif
73 1.1 bouyer };
74 1.1 bouyer
75 1.6 lukem static int findlru(void);
76 1.1 bouyer
77 1.1 bouyer static void *shareBuffer = NULL;
78 1.1 bouyer static struct cheader *cheader;
79 1.1 bouyer static struct cdesc *cdesc;
80 1.1 bouyer static char *cdata;
81 1.1 bouyer static int cachebufs;
82 1.1 bouyer static int nblksread;
83 1.1 bouyer
84 1.1 bouyer #ifdef STATS
85 1.1 bouyer static int nreads;
86 1.1 bouyer static int nphysread;
87 1.1 bouyer static int64_t readsize;
88 1.1 bouyer static int64_t physreadsize;
89 1.1 bouyer #endif
90 1.1 bouyer
91 1.1 bouyer #define CDATA(i) (cdata + ((i) * nblksread * dev_bsize))
92 1.1 bouyer
93 1.1 bouyer void
94 1.6 lukem initcache(int cachesize, int readblksize)
95 1.1 bouyer {
96 1.1 bouyer size_t len;
97 1.1 bouyer size_t sharedSize;
98 1.1 bouyer
99 1.4 perseant nblksread = (readblksize + ufsib->ufs_bsize - 1) / ufsib->ufs_bsize;
100 1.1 bouyer if(cachesize == -1) { /* Compute from memory available */
101 1.1 bouyer int usermem;
102 1.1 bouyer int mib[2] = { CTL_HW, HW_USERMEM };
103 1.1 bouyer
104 1.1 bouyer len = sizeof(usermem);
105 1.1 bouyer if (sysctl(mib, 2, &usermem, &len, NULL, 0) < 0) {
106 1.1 bouyer msg("sysctl(hw.usermem) failed: %s\n", strerror(errno));
107 1.1 bouyer return;
108 1.1 bouyer }
109 1.1 bouyer cachebufs = (usermem / MAXMEMPART) / (nblksread * dev_bsize);
110 1.1 bouyer } else { /* User specified */
111 1.1 bouyer cachebufs = cachesize;
112 1.1 bouyer }
113 1.1 bouyer
114 1.1 bouyer if(cachebufs) { /* Don't allocate if zero --> no caching */
115 1.1 bouyer if (cachebufs > MAXCACHEBUFS)
116 1.1 bouyer cachebufs = MAXCACHEBUFS;
117 1.1 bouyer
118 1.1 bouyer sharedSize = sizeof(struct cheader) +
119 1.1 bouyer sizeof(struct cdesc) * cachebufs +
120 1.1 bouyer nblksread * cachebufs * dev_bsize;
121 1.1 bouyer #ifdef STATS
122 1.1 bouyer fprintf(stderr, "Using %d buffers (%d bytes)\n", cachebufs,
123 1.1 bouyer sharedSize);
124 1.1 bouyer #endif
125 1.1 bouyer shareBuffer = mmap(NULL, sharedSize, PROT_READ | PROT_WRITE,
126 1.1 bouyer MAP_ANON | MAP_SHARED, -1, 0);
127 1.1 bouyer if (shareBuffer == (void *)-1) {
128 1.1 bouyer msg("can't mmap shared memory for buffer: %s\n",
129 1.1 bouyer strerror(errno));
130 1.1 bouyer return;
131 1.1 bouyer }
132 1.1 bouyer cheader = shareBuffer;
133 1.1 bouyer cdesc = (struct cdesc *) (((char *) shareBuffer) +
134 1.1 bouyer sizeof(struct cheader));
135 1.1 bouyer cdata = ((char *) shareBuffer) + sizeof(struct cheader) +
136 1.1 bouyer sizeof(struct cdesc) * cachebufs;
137 1.1 bouyer
138 1.1 bouyer memset(shareBuffer, '\0', sharedSize);
139 1.1 bouyer }
140 1.1 bouyer }
141 1.1 bouyer
142 1.6 lukem /*
143 1.6 lukem * Find the cache buffer descriptor that shows the minimal access time
144 1.6 lukem */
145 1.1 bouyer static int
146 1.6 lukem findlru(void)
147 1.1 bouyer {
148 1.1 bouyer int i;
149 1.1 bouyer int minTime = cdesc[0].time;
150 1.1 bouyer int minIdx = 0;
151 1.1 bouyer
152 1.1 bouyer for (i = 0; i < cachebufs; i++) {
153 1.1 bouyer if (cdesc[i].time < minTime) {
154 1.1 bouyer minIdx = i;
155 1.1 bouyer minTime = cdesc[i].time;
156 1.1 bouyer }
157 1.1 bouyer }
158 1.1 bouyer
159 1.1 bouyer return minIdx;
160 1.1 bouyer }
161 1.6 lukem
162 1.1 bouyer /*
163 1.1 bouyer * Read data directly from disk, with smart error handling.
164 1.1 bouyer * Try to recover from hard errors by reading in sector sized pieces.
165 1.1 bouyer * Error recovery is attempted at most BREADEMAX times before seeking
166 1.1 bouyer * consent from the operator to continue.
167 1.1 bouyer */
168 1.1 bouyer
169 1.1 bouyer static int breaderrors = 0;
170 1.1 bouyer #define BREADEMAX 32
171 1.1 bouyer
172 1.1 bouyer void
173 1.6 lukem rawread(daddr_t blkno, char *buf, int size)
174 1.1 bouyer {
175 1.1 bouyer int cnt, i;
176 1.1 bouyer #ifdef STATS
177 1.1 bouyer nphysread++;
178 1.1 bouyer physreadsize += size;
179 1.1 bouyer #endif
180 1.1 bouyer
181 1.1 bouyer if (lseek(diskfd, ((off_t) blkno << dev_bshift), 0) < 0) {
182 1.1 bouyer msg("rawread: lseek fails\n");
183 1.1 bouyer goto err;
184 1.1 bouyer }
185 1.1 bouyer if ((cnt = read(diskfd, buf, size)) == size)
186 1.1 bouyer return;
187 1.1 bouyer if (cnt == -1)
188 1.1 bouyer msg("read error from %s: %s: [block %d]: count=%d\n",
189 1.1 bouyer disk, strerror(errno), blkno, size);
190 1.1 bouyer else
191 1.1 bouyer msg("short read error from %s: [block %d]: count=%d, got=%d\n",
192 1.1 bouyer disk, blkno, size, cnt);
193 1.1 bouyer err:
194 1.1 bouyer if (++breaderrors > BREADEMAX) {
195 1.5 briggs msg("More than %d block read errors from %s\n",
196 1.1 bouyer BREADEMAX, disk);
197 1.1 bouyer broadcast("DUMP IS AILING!\n");
198 1.1 bouyer msg("This is an unrecoverable error.\n");
199 1.1 bouyer if (!query("Do you want to attempt to continue?")){
200 1.1 bouyer dumpabort(0);
201 1.1 bouyer /*NOTREACHED*/
202 1.1 bouyer } else
203 1.1 bouyer breaderrors = 0;
204 1.1 bouyer }
205 1.1 bouyer /*
206 1.1 bouyer * Zero buffer, then try to read each sector of buffer separately.
207 1.1 bouyer */
208 1.1 bouyer memset(buf, 0, size);
209 1.1 bouyer for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
210 1.1 bouyer if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0) {
211 1.1 bouyer msg("rawread: lseek2 fails: %s!\n",
212 1.1 bouyer strerror(errno));
213 1.1 bouyer continue;
214 1.1 bouyer }
215 1.1 bouyer if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
216 1.1 bouyer continue;
217 1.1 bouyer if (cnt == -1) {
218 1.5 briggs msg("read error from %s: %s: [sector %d]: count=%ld: "
219 1.1 bouyer "%s\n", disk, strerror(errno), blkno, dev_bsize,
220 1.1 bouyer strerror(errno));
221 1.1 bouyer continue;
222 1.1 bouyer }
223 1.5 briggs msg("short read error from %s: [sector %d]: count=%ld, got=%d\n",
224 1.1 bouyer disk, blkno, dev_bsize, cnt);
225 1.1 bouyer }
226 1.1 bouyer }
227 1.1 bouyer
228 1.1 bouyer void
229 1.6 lukem bread(daddr_t blkno, char *buf, int size)
230 1.1 bouyer {
231 1.1 bouyer int osize = size;
232 1.1 bouyer daddr_t oblkno = blkno;
233 1.1 bouyer char *obuf = buf;
234 1.1 bouyer daddr_t numBlocks = (size + dev_bsize -1) / dev_bsize;
235 1.1 bouyer
236 1.1 bouyer #ifdef STATS
237 1.1 bouyer nreads++;
238 1.1 bouyer readsize += size;
239 1.1 bouyer #endif
240 1.1 bouyer
241 1.1 bouyer if (!shareBuffer) {
242 1.1 bouyer rawread(blkno, buf, size);
243 1.1 bouyer return;
244 1.1 bouyer }
245 1.1 bouyer
246 1.1 bouyer if (flock(diskfd, LOCK_EX)) {
247 1.1 bouyer msg("flock(LOCK_EX) failed: %s\n",
248 1.1 bouyer strerror(errno));
249 1.1 bouyer rawread(blkno, buf, size);
250 1.1 bouyer return;
251 1.1 bouyer }
252 1.1 bouyer
253 1.1 bouyer
254 1.1 bouyer retry:
255 1.1 bouyer while(size > 0) {
256 1.1 bouyer int i;
257 1.1 bouyer
258 1.1 bouyer for (i = 0; i < cachebufs; i++) {
259 1.1 bouyer struct cdesc *curr = &cdesc[i];
260 1.1 bouyer
261 1.1 bouyer #ifdef DIAGNOSTICS
262 1.1 bouyer if (curr->owner) {
263 1.1 bouyer fprintf(stderr, "Owner is set (%d, me=%d), can"
264 1.1 bouyer "not happen.\n", curr->owner, getpid());
265 1.1 bouyer }
266 1.1 bouyer #endif
267 1.1 bouyer
268 1.1 bouyer if (curr->blkend == 0)
269 1.1 bouyer continue;
270 1.1 bouyer /*
271 1.1 bouyer * If we find a bit of the read in the buffers,
272 1.1 bouyer * now compute how many blocks we can copy,
273 1.1 bouyer * copy them out, adjust blkno, buf and size,
274 1.1 bouyer * and restart
275 1.1 bouyer */
276 1.1 bouyer if (curr->blkstart <= blkno &&
277 1.1 bouyer blkno < curr->blkend) {
278 1.1 bouyer /* Number of data blocks to be copied */
279 1.6 lukem int toCopy = MIN(size,
280 1.1 bouyer (curr->blkend - blkno) * dev_bsize);
281 1.1 bouyer #ifdef DIAGNOSTICS
282 1.1 bouyer if (toCopy <= 0 ||
283 1.1 bouyer toCopy > nblksread * dev_bsize) {
284 1.1 bouyer fprintf(stderr, "toCopy %d !\n",
285 1.1 bouyer toCopy);
286 1.1 bouyer dumpabort(0);
287 1.1 bouyer }
288 1.1 bouyer if (CDATA(i) + (blkno - curr->blkstart) *
289 1.1 bouyer dev_bsize < CDATA(i) ||
290 1.1 bouyer CDATA(i) + (blkno - curr->blkstart) *
291 1.1 bouyer dev_bsize >
292 1.1 bouyer CDATA(i) + nblksread * dev_bsize) {
293 1.1 bouyer fprintf(stderr, "%p < %p !!!\n",
294 1.1 bouyer CDATA(i) + (blkno -
295 1.1 bouyer curr->blkstart) * dev_bsize,
296 1.1 bouyer CDATA(i));
297 1.1 bouyer fprintf(stderr, "cdesc[i].blkstart %d "
298 1.1 bouyer "blkno %d dev_bsize %ld\n",
299 1.1 bouyer curr->blkstart, blkno, dev_bsize);
300 1.1 bouyer dumpabort(0);
301 1.1 bouyer }
302 1.1 bouyer #endif
303 1.1 bouyer memcpy(buf, CDATA(i) +
304 1.1 bouyer (blkno - curr->blkstart) * dev_bsize,
305 1.1 bouyer toCopy);
306 1.1 bouyer
307 1.1 bouyer buf += toCopy;
308 1.1 bouyer size -= toCopy;
309 1.1 bouyer blkno += (toCopy + dev_bsize - 1) / dev_bsize;
310 1.1 bouyer numBlocks -=
311 1.1 bouyer (toCopy + dev_bsize - 1) / dev_bsize;
312 1.1 bouyer
313 1.1 bouyer curr->time = cheader->count++;
314 1.1 bouyer
315 1.1 bouyer /*
316 1.1 bouyer * If all data of a cache block have been
317 1.1 bouyer * read, chances are good no more reads
318 1.1 bouyer * will occur, so expire the cache immediately
319 1.1 bouyer */
320 1.1 bouyer
321 1.1 bouyer curr->blocksRead +=
322 1.1 bouyer (toCopy + dev_bsize -1) / dev_bsize;
323 1.1 bouyer if (curr->blocksRead >= nblksread)
324 1.1 bouyer curr->time = 0;
325 1.1 bouyer
326 1.1 bouyer goto retry;
327 1.1 bouyer }
328 1.1 bouyer }
329 1.1 bouyer
330 1.1 bouyer /* No more to do? */
331 1.1 bouyer if (size == 0)
332 1.1 bouyer break;
333 1.1 bouyer
334 1.1 bouyer /*
335 1.1 bouyer * This does actually not happen if fs blocks are not greater
336 1.1 bouyer * than nblksread.
337 1.1 bouyer */
338 1.1 bouyer if (numBlocks > nblksread) {
339 1.1 bouyer rawread(oblkno, obuf, osize);
340 1.1 bouyer break;
341 1.1 bouyer } else {
342 1.1 bouyer int idx;
343 1.1 bouyer ssize_t rsize;
344 1.1 bouyer daddr_t blockBlkNo;
345 1.1 bouyer
346 1.1 bouyer blockBlkNo = (blkno / nblksread) * nblksread;
347 1.1 bouyer idx = findlru();
348 1.6 lukem rsize = MIN(nblksread,
349 1.4 perseant ufsib->ufs_dsize - blockBlkNo) *
350 1.1 bouyer dev_bsize;
351 1.3 perseant
352 1.1 bouyer #ifdef DIAGNOSTICS
353 1.1 bouyer if (cdesc[idx].owner)
354 1.1 bouyer fprintf(stderr, "Owner is set (%d, me=%d), can"
355 1.1 bouyer "not happen(2).\n", cdesc[idx].owner,
356 1.1 bouyer getpid());
357 1.1 bouyer cdesc[idx].owner = getpid();
358 1.1 bouyer #endif
359 1.1 bouyer cdesc[idx].time = cheader->count++;
360 1.1 bouyer cdesc[idx].blkstart = blockBlkNo;
361 1.1 bouyer cdesc[idx].blocksRead = 0;
362 1.1 bouyer
363 1.1 bouyer if (lseek(diskfd,
364 1.1 bouyer ((off_t) (blockBlkNo) << dev_bshift), 0) < 0) {
365 1.1 bouyer msg("readBlocks: lseek fails: %s\n",
366 1.1 bouyer strerror(errno));
367 1.1 bouyer rsize = -1;
368 1.1 bouyer } else {
369 1.1 bouyer rsize = read(diskfd, CDATA(idx), rsize);
370 1.1 bouyer if (rsize < 0) {
371 1.1 bouyer msg("readBlocks: read fails: %s\n",
372 1.1 bouyer strerror(errno));
373 1.1 bouyer }
374 1.1 bouyer }
375 1.1 bouyer
376 1.1 bouyer /* On errors, panic, punt, try to read without
377 1.1 bouyer * cache and let raw read routine do the rest.
378 1.1 bouyer */
379 1.1 bouyer
380 1.1 bouyer if (rsize <= 0) {
381 1.1 bouyer rawread(oblkno, obuf, osize);
382 1.1 bouyer #ifdef DIAGNOSTICS
383 1.1 bouyer if (cdesc[idx].owner != getpid())
384 1.1 bouyer fprintf(stderr, "Owner changed from "
385 1.1 bouyer "%d to %d, can't happen\n",
386 1.1 bouyer getpid(), cdesc[idx].owner);
387 1.1 bouyer cdesc[idx].owner = 0;
388 1.1 bouyer #endif
389 1.1 bouyer break;
390 1.1 bouyer }
391 1.1 bouyer
392 1.1 bouyer /* On short read, just note the fact and go on */
393 1.1 bouyer cdesc[idx].blkend = blockBlkNo + rsize / dev_bsize;
394 1.1 bouyer
395 1.1 bouyer #ifdef STATS
396 1.1 bouyer nphysread++;
397 1.1 bouyer physreadsize += rsize;
398 1.1 bouyer #endif
399 1.1 bouyer #ifdef DIAGNOSTICS
400 1.1 bouyer if (cdesc[idx].owner != getpid())
401 1.1 bouyer fprintf(stderr, "Owner changed from "
402 1.1 bouyer "%d to %d, can't happen\n",
403 1.1 bouyer getpid(), cdesc[idx].owner);
404 1.1 bouyer cdesc[idx].owner = 0;
405 1.1 bouyer #endif
406 1.1 bouyer /*
407 1.1 bouyer * We swapped some of data in, let the loop fetch
408 1.1 bouyer * them from cache
409 1.1 bouyer */
410 1.1 bouyer }
411 1.1 bouyer }
412 1.1 bouyer
413 1.1 bouyer if (flock(diskfd, LOCK_UN))
414 1.1 bouyer msg("flock(LOCK_UN) failed: %s\n",
415 1.1 bouyer strerror(errno));
416 1.1 bouyer return;
417 1.1 bouyer }
418 1.1 bouyer
419 1.1 bouyer void
420 1.6 lukem printcachestats(void)
421 1.1 bouyer {
422 1.1 bouyer #ifdef STATS
423 1.1 bouyer fprintf(stderr, "Pid %d: %d reads (%u bytes) "
424 1.1 bouyer "%d physical reads (%u bytes) %d%% hits, %d%% overhead\n",
425 1.1 bouyer getpid(), nreads, (u_int) readsize, nphysread,
426 1.1 bouyer (u_int) physreadsize, (nreads - nphysread) * 100 / nreads,
427 1.1 bouyer (int) (((physreadsize - readsize) * 100) / readsize));
428 1.1 bouyer #endif
429 1.1 bouyer }
430