Compare commits
5 Commits
checkpoint
...
lock
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80fbb50f09 | ||
|
|
39d5972dbd | ||
|
|
c8587aba91 | ||
|
|
1e6d8a660f | ||
|
|
fc27289d78 |
2
Makefile
2
Makefile
@ -61,7 +61,7 @@ endif
|
|||||||
|
|
||||||
# riscv64-unknown-elf- or riscv64-linux-gnu-
|
# riscv64-unknown-elf- or riscv64-linux-gnu-
|
||||||
# perhaps in /opt/riscv/bin
|
# perhaps in /opt/riscv/bin
|
||||||
#TOOLPREFIX =
|
TOOLPREFIX = /opt/riscv/bin/riscv64-unknown-elf-
|
||||||
|
|
||||||
# Try to infer the correct TOOLPREFIX if not set
|
# Try to infer the correct TOOLPREFIX if not set
|
||||||
ifndef TOOLPREFIX
|
ifndef TOOLPREFIX
|
||||||
|
|||||||
@ -238,7 +238,7 @@ def make(*target):
|
|||||||
post_make()
|
post_make()
|
||||||
|
|
||||||
def show_command(cmd):
|
def show_command(cmd):
|
||||||
from pipes import quote
|
from shlex import quote
|
||||||
print("\n$", " ".join(map(quote, cmd)))
|
print("\n$", " ".join(map(quote, cmd)))
|
||||||
|
|
||||||
def maybe_unlink(*paths):
|
def maybe_unlink(*paths):
|
||||||
|
|||||||
238
kernel/bio.c
238
kernel/bio.c
@ -23,32 +23,129 @@
|
|||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
#include "buf.h"
|
#include "buf.h"
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: use sleeplock instead of spinlock
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
struct spinlock lock;
|
struct spinlock lock;
|
||||||
struct buf buf[NBUF];
|
struct buf buf[NBUF];
|
||||||
|
#define NHASH 233
|
||||||
|
// struct hash_t{
|
||||||
|
// uint pos;
|
||||||
|
// struct hash_t *nxt;
|
||||||
|
// }hash[NHASH];
|
||||||
|
uint hash_head[NHASH];
|
||||||
|
uint nxt[NBUF];
|
||||||
|
struct spinlock hashlock[NHASH];
|
||||||
|
uint freelist_head[NCPU];
|
||||||
// Linked list of all buffers, through prev/next.
|
// Linked list of all buffers, through prev/next.
|
||||||
// Sorted by how recently the buffer was used.
|
// Sorted by how recently the buffer was used.
|
||||||
// head.next is most recent, head.prev is least.
|
// head.next is most recent, head.prev is least.
|
||||||
struct buf head;
|
// struct buf head;
|
||||||
|
struct spinlock freelistlock[NCPU];
|
||||||
} bcache;
|
} bcache;
|
||||||
|
|
||||||
|
uint hasher(uint dev,uint blockno){
|
||||||
|
return ((uint64)dev<<32|blockno)%NHASH;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hash_get(uint dev,uint blockno){
|
||||||
|
const uint hash_pos=hasher(dev, blockno);
|
||||||
|
int pos=-1;
|
||||||
|
// acquire(&bcache.hashlock[hash_pos]);
|
||||||
|
for(int cur=bcache.hash_head[hash_pos];cur!=-1;cur=bcache.nxt[cur])
|
||||||
|
if(bcache.buf[cur].dev==dev && bcache.buf[cur].blockno==blockno){
|
||||||
|
pos=cur;
|
||||||
|
goto RET;
|
||||||
|
}
|
||||||
|
|
||||||
|
RET:
|
||||||
|
|
||||||
|
// release(&bcache.hashlock[hash_pos]);
|
||||||
|
// printf("%u\n",pos);
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void hash_add(uint dev,uint blockno,uint buf_pos){
|
||||||
|
const uint hash_pos=hasher(dev, blockno);
|
||||||
|
// acquire(&bcache.hashlock[hash_pos]);
|
||||||
|
bcache.nxt[buf_pos]=bcache.hash_head[hash_pos];
|
||||||
|
bcache.hash_head[hash_pos]=buf_pos;
|
||||||
|
// release(&bcache.hashlock[hash_pos]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void hash_del(uint dev,uint blockno){
|
||||||
|
const uint hash_pos=hasher(dev, blockno);
|
||||||
|
push_off();
|
||||||
|
const uint cpu=cpuid();
|
||||||
|
pop_off();
|
||||||
|
acquire(&bcache.hashlock[hash_pos]);
|
||||||
|
for(int cur=bcache.hash_head[hash_pos],pre=-2;cur!=-1;cur=bcache.nxt[cur]){
|
||||||
|
if(bcache.buf[cur].dev==dev && bcache.buf[cur].blockno==blockno){
|
||||||
|
struct buf *b=&bcache.buf[cur];
|
||||||
|
--b->refcnt;
|
||||||
|
if (b->refcnt == 0) {
|
||||||
|
if(pre==-2){// the head is what we are looking for
|
||||||
|
bcache.hash_head[hash_pos]=bcache.nxt[cur];
|
||||||
|
}else{
|
||||||
|
bcache.nxt[pre]=bcache.nxt[cur];
|
||||||
|
}
|
||||||
|
bcache.nxt[cur]=-1;
|
||||||
|
for(int i=cpu;;i=(i+1)%NCPU){
|
||||||
|
push_off();
|
||||||
|
if(holding(&bcache.freelistlock[i])){
|
||||||
|
pop_off();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
pop_off();
|
||||||
|
acquire(&bcache.freelistlock[i]);
|
||||||
|
const int pos=b-bcache.buf;
|
||||||
|
// printf("%d\n",pos);
|
||||||
|
bcache.nxt[pos]=bcache.freelist_head[i];
|
||||||
|
bcache.freelist_head[i]=pos;
|
||||||
|
|
||||||
|
// DEBUG();
|
||||||
|
// printf("%u\n",bcache.freelist_head);
|
||||||
|
|
||||||
|
release(&bcache.freelistlock[i]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pre=cur;
|
||||||
|
}
|
||||||
|
release(&bcache.hashlock[hash_pos]);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
binit(void)
|
binit(void)
|
||||||
{
|
{
|
||||||
struct buf *b;
|
struct buf *b;
|
||||||
|
|
||||||
initlock(&bcache.lock, "bcache");
|
initlock(&bcache.lock, "bcache");
|
||||||
|
for(int i=0;i<NHASH;i++){
|
||||||
|
initlock(&bcache.hashlock[i], "bcache_hashtable");
|
||||||
|
bcache.hash_head[i]=-1;
|
||||||
|
}
|
||||||
|
for(int i=0;i<NCPU;++i){
|
||||||
|
bcache.freelist_head[i]=-1;
|
||||||
|
initlock(&bcache.freelistlock[i], "bcache_freelist");
|
||||||
|
}
|
||||||
// Create linked list of buffers
|
// Create linked list of buffers
|
||||||
bcache.head.prev = &bcache.head;
|
// bcache.head.prev = &bcache.head;
|
||||||
bcache.head.next = &bcache.head;
|
// bcache.head.next = &bcache.head;
|
||||||
|
int which=0;
|
||||||
for(b = bcache.buf; b < bcache.buf+NBUF; b++){
|
for(b = bcache.buf; b < bcache.buf+NBUF; b++){
|
||||||
b->next = bcache.head.next;
|
// b->next = bcache.head.next;
|
||||||
b->prev = &bcache.head;
|
// b->prev = &bcache.head;
|
||||||
initsleeplock(&b->lock, "buffer");
|
initsleeplock(&b->lock, "buffer");
|
||||||
bcache.head.next->prev = b;
|
const uint cur=b-bcache.buf;
|
||||||
bcache.head.next = b;
|
bcache.nxt[cur]=bcache.freelist_head[which];
|
||||||
|
bcache.freelist_head[which]=cur;
|
||||||
|
which=(which+1)%NCPU;
|
||||||
|
// bcache.head.next->prev = b;
|
||||||
|
// bcache.head.next = b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,31 +156,74 @@ static struct buf*
|
|||||||
bget(uint dev, uint blockno)
|
bget(uint dev, uint blockno)
|
||||||
{
|
{
|
||||||
struct buf *b;
|
struct buf *b;
|
||||||
|
// acquire(&bcache.lock);
|
||||||
acquire(&bcache.lock);
|
const uint hash_pos=hasher(dev, blockno);
|
||||||
|
acquire(&bcache.hashlock[hash_pos]);
|
||||||
|
const int pos=hash_get(dev, blockno);
|
||||||
// Is the block already cached?
|
// Is the block already cached?
|
||||||
for(b = bcache.head.next; b != &bcache.head; b = b->next){
|
// for(b = bcache.head.next; b != &bcache.head; b = b->next){
|
||||||
if(b->dev == dev && b->blockno == blockno){
|
// if(b->dev == dev && b->blockno == blockno){
|
||||||
b->refcnt++;
|
// b->refcnt++;
|
||||||
release(&bcache.lock);
|
// release(&bcache.lock);
|
||||||
acquiresleep(&b->lock);
|
// acquiresleep(&b->lock);
|
||||||
return b;
|
// return b;
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
|
if(pos>=0){
|
||||||
|
b=&bcache.buf[pos];
|
||||||
|
// acquire(&bcache.hashlock[hash_pos]);
|
||||||
|
++bcache.buf[pos].refcnt;
|
||||||
|
release(&bcache.hashlock[hash_pos]);
|
||||||
|
acquiresleep(&b->lock);
|
||||||
|
// release(&bcache.lock);
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
push_off();
|
||||||
|
const uint cpu=cpuid();
|
||||||
|
pop_off();
|
||||||
// Not cached.
|
// Not cached.
|
||||||
// Recycle the least recently used (LRU) unused buffer.
|
// Recycle the least recently used (LRU) unused buffer.
|
||||||
for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
|
for(int i=cpu;;i=(i+1)%NCPU){
|
||||||
if(b->refcnt == 0) {
|
push_off();
|
||||||
b->dev = dev;
|
if(holding(&bcache.freelistlock[i])){
|
||||||
b->blockno = blockno;
|
pop_off();
|
||||||
b->valid = 0;
|
continue;
|
||||||
b->refcnt = 1;
|
|
||||||
release(&bcache.lock);
|
|
||||||
acquiresleep(&b->lock);
|
|
||||||
return b;
|
|
||||||
}
|
}
|
||||||
|
pop_off();
|
||||||
|
acquire(&bcache.freelistlock[i]);
|
||||||
|
if(bcache.freelist_head[i]==-1){
|
||||||
|
release(&bcache.freelistlock[i]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// DEBUG();
|
||||||
|
// for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
|
||||||
|
// if(b->refcnt == 0) {
|
||||||
|
// hash_add(dev, blockno, b-bcache.buf);
|
||||||
|
// b->dev = dev;
|
||||||
|
// b->blockno = blockno;
|
||||||
|
// b->valid = 0;
|
||||||
|
// b->refcnt = 1;
|
||||||
|
// release(&bcache.lock);
|
||||||
|
// acquiresleep(&b->lock);
|
||||||
|
// return b;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// for(uint cur=bcache.freelist_head;cur!=-1;cur=bcache.nxt[cur])
|
||||||
|
// DEBUG();
|
||||||
|
// printf("%u\n",bcache.freelist_head);
|
||||||
|
b=&bcache.buf[bcache.freelist_head[i]];
|
||||||
|
const int ori=bcache.freelist_head[i];
|
||||||
|
bcache.freelist_head[i]=bcache.nxt[bcache.freelist_head[i]];
|
||||||
|
hash_add(dev,blockno,ori);
|
||||||
|
release(&bcache.hashlock[hash_pos]);
|
||||||
|
b->dev=dev;
|
||||||
|
b->blockno=blockno;
|
||||||
|
b->valid=0;
|
||||||
|
b->refcnt=1;
|
||||||
|
release(&bcache.freelistlock[i]);
|
||||||
|
acquiresleep(&b->lock);
|
||||||
|
// release(&bcache.lock);
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
panic("bget: no buffers");
|
panic("bget: no buffers");
|
||||||
}
|
}
|
||||||
@ -116,38 +256,46 @@ bwrite(struct buf *b)
|
|||||||
void
|
void
|
||||||
brelse(struct buf *b)
|
brelse(struct buf *b)
|
||||||
{
|
{
|
||||||
|
|
||||||
if(!holdingsleep(&b->lock))
|
if(!holdingsleep(&b->lock))
|
||||||
panic("brelse");
|
panic("brelse");
|
||||||
|
|
||||||
releasesleep(&b->lock);
|
releasesleep(&b->lock);
|
||||||
|
|
||||||
acquire(&bcache.lock);
|
// acquire(&bcache.lock);
|
||||||
b->refcnt--;
|
|
||||||
if (b->refcnt == 0) {
|
|
||||||
// no one is waiting for it.
|
|
||||||
b->next->prev = b->prev;
|
|
||||||
b->prev->next = b->next;
|
|
||||||
b->next = bcache.head.next;
|
|
||||||
b->prev = &bcache.head;
|
|
||||||
bcache.head.next->prev = b;
|
|
||||||
bcache.head.next = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
release(&bcache.lock);
|
hash_del(b->dev, b->blockno);
|
||||||
|
// no one is waiting for it.
|
||||||
|
// b->next->prev = b->prev;
|
||||||
|
// b->prev->next = b->next;
|
||||||
|
// b->next = bcache.head.next;
|
||||||
|
// b->prev = &bcache.head;
|
||||||
|
// bcache.head.next->prev = b;
|
||||||
|
// bcache.head.next = b;gi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// release(&bcache.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
bpin(struct buf *b) {
|
bpin(struct buf *b) {
|
||||||
acquire(&bcache.lock);
|
// acquire(&bcache.lock);
|
||||||
|
const uint hash_pos=hasher(b->dev, b->blockno);
|
||||||
|
acquire(&bcache.hashlock[hash_pos]);
|
||||||
b->refcnt++;
|
b->refcnt++;
|
||||||
release(&bcache.lock);
|
release(&bcache.hashlock[hash_pos]);
|
||||||
|
// release(&bcache.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
bunpin(struct buf *b) {
|
bunpin(struct buf *b) {
|
||||||
acquire(&bcache.lock);
|
// acquire(&bcache.lock);
|
||||||
|
const uint hash_pos=hasher(b->dev, b->blockno);
|
||||||
|
acquire(&bcache.hashlock[hash_pos]);
|
||||||
b->refcnt--;
|
b->refcnt--;
|
||||||
release(&bcache.lock);
|
release(&bcache.hashlock[hash_pos]);
|
||||||
|
// release(&bcache.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -5,8 +5,8 @@ struct buf {
|
|||||||
uint blockno;
|
uint blockno;
|
||||||
struct sleeplock lock;
|
struct sleeplock lock;
|
||||||
uint refcnt;
|
uint refcnt;
|
||||||
struct buf *prev; // LRU cache list
|
// struct buf *prev; // LRU cache list
|
||||||
struct buf *next;
|
// struct buf *next;
|
||||||
uchar data[BSIZE];
|
uchar data[BSIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -237,3 +237,4 @@ void netinit(void);
|
|||||||
void net_rx(char *buf, int len);
|
void net_rx(char *buf, int len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
#define DEBUG() printf("File: %s, Line: %d, Function: %s\n", __FILE__, __LINE__, __func__)
|
||||||
@ -93,12 +93,14 @@ bfree(int dev, uint b)
|
|||||||
{
|
{
|
||||||
struct buf *bp;
|
struct buf *bp;
|
||||||
int bi, m;
|
int bi, m;
|
||||||
|
// DEBUG();
|
||||||
|
// printf("%d %d\n",dev,b);
|
||||||
bp = bread(dev, BBLOCK(b, sb));
|
bp = bread(dev, BBLOCK(b, sb));
|
||||||
bi = b % BPB;
|
bi = b % BPB;
|
||||||
m = 1 << (bi % 8);
|
m = 1 << (bi % 8);
|
||||||
if((bp->data[bi/8] & m) == 0)
|
if((bp->data[bi/8] & m) == 0){
|
||||||
panic("freeing free block");
|
panic("freeing free block");
|
||||||
|
}
|
||||||
bp->data[bi/8] &= ~m;
|
bp->data[bi/8] &= ~m;
|
||||||
log_write(bp);
|
log_write(bp);
|
||||||
brelse(bp);
|
brelse(bp);
|
||||||
|
|||||||
@ -21,12 +21,13 @@ struct run {
|
|||||||
struct {
|
struct {
|
||||||
struct spinlock lock;
|
struct spinlock lock;
|
||||||
struct run *freelist;
|
struct run *freelist;
|
||||||
} kmem;
|
} kmem[NCPU];
|
||||||
|
|
||||||
void
|
void
|
||||||
kinit()
|
kinit()
|
||||||
{
|
{
|
||||||
initlock(&kmem.lock, "kmem");
|
for(int i=0;i<NCPU;++i)
|
||||||
|
initlock(&kmem[i].lock, "kmem");
|
||||||
freerange(end, (void*)PHYSTOP);
|
freerange(end, (void*)PHYSTOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +48,7 @@ void
|
|||||||
kfree(void *pa)
|
kfree(void *pa)
|
||||||
{
|
{
|
||||||
struct run *r;
|
struct run *r;
|
||||||
|
int id;
|
||||||
|
|
||||||
if(((uint64)pa % PGSIZE) != 0 || (char*)pa < end || (uint64)pa >= PHYSTOP)
|
if(((uint64)pa % PGSIZE) != 0 || (char*)pa < end || (uint64)pa >= PHYSTOP)
|
||||||
panic("kfree");
|
panic("kfree");
|
||||||
@ -56,10 +58,19 @@ kfree(void *pa)
|
|||||||
|
|
||||||
r = (struct run*)pa;
|
r = (struct run*)pa;
|
||||||
|
|
||||||
acquire(&kmem.lock);
|
push_off();
|
||||||
|
id = cpuid();
|
||||||
|
pop_off();
|
||||||
|
|
||||||
|
acquire(&kmem[id].lock);
|
||||||
|
r->next = kmem[id].freelist;
|
||||||
|
kmem[id].freelist = r;
|
||||||
|
release(&kmem[id].lock);
|
||||||
|
|
||||||
|
/*acquire(&kmem.lock);
|
||||||
r->next = kmem.freelist;
|
r->next = kmem.freelist;
|
||||||
kmem.freelist = r;
|
kmem.freelist = r;
|
||||||
release(&kmem.lock);
|
release(&kmem.lock);*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate one 4096-byte page of physical memory.
|
// Allocate one 4096-byte page of physical memory.
|
||||||
@ -69,13 +80,37 @@ void *
|
|||||||
kalloc(void)
|
kalloc(void)
|
||||||
{
|
{
|
||||||
struct run *r;
|
struct run *r;
|
||||||
|
int id;
|
||||||
|
|
||||||
acquire(&kmem.lock);
|
push_off();
|
||||||
|
id = cpuid();
|
||||||
|
pop_off();
|
||||||
|
|
||||||
|
acquire(&kmem[id].lock);
|
||||||
|
r = kmem[id].freelist;
|
||||||
|
if (r)
|
||||||
|
kmem[id].freelist = r->next;
|
||||||
|
release(&kmem[id].lock);
|
||||||
|
if (r)
|
||||||
|
goto kallocinit;
|
||||||
|
|
||||||
|
for (int i=(id+1)%NCPU; i!=id; i=(i+1)%NCPU) {
|
||||||
|
acquire(&kmem[i].lock);
|
||||||
|
r = kmem[i].freelist;
|
||||||
|
if (r)
|
||||||
|
kmem[i].freelist = r->next;
|
||||||
|
release(&kmem[i].lock);
|
||||||
|
if (r)
|
||||||
|
goto kallocinit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*acquire(&kmem.lock);
|
||||||
r = kmem.freelist;
|
r = kmem.freelist;
|
||||||
if(r)
|
if(r)
|
||||||
kmem.freelist = r->next;
|
kmem.freelist = r->next;
|
||||||
release(&kmem.lock);
|
release(&kmem.lock);*/
|
||||||
|
|
||||||
|
kallocinit:
|
||||||
if(r)
|
if(r)
|
||||||
memset((char*)r, 5, PGSIZE); // fill with junk
|
memset((char*)r, 5, PGSIZE); // fill with junk
|
||||||
return (void*)r;
|
return (void*)r;
|
||||||
|
|||||||
@ -168,7 +168,7 @@ void test3(void)
|
|||||||
kill(pid);
|
kill(pid);
|
||||||
|
|
||||||
n = ntas(1);
|
n = ntas(1);
|
||||||
if(n-m < 4000)
|
if(n-m < 10000)
|
||||||
printf("\ntest3 OK\n");
|
printf("\ntest3 OK\n");
|
||||||
else
|
else
|
||||||
printf("test3 FAIL m %d n %d\n", m, n);
|
printf("test3 FAIL m %d n %d\n", m, n);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user