About Me
Java/JVM/GC performance person
Current Life:
Consultant who solves Managed Runtime performance issues
Past Life:
Worked with Oracle, Sun,AMD…
©2017 CodeKaram1
Agenda
• Setting the stage
• A quick glance at JDK 9 JEPs
• Building a problem statement
• Studying JEP 143 – Improve contended locking
©2017 CodeKaram2
Agenda
• Quick overview of top-down and bottom-up performance
approaches
• Choosing the right approach!
• Building our arsenal!
©2017 CodeKaram3
Agenda
• Observing the targeted improvements
• Demo comparing JDK 9 with JDK 8
• Summarizing the observations
©2017 CodeKaram4
Setting The Stage
©2017 CodeKaram5
JEPs in JDK 9
©2017 CodeKaram6
There are 89 total JEPs in JDK 9
JEPs in JDK 9
…and contended locking performanceimprovement (JEP143) is
one of them J
©2017 CodeKaram7
What is JEP 143?
©2017 CodeKaram8
JEP 143 brings about improvements to contended locks*
*In the past (Java 6) we have alreadyseen improvements to uncontendedlocksusing
BiasedLockingtechnique.
Building A Problem
Statement
©2017 CodeKaram9
Summary of JEP 143
©2017 CodeKaram10
“Improve the performance of contended Java object monitors”
©2017 CodeKaram11
“Improve the performance of contended Java object monitors”
Summary of JEP 143
Uncontended Locks
©2017 CodeKaram12
A single thread ‘t’ is executing a synchronized method
Contended Locks
©2017 CodeKaram13
A different thread ‘u’ wants to enter the synchronized method
that is already locked by thread ‘t’
Contended Locks
©2017 CodeKaram14
This is a heavy weight lock also known as inflated lock
Object Monitor maintains “WaitSet” for threads waiting for the
contended lock
Java Monitors
©2017 CodeKaram15
Wait
Q
Object
Monitor
Enter
Exit
Q
Wait Set
©2017 CodeKaram16
• A set of threads
• At creation – empty set of threads
• Can be manipulated only by:
• Object.wait, Object.notify, Object.notifyAll
Java Monitors
©2017 CodeKaram17
Wait
Q
Object
Monitor
Enter
Exit
Q
Wait
Notify
Top-Down
Performance Approach
©2017 CodeKaram18
What areYouTrying to Achieve?
©2017 CodeKaram19
Improve application?
Top-Down Approach
©2017 CodeKaram20
OS
Hardware
Application Ecosystem
Application
JRE
JVM
Top Down Approach - Process
•Step 1Monitor
•Step 2 Profile
•Step 3Analyze
•Step 4 Tune +
Apply
Bottom-Up
Performance Approach
©2017 CodeKaram22
What areYouTrying to Achieve?
©2017 CodeKaram23
Improve the platform?
Bottom-Up Approach
©2017 CodeKaram24
OS
Hardware
Application Ecosystem
Application
JRE
JVM
Bottom Up Approach - Process
•Step 1Monitor
•Step 2 Profile
•Step 3Analyze
•Step 4 Tune +
Apply
Choosing The Right
Approach …
©2017 CodeKaram26
Improve the JVM
What AreWeTryingTo Do?
©2017 CodeKaram27
The Bottom-Up
Approach
HenceWeChoose…
©2017 CodeKaram28
Bottom Up Approach
©2017 CodeKaram29
I NEED the power!!
… to stress the platform
Setting Up For
Benchmarking
©2017 CodeKaram30
Where to Start?
©2017 CodeKaram31
• Know what you are stressing
• Get/ write the appropriate workload/ application
• Get/write the appropriate tools
Know What You’re
Stressing…
©2017 CodeKaram32
Speed UpTargets
©2017 CodeKaram33
• PlatformEvent::unpark()
• Java monitor enter operations
• Java monitor exit operations
• Java monitor notify/notifyAll operations
Get/Write The
Appropriate
Workload/Application
…
©2017 CodeKaram34
FromThe JEP …
©2017 CodeKaram35
LockLoops-JSR166-Doug-Sept2009 (was LockLoops):
The benchmark compares multiple locking techniques
For our purpose, we just need to test the contended locks.
Contended Locking Test
©2017 CodeKaram36
private static class BuiltinLockLoop extends LockLoop {
final int loop(int n) {
int sum = 0;
while (n-- > 0) {
synchronized (this) {
v = LoopHelpers.compute1(v);
}
sum += LoopHelpers.compute2(v);
}
return sum;
}
}
Contended Locking Test
©2017 CodeKaram37
private static class BuiltinLockLoop extends LockLoop {
final int loop(int n) {
int sum = 0;
while (n-- > 0) {
synchronized (this) {
v = LoopHelpers.compute1(v);
}
sum += LoopHelpers.compute2(v);
}
return sum;
}
}
Where to Next?
©2017 CodeKaram38
Ensure that you are in fact measuring contended object monitor
performance!
How DoWe DoThat?
©2017 CodeKaram39
Bypass biased locking: Use -XX:-UseBiasedLocking
Bypass stack based locking: Use -XX:+UseHeavyMonitors
Get/Write The
Appropriate Tools …
©2017 CodeKaram40
Oracle Developer
Studio Performance
Tool
©2017 CodeKaram41
Profiling with ‘collect’
©2017 CodeKaram42
• -j on: default for when target is Java
• -p on: default clock-profiling rate of ~100 samples/second
• -H on: heap tracing
• -t <duration>: time over which to record data
• -h <ctr_def>…: specify HW counter profiling
Observing The
Targeted
Improvements
©2017 CodeKaram43
Speed UpTargets
©2017 CodeKaram44
• PlatformEvent::unpark()
• Java monitor enter operations
• Java monitor exit operations
• Java monitor notify/notifyAll operations
Demo - Comparing
Contended Locking in
JDK 9 to JDK 8
©2017 CodeKaram45
Summarizing the
Observations
©2017 CodeKaram46
Speed UpTargets
©2017 CodeKaram47
•PlatformEvent::unpark()
• Java monitor enter operations
• Java monitor exit operations
• Java monitor notify/notifyAll operations
©2017 CodeKaram48
JDK 9 ExclusiveTime JDK8 ExclusiveTime Method Name
0.400 0.941 os::PlatformEvent::unpark()
Unpark
ImprovementTargets
©2017 CodeKaram49
• PlatformEvent::unpark()
•Java monitor enter operations
• Java monitor exit operations
• Java monitor notify/notifyAll operations
©2017 CodeKaram50
JDK 9 ExclusiveTime JDK8 ExclusiveTime Method Name
97.678 187.001 ObjectMonitor::enter(Thread*)
2.462 3.432 ObjectMonitor::EnterI(Thread*)
Monitor Enter
Speed UpTargets
©2017 CodeKaram51
• PlatformEvent::unpark()
• Java monitor enter operations
•Java monitor exit operations
• Java monitor notify/notifyAll operations
©2017 CodeKaram52
JDK 9 ExclusiveTime JDK8 ExclusiveTime Method Name
2.302 2.352 ObjectMonitor::exit(bool,…)
0.000 0.961 ObjectMonitor::ExitEpilog(Thread*
,…)
Monitor Exit
Speed UpTargets
©2017 CodeKaram53
• PlatformEvent::unpark()
• Java monitor enter operations
• Java monitor exit operations
•Java monitor notify/notifyAll operations
©2017 CodeKaram54
I’ll leave that as a take-home exercise for you all! J
Notify/ NotifyAll
Further Reading
©2017 CodeKaram55
http://openjdk.java.net/jeps/143
https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html
https://wiki.openjdk.java.net/display/HotSpot/Synchronization
https://blogs.oracle.com/dave/entry/biased_locking_in_hotspot
http://extremeperformance.expert
©2017 CodeKaram56

JFokus Java 9 contended locking performance