Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock()...

40
Jiannan Ouyang, Jack Lange Department of Computer Science University of Pittsburgh Preemptable Ticket Spinlocks Improving Consolidated Performance in the Cloud

Transcript of Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock()...

Page 1: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Jiannan Ouyang, Jack Lange

Department of Computer Science

University of Pittsburgh

Preemptable Ticket Spinlocks Improving Consolidated Performance in the Cloud

Page 2: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Motivation

2

VM interference in overcommitted environments

OS synchronization overhead

Lock holder preemption (LHP)

Page 3: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Lock Waiter Preemption

significance analysis of lock waiter preemption

Preemptable Ticket Spinlock

implementation inside Linux

Evaluation

significant speedup over Linux

Contributions

3

Page 4: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Spinlocks

4

Basics

lock() & unlock()

Busy waiting lock

generic spinlock: random order, unfair (starvation)

ticket spinlock: FIFO order, fair

Designed for fast mutual exclusion

busy waiting vs. sleep/wakeup spinlocks for short & fast critical sections (~1us)

OS assumptions use spinlocks for short critical section only

never preempt a thread holding or waiting a kernel spinlock

Page 5: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in VMs

5

Lock Holder Preemption (LHP)

virtualization breaks the OS assumption

vCPU holding a lock is unscheduled by VMM

preemption prolongs critical section (~1m v.s. ~1us)

Proposed Solutions

Co-scheduling and variants

Hardware-assisted scheme (Pause Loop Exiting)

Paravirtual spinlocks

Page 6: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

6

0 1 2

head = 0 tail = 2

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 7: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

7

0 1 2

head = 0 tail = 2

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 8: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

8

0 1 2 3

head = 0 tail = 3

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 9: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

9

0 1 2 3 4

head = 0 tail = 4

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 10: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

10

1 2 3 4

tail = 4 head = 1

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 11: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

11

1 2 3 4

tail = 4 head = 1

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Lock Holder Preemption!

Page 12: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

12

1 2 3 4

tail = 4 head = 1

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 13: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

13

1 2 3 4

tail = 4 head = 1

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 14: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

14

2 3 4

tail = 4 head = 2

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 15: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

15

3 4

head = 3

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

tail = 4

Page 16: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

16

3 4 5

head = 3 tail = 5

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Page 17: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemption in Ticket Lock

17

3 4 5 6

head = 3 tail = 6

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

Lock Waiter Preemption

wait on available resource

Page 18: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Lock Waiter Preemption

18

Lock waiter is preempted

Later waiters wait on an available lock

Possible to adapt to it, if we

detect preempted waiter

acquire lock out of order

How significant is it??

Page 19: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Waiter Preemption Dominates

19

LHP + LWP LWP 𝐋𝐖𝐏

𝐋𝐇𝐏 + 𝐋𝐖𝐏

hackbench x1 1089 452 41.5%

hackbench x2 44342 39221 88.5%

ebizzy x1 294 166 56.5%

ebizzy x2 1017 980 96.4%

Table 2: Lock Waiter Preemption Problem in the Linux Kernel

Lock waiter preemption dominates in overcommitted environments

Page 20: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Challenges & Approach

20

How to identify a preempted waiter?

timeout threshold

How to violate order constraints?

allow timed out waiters get the lock randomly

ensure mutual exclusion between them

How NOT to break the whole ordering mechanism?

timeout threshold proportional to queue position

Page 21: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Queue Position Index

21

N = ticket – queue_head

ticket: copy of queue tail value upon enqueue

N: number of earlier waiters

n n+1 n+2

head = n tail = n+2

ticket = n+2

N = 2

Page 22: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Proportional Timeout Threshold

22

T = N x t

t is a constant parameter

large enough to avoid false detection

small enough to save waiting time

Performance is NOT t value sensitive

most locks take ~1us & most spinning time wasted on locks

that wait ~1ms

larger t does not harm & smaller t does not gain much

n n+1 n+2

head = n tail = n+2

0 t 2t Timeout

Threshold

Page 23: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemptable Ticket Spinlock

23

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

0 1 2 3 4 5

head = 0 tail = 5

0 Timeout

Threshold t 2t 3t 4t 5t

Page 24: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemptable Ticket Spinlock

24

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

1 2 3 4 5

head = 1 tail = 5

0 Timeout

Threshold t 2t 3t 4t

Page 25: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemptable Ticket Spinlock

25

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

1 2 3 4 5

head = 1 tail = 5

0 Timeout

Threshold t 2t 3t 4t

Page 26: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemptable Ticket Spinlock

26

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

1 3 4 5

head = 2 tail = 5

Timeout

Threshold t 2t 3t

N = ticket – head

Page 27: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemptable Ticket Spinlock

27

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

1 3 4 5

head = 2 tail = 5

Timeout

Threshold t 2t 3t

Page 28: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemptable Ticket Spinlock

28

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

1 3 5

head = 3 tail = 5

Timeout

Threshold 0 2t

Page 29: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemptable Ticket Spinlock

29

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

1 3 5

head = 3 tail = 5

Timeout

Threshold 0 2t

Page 30: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Preemptable Ticket Spinlock

30

0

1

a scheduled waiter with ticket 0

a preempted waiter with ticket 1

1 3 5

head = 3 tail = 5

Timeout

Threshold 0 2t

Page 31: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Summary

31

Preemptable Ticket Lock adapts to preemption

preserve order in absence of preemption

violate order upon preemption

Preemptable Ticket Lock preserves fairness

order violations are restricted

priority is always given to timed out waiters

timed out waiters bounded by vCPU numbers of a VM

Page 32: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Implementation

32

Drop-in replacement

lock(), unlock(), is_locked(), trylock(), etc.

Correct

race condition free: atomic updates

Fast

performance is sensitive to lock efficiency

~60 lines of C/inline-assembly in Linux 3.5.0

Page 33: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Paravirtual Spinlocks

33

Lock holder preemption is unaddressed

semantic gap between guest and host

paravirtualization: guest/host cooperation

signal long waiting lock / put a vCPU to sleep

notify to wake up a vCPU / wake up a vCPU

paravirtual preemptable ticket spinlock

sleep when waiting too long after timed out

wake up all sleeping waiters upon lock releasing

Page 34: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Evaluation

34

Host

8 core 2.6GHz Intel Core i7 CPU, 8 GB RAM, 1Gbit NIC, Fedora 17 (Linux 3.5.0)

Guest

8 core, 1G RAM, Fedora 17 (Linux 3.5.0)

Benchmarks

hackbench, ebizzy, dell dvd store

Lock implementations

baseline: ticket lock, paravirtual ticket lock (pv-lock)

preemptable ticket lock

paravirtual (pv) preemptable ticket lock

Page 35: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Hackbench

35

Average Speedup

preemptable-lock vs. ticket lock: 4.82X

pv-preemptable-lock v.s. ticket lock: 7.08X

pv-preemptable-lock v.s. pv-lock: 1.03X

Page 36: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Ebizzy

36

Less variance over ticket lock and pv-lock

in-VM preemption adaptivity

less VM interference

variance

80.36 vs. 10.94

variance

131.62 vs. 16.09

Page 37: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Dell DVD Store (apache/mysql)

37

Average Speedup

preemptable-lock vs. ticket lock: 11.68X

pv-preemptable-lock v.s. ticket lock: 19.52X

pv-preemptable-lock v.s. pv-lock: 1.11X

Page 38: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Evaluation Summary

38

Preemptable Ticket Spinlocks speedup

5.32X over ticket lock

Paravirtual Preemptable Ticket Spinlocks speedup

7.91X over ticket lock

1.08X over paravirtual ticket lock

Average speedup across cases for all benchmarks

Page 39: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Lock Waiter Preemption

most significant preemption problem in queue based lock under

overcommitted environment

Preemptable Ticket Spinlock

Implementation with ~60 lines of code in Linux

Better performance in overcommitted environment

5.32X average speedup up over ticket lock w/o VMM support

1.08X average speedup over pv-lock with less variance

Conclusion

39

Page 40: Preemptable Ticket Spinlocksjarusl/presentations/vee-2013.pdfSpinlocks 4 Basics lock() & unlock() Busy waiting lock generic spinlock: random order, unfair (starvation) ticket spinlock:

Thank You

40

Jiannan Ouyang

[email protected]

http://www.cs.pitt.edu/~ouyang/

1 2 3 4 5

0 t 2t 3t 4t

Preemptable Ticket Spinlock