Writing a simple lock profiler
on the JVM
Me
● Pierre Laporte
– Java guy at Xebia France
– JVM internals since oct-2012
– Java Performance Tuning
● @pingtimeout
● p...
This talk
● Tyrion
– Lock Profiler
– Prototype
– Open Source (GPL)
– https://github.com/pingtimeout/tyrion
– Be critical !
Why a lock profiler ?
● Initial idea : GC logs parser
– for science (and fun)
● Challenged by Kirk
– Write a lock profiler...
Goals
● Create an open-source lock-profiler
● First prototype should :
– List the locks that are never accessed
– List the...
Additional goals
● Follow the GC logs approach
– Generate a log file
– Information density
– Parse & analyse it after
Big Picture
● Java Agent
– Bytecode manipulation (ASM)
– JSON output (Jackson)
● Analyser
– Console only (for now)
Implementation
● Instrument synchronized blocks/methods
– Call profiler inside critical sections
● Before the first instru...
Before/After : Synchronized blocks
synchronized(obj) {
/* Stuff */
}
synchronized(obj) {
Interceptor.enteredCriticalSectio...
Before : Synchronized blocks
ALOAD_1
MONITORENTER
/*...*/
ALOAD_1
MONITOREXIT
After : Synchronized blocks
ALOAD_1
DUP
MONITORENTER
INVOKESTATIC #3
Method fr/pingtimeout/tyrion/Interceptor
.enteredCrit...
Before/After : Synchronized
methods
synchronized void foo() {
/* Stuff */
}
synchronized void foo() {
Interceptor.enteredC...
Before : Synchronized methods
● Nothing special
– Regular methods
– Synchronization done by the JVM
● No MONITORENTER / MO...
After : Synchronized methods
// Method beginning
ALOAD_0
INVOKESTATIC #3
// Method fr/pingtimeout/tyrion/Interceptor
enter...
Before/After : Static methods
static synchronized void bar() {
/* Stuff */
}
static synchronized void bar() {
Class<?> cla...
Before : Static methods
● Nothing special
– Regular methods
– Synchronization done by the JVM
● No MONITORENTER / MONITORE...
After : Static methods
LDC #4 // Current class name known by ASM
INVOKESTATIC #2 // Method java.lang.Class.forName:
(Ljava...
After : Static methods
LDC #4
● // Current class name known by ASM
INVOKESTATIC #2
● // Method java.lang.Class.forName:
(L...
Drawbacks
● Only time spent inside critical section
– No "waiting on monitor" time
– Simplest (fastest) solution
● Infer "...
Demo
● Hello World
● Cassandra Startup
Fun stuff
● Store events in a shared data structure
Map<Thread, Set<Event>> accesses;
● Infinite interception loop
synchro...
Roadmap
● Build a fancy GUI
– JavaFX ? Swing+JFreecharts ? … ?
● Use System.nanoTime()
● Densify output
● … ?
Thanks !
● Code on Github :
https://github.com/pingtimeout/tyrion
● Slides posted online
http://fr.slideshare.net/PierreLa...
Upcoming SlideShare
Loading in …5
×

Building a lock profiler on the JVM

633 views
521 views

Published on

This presentation is a feedback of how I wrote a simple lock profiler on the JVM, including architectural overview, limits of the current approach and roadmap for future versions

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
633
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
10
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Building a lock profiler on the JVM

  1. 1. Writing a simple lock profiler on the JVM
  2. 2. Me ● Pierre Laporte – Java guy at Xebia France – JVM internals since oct-2012 – Java Performance Tuning ● @pingtimeout ● pierre@pingtimeout.fr ● http://www.pingtimeout.fr/
  3. 3. This talk ● Tyrion – Lock Profiler – Prototype – Open Source (GPL) – https://github.com/pingtimeout/tyrion – Be critical !
  4. 4. Why a lock profiler ? ● Initial idea : GC logs parser – for science (and fun) ● Challenged by Kirk – Write a lock profiler – Harder, even more fun ● My skills ? – No idea of bytecode manipulation – ASM means “The Assembly language”, right ?
  5. 5. Goals ● Create an open-source lock-profiler ● First prototype should : – List the locks that are never accessed – List the locks accessed by only one thread – List the locks accessed by more than one thread
  6. 6. Additional goals ● Follow the GC logs approach – Generate a log file – Information density – Parse & analyse it after
  7. 7. Big Picture ● Java Agent – Bytecode manipulation (ASM) – JSON output (Jackson) ● Analyser – Console only (for now)
  8. 8. Implementation ● Instrument synchronized blocks/methods – Call profiler inside critical sections ● Before the first instruction (record entry) ● After the last instruction (record exit) – Store events per thread – Events collection and dump ● Asynchronously ● Dedicated thread
  9. 9. Before/After : Synchronized blocks synchronized(obj) { /* Stuff */ } synchronized(obj) { Interceptor.enteredCriticalSection(obj); /* Stuff */ Interceptor.leavingCriticalSection(obj); }
  10. 10. Before : Synchronized blocks ALOAD_1 MONITORENTER /*...*/ ALOAD_1 MONITOREXIT
  11. 11. After : Synchronized blocks ALOAD_1 DUP MONITORENTER INVOKESTATIC #3 Method fr/pingtimeout/tyrion/Interceptor .enteredCriticalSection:(Ljava/lang/Object;)V /*...*/ ALOAD_1 DUP INVOKESTATIC #4 Method fr/pingtimeout/tyrion/Interceptor .leavingCriticalSection (Ljava/lang/Object;)V MONITOREXIT
  12. 12. Before/After : Synchronized methods synchronized void foo() { /* Stuff */ } synchronized void foo() { Interceptor.enteredCriticalSection(this); /* Stuff */ Interceptor.leavingCriticalSection(this); }
  13. 13. Before : Synchronized methods ● Nothing special – Regular methods – Synchronization done by the JVM ● No MONITORENTER / MONITOREXIT – Cannot intercept lock before it is held
  14. 14. After : Synchronized methods // Method beginning ALOAD_0 INVOKESTATIC #3 // Method fr/pingtimeout/tyrion/Interceptor enteredCriticalSection:(Ljava/lang/Object;)V /*...*/ ALOAD_0 INVOKESTATIC #4 // Method fr/pingtimeout/tyrion/Interceptor leavingCriticalSection (Ljava/lang/Object;)V // Method end
  15. 15. Before/After : Static methods static synchronized void bar() { /* Stuff */ } static synchronized void bar() { Class<?> clazz = Class.forName("FQN"); Interceptor.enteredCriticalSection(clazz); /* Stuff */ Interceptor.leavingCriticalSection(clazz); }
  16. 16. Before : Static methods ● Nothing special – Regular methods – Synchronization done by the JVM ● No MONITORENTER / MONITOREXIT – Cannot intercept lock before it is held
  17. 17. After : Static methods LDC #4 // Current class name known by ASM INVOKESTATIC #2 // Method java.lang.Class.forName: (Ljava/lang/String;)Ljava/lang/Class; INVOKESTATIC #3 // Method fr/pingtimeout/tyrion/Interceptor enteredCriticalSection:(Ljava/lang/Object;)V /*...*/ LDC #4 // Current class name known by ASM INVOKESTATIC #2 // Method java.lang.Class.forName: (Ljava/lang/String;)Ljava/lang/Class; INVOKESTATIC #4 // Method fr/pingtimeout/tyrion/Interceptor leavingCriticalSection (Ljava/lang/Object;)V
  18. 18. After : Static methods LDC #4 ● // Current class name known by ASM INVOKESTATIC #2 ● // Method java.lang.Class.forName: (Ljava/lang/String;)Ljava/lang/Class; INVOKESTATIC #3 ● // Method fr/pingtimeout/tyrion/Interceptor enteredCriticalSection:(Ljava/lang/Object;)V
  19. 19. Drawbacks ● Only time spent inside critical section – No "waiting on monitor" time – Simplest (fastest) solution ● Infer "likely contended" locks – Locks accessed by several threads – Within millis/micros/nanos ?
  20. 20. Demo ● Hello World ● Cassandra Startup
  21. 21. Fun stuff ● Store events in a shared data structure Map<Thread, Set<Event>> accesses; ● Infinite interception loop synchronized(accesses) {…} ● Has to be lock-free ! – Map<Thread, AtomicReference<Set<Event>>> ● Immutable ConsList<Event> – CAS possible
  22. 22. Roadmap ● Build a fancy GUI – JavaFX ? Swing+JFreecharts ? … ? ● Use System.nanoTime() ● Densify output ● … ?
  23. 23. Thanks ! ● Code on Github : https://github.com/pingtimeout/tyrion ● Slides posted online http://fr.slideshare.net/PierreLaporte ● Any questions ?

×