From patchwork Sat Oct 1 04:13:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1685142 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=X23R84qL; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MfYcx0sMFz20Zd for ; Sat, 1 Oct 2022 14:13:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF009385E440 for ; Sat, 1 Oct 2022 04:13:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF009385E440 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664597630; bh=FBrIFEmytpFVKYCC6eetMqNkrn32AZyA1SJ/Xmpr9kM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=X23R84qLvVhKR4/j2dbE0aJwKSljN6SR8+c9APBEurpfmt4LC3SLFvdlz9qoN/86G /Hs0j7jZUIBn+3YQOagnclsP/rwkvIjcPgg+eC2+wJVANprC6lXKlF1ujm+6cnuOhh VtI1hJFW/YKbANoCzng7IaVyfvBG/oCC+CIGzjog= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by sourceware.org (Postfix) with ESMTPS id 992063858282 for ; Sat, 1 Oct 2022 04:13:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 992063858282 Received: by mail-qk1-x734.google.com with SMTP id d15so3971958qka.9 for ; Fri, 30 Sep 2022 21:13:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=FBrIFEmytpFVKYCC6eetMqNkrn32AZyA1SJ/Xmpr9kM=; b=eXnTzo7h1/8yGTOCajbcBdmiaBe0q0ovlldytTfvShBaStvAwOzub2r7plOPumnW92 jHDvTezNQDD0NG/o/h07RzyCqpgQafcn6pkRGY7JZfNqCfCP+jNOYYi5gUeAxFw50wTz rIQtq9uTnkCAbnb3RzN/11flHj4Nsl/LwE43vNbIXdzvaOTWSMUBbTDefXIN05REtaJZ fYQbkfIfGEZJoy1KpA5sp/gnXa1VLcYErmvkU96+iUhjfemVxMR6z+5vDE6MG0Jwy0qi tt0IlDqWnbCSuZu4QrUVGjj7e+8UMcPKqzFY9nnqjR+V8Mk6y8+q9VlLSM56vC411NVu y9Vg== X-Gm-Message-State: ACrzQf2WUklBw71W8Cj31gIyeJoj86yJPpV864moSp4/2hO9O0c4PnYr EQvbzCpczIRpo2hR72OZDYtRtQhpveY= X-Google-Smtp-Source: AMsMyM7t0tUMQtESM2ZinmSLoHmpct+wCG/vh7/tsimoC+MJkC0J4LPUTs2ByA5RzHvliCeHBqEWsQ== X-Received: by 2002:a37:e101:0:b0:6ce:6a09:879e with SMTP id c1-20020a37e101000000b006ce6a09879emr8319420qkm.587.1664597609459; Fri, 30 Sep 2022 21:13:29 -0700 (PDT) Received: from noah-tgl.. (cpe-74-64-106-10.nyc.res.rr.com. [74.64.106.10]) by smtp.gmail.com with ESMTPSA id o2-20020ac87c42000000b0035cebb79aaesm3896785qtv.18.2022.09.30.21.13.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Sep 2022 21:13:29 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 1/2] Benchtests: Add bench for pthread_spin_{try}lock and mutex_trylock Date: Fri, 30 Sep 2022 21:13:26 -0700 Message-Id: <20221001041327.1133757-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221001023337.1127793-2-goldstein.w.n@gmail.com> References: <20221001023337.1127793-2-goldstein.w.n@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Reuses infrastructure from previous pthread_mutex_lock benchmarks to test other performance sensitive functions. --- benchtests/Makefile | 10 ++++- ...utex-locks.c => bench-pthread-lock-base.c} | 20 +++++----- benchtests/bench-pthread-mutex-lock.c | 32 ++++++++++++++++ benchtests/bench-pthread-mutex-trylock.c | 37 +++++++++++++++++++ benchtests/bench-pthread-spin-lock.c | 30 +++++++++++++++ benchtests/bench-pthread-spin-trylock.c | 34 +++++++++++++++++ 6 files changed, 151 insertions(+), 12 deletions(-) rename benchtests/{bench-pthread-mutex-locks.c => bench-pthread-lock-base.c} (93%) create mode 100644 benchtests/bench-pthread-mutex-lock.c create mode 100644 benchtests/bench-pthread-mutex-trylock.c create mode 100644 benchtests/bench-pthread-spin-lock.c create mode 100644 benchtests/bench-pthread-spin-trylock.c diff --git a/benchtests/Makefile b/benchtests/Makefile index d99771be74..fc1cda7fc3 100644 --- a/benchtests/Makefile +++ b/benchtests/Makefile @@ -103,11 +103,19 @@ endif bench-pthread := \ pthread-locks \ - pthread-mutex-locks \ + pthread-mutex-lock \ + pthread-mutex-trylock \ + pthread-spin-lock \ + pthread-spin-trylock \ pthread_once \ thread_create \ # bench-pthread +LDLIBS-bench-pthread-mutex-lock += -lm +LDLIBS-bench-pthread-mutex-trylock += -lm +LDLIBS-bench-pthread-spin-lock += -lm +LDLIBS-bench-pthread-spin-trylock += -lm + bench-string := \ ffs \ ffsll \ diff --git a/benchtests/bench-pthread-mutex-locks.c b/benchtests/bench-pthread-lock-base.c similarity index 93% rename from benchtests/bench-pthread-mutex-locks.c rename to benchtests/bench-pthread-lock-base.c index 1685b9dd1f..fac8a12b52 100644 --- a/benchtests/bench-pthread-mutex-locks.c +++ b/benchtests/bench-pthread-lock-base.c @@ -1,4 +1,4 @@ -/* Measure mutex_lock for different threads and critical sections. +/* Measure lock functions for different threads and critical sections. Copyright (C) 2022 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -17,7 +17,6 @@ . */ #define TEST_MAIN -#define TEST_NAME "pthread-mutex-locks" #define TIMEOUT (20 * 60) #include @@ -31,8 +30,8 @@ #include "bench-timing.h" #include "json-lib.h" -static pthread_mutex_t lock; -static pthread_mutexattr_t attr; +static bench_lock_t lock; +static bench_lock_attr_t attr; static pthread_barrier_t barrier; #define START_ITERS 1000 @@ -104,9 +103,9 @@ worker (void *v) TIMING_NOW (start); while (iters--) { - pthread_mutex_lock (&lock); + LOCK (&lock); critical_section (crt_len); - pthread_mutex_unlock (&lock); + UNLOCK (&lock); non_critical_section (non_crt_len); } TIMING_NOW (stop); @@ -123,7 +122,7 @@ do_one_test (int num_threads, int crt_len, int non_crt_len, long iters) Worker_Params *p, params[num_threads]; pthread_t threads[num_threads]; - pthread_mutex_init (&lock, &attr); + LOCK_INIT (&lock, &attr); pthread_barrier_init (&barrier, NULL, num_threads); for (i = 0; i < num_threads; i++) @@ -137,7 +136,7 @@ do_one_test (int num_threads, int crt_len, int non_crt_len, long iters) for (i = 0; i < num_threads; i++) pthread_join (threads[i], NULL); - pthread_mutex_destroy (&lock); + LOCK_DESTROY (&lock); pthread_barrier_destroy (&barrier); mean = 0; @@ -246,7 +245,7 @@ do_bench (void) char name[128]; json_init (&json_ctx, 2, stdout); - json_attr_object_begin (&json_ctx, "pthread_mutex_locks"); + json_attr_object_begin (&json_ctx, TEST_NAME); /* The thread config begins from 1, and increases by 2x until nprocs. We also wants to test over-saturation case (1.25*nprocs). */ @@ -260,8 +259,7 @@ do_bench (void) threads[th_conf++] = nprocs; threads[th_conf++] = nprocs + nprocs / 4; - pthread_mutexattr_init (&attr); - pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); + LOCK_ATTR_INIT (&attr); snprintf (name, sizeof name, "type=adaptive"); for (k = 0; k < (sizeof (non_crt_lens) / sizeof (int)); k++) diff --git a/benchtests/bench-pthread-mutex-lock.c b/benchtests/bench-pthread-mutex-lock.c new file mode 100644 index 0000000000..16556d4116 --- /dev/null +++ b/benchtests/bench-pthread-mutex-lock.c @@ -0,0 +1,32 @@ +/* Measure mutex_lock for different threads and critical sections. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define LOCK(lock) pthread_mutex_lock (lock) +#define UNLOCK(lock) pthread_mutex_unlock (lock) +#define LOCK_INIT(lock, attr) pthread_mutex_init (lock, attr) +#define LOCK_DESTROY(lock) pthread_mutex_destroy (lock) +#define LOCK_ATTR_INIT(attr) \ + pthread_mutexattr_init (attr); \ + pthread_mutexattr_settype (attr, PTHREAD_MUTEX_ADAPTIVE_NP); + +#define bench_lock_t pthread_mutex_t +#define bench_lock_attr_t pthread_mutexattr_t + +#define TEST_NAME "pthread-mutex-lock" + +#include "bench-pthread-lock-base.c" diff --git a/benchtests/bench-pthread-mutex-trylock.c b/benchtests/bench-pthread-mutex-trylock.c new file mode 100644 index 0000000000..66318f499f --- /dev/null +++ b/benchtests/bench-pthread-mutex-trylock.c @@ -0,0 +1,37 @@ +/* Measure mutex_trylock for different threads and critical sections. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define LOCK(lock) \ + while (pthread_mutex_trylock (lock) != 0) \ + { \ + non_critical_section (non_crt_len); \ + } + +#define UNLOCK(lock) pthread_mutex_unlock (lock) +#define LOCK_INIT(lock, attr) pthread_mutex_init (lock, attr) +#define LOCK_DESTROY(lock) pthread_mutex_destroy (lock) +#define LOCK_ATTR_INIT(attr) \ + pthread_mutexattr_init (attr); \ + pthread_mutexattr_settype (attr, PTHREAD_MUTEX_ADAPTIVE_NP); + +#define bench_lock_t pthread_mutex_t +#define bench_lock_attr_t pthread_mutexattr_t + +#define TEST_NAME "pthread-mutex-trylock" + +#include "bench-pthread-lock-base.c" diff --git a/benchtests/bench-pthread-spin-lock.c b/benchtests/bench-pthread-spin-lock.c new file mode 100644 index 0000000000..2174933d6b --- /dev/null +++ b/benchtests/bench-pthread-spin-lock.c @@ -0,0 +1,30 @@ +/* Measure mutex_trylock for different threads and critical sections. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define LOCK(lock) pthread_spin_lock (lock) +#define UNLOCK(lock) pthread_spin_unlock (lock) +#define LOCK_INIT(lock, attr) pthread_spin_init (lock, *(attr)) +#define LOCK_DESTROY(lock) pthread_spin_destroy (lock) +#define LOCK_ATTR_INIT(attr) *(attr) = 0 + +#define bench_lock_t pthread_spinlock_t +#define bench_lock_attr_t int + +#define TEST_NAME "pthread-spin-lock" + +#include "bench-pthread-lock-base.c" diff --git a/benchtests/bench-pthread-spin-trylock.c b/benchtests/bench-pthread-spin-trylock.c new file mode 100644 index 0000000000..49eb972761 --- /dev/null +++ b/benchtests/bench-pthread-spin-trylock.c @@ -0,0 +1,34 @@ +/* Measure spin_trylock for different threads and critical sections. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define LOCK(lock) \ + while (pthread_spin_lock (lock) != 0) \ + { \ + non_critical_section (non_crt_len); \ + } +#define UNLOCK(lock) pthread_spin_unlock (lock) +#define LOCK_INIT(lock, attr) pthread_spin_init (lock, *(attr)) +#define LOCK_DESTROY(lock) pthread_spin_destroy (lock) +#define LOCK_ATTR_INIT(attr) *(attr) = 0 + +#define bench_lock_t pthread_spinlock_t +#define bench_lock_attr_t int + +#define TEST_NAME "pthread-spin-trylock" + +#include "bench-pthread-lock-base.c" From patchwork Sat Oct 1 04:13:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 1685143 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=kwQ6YHGF; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MfYdl02LLz20Zd for ; Sat, 1 Oct 2022 14:14:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DD6963853839 for ; Sat, 1 Oct 2022 04:14:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD6963853839 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664597671; bh=M5vxv/Iy/TBEHNOwe+gjtu/JEOuUFK0N5+uNxJOXcws=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=kwQ6YHGFU4GdhbOZ81bQuwDgkCwFr+PptVrhD76TyRy2JqmzpSfBJ/feqmLZLmaah t/tzPGOQo8uRcOf6LBMUKfGYL8r8S93tWIkrI+rn944cK8AZoLdVCRJTPrtSkSj8Nt duNk/0JHlEbHOaI7Ha5/tixKJ4m844hx0M08wh40= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by sourceware.org (Postfix) with ESMTPS id C7EAA385C401 for ; Sat, 1 Oct 2022 04:13:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C7EAA385C401 Received: by mail-qt1-x82b.google.com with SMTP id c11so3794522qtw.8 for ; Fri, 30 Sep 2022 21:13:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=M5vxv/Iy/TBEHNOwe+gjtu/JEOuUFK0N5+uNxJOXcws=; b=ICg/VvoZs3Z9qST8/95uqdqY1+enUADFxu8A3H8GYo5fJHwYjzPsjKCEeTTskqIiFe 4glOSfNGnZYoCG3Mq/r+j440VxJpGavWBRvtGZE121Xqb3/O9cttYVSYUGKq/aYAdx4I ROwm1UT8lU08YVAjV+8TqWEQRYDk9vXfjsVnu/FtVS7p5BTFa3RGaxpFImSJDRvV08lt 7Fj4Rgnw5euBKFHwOmfHq3BcVkz7vp88Qt9HCxRlzhxRh5GeJ+lMsdiERm3QUh3NYOdv zylMMINj1b0tXVyDfbYPoPM0fnPqlHiFhkVqJ22le9w8WV07jZBNvW2jUYuWP7EUj5N4 6q/g== X-Gm-Message-State: ACrzQf2qACRF/DFaE48t47cVcPI+KcdIzp84MpjgSiG6VnPstIBTH/iZ QLtJV0yYjsQBeHJ9qhPdICYffLBs5E0= X-Google-Smtp-Source: AMsMyM5n72x3qVNlldtgEK5p3AKLDJILjBdBHazRPhcm1bpzYW7k7o2KfVFMJYiXYaluIVKwwEYmvQ== X-Received: by 2002:ac8:7d55:0:b0:35c:d884:449b with SMTP id h21-20020ac87d55000000b0035cd884449bmr9201554qtb.446.1664597610738; Fri, 30 Sep 2022 21:13:30 -0700 (PDT) Received: from noah-tgl.. (cpe-74-64-106-10.nyc.res.rr.com. [74.64.106.10]) by smtp.gmail.com with ESMTPSA id o2-20020ac87c42000000b0035cebb79aaesm3896785qtv.18.2022.09.30.21.13.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Sep 2022 21:13:30 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2 2/2] x86: Cleanup pthread_spin_{try}lock.S Date: Fri, 30 Sep 2022 21:13:27 -0700 Message-Id: <20221001041327.1133757-2-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221001041327.1133757-1-goldstein.w.n@gmail.com> References: <20221001023337.1127793-2-goldstein.w.n@gmail.com> <20221001041327.1133757-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Save a jmp on the lock path coming from an initial failure in pthread_spin_lock.S. This costs 4-bytes of code but since the function still fits in the same number of 16-byte blocks (default function alignment) it does not have affect on the total binary size of libc.so (unchanged after this commit). pthread_spin_trylock was using a CAS when a simple xchg works which is often more expensive. Full check passes on x86-64. --- sysdeps/x86_64/nptl/pthread_spin_lock.S | 23 +++++++++++++++------- sysdeps/x86_64/nptl/pthread_spin_trylock.S | 18 ++++++++++++----- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/sysdeps/x86_64/nptl/pthread_spin_lock.S b/sysdeps/x86_64/nptl/pthread_spin_lock.S index 44b837d9db..1e09e59b10 100644 --- a/sysdeps/x86_64/nptl/pthread_spin_lock.S +++ b/sysdeps/x86_64/nptl/pthread_spin_lock.S @@ -19,18 +19,27 @@ #include ENTRY(__pthread_spin_lock) -1: LOCK - decl 0(%rdi) - jne 2f + /* Always return zero. */ xor %eax, %eax + LOCK + decl 0(%rdi) + jne 1f ret .align 16 -2: rep +1: + /* `rep nop` == `pause`. */ + rep nop - cmpl $0, 0(%rdi) - jg 1b - jmp 2b + cmpl %eax, 0(%rdi) + jle 1b + /* Just repeat the `lock decl` logic here. The code size save + of jumping back to entry doesn't change how many 16-byte + chunks (default function alignment) that the code fits in. */ + LOCK + decl 0(%rdi) + jne 1b + ret END(__pthread_spin_lock) versioned_symbol (libc, __pthread_spin_lock, pthread_spin_lock, GLIBC_2_34) diff --git a/sysdeps/x86_64/nptl/pthread_spin_trylock.S b/sysdeps/x86_64/nptl/pthread_spin_trylock.S index fffdb27dd9..a1f97cb420 100644 --- a/sysdeps/x86_64/nptl/pthread_spin_trylock.S +++ b/sysdeps/x86_64/nptl/pthread_spin_trylock.S @@ -20,13 +20,21 @@ #include ENTRY(__pthread_spin_trylock) - movl $1, %eax xorl %ecx, %ecx - lock - cmpxchgl %ecx, (%rdi) + /* xchg has implicit LOCK prefix. */ + xchgl %ecx, (%rdi) + + /* Branch on result. Expectation is the use of trylock will be + branching on success/failure so this branch can be used to + to predict the coming branch. It has the benefit of + breaking the likely expensive memory dependency on (%rdi). */ + cmpl $1, %ecx + jnz 1f + xorl %eax, %eax + ret +1: movl $EBUSY, %eax - cmovel %ecx, %eax - retq + ret END(__pthread_spin_trylock) versioned_symbol (libc, __pthread_spin_trylock, pthread_spin_trylock, GLIBC_2_34)