Slide 1

Slide 1 text

Spinlocks Spinlocks Samy Al Bahra Devon H. O’Dell Message Systems, Inc. April 8, 2011

Slide 2

Slide 2 text

Spinlocks Introduction Mutexes A mutex is an object which implements acquire and relinquish operations such that the execution following an acquire operation and up to the relinquish operation is executed in a mutually exclusive manner relative to the object implementing a mutex.

Slide 3

Slide 3 text

Spinlocks Introduction Locks Locks are an implementation of a mutex. Sleep lock Any mutex type which deactivates processes that attempt to acquire a mutex that has already been acquired by another process until a relinquish operation on the mutex activates one or more of them. Spinlock Any mutex type which forces callers of an acquire operation to spend an unbounded number of processor cycles re-evaluating the availability of the mutex until it has been acquired. The process that invokes acquire is never deactivated before the completion of the acquire operation. Spinlocks are preferred to sleep mutexes when the waiting time for a resource is less than the time for the scheduling overhead of process activation/deactivation or when scheduling simply is not possible.

Slide 4

Slide 4 text

Spinlocks Non-Arbitrating Spinlocks Naive void lock(uint32_t *mutex) { while (ck_pr_fas_32(mutex, true) != false) ck_pr_stall(); return; } void unlock(uint32_t *mutex) { *mutex = false; return; }

Slide 5

Slide 5 text

Spinlocks Non-Arbitrating Spinlocks Naive 3e+07 4e+07 5e+07 6e+07 7e+07 8e+07 9e+07 1e+08 1.1e+08 1.2e+08 1.3e+08 2 3 4 5 6 7 8 9 10 11 12 Total Acquisitions (in 10 seconds) Number of Cores Naive

Slide 6

Slide 6 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 7

Slide 7 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 8

Slide 8 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 9

Slide 9 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 10

Slide 10 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 11

Slide 11 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 12

Slide 12 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 13

Slide 13 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 14

Slide 14 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 15

Slide 15 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 16

Slide 16 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 17

Slide 17 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 18

Slide 18 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 19

Slide 19 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 20

Slide 20 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 21

Slide 21 text

Spinlocks Non-Arbitrating Spinlocks Cache Coherency

Slide 22

Slide 22 text

Spinlocks Non-Arbitrating Spinlocks TATAS void lock(uint32_t *mutex) { while (ck_pr_fas_32(mutex, true) != false) { while (ck_pr_load_32(mutex) == true) ck_pr_stall(); } return; } void unlock(uint32_t *mutex) { *mutex = false; return; }

Slide 23

Slide 23 text

Spinlocks Non-Arbitrating Spinlocks TATAS 2e+07 4e+07 6e+07 8e+07 1e+08 1.2e+08 1.4e+08 1.6e+08 2 3 4 5 6 7 8 9 10 11 12 Total Acquisitions (in 10 seconds) Number of Cores Naive TATAS

Slide 24

Slide 24 text

Spinlocks Non-Arbitrating Spinlocks Exponential Backoff void lock(uint32_t *mutex) { ck_backoff_t backoff = CK_BACKOFF_INITIALIZER; while (ck_pr_fas_32(mutex, true) != false) ck_backoff_eb(&backoff); return; } void unlock(uint32_t *mutex) { *mutex = false; return; }

Slide 25

Slide 25 text

Spinlocks Non-Arbitrating Spinlocks Exponential Backoff 0 2e+08 4e+08 6e+08 8e+08 1e+09 1.2e+09 1.4e+09 2 3 4 5 6 7 8 9 10 11 12 Total Acquisitions (in 10 seconds) Number of Cores Naive TATAS EB

Slide 26

Slide 26 text

Spinlocks Non-Arbitrating Spinlocks Fairness 0 5e+06 1e+07 1.5e+07 2e+07 2.5e+07 3e+07 3.5e+07 4e+07 4.5e+07 Total Acquisitions (1o seconds) Processor

Slide 27

Slide 27 text

Spinlocks Non-Arbitrating Spinlocks Fairness Non-arbitrating spinlocks do not provide fairness (or starvation-freedom) guarantees.

Slide 28

Slide 28 text

Spinlocks Non-Arbitrating Spinlocks Ticket 0 1 0 2 2 1 3 2 3 Now Serving: 0 Now Serving: 0 Now Serving: 1 Now Serving: 2 Now Serving: 3 LOCK LOCK, LOCK UNLOCK UNLOCK UNLOCK UNLOCK, LOCK 4 Now Serving: 4 UNLOCK

Slide 29

Slide 29 text

Spinlocks Non-Arbitrating Spinlocks Anderson U L L L U L L L U L L L L U L L L U L L P P P P P L L U L P L L U L P LOCK LOCK UNLOCK LOCK UNLOCK LOCK L L L U P U L L L P UNLOCK UNLOCK

Slide 30

Slide 30 text

Spinlocks Non-Arbitrating Spinlocks MCS L L T0 R L T0 R T1 S L T0 R T1 S FAS L T0 N T1 S R = Running S = Spinning N = Notify L T1 R T0 LOCK T1 LOCK T0 UNLOCK

Slide 31

Slide 31 text

Spinlocks Non-Arbitrating Spinlocks MCS L T1 R T2 S L T1 S T2 S L T1 N T2 S L T2 R T2 LOCK T1 UNLOCK

Slide 32

Slide 32 text

Spinlocks Non-Arbitrating Spinlocks CLH Stub Stub T0 Stub T0 T1 T1 LOCK LOCK UNLOCK Stub T0 T1 T0 T1 UNLOCK R R R S S

Slide 33

Slide 33 text

Spinlocks Non-Arbitrating Spinlocks Fast path latency Fast path latency See http://concurrencykit.org/doc/appendixZ.html

Slide 34

Slide 34 text

Spinlocks Non-Arbitrating Spinlocks Limitations Mutexes in general are not composable. Subtle ordering issues can lead to hard-to-detect deadlock conditions. Blocking synchronization is sensitive to preemption.

Slide 35

Slide 35 text

Spinlocks Discussion Questions?