LOCK
Memory allocator (moderate)
这部分比较简单,就是让每个cpu有自己的freelist。
在初始化时将所有的空页放入cpu0的freelist。如果一个cpu需要页表了并且自身的freelist分配完毕了,就去其它cpu的freelist中偷取空页。
// Physical memory allocator, for user processes,
// kernel stacks, page-table pages,
// and pipe buffers. Allocates whole 4096-byte pages.
#include "types.h"
#include "param.h"
#include "memlayout.h"
#include "spinlock.h"
#include "riscv.h"
#include "defs.h"
void freerange(void *pa_start, void *pa_end);
extern char end[]; // first address after kernel.
// defined by kernel.ld.
struct run {
struct run *next;
};
struct {
struct spinlock lock;
struct run *freelist;
char lockname[8];
} kmems[NCPU];// 八个锁对应八个 CPU
void
kinit()
{
// initlock(&kmem.lock, "kmem");
for(int i = 0; i < NCPU; i++){
snprintf(kmems[i].lockname, 8, "kmem_%d", i);
initlock(&kmems[i].lock, "kmem");
}
freerange(end, (void*)PHYSTOP);
}
void
freerange(void *pa_start, void *pa_end)
{
char *p;
p = (char*)PGROUNDUP((uint64)pa_start);
for(; p + PGSIZE <= (char*)pa_end; p += PGSIZE)
kfree(p);
}
// Free the page of physical memory pointed at by pa,
// which normally should have been returned by a
// call to kalloc(). (The exception is when
// initializing the allocator; see kinit above.)
void
kfree(void *pa)
{
if(((uint64)pa % PGSIZE) != 0 || (char*)pa < end || (uint64)pa >= PHYSTOP)
panic("kfree");
// Fill with junk to catch dangling refs.
memset(pa, 1, PGSIZE);
struct run *r;
push_off(); // 关闭中断
int cpu_id = cpuid(); // 获取当前 CPU 的 ID
acquire(&kmems[cpu_id].lock);
pop_off(); // 重新打开中断
r = (struct run*)pa;
r->next = kmems[cpu_id].freelist;
kmems[cpu_id].freelist = r;
release(&kmems[cpu_id].lock);
}
struct run *steal(){
// push_off(); // 关闭中断
// int c = cpuid();
// pop_off(); // 重新打开中断
// if(c != cpu_id)
// printf("stral: wrong cpu_id\n");
// panic("stral: wrong cpu_id");
for(int i=0;i<NCPU;i++){
struct run *r;
push_off(); // 关闭中断
acquire(&kmems[i].lock);
pop_off(); // 重新打开中断
r = kmems[i].freelist;
if(r){
kmems[i].freelist = r->next;
release(&kmems[i].lock);
return r;
}
release(&kmems[i].lock);
}
return 0;
}
// Allocate one 4096-byte page of physical memory.
// Returns a pointer that the kernel can use.
// Returns 0 if the memory cannot be allocated.
void *
kalloc(void)
{
struct run *r;
push_off(); // 关闭中断
int cpu_id = cpuid(); // 获取当前 CPU 的 ID
acquire(&kmems[cpu_id].lock);
pop_off(); // 重新打开中断
r = kmems[cpu_id].freelist;
if(r)
kmems[cpu_id].freelist = r->next;
release(&kmems[cpu_id].lock);
if(!r) // 如果没有空闲的内存块
r = steal();
if(r)
memset((char*)r, 5, PGSIZE); // fill with junk
return (void*)r;
}
Buffer cache (hard)
这部分需要让缓冲区减少竞争,使用的方法是桶锁。原先是每次分配缓冲区都需要竞争一个锁,现在通过hash,使得只有相同哈希值的硬盘区会竞争一个锁。在初始化时,尽量将缓冲区均匀分布到不同的桶中。
- 如果使用了缓冲区,不管是已经有缓存的还是新分配的都会把这个缓冲区放在桶的头部,这样可以保证未使用的缓冲区留在尾部。
- 如果需要缓冲区,优先在哈希值对应的桶中查看是否存在缓存。如果没有则在桶的底部找refcnt==0的缓冲区,从自己在的桶开始。这样最多只会遍历桶的数量这么多次。
- release中只减少refcnt
// Buffer cache.
//
// The buffer cache is a linked list of buf structures holding
// cached copies of disk block contents. Caching disk blocks
// in memory reduces the number of disk reads and also provides
// a synchronization point for disk blocks used by multiple processes.
//
// Interface:
// * To get a buffer for a particular disk block, call bread.
// * After changing buffer data, call bwrite to write it to disk.
// * When done with the buffer, call brelse.
// * Do not use the buffer after calling brelse.
// * Only one process at a time can use a buffer,
// so do not keep them longer than necessary.
#include "types.h"
#include "param.h"
#include "spinlock.h"
#include "sleeplock.h"
#include "riscv.h"
#include "defs.h"
#include "fs.h"
#include "buf.h"
#define NHASH 13
#define HASH(blockno) (blockno % NHASH)
struct {
struct spinlock lock[NHASH];
struct buf buf[NBUF];
struct buf hash[NHASH];
struct spinlock freelist_lock;
struct spinlock get_lock;
} bcache;
void
insert_into_hash(struct buf *b, int idx)
{
// 插入到链表头部(空表头节点之后)
b->next = bcache.hash[idx].next;
b->prev = &bcache.hash[idx];
bcache.hash[idx].next->prev = b;
bcache.hash[idx].next = b;
}
void
remove_from_hash(struct buf *b, int idx)
{
// 断开b与链表的连接
b->prev->next = b->next;
b->next->prev = b->prev;
}
void
binit(void)
{
struct buf *b;
for(int i=0;i<NHASH;++i){
char lock_name[9];
snprintf(lock_name, 9, "bcache%d", i);
initlock(&bcache.lock[i], lock_name);
bcache.hash[i].next = &bcache.hash[i]; // 初始化空表头节点
bcache.hash[i].prev = &bcache.hash[i];
}
initlock(&bcache.freelist_lock, "bcache_freelist");
initlock(&bcache.get_lock, "bcache_get");
int idx=0;
for(b = bcache.buf; b < bcache.buf+NBUF; b++){
initsleeplock(&b->lock, "buffer");
b->refcnt = 0;
insert_into_hash(b, idx);
idx = (idx+1)%NHASH;
}
}
static struct buf*
bget(uint dev, uint blockno)
{
// acquire(&bcache.get_lock);
struct buf *b;
int idx = HASH(blockno);
acquire(&bcache.lock[idx]);
// Is the block already cached?
for (b = bcache.hash[idx].next; b != &bcache.hash[idx]; b = b->next) {
if(b->dev == dev && b->blockno == blockno){
b->refcnt++;
remove_from_hash(b, idx);
insert_into_hash(b, idx);
release(&bcache.lock[idx]);
acquiresleep(&b->lock);
// release(&bcache.get_lock);
return b;
}
}
release(&bcache.lock[idx]);
acquire(&bcache.freelist_lock);
//查找空闲的缓冲区,优先遍历当前桶
for (int i = 0; i < NHASH; i++) {
int bucket_idx = (idx + i) % NHASH; // 遍历从当前桶开始
acquire(&bcache.lock[bucket_idx]);
if(bcache.hash[bucket_idx].prev == &bcache.hash[bucket_idx]){
release(&bcache.lock[bucket_idx]);
continue;
}
b = bcache.hash[bucket_idx].prev;//只看链表尾部,即最近最少使用的缓冲区
if(b->refcnt == 0){
//不论是不是一个桶都把缓冲区取出来放到当前桶的头部
remove_from_hash(b, bucket_idx);
release(&bcache.lock[bucket_idx]);
acquire(&bcache.lock[idx]);
insert_into_hash(b, idx);
b->dev = dev;// 更新缓冲区信息
b->blockno = blockno;
b->valid = 0;
b->refcnt = 1;
release(&bcache.lock[idx]);
acquiresleep(&b->lock);
// release(&bcache.get_lock);
release(&bcache.freelist_lock);
return b;
}
release(&bcache.lock[bucket_idx]);
}
// 未找到可用缓冲区
panic("bget: no buffers");
}
// Return a locked buf with the contents of the indicated block.
struct buf*
bread(uint dev, uint blockno)
{
struct buf *b;
b = bget(dev, blockno);
if(!b->valid) {
virtio_disk_rw(b, 0);
b->valid = 1;
}
return b;
}
// Write b's contents to disk. Must be locked.
void
bwrite(struct buf *b)
{
if(!holdingsleep(&b->lock))
panic("bwrite");
virtio_disk_rw(b, 1);
}
// Release a locked buffer.
// Move to the head of the most-recently-used list.
void
brelse(struct buf *b)
{
if(!holdingsleep(&b->lock))
panic("brelse");
releasesleep(&b->lock);
int idx = HASH(b->blockno);
acquire(&bcache.lock[idx]);
b->refcnt--;
release(&bcache.lock[idx]);
}
void
bpin(struct buf *b) {
int idx = HASH(b->blockno);
acquire(&bcache.lock[idx]);
b->refcnt++;
release(&bcache.lock[idx]);
}
void
bunpin(struct buf *b) {
int idx = HASH(b->blockno);
acquire(&bcache.lock[idx]);
b->refcnt--;
release(&bcache.lock[idx]);
}
301




被折叠的 条评论
为什么被折叠?



