spinlock.c 5.59 KB
Newer Older
Wilke Pierre's avatar
Wilke Pierre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Mutual exclusion spin locks.

#include "types.h"
#include "param.h"
#include "memlayout.h"
#include "spinlock.h"
#include "riscv.h"
#include "proc.h"
#include "defs.h"

#define NLOCK 1000

static int nlock;
static struct spinlock *locks[NLOCK];

// assumes locks are not freed
void
initlock(struct spinlock *lk, char *name)
{
  lk->name = name;
  lk->locked = 0;
  lk->cpu = 0;
  lk->nts = 0;
  lk->n = 0;
  if(nlock >= NLOCK)
    panic("initlock");
  locks[nlock] = lk;
  nlock++;
}

Wilke Pierre's avatar
TP0  
Wilke Pierre committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
void dump_locks(void){
  printf_no_lock("LID\tLOCKED\tCPU\tPID\tNAME\t\tPC\n");
  for(int i = 0; i < nlock; i++){
    if(locks[i]->locked)
      printf_no_lock("%d\t%d\t%d\t%d\t%s\t\t%p\n",
                     i,
                     locks[i]->locked,
                     locks[i]->cpu - cpus,
                     locks[i]->pid,
                     locks[i]->name,
                     locks[i]->pc
        );
  }
}

Wilke Pierre's avatar
Wilke Pierre committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#define MAXTRIES 100000

// Acquire the lock.
// Loops (spins) until the lock is acquired.
void
acquire(struct spinlock *lk)
{
  uint64 ra;
  asm volatile("mv %0, ra" : "=r" (ra));
  ra -= 4;
  push_off(); // disable interrupts to avoid deadlock.
  if(holding(lk)){
    printf_no_lock("requesting %s (%p) but already have it\n", lk->name, lk);
    printf_no_lock("process %d (CPU %d) took it at pc=%p \n", lk->pid,
                   lk->cpu - cpus,
                   lk->pc);
    printf_no_lock("I am myself at pc=%p in pid=%d on CPU %d\n",
                   ra,
                   myproc() ? myproc()->pid : -1,
                   cpuid());
    procdump();
    panic("acquire");
  }

  __sync_fetch_and_add(&(lk->n), 1);

  // On RISC-V, sync_lock_test_and_set turns into an atomic swap:
  //   a5 = 1
  //   s1 = &lk->locked
  //   amoswap.w.aq a5, a5, (s1)
  int nbtries = 0;
  int warned = 0;
  while(__sync_lock_test_and_set(&lk->locked, 1) != 0) {
    nbtries++;
    if(nbtries > MAXTRIES && !warned){
      printf_no_lock("CPU %d: Blocked while acquiring %s (%p)\n", cpuid(), lk->name, lk);
      printf_no_lock("process %d (CPU %d) took it at pc=%p \n", lk->pid,
                     lk->cpu - cpus,
                     lk->pc);
      printf_no_lock("I am myself at pc=%p in pid=%d on CPU %d\n",
                     ra,
                     myproc() ? myproc()->pid : -1,
                     cpuid());
      procdump();
      warned = 1;
    }
     __sync_fetch_and_add(&lk->nts, 1);
  }
Wilke Pierre's avatar
TP0  
Wilke Pierre committed
94
  if(warned){
Wilke Pierre's avatar
Wilke Pierre committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    printf_no_lock("CPU %d: Finally acquired %s (%p) after %d tries\n", cpuid(), lk->name, lk, nbtries);
  }

  // Tell the C compiler and the processor to not move loads or stores
  // past this point, to ensure that the critical section's memory
  // references happen strictly after the lock is acquired.
  // On RISC-V, this emits a fence instruction.
  __sync_synchronize();

  // Record info about lock acquisition for holding() and debugging.
  lk->cpu = mycpu();
  lk->pc = ra;
  lk->pid = myproc() ? myproc()->pid : -1;
}

// Release the lock.
void
release(struct spinlock *lk)
{
  if(!holding(lk))
    panic("release");

  lk->cpu = 0;

  // Tell the C compiler and the CPU to not move loads or stores
  // past this point, to ensure that all the stores in the critical
  // section are visible to other CPUs before the lock is released,
  // and that loads in the critical section occur strictly before
  // the lock is released.
  // On RISC-V, this emits a fence instruction.
  __sync_synchronize();

  // Release the lock, equivalent to lk->locked = 0.
  // This code doesn't use a C assignment, since the C standard
  // implies that an assignment might be implemented with
  // multiple store instructions.
  // On RISC-V, sync_lock_release turns into an atomic swap:
  //   s1 = &lk->locked
  //   amoswap.w zero, zero, (s1)
  __sync_lock_release(&lk->locked);

  pop_off();
}

// Check whether this cpu is holding the lock.
// Must be called with interrupts off.
int
holding(struct spinlock *lk)
{
  int r;
  r = (lk->locked && lk->cpu == mycpu());
  return r;
}

// push_off/pop_off are like intr_off()/intr_on() except that they are matched:
// it takes two pop_off()s to undo two push_off()s.  Also, if interrupts
// are initially off, then push_off, pop_off leaves them off.

void
push_off(void)
{
  int old = intr_get();
  if(old)
    intr_off();
  if(mycpu()->noff == 0)
    mycpu()->intena = old;
  mycpu()->noff += 1;
}

void
pop_off(void)
{
  if(intr_get())
    panic("pop_off - interruptible");
  struct cpu *c = mycpu();
  if(c->noff < 1)
    panic("pop_off");
  c->noff -= 1;
  if(c->noff == 0 && c->intena)
    intr_on();
}

void
print_lock(struct spinlock *lk)
{
  if(lk->n > 0) 
    printf("lock: %s: #test-and-set %d #acquire() %d\n", lk->name, lk->nts, lk->n);
}

uint64
sys_ntas(void)
{
  int zero = 0;
  int tot = 0;
  
  if (argint(0, &zero) < 0) {
    return -1;
  }
  if(zero == 0) {
    for(int i = 0; i < NLOCK; i++) {
      if(locks[i] == 0)
        break;
      locks[i]->nts = 0;
      locks[i]->n = 0;
    }
    return 0;
  }

  printf("=== lock kmem/bcache stats\n");
  for(int i = 0; i < NLOCK; i++) {
    if(locks[i] == 0)
      break;
    if(strncmp(locks[i]->name, "bcache", strlen("bcache")) == 0 ||
       strncmp(locks[i]->name, "kmem", strlen("kmem")) == 0) {
      tot += locks[i]->nts;
      print_lock(locks[i]);
    }
  }

  printf("=== top 5 contended locks:\n");
  int last = 100000000;
  // stupid way to compute top 5 contended locks
  for(int t= 0; t < 5; t++) {
    int top = 0;
    for(int i = 0; i < NLOCK; i++) {
      if(locks[i] == 0)
        break;
      if(locks[i]->nts > locks[top]->nts && locks[i]->nts < last) {
        top = i;
      }
    }
    print_lock(locks[top]);
    last = locks[top]->nts;
  }
  return tot;
}