Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Transactional Memory / Garbage Collection Analogy Dan Grossman University of Washington OOPSLA 2007.

Similar presentations


Presentation on theme: "The Transactional Memory / Garbage Collection Analogy Dan Grossman University of Washington OOPSLA 2007."— Presentation transcript:

1 The Transactional Memory / Garbage Collection Analogy Dan Grossman University of Washington OOPSLA 2007

2 24 October 2007Dan Grossman: The TM/GC Analogy2 Why are you here? Transactional Memory / Garbage Collection Analogy How an analogy produces new research –A foundation of cognition? –A way to educate? Probably not why you’re here… But hope to encourage analogies as a key intellectual tool for practical results

3 24 October 2007Dan Grossman: The TM/GC Analogy3 Why are you here? Transactional Memory / Garbage Collection Analogy Why GC is great –Benefits, limitations, implementation techniques Probably not why you’re here… But hope to give a new perspective on this mature topic

4 24 October 2007Dan Grossman: The TM/GC Analogy4 Why are you here? Transactional Memory / Garbage Collection Analogy Why TM is great –Benefits, limitations, implementation techniques Probably is why you’re here… A very hot topic I hope to put in context –Promote without overselling

5 24 October 2007Dan Grossman: The TM/GC Analogy5 Why am I here? Transactional Memory / Garbage Collection Analogy Understand TM and GC better by explaining remarkable similarities –Benefits, limitations, and implementations “TM is to shared-memory concurrency as GC is to memory management”

6 24 October 2007Dan Grossman: The TM/GC Analogy6 How to do it Why an analogy helps Brief separate overview of GC and TM The core technical analogy (but read the essay) –And why concurrency is still harder Provocative questions based on the analogy (about 40 minutes; plenty of time for discussion)

7 24 October 2007Dan Grossman: The TM/GC Analogy7 Two bags of concepts reachability dangling pointers reference counting space exhaustion weak pointers real-time guarantees liveness analysis conservative collection finalization GC races eager update deadlock obstruction-freedom open nesting false sharing TM memory conflicts escape analysis

8 24 October 2007Dan Grossman: The TM/GC Analogy8 Interbag connections reachability dangling pointers reference counting space exhaustion weak pointers real-time guarantees liveness analysis conservative collection finalization GC races eager update deadlock obstruction-freedom open nesting false sharing TM memory conflicts escape analysis

9 24 October 2007Dan Grossman: The TM/GC Analogy9 Analogies help organize reachability dangling pointers reference counting space exhaustion weak pointers real-time guarantees liveness analysis conservative collection finalization GC races eager update deadlock obstruction-freedom open nesting false sharing TM memory conflicts escape analysis

10 24 October 2007Dan Grossman: The TM/GC Analogy10 So the goals are… Leverage the design trade-offs of GC to guide TM –And vice-versa? Identify open research Motivate TM –TM improves concurrency as GC improves memory –GC is a huge help despite its imperfections –So TM is a huge help despite its imperfections

11 24 October 2007Dan Grossman: The TM/GC Analogy11 How to do it “TM is to shared-memory concurrency as GC is to memory management” Why an analogy helps Brief separate overview of GC and TM The core technical analogy (but read the essay) –And why concurrency is still harder Provocative questions based on the analogy

12 24 October 2007Dan Grossman: The TM/GC Analogy12 Memory management Allocate objects in the heap Deallocate objects to reuse heap space –If too soon, dangling-pointer dereferences –If too late, poor performance / space exhaustion

13 24 October 2007Dan Grossman: The TM/GC Analogy13 GC Basics Automate deallocation via reachability approximation –Approximation can be terrible in theory Reachability via tracing or reference-counting –Duals [Bacon et al OOPSLA04] Lots of bit-level tricks for simple ideas –And high-level ideas like a nursery for new objects rootsheap objects

14 24 October 2007Dan Grossman: The TM/GC Analogy14 A few GC issues Weak pointers –Let programmers overcome reachability approx. Accurate vs. conservative –Conservative can be unusable (only) in theory Real-time guarantees for responsiveness

15 24 October 2007Dan Grossman: The TM/GC Analogy15 GC Bottom-line Established technology with widely accepted benefits Even though it can perform terribly in theory Even though you can’t always ignore how GC works (at a high-level) Even though an active research area after 40 years

16 24 October 2007Dan Grossman: The TM/GC Analogy16 Concurrency Restrict attention to explicit threads communicating via shared memory Synchronization mechanisms coordinate access to shared memory –Bad synchronization can lead to races or a lack of parallelism (even deadlock)

17 24 October 2007Dan Grossman: The TM/GC Analogy17 Atomic An easier-to-use and harder-to-implement primitive lock acquire/release(behave as if) no interleaved computation; no unfair starvation void deposit(int x){ synchronized(this){ int tmp = balance; tmp += x; balance = tmp; }} void deposit(int x){ atomic { int tmp = balance; tmp += x; balance = tmp; }}

18 24 October 2007Dan Grossman: The TM/GC Analogy18 TM basics atomic (and related constructs) implemented via transactional memory Preserve parallelism as long as no memory conflicts –Can lead to unnecessary loss of parallelism If conflict detected, abort and retry Lots of complicated details –All updates must appear to happen at once

19 24 October 2007Dan Grossman: The TM/GC Analogy19 A few TM issues Open nesting: atomic { … open { s; } … } Granularity (potential false conflicts) atomic{… x.f++; …} atomic{… x.g++; … } Update-on-commit vs. update-in-place Obstruction-freedom …

20 24 October 2007Dan Grossman: The TM/GC Analogy20 Advantages So atomic “sure feels better than locks” But the crisp reasons I’ve seen are all (great) examples –Personal favorite from Flanagan et al Same issue as Java’s StringBuffer.append –(see essay for close 2nds)

21 24 October 2007Dan Grossman: The TM/GC Analogy21 Code evolution void deposit(…) { synchronized(this) { … }} void withdraw(…) { synchronized(this) { … }} int balance(…) { synchronized(this) { … }}

22 24 October 2007Dan Grossman: The TM/GC Analogy22 Code evolution void deposit(…) { synchronized(this) { … }} void withdraw(…) { synchronized(this) { … }} int balance(…) { synchronized(this) { … }} void transfer(Acct from, int amt) { if(from.balance()>=amt && amt < maxXfer) { from.withdraw(amt); this.deposit(amt); } }

23 24 October 2007Dan Grossman: The TM/GC Analogy23 Code evolution void deposit(…) { synchronized(this) { … }} void withdraw(…) { synchronized(this) { … }} int balance(…) { synchronized(this) { … }} void transfer(Acct from, int amt) { synchronized(this) { //race if(from.balance()>=amt && amt < maxXfer) { from.withdraw(amt); this.deposit(amt); }

24 24 October 2007Dan Grossman: The TM/GC Analogy24 Code evolution void deposit(…) { synchronized(this) { … }} void withdraw(…) { synchronized(this) { … }} int balance(…) { synchronized(this) { … }} void transfer(Acct from, int amt) { synchronized(this) { synchronized(from) { //deadlock (still) if(from.balance()>=amt && amt < maxXfer) { from.withdraw(amt); this.deposit(amt); } }} }

25 24 October 2007Dan Grossman: The TM/GC Analogy25 Code evolution void deposit(…) { atomic { … }} void withdraw(…) { atomic { … }} int balance(…) { atomic { … }}

26 24 October 2007Dan Grossman: The TM/GC Analogy26 Code evolution void deposit(…) { atomic { … }} void withdraw(…) { atomic { … }} int balance(…) { atomic { … }} void transfer(Acct from, int amt) { //race if(from.balance()>=amt && amt < maxXfer) { from.withdraw(amt); this.deposit(amt); } }

27 24 October 2007Dan Grossman: The TM/GC Analogy27 Code evolution void deposit(…) { atomic { … }} void withdraw(…) { atomic { … }} int balance(…) { atomic { … }} void transfer(Acct from, int amt) { atomic { //correct and parallelism-preserving! if(from.balance()>=amt && amt < maxXfer){ from.withdraw(amt); this.deposit(amt); }

28 24 October 2007Dan Grossman: The TM/GC Analogy28 But can we generalize So TM sure looks appealing… But what is the essence of the benefit? You know my answer…

29 24 October 2007Dan Grossman: The TM/GC Analogy29 How to do it “TM is to shared-memory concurrency as GC is to memory management” Why an analogy helps Brief separate overview of GC and TM The core technical analogy (but read the essay) –And why concurrency is still harder Provocative questions based on the analogy

30 24 October 2007Dan Grossman: The TM/GC Analogy30 The problem, part 1 Why memory management is hard: Balance correctness (avoid dangling pointers) And performance (no space waste or exhaustion) Manual approaches require whole-program protocols Example: Manual reference count for each object Must avoid garbage cycles race conditions concurrent programming loss of parallelismdeadlock lock lock acquisition

31 24 October 2007Dan Grossman: The TM/GC Analogy31 The problem, part 2 Manual memory-management is non-modular: Caller and callee must know what each other access or deallocate to ensure right memory is live A small change can require wide-scale changes to code –Correctness requires knowing what data subsequent computation will access synchronization locks are held release concurrent

32 24 October 2007Dan Grossman: The TM/GC Analogy32 The solution Move whole-program protocol to language implementation One-size-fits-most implemented by experts –Usually combination of compiler and run-time GC system uses subtle invariants, e.g.: –Object header-word bits –No unknown mature pointers to nursery objects In theory, object relocation can improve performance by increasing spatial locality –In practice, some performance loss worth convenience thread-sharedthread-local TM optimistic concurrency parallelism

33 24 October 2007Dan Grossman: The TM/GC Analogy33 Two basic approaches Tracing: assume all data is live, detect garbage later Reference-counting: can detect garbage immediately –Often defer some counting to trade immediacy for performance (e.g., trace the stack) update-on-commitconflict-freeconflicts update-in-placeconflicts conflict-detection optimistic reads

34 24 October 2007Dan Grossman: The TM/GC Analogy34 So far… memory managementconcurrency correctnessdangling pointersraces performancespace exhaustiondeadlock automationgarbage collectiontransactional memory new objectsnursery datathread-local data eager approachreference-countingupdate-in-place lazy approachtracingupdate-on-commit

35 24 October 2007Dan Grossman: The TM/GC Analogy35 Incomplete solution GC a bad idea when “reachable” is a bad approximation of “cannot-be-deallocated” Weak pointers overcome this fundamental limitation –Best used by experts for well-recognized idioms (e.g., software caches) In extreme, programmers can encode manual memory management on top of GC –Destroys most of GC’s advantages…

36 24 October 2007Dan Grossman: The TM/GC Analogy36 Circumventing GC class Allocator { private SomeObjectType[] buf = …; private boolean[] avail = …; Allocator() { /*initialize arrays*/ } SomeOjbectType malloc() { /* find available index */ } void free(SomeObjectType o) { /* set corresponding index available */ }

37 24 October 2007Dan Grossman: The TM/GC Analogy37 Incomplete solution GC a bad idea when “reachable” is a bad approximation of “cannot-be-deallocated” Weak pointers overcome this fundamental limitation –Best used by experts for well-recognized idioms (e.g., software caches) In extreme, programmers can encode manual memory management on top of GC –Destroys most of GC’s advantages… TM memory conflict run-in-parallel Open nested txns unique id generation lockingTM

38 24 October 2007Dan Grossman: The TM/GC Analogy38 Circumventing GC class SpinLock { private boolean b = false; void acquire() { while(true) atomic { if(b) continue; b = true; return; } void release() { atomic { b = false; } } TM

39 24 October 2007Dan Grossman: The TM/GC Analogy39 Programmer control For performance and simplicity, GC treats entire objects as reachable, which can lead to more space Space-conscious programmers can reorganize data accordingly But with conservative collection, programmers cannot completely control what appears reachable –Arbitrarily bad in theory (some) TM accessed less parallelism Parallelism coarser granularity (e.g., cache lines) conflicting

40 24 October 2007Dan Grossman: The TM/GC Analogy40 So far… memory managementconcurrency correctnessdangling pointersraces performancespace exhaustiondeadlock automationgarbage collectiontransactional memory new objectsnursery datathread-local data eager approachreference-countingupdate-in-place lazy approachtracingupdate-on-commit key approximationreachabilitymemory conflicts manual circumventionweak pointersopen nesting uncontrollable approx.conservative collectionfalse memory conflicts

41 24 October 2007Dan Grossman: The TM/GC Analogy41 More I/O: input after output of pointers can cause incorrect behavior due to dangling pointers Real-time guarantees doable but costly Static analysis can avoid overhead –Example: liveness analysis for fewer root locations –Example: remove write-barriers on nursery data Obstruction-freedom thread-local escape potential conflicts irreversible actions in transactions

42 24 October 2007Dan Grossman: The TM/GC Analogy42 Too much coincidence! memory managementconcurrency correctnessdangling pointersraces performancespace exhaustiondeadlock automationgarbage collectiontransactional memory new objectsnursery datathread-local data eager approachreference-countingupdate-in-place lazy approachtracingupdate-on-commit key approximationreachabilitymemory conflicts manual circumventionweak pointersopen nesting uncontrollable approx.conservative collectionfalse memory conflicts more…I/O of pointersI/O in transactions real-timeobstruction-free liveness analysisescape analysis ……

43 24 October 2007Dan Grossman: The TM/GC Analogy43 How to do it “TM is to shared-memory concurrency as GC is to memory management” Why an analogy helps Brief separate overview of GC and TM The core technical analogy (but read the essay) –And why concurrency is still harder Provocative questions based on the analogy

44 24 October 2007Dan Grossman: The TM/GC Analogy44 Concurrency is hard! I never said the analogy means TM parallel programming is as easy as GC sequential programming By moving low-level protocols to the language run-time, TM lets programmers just declare where critical sections should be But that is still very hard and – by definition – unnecessary in sequential programming Huge step forward = panacea /

45 24 October 2007Dan Grossman: The TM/GC Analogy45 Stirring things up I can defend the technical analogy on solid ground Then push things (perhaps) too far … 1.Many used to think GC was too slow without hardware 2.Many used to think GC was “about to take over” (decades before it did) 3.Many used to think we needed a “back door” for when GC was too approximate

46 24 October 2007Dan Grossman: The TM/GC Analogy46 Inciting you Push the analogy further or discredit it Generational GC? Contention management? Inspire new language design and implementation Teach programming with TM as we teach programming with GC Find other analogies and write essays

47 24 October 2007Dan Grossman: The TM/GC Analogy47 “TM is to shared-memory concurrency as GC is to memory management”


Download ppt "The Transactional Memory / Garbage Collection Analogy Dan Grossman University of Washington OOPSLA 2007."

Similar presentations


Ads by Google