Java Memory (Consistency) Model - Polish JUG One Beer Talk #2

827 views

Published on

what is Java Memory Consistency Model, why you should not think of GC when you hear it and how it's done in general and in Java

Published in: Technology, Business
1 Comment
1 Like
Statistics
Notes
  • Sources: Pugh's papers, Adve's papers, Boehm's paper, Martin Thompson's blog (not credited in preso on image of exemplary CPU), Trisha Gee's blog post on barriers (image only) and last, but DEFINITELY not least - JSR-133 FAQ page.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
827
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Java Memory (Consistency) Model - Polish JUG One Beer Talk #2

  1. 1. @LAFK_pl, Polish JUG, May 2014 2
  2. 2. @LAFK_pl, Polish JUG, May 2014 3 MANAGEMENT!!
  3. 3. @LAFK_pl, Polish JUG, May 2014 4 Posted in 2012...
  4. 4. @LAFK_pl, Polish JUG, May 2014 5 ... and still not corrected
  5. 5. @LAFK_pl, Polish JUG, May 2014 6 Leslie Lamport ● Distributed system clocks ● Happens-before ● Sequential Consistency
  6. 6. @LAFK_pl, Polish JUG, May 2014 7 Bill Pugh ● FindBugs ● Java Memory Model is broken ● Final - Volatile ● Double-checked locking ● New JMM
  7. 7. @LAFK_pl, Polish JUG, May 2014 8 Sarita Adve ● Java Memory Model is broken ● Number of great papers on memory (consistency) models ● Best definition of MCM I found
  8. 8. @LAFK_pl, Polish JUG, May 2014 9 Within these 15-20 minutes Intro Memory model means? Hardware Java stuff
  9. 9. @LAFK_pl, Polish JUG, May 2014 10 Memory model? If NOT about GC then... what's it about?
  10. 10. @LAFK_pl, Polish JUG, May 2014 11 Memory CONSISTENCY ● Allowed optimizations ● Possible behaviours / executions of a (possibly multithreaded!) program ● Which cores / threads see which values ● How to make it consistent for us, programmers
  11. 11. @LAFK_pl, Polish JUG, May 2014 12
  12. 12. @LAFK_pl, Polish JUG, May 2014 13
  13. 13. @LAFK_pl, Polish JUG, May 2014 14 Where it matters? ● Javac / Jython / ... ● JIT ● Hardware of course ● So JMM is a LANGUAGE memory consistency model
  14. 14. @LAFK_pl, Polish JUG, May 2014 15 Hardware ● CISC or RISC CPU ● Number of registers ● Caches size or type ● How many functional units per CPU ● Pipeline: ● Instruction decode > address decode > memory fetch > register fetch > compute ...
  15. 15. @LAFK_pl, Polish JUG, May 2014 16 Program / code optimizations? ● Reorder ● (e.g. prescient store) ● Remove what's unnecessary ● (e.g. synchronize) ● Replace instructions / shorten machine code ● Function optimizations ● (e.g. Inlining) ● ...
  16. 16. @LAFK_pl, Polish JUG, May 2014 17 Exemplary CPU
  17. 17. @LAFK_pl, Polish JUG, May 2014 18 Barriers / fences „once memory has been pushed to the cache then a protocol of messages will occur to ensure all caches are coherent for any shared data. The techniques for making memory visible from a processor core are known as memory barriers or fences. – Martin Thompson, Mechanical Sympathy differs per architecture / CPU / cache type!
  18. 18. @LAFK_pl, Polish JUG, May 2014 19 Barriers / Fences ● CPU instruction ● Means „Flush now!” ● Forces update ● Starts cache coherency protocols ● Read / Write / Full
  19. 19. @LAFK_pl, Polish JUG, May 2014 20
  20. 20. @LAFK_pl, Polish JUG, May 2014 21 Summarizing Java Language Spec: ● Describes whether the execution trace is a legal execution of the program ● Works by examining each read and checking write observed by that read ● Write is valid when it follows certain rules ● Describes possible behaviours of the program ● Implementor adhering to above can optimize code as he likes
  21. 21. @LAFK_pl, Polish JUG, May 2014 22 What rules? ● Write is valid when it follows certain rules
  22. 22. @LAFK_pl, Polish JUG, May 2014 23 Before, or after?
  23. 23. @LAFK_pl, Polish JUG, May 2014 24 Before, or after?
  24. 24. @LAFK_pl, Polish JUG, May 2014 25 JSR-133 = new JM(C)M
  25. 25. @LAFK_pl, Polish JUG, May 2014 26 Class Reordering { int x = 0, y = 0; public void writer() { x = 1; y = 2; } public void reader() { int r1 = y; // TUTAJ: y == 2 => x == ? int r2 = x; } } Reordering - classic example
  26. 26. @LAFK_pl, Polish JUG, May 2014 27 What was wrong with old JM(C)M? You took a look, read the specs entire and...
  27. 27. @LAFK_pl, Polish JUG, May 2014 28
  28. 28. @LAFK_pl, Polish JUG, May 2014 29 New Java Memory Model ● SC on single-core and single-thread CPU is fine but it doesn't cut it now ● To ensure visibility, JVM JMM spec ensures: ● final ● volatile ● Synchronized are done well thistime
  29. 29. @LAFK_pl, Polish JUG, May 2014 30 Barriers in Java – rules ● JMM ● volatile – sfence after write, lfence before read ● final – sfence after init (field visibility) ● Atomic instructions (like lock) = mfence
  30. 30. @LAFK_pl, Polish JUG, May 2014 31 Further topics ● Why MCM and not a threading library? H.Boehm ● Better MCM? Sarita Adve ● Possible optimizations and their gains ● Performance of Java and hardware MCMs? Clashes? ● JMCM rules in more detail

×