两个作业都是修改数据结构将粗粒度锁替换为细粒度锁降低锁争用增加并行度
Memory allocator
将唯一的空闲链表改为每个CPU一个,只有在当前CPU去其他CPU下的空闲链表取块时会产生锁争用
-
修改
kmem
结构体:struct { struct spinlock lock[NCPU]; struct run *freelist[NCPU]; } kmem;
-
修改函数
kinit
:void kinit() { for(int i = 0; i<NCPU; ++i) initlock(&kmem.lock[i], "kmem"); freerange(end, (void*)PHYSTOP); }
-
修改函数
kfree
:void kfree(void *pa) { struct run *r; if(((uint64)pa % PGSIZE) != 0 || (char*)pa < end || (uint64)pa >= PHYSTOP) panic("kfree"); // Fill with junk to catch dangling refs. memset(pa, 1, PGSIZE); r = (struct run*)pa; push_off(); acquire(&kmem.lock[cpuid()]); r->next = kmem.freelist[cpuid()]; kmem.freelist[cpuid()] = r; release(&kmem.lock[cpuid()]); pop_off(); }
-
修改函数
kalloc
:void * kalloc(void) { struct run *r; push_off(); for(int i = cpuid(), j = 0; j<NCPU; j++, i = (i+1)%NCPU) { acquire(&kmem.lock[i]); r = kmem.freelist[i]; if(r) { kmem.freelist[i] =