Top Banner
Java Memory Consistency Model @LAFK_pl Consultant @ Tomasz Borek
62

4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Jul 16, 2015

Download

Software

PROIDEA
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Java Memory Consistency Model

@LAFK_plConsultant @

Tomasz Borek

Page 2: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

GeeCON 2015 Kraków!

@LAFK_plConsultant @

http://2015.geecon.org/register/

Page 3: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

I'll take ALL feedback I can get. @LAFK_pl, #JMCM

@LAFK_plConsultant @

Page 4: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Symentis

@LAFK_pl

Page 5: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Symentis

@LAFK_pl

Jarosław Pałka Kuba Marchwicki

Page 6: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Today...

● A little bragging● Fallacy correction● Memory model

● The Java one mostly

● Short advice what about it● Lot of links for later reading

● Yeah, 45 minutes only :P

@LAFK_plConsultant @

Page 7: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Who knows (heard of)?

● Gene Amdahl?● Gordon Moore?● Leslie Lamporte?● Bill Pugh?● Sarita Adve?● Hans Boehm?● Martin Thompson?● Aleksey Shipilev?

@LAFK_pl

Page 8: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Hands up, who...

● Doesn't program● Knows Moore's law?● Knows Amdahl's law?● Can explain concurrency vs parallelism?● Codes with mechanical sympathy?

● Tries to?● Knows what mechanical sympathy is?

@LAFK_pl

Page 9: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Fallacy #0

@LAFK_pl

Java Memory Model is about GC, memory management, Eden, Survivors etc.

Page 10: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Not true at all!

@LAFK_pl

Java Memory Model is about GC, memory management, Eden, Survivors, etc.

Page 11: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

@LAFK_pl

Page 12: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

MANAGEMENT!!

@LAFK_pl

Page 13: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Written in 2012...

@LAFK_pl

Page 14: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

… and still there

@LAFK_pl

Page 15: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Correction!

@LAFK_plConsultant @

Page 16: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

JLS, section 17.4:

A memory model describes, given a program and an execution trace of that program, whether the

execution trace is a legal execution of the program. The Java programming language

memory model works by examining each read in an execution trace and checking that the write

observed by that read is valid according to certain rules.

@LAFK_plConsultant @

Page 17: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

JLS, section 17.4:

A memory model describes, given a program and an execution trace of that program, whether the

execution trace is a legal execution of the program. The Java programming language

memory model works by examining each read in an execution trace and checking that the write

observed by that read is valid according to certain rules.

@LAFK_plConsultant @

Page 18: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Sarita Adve: memory consistency model

@LAFK_plConsultant @

Page 19: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Aleksey Shipilev:

@LAFK_plConsultant @

Memory model answers one simple question:What values can a particular read in a program

return?

Page 20: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Bill Pugh, Jeremy Manson

● Most cores have many cache layers● What if 2 cores look at same value?● Memory model defines when and who sees

what● There're strong and weak models

● Strong guarantee seeing same things across whole system

● Weak only sometimes, via barriers / fences

@LAFK_plConsultant @

Page 21: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Bill Pugh, Jeremy Manson:

What is a memory model, anyway?

At the processor level, a memory model defines necessary and sufficient conditions for knowing that writes to memory by other processors are

visible to the current processor, and writes by the current processor are visible to other processors.

@LAFK_plConsultant @

Page 22: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

So?

● Memory CONSISTENCY● Allowed optimisations● Possible executions of a (possibly

multithreaded!) program● Which cores / threads see which values● How to make it consistent for programmers● What you're allowed to assume

@LAFK_plConsultant @

Page 23: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Fallacy #1

I don't need to worry about JMCM since REALLY smart engineers crafted it.

@LAFK_plConsultant @

Page 24: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Half-true

I don't need to worry about JMCM since REALLY smart engineers crafted it

@LAFK_plConsultant @

Page 25: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Smart, sure! But still:

● Smart people are still people● JMCM is damn hard! Yeah, they botched it.

● Java <> JVM● JMCM is for JVM... but with Java in mind● NO tech is a talisman of functionality!

@LAFK_plConsultant @

Page 26: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

JSR-133?

● Messed up final● Spec not for humans● Messed up double-locking● Messed up volatile● Each implementation on it's own

@LAFK_plConsultant @

Page 27: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

More?

@LAFK_plConsultant @

JEPS-188 and JMM9?

Page 28: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Fallacy #2

JMCM is irrelevant for me.

@LAFK_plConsultant @

Page 29: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Depends!

● What you write and with what● Java? Not?● Need performance?● What platforms?

● Multicore era...

@LAFK_plConsultant @

Page 30: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Fallacy #3

JMCM is for fanatics. I have frameworks for that.

@LAFK_plConsultant @

Page 31: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Moore's ”law”?

Consultant @ Consultant @ @LAFK_pl

Page 32: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Moore's ”law”

I see Moore’s law dying here in the next decade or so.

– Gordon Moore, 2015

Consultant @ @LAFK_pl

Consultant @

Page 33: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Amdahl's law?

@LAFK_pl Consultant @

Page 34: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Amdahl's law

@LAFK_pl

The speedup of a program using multiple processors in parallel computing is limited by the

sequential fraction of the program. For example, if 95% of the program can be parallelized, the theoretical maximum speedup using parallel

computing would be 20× as shown in the diagram, no matter how many processors are

used.

Consultant @

Page 35: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Rok 1967, Gene Amdahl states:

@LAFK_pl

For over a decade prophets have voiced the contention that the organization of a single

computer has reached its limits and that truly significant advances can be made only by

interconnection of a multiplicity of computers in such a manner as to permit cooperative solution.

Consultant @

Page 36: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Rok 1967, Gene Amdahl states:

@LAFK_pl

For over a decade prophets have voiced the contention that the organization of a single

computer has reached its limits and that truly significant advances can be made only by

interconnection of a multiplicity of computers in such a manner as to permit cooperative

solution.

Consultant @

Page 37: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Cores... MOAR!!

@LAFK_pl Consultant @

Page 38: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Data distance

● http://i.imgur.com/k0t1e.png

Page 39: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

So, data distance varies

@LAFK_pl

Know thyne cache lines!

Consultant @

Page 40: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

@LAFK_plConsultant @

So if it matters, what then?

Page 41: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Where it matters

● Javac / Jython / ...● JIT

● Hardware, duh!

● Each time: another team

@LAFK_plConsultant @

Page 42: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Hardware

● Various ISA CPUs● Number of registers● Caches size or type, buses implementations● Cache protocols (MESI, AMD's MOESI, Intel's...)● How many functional units per CPU● How many CPUs● Pipeline:

● Instruction decode > address decode > memory fetch > register fetch > compute ...

@LAFK_plConsultant @

Page 43: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

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)

● ...

@LAFK_plConsultant @

Page 44: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Exemplary CPU

@LAFK_plConsultant @

Page 45: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

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!

@LAFK_plConsultant @

Page 46: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Barriers / Fences● CPU instruction● Means ”flush BUFFER now!”● CMPXCHG (may be

lacking!)

● Forces update● Starts cache coherency

protocols

● Read / Write / Full

@LAFK_plConsultant @

Page 47: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

@LAFK_plConsultant @

Words of summary and gratitude

Page 48: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Doug Lea says:

@LAFK_plConsultant @

The best way is to build up a small repertoire of constructions that you know the answers for and

then never think about the JMM rules again unless you are forced to do so! Literally nobody

likes figuring things out of JMM rules as stated, or can even routinely do so correctly. This is one of

the many reasons we need to overhaul JMM someday.

Page 49: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Doug Lea advice:

@LAFK_plConsultant @

The best way is to build up a small repertoire of constructions that you know the answers for and then never think about the JMM rules again unless you are forced to do so! Literally

nobody likes figuring things out of JMM rules as stated, or can even routinely do so correctly. This is one of the many reasons we need to overhaul

JMM someday.

Page 50: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Mechanical sympathy:

@LAFK_plConsultant @

● Cache lines misses hurt● Going to main memory hurts● Cycles are important● L1, L2 caches are cheap but require cache

coherency protocols and memory barriers● Not every hardware has all barriers●

Page 51: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Gordon Moore

● Fairchild Semi-conductors co-founder

● ”Law author”

● Intel co-founder

@LAFK_pl

Page 52: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Gene Amdahl

● IBM fellow

● IBM & Amdahl mainframes

● Coined law in 1967

@LAFK_pl

Page 53: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Leslie Lamport

● Distributed system clocks

● Happens before

● Sequential consistency

@LAFK_pl

Page 54: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Bill Pugh

● FindBugs

● Java Memory Model is broken● Final - Volatile● Double-checked

locking

● ”New” JMM

@LAFK_pl

Page 55: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Sarita Adve

● Java Memory Model is broken

● Great many MCM papers

● Best MCM def I found

@LAFK_pl

Page 56: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Martin Thompson

● Mechanical sympathy blog & mailing list

● Aeron protocol

● Mechanical sympathy proponent

@LAFK_pl

Page 57: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

This wouldn't have happened if not

● Jarek Pałka, who kicked me out here some time ago

● Those folks, who said ”make more” after the lightning talk I've done

● Java Day Kiev 2014

@LAFK_plConsultant @

Page 58: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Not possible without:

● Leslie Lamport's works on distributed sistems

● Bill Pugh's work on JSR-133! http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html

● Sarita Adve's paperts, especially shared MCM tutorial: http://www.hpl.hp.com/techreports/Compaq-DEC/WRL-95-7.pdf

@LAFK_plConsultant @

Page 59: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Terrific – and tough - reading

● Martin Thompson: Mechanical Sympathy (mailing list & blog)● JEPS 188: http://openjdk.java.net/jeps/188

● Goetz et al: "Java Concurrency in Practice"● Herilhy, Shavit, "The Art of Multiprocessor Programming"● Adve, "Shared Memory Consistency Models: A Tutorial"● Manson, "Special PoPL Issue: The Java Memory Model"● Huisman, Petri, "JMM: A Formal Explanation"● Aleksey Shipilev blog post: http://shipilev.net/blog/2014/jmm-pragmatics/

@LAFK_plConsultant @

Page 60: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Laws and related:

● Moore's ”law”: http://www.cs.utexas.edu/~fussell/courses/cs352h/papers/moore.pdf

● Rock's law: http://en.wikipedia.org/wiki/Rock's_law

● Amdahl's law: ● http://en.wikipedia.org/wiki/Amdahl%27s_law

● Validity of the Single-Processor Approach to Achieving Large-Scale Computing Capabilities AFIPS Press, 1967

● J.L. Gustafson, “Reevaluating Amdahl’s Law,” Comm. ACM, May 1988

● Pleasantly parallel problems: http://en.wikipedia.org/wiki/Embarrassingly_parallel

@LAFK_plConsultant @

Page 61: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

Special thanks

● Konrad Malawski and Tomek Kowalczewski, these guys really dig that stuff

● Bartosz Milewski who helped me rediscover Hans Boehm

@LAFK_plConsultant @

Page 62: 4Developers 2015: Java Memory Consistency Model or intro to multithreaded programming - Tomasz Borek, Jacek Jagieła

YOU! You persevered through!

@LAFK_plConsultant @