From patchwork Fri Apr 25 14:55:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Serhei Makarov X-Patchwork-Id: 111019 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E54E0385781A for ; Fri, 25 Apr 2025 14:57:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E54E0385781A Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=serhei.io header.i=@serhei.io header.a=rsa-sha256 header.s=fm3 header.b=QngLU2FG; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=PCjJomKI X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from fout-a2-smtp.messagingengine.com (fout-a2-smtp.messagingengine.com [103.168.172.145]) by sourceware.org (Postfix) with ESMTPS id 9A08D3858406 for ; Fri, 25 Apr 2025 14:56:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A08D3858406 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=serhei.io Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=serhei.io ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9A08D3858406 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=103.168.172.145 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745592981; cv=none; b=ZiiG/d3gZKijqwOywS16X+r3bA4fZvS5681swm+ygVa7zYR3IGl66Gey2hqJ+BEeuDNUTBmtGWLz/Yvq4fZ7467g9CFk3i3b9zb7lVDtY0+l+0V0i3SNWdvmc7ayRoW5el8dW6ARGSY3HRl8HWqCOvQHIDpKNoU2t9D+Olm00mY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745592981; c=relaxed/simple; bh=nv4kwmxyCmTCmoRQaYsLTKneKVbIVCdr6XRsEc2CukY=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=E3U19XIlk3ixwejg/JdnKxswhBiLiDgrV/a7V+rMNzfZl7HCOJtqT3SrRtXFbWxTKCcXjOnwUuTu8v1t1u8OfghuaYluNYdfOpU6AFk7/LYqPOmbnrqj+Nbl++gO21/M7eWx4F0cJ5gVSP+oSc0Uk2eOS4pTA8tigVX3WaDmnnM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A08D3858406 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfout.phl.internal (Postfix) with ESMTP id 6D4FF13801E7; Fri, 25 Apr 2025 10:56:21 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-11.internal (MEProxy); Fri, 25 Apr 2025 10:56:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=serhei.io; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm3; t=1745592981; x= 1745679381; bh=eqKb4HsTicR9Xqav3flzZIS3Z9/fLIKysghiJVK5thQ=; b=Q ngLU2FG6YAy4NBkJz7pRNpK9QEhIIEwAVcMrmR1/x8ne1OtXQ8NHlyp7OQTOIGe9 i4nsGeu+Z0yewWnGXrKrMb7KtJ3gRshtyu54lyU0BnNbZh/e6zNd3W4Kw0QYEG6o w6qu20P/29OHf9uOAUeilot1pmY4ix8mIe4MmOn+EYqRBvkAOcROdngBVzuW3Huw f9mclQr4Ut+j+TspE6ZwLAImNOuSzENACdeLiDMUqrrzPBpodyMzU7OnHzDR8VNe 4RApkUypxQaOtitcBeqnKxMEbcIb4Dkjzs9y3Bj15nrl3E1o5DGfeFbEsV4ul9o3 T66d0+oH5PKIguHtWSegA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1745592981; x=1745679381; bh=e qKb4HsTicR9Xqav3flzZIS3Z9/fLIKysghiJVK5thQ=; b=PCjJomKI1G8du2CB4 27Ii5HnvC1O1RGfAH6d7pynR2a+mIGiHnLstpnjlGetmgsIDfVFWu1YC8zfRSuY/ aP6M2dP9JLQniiivbMgc2vHB6Kps6r7jhjts/Lc48iyS/f4Uz2PRLvKlqthyRmHr bptdwNXEkxAsBtWJJnxaZc5sutRzQwWBBwMzN0BvogQjBu3GJpMdWWi8f0TdHvYi +b0OFaL8HUDzuqQA077Z3su6XXZekFTpFD0CeyItqFLixHNRXsIhgv8i1OGLt51c CgIuhqFhNryHUJNsNZGw1YhIRcO8Stptkg9kljVp3hoKQIMkljEZZnJQ/fAYYpw4 bPv7Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvhedvieefucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefuvghrhhgvihcuofgrkhgr rhhovhcuoehsvghrhhgvihesshgvrhhhvghirdhioheqnecuggftrfgrthhtvghrnheptd dtheeljefhjefggefgteegveetfeetudejkeduvdeigfffjeduveffgeeltddunecuffho mhgrihhnpehgnhhurdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomhepshgvrhhhvghisehsvghrhhgvihdrihhopdhnsggprhgtphhtthho pedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopegvlhhfuhhtihhlshdquggvvh gvlhesshhouhhrtggvfigrrhgvrdhorhhgpdhrtghpthhtohepshgvrhhhvghisehsvghr hhgvihdrihho X-ME-Proxy: Feedback-ID: i572946fc:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 25 Apr 2025 10:56:20 -0400 (EDT) From: Serhei Makarov To: elfutils-devel@sourceware.org Cc: Serhei Makarov Subject: [PATCH v6 05/12] libdwfl_stacktrace [5/12]: introduce Dwflst_Process_Tracker Date: Fri, 25 Apr 2025 10:55:47 -0400 Message-ID: <20250425145554.383010-5-serhei@serhei.io> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250425145554.383010-1-serhei@serhei.io> References: <20250425145554.383010-1-serhei@serhei.io> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces~patchwork=sourceware.org@sourceware.org Changes for v4: - Separate out libdwfl_stacktrace, as requested. * * * New data structure to coordinate caching Elf data among multiple Dwfl structs attached to different processes. Meant to reduce the overhead for profilers that use elfutils for unwinding. The caching is well-justified, as the prior approach (e.g. in eu-stacktrace, sysprof-live-unwinder) of creating a separate Dwfl per process was wildly redundant, opening ~hundreds of file descriptors just for each common library such as libc.so and leaking the data. Initial patch just introduces the struct, to be filled in by the rest of the patch series. * libdwfl_stacktrace/libdwfl_stacktrace.h (Dwflst_Process_Tracker): New struct. (dwflst_tracker_begin): New function. (dwflst_tracker_dwfl_begin): New function. (dwflst_tracker_end): New function. * libdw/libdw.map: Add new functions. * libdwfl_stacktrace/libdwfl_stacktraceP.h (struct Dwflst_Process_Tracker): New struct. * libdwfl/libdwflP.h (Dwflst_Process_Tracker): Typedef forward decl. (struct Dwfl): Add 'tracker' field. * libdwfl_stacktrace/Makefile.am (libdwfl_stacktrace_a_SOURCES): Add dwflst_process_tracker.c. * libdwfl_stacktrace/dwflst_process_tracker.c: New file. (dwflst_tracker_begin): Initialize the tracker. (dwflst_tracker_dwfl_begin): Initialize Dwfl * with specified tracker. (dwflst_tracker_end): Deallocate the tracker. --- libdw/libdw.map | 4 ++ libdwfl/libdwflP.h | 4 ++ libdwfl_stacktrace/Makefile.am | 3 +- libdwfl_stacktrace/dwflst_process_tracker.c | 66 +++++++++++++++++++++ libdwfl_stacktrace/libdwfl_stacktrace.h | 20 +++++++ libdwfl_stacktrace/libdwfl_stacktraceP.h | 6 ++ 6 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 libdwfl_stacktrace/dwflst_process_tracker.c diff --git a/libdw/libdw.map b/libdw/libdw.map index 4f3fe6ba..fb69a62a 100644 --- a/libdw/libdw.map +++ b/libdw/libdw.map @@ -396,4 +396,8 @@ ELFUTILS_0.193 { ELFUTILS_0.193_EXPERIMENTAL { global: dwflst_perf_sample_preferred_regs_mask; + dwflst_perf_sample_preferred_regs_mask; + dwflst_tracker_begin; + dwflst_tracker_dwfl_begin; + dwflst_tracker_end; }; diff --git a/libdwfl/libdwflP.h b/libdwfl/libdwflP.h index 150d26f9..8e6157ea 100644 --- a/libdwfl/libdwflP.h +++ b/libdwfl/libdwflP.h @@ -111,9 +111,13 @@ struct Dwfl_User_Core int fd; /* close if >= 0. */ }; +/* forward decl from ../libdwfl_stacktrace/ */ +typedef struct Dwflst_Process_Tracker Dwflst_Process_Tracker; + struct Dwfl { const Dwfl_Callbacks *callbacks; + Dwflst_Process_Tracker *tracker; #ifdef ENABLE_LIBDEBUGINFOD debuginfod_client *debuginfod; #endif diff --git a/libdwfl_stacktrace/Makefile.am b/libdwfl_stacktrace/Makefile.am index 6364c292..d57431c0 100644 --- a/libdwfl_stacktrace/Makefile.am +++ b/libdwfl_stacktrace/Makefile.am @@ -40,7 +40,8 @@ noinst_LIBRARIES += libdwfl_stacktrace_pic.a pkginclude_HEADERS = libdwfl_stacktrace.h -libdwfl_stacktrace_a_SOURCES = dwflst_perf_frame.c +libdwfl_stacktrace_a_SOURCES = dwflst_process_tracker.c \ + dwflst_perf_frame.c libdwfl_stacktrace = $(libdw) libdw = ../libdw/libdw.so diff --git a/libdwfl_stacktrace/dwflst_process_tracker.c b/libdwfl_stacktrace/dwflst_process_tracker.c new file mode 100644 index 00000000..057c9f7a --- /dev/null +++ b/libdwfl_stacktrace/dwflst_process_tracker.c @@ -0,0 +1,66 @@ +/* Track multiple Dwfl structs for multiple processes. + Copyright (C) 2025, Red Hat, Inc. + This file is part of elfutils. + + This file is free software; you can redistribute it and/or modify + it under the terms of either + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at + your option) any later version + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at + your option) any later version + + or both in parallel, as here. + + elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "libdwfl_stacktraceP.h" + +Dwflst_Process_Tracker *dwflst_tracker_begin (const Dwfl_Callbacks *callbacks) +{ + Dwflst_Process_Tracker *tracker = calloc (1, sizeof *tracker); + if (tracker == NULL) + { + __libdwfl_seterrno (DWFL_E_NOMEM); + return tracker; + } + + tracker->callbacks = callbacks; + return tracker; +} + +Dwfl *dwflst_tracker_dwfl_begin (Dwflst_Process_Tracker *tracker) +{ + Dwfl *dwfl = INTUSE(dwfl_begin) (tracker->callbacks); + if (dwfl == NULL) + return dwfl; + + /* TODO: Could also share dwfl->debuginfod, but thread-safely? */ + dwfl->tracker = tracker; + return dwfl; +} + +void dwflst_tracker_end (Dwflst_Process_Tracker *tracker) +{ + if (tracker == NULL) + return; + + /* TODO: Call dwfl_end for each Dwfl connected to this tracker. */ + free (tracker); +} diff --git a/libdwfl_stacktrace/libdwfl_stacktrace.h b/libdwfl_stacktrace/libdwfl_stacktrace.h index 564f504a..f3a82d18 100644 --- a/libdwfl_stacktrace/libdwfl_stacktrace.h +++ b/libdwfl_stacktrace/libdwfl_stacktrace.h @@ -41,6 +41,26 @@ extern "C" { #endif +/* Keeps track of and caches Elf structs across multiple libdwfl + sessions corresponding to different processes. */ +typedef struct Dwflst_Process_Tracker Dwflst_Process_Tracker; + + +/* Initialize a new tracker for multiple libdwfl sessions. Since Elf + data will shared between the libdwfl sessions, each Dwfl must use + the same Dwfl_Callbacks CALLBACKS provided when the tracker is + created. */ +extern Dwflst_Process_Tracker *dwflst_tracker_begin (const Dwfl_Callbacks *callbacks) + __nonnull_attribute__ (1); + +/* Create a new Dwfl linked to this tracker. */ +extern Dwfl *dwflst_tracker_dwfl_begin (Dwflst_Process_Tracker *tracker) + __nonnull_attribute__ (1); + +/* End all sessions with this tracker. */ +extern void dwflst_tracker_end (Dwflst_Process_Tracker *tracker); + + /* XXX dwflst_perf_sample_getframes to be added in subsequent patch */ /* Returns the linux perf_events register mask describing a set of diff --git a/libdwfl_stacktrace/libdwfl_stacktraceP.h b/libdwfl_stacktrace/libdwfl_stacktraceP.h index fe6945fc..9313176c 100644 --- a/libdwfl_stacktrace/libdwfl_stacktraceP.h +++ b/libdwfl_stacktrace/libdwfl_stacktraceP.h @@ -33,4 +33,10 @@ #include "libdwflP.h" +struct Dwflst_Process_Tracker +{ + const Dwfl_Callbacks *callbacks; + /* ... */ +}; + #endif /* libdwfl_stacktraceP.h */