Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure thread contention on particular monitor

I'm considering switching from synchronized to a ReadWriteLock. Before doing it, I would like to check if it's worth it.

ThreadMXBean and ThreadInfo provide information about overall thread blocked count and time. Those blocks can be caused by multiple monitors. Is there a way to measure block statistics given a particular monitor object?

like image 813
Konstantin Milyutin Avatar asked Feb 06 '23 15:02

Konstantin Milyutin


1 Answers

Yes, it is possible using JVMTI.

What is you need is to write a native agent that handles a pair of events:

  • MonitorContendedEnter - fired when a thread is about to enter syncrhonized block already acquired by another thread;
  • MonitorContendedEntered - fired when a thread successfully acquires monitor after waiting.

Both events accept jthread and jobject arguments that correspond to a thread acquiring the monitor and the monitor object itself.


Here is a sample code for the contention profiler agent.

like image 122
apangin Avatar answered Feb 19 '23 08:02

apangin