Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACCB73207 for ; Tue, 23 Aug 2022 22:09:36 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-334f49979a0so264105097b3.10 for ; Tue, 23 Aug 2022 15:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=HGXvwC70P4lQM2SClDMy1A450ci+Pd66+W81CaUVSbs=; b=YFJ+R6mwgQ9oKPaH6psZN8+PmZ8nr2tYvLI9upGLW9XRXR1kFqWJkfj4abLrpb3Hrc I968K2izzHbGreVMfH8Nre+zZcAEfu79EUU7ARSahNut/NPEWGTZ/0RJIpb9FQ4ZeExj mks3mn1D+5g4txFEgf+WJpGfe8nWo0W0SuDvtP5/bBfxdEDgVId5hifdMaH8iuAU9XB4 GeCtI9QzJZTsCskOkuJ2Ef7gOLeI9B615teO2AZ5TMbj/c7w3o9BzFvx1u6UjyzWkrlZ rF3g5v5vteJqRivGfJQ1Gpyv0cKcxn+Infe4UjnEBCp7GyYaoCfHNQM+d9iTVS4Si+CB RHIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=HGXvwC70P4lQM2SClDMy1A450ci+Pd66+W81CaUVSbs=; b=0FYw/bZH+pgaDkgdqBM/B34Rz8Eq+MFGTgAQtdgCofHX4hWpLIMrXK9BgVvqJnmSCo mQuqNJDxI3D7CotcXR6LkNdFUOZfxvamYqAziibj90Vb587tVfW2vCR0ZCy6WcpzADyK lb4zdhe3+eJ6Z7z9AD7xK7z0+M4Z8X2iGFwhk0UujO1DszaesVugtB4pxm9DkK3RYTYv xrefmoQtoceIpEIbltYA9bJuqVWA48LYD2JHypMRlZg01Oc5ekyuiS+bPdDqC+8B+SrF sGq3XyFmmNEknZ5sQZEbqF2/htssVAEw+4eynv0HR8zSRu63DFJNXKpbLVd9YBdSaDwY yQOQ== X-Gm-Message-State: ACgBeo1KE6kbE/gSiaOjPSD4bhtjO2ZBCE8sbSn48AGW44B0jSfp2nls VYKVIXdvu9HUXCeCQgunq5v6ZQkZ187z X-Google-Smtp-Source: AA6agR6wsVs9Ektezw4eqFarF6XaQ48d0ENVXVoZVMJxS1RzWjeg+uj9OG2/9mmpV9nP7+j6GnE/maf+tFs8 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7dbd:c08f:de81:c2a3]) (user=irogers job=sendgmr) by 2002:a05:6902:1502:b0:695:a994:475c with SMTP id q2-20020a056902150200b00695a994475cmr11974185ybu.547.1661292575542; Tue, 23 Aug 2022 15:09:35 -0700 (PDT) Date: Tue, 23 Aug 2022 15:09:05 -0700 In-Reply-To: <20220823220922.256001-1-irogers@google.com> Message-Id: <20220823220922.256001-2-irogers@google.com> Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20220823220922.256001-1-irogers@google.com> X-Mailer: git-send-email 2.37.2.609.g9ff673ca1a-goog Subject: [PATCH v2 01/18] perf mutex: Wrapped usage of mutex and cond From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Darren Hart , Davidlohr Bueso , "=?UTF-8?q?Andr=C3=A9=20Almeida?=" , Nathan Chancellor , Nick Desaulniers , Tom Rix , Weiguo Li , Athira Rajeev , Thomas Richter , Ravi Bangoria , Dario Petrillo , Hewenliang , yaowenbin , Wenyu Liu , Song Liu , Andrii Nakryiko , Dave Marchevsky , Leo Yan , Kim Phillips , Pavithra Gurushankar , Alexandre Truong , Quentin Monnet , William Cohen , Andres Freund , Adrian Hunter , "=?UTF-8?q?Martin=20Li=C5=A1ka?=" , Colin Ian King , James Clark , Fangrui Song , Stephane Eranian , Kajol Jain , Alexey Bayduraev , Riccardo Mancini , Andi Kleen , Masami Hiramatsu , Zechuan Chen , Jason Wang , Christophe JAILLET , Remi Bernon , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, llvm@lists.linux.dev Cc: Ian Rogers Content-Type: text/plain; charset="UTF-8" From: Pavithra Gurushankar Added a new header file mutex.h that wraps the usage of pthread_mutex_t and pthread_cond_t. By abstracting these it is possible to introduce error checking. Signed-off-by: Pavithra Gurushankar Signed-off-by: Ian Rogers --- tools/perf/util/Build | 1 + tools/perf/util/mutex.c | 97 +++++++++++++++++++++++++++++++++++++++++ tools/perf/util/mutex.h | 43 ++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 tools/perf/util/mutex.c create mode 100644 tools/perf/util/mutex.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 9dfae1bda9cc..8fd6dc8de521 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -143,6 +143,7 @@ perf-y += branch.o perf-y += mem2node.o perf-y += clockid.o perf-y += list_sort.o +perf-y += mutex.o perf-$(CONFIG_LIBBPF) += bpf-loader.o perf-$(CONFIG_LIBBPF) += bpf_map.o diff --git a/tools/perf/util/mutex.c b/tools/perf/util/mutex.c new file mode 100644 index 000000000000..d12cf0714268 --- /dev/null +++ b/tools/perf/util/mutex.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "mutex.h" + +#include "debug.h" +#include +#include + +static void check_err(const char *fn, int err) +{ + char sbuf[STRERR_BUFSIZE]; + + if (err == 0) + return; + + pr_err("%s error: '%s'", fn, str_error_r(err, sbuf, sizeof(sbuf))); +} + +#define CHECK_ERR(err) check_err(__func__, err) + +void mutex_init(struct mutex *mtx, bool pshared) +{ + pthread_mutexattr_t attr; + + CHECK_ERR(pthread_mutexattr_init(&attr)); + +#ifndef NDEBUG + /* In normal builds enable error checking, such as recursive usage. */ + CHECK_ERR(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)); +#endif + if (pshared) + pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + + CHECK_ERR(pthread_mutex_init(&mtx->lock, &attr)); + CHECK_ERR(pthread_mutexattr_destroy(&attr)); +} + +void mutex_destroy(struct mutex *mtx) +{ + CHECK_ERR(pthread_mutex_destroy(&mtx->lock)); +} + +void mutex_lock(struct mutex *mtx) +{ + CHECK_ERR(pthread_mutex_lock(&mtx->lock)); +} + +void mutex_unlock(struct mutex *mtx) +{ + CHECK_ERR(pthread_mutex_unlock(&mtx->lock)); +} + +bool mutex_trylock(struct mutex *mtx) +{ + int ret = pthread_mutex_trylock(&mtx->lock); + + if (ret == 0) + return true; /* Lock acquired. */ + + if (ret == EBUSY) + return false; /* Lock busy. */ + + /* Print error. */ + CHECK_ERR(ret); + return false; +} + +void cond_init(struct cond *cnd, bool pshared) +{ + pthread_condattr_t attr; + + CHECK_ERR(pthread_condattr_init(&attr)); + if (pshared) + CHECK_ERR(pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)); + + CHECK_ERR(pthread_cond_init(&cnd->cond, &attr)); + CHECK_ERR(pthread_condattr_destroy(&attr)); +} + +void cond_destroy(struct cond *cnd) +{ + CHECK_ERR(pthread_cond_destroy(&cnd->cond)); +} + +void cond_wait(struct cond *cnd, struct mutex *mtx) +{ + CHECK_ERR(pthread_cond_wait(&cnd->cond, &mtx->lock)); +} + +void cond_signal(struct cond *cnd) +{ + CHECK_ERR(pthread_cond_signal(&cnd->cond)); +} + +void cond_broadcast(struct cond *cnd) +{ + CHECK_ERR(pthread_cond_broadcast(&cnd->cond)); +} diff --git a/tools/perf/util/mutex.h b/tools/perf/util/mutex.h new file mode 100644 index 000000000000..952276ad83bd --- /dev/null +++ b/tools/perf/util/mutex.h @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_MUTEX_H +#define __PERF_MUTEX_H + +#include +#include + +/* + * A wrapper around the mutex implementation that allows perf to error check + * usage, etc. + */ +struct mutex { + pthread_mutex_t lock; +}; + +/* A wrapper around the condition variable implementation. */ +struct cond { + pthread_cond_t cond; +}; + +/* + * Initialize the mtx struct, if pshared is set then specify the process-shared + * rather than default process-private attribute. + */ +void mutex_init(struct mutex *mtx, bool pshared); +void mutex_destroy(struct mutex *mtx); + +void mutex_lock(struct mutex *mtx); +void mutex_unlock(struct mutex *mtx); +bool mutex_trylock(struct mutex *mtx); + +/* + * Initialize the cond struct, if pshared is set then specify the process-shared + * rather than default process-private attribute. + */ +void cond_init(struct cond *cnd, bool pshared); +void cond_destroy(struct cond *cnd); + +void cond_wait(struct cond *cnd, struct mutex *mtx); +void cond_signal(struct cond *cnd); +void cond_broadcast(struct cond *cnd); + +#endif /* __PERF_MUTEX_H */ -- 2.37.2.609.g9ff673ca1a-goog