RCU Theory and Practice Marwan Burelle - LSE Summer Week 2015 - - PowerPoint PPT Presentation

rcu
SMART_READER_LITE
LIVE PREVIEW

RCU Theory and Practice Marwan Burelle - LSE Summer Week 2015 - - PowerPoint PPT Presentation

RCU Theory and Practice Marwan Burelle - LSE Summer Week 2015 Overview RCU concepts Short overview of how RCU may solves your problems Wait for readers Userland implementations for real Pseudo RCU Implementing RCU concepts with


slide-1
SLIDE 1

RCU

Theory and Practice

Marwan Burelle - LSE Summer Week 2015

slide-2
SLIDE 2

Overview

➢ RCU concepts

Short overview of how RCU may solves your problems

➢ Wait for readers

Userland implementations for real

➢ Pseudo RCU

Implementing RCU concepts with non-RCU tools

slide-3
SLIDE 3

… it’s all about procrastination …

slide-4
SLIDE 4

Postponing operations can solve your synchronization problem …

slide-5
SLIDE 5

… time is very relative …

slide-6
SLIDE 6

Changes append when you see them !

slide-7
SLIDE 7

Read - Copy - Update

slide-8
SLIDE 8

What for ?

➢ Concurrent shared data ➢ Kind of non-blocking ➢ Read intensive context ➢ Few updates ➢ Minimizing readers overhead

slide-9
SLIDE 9

RCU is not only about when to free old pointers ...

slide-10
SLIDE 10

The keystone of read-copy update is the ability to determine when all threads have passed through a quiescent state since a particular point in time.

READ-COPY UPDATE: USING EXECUTION HISTORY TO SOLVE CONCURRENCY PROBLEMS

PAUL E. MCKENNEY & JOHN D. SLINGWINE

slide-11
SLIDE 11

Quiescent State: when a thread no longer care about shared and protected data structures. Quiescent Period: a time interval during which each thread passes through at least one quiescent state.

slide-12
SLIDE 12

Quiescent Period

Thread 1 Thread 2 Thread 3 Thread 0

Quiescent State Quiescent Period Previous State Life-Time

slide-13
SLIDE 13

RCU

RCU framework: ➢ Wait for readers (WFR) ➢ Respect of quiescent period ➢ Defines API/Constraints ➢ Underlying mechanism RCU-based algo: ➢ wait-free readers ➢ Use WFR for sync ➢ Respect API/Constraints ➢ Copy-based updates

slide-14
SLIDE 14

Wait For Readers

Key points: writer's operation terminates when all readers have leaved the update region ➢ Writer offers a grace period ➢ Readers won’t continue longer than this period ➢ Quiescent Period will be our grace period

slide-15
SLIDE 15

Simple Buffer Example

Writer: ➢ Copy data in new buffer ➢ Update new buffer ➢ Replace buffer pointer ➢ Wait for readers ➢ Free old buffer Readers: ➢ Arrived before update:

  • See old buffer

➢ Arrived after update

  • See new buffer
slide-16
SLIDE 16

Reader/Writer Lock Solution

Writer asks for lock Writer obtains lock Writer releases lock Data

  • ld

version Data new version Update Readers allowed Readers are blocked Readers allowed

slide-17
SLIDE 17
slide-18
SLIDE 18

Simple Buffer Example

Quiescent State: When reader leave buffer’s critical section Quiescent Period: All readers have leaved critical section

slide-19
SLIDE 19

Simple Buffer Example

Readers on second buffer time-line First Buffer lifetime Readers on first buffer Second buffer Active First buffer Deleted Second Buffer lifetime Writer Active

Copy Update

slide-20
SLIDE 20

Writer:

// Sync with other writers char *old = rcu_dereference(buf) char *new = malloc( enough ) memcpy(new, old) // copy update_content(new) // update rcu_assign_pointer(buf, new) synchronize_rcu() free(old)

Simple Buffer Pseudo Code

Reader:

rcu_read_lock() char *b = rcu_dereference(buf) read_content(b) // read rcu_read_unlock()

slide-21
SLIDE 21

RCU Linked List

slide-22
SLIDE 22

cur = list-entry-point; while (cur != NULL) { // Your job here cur = cur->next; }

Readers Traversing Loop

Constraints: ➢ Minimize traversing cost ➢ Wait-free (no lock, no spin) ➢ Independent iterations ➢ cur must be valid in body ➢ cur can be detached ➢ cur->next must be valid

slide-23
SLIDE 23

Classical Solutions

➢ Coarse Grain lock: enough said … ➢ Fine Grain lock: not wait free (chain locking) ➢ Optimistic lock: ➢ Lazy lock ➢ Lock-Free wait free but with overhead

slide-24
SLIDE 24

RCU List Delete Element

Wait for next quiescent period.

slide-25
SLIDE 25

RCU Update List Element

Reader writer

Wait for next quiescent period.

Reader

slide-26
SLIDE 26

Writer strategy

Update: ➢ Copy the element and update the copy ➢ Replace access pointer when ready Delete: ➢ Replace pointer Both: ➢ Wait for readers before deleting old element

slide-27
SLIDE 27

Waiting For Readers ?

slide-28
SLIDE 28

I’m waiting, I got a lot of time ...

slide-29
SLIDE 29

Non-preemptive Kernel

➢ Threads leave CPU only when complete ➢ Available CPU means one or more threads gone through quiescent state ➢ All CPU available means end of quiescent period

slide-30
SLIDE 30

And in Userland ?

➢ Previous strategy is irrealistic ➢ We need more stuff ➢ We still want:

  • wait-free readers
  • minimal overhead in readers
  • keep the same structure
slide-31
SLIDE 31

Common operations

➢ rcu_dereference: load/consume ➢ rcu_assign_pointer: store/release ➢ Compiler barrier

#define barrier() asm volatile("" : : : "memory")

➢ Memory barrier (full sequential consistency)

slide-32
SLIDE 32

Common data

Per thread meta-info: ➢ TLS or like ➢ Threads need to register ➢ Readable from writer

slide-33
SLIDE 33

Strategies ?

➢ Use GC/HP like mechanism ➢ Using RCU reader lock/unlock and barrier ➢ Update brain ?

slide-34
SLIDE 34

Garbage Collecting

➢ Pretty easy when available ➢ Price ? ➢ Not sufficient for certain cases ➢ More suited ? Eventually Hazard Pointers ➢ Using Smart counters ? See later ...

slide-35
SLIDE 35

Using RCU reader lock/unlock

➢ Already explicit in the code ➢ May break requirement of minimal overhead ➢ Still interesting

slide-36
SLIDE 36

➢ Wait-free (bounded spin, non blocking ops) ➢ Nested read sections ➢ RCU properties must hold ...

Issues

slide-37
SLIDE 37

Principle

➢ Per reader counter set using memory barrier

  • High order bit: phase (global)
  • lower-order bits: nesting

➢ Writer does 2 grace period spin-waits

  • spin on each reader with same phase and nesting ≠ 0
  • 2 grace periods to avoid race condition
slide-38
SLIDE 38

Discussion

➢ Only lock/unlock (read) and synchronize (write) ➢ Safe and easy to use in all cases ➢ Single writer (that’s RCU, don’t care ... ) ➢ Memory barriers are expensive !

slide-39
SLIDE 39

We can eliminate barrier cost using POSIX thread signal !

slide-40
SLIDE 40
slide-41
SLIDE 41

Avoiding unneeded barriers

Goal: avoid barriers when no update is running ➢ Add a need barrier tag to meta-info ➢ Writer set the tag on readers and send a signal ➢ Signal handler reset the tag ➢ Signal enforces a real memory barrier ➢ Real gain (check urcu papers)

slide-42
SLIDE 42

Can do better ?

slide-43
SLIDE 43

Detecting Quiescent States

➢ Nothing in lock/unlock ➢ Explicit indication of quiescent states ➢ More intrusive but more efficient !

slide-44
SLIDE 44

Marking Quiescent State

➢ Snapshot current period counter in reader

  • wait free operation

➢ Writer wait while readers have current value

  • blocking, but that’s RCU

➢ Possible overflow: solved with 64bits counter

  • can be solved using double period also

➢ Provides also extended quiescent state

  • thread online/offline API
slide-45
SLIDE 45

Was it worth the effort ?

slide-46
SLIDE 46

Some bench ...

slide-47
SLIDE 47

Pseudo RCU

slide-48
SLIDE 48

Most RCU-like algorithms can be implemented using other pointers reclamation mechanism.

slide-49
SLIDE 49

Using smart pointers

➢ Pretty easy to set-up ➢ Use C++11’s std::shared_ptr ➢ Smart pointers are synchronized ➢ Hope ? std::atomic_is_lock_free ?

slide-50
SLIDE 50

Using Hazard Pointers

➢ HP are wait free ➢ Just need a double check when getting pointer ➢ HP are another kind of relativistic programming ... once again, it’s all about procrastination

slide-51
SLIDE 51
slide-52
SLIDE 52

RCU-like shared buffer Readers checking content Occasional single writer update buffer

slide-53
SLIDE 53

Lock-free shared_ptr is like ...

slide-54
SLIDE 54
slide-55
SLIDE 55
slide-56
SLIDE 56

Readings

➢ RCU author’s page: http://www.rdrop.com/~paulmck/RCU/

Lot of links and useful articles

➢ User-Level Implementations of Read-Copy Update

Desnoyers, McKenney, Stern, Dagenais and Walpole IEEE Transaction on Parallel and Distributed Systems, 23 (2): 375-382 (2012)

➢ Structured Deferral: Synchronization via Procrastination

Paul E. McKenney, ACM Queue 2013

➢ Introduction to RCU Concepts

Liberal application of procrastination for accommodation of the laws of physics – for more than two decades!

Paul E. McKenney, LinuxCon 2013