From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by sourceware.org (Postfix) with ESMTPS id 1835738582B0 for ; Mon, 3 Oct 2022 17:10:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1835738582B0 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-x833.google.com with SMTP id g23so6719703qtu.2 for ; Mon, 03 Oct 2022 10:10:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=z4FigHJSI0uZeicGkM52XI58y+HYTRXvv61hcTM5AC4=; b=oTq7FQNSPT1dgIADhi7LETkuqZOS8ARySSl/++4McY4iBWQFrjQcZiPOaaabmwdC4c YwSucLUnyeae0SRcWezdCbBUQjpWSR11rVGOK4R3aqDdtRbKfygWE+FJl3HT0HpgXIfM SeQ5W+xJaENotN7WpDPC0piS64rml7d+c8AetZtzsumEg8ysXvIUGYwKFlzZC7oFZQTR xGuRaWhP+pRJYl6MYN3cebvmdSiHpE/9osxcIx+suo0BnZYVKaLA6o1MLJlRhZJiGaid 9LafN8bRpLcy5gyVbtHP26Zqbf75QIfmAOmeGHXGKl7uw2FCs5T3MTOdf1E2SRhU8coe fnjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=z4FigHJSI0uZeicGkM52XI58y+HYTRXvv61hcTM5AC4=; b=WMIknAp7hfQwPHVbePKLF2CftXg62Bk0rZe9K0wBo8ODU7ogmkfe8SYp+1ZgV9KZ2r CDL4SW/UmemF0XZuPjK0qrTQZywNLDdnNiUejR3+0kK+8+EytnmaPfQm49ht37RR/KoG 1jOZeufoCBFcIJbjJ9eCr/v6rRouzAEtR+U4WV9fDsbpt7sNEWFLpvLLniLwG0lhVZZ5 uYlcYRq6KOGC8Wbdma+dZ4xuiPIfuT31iHASplGEgiaqZghmt5gKg2CvXVnpXMCeDnxy YlWCStPKXTq0pzGXyz+wxA5STE+CubNDz06to2aFiSwsJ+RgG1sYz6qxPqR36gMRSePF VE1Q== X-Gm-Message-State: ACrzQf1/BHt36ntnkiIHy+NyJfoIVK1G6jxINU0s1LhSQ0QYFGPRXgFv nPVrwiEvvONkZvmmrG+cJeSj+eCJzoaBPmozDrSTlGmy X-Google-Smtp-Source: AMsMyM4dBYcVSG1No0lTJ72xOMdSeXCJFTeSnMN/g0Cf9DYKkHglUlppvfzqKBr/ZFmuhwIPjBFqg3RYKb9IDc8ngUs= X-Received: by 2002:ac8:4e56:0:b0:35d:56c8:3a93 with SMTP id e22-20020ac84e56000000b0035d56c83a93mr16171397qtw.617.1664817037196; Mon, 03 Oct 2022 10:10:37 -0700 (PDT) MIME-Version: 1.0 References: <20221001023337.1127793-2-goldstein.w.n@gmail.com> <20221001041327.1133757-1-goldstein.w.n@gmail.com> In-Reply-To: <20221001041327.1133757-1-goldstein.w.n@gmail.com> From: "H.J. Lu" Date: Mon, 3 Oct 2022 10:10:01 -0700 Message-ID: Subject: Re: [PATCH v2 1/2] Benchtests: Add bench for pthread_spin_{try}lock and mutex_trylock To: Noah Goldstein Cc: libc-alpha@sourceware.org, carlos@systemhalted.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3023.4 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 List-Id: On Fri, Sep 30, 2022 at 9:13 PM Noah Goldstein wrote: > > 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" > -- > 2.34.1 > LGTM. Thanks. -- H.J.