adaptive mutexes, was Re: btrfs_tree_lock & trylock



On Mon, Sep 08, 2008 at 08:07:51AM -0700, Stephen Hemminger wrote:
The problem is that they are a nuisance. It is impossible to choose
the right trade off between spin an no-spin, also they optimize for
a case that doesn't occur often enough to be justified.

People seem to repeatedly come up with adaptive mutex based on intuitive
hunch, and never do much analysis before or afterwards.

You need some facts to come up with a useful model:
% of time lock is contended
average lock hold time
overhead of entry-exit for lock primitive (spin time)
overhead of the adaptive version either pure spin or pure mutex

Also, adaptive locks have even worse unfairness than spin locks under
contention.

Well, the traditional wisdom in kernel land is that you want a spinlock
if the contention phases are short. But we grow more an more places
where we might do sleep under the lock. One optimization would be
to spin, but only if the mutex holder is not sleeping. Or we make the
spinning a completely different API, mutex_lock_adaptive()

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • Re: Critical Sections, Thread Priorities, and Pre-emption
    ... If you set the spin count to zero, then you get the same benefit of a Mutex ... where there is no contention, then you avoid the UM-KM transitions. ... a single app, or working with a broken sync design, that can't easily be ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Locking etc. (Long, boring, redundant, newbie questions)
    ... What is really meant is that depending on the type of mutex a thread is trying to acquire, the thread will either spin or it will sleep waiting for the lock to become available. ...
    (freebsd-hackers)
  • Re: An idea of remove MUTEX_WAKE_ALL
    ... >> This makes a mutex always be owned by a thread when there are threads ... >> term mutex more expensive(maybe should use spin mutex instead). ... > they will acquire the lock in sequence and the lock acquires and releaes will ...
    (freebsd-hackers)
  • Re: Lockless 63-bit Counter
    ... It would be much easier if the spin ... >> Obviously the xadds need the lock prefix. ... For memory where there is no ...
    (comp.lang.asm.x86)
  • Re: Spin lock + mutex in pthreads.
    ... When you give it a non-zero spin count, when you try to acquire the ... EnterCriticalSection first spins a few times ... Is there an equivalent construct in pthreads? ... "when a thread attempts to acquire a lock, and the lock is being held, the kernel examines the state of the thread that holds the lock. ...
    (comp.programming.threads)