hazard pointers: safe memory reclamation for lock-free objects

19
Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects Maged M. Michael Presented by Abdulai Sei

Upload: abby

Post on 27-Jan-2016

40 views

Category:

Documents


2 download

DESCRIPTION

Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects. Maged M. Michael Presented by Abdulai Sei. Recap. So far we have seen many lock-free dynamic objects algorithms How many reclaim memory of reused nodes? How many of these return memory to the operating system? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Maged M. MichaelPresented by Abdulai Sei

Page 2: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Recap So far we have seen many lock-free dynamic objects

algorithms How many reclaim memory of reused nodes? How many of these return memory to the operating

system? What happens if a thread frees a node and another thread

has a pointer to that node?

Page 3: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Related paper -Type Stable Memory (TSM)

Similar concept to HP, non-blocking Three basic extension to conventional type

implementation type descriptor remains a valid instance of the type even

when inactive (in the free list) allows multiple memory allocation for the same type type change over time (descriptor has to be inactive before

reallocation) Guarantee a pointer to point to a descriptor of the type

readers can continue reading but does not guarantee

correct data

Page 4: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Hazard Pointers (HP) A memory management methodology that allows

memory reclamation for arbitrary reuse Memory is finite and needs to be reused Thread failures and dead lock leave chunks of memory

unclaimed for reuse by the operating system

Page 5: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

HP Claims Suitable for user-level application as well as system

level program without dependency on kernel or scheduler

Very efficient algorithm for dynamic lock-free object Requires only single-word reads and writes Solves the ABA problem Wait-free

Page 6: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

ABA Problem Common problem with CAS, CAS2, DCAS Affects almost all lock-free algorithms

Thread A reads a value (XX) from a shared memory, then another thread B reads the same value XX, change it to XY and then back to XX. When thread A reads the value for the second time, it sees XX and assume it was not changed

Must be prevented regardless of memory reclamation Applying HP to ABA-safe algorithm makes it safe

Page 7: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Methodology Based on the observation that

A thread holds a small number of references References do not need further validation for

accessing the content of dynamic nodes References are not ABA-prone

Core ideas Single-writer multi-reader shared pointers called HP HP varies among threads – typically one to two

Communicates with the algorithm through HP Consists of two parts

One for processing retired nodes Memory reclamation and ABA prevention

Page 8: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Algorithm – (reader only)

add1*

READER

add2

add2HP List

(add2) – dereference add1

Page 9: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Algorithm – (reader + writer)

add1*

READER

add2add3

add2HP List

WRITER

(add2) – dereference add1

allocate add3

Deference add1

copy add2 to add3

modify add3

Page 10: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Algorithm (reader + writer)

add1*

READER

add2add3

add2HP List

WRITER

(add2) – reader has already deference add1* before writer start

allocate add3

copy add2 to add3

modify add3

CAS

retired add2

Page 11: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Questions for class Is the reader reading the right data after the writer

perform CAS? See slide 13, the conditions Can the garbage collector free add2? Any new problems?

yes, readers have to write to the HP list others?

Page 12: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Code

Page 13: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

The Condition A thread announces to other threads when it assigns a

reference to one of it HP Other threads refrain from reclaiming or reusing node

At time t, each node n is in one of the states Allocated, reachable, removed, retired, free, unavailable,

undefined A thread holds a reference to a node when it is safe

Question: Can a thread create a new hazardous reference to a node

when it is retired?

Page 14: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Applying HP Apply to existing lock-free algorithms based on

conditions Examine the target algorithm

Identify HP and hazard reference they use Determine where hazard reference is created and last hazard pointer

that uses it Determine the max # that can be hazardous

Insert the following in target algorithm Write the address of nodes that are target for reference to an

unavailable hazard reference Validate the node is safe

Example of existing algorithms to apply the use of HP FIFO Queues LIFO Stacks List-Based Sets and Hash Tables Single-Writer Multireader Dynamic Structures

Page 15: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Applications – FIFO queues

Page 16: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Applications – Lock-free stack using HP

Page 17: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Experiment Comparing performance of new methodology to lock-

free memory management system FIFI queues, LIFO stacks, chaining hash table – all

implemented using HP Comparison with commonly use locks

Using test-and-test and set with exponential backoff Hash table with 100 separate locks

IBM RS /6000 multiprocessor with 4 processors Align cache data structure Locks, single-word, double-word CAS implemented using

LL/SC All implementation compiled at highest optimization Ran each experiment 5 times and reported the average

Page 18: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Performance Comparison

Page 19: Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects

Conclusion HP provided unrestricted memory reclamation for

dynamic lock-free objects Takes constant amortized time per retired node Uses only single-word instructions Comparable performance to that of lock-based

implementation Both under no contention and no multiprogramming

Outperform them significantly under moderate multiprogramming and/or contention

Guarantee progress even if a thread fails or delays (prevent deadlock)