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
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. This talk
● Tyrion
– Lock Profiler
– Prototype
– Open Source (GPL)
– https://github.com/pingtimeout/tyrion
– Be critical !
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. 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. Additional goals
● Follow the GC logs approach
– Generate a log file
– Information density
– Parse & analyse it after
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
13. Before : Synchronized methods
● Nothing special
– Regular methods
– Synchronization done by the JVM
● No MONITORENTER / MONITOREXIT
– Cannot intercept lock before it is held
16. Before : Static methods
● Nothing special
– Regular methods
– Synchronization done by the JVM
● No MONITORENTER / MONITOREXIT
– Cannot intercept lock before it is held
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. 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. 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 ?
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