Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp451839rwd; Wed, 14 Jun 2023 19:16:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6LCrs6/2KqxZinueupCZ4tENReCGypP5z4dk1z7rCBcF0dW6+wcc8+oc61wCFCjiJT9hoK X-Received: by 2002:a17:906:9756:b0:96a:440b:d5c8 with SMTP id o22-20020a170906975600b0096a440bd5c8mr14063482ejy.59.1686795408192; Wed, 14 Jun 2023 19:16:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686795408; cv=none; d=google.com; s=arc-20160816; b=zo8UF9WpGTJm1eJxs/8gIIcworas6fpnAfd++CQ6DQElj1Iz1dL0wnLO1KFR8+lkXI +c89Qjduxig1SrW3bMJSMkqsJ6hgSFJNHxXlFZrgmgGaS0LVhozoKNNvz9fE2OfHs9Ai efA4T+QKAYEzQnU4PVhohDDLhpxUBRryX/MNG4W1n5tr9y2RQYqTZlHVJV93ceInrMaL urIX4xFJFoV1VqupmUldbNVL2CxPl3+HfLn2vnUJMlHJ7O3PZU0SKL3SndMF7L4S3HDT fD0r9EuQfmBqgoka361zlRUErDHRnhFsTHm5nOPgWdzvmm5ovF4Uo7P3d2HBhkYtScob 4c3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=MUGtQqvU4doNuraDYOeyIMqy4qa8mxp0g8G/zCpNJBo=; b=NvsrVqO5GxNjylgDoEG2wc6CSDl8Ch2WhNKqEtiBh/yIPyuEf5IK5EzeTVSWq5/Yl0 +5hO7CVllD/uS0m2uCrhnz8OjnLpo4Cvmys0DM9+/Ft8S80g2lYZH46iHV/kd6OSD4KX zfK/oNE88duOT/3uleVeDKpqP5rHye5rmSTdSPzd7qb94eIcJYJdexzhprdLHkQvW7dM 74yQM3iQcuD903x6awhAy+gQyTGeHNtjKezykJ1NLJEnNY+b+rs8yzs4Rdg+eSSjUr02 BZIEK7aPjcupT5kGkSxoX4dNrxhPSfn3/ypE7kvMRjcgv6XXm2K84Vm+5wgu91FUbq5Z v8BQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=3Mv27sSo; 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 bw18-20020a170906c1d200b009745c464a64si8986877ejb.472.2023.06.14.19.16.23; Wed, 14 Jun 2023 19:16:48 -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=3Mv27sSo; 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 S239767AbjFOBuN (ORCPT + 99 others); Wed, 14 Jun 2023 21:50:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240276AbjFOBt4 (ORCPT ); Wed, 14 Jun 2023 21:49:56 -0400 Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E1A2DF for ; Wed, 14 Jun 2023 18:49:54 -0700 (PDT) Received: by mail-qt1-x82b.google.com with SMTP id d75a77b69052e-3f9b7de94e7so114561cf.0 for ; Wed, 14 Jun 2023 18:49:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686793793; x=1689385793; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=MUGtQqvU4doNuraDYOeyIMqy4qa8mxp0g8G/zCpNJBo=; b=3Mv27sSob3hnvKuUvL3GhR2J4Zug+RHsAbzy84j7grFB9UCkt7jfIecOwlmblOnMSu fSZobWYEatbbU9bsg0RQGMrLx7gqLaHPMyRMW1P4XPQev2XkQszTJYIC2WevLfj40y+Q 5d5TyZCLlIc1BxEO12Ih9+C+CR4Ob+Ubm4q9QMBwSrLlNJZfHImfU1KCxmZHDykq6Xrt uNa8QeKKkeqyty/4LUCkGVassMhskovsomi3LMqw6gmutN7KnJps2cd+T+zgH5qTjpbn /gWF//Tvx7UeDdYMQ+O4QTaHyDn7A8GhLNYZkuZy3szjUGtlXhmmmZghlyswfM8HhVeZ 7fow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686793793; x=1689385793; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MUGtQqvU4doNuraDYOeyIMqy4qa8mxp0g8G/zCpNJBo=; b=guk5ckYbYLF32tLJw1D2bYlJrtlpzAnNEFshF0iKUR+AOE7EKvHWBVwGKtXUryRoJa zZ23FaUpjfhUlnDijMsdQQ28VdoPszYwrB8HRP/+VGcMNsBuuw4RwOkUMmtc/vfPaMVY n24bSr6CeTUG6jGM4AWletLFdwLJb+u3/GXfl1wCTlo7ya/PHI0/eTVfw6jMAkKfGTtY UArDwDtesJFoBGgFEC8vI2OV2uRKp2gwNRIaThToVL0OoMJTi0cW8vQntwVMIEK+qJ8k Fevn42fl76M1jPWjhz+FI5M3/EYOnpIEyMzmltt3QTYgVrcHi7zvZHVJJHqU/72WV402 dmFQ== X-Gm-Message-State: AC+VfDzo9HQV3pw8clpxZlFoPqVbdPQZWP0gYdm9nwgNjrblU+kj5ZQq sKQ07tUSmUb5DtYmXnYtASFlcNyFShBh0IA9WejwZA== X-Received: by 2002:a05:622a:1748:b0:3f8:3065:7fc5 with SMTP id l8-20020a05622a174800b003f830657fc5mr39981qtk.1.1686793793518; Wed, 14 Jun 2023 18:49:53 -0700 (PDT) MIME-Version: 1.0 References: <20230611072751.637227-1-irogers@google.com> <20230611072751.637227-2-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Wed, 14 Jun 2023 18:49:40 -0700 Message-ID: Subject: Re: [PATCH v1 2/2] perf annotation: Switch lock from a mutex to a sharded_mutex To: Namhyung Kim Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , 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" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 On Wed, Jun 14, 2023 at 5:34=E2=80=AFPM Namhyung Kim = wrote: > > Hi Ian, > > On Sun, Jun 11, 2023 at 12:28=E2=80=AFAM Ian Rogers = wrote: > > > > Remove the "struct mutex lock" variable from annotation that is > > allocated per symbol. This removes in the region of 40 bytes per > > symbol allocation. Use a sharded mutex where the number of shards is > > set to the number of CPUs. Assuming good hashing of the annotation > > (done based on the pointer), this means in order to contend there > > needs to be more threads than CPUs, which is not currently true in any > > perf command. Were contention an issue it is straightforward to > > increase the number of shards in the mutex. > > > > On my Debian/glibc based machine, this reduces the size of struct > > annotation from 136 bytes to 96 bytes, or nearly 30%. > > That's quite a good improvement given the number of symbols > we can have in a report session! > > > > > Signed-off-by: Ian Rogers > > --- > > [SNIP] > > @@ -1291,17 +1292,64 @@ int disasm_line__scnprintf(struct disasm_line *= dl, char *bf, size_t size, bool r > > return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_nam= e); > > } > > > > -void annotation__init(struct annotation *notes) > > +void annotation__exit(struct annotation *notes) > > { > > - mutex_init(¬es->lock); > > + annotated_source__delete(notes->src); > > } > > > > -void annotation__exit(struct annotation *notes) > > +static struct sharded_mutex *sharded_mutex; > > + > > +static void annotation__init_sharded_mutex(void) > > { > > - annotated_source__delete(notes->src); > > - mutex_destroy(¬es->lock); > > + /* As many mutexes as there are CPUs. */ > > + sharded_mutex =3D sharded_mutex__new(cpu__max_present_cpu().cpu= ); > > +} > > + > > +static size_t annotation__hash(const struct annotation *notes) > > +{ > > + return ((size_t)notes) >> 4; > > But I'm afraid it might create more contention depending on the > malloc implementation. If it always returns 128-byte (or 256) > aligned memory for this struct then it could always collide in the > slot 0 if the number of CPU is 8 or less, right? Right, I think we can use a secondary hash and hashmap.h has one lying around for us: https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/tree/tools/p= erf/util/hashmap.h?h=3Dtmp.perf-tools-next#n15 it will mean that the sharded locks will need to be a power of 2 capacity. I'll work on a v2. Fwiw, the hash of a pointer for collection like those in abseil is just the pointer value, so I'll drop the shift to remove the low-bits once I'm using a more expensive hash function. Thanks, Ian > Thanks, > Namhyung > > > > } > > > > +static struct mutex *annotation__get_mutex(const struct annotation *no= tes) > > +{ > > + static pthread_once_t once =3D PTHREAD_ONCE_INIT; > > + > > + pthread_once(&once, annotation__init_sharded_mutex); > > + if (!sharded_mutex) > > + return NULL; > > + > > + return sharded_mutex__get_mutex(sharded_mutex, annotation__hash= (notes)); > > +} > > + > > +void annotation__lock(struct annotation *notes) > > + NO_THREAD_SAFETY_ANALYSIS > > +{ > > + struct mutex *mutex =3D annotation__get_mutex(notes); > > + > > + if (mutex) > > + mutex_lock(mutex); > > +} > > + > > +void annotation__unlock(struct annotation *notes) > > + NO_THREAD_SAFETY_ANALYSIS > > +{ > > + struct mutex *mutex =3D annotation__get_mutex(notes); > > + > > + if (mutex) > > + mutex_unlock(mutex); > > +} > > + > > +bool annotation__trylock(struct annotation *notes) > > +{ > > + struct mutex *mutex =3D annotation__get_mutex(notes); > > + > > + if (!mutex) > > + return false; > > + > > + return mutex_trylock(mutex); > > +} > > + > > + > > static void annotation_line__add(struct annotation_line *al, struct li= st_head *head) > > { > > list_add_tail(&al->node, head);