Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have not used AspectJ so cannot vouch for how easy it is to use. I have used <a href="http://asm.ow2.org" rel="nofollow">ASM</a> to create a custom code profiler, this was about 2 days work. The effort to instrument synchronization should be similar. AspectJ should be quicker and easier once you are up to speed with aspects.</p> <p>I have implemented deadlock detecting trace for our c++ based server. Here is how I did it:</p> <ul> <li>When ever acquiring or releasing a lock I traced: <ul> <li><code>&lt;time&gt; &lt;tid&gt; &lt;lockid&gt; &lt;acquiring|releasing&gt; &lt;location in code&gt;</code></li> </ul></li> <li>This extra trace affected performance quite drastically and was not usable in production.</li> <li>So when a possible deadlock was discovered in production I used the log file to figure out what was happening around the deadlock. Then reproduced this functionality in a test environment with my tracing turned on.</li> <li>Then I ran a script on the log file to see if deadlock was possible and how. I used an awk script, using this algoritm: <ul> <li>Foreach line <ul> <li>if acquiring <ul> <li>add lockid to list of current locks for this thread</li> <li>add each pair of locks in this list to a set lock pairs for this thread. eg for list of <code>Lock A -&gt; Lock B -&gt; Lock C</code> generate the pairs <code>(Lock A, Lock B), (Lock A, Lock C), (Lock B, Lock C)</code></li> </ul></li> <li>if releasing <ul> <li>remove current lockid from tail of list for this thread</li> </ul></li> </ul></li> <li>For each lock pair search all other threads for the reverse lock pairs, each match is a potential deadlock so print the pairs and threads affected</li> <li>Instead of making the algorithm smarter I then desk checked that the lock acquisition to see if it was a real deadlock.</li> </ul></li> </ul> <p>I did this after failing to find the cause of a deadlock for a number of days, it took a few more days to implement and a few hours to find the deadlock.</p> <p>If you are considering this approach in Java things to consider are:</p> <ul> <li>Do you only use <code>synchronized</code> to protect your critical sections? Are you using the classes in java.lang.concurrent? (these might require special handling/instrumentation)</li> <li>How easy is it to print the code location with aspects/ASM? I used <code>__FILE__</code> and <code>__LINE__</code> in c++. ASM will give you the class name, method name and signature.</li> <li>You cannot instrument the locks used to protect your tracing/logging.</li> <li>You can streamline your instrumentation if you use a log file per thread and thread local storage for the file object.</li> <li>How do you uniquely identify objects you synchronize on? Maybe toString() and System.identityHashCode() would be enough, but might require more. I used the address of the object in C++.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload