1998-11-04 00:29:01 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1998-11-02 23:28:11 +00:00
|
|
|
*/
|
|
|
|
|
2003-08-25 23:30:41 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-11-02 23:28:11 +00:00
|
|
|
/*
|
|
|
|
* Simple LRU block cache
|
|
|
|
*/
|
|
|
|
|
2004-10-03 16:34:01 +00:00
|
|
|
#include <sys/stdint.h>
|
|
|
|
|
1998-11-02 23:28:11 +00:00
|
|
|
#include <stand.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <bitstring.h>
|
|
|
|
|
|
|
|
#include "bootstrap.h"
|
|
|
|
|
|
|
|
/* #define BCACHE_DEBUG */
|
|
|
|
|
|
|
|
#ifdef BCACHE_DEBUG
|
|
|
|
#define BCACHE_TIMEOUT 10
|
2001-12-10 08:09:49 +00:00
|
|
|
# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
|
1998-11-02 23:28:11 +00:00
|
|
|
#else
|
|
|
|
#define BCACHE_TIMEOUT 2
|
|
|
|
# define DEBUG(fmt, args...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
struct bcachectl
|
|
|
|
{
|
|
|
|
daddr_t bc_blkno;
|
|
|
|
time_t bc_stamp;
|
1998-11-02 23:50:59 +00:00
|
|
|
int bc_count;
|
1998-11-02 23:28:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct bcachectl *bcache_ctl;
|
|
|
|
static caddr_t bcache_data;
|
|
|
|
static bitstr_t *bcache_miss;
|
2000-08-03 09:14:02 +00:00
|
|
|
static u_int bcache_nblks;
|
|
|
|
static u_int bcache_blksize;
|
|
|
|
static u_int bcache_hits, bcache_misses, bcache_ops, bcache_bypasses;
|
|
|
|
static u_int bcache_flushes;
|
|
|
|
static u_int bcache_bcount;
|
1998-11-02 23:28:11 +00:00
|
|
|
|
2001-12-11 00:10:00 +00:00
|
|
|
static void bcache_invalidate(daddr_t blkno);
|
1998-11-02 23:28:11 +00:00
|
|
|
static void bcache_insert(caddr_t buf, daddr_t blkno);
|
|
|
|
static int bcache_lookup(caddr_t buf, daddr_t blkno);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialise the cache for (nblks) of (bsize).
|
|
|
|
*/
|
|
|
|
int
|
2000-08-03 09:14:02 +00:00
|
|
|
bcache_init(u_int nblks, size_t bsize)
|
1998-11-02 23:28:11 +00:00
|
|
|
{
|
|
|
|
/* discard any old contents */
|
|
|
|
if (bcache_data != NULL) {
|
|
|
|
free(bcache_data);
|
|
|
|
bcache_data = NULL;
|
|
|
|
free(bcache_ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate control structures */
|
|
|
|
bcache_nblks = nblks;
|
|
|
|
bcache_blksize = bsize;
|
|
|
|
bcache_data = malloc(bcache_nblks * bcache_blksize);
|
|
|
|
bcache_ctl = (struct bcachectl *)malloc(bcache_nblks * sizeof(struct bcachectl));
|
|
|
|
bcache_miss = bit_alloc((bcache_nblks + 1) / 2);
|
|
|
|
if ((bcache_data == NULL) || (bcache_ctl == NULL) || (bcache_miss == NULL)) {
|
|
|
|
if (bcache_miss)
|
|
|
|
free(bcache_miss);
|
|
|
|
if (bcache_ctl)
|
|
|
|
free(bcache_ctl);
|
|
|
|
if (bcache_data)
|
|
|
|
free(bcache_data);
|
|
|
|
bcache_data = NULL;
|
|
|
|
return(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2000-03-15 01:56:12 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Flush the cache
|
|
|
|
*/
|
|
|
|
void
|
2000-08-03 09:14:02 +00:00
|
|
|
bcache_flush(void)
|
2000-03-15 01:56:12 +00:00
|
|
|
{
|
2000-08-03 09:14:02 +00:00
|
|
|
u_int i;
|
2000-03-15 01:56:12 +00:00
|
|
|
|
|
|
|
bcache_flushes++;
|
|
|
|
|
|
|
|
/* Flush the cache */
|
1998-11-19 18:12:03 +00:00
|
|
|
for (i = 0; i < bcache_nblks; i++) {
|
1998-11-02 23:50:59 +00:00
|
|
|
bcache_ctl[i].bc_count = -1;
|
1998-11-19 18:12:03 +00:00
|
|
|
bcache_ctl[i].bc_blkno = -1;
|
|
|
|
}
|
1998-11-02 23:28:11 +00:00
|
|
|
}
|
|
|
|
|
2001-12-11 00:10:00 +00:00
|
|
|
/*
|
|
|
|
* Handle a write request; write directly to the disk, and populate the
|
|
|
|
* cache with the new values.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
write_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
|
|
|
|
char *buf, size_t *rsize)
|
|
|
|
{
|
|
|
|
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
|
|
|
|
daddr_t i, nblk;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
nblk = size / bcache_blksize;
|
|
|
|
|
|
|
|
/* Invalidate the blocks being written */
|
|
|
|
for (i = 0; i < nblk; i++) {
|
|
|
|
bcache_invalidate(blk + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the blocks */
|
|
|
|
err = dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize);
|
|
|
|
|
|
|
|
/* Populate the block cache with the new data */
|
|
|
|
if (err == 0) {
|
|
|
|
for (i = 0; i < nblk; i++) {
|
|
|
|
bcache_insert(buf + (i * bcache_blksize),blk + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle a read request; fill in parts of the request that can
|
1998-11-02 23:28:11 +00:00
|
|
|
* be satisfied by the cache, use the supplied strategy routine to do
|
|
|
|
* device I/O and then use the I/O results to populate the cache.
|
|
|
|
*/
|
2001-12-11 00:10:00 +00:00
|
|
|
static int
|
|
|
|
read_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
|
2000-08-03 09:14:02 +00:00
|
|
|
char *buf, size_t *rsize)
|
1998-11-02 23:28:11 +00:00
|
|
|
{
|
|
|
|
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
|
2000-08-03 09:14:02 +00:00
|
|
|
int p_size, result;
|
|
|
|
daddr_t p_blk, i, j, nblk;
|
1998-11-02 23:28:11 +00:00
|
|
|
caddr_t p_buf;
|
|
|
|
|
|
|
|
nblk = size / bcache_blksize;
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
/* Satisfy any cache hits up front */
|
|
|
|
for (i = 0; i < nblk; i++) {
|
|
|
|
if (bcache_lookup(buf + (bcache_blksize * i), blk + i)) {
|
|
|
|
bit_set(bcache_miss, i); /* cache miss */
|
|
|
|
bcache_misses++;
|
|
|
|
} else {
|
|
|
|
bit_clear(bcache_miss, i); /* cache hit */
|
|
|
|
bcache_hits++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go back and fill in any misses XXX optimise */
|
|
|
|
p_blk = -1;
|
|
|
|
p_buf = NULL;
|
|
|
|
p_size = 0;
|
|
|
|
for (i = 0; i < nblk; i++) {
|
|
|
|
if (bit_test(bcache_miss, i)) {
|
|
|
|
/* miss, add to pending transfer */
|
|
|
|
if (p_blk == -1) {
|
|
|
|
p_blk = blk + i;
|
|
|
|
p_buf = buf + (bcache_blksize * i);
|
|
|
|
p_size = 1;
|
|
|
|
} else {
|
|
|
|
p_size++;
|
|
|
|
}
|
|
|
|
} else if (p_blk != -1) {
|
|
|
|
/* hit, complete pending transfer */
|
|
|
|
result = dd->dv_strategy(dd->dv_devdata, rw, p_blk, p_size * bcache_blksize, p_buf, NULL);
|
|
|
|
if (result != 0)
|
|
|
|
goto done;
|
|
|
|
for (j = 0; j < p_size; j++)
|
|
|
|
bcache_insert(p_buf + (j * bcache_blksize), p_blk + j);
|
|
|
|
p_blk = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p_blk != -1) {
|
|
|
|
/* pending transfer left */
|
|
|
|
result = dd->dv_strategy(dd->dv_devdata, rw, p_blk, p_size * bcache_blksize, p_buf, NULL);
|
|
|
|
if (result != 0)
|
|
|
|
goto done;
|
|
|
|
for (j = 0; j < p_size; j++)
|
|
|
|
bcache_insert(p_buf + (j * bcache_blksize), p_blk + j);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if ((result == 0) && (rsize != NULL))
|
|
|
|
*rsize = size;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2001-12-11 00:10:00 +00:00
|
|
|
/*
|
|
|
|
* Requests larger than 1/2 the cache size will be bypassed and go
|
|
|
|
* directly to the disk. XXX tune this.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
bcache_strategy(void *devdata, int unit, int rw, daddr_t blk, size_t size,
|
|
|
|
char *buf, size_t *rsize)
|
|
|
|
{
|
|
|
|
static int bcache_unit = -1;
|
|
|
|
struct bcache_devdata *dd = (struct bcache_devdata *)devdata;
|
|
|
|
|
|
|
|
bcache_ops++;
|
|
|
|
|
|
|
|
if(bcache_unit != unit) {
|
|
|
|
bcache_flush();
|
|
|
|
bcache_unit = unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bypass large requests, or when the cache is inactive */
|
|
|
|
if ((bcache_data == NULL) || ((size * 2 / bcache_blksize) > bcache_nblks)) {
|
|
|
|
DEBUG("bypass %d from %d", size / bcache_blksize, blk);
|
|
|
|
bcache_bypasses++;
|
|
|
|
return(dd->dv_strategy(dd->dv_devdata, rw, blk, size, buf, rsize));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rw) {
|
|
|
|
case F_READ:
|
|
|
|
return read_strategy(devdata, unit, rw, blk, size, buf, rsize);
|
|
|
|
case F_WRITE:
|
|
|
|
return write_strategy(devdata, unit, rw, blk, size, buf, rsize);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1998-11-02 23:28:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a block into the cache. Retire the oldest block to do so, if required.
|
1998-11-02 23:50:59 +00:00
|
|
|
*
|
|
|
|
* XXX the LRU algorithm will fail after 2^31 blocks have been transferred.
|
1998-11-02 23:28:11 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bcache_insert(caddr_t buf, daddr_t blkno)
|
|
|
|
{
|
|
|
|
time_t now;
|
2000-08-03 09:14:02 +00:00
|
|
|
int cand, ocount;
|
|
|
|
u_int i;
|
1998-11-02 23:28:11 +00:00
|
|
|
|
|
|
|
time(&now);
|
1998-11-02 23:50:59 +00:00
|
|
|
cand = 0; /* assume the first block */
|
|
|
|
ocount = bcache_ctl[0].bc_count;
|
1998-11-02 23:28:11 +00:00
|
|
|
|
|
|
|
/* find the oldest block */
|
1998-11-02 23:50:59 +00:00
|
|
|
for (i = 1; i < bcache_nblks; i++) {
|
1998-11-02 23:28:11 +00:00
|
|
|
if (bcache_ctl[i].bc_blkno == blkno) {
|
|
|
|
/* reuse old entry */
|
|
|
|
cand = i;
|
|
|
|
break;
|
|
|
|
}
|
1998-11-02 23:50:59 +00:00
|
|
|
if (bcache_ctl[i].bc_count < ocount) {
|
|
|
|
ocount = bcache_ctl[i].bc_count;
|
1998-11-02 23:28:11 +00:00
|
|
|
cand = i;
|
1998-11-02 23:50:59 +00:00
|
|
|
}
|
1998-11-02 23:28:11 +00:00
|
|
|
}
|
|
|
|
|
1998-11-02 23:50:59 +00:00
|
|
|
DEBUG("insert blk %d -> %d @ %d # %d", blkno, cand, now, bcache_bcount);
|
1998-11-02 23:28:11 +00:00
|
|
|
bcopy(buf, bcache_data + (bcache_blksize * cand), bcache_blksize);
|
|
|
|
bcache_ctl[cand].bc_blkno = blkno;
|
|
|
|
bcache_ctl[cand].bc_stamp = now;
|
1998-11-02 23:50:59 +00:00
|
|
|
bcache_ctl[cand].bc_count = bcache_bcount++;
|
1998-11-02 23:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for a block in the cache. Blocks more than BCACHE_TIMEOUT seconds old
|
1998-11-02 23:50:59 +00:00
|
|
|
* may be stale (removable media) and thus are discarded. Copy the block out
|
1998-11-02 23:28:11 +00:00
|
|
|
* if successful and return zero, or return nonzero on failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
bcache_lookup(caddr_t buf, daddr_t blkno)
|
|
|
|
{
|
|
|
|
time_t now;
|
2000-08-03 09:14:02 +00:00
|
|
|
u_int i;
|
1998-11-02 23:28:11 +00:00
|
|
|
|
|
|
|
time(&now);
|
|
|
|
|
|
|
|
for (i = 0; i < bcache_nblks; i++)
|
|
|
|
/* cache hit? */
|
|
|
|
if ((bcache_ctl[i].bc_blkno == blkno) && ((bcache_ctl[i].bc_stamp + BCACHE_TIMEOUT) >= now)) {
|
|
|
|
bcopy(bcache_data + (bcache_blksize * i), buf, bcache_blksize);
|
|
|
|
DEBUG("hit blk %d <- %d (now %d then %d)", blkno, i, now, bcache_ctl[i].bc_stamp);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
return(ENOENT);
|
|
|
|
}
|
|
|
|
|
2001-12-11 00:10:00 +00:00
|
|
|
/*
|
|
|
|
* Invalidate a block from the cache.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bcache_invalidate(daddr_t blkno)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < bcache_nblks; i++) {
|
|
|
|
if (bcache_ctl[i].bc_blkno == blkno) {
|
|
|
|
bcache_ctl[i].bc_count = -1;
|
|
|
|
bcache_ctl[i].bc_blkno = -1;
|
|
|
|
DEBUG("invalidate blk %d", blkno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-02 23:28:11 +00:00
|
|
|
COMMAND_SET(bcachestat, "bcachestat", "get disk block cache stats", command_bcache);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_bcache(int argc, char *argv[])
|
|
|
|
{
|
2000-08-03 09:14:02 +00:00
|
|
|
u_int i;
|
1998-11-02 23:28:11 +00:00
|
|
|
|
|
|
|
for (i = 0; i < bcache_nblks; i++) {
|
2004-10-03 16:34:01 +00:00
|
|
|
printf("%08jx %04x %04x|", (uintmax_t)bcache_ctl[i].bc_blkno, (unsigned int)bcache_ctl[i].bc_stamp & 0xffff, bcache_ctl[i].bc_count & 0xffff);
|
1998-11-02 23:28:11 +00:00
|
|
|
if (((i + 1) % 4) == 0)
|
|
|
|
printf("\n");
|
|
|
|
}
|
2000-03-15 01:56:12 +00:00
|
|
|
printf("\n%d ops %d bypasses %d hits %d misses %d flushes\n", bcache_ops, bcache_bypasses, bcache_hits, bcache_misses, bcache_flushes);
|
1998-11-02 23:28:11 +00:00
|
|
|
return(CMD_OK);
|
|
|
|
}
|
|
|
|
|