Date: Fri, 19 Nov 2004 09:19:36 -0000 From: "Niall Douglas" <s_sourceforge@nedprod.com> To: Petri Helenius <pete@he.iki.fi> Cc: freebsd-threads@freebsd.org Subject: Re: mutex performance Message-ID: <419DBAA8.29867.1D449929@localhost> In-Reply-To: <419DA9B9.50105@he.iki.fi> References: <419DA7EC.20473.1CFB6AE9@localhost>
next in thread | previous in thread | raw e-mail | index | archive | help
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 19 Nov 2004 at 10:07, Petri Helenius wrote: > >This is on a dual Athon 1700 (1.43Ghz), so that's 77.76 cycles per > >lock/unlock with SMP build and 52.28 cycles on non-SMP build. The > >difference between SMP and non-SMP is that the former uses the lock > >prefix on the x86 instructions. > > > >So yes, I think there is some scope for improvement. > > > Are these mutexes spinlocks or "real" locks which make the thread > actually yield if they have to wait longer? Or should I RTFS? :-) You would have had to read the source actually. I wrote them as I was very unhappy with the performance of the locks supplied by Win2k which are far slower than the same code on WinNT 4.0. They're also far faster than most pthread lock implementations. Here's the schema: 1. Is lock free? If so take possession and exit 2. Is lock already held by this thread? If so, increment refcount and exit 3. If on multiprocessor, examine lock's freeness X times (default 4000). If still not free, ask kernel to suspend me. If on uniprocessor or non-SMP build, ask kernel to switch immediately to next thread. The key to the high performance is the use of "lock inc" - a lot of the time we don't care about what the value is, just that it is or isn't zero. By using inc and dec instead of xadd I gained quite a few percent if I remember plus it's a lot faster without the lock prefix. Of course, I have no idea what performance would be like on Intel chips. It's likely that the many branches would penalise code though I have used the pause x86 instruction where possible. Cheers, Niall -----BEGIN PGP SIGNATURE----- Version: idw's PGP-Frontend 4.9.6.1 / 9-2003 + PGP 8.0.2 iQA/AwUBQZ26qMEcvDLFGKbPEQIpGQCg124mjqWElk4cP/LpNtRUgEMO7a8An0ye M/Ku1yACotLlZDyvpPdWI1cC =/p1K -----END PGP SIGNATURE-----
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?419DBAA8.29867.1D449929>