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

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

If you can't read please download the document

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

MANAGEMENT!!

Posted in 2012...

... and still not corrected

Leslie Lamport

Distributed system clocks

Happens-before

Sequential Consistency

Bill Pugh

FindBugs

Java Memory Model is brokenFinal - Volatile

Double-checked locking

New JMM

Sarita Adve

Java Memory Model is broken

Number of great papers on memory (consistency) models

Best definition of MCM I found

Within these 15-20 minutes

IntroMemory model means?HardwareJava stuff

Memory model?

If NOT about GC then...

what's it about?

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

Where it matters?

Javac / Jython / ...

JIT

Hardware of course

So JMM is a LANGUAGE memory consistency model

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 ...

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)

...

Exemplary CPU

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!

Barriers / Fences

CPU instruction

Means Flush now!

Forces update

Starts cache coherency protocols

Read / Write / Full

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

What rules?

Write is valid when it follows certain rules

Before, or after?

Before, or after?

JSR-133 = new JM(C)M

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

What was wrong with old JM(C)M?

You took a look, read the specs entire and...

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

Synchronizedare done well thistime

Barriers in Java rules

JMMvolatile sfence after write, lfence before read

final sfence after init (field visibility)

Atomic instructions (like lock) = mfence

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