<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=windows-1252"
 http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
OK, Some info to back this up, please see inline.<br>
<br>
On 12/15/2010 01:20 PM, Thomas Hellstrom wrote:
<blockquote cite="mid:4D08B281.5040602@vmware.com" type="cite">
  <meta content="text/html; charset=windows-1252"
 http-equiv="Content-Type">
On 12/15/2010 09:23 AM, Marek Olšák wrote:
  <blockquote
 cite="mid:AANLkTi=3ZFPZyR79ARY5+zVUDHaAc4VNLv08RODtBeNA@mail.gmail.com"
 type="cite">
    <meta http-equiv="Content-Type"
 content="text/html; charset=windows-1252">
    <div class="gmail_quote">On Tue, Dec 14, 2010 at 8:10 PM, Thomas
Hellstrom <span dir="ltr">&lt;<a moz-do-not-send="true"
 href="mailto:thellstrom@vmware.com" target="_blank">thellstrom@vmware.com</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;">Hmm,<br>
      <br>
for the uninformed, where do we need to use spinlocks in gallium and
how do<br>
we avoid using them on an UP system?<br>
    </blockquote>
    <div><br>
I plan to use spinlocks to guard very simple code like the macro
remove_from_list, which might be, under some circumstances, called too
often. Entering and leaving a mutex is quite visible in callgrind.<br>
    <br>
What does UP stand for?<br>
    <br>
Marek<br>
    </div>
    </div>
  </blockquote>
  <br>
I've also noted that mutexes show up high on profiling on a number of
occasions, but<br>
  <br>
Linux mutexes should theoretically be roughly as fast as spinlocks
unless there is high contention. (They shouldn't call into the kernel
unless there is contention). If this is not the case it is worth
figuring out why.<br>
</blockquote>
<br>
At a first glance, looking at this article,<br>
<br>
<a class="moz-txt-link-freetext" href="http://www.alexonlinux.com/pthread-mutex-vs-pthread-spinlock">http://www.alexonlinux.com/pthread-mutex-vs-pthread-spinlock</a><br>
<br>
spinlocks should be superior for things like quick list manipulation,
but If I try that code with the second thread disabled (thus avoiding
lock contention), it turns out that in the uncontended case, spinlocks
are only some 13% faster.<br>
<br>
<blockquote cite="mid:4D08B281.5040602@vmware.com" type="cite"><br>
The problem with spinlocks in user-space is that a context switch may
occur in a locked section and that will have other processes trying to
lock spinning until the process holding the lock is scheduled again.<br>
  <br>
On uni-processor (UP) systems spinlocks are particularly bad, since if
there is contention on a lock, the process will always continue to spin
until a timeout context switch, and thus behave much worse than a
mutex  which will context switch immediately. <br>
(This is of course unless pthread spinlocks does some magic I'm not
aware of)<br>
</blockquote>
<br>
<br>
Trying the code with two threads on a uni-processor desktop system,
spinlocks are some 63% slower than mutexes. This is with a kernel tick
of 1000HZ. Probably spinlock stats  would be much worse with a slower
tick.<br>
<br>
<blockquote cite="mid:4D08B281.5040602@vmware.com" type="cite"><br>
In kernel space the situation is different, since spinlocks block
preemption and contention can thus never occur on uniprocessor systems.<br>
  <br>
There are of course situations where user-space spinlocks are faster
(high contention and low chance of context switch), but that really
means the user needs to know exactly what he's doing and the drawbacks
of using spinlocks.<br>
  <br>
I don't really think we should add spinlocks to gallium without<br>
  <br>
a) Figuring out why, in your case, mutexes behave so much worse than
spinlocks. Do you expect high lock contention?<br>
b) adding some kind of warning about the drawbacks of using them.<br>
</blockquote>
<br>
<br>
Finally, trying to repeat the author's findings on my dual-processor
system, Spinlocks are faster (but not nearly as much as the author
states, but that's probably because my CPUs are faster hence lower lock
contention), but the cpu-usage is roughly the same as in the mutex case.<br>
<br>
Given this, I would advise strongly against building spinlocks into any
code that might be run on a uni-processor system.  Particularly gallium
utility code. <br>
If we want to get rid of unnecessary locking overhead we should
probably fix the code up to avoid taking the locks when not strictly
needed. <br>
<br>
/Thomas<br>
<br>
<blockquote cite="mid:4D08B281.5040602@vmware.com" type="cite"><br>
/Thomas<br>
  <br>
</blockquote>
<br>
</body>
</html>