From nobody Mon Sep 16 19:45:37 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 691B717578; Fri, 26 Jul 2024 01:57:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721959045; cv=none; b=UHu8ghADBpPLydxm2rqKWkitvydBX5t0aywngfc/Lu8lwYaMf1eYFNc4CqngeooORMLhZux6GPVD6Ty9awWO7pnxy/Arl5M2Fk1s7JqbVDTpRI113yiwec8tOQuG8b3mwr2aycsyXr2f9fG17zsw9P14ZqtZpkyVDIgRyn2DraA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721959045; c=relaxed/simple; bh=oChcQZZT8W/cZSh69mW2NQ3t5gRenM6b+5iA03yhxDE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=gpiySCu0izA1PCxAKbfYRc0Nh2y6sktIuESPZpEB/nZTm2wLmnzia80qUYEnaMRgV2fBElej6u+AQj9ZavCiml0fXxrTHWnTIibzER8tClNoJgBVUj2B5uIc3F/mruoNgRtwQdNvbdZTCJZPHyxnrAebZQbCHewAKwyvYrG0pyA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VtASLlCn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VtASLlCn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96BFCC116B1; Fri, 26 Jul 2024 01:57:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721959044; bh=oChcQZZT8W/cZSh69mW2NQ3t5gRenM6b+5iA03yhxDE=; h=From:To:Cc:Subject:Date:From; b=VtASLlCnBxZEp8cFwUh4F+2tIAuPeXnfSglNX6ZhD7mYcXHAwjf4jMYWp3659iJkk 6+HHupkxOo24Gd/T+urrlnBaHun0KsySGtLLqOFDQ+pk68I5VShQfLenU9ATlf15tk cSallnfPZ7fJKR+uDPKcz5j0fhK8GBOWL83R3hN0WbSLyYcVX/MtBy2meKbjKsJhNA /xxZT1DRN40sEEnBdDWXdGnnh6LtM+NUQg4FACNwtRul/G22d7iezDtOJSeJdh+bJ0 XzZGojFPaPS9carqDmLkRk8k2+0uorFzLhYWFiee12dV/QZ0dPyMQXd/jM0yyuWayX V5ShcA56+3a6A== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH v2] perf annotate: Cache debuginfo for data type profiling Date: Thu, 25 Jul 2024 18:57:23 -0700 Message-ID: <20240726015723.1329937-1-namhyung@kernel.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In find_data_type(), it creates and deletes a debug info whenver it tries to find data type for a sample. This is inefficient and it most likely accesses the same binary again and again. Let's add a single entry cache the debug info structure for the last DSO. Depending on sample data, it usually gives me 2~3x (and sometimes more) speed ups. Note that this will introduce a little difference in the output due to the order of checking stack operations. It used to check the stack ops before checking the availability of debug info but I moved it after the symbol check. So it'll report stack operations in DSOs without debug info as unknown. But I think it's ok and better to have the checking near the caching logic. Signed-off-by: Namhyung Kim --- v2) add an assertion for the single-threaded case. tools/perf/util/annotate-data.c | 15 ++------------ tools/perf/util/annotate-data.h | 2 +- tools/perf/util/annotate.c | 35 +++++++++++++++++++++++++++++++++ tools/perf/util/annotate.h | 2 ++ tools/perf/util/session.c | 2 ++ 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 734acdd8c4b7..f125ac5f0bda 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -1345,16 +1345,9 @@ static int find_data_type_die(struct data_loc_info *= dloc, Dwarf_Die *type_die) */ struct annotated_data_type *find_data_type(struct data_loc_info *dloc) { - struct annotated_data_type *result =3D NULL; struct dso *dso =3D map__dso(dloc->ms->map); Dwarf_Die type_die; =20 - dloc->di =3D debuginfo__new(dso__long_name(dso)); - if (dloc->di =3D=3D NULL) { - pr_debug_dtp("cannot get the debug info\n"); - return NULL; - } - /* * The type offset is the same as instruction offset by default. * But when finding a global variable, the offset won't be valid. @@ -1364,13 +1357,9 @@ struct annotated_data_type *find_data_type(struct da= ta_loc_info *dloc) dloc->fbreg =3D -1; =20 if (find_data_type_die(dloc, &type_die) < 0) - goto out; - - result =3D dso__findnew_data_type(dso, &type_die); + return NULL; =20 -out: - debuginfo__delete(dloc->di); - return result; + return dso__findnew_data_type(dso, &type_die); } =20 static int alloc_data_type_histograms(struct annotated_data_type *adt, int= nr_entries) diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-dat= a.h index 992b7ce4bd11..37a1a3b68e0b 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -123,9 +123,9 @@ struct data_loc_info { u64 var_addr; u8 cpumode; struct annotated_op_loc *op; + struct debuginfo *di; =20 /* These are used internally */ - struct debuginfo *di; int fbreg; bool fb_cfa; =20 diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index a2ee4074f768..2ef119539336 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -25,6 +25,7 @@ #include "srcline.h" #include "units.h" #include "debug.h" +#include "debuginfo.h" #include "annotate.h" #include "annotate-data.h" #include "evsel.h" @@ -2326,6 +2327,20 @@ u64 annotate_calc_pcrel(struct map_symbol *ms, u64 i= p, int offset, return map__rip_2objdump(ms->map, addr); } =20 +static struct debuginfo_cache { + struct dso *dso; + struct debuginfo *dbg; +} di_cache; + +void debuginfo_cache__delete(void) +{ + dso__put(di_cache.dso); + di_cache.dso =3D NULL; + + debuginfo__delete(di_cache.dbg); + di_cache.dbg =3D NULL; +} + /** * hist_entry__get_data_type - find data type for given hist entry * @he: hist entry @@ -2360,6 +2375,25 @@ struct annotated_data_type *hist_entry__get_data_typ= e(struct hist_entry *he) return NULL; } =20 + /* + * It needs to protect di_cache as well as data_types and global_vars in + * DSO when enabling multi-threading. + */ + assert(perf_singlethreaded); + + if (map__dso(ms->map) !=3D di_cache.dso) { + dso__put(di_cache.dso); + di_cache.dso =3D dso__get(map__dso(ms->map)); + + debuginfo__delete(di_cache.dbg); + di_cache.dbg =3D debuginfo__new(dso__long_name(di_cache.dso)); + } + + if (di_cache.dbg =3D=3D NULL) { + ann_data_stat.no_dbginfo++; + return NULL; + } + /* Make sure it has the disasm of the function */ if (symbol__annotate(ms, evsel, &arch) < 0) { ann_data_stat.no_insn++; @@ -2404,6 +2438,7 @@ struct annotated_data_type *hist_entry__get_data_type= (struct hist_entry *he) .ip =3D ms->sym->start + dl->al.offset, .cpumode =3D he->cpumode, .op =3D op_loc, + .di =3D di_cache.dbg, }; =20 if (!op_loc->mem_ref && op_loc->segment =3D=3D INSN_SEG_NONE) diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 9ba772f46270..27d9540604ef 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -543,4 +543,6 @@ struct annotated_basic_block { int annotate_get_basic_blocks(struct symbol *sym, s64 src, s64 dst, struct list_head *head); =20 +void debuginfo_cache__delete(void); + #endif /* __PERF_ANNOTATE_H */ diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 5596bed1b8c8..f9072e003367 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -36,6 +36,7 @@ #include "util.h" #include "arch/common.h" #include "units.h" +#include "annotate.h" #include =20 #ifdef HAVE_ZSTD_SUPPORT @@ -304,6 +305,7 @@ void perf_session__delete(struct perf_session *session) return; auxtrace__free(session); auxtrace_index__free(&session->auxtrace_index); + debuginfo_cache__delete(); perf_session__destroy_kernel_maps(session); perf_decomp__release_events(session->decomp_data.decomp); perf_env__exit(&session->header.env); --=20 2.46.0.rc1.232.g9752f9e123-goog