From nobody Wed Dec 17 12:45:07 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60E21C4167B for ; Mon, 27 Nov 2023 22:24:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233799AbjK0WYm (ORCPT ); Mon, 27 Nov 2023 17:24:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233755AbjK0WYX (ORCPT ); Mon, 27 Nov 2023 17:24:23 -0500 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 55D40358E for ; Mon, 27 Nov 2023 14:11:01 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ce16bc121aso45619307b3.1 for ; Mon, 27 Nov 2023 14:11:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123060; x=1701727860; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=FaCf9NnwHHtcnVguWv1S+wqXgvVzGN+3ItIN6S57klU=; b=yU6CxkCrUrLPffYCwnqHBcVEvfRxIqZ+Lc9l0m8QuJe9xEdOVAGUluRO4ZfEY6z6Jj AK2PQWJN21QcDZBxdIPWvyDJSP5qzX1id/4/FUpPoYnXVlwsiUGdpAJ1QqWBIihoOgG+ DvJ8MTRfloufK+Kl/nOR96FAnaFqSoad1J4ZMCG0tbWO1cl0tZW3Ixm5fWnCQwlsLmkB ELTLg1Cg0GA309CgTwkV5aLw5/CnuRvu7+ZjGOgpQG70wc4uEygDJFDqh0/MkomLBK8R MEFADtcoFEpUqtSP79XclQh2LRuJ0+fBJOpoVYiRElU8y9QcAp/AMqAkiifQG9ZJLq1G yUGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123060; x=1701727860; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FaCf9NnwHHtcnVguWv1S+wqXgvVzGN+3ItIN6S57klU=; b=YgCiX66X7rwsIg3nk5SVVB4RogkhAdtMidg0LkBj0hRnXS8rMU3nV+KjF8bWKYlueX MsRc4pexFi+vsAw+zesxaiX2bNOc15tY06DXBpJhBX/TD50WYAC0uMAG8PjgLAXME/Wk wc3RBFXbj1PL+G6Y1fGdkfMjCHFHWzCEpFh6zBZfibp0M3EEIzP9qE1aIcvqugtS9fk6 rp81gMiCw4sjGKJBrMsgHJREi4bCvCS/ARESNClU9i/q/eoRguZn25D9qzckha2QxEbz s+SeQNNuAf4mXYwhp2cxOskMt9/xAm04/SK6AZnN/nRkxOKOxpVutm9aoq1l6YgfpPxn h0jg== X-Gm-Message-State: AOJu0YxYbXXv8dRrxnw3oPA433F5Iezd783FiQKcblBrnxI7RZ1nuCpS k7Ke9g9rvDJYWA1RGZJFBtpWyko9mdvJ X-Google-Smtp-Source: AGHT+IFP5lF8UeRRElzOYV/NAm9G1ggs+Qc0gtZSJgaDG1TUW7CWcmdxg+aKeOpm7voVfxDJmUxIjkRP4oBT X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a05:690c:3102:b0:57a:118a:f31 with SMTP id fb2-20020a05690c310200b0057a118a0f31mr359611ywb.7.1701123059880; Mon, 27 Nov 2023 14:10:59 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:54 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-43-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 42/50] perf dsos: Switch more loops to dsos__for_each_dso From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Switch loops within dsos.c, add a version that isn't locked. Switch some unlocked loops to hold the read lock. Signed-off-by: Ian Rogers --- tools/perf/util/build-id.c | 2 +- tools/perf/util/dsos.c | 258 ++++++++++++++++++++++++------------- tools/perf/util/dsos.h | 8 +- tools/perf/util/machine.c | 8 +- 4 files changed, 174 insertions(+), 102 deletions(-) diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index a6d3c253f19f..864bc26b6b46 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -964,7 +964,7 @@ int perf_session__cache_build_ids(struct perf_session *= session) =20 static bool machine__read_build_ids(struct machine *machine, bool with_hit= s) { - return __dsos__read_build_ids(&machine->dsos, with_hits); + return dsos__read_build_ids(&machine->dsos, with_hits); } =20 bool perf_session__read_build_ids(struct perf_session *session, bool with_= hits) diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index f816927a21ff..b7fbfb877ae3 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -41,38 +41,65 @@ void dsos__exit(struct dsos *dsos) exit_rwsem(&dsos->lock); } =20 -bool __dsos__read_build_ids(struct dsos *dsos, bool with_hits) + +static int __dsos__for_each_dso(struct dsos *dsos, + int (*cb)(struct dso *dso, void *data), + void *data) +{ + struct dso *dso; + + list_for_each_entry(dso, &dsos->head, node) { + int err; + + err =3D cb(dso, data); + if (err) + return err; + } + return 0; +} + +struct dsos__read_build_ids_cb_args { + bool with_hits; + bool have_build_id; +}; + +static int dsos__read_build_ids_cb(struct dso *dso, void *data) { - struct list_head *head =3D &dsos->head; - bool have_build_id =3D false; - struct dso *pos; + struct dsos__read_build_ids_cb_args *args =3D data; struct nscookie nsc; =20 - list_for_each_entry(pos, head, node) { - if (with_hits && !pos->hit && !dso__is_vdso(pos)) - continue; - if (pos->has_build_id) { - have_build_id =3D true; - continue; - } - nsinfo__mountns_enter(pos->nsinfo, &nsc); - if (filename__read_build_id(pos->long_name, &pos->bid) > 0) { - have_build_id =3D true; - pos->has_build_id =3D true; - } else if (errno =3D=3D ENOENT && pos->nsinfo) { - char *new_name =3D dso__filename_with_chroot(pos, pos->long_name); - - if (new_name && filename__read_build_id(new_name, - &pos->bid) > 0) { - have_build_id =3D true; - pos->has_build_id =3D true; - } - free(new_name); + if (args->with_hits && !dso->hit && !dso__is_vdso(dso)) + return 0; + if (dso->has_build_id) { + args->have_build_id =3D true; + return 0; + } + nsinfo__mountns_enter(dso->nsinfo, &nsc); + if (filename__read_build_id(dso->long_name, &dso->bid) > 0) { + args->have_build_id =3D true; + dso->has_build_id =3D true; + } else if (errno =3D=3D ENOENT && dso->nsinfo) { + char *new_name =3D dso__filename_with_chroot(dso, dso->long_name); + + if (new_name && filename__read_build_id(new_name, &dso->bid) > 0) { + args->have_build_id =3D true; + dso->has_build_id =3D true; } - nsinfo__mountns_exit(&nsc); + free(new_name); } + nsinfo__mountns_exit(&nsc); + return 0; +} =20 - return have_build_id; +bool dsos__read_build_ids(struct dsos *dsos, bool with_hits) +{ + struct dsos__read_build_ids_cb_args args =3D { + .with_hits =3D with_hits, + .have_build_id =3D false, + }; + + dsos__for_each_dso(dsos, dsos__read_build_ids_cb, &args); + return args.have_build_id; } =20 static int __dso__cmp_long_name(const char *long_name, struct dso_id *id, = struct dso *b) @@ -105,6 +132,7 @@ struct dso *__dsos__findnew_link_by_longname_id(struct = rb_root *root, struct dso =20 if (!name) name =3D dso->long_name; + /* * Find node with the matching name */ @@ -185,17 +213,40 @@ static struct dso *__dsos__findnew_by_longname_id(str= uct rb_root *root, const ch return __dsos__findnew_link_by_longname_id(root, NULL, name, id); } =20 +struct dsos__find_id_cb_args { + const char *name; + struct dso_id *id; + struct dso *res; +}; + +static int dsos__find_id_cb(struct dso *dso, void *data) +{ + struct dsos__find_id_cb_args *args =3D data; + + if (__dso__cmp_short_name(args->name, args->id, dso) =3D=3D 0) { + args->res =3D dso__get(dso); + return 1; + } + return 0; + +} + static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, st= ruct dso_id *id, bool cmp_short) { - struct dso *pos; + struct dso *res; =20 if (cmp_short) { - list_for_each_entry(pos, &dsos->head, node) - if (__dso__cmp_short_name(name, id, pos) =3D=3D 0) - return dso__get(pos); - return NULL; + struct dsos__find_id_cb_args args =3D { + .name =3D name, + .id =3D id, + .res =3D NULL, + }; + + __dsos__for_each_dso(dsos, dsos__find_id_cb, &args); + return args.res; } - return __dsos__findnew_by_longname_id(&dsos->root, name, id); + res =3D __dsos__findnew_by_longname_id(&dsos->root, name, id); + return res; } =20 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short) @@ -275,48 +326,74 @@ struct dso *dsos__findnew_id(struct dsos *dsos, const= char *name, struct dso_id return dso; } =20 -size_t __dsos__fprintf_buildid(struct dsos *dsos, FILE *fp, - bool (skip)(struct dso *dso, int parm), int parm) -{ - struct list_head *head =3D &dsos->head; - struct dso *pos; - size_t ret =3D 0; +struct dsos__fprintf_buildid_cb_args { + FILE *fp; + bool (*skip)(struct dso *dso, int parm); + int parm; + size_t ret; +}; =20 - list_for_each_entry(pos, head, node) { - char sbuild_id[SBUILD_ID_SIZE]; +static int dsos__fprintf_buildid_cb(struct dso *dso, void *data) +{ + struct dsos__fprintf_buildid_cb_args *args =3D data; + char sbuild_id[SBUILD_ID_SIZE]; =20 - if (skip && skip(pos, parm)) - continue; - build_id__sprintf(&pos->bid, sbuild_id); - ret +=3D fprintf(fp, "%-40s %s\n", sbuild_id, pos->long_name); - } - return ret; + if (args->skip && args->skip(dso, args->parm)) + return 0; + build_id__sprintf(&dso->bid, sbuild_id); + args->ret +=3D fprintf(args->fp, "%-40s %s\n", sbuild_id, dso->long_name); + return 0; } =20 -size_t __dsos__fprintf(struct dsos *dsos, FILE *fp) +size_t dsos__fprintf_buildid(struct dsos *dsos, FILE *fp, + bool (*skip)(struct dso *dso, int parm), int parm) { - struct list_head *head =3D &dsos->head; - struct dso *pos; - size_t ret =3D 0; + struct dsos__fprintf_buildid_cb_args args =3D { + .fp =3D fp, + .skip =3D skip, + .parm =3D parm, + .ret =3D 0, + }; + + dsos__for_each_dso(dsos, dsos__fprintf_buildid_cb, &args); + return args.ret; +} =20 - list_for_each_entry(pos, head, node) { - ret +=3D dso__fprintf(pos, fp); - } +struct dsos__fprintf_cb_args { + FILE *fp; + size_t ret; +}; =20 - return ret; +static int dsos__fprintf_cb(struct dso *dso, void *data) +{ + struct dsos__fprintf_cb_args *args =3D data; + + args->ret +=3D dso__fprintf(dso, args->fp); + return 0; } =20 -int __dsos__hit_all(struct dsos *dsos) +size_t dsos__fprintf(struct dsos *dsos, FILE *fp) { - struct list_head *head =3D &dsos->head; - struct dso *pos; + struct dsos__fprintf_cb_args args =3D { + .fp =3D fp, + .ret =3D 0, + }; =20 - list_for_each_entry(pos, head, node) - pos->hit =3D true; + dsos__for_each_dso(dsos, dsos__fprintf_cb, &args); + return args.ret; +} =20 +static int dsos__hit_all_cb(struct dso *dso, void *data __maybe_unused) +{ + dso->hit =3D true; return 0; } =20 +int dsos__hit_all(struct dsos *dsos) +{ + return dsos__for_each_dso(dsos, dsos__hit_all_cb, NULL); +} + struct dso *dsos__findnew_module_dso(struct dsos *dsos, struct machine *machine, struct kmod_path *m, @@ -342,49 +419,44 @@ struct dso *dsos__findnew_module_dso(struct dsos *dso= s, return dso; } =20 -struct dso *dsos__find_kernel_dso(struct dsos *dsos) +static int dsos__find_kernel_dso_cb(struct dso *dso, void *data) { - struct dso *dso, *res =3D NULL; + struct dso **res =3D data; + /* + * The cpumode passed to is_kernel_module is not the cpumode of *this* + * event. If we insist on passing correct cpumode to is_kernel_module, + * we should record the cpumode when we adding this dso to the linked + * list. + * + * However we don't really need passing correct cpumode. We know the + * correct cpumode must be kernel mode (if not, we should not link it + * onto kernel_dsos list). + * + * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. + * is_kernel_module() treats it as a kernel cpumode. + */ + if (!dso->kernel || + is_kernel_module(dso->long_name, PERF_RECORD_MISC_CPUMODE_UNKNOWN)) + return 0; =20 - down_read(&dsos->lock); - list_for_each_entry(dso, &dsos->head, node) { - /* - * The cpumode passed to is_kernel_module is not the cpumode of - * *this* event. If we insist on passing correct cpumode to - * is_kernel_module, we should record the cpumode when we adding - * this dso to the linked list. - * - * However we don't really need passing correct cpumode. We - * know the correct cpumode must be kernel mode (if not, we - * should not link it onto kernel_dsos list). - * - * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. - * is_kernel_module() treats it as a kernel cpumode. - */ - if (!dso->kernel || - is_kernel_module(dso->long_name, - PERF_RECORD_MISC_CPUMODE_UNKNOWN)) - continue; + *res =3D dso__get(dso); + return 1; +} =20 - res =3D dso__get(dso); - break; - } - up_read(&dsos->lock); +struct dso *dsos__find_kernel_dso(struct dsos *dsos) +{ + struct dso *res =3D NULL; + + dsos__for_each_dso(dsos, dsos__find_kernel_dso_cb, &res); return res; } =20 int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void = *data), void *data) { - struct dso *dso; + int err; =20 down_read(&dsos->lock); - list_for_each_entry(dso, &dsos->head, node) { - int err; - - err =3D cb(dso, data); - if (err) - return err; - } + err =3D __dsos__for_each_dso(dsos, cb, data); up_read(&dsos->lock); - return 0; + return err; } diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h index 317a263f0e37..50bd51523475 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -33,16 +33,16 @@ struct dso *dsos__find(struct dsos *dsos, const char *n= ame, bool cmp_short); =20 struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, struct d= so_id *id); =20 -bool __dsos__read_build_ids(struct dsos *dsos, bool with_hits); +bool dsos__read_build_ids(struct dsos *dsos, bool with_hits); =20 struct dso *__dsos__findnew_link_by_longname_id(struct rb_root *root, stru= ct dso *dso, const char *name, struct dso_id *id); =20 -size_t __dsos__fprintf_buildid(struct dsos *dsos, FILE *fp, +size_t dsos__fprintf_buildid(struct dsos *dsos, FILE *fp, bool (skip)(struct dso *dso, int parm), int parm); -size_t __dsos__fprintf(struct dsos *dsos, FILE *fp); +size_t dsos__fprintf(struct dsos *dsos, FILE *fp); =20 -int __dsos__hit_all(struct dsos *dsos); +int dsos__hit_all(struct dsos *dsos); =20 struct dso *dsos__findnew_module_dso(struct dsos *dsos, struct machine *ma= chine, struct kmod_path *m, const char *filename); diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 6505f8c2cecc..3646d4593502 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -853,11 +853,11 @@ static struct map *machine__addnew_module_map(struct = machine *machine, u64 start size_t machines__fprintf_dsos(struct machines *machines, FILE *fp) { struct rb_node *nd; - size_t ret =3D __dsos__fprintf(&machines->host.dsos, fp); + size_t ret =3D dsos__fprintf(&machines->host.dsos, fp); =20 for (nd =3D rb_first_cached(&machines->guests); nd; nd =3D rb_next(nd)) { struct machine *pos =3D rb_entry(nd, struct machine, rb_node); - ret +=3D __dsos__fprintf(&pos->dsos, fp); + ret +=3D dsos__fprintf(&pos->dsos, fp); } =20 return ret; @@ -866,7 +866,7 @@ size_t machines__fprintf_dsos(struct machines *machines= , FILE *fp) size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp, bool (skip)(struct dso *dso, int parm), int parm) { - return __dsos__fprintf_buildid(&m->dsos, fp, skip, parm); + return dsos__fprintf_buildid(&m->dsos, fp, skip, parm); } =20 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp, @@ -3204,5 +3204,5 @@ bool machine__is_lock_function(struct machine *machin= e, u64 addr) =20 int machine__hit_all_dsos(struct machine *machine) { - return __dsos__hit_all(&machine->dsos); + return dsos__hit_all(&machine->dsos); } --=20 2.43.0.rc1.413.gea7ed67945-goog