From nobody Sat Jul 8 06:09:33 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QyfxB06z2z4ltDG; Sat, 8 Jul 2023 06:09:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Qyfx95sMhz3Qsq; Sat, 8 Jul 2023 06:09:33 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688796573; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AxGsh4ywucaQT69n8gLP23WC++T0ZG5HBfxBEt2OYAU=; b=oqZFqHZqUIbnh2PdPJl0fhF1vN8vUAVRQKSckVy7S/kjzdqfBNK/Jm9Pl2hXJzV4fUHVZ2 xK7trYQkgANSYRLgZd6GmEl9mRVYl/xOxIg74/mAcGZGgNg3ZF1BJbocP/QBioG/BQrHYN dOR3YeGeK80+Fq9aDDjF8H+SXf2br5lbll/0s5a4e7IRnOiphCofCxGigYjI2UojCvnKKY poIlwgBOHSyecDAWroOzIC3e5LUBjs6Tpwc/yH5DzFkUzAb4uF1ThZ8thieAzo7b67Ccg8 hg0Nk4NyNbikKCm5fFnEkfSIR0Rr9BDlEIWbZYAYmFy9b/Goz5xAIdYfm7J2Lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688796573; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AxGsh4ywucaQT69n8gLP23WC++T0ZG5HBfxBEt2OYAU=; b=fXR+qQcYdbYYcg6RPp370K6URpu1tJhvqEWxEMJ1vhxCs4/Nee1brft1pIIeulJfOaJE9X azGUeHKYWch3zBz6K6GYX7VX7t+ZBK2bwGVlX8vpd/30a7sZJGMftFOoizfSKCuTVDhq53 dU6wuG33pDyYFhPNe3O9CBQlql1z15hX5Xx59BZiQoVGB728O3Vwjzqos4LCwM6nfJ9mBc j3AaipE1u1ZW9KCM6S8yIpP4ChafSOxlKHdAjzrBVkCwEhe1C2MDEWu0vefv++2impASQh ED2KQ3WHQa6kPoeocFlO7uVBLlJBxAbgqhCrcbR0FkxumJbUrkqnehVMX5qoFQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1688796573; a=rsa-sha256; cv=none; b=O84YiRa+f+ejh3jhtIE0aALIpTMQRlU0KrPLgb39Wj4+EZH1r+TPkZynVXRxUh74XD2muz W7opLbzX+KmP6uH5SANzjs6giSLa81/DMjahVCgET6EkazY0i6I1UrSWfvoto+9bGd9s4a GSf/GdcUjby6ScCt9bFK9+juoTu7PXkMjYyNyh2m37OHn3tIDEc4esfR8dLaMkLVhwZ1lH sXnaltsZCBSSiSB994of0yllJSSCZlK1dsuAEkluVGpUtCRNGgIp1z5kW/ZGHD1vCQ4iVc i5ZCBfSIEpK+7m6U53WtNK0dIh6wDGXsJr3IkUcCXJFheG421WD5qD4ZGyF98g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Qyfx94n94z18xY; Sat, 8 Jul 2023 06:09:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 36869XY9025126; Sat, 8 Jul 2023 06:09:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 36869XkT025125; Sat, 8 Jul 2023 06:09:33 GMT (envelope-from git) Date: Sat, 8 Jul 2023 06:09:33 GMT Message-Id: <202307080609.36869XkT025125@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: b370ef156ab9 - main - libthr: Patch to reduce latency to acquire+release a pthread mutex. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b370ef156ab9d88450e9bc0440df522aec88cc44 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=b370ef156ab9d88450e9bc0440df522aec88cc44 commit b370ef156ab9d88450e9bc0440df522aec88cc44 Author: Greg Becker AuthorDate: 2023-07-07 22:03:14 +0000 Commit: Konstantin Belousov CommitDate: 2023-07-08 06:00:12 +0000 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 --- lib/libthr/thread/thr_mutex.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c index 2cbfd9a459d3..40e0b7382f95 100644 --- a/lib/libthr/thread/thr_mutex.c +++ b/lib/libthr/thread/thr_mutex.c @@ -596,7 +596,7 @@ check_and_init_mutex(pthread_mutex_t *mutex, struct pthread_mutex **m) *m = *mutex; ret = 0; - if (*m == THR_PSHARED_PTR) { + if (__predict_false(*m == THR_PSHARED_PTR)) { *m = __thr_pshared_offpage(mutex, 0); if (*m == NULL) ret = EINVAL; @@ -714,7 +714,7 @@ done: return (ret); } -static inline int +static __always_inline int mutex_lock_common(struct pthread_mutex *m, const struct timespec *abstime, bool cvattach, bool rb_onlist) { @@ -728,7 +728,7 @@ mutex_lock_common(struct pthread_mutex *m, const struct timespec *abstime, if (!rb_onlist) robust = _mutex_enter_robust(curthread, m); ret = _thr_umutex_trylock2(&m->m_lock, TID(curthread)); - if (ret == 0 || ret == EOWNERDEAD) { + if (__predict_true(ret == 0) || ret == EOWNERDEAD) { enqueue_mutex(curthread, m, ret); if (ret == EOWNERDEAD) m->m_lock.m_flags |= UMUTEX_NONCONSISTENT; @@ -951,7 +951,7 @@ mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime) return (ret); } -static int +static __always_inline int mutex_unlock_common(struct pthread_mutex *m, bool cv, int *mtx_defer) { struct pthread *curthread;