Hello all,<br><br>While I have not tested any of the scenario&#39;s described above, I think the remarks from Ralph make a lot of sense.<br>And I am also interested in knowing the impact on throughput of using fair locks.<br>
<br>When the fairness of non-fair locks has changed so drastically with 1.6, I would guess that more people would suffer from it, no ?<br>Logback is certainly not the only code using non-fair locks.<br>I googled a bit about &quot;java 1.6 starvation&quot; but nothing really alarming came up.<br>
<br>regards,<br>Maarten<br><br><div class="gmail_quote">On Thu, Jun 4, 2009 at 7:12 PM, Joern Huxhorn (JIRA) <span dir="ltr">&lt;<a href="mailto:noreply-jira@qos.ch">noreply-jira@qos.ch</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
    [ <a href="http://jira.qos.ch/browse/LBCORE-97?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&amp;focusedCommentId=11139#action_11139" target="_blank">http://jira.qos.ch/browse/LBCORE-97?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&amp;focusedCommentId=11139#action_11139</a> ]<br>

<div class="im"><br>
Joern Huxhorn commented on LBCORE-97:<br>
-------------------------------------<br>
<br>
</div>Have you done any benchmarking concerning the performance impact of the fair ReentrantLock?<br>
<br>
I guess you have logback benchmarks at your fingertips, I&#39;d be quite interested in the numbers.<br>
<br>
Concerning the &quot;bug in the Sun JDK&quot; part: It&#39;s the same way in case of the Mac version of Java 6.<br>
I highly doubt that Sun will come to it&#39;s senses ;) and I&#39;m not even sure that it can be validly called a bug since it doesn&#39;t breach any documented behavior. This might really be a valid performance enhancement because switching threads means degrading performance.<br>

<br>
I&#39;d be really grateful if this patch could be applied before the next release. We are suffering quite bad because of it.<br>
In our case it&#39;s - as mentioned - a Solaris and we can&#39;t just switch the VM or the OS as we like because of customer rules.<br>
<div><div></div><div class="h5"><br>
&gt; Starvation on AppenderBase.doAppend<br>
&gt; -----------------------------------<br>
&gt;<br>
&gt;                 Key: LBCORE-97<br>
&gt;                 URL: <a href="http://jira.qos.ch/browse/LBCORE-97" target="_blank">http://jira.qos.ch/browse/LBCORE-97</a><br>
&gt;             Project: logback-core<br>
&gt;          Issue Type: Bug<br>
&gt;          Components: Appender<br>
&gt;    Affects Versions: 0.9.15<br>
&gt;            Reporter: Joern Huxhorn<br>
&gt;            Assignee: Logback dev list<br>
&gt;            Priority: Critical<br>
&gt;         Attachments: AppenderBaseLock2.patch, SynchronizedVsFairLock.java<br>
&gt;<br>
&gt;<br>
&gt; The problem we are facing here is that several threads are trying to obtain the object monitor of the exact same resource, namely the Appender.<br>
&gt; The assumption that multiple threads waiting for ownership of a monitor would receive the monitor in the order that they tried to acquire it is simply incorrect. This is documented behavior.<br>
&gt; See the last paragraph of [1]:<br>
&gt; &quot;Likewise, no assumptions should be made about the order in which threads are granted ownership of a monitor or the order in which threads wake in response to the notify or notifyAll method. An excellent reference for these topics is Chapter 9, &quot;Threads,&quot; in Joshua Bloch&#39;s book Effective Java Programming Language Guide. &quot;<br>

&gt; The documentation of Object.notifyAll() [2] states the following:<br>
&gt; &quot;[..] The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.&quot;<br>

&gt; The documentation in [5] also lists the following as a weak-spot of Built-in Synchronization in J2SE 1.4.x:<br>
&gt; &quot;No way to alter the semantics of a lock, for example, with respect to reentrancy, read versus write protection, or fairness.&quot;<br>
&gt; In LBCORE-96 Ceki stated the following:<br>
&gt; &quot;Logback is using the standard synchronization mechanism found in the JDK. You are saying that that mechanism is unsuitable which, truth be told, sounds quite suspicious.&quot;<br>
&gt; Yes, it&#39;s unsuitable in this situation because obtaining the object monitor is not guaranteed to be fair. It&#39;s not working in a &quot;first come, first serve&quot; manner. I assumed that, too, but it isn&#39;t the case. I had the exact same problem in Lilith some time ago because I made exactly this very same wrong assumption.<br>

&gt; Fairness of object monitor lock acquisition seems to be logical and a &quot;good thing&quot; but it&#39;s not specified that way, and for good reasons.<br>
&gt; Without fairness in place, the VM can optimize the execution of an application much better. A context switch is a costly operation for a CPU so performance is increased significantly if such a switch can be omitted.<br>

&gt; Concerning a test-case, this would be pretty hardcore to implement since it&#39;s 100% implementation dependent. One implementation *may* handle the locking of object monitors fairly while others don&#39;t.<br>
&gt; Therefore I&#39;ll try the following first:<br>
&gt; I assume I could convince you that object monitor acquisition (OMA) is not fair.<br>
&gt; If we take that for granted the following scenario should show my point:<br>
&gt; There are 4 Threads (e.g. 4 Threads handling concurrent webapp-requests) that have one single chokepoint, the appender.<br>
&gt; Since OMA isn&#39;t fair, it&#39;s possible that only some of those threads can work at all (see [3]). Exactly that is happening right now in our webapp running on a 4-core Solaris on JDK6.<br>
&gt; My next assumption is that we both agree that this isn&#39;t acceptable behavior.<br>
&gt; Logging should be handled &quot;first come, first served&quot; so that if Thread A is waiting to append an event and Thread B is waiting to append an event subsequently, the actual appending order should be A, then B.<br>

&gt; This is currently not the case. It *may* be the case but there is no guarantee of it.<br>
&gt; One could even argue that the logging system is working incorrectly (aside from the starvation problem) because events are appended in a different order than the actual execution of the logger call.<br>
&gt; The only way to prevent this is the introduction of fairness into the locking process. The way to do this is to use ReentrantLock [4].<br>
&gt; From the ReentrantLock javadoc:<br>
&gt; &quot;The constructor for this class accepts an optional fairness parameter. When set true, under contention, locks favor granting access to the longest-waiting thread. Otherwise this lock does not guarantee any particular access order. Programs using fair locks accessed by many threads may display lower overall throughput (i.e., are slower; often much slower) than those using the default setting, but have smaller variances in times to obtain locks and guarantee lack of starvation.&quot;<br>

&gt; This is exactly what is absolutely necessary for a logging framework.<br>
&gt; Logging must *NOT* be able to introduce a starvation problem into a multi-threaded application! And it does exactly that right now.<br>
&gt; I doubt that the performance will decrease in a significant manner due to the use of a fair ReentrantLock but even if this would be the case it would be necessary anyway.<br>
&gt; Otherwise, appending simply isn&#39;t working correctly.<br>
&gt; [1] <a href="http://java.sun.com/j2se/1.5.0/docs/guide/vm/thread-priorities.html" target="_blank">http://java.sun.com/j2se/1.5.0/docs/guide/vm/thread-priorities.html</a><br>
&gt; [2] <a href="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#notifyAll%28%29" target="_blank">http://java.sun.com/javase/6/docs/api/java/lang/Object.html#notifyAll()</a><br>
&gt; [3] <a href="http://en.wikipedia.org/wiki/Starvation_%28computing%29" target="_blank">http://en.wikipedia.org/wiki/Starvation_(computing)</a><br>
&gt; [4] <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantLock.html" target="_blank">http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantLock.html</a><br>
&gt; [5] <a href="http://java.sun.com/developer/technicalArticles/J2SE/concurrency/" target="_blank">http://java.sun.com/developer/technicalArticles/J2SE/concurrency/</a><br>
<br>
--<br>
This message is automatically generated by JIRA.<br>
-<br>
If you think it was sent incorrectly contact one of the administrators: <a href="http://jira.qos.ch/secure/Administrators.jspa" target="_blank">http://jira.qos.ch/secure/Administrators.jspa</a><br>
-<br>
For more information on JIRA, see: <a href="http://www.atlassian.com/software/jira" target="_blank">http://www.atlassian.com/software/jira</a><br>
<br>
<br>
_______________________________________________<br>
logback-dev mailing list<br>
<a href="mailto:logback-dev@qos.ch">logback-dev@qos.ch</a><br>
<a href="http://qos.ch/mailman/listinfo/logback-dev" target="_blank">http://qos.ch/mailman/listinfo/logback-dev</a><br>
</div></div></blockquote></div><br>