From nobody Thu May 7 19:05:33 2026 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 DA677C433F5 for ; Fri, 20 May 2022 13:24:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349782AbiETNY0 (ORCPT ); Fri, 20 May 2022 09:24:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349766AbiETNYW (ORCPT ); Fri, 20 May 2022 09:24:22 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F11EE149D83 for ; Fri, 20 May 2022 06:24:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653053060; x=1684589060; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Iv29HntL/Q08w5g4iDaOvR5G0CNgN0/IpP8ZjjHsRRU=; b=FDGICByjzDnMRB6hA0AWjIPhiSkx3Dq6XcocEkHbIAvn7BryfrwNa4lx 5Ef9DYKMqyRHSSYoZYIhEO3VadnGiov3A+2Wxm3NEQ5lxWLQyoT24zDUb DTRPzlmmJ9AlCbMPILEfYHiBuVp7WXTkk3GD9rDyzGcCgOuNaZ9aZiOjo VP4LLqymZ3a7ByzJ/s0ywe6pAHrJUuNP9s/b52g0XEhVkTTcn8zZi6uZN xnaiybNVR6NXUZwNXVWsi9GhRKK8K6MXziodbO40XbVC7HBnAzMr+753q vY57VXf6wEr1RyK1VMipvePth4VXJwKLrbpuAmd7pRCRgHFQNJzLWLX4H A==; X-IronPort-AV: E=McAfee;i="6400,9594,10353"; a="335661010" X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="335661010" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:20 -0700 X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="715530808" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.38.124]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:19 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org Subject: [PATCH 1/5] perf header: Add ability to keep feature sections Date: Fri, 20 May 2022 16:24:00 +0300 Message-Id: <20220520132404.25853-2-adrian.hunter@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220520132404.25853-1-adrian.hunter@intel.com> References: <20220520132404.25853-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Many feature sections should not be re-written during perf inject. In preparation to support that, add callbacks that a tool can use to copy a feature section from elsewhere. perf inject will use this facility to copy features sections from the input file. Signed-off-by: Adrian Hunter Acked-by: Jiri Olsa --- tools/perf/util/header.c | 54 ++++++++++++++++++++++++++++++++++------ tools/perf/util/header.h | 10 ++++++++ 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index a27132e5a5ef..b0c57a130d1e 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -3462,9 +3462,22 @@ int perf_header__fprintf_info(struct perf_session *s= ession, FILE *fp, bool full) return 0; } =20 +struct header_fw { + struct feat_writer fw; + struct feat_fd *ff; +}; + +static int feat_writer_cb(struct feat_writer *fw, void *buf, size_t sz) +{ + struct header_fw *h =3D container_of(fw, struct header_fw, fw); + + return do_write(h->ff, buf, sz); +} + static int do_write_feat(struct feat_fd *ff, int type, struct perf_file_section **p, - struct evlist *evlist) + struct evlist *evlist, + struct feat_copier *fc) { int err; int ret =3D 0; @@ -3478,7 +3491,23 @@ static int do_write_feat(struct feat_fd *ff, int typ= e, =20 (*p)->offset =3D lseek(ff->fd, 0, SEEK_CUR); =20 - err =3D feat_ops[type].write(ff, evlist); + /* + * Hook to let perf inject copy features sections from the input + * file. + */ + if (fc && fc->copy) { + struct header_fw h =3D { + .fw.write =3D feat_writer_cb, + .ff =3D ff, + }; + + /* ->copy() returns 0 if the feature was not copied */ + err =3D fc->copy(fc, type, &h.fw); + } else { + err =3D 0; + } + if (!err) + err =3D feat_ops[type].write(ff, evlist); if (err < 0) { pr_debug("failed to write feature %s\n", feat_ops[type].name); =20 @@ -3494,7 +3523,8 @@ static int do_write_feat(struct feat_fd *ff, int type, } =20 static int perf_header__adds_write(struct perf_header *header, - struct evlist *evlist, int fd) + struct evlist *evlist, int fd, + struct feat_copier *fc) { int nr_sections; struct feat_fd ff; @@ -3523,7 +3553,7 @@ static int perf_header__adds_write(struct perf_header= *header, lseek(fd, sec_start + sec_size, SEEK_SET); =20 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { - if (do_write_feat(&ff, feat, &p, evlist)) + if (do_write_feat(&ff, feat, &p, evlist, fc)) perf_header__clear_feat(header, feat); } =20 @@ -3561,9 +3591,10 @@ int perf_header__write_pipe(int fd) return 0; } =20 -int perf_session__write_header(struct perf_session *session, - struct evlist *evlist, - int fd, bool at_exit) +static int perf_session__do_write_header(struct perf_session *session, + struct evlist *evlist, + int fd, bool at_exit, + struct feat_copier *fc) { struct perf_file_header f_header; struct perf_file_attr f_attr; @@ -3615,7 +3646,7 @@ int perf_session__write_header(struct perf_session *s= ession, header->feat_offset =3D header->data_offset + header->data_size; =20 if (at_exit) { - err =3D perf_header__adds_write(header, evlist, fd); + err =3D perf_header__adds_write(header, evlist, fd, fc); if (err < 0) return err; } @@ -3648,6 +3679,13 @@ int perf_session__write_header(struct perf_session *= session, return 0; } =20 +int perf_session__write_header(struct perf_session *session, + struct evlist *evlist, + int fd, bool at_exit) +{ + return perf_session__do_write_header(session, evlist, fd, at_exit, NULL); +} + static int perf_header__getbuffer64(struct perf_header *header, int fd, void *buf, size_t size) { diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h index 0eb4bc29a5a4..e76ab02d5541 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h @@ -121,6 +121,16 @@ int perf_session__write_header(struct perf_session *se= ssion, int fd, bool at_exit); int perf_header__write_pipe(int fd); =20 +/* feat_writer writes a feature section to output */ +struct feat_writer { + int (*write)(struct feat_writer *fw, void *buf, size_t sz); +}; + +/* feat_copier copies a feature section using feat_writer to output */ +struct feat_copier { + int (*copy)(struct feat_copier *fc, int feat, struct feat_writer *fw); +}; + void perf_header__set_feat(struct perf_header *header, int feat); void perf_header__clear_feat(struct perf_header *header, int feat); bool perf_header__has_feat(const struct perf_header *header, int feat); --=20 2.25.1 From nobody Thu May 7 19:05:33 2026 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 EBD9DC433EF for ; Fri, 20 May 2022 13:24:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344015AbiETNYc (ORCPT ); Fri, 20 May 2022 09:24:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349779AbiETNYY (ORCPT ); Fri, 20 May 2022 09:24:24 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BE551498C4 for ; Fri, 20 May 2022 06:24:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653053062; x=1684589062; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U4srRq5yE5CT++6h1AC80Vn5y1NUKDalEuopQN1pxhA=; b=lIh8zbOYfpveYI2JcgGja+iJ70qqdsf8NIugKDFW+V8G9aXhSGPdzRiI 4BZKhRFTPCGpVEkJA53FaeusLQENuvfe0eT7E+SHKBkQVjLnHemS63Kdt G4ktNtwN4/YmydNiBnEste0Y85Wi0sZtlWCZLSzQxAN9eKlaDBeGQc4R1 w2m9BTUaVtrQRYREyluqOGQGetmSiFyisq56rMOON1rUyAGxrVz4czHKL 9dOjTrIUp7aXEbjPW4zl7VAVZYufGtQoQHIT2BBRqnMWYGhj0iFh4O/Sy ZelV0ko5wfiTPWNPal9+PnXLwEkIAgjVjwXu2JmazHHvTtGR+vU6yKKgS g==; X-IronPort-AV: E=McAfee;i="6400,9594,10353"; a="335661013" X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="335661013" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:22 -0700 X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="715530812" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.38.124]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:20 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org Subject: [PATCH 2/5] libperf: Add preadn() Date: Fri, 20 May 2022 16:24:01 +0300 Message-Id: <20220520132404.25853-3-adrian.hunter@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220520132404.25853-1-adrian.hunter@intel.com> References: <20220520132404.25853-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add preadn() to provide pread() and readn() semantics. Signed-off-by: Adrian Hunter Acked-by: Jiri Olsa --- tools/lib/perf/include/internal/lib.h | 2 ++ tools/lib/perf/lib.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/tools/lib/perf/include/internal/lib.h b/tools/lib/perf/include= /internal/lib.h index 5175d491b2d4..85471a4b900f 100644 --- a/tools/lib/perf/include/internal/lib.h +++ b/tools/lib/perf/include/internal/lib.h @@ -9,4 +9,6 @@ extern unsigned int page_size; ssize_t readn(int fd, void *buf, size_t n); ssize_t writen(int fd, const void *buf, size_t n); =20 +ssize_t preadn(int fd, void *buf, size_t n, off_t offs); + #endif /* __LIBPERF_INTERNAL_CPUMAP_H */ diff --git a/tools/lib/perf/lib.c b/tools/lib/perf/lib.c index 18658931fc71..ecc8035a3ae3 100644 --- a/tools/lib/perf/lib.c +++ b/tools/lib/perf/lib.c @@ -38,6 +38,26 @@ ssize_t readn(int fd, void *buf, size_t n) return ion(true, fd, buf, n); } =20 +ssize_t preadn(int fd, void *buf, size_t n, off_t offs) +{ + ssize_t ret; + off_t cur; + + cur =3D lseek(fd, 0, SEEK_CUR); + if (cur < 0) + return -1; + + if (lseek(fd, offs, SEEK_SET) < 0) + return -1; + + ret =3D readn(fd, buf, n); + + if (lseek(fd, cur, SEEK_CUR) < 0) + return -1; + + return ret; +} + /* * Write exactly 'n' bytes or return an error. */ --=20 2.25.1 From nobody Thu May 7 19:05:33 2026 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 BA51BC433EF for ; Fri, 20 May 2022 13:24:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349816AbiETNYh (ORCPT ); Fri, 20 May 2022 09:24:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349775AbiETNYZ (ORCPT ); Fri, 20 May 2022 09:24:25 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6824914A246 for ; Fri, 20 May 2022 06:24:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653053064; x=1684589064; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mNKd/tsQRuan+vrDqCBtTMp3Z8UUt6bCZo0Wdm3XR2M=; b=D0kInXMiyn76FUHBwa+nPx95h+tZShTjkvxVrfM2GQiAontk0uqKSA45 TXij0rtqDvK7gTGHUbpIQVlzmzlmwM34XNYmW0XyOdvRifaxTxQihGEPm y3+ARCJf8u4fZ0Phr1dj0gzaj/YOdbIoq/0epKs6fNg0OXhfnKEd6JBla ALRCRyAW4wV/fYpxcR0jqJS0zQshN+uQBonmmBcKThFHcIHLl/FAvfogs vRYTwMV7v4gHSymc74cG/FtLZYKTPtzci7fiOASvAsHRyGefNz7cz75m3 J/uQgq12FWNf61u3w2C0JP95aJXBhNZYa2ZwrmeN7d9G4SlBig95v/Bcq w==; X-IronPort-AV: E=McAfee;i="6400,9594,10353"; a="335661021" X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="335661021" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:24 -0700 X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="715530816" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.38.124]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:22 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org Subject: [PATCH 3/5] perf inject: Keep some features sections from input file Date: Fri, 20 May 2022 16:24:02 +0300 Message-Id: <20220520132404.25853-4-adrian.hunter@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220520132404.25853-1-adrian.hunter@intel.com> References: <20220520132404.25853-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" perf inject overwrites feature sections with information from the current machine. It makes more sense to keep original information that describes the machine or software when perf record was run. Example: perf.data from "Desktop" injected on "nuc11" Before: $ perf script --header-only -i perf.data-from-desktop | head -15 # =3D=3D=3D=3D=3D=3D=3D=3D # captured on : Thu May 19 09:55:50 2022 # header version : 1 # data offset : 1208 # data size : 837480 # feat offset : 838688 # hostname : Desktop # os release : 5.13.0-41-generic # perf version : 5.18.rc5.gac837f7ca7ed # arch : x86_64 # nrcpus online : 28 # nrcpus avail : 28 # cpudesc : Intel(R) Core(TM) i9-9940X CPU @ 3.30GHz # cpuid : GenuineIntel,6,85,4 # total memory : 65548656 kB $ perf inject -i perf.data-from-desktop -o injected-perf.data $ perf script --header-only -i injected-perf.data | head -15 # =3D=3D=3D=3D=3D=3D=3D=3D # captured on : Fri May 20 15:06:55 2022 # header version : 1 # data offset : 1208 # data size : 837480 # feat offset : 838688 # hostname : nuc11 # os release : 5.17.5-local # perf version : 5.18.rc5.g0f828fdeb9af # arch : x86_64 # nrcpus online : 8 # nrcpus avail : 8 # cpudesc : 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz # cpuid : GenuineIntel,6,140,1 # total memory : 16012124 kB After: $ perf inject -i perf.data-from-desktop -o injected-perf.data $ perf script --header-only -i injected-perf.data | head -15 # =3D=3D=3D=3D=3D=3D=3D=3D # captured on : Fri May 20 15:08:54 2022 # header version : 1 # data offset : 1208 # data size : 837480 # feat offset : 838688 # hostname : Desktop # os release : 5.13.0-41-generic # perf version : 5.18.rc5.gac837f7ca7ed # arch : x86_64 # nrcpus online : 28 # nrcpus avail : 28 # cpudesc : Intel(R) Core(TM) i9-9940X CPU @ 3.30GHz # cpuid : GenuineIntel,6,85,4 # total memory : 65548656 kB Signed-off-by: Adrian Hunter Acked-by: Jiri Olsa --- tools/perf/builtin-inject.c | 129 +++++++++++++++++++++++++++++++++++- tools/perf/util/header.c | 8 +++ tools/perf/util/header.h | 5 ++ 3 files changed, 141 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 5b50a4abf95f..71b6eafe4c19 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -27,6 +27,8 @@ #include "util/namespaces.h" #include "util/util.h" =20 +#include + #include #include #include /* To get things like MAP_HUGETLB even on old= er libc headers */ @@ -55,6 +57,7 @@ struct perf_inject { struct list_head samples; struct itrace_synth_opts itrace_synth_opts; char event_copy[PERF_SAMPLE_MAX_SIZE]; + struct perf_file_section secs[HEADER_FEAT_BITS]; }; =20 struct event_entry { @@ -763,6 +766,120 @@ static int parse_vm_time_correlation(const struct opt= ion *opt, const char *str, return inject->itrace_synth_opts.vm_tm_corr_args ? 0 : -ENOMEM; } =20 +static int save_section_info_cb(struct perf_file_section *section, + struct perf_header *ph __maybe_unused, + int feat, int fd __maybe_unused, void *data) +{ + struct perf_inject *inject =3D data; + + inject->secs[feat] =3D *section; + return 0; +} + +static int save_section_info(struct perf_inject *inject) +{ + struct perf_header *header =3D &inject->session->header; + int fd =3D perf_data__fd(inject->session->data); + + return perf_header__process_sections(header, fd, inject, save_section_inf= o_cb); +} + +static bool keep_feat(int feat) +{ + switch (feat) { + /* Keep original information that describes the machine or software */ + case HEADER_TRACING_DATA: + case HEADER_HOSTNAME: + case HEADER_OSRELEASE: + case HEADER_VERSION: + case HEADER_ARCH: + case HEADER_NRCPUS: + case HEADER_CPUDESC: + case HEADER_CPUID: + case HEADER_TOTAL_MEM: + case HEADER_CPU_TOPOLOGY: + case HEADER_NUMA_TOPOLOGY: + case HEADER_PMU_MAPPINGS: + case HEADER_CACHE: + case HEADER_MEM_TOPOLOGY: + case HEADER_CLOCKID: + case HEADER_BPF_PROG_INFO: + case HEADER_BPF_BTF: + case HEADER_CPU_PMU_CAPS: + case HEADER_CLOCK_DATA: + case HEADER_HYBRID_TOPOLOGY: + case HEADER_HYBRID_CPU_PMU_CAPS: + return true; + /* Information that can be updated */ + case HEADER_BUILD_ID: + case HEADER_CMDLINE: + case HEADER_EVENT_DESC: + case HEADER_BRANCH_STACK: + case HEADER_GROUP_DESC: + case HEADER_AUXTRACE: + case HEADER_STAT: + case HEADER_SAMPLE_TIME: + case HEADER_DIR_FORMAT: + case HEADER_COMPRESSED: + default: + return false; + }; +} + +static int read_file(int fd, u64 offs, void *buf, size_t sz) +{ + ssize_t ret =3D preadn(fd, buf, sz, offs); + + if (ret < 0) + return -errno; + if ((size_t)ret !=3D sz) + return -EINVAL; + return 0; +} + +static int feat_copy(struct perf_inject *inject, int feat, struct feat_wri= ter *fw) +{ + int fd =3D perf_data__fd(inject->session->data); + u64 offs =3D inject->secs[feat].offset; + size_t sz =3D inject->secs[feat].size; + void *buf =3D malloc(sz); + int ret; + + if (!buf) + return -ENOMEM; + + ret =3D read_file(fd, offs, buf, sz); + if (ret) + goto out_free; + + ret =3D fw->write(fw, buf, sz); +out_free: + free(buf); + return ret; +} + +struct inject_fc { + struct feat_copier fc; + struct perf_inject *inject; +}; + +static int feat_copy_cb(struct feat_copier *fc, int feat, struct feat_writ= er *fw) +{ + struct inject_fc *inj_fc =3D container_of(fc, struct inject_fc, fc); + struct perf_inject *inject =3D inj_fc->inject; + int ret; + + if (!inject->secs[feat].offset || + !keep_feat(feat)) + return 0; + + ret =3D feat_copy(inject, feat, fw); + if (ret < 0) + return ret; + + return 1; /* Feature section copied */ +} + static int output_fd(struct perf_inject *inject) { return inject->in_place_update ? -1 : perf_data__fd(&inject->output); @@ -848,6 +965,11 @@ static int __cmd_inject(struct perf_inject *inject) return ret; =20 if (!inject->is_pipe && !inject->in_place_update) { + struct inject_fc inj_fc =3D { + .fc.copy =3D feat_copy_cb, + .inject =3D inject, + }; + if (inject->build_ids) perf_header__set_feat(&session->header, HEADER_BUILD_ID); @@ -872,7 +994,7 @@ static int __cmd_inject(struct perf_inject *inject) } session->header.data_offset =3D output_data_offset; session->header.data_size =3D inject->bytes_written; - perf_session__write_header(session, session->evlist, fd, true); + perf_session__inject_header(session, session->evlist, fd, &inj_fc.fc); } =20 return ret; @@ -1037,6 +1159,11 @@ int cmd_inject(int argc, const char **argv) if (zstd_init(&(inject.session->zstd_data), 0) < 0) pr_warning("Decompression initialization failed.\n"); =20 + /* Save original section info before feature bits change */ + ret =3D save_section_info(&inject); + if (ret) + goto out_delete; + if (!data.is_pipe && inject.output.is_pipe) { ret =3D perf_header__write_pipe(perf_data__fd(&inject.output)); if (ret < 0) { diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index b0c57a130d1e..53332da100e8 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -3686,6 +3686,14 @@ int perf_session__write_header(struct perf_session *= session, return perf_session__do_write_header(session, evlist, fd, at_exit, NULL); } =20 +int perf_session__inject_header(struct perf_session *session, + struct evlist *evlist, + int fd, + struct feat_copier *fc) +{ + return perf_session__do_write_header(session, evlist, fd, true, fc); +} + static int perf_header__getbuffer64(struct perf_header *header, int fd, void *buf, size_t size) { diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h index e76ab02d5541..08563c1f1bff 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h @@ -131,6 +131,11 @@ struct feat_copier { int (*copy)(struct feat_copier *fc, int feat, struct feat_writer *fw); }; =20 +int perf_session__inject_header(struct perf_session *session, + struct evlist *evlist, + int fd, + struct feat_copier *fc); + void perf_header__set_feat(struct perf_header *header, int feat); void perf_header__clear_feat(struct perf_header *header, int feat); bool perf_header__has_feat(const struct perf_header *header, int feat); --=20 2.25.1 From nobody Thu May 7 19:05:33 2026 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 70A6BC433F5 for ; Fri, 20 May 2022 13:24:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349779AbiETNYo (ORCPT ); Fri, 20 May 2022 09:24:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241528AbiETNYe (ORCPT ); Fri, 20 May 2022 09:24:34 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 089F814E2C7 for ; Fri, 20 May 2022 06:24:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653053066; x=1684589066; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=l/5zgvFU2ybyvyyFUoqpvM63YLZE49DLKFEnrzYhU8o=; b=V3M0pSnl06wJolLg7UOC13DpX7oAQ33BiMo6OISsdZDvAFVCE+5q+hfR KLRNo0CZZAwq6/JNKobwiaL2okT2/4RLtdE/26plrYescXsz42yiggQxF 7wZ6RlnoGItRa7XWZuHbCadmw8JryFnzZ0yTsCKEUH+2yg5VFXHlUjQ2O pzsL7SYQTDabW7C7A4XGUywih0vMnauVg6Jo2y9w/ZA2EhFPYOyTcZcua MDWG6OseszUTWMSdTdXdi5Bku/i2+UPKjuZWaoFijFI2wIsUCblPABHHz dcU4hyD95g1TlKqSoBSNU941b+i/bB9bg8dp8nns9sxMsX3dhk9tEYQA8 Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10353"; a="335661025" X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="335661025" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:25 -0700 X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="715530821" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.38.124]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:24 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org Subject: [PATCH 4/5] perf data: Add has_kcore_dir() Date: Fri, 20 May 2022 16:24:03 +0300 Message-Id: <20220520132404.25853-5-adrian.hunter@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220520132404.25853-1-adrian.hunter@intel.com> References: <20220520132404.25853-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add a helper function has_kcore_dir(), so that perf inject can determine if it needs to keep the kcore_dir. Signed-off-by: Adrian Hunter Acked-by: Jiri Olsa --- tools/perf/util/data.c | 14 ++++++++++++++ tools/perf/util/data.h | 1 + 2 files changed, 15 insertions(+) diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c index a5ace2bbc28d..caabeac24c69 100644 --- a/tools/perf/util/data.c +++ b/tools/perf/util/data.c @@ -479,6 +479,20 @@ int perf_data__make_kcore_dir(struct perf_data *data, = char *buf, size_t buf_sz) return mkdir(buf, S_IRWXU); } =20 +bool has_kcore_dir(const char *path) +{ + char *kcore_dir; + int ret; + + if (asprintf(&kcore_dir, "%s/kcore_dir", path) < 0) + return false; + + ret =3D access(kcore_dir, F_OK); + + free(kcore_dir); + return !ret; +} + char *perf_data__kallsyms_name(struct perf_data *data) { char *kallsyms_name; diff --git a/tools/perf/util/data.h b/tools/perf/util/data.h index 1402d9657ef2..7de53d6e2d7f 100644 --- a/tools/perf/util/data.h +++ b/tools/perf/util/data.h @@ -99,6 +99,7 @@ void perf_data__close_dir(struct perf_data *data); int perf_data__update_dir(struct perf_data *data); unsigned long perf_data__size(struct perf_data *data); int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t bu= f_sz); +bool has_kcore_dir(const char *path); char *perf_data__kallsyms_name(struct perf_data *data); bool is_perf_data(const char *path); #endif /* __PERF_DATA_H */ --=20 2.25.1 From nobody Thu May 7 19:05:33 2026 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 5EDB4C433F5 for ; Fri, 20 May 2022 13:24:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349846AbiETNYu (ORCPT ); Fri, 20 May 2022 09:24:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349809AbiETNYf (ORCPT ); Fri, 20 May 2022 09:24:35 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B75A216A259 for ; Fri, 20 May 2022 06:24:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653053067; x=1684589067; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SMnp/mUn9icKbmgubdGL7zBlHw8RhPKRnVLhR4Xj7z0=; b=heKWjdnw8xkNC4ErvX6YCMbVBTo7CRwpTkkJyJJrORcw9j6cHGS5ynAt j18ZagxwE31cxW6P7771rWhF5Kpy3/ZmcO989VaVqxHT9ndg0b/t+2ESy xB2F8P34JINDdLB4wFkxiYySZOTyXg6gCK4r+iXZKiM2zpdSjXmBWElbg niDh1BQ61s8CQnt/z5vb6fdwcsfQtIOeid9Im8UmivfGrZ26zkck0HmpG YxVEWmrZSWEWirrOJjtYmYgcxQTByxu0/QQI7r8lSEg9BRHY09nqZZW3Z z9IFyih9oh5EaFdsXH91ylYI42/gXXDWxbi2Fgvp6RJWvAyT+5NSW0qPq g==; X-IronPort-AV: E=McAfee;i="6400,9594,10353"; a="335661033" X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="335661033" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:27 -0700 X-IronPort-AV: E=Sophos;i="5.91,239,1647327600"; d="scan'208";a="715530824" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.38.124]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 06:24:25 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org Subject: [PATCH 5/5] perf inject: Keep a copy of kcore_dir Date: Fri, 20 May 2022 16:24:04 +0300 Message-Id: <20220520132404.25853-6-adrian.hunter@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220520132404.25853-1-adrian.hunter@intel.com> References: <20220520132404.25853-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If the input perf.data has a kcore_dir, copy it into the output, since at least the kallsyms in the kcore_dir will be useful to the output. Example: Before: $ ls -lR perf.data-from-desktop perf.data-from-desktop: total 916 -rw------- 1 user user 931756 May 19 09:55 data drwx------ 2 user user 4096 May 19 09:55 kcore_dir perf.data-from-desktop/kcore_dir: total 42952 -r-------- 1 user user 7582467 May 19 09:55 kallsyms -r-------- 1 user user 36388864 May 19 09:55 kcore -r-------- 1 user user 4828 May 19 09:55 modules $ perf inject -i perf.data-from-desktop -o injected-perf.data $ ls -lR injected-perf.data -rw------- 1 user user 931320 May 20 15:08 injected-perf.data After: $ perf inject -i perf.data-from-desktop -o injected-perf.data $ ls -lR injected-perf.data injected-perf.data: total 916 -rw------- 1 user user 931320 May 20 15:21 data drwx------ 2 user user 4096 May 20 15:21 kcore_dir injected-perf.data/kcore_dir: total 42952 -r-------- 1 user user 7582467 May 20 15:21 kallsyms -r-------- 1 user user 36388864 May 20 15:21 kcore -r-------- 1 user user 4828 May 20 15:21 modules Signed-off-by: Adrian Hunter Acked-by: Jiri Olsa --- tools/perf/builtin-inject.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 71b6eafe4c19..a75bf11585b5 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -50,6 +50,7 @@ struct perf_inject { bool in_place_update; bool in_place_update_dry_run; bool is_pipe; + bool copy_kcore_dir; const char *input_name; struct perf_data output; u64 bytes_written; @@ -880,6 +881,19 @@ static int feat_copy_cb(struct feat_copier *fc, int fe= at, struct feat_writer *fw return 1; /* Feature section copied */ } =20 +static int copy_kcore_dir(struct perf_inject *inject) +{ + char *cmd; + int ret; + + ret =3D asprintf(&cmd, "cp -r -n %s/kcore_dir* %s >/dev/null 2>&1", + inject->input_name, inject->output.path); + if (ret < 0) + return ret; + pr_debug("%s\n", cmd); + return system(cmd); +} + static int output_fd(struct perf_inject *inject) { return inject->in_place_update ? -1 : perf_data__fd(&inject->output); @@ -995,6 +1009,12 @@ static int __cmd_inject(struct perf_inject *inject) session->header.data_offset =3D output_data_offset; session->header.data_size =3D inject->bytes_written; perf_session__inject_header(session, session->evlist, fd, &inj_fc.fc); + + if (inject->copy_kcore_dir) { + ret =3D copy_kcore_dir(inject); + if (ret) + return ret; + } } =20 return ret; @@ -1131,9 +1151,16 @@ int cmd_inject(int argc, const char **argv) } if (!inject.in_place_update_dry_run) data.in_place_update =3D true; - } else if (perf_data__open(&inject.output)) { - perror("failed to create output file"); - return -1; + } else { + if (strcmp(inject.output.path, "-") && !inject.strip && + has_kcore_dir(inject.input_name)) { + inject.output.is_dir =3D true; + inject.copy_kcore_dir =3D true; + } + if (perf_data__open(&inject.output)) { + perror("failed to create output file"); + return -1; + } } =20 data.path =3D inject.input_name; --=20 2.25.1