![Page 1: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/1.jpg)
Efficient Tracing and Versatile AnalysisEfficient Tracing and Versatile Analysisof Lock Contention in Java Applicationsof Lock Contention in Java Applications
on the Virtual Machine Levelon the Virtual Machine Level
Peter HoferPeter HoferDavid GnedtDavid Gnedt
Andreas SchörgenhumerAndreas SchörgenhumerHanspeter MössenböckHanspeter Mössenböck
16 March 201616 March 2016
![Page 2: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/2.jpg)
The Need for Analyzing Lock Contention
T1
T2
T3
T4
t
final Segment<K,V>[] segments = this.segments;int size;boolean overflow; // true if size overflows 32 bitslong sum; // sum of modCountslong last = 0L; // previous sumint retries = -1; // first iteration isn't retrytry { for (;;) { if (retries++ == RETRIES_BEFORE_LOCK) { for (int j = 0; j < segments.length; ++j) ensureSegment(j).lock(); // force creation } sum = 0L; size = 0; overflow = false; for (int j = 0; j < segments.length; ++j) { Segment<K,V> seg = segmentAt(segments, j); if (seg != null) { sum += seg.modCount; int c = seg.count; if (c < 0 || (size += c) < 0) overflow = true; } } if (sum == last) break; last = sum; }} finally { if (retries > RETRIES_BEFORE_LOCK) { for (int j = 0; j < segments.length; ++j) segmentAt(segments, j).unlock(); }}return overflow ? Integer.MAX_VALUE : size;
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException{ synchronized (getClassLoadingLock(name)) { // First, check if the class has already been loaded Class<?> c = findLoadedClass(name); if (c == null) { long t0 = System.nanoTime(); try { if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader }
if (c == null) { // If still not found, then invoke findClass in order // to find the class. long t1 = System.nanoTime(); c = findClass(name);
// this is the defining class loader; record the stats sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); } } if (resolve) { resolveClass(c); } return c; }}
/** * Removes all of the mappings from this map. */public void clear() { long delta = 0L; // negative number of deletions int i = 0; Node<K,V>[] tab = table; while (tab != null && i < tab.length) { int fh; Node<K,V> f = tabAt(tab, i); if (f == null) ++i; else if ((fh = f.hash) == MOVED) { tab = helpTransfer(tab, f); i = 0; // restart } else { synchronized (f) { if (tabAt(tab, i) == f) { Node<K,V> p = (fh >= 0 ? f : (f instanceof TreeBin) ? ((TreeBin<K,V>)f).first : null); while (p != null) { --delta; p = p.next; } setTabAt(tab, i++, null); } } } } if (delta != 0L) addCount(delta, -1);}
/** * Removes all of the mappings from this map. */public void clear() { long delta = 0L; // negative number of deletions int i = 0; Node<K,V>[] tab = table; while (tab != null && i < tab.length) { int fh; Node<K,V> f = tabAt(tab, i); if (f == null) ++i; else if ((fh = f.hash) == MOVED) { tab = helpTransfer(tab, f); i = 0; // restart } else { synchronized (f) { if (tabAt(tab, i) == f) { Node<K,V> p = (fh >= 0 ? f : (f instanceof TreeBin) ? ((TreeBin<K,V>)f).first : null); while (p != null) { --delta; p = p.next; } setTabAt(tab, i++, null); } } } } if (delta != 0L) addCount(delta, -1);}
Images: Intel Haswell Chip, Intel Free Press, licensed under Creative Commons BY-SA 2.0 / System Lock, Yuri Samoilov, CC BY 2.0
![Page 3: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/3.jpg)
3
Java Intrinsic Object Locksclass SynchronizedArrayList { ArrayList list = new ArrayList();
void get(int index) { synchronized(list) { return list.get(index); } }
void add(int index, Object obj) { synchronized(list) { return list.add(index, obj); } }}
![Page 4: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/4.jpg)
4
java.util.concurrent Locksclass SynchronizedArrayList { ArrayList list = new ArrayList(); final ReentrantLock lock = new ReentrantLock();
void get(int index) { lock.lock(); try { return list.get(index); } finally { lock.unlock(); } }
void add(int index, Object obj) { lock.lock(); try { return list.add(index, obj); } finally { lock.unlock(); } }}
![Page 5: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/5.jpg)
Java Synchronization
Locking is cheap: fast compare-and-set operations
Contention is expensive
no progress in blocked threads
threads must coordinate
T2
T1
t
Images: KDE Oxygen Icon Project, licensed under the LGPL
![Page 6: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/6.jpg)
6
Blame
get() add()
get() add()
Time spent waiting at...
Time spent waiting for...
![Page 7: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/7.jpg)
7
Tracing Monitors in the HotSpot Java VM
t
T1
Contended Exit
T3
Contended Exit
T2
Contended Enter Entered
![Page 8: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/8.jpg)
8
Metadata
Metadata recorded in special events
Call chains
execution state – no identity
many identical call chains
89?
89
Call Chain Metadata Eventid: 89frames: ...
Contended Exit Eventcall chain: 89
...lock-free!
![Page 9: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/9.jpg)
9
Trace Analysis
Trace Parser
EEE
Metadata Resolver
MD
Event Rearranger
EEE
EEE
IL1IL2
41 594
IL Dispatcher
E
E
E
IL1 Analyzer
ILn Analyzer
EE
EE
CC
Aggregation
CC
java.util.concurrent Analysis
![Page 10: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/10.jpg)
10
0%
100%
110%
120%
130%
140%
150%
160%
170%
180%
190%
No Tracing Compressed Output Online Analysis
Overhead
multi-threaded single-threaded
DaCapo and scalabench benchmarks (except batik and eclipse)2x Xeon E5-2670v2 = 20 cores + HT, Oracle Linux 7, OpenJDK 8u45-b14
![Page 11: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/11.jpg)
11
0 B/s
5 MB/s
10 MB/s
15 MB/s
20 MB/s
25 MB/s
30 MB/s
actorsapparat
avrorah2 jython
luindex
lusearch
pmdscalac
scaladoc
scalatest
sunflow
tmttomcat
tradebeans
tradesoap
xalan
Uncompressed Compressed
Bytes per Second DaCapo and scalabench benchmarks (except batik and eclipse)2x Xeon E5-2670v2 = 20 cores + HT, Oracle Linux 7, OpenJDK 8u45-b14
multi-threaded
![Page 12: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/12.jpg)
12
Demonstration
![Page 13: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/13.jpg)
13
![Page 14: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/14.jpg)
14
![Page 15: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/15.jpg)
15
![Page 16: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/16.jpg)
16
![Page 17: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/17.jpg)
17
![Page 18: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/18.jpg)
18
![Page 19: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/19.jpg)
19
![Page 20: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/20.jpg)
20
![Page 21: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/21.jpg)
21
![Page 22: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/22.jpg)
22
![Page 23: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/23.jpg)
23
![Page 24: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/24.jpg)
24
![Page 25: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/25.jpg)
25
![Page 26: Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications … · 2016. 3. 29. · Efficient Tracing and Versatile Analysis of Lock Contention in Java Applications](https://reader033.vdocument.in/reader033/viewer/2022060900/609df167fa8e2433a64362db/html5/thumbnails/26.jpg)
26
Read our paper
for more details on tracing and analysis,especially java.util.concurrent
Download our JDK and visualization tool:
http://mevss.jku.at/ → Tools → Lock Contention Tracing
Ask questions!