freebsd-dev/lib/libthr
Greg Becker b370ef156a libthr: Patch to reduce latency to acquire+release a pthread mutex.
The acquisition and release of an uncontended default/normal pthread
mutex on FreeBSD is suprisingly slow, e.g., pthread wrlocks and binary
semaphores both exhibit roughly 33% lower latency, while default/normal
mutexes on Linux exhibit roughly 67% lower latency than FreeBSD. This is
likely explained by the fact that AFAICT in the best case to acquire an
uncontended mutex on Linux one need touch only 1 page and read+modify
only 1 cacheline, whereas on FreeBSD we need to touch at least 4 pages,
read 6 cachelines, and modify at least 4 cachelines.

This patch does not address the pthread mutex architecture. Instead,
it improves performance by adding the __always_inline attribute to
mutex_lock_common() and mutex_unlock_common() to encourage constant
folding and propagation, thereby lowering the latency to acquire and
release a mutex due to a shorter code path with fewer compares, jumps,
and mispredicts.

With this patch on a stock build I see a reduction in latency of roughly
7% for default/normal mutexes, and 17% for robust mutexes. When built
without PTHREADS_ASSERTIONS enabled I see a reduction in latency of
roughly 15% and 26%, respectively. Suprisingly, I see similar reductions
in latency for heavily contended mutexes.

By default, this patch increases the size of libthr.so.3 by 2448 bytes,
but when built without PTHREAD_ASSERTIONS enabled it only increases by
448 bytes.

Reviewed by:	jhb (previous version), kib
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D40912
2023-07-08 09:00:12 +03:00
..
arch spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD 2023-05-12 10:44:03 -06:00
sys spdx: initial adoption of licensing ID tags. 2017-11-18 14:26:50 +00:00
tests _umtx_op: fix a compat32 bug in UMTX_OP_NWAKE_PRIVATE 2020-11-17 03:34:01 +00:00
thread libthr: Patch to reduce latency to acquire+release a pthread mutex. 2023-07-08 09:00:12 +03:00
libthr.3 umtx: allow to configure minimal timeout (in nanoseconds) 2023-04-19 02:22:28 +03:00
Makefile libthr: Add src.conf variable WITHOUT_PTHREADS_ASSERTIONS 2023-07-07 04:45:09 +03:00
Makefile.depend Update/fix Makefile.depend for userland 2023-04-18 17:14:23 -07:00
plockstat.d
pthread.map rtld: workaround for broken ABI 2021-04-10 17:33:33 +03:00