Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp546375rwd; Wed, 14 Jun 2023 21:22:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6k9zkw97xYmVDjcqCiuiTzvXveQubwURhmdH/aKhZ8BKjBRS4H9TXNZoLVBd9CWTzCt/aJ X-Received: by 2002:a17:906:9b8a:b0:978:70e1:f02b with SMTP id dd10-20020a1709069b8a00b0097870e1f02bmr21869577ejc.75.1686802945373; Wed, 14 Jun 2023 21:22:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686802945; cv=none; d=google.com; s=arc-20160816; b=wRUNL26tfOXLaLx8vKdz6m4pVpC5tvmhLhINzRkoGvs0X+16uBPuRygDY0vdpFW3Za P6VobA1zkLsV5t6P5o1huI2YcVozdYrEtRQuMAThOOetyl95DQ7TAJHJpijtGhi9I+54 QO1BuaaM78czPTJzRxyxLRR/m/YH8EPHvhb9y7rlIiwzMgwzMhMREFpOM7wzU3eL/dDC RTyXYwjzcIOtcHbFybXdbfaQa3R3uN/ezxqqQAQUlZysGVCfV9tOsDAKlWRl0Snyqjo8 k9yda2VlEgu3MX+z1doThZcY3C/NwPa6aiMr/IO6N9/U5wwc6X9uPtPziluyEqDk2/0f lyXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:mime-version:message-id:date :dkim-signature; bh=EGSFse5BO9SaZtO47qLHwozj1QdxYYuFsGDqkkwyYl4=; b=X7O9gITdUbIXBVO6Kn2U1mWz4BUfqE/iJkyT+pNYEp5T4K6hJ/z5tZOOZNr+MosxEV GTOxbiSgUZNTax4xBbZp3NDVh8aE6lvmPAODAACNFaqhX5rksbJpWAGtR7wLYx+WZ9Eb 9RRfmY3kFunZpxGez5YiEInmLV2qULsQo1sq8gSBtGByBE/SBTJLtyecVBTNcNW2YvbX jjPa5zrBDj5980aln7TuIteS9EiW/Ava/1JhKnTg9kcmZXuhe8cjIk4DtGvZL61fo/CV KwIVzdIn1BuKdhf7iOhUS7/RTXDoBc1Hw9bR7mTpHzetwsmvTrgUdH0nVKRfxA3lu5CO Svdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=eAW7X9o+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ay6-20020a170906d28600b009827b267dddsi1375656ejb.1026.2023.06.14.21.22.00; Wed, 14 Jun 2023 21:22:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=eAW7X9o+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238279AbjFOEHl (ORCPT + 99 others); Thu, 15 Jun 2023 00:07:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237788AbjFOEHc (ORCPT ); Thu, 15 Jun 2023 00:07:32 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7AC41BF8 for ; Wed, 14 Jun 2023 21:07:30 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-56938733c13so17471867b3.1 for ; Wed, 14 Jun 2023 21:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686802050; x=1689394050; h=to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=EGSFse5BO9SaZtO47qLHwozj1QdxYYuFsGDqkkwyYl4=; b=eAW7X9o+ZbRUc1+mOuwXgN8xMLhafZ22t4yvIswPDNtFn8tNXX/x6GBqfO71ZdsffB IGbhYiz8Eph5SDgAY/HqTDBTTTwwVl/zV0GojBrRf1NnCStxSXVnxQLnCO2aUPOrn8cM 8SMc3klIdLg0lE3LTql/4Yx7NgcsJfMHA1erciyv26keJXlKt9C45wVlShaAbzQtBjvK rROE2cQM87K2oiSooHVwHRK6XWvHoJKS/OYQpEFRI8+YtdpEs7rBo+Wfv+Jlxh2bo1RS WlFLtqlNGEoD2uud10r/bxUAz6rou5r98QQttMm4YKhta4cbMSTgZZH6z6ALpUp10pLu JX+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686802050; x=1689394050; h=to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=EGSFse5BO9SaZtO47qLHwozj1QdxYYuFsGDqkkwyYl4=; b=TR1LJGHlBPJwGMYVNISFAesiqWBS5JiRvL+WdgDnvqeZYKZsIuLZs34uH+G1sAqa+G YZjuDdCILFCK83gOWEXkCp2E+OgiIN3/mS3FEJRwH+Rwv4Vs1EcaDXSKqgU0yh7qpwWf Hkhf7/s3vZ/wLXFJccnW3HsSc/BAALPzPWA00n5+cwjIOagk90R/cISsh6miRjYdAPDj 4vFqbopombSElSzdYE802wM7h87d4qjZe/zi5U65T5taFuDhcvJH9P70xpSpG8QUoo+4 4nfub/pbg1zwHGpUAg1P2BY/auxu8SsWgPa7qEXS5pxil4kL+W+rvNQ00geMOnu0SUsV 77og== X-Gm-Message-State: AC+VfDwS2WhHRBQO0NdjexurjKaep3TPmPPSsURDD54r3G80dRPi7VNN ysYT7fALpZSfjF/5mM8ajAmtjf26XEup X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:768d:713f:8ce8:1d35]) (user=irogers job=sendgmr) by 2002:a81:b644:0:b0:569:e04a:239d with SMTP id h4-20020a81b644000000b00569e04a239dmr1668459ywk.0.1686802049975; Wed, 14 Jun 2023 21:07:29 -0700 (PDT) Date: Wed, 14 Jun 2023 21:07:14 -0700 Message-Id: <20230615040715.2064350-1-irogers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Subject: [PATCH v2 1/2] perf sharded_mutex: Introduce sharded_mutex From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Yuan Can , Kan Liang , Masami Hiramatsu , Huacai Chen , Andres Freund , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Per object mutexes may come with significant memory cost while a global mutex can suffer from unnecessary contention. A sharded mutex is a compromise where objects are hashed and then a particular mutex for the hash of the object used. Contention can be controlled by the number of shards. Signed-off-by: Ian Rogers v2. Use hashmap.h's hash_bits in case of contention from alignment of objects. --- tools/perf/util/Build | 1 + tools/perf/util/sharded_mutex.c | 33 +++++++++++++++++++++++++++++++++ tools/perf/util/sharded_mutex.h | 29 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 tools/perf/util/sharded_mutex.c create mode 100644 tools/perf/util/sharded_mutex.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index ff2fd1a36bb8..96f4ea1d45c5 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -145,6 +145,7 @@ perf-y += mem2node.o perf-y += clockid.o perf-y += list_sort.o perf-y += mutex.o +perf-y += sharded_mutex.o perf-$(CONFIG_LIBBPF) += bpf-loader.o perf-$(CONFIG_LIBBPF) += bpf_map.o diff --git a/tools/perf/util/sharded_mutex.c b/tools/perf/util/sharded_mutex.c new file mode 100644 index 000000000000..e11e8d0945a7 --- /dev/null +++ b/tools/perf/util/sharded_mutex.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "sharded_mutex.h" + +#include + +struct sharded_mutex *sharded_mutex__new(size_t num_shards) +{ + struct sharded_mutex *result; + size_t size; + unsigned int bits; + + for (bits = 0; ((size_t)1 << bits) < num_shards; bits++) + ; + + size = sizeof(*result) + sizeof(struct mutex) * (1 << bits); + result = malloc(size); + if (!result) + return NULL; + + result->cap_bits = bits; + for (size_t i = 0; i < ((size_t)1 << bits); i++) + mutex_init(&result->mutexes[i]); + + return result; +} + +void sharded_mutex__delete(struct sharded_mutex *sm) +{ + for (size_t i = 0; i < ((size_t)1 << sm->cap_bits); i++) + mutex_destroy(&sm->mutexes[i]); + + free(sm); +} diff --git a/tools/perf/util/sharded_mutex.h b/tools/perf/util/sharded_mutex.h new file mode 100644 index 000000000000..7325e969eee3 --- /dev/null +++ b/tools/perf/util/sharded_mutex.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef PERF_SHARDED_MUTEX_H +#define PERF_SHARDED_MUTEX_H + +#include "mutex.h" +#include "hashmap.h" + +/* + * In a situation where a lock is needed per object, having a mutex can be + * relatively memory expensive (40 bytes on x86-64). If the object can be + * constantly hashed, a sharded mutex is an alternative global pool of mutexes + * where the mutex is looked up from a hash value. This can lead to collisions + * if the number of shards isn't large enough. + */ +struct sharded_mutex { + /* mutexes array is 1<mutexes[hash_bits(hash, sm->cap_bits)]; +} + +#endif /* PERF_SHARDED_MUTEX_H */ -- 2.41.0.162.gfafddb0af9-goog