1 / 16

Avoiding L iveness hazards

Avoiding L iveness hazards. Indiscriminate use of locking Lock-ordering deadlocks Incorrect use of thread pools Resource deadlocks Tradeoff between safety and liveness Example Dining philosopher’s problem Database transactions recover by aborting transactions

leon
Download Presentation

Avoiding L iveness hazards

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Avoiding Liveness hazards • Indiscriminate use of locking • Lock-ordering deadlocks • Incorrect use of thread pools • Resource deadlocks • Tradeoff between safety and liveness • Example • Dining philosopher’s problem • Database transactions recover by aborting transactions • Deadlocks are non-deterministic

  2. Example

  3. Dynamic lock-order deadlocks • How to fix such deadlocks?

  4. Resource deadlocks • Task requires two resources • Tasks from two threads acquire the two resources in opposite order • Resource deadlocks

  5. Avoiding/diagnosing deadlocks • Not acquire more than one lock • Not practical • Lock-ordering protocol for locks acquired together • Use open calls • synchronized (this) { o.foo(); } -- ? • Timed lock attempts • Back off and try again • random backoff

  6. Starvation • Perpetually denied acess to resources • E.g, CPU cycles • Inappropriate use of thread priorities • Non terminating constructs • Infinite loops • Resource waits • Poor responsiveness • GUI applications • Use background threads for processing

  7. Livelock • Thread not blocked • Does not make progress however • Operation fails repeatedly • E.g., processing a message type • Introduce randomness to avoid livelocks

  8. Performance and Scalability • Threads for improved performance • Overheads • Coordinating between threads • Context switching • Thread creation and teardown • Scheduling overhead • Greater throughput than overheads – beneficial • To achieve better performance • Utilize processing resources effectively • Enable program to exploit addl. processing resources

  9. Scalability versus Performance • Performance • “how fast” • Same work with less effort • E.g., reuse cache results • Scalability • “how much” • More work with more resources • Increases amount of work to be done • Divide into tasks • Consolidate tasks

  10. Amdahl’s law • Speedup <= 1/(F + (1 – F)/N) • F: fraction of work done serially

  11. Context switching • Manipulate shared data structures in the OS • Less CPU available for program • Data required not in local processor cache • Cache misses initially • Threads run slowly • Minimum time for each thread to run • Amortizes cost of context switching • Reduces responsiveness • More blocking = more context switches • vmstat on Unix reports number of context switches

  12. Memory synchronization • Visibility guarantees of synchronization causes • Memory barriers • Flush/invalidate caches • Flush hardware write buffers • Inhibit compiler optimizations • Uncontended vs Contended synchronization • Uncontended • Perform optimizations • E.g., lock object accessible by only one thread

  13. Example • Lock coarsening

  14. Blocking • Spin waiting • Suspending blocked thread • Unnecessary context switches

  15. Reducing lock contention • Reduce duration of locks hold • Reduce frequency of requests • Replace exclusive locks with coordination mechanisms

  16. Reducing lock contention • Narrow lock scope • Reduce lock granularity • Lock splitting • Different locks for different variables • Lock striping • Extension of split to variable sized objects • Avoid hot fields • E.g., addQuery, addUser • Alternative to exclusive locks • ReadWriteLocks • Avoid object pooling • Coordination to the pool structure

More Related