REGION-BASED DYNAMIC SEPARATION FOR STM HASKELL Laura Effinger-Dean - - PowerPoint PPT Presentation

region based dynamic separation for stm haskell
SMART_READER_LITE
LIVE PREVIEW

REGION-BASED DYNAMIC SEPARATION FOR STM HASKELL Laura Effinger-Dean - - PowerPoint PPT Presentation

REGION-BASED DYNAMIC SEPARATION FOR STM HASKELL Laura Effinger-Dean and Dan Grossman University of Washington TRANSACT, San Jose, CA, June 5, 2011 Region-Based Dynamic Separation Motivation We want STM to be correct and fast Do


slide-1
SLIDE 1

REGION-BASED DYNAMIC SEPARATION FOR STM HASKELL

Laura Effinger-Dean and Dan Grossman University of Washington TRANSACT, San Jose, CA, June 5, 2011

slide-2
SLIDE 2

Motivation

  • We want STM to be correct and fast
  • Do nontransactional accesses interact with STM?
  • Yes – reasonable behavior, but slow (strong atomicity)
  • No – fast but has strange behavior (weak atomicity)
  • This work focuses on one compromise: dynamic

separation

  • Programmer inserts calls to STM when sharing behavior of data

changes

Region-Based Dynamic Separation

slide-3
SLIDE 3

Background: Transactional races

  • Growing consensus: the following code is racy and

therefore the assertion might fail

Thread ¡1 ¡ x ¡= ¡2; ¡ Thread ¡2 ¡ atomic ¡{ ¡ ¡ ¡r1 ¡= ¡x; ¡ ¡ ¡r2 ¡= ¡x; ¡ ¡ ¡assert ¡(r1 ¡== ¡r2); ¡ } ¡

Region-Based Dynamic Separation

slide-4
SLIDE 4

Background: Privatization

  • Some non-racy idioms are unsafe in basic weak STMs
  • Canonical privatization example:

Thread ¡1 ¡ atomic ¡{ ¡ ¡ ¡r ¡= ¡ptr; ¡ ¡ ¡ptr ¡= ¡new ¡C(); ¡ } ¡ assert(r-­‑>f ¡== ¡r-­‑>g); ¡ Thread ¡2 ¡ atomic ¡{ ¡ ¡ ¡++ptr-­‑>f; ¡ ¡ ¡++ptr-­‑>g; ¡ } ¡ Initially ptr-­‑>f ¡== ¡ptr-­‑>g ¡ r ¡ ptr ¡ f ¡ g ¡ …

Region-Based Dynamic Separation

slide-5
SLIDE 5

Background: Privatization

  • Eager update: assert sees update from zombie
  • Lazy update: assert sees a partially committed transaction
  • Symmetric problem: publication

Thread ¡1 ¡ atomic ¡{ ¡ ¡ ¡r ¡= ¡ptr; ¡ ¡ ¡ptr ¡= ¡new ¡C(); ¡ } ¡ assert(r-­‑>f ¡== ¡r-­‑>g); ¡ Thread ¡2 ¡ atomic ¡{ ¡ ¡ ¡++ptr-­‑>f; ¡ ¡ ¡++ptr-­‑>g; ¡ } ¡ Initially ptr-­‑>f ¡== ¡ptr-­‑>g ¡

Region-Based Dynamic Separation

slide-6
SLIDE 6

Existing solutions

  • Special-case the privatization and publication idioms
  • Support “single global lock atomicity”
  • Require programs to obey a separation discipline
  • Separate objects into “always accessed in transactions” and “never

accessed in transactions” (and other useful categories)

  • Weak implementation is correct for these programs
  • Static and dynamic approaches
  • Our work is on making the dynamic approach more

convenient and expressive

Region-Based Dynamic Separation

slide-7
SLIDE 7

Background: Static separation

  • Type system separates objects into “always accessed in

transactions” and “never accessed in transactions”

  • State changes like privatization now illegal:

Thread ¡1 ¡ atomic ¡{ ¡ ¡ ¡r ¡= ¡ptr; ¡ ¡ ¡ptr ¡= ¡new ¡C(); ¡ } ¡ assert(r-­‑>f ¡== ¡r-­‑>g); ¡ Thread ¡2 ¡ atomic ¡{ ¡ ¡ ¡++ptr-­‑>f; ¡ ¡ ¡++ptr-­‑>g; ¡ } ¡ Initially ptr-­‑>f ¡== ¡ptr-­‑>g ¡ does not typecheck – r’s target is accessed both inside and outside transactions [HMPJH05, ABHI08, MG08]

Region-Based Dynamic Separation

slide-8
SLIDE 8

Background: Dynamic separation

  • Every object has a dynamic protection state
  • Programmer manually instruments state changes

Thread ¡1 ¡ atomic ¡{ ¡ ¡ ¡r ¡= ¡ptr; ¡ ¡ ¡ptr ¡= ¡new ¡C(); ¡ } ¡ unprotect(r); ¡ assert(r-­‑>f ¡== ¡r-­‑>g); ¡ Thread ¡2 ¡ atomic ¡{ ¡ ¡ ¡++ptr-­‑>f; ¡ ¡ ¡++ptr-­‑>g; ¡ } ¡ Initially ptr-­‑>f ¡== ¡ptr-­‑>g ¡ [AHM08, ABHHI09] Wait for any active transactions to complete

Region-Based Dynamic Separation

slide-9
SLIDE 9

Our contributions

  • Most important: Dynamic regions allow constant-time

state changes for shared data structures

  • Extended set of protection states for variables
  • Read-only, thread-local
  • Static and dynamic separation exist side-by-side
  • Libraries can be agnostic with respect to transactions
  • Formal semantics and proof of correctness
  • Haskell implementation & evaluation

Region-Based Dynamic Separation

slide-10
SLIDE 10

So why Haskell?

  • Static separation is a natural fit for Haskell
  • Add dynamic separation in order to handle cases where

static separation is insufficient

  • E.g., privatization
  • Existing formal semantics and benchmark suite

Region-Based Dynamic Separation

slide-11
SLIDE 11

STM Haskell

  • Static separation
  • Nontransactional variables are IORefs
  • Transactional variables are TVars
  • Impossible to use IORefs inside an atomic block
  • Natural application of Haskell’s monadic type system
  • Clean semantics for transactions:
  • Sequential and alternative composition
  • Exception handling
  • Manual retry

[HMPJH05]

Region-Based Dynamic Separation

slide-12
SLIDE 12

STM actions

  • Actions on TVars can be composed to form STM actions
  • STM actions are executed via “atomically”

Interface ¡ data ¡STM ¡a ¡ data ¡TVar ¡a ¡ newTVar ¡:: ¡a ¡-­‑> ¡STM ¡(TVar ¡a) ¡ readTVar ¡:: ¡TVar ¡a ¡-­‑> ¡STM ¡a ¡ writeTVar ¡:: ¡TVar ¡a ¡-­‑> ¡a ¡-­‑> ¡STM ¡a ¡ atomically ¡:: ¡STM ¡a ¡-­‑> ¡IO ¡a ¡ Example ¡ atomically ¡(do ¡{ ¡ ¡ ¡t ¡<-­‑ ¡newTVar ¡5; ¡ ¡ ¡x ¡<-­‑ ¡readTVar ¡t; ¡ ¡ ¡writeTVar ¡t ¡(x ¡+ ¡1) ¡ }) ¡

Region-Based Dynamic Separation

slide-13
SLIDE 13

Adding dynamic separation

  • We will introduce our extensions to STM Haskell’s

interface one at a time:

1.

New variable type for dynamic separation: DVars

2.

How to execute dynamic-separation code

3.

New protection states: read-only and thread-local

4.

Shared protection states via regions

Region-Based Dynamic Separation

slide-14
SLIDE 14
  • 1. Adding DVars
  • Now three variable types: IORef, TVar, DVar
  • DVars correspond to normal variables in languages

without static separation

Static ¡separation ¡interface ¡ data ¡STM ¡a ¡ data ¡TVar ¡a ¡ newTVar ¡ ¡ ¡:: ¡a ¡-­‑> ¡STM ¡(TVar ¡a) ¡ readTVar ¡ ¡:: ¡TVar ¡a ¡-­‑> ¡STM ¡a ¡ writeTVar ¡:: ¡TVar ¡a ¡-­‑> ¡a ¡-­‑> ¡STM ¡a ¡ Dynamic ¡separation ¡interface ¡ data ¡DSTM ¡a ¡ data ¡DVar ¡a ¡ newDVar ¡ ¡ ¡:: ¡a ¡-­‑> ¡DSTM ¡(DVar ¡a) ¡ readDVar ¡ ¡:: ¡DVar ¡a ¡-­‑> ¡DSTM ¡a ¡ writeDVar ¡:: ¡DVar ¡a ¡-­‑> ¡a ¡-­‑> ¡DSTM ¡a ¡ protectDVar ¡ ¡ ¡:: ¡DVar ¡a ¡-­‑> ¡IO ¡() ¡ unprotectDVar ¡:: ¡DVar ¡a ¡-­‑> ¡IO ¡() ¡

Region-Based Dynamic Separation

slide-15
SLIDE 15
  • 2. Running dynamic separation code
  • protected converts “DSTM action” to “STM action”
  • execute transaction using “atomically (protected (…))”
  • or, seamlessly combined with other STM actions, e.g. using

standard sequential composition:

  • unprotected converts “DSTM action” to “IO action”
  • runs as a non-transaction

Execution ¡interface ¡ protected ¡ ¡ ¡:: ¡DSTM ¡a ¡-­‑> ¡STM ¡a ¡ unprotected ¡:: ¡DSTM ¡a ¡-­‑> ¡IO ¡a ¡ atomically ¡ ¡:: ¡STM ¡a ¡-­‑> ¡IO ¡a ¡ Execution ¡example ¡ do ¡{ ¡ ¡ ¡x ¡<-­‑ ¡readTVar ¡t; ¡ ¡ ¡protected ¡(writeDVar ¡d ¡x)} ¡

Region-Based Dynamic Separation

slide-16
SLIDE 16
  • 3. Extra protection states
  • Prior work included three protection states
  • protected (always-accessed-in-transactions)
  • unprotected (never-accessed-in-transactions)
  • read-only
  • We add a fourth state: thread-local

Protection ¡state ¡interface ¡ protectDVar ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡:: ¡DVar ¡a ¡-­‑> ¡IO ¡() ¡ unprotectDVar ¡ ¡ ¡ ¡ ¡ ¡ ¡:: ¡DVar ¡a ¡-­‑> ¡IO ¡() ¡ makeReadOnlyDVar ¡ ¡ ¡ ¡:: ¡DVar ¡a ¡-­‑> ¡IO ¡() ¡ makeThreadLocalDVar ¡:: ¡DVar ¡a ¡-­‑> ¡IO ¡() ¡

Region-Based Dynamic Separation

slide-17
SLIDE 17
  • 4. Regions
  • Key feature: share protection state across objects in a

single data structure

  • Each DVar is allocated in a DRgn
  • Constant-time protection state changes

rgn ¡ rgn ¡ rgn ¡ rgn ¡

Data structure protection_state ¡ Region DVars

Region-Based Dynamic Separation

slide-18
SLIDE 18

Final interface

Dynamic ¡separation ¡interface ¡ data ¡DSTM ¡a ¡ data ¡DRgn ¡ data ¡DVar ¡a ¡ newDRgn ¡ ¡ ¡:: ¡DSTM ¡DRgn ¡ newDVar ¡ ¡ ¡:: ¡a ¡-­‑> ¡DRgn ¡-­‑> ¡DSTM ¡(DVar ¡a) ¡ readDVar ¡ ¡:: ¡DVar ¡a ¡-­‑> ¡DSTM ¡a ¡ writeDVar ¡:: ¡DVar ¡a ¡-­‑> ¡a ¡-­‑> ¡DSTM ¡a ¡ protectDRgn ¡ ¡ ¡:: ¡DRgn ¡-­‑> ¡IO ¡() ¡ unprotectDRgn ¡:: ¡DRgn ¡-­‑> ¡IO ¡() ¡ makeReadOnlyDRgn ¡:: ¡DRgn ¡-­‑> ¡IO ¡() ¡ makeThreadLocalDRgn ¡:: ¡DRgn ¡-­‑> ¡IO ¡() ¡ protected ¡ ¡ ¡:: ¡DSTM ¡a ¡-­‑> ¡STM ¡a ¡ unprotected ¡:: ¡DSTM ¡a ¡-­‑> ¡IO ¡a ¡

Region-Based Dynamic Separation

slide-19
SLIDE 19

Example: hash table

  • Many DVars, allocated in a single DRgn
  • Usually “protected”: multiple threads concurrently perform

inserts, lookups, and deletes using transactions

  • Rehashing of all keys causes high contention: do in

isolation (no concurrent operations)

  • Implement rehash as non-transaction for efficiency,

“unprotecting” the table’s DRgn during rehash

Region-Based Dynamic Separation

slide-20
SLIDE 20

Example: hash table

Dynamic ¡separation ¡example ¡ insert ¡:: ¡DHash ¡k ¡v ¡-­‑> ¡k ¡-­‑> ¡v ¡-­‑> ¡IO ¡() ¡ insert ¡hash ¡key ¡value ¡= ¡do ¡{ ¡ ¡ ¡doRehash ¡<-­‑ ¡atomically ¡(...); ¡ ¡ ¡when ¡doRehash ¡(do ¡{ ¡ ¡ ¡ ¡ ¡unprotectDRgn ¡(rgn ¡hash); ¡ ¡ ¡ ¡ ¡unprotected ¡(do ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡table ¡<-­‑ ¡readDVar ¡(dvar ¡hash); ¡ ¡ ¡ ¡ ¡ ¡ ¡table' ¡<-­‑ ¡DHT.rehash ¡table; ¡ ¡ ¡ ¡ ¡ ¡ ¡writeDVar ¡(dvar ¡hash) ¡table' ¡}); ¡ ¡ ¡ ¡ ¡protectDRgn ¡(rgn ¡hash); ¡ ¡ ¡ ¡ ¡atomically ¡(...) ¡}); ¡ ¡ ¡atomically ¡(insertHelper ¡hash ¡key ¡value) ¡}; ¡ Check table size and acquire table lock. Unprotect table’s DRgn. Perform rehash as non- transaction. Re-protect table’s DRgn. Release lock. Do insert.

Region-Based Dynamic Separation

slide-21
SLIDE 21

Formalism

  • We define two sets of formal semantics:

1.

Strong: describes a strongly-atomic STM.

2.

Weak: describes a lazy-update weakly-atomic STM.

  • Novel to our work:
  • Formalism extended to include regions, thread-local, read-only
  • Weak semantics uses lazy- rather than eager-update
  • All proofs verified in Coq
  • See paper for more details

Region-Based Dynamic Separation

slide-22
SLIDE 22

Theoretical Results

  • Strong => Weak

“Easy” theorem:

  • Weak => Strong (for well-behaved

programs only) (Much, much) harder theorem:

Region-Based Dynamic Separation

slide-23
SLIDE 23

Well-behaved?

  • A program is well-behaved if, under the Strong semantics,

a thread executes a read or write of a DVar and the DVar’s DRgn’s protection state is:

1.

protected – the thread must be in a transaction

2.

unprotected – the thread must NOT be in a transaction

3.

read-only – the action must be a read

4.

thread-local to θ – the thread’s ID must be θ

Region-Based Dynamic Separation

slide-24
SLIDE 24

Implementation

  • Protection state change is mini-transaction
  • readDVar and writeDVar check the DRgn’s protection

state and abort if inconsistent

  • See paper for more details

DVar ¡ rgn ¡ value ¡ … ¡ DRgn ¡ state ¡ tid ¡ … ¡

Region-Based Dynamic Separation

slide-25
SLIDE 25

Performance results

  • Approach 1: Take existing STM benchmarks, use all

DVars instead of TVars, measure slowdown: 0-20%

  • Caveat: STM Haskell is not state-of-the-art
  • Approach 2: Code up “killer uses” of dynamic separation,

measure speedup: 2-8x for 4 threads

  • e.g., initializing a data structure, resizing a hash table
  • Approach 3: Find an STM Haskell program that would

benefit from dynamic separation and rewrite it: future work

Region-Based Dynamic Separation

slide-26
SLIDE 26

Conclusion

  • Dynamic separation is an elegant way of addressing the

semantic issues with weakly-atomic STM implementations

  • We enrich existing work on dynamic separation by adding

regions and useful new protection states

  • Novel Haskell interface allows static- and dynamic-

separation code to be used side-by-side

  • We also provide a rigorous formalism and prototype

implementation

Region-Based Dynamic Separation