From nobody Fri Oct 10 21:10:20 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1552C2D4B5A for ; Thu, 12 Jun 2025 19:49:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757786; cv=none; b=sCO7z2L/W08G+fGtI7+y23QbzJ6jUnYpxhrug/SblYQmUckA9aVGM0j0vU9XOdyIIZ1YSVlvn35K0CEDxUv+chqnCsIz76Xt2VaXiZ8Nx6+kGSYEP+vSjXf3N1k9C5/lu9DEPdc/+PojfIe0Q5rKERyc75xsu6l/M4gMctruEdU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757786; c=relaxed/simple; bh=jY3MGFXxhQhmObC8B0QQNU5r9zUDqG3Bm/y01WftukM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fxSrKoMkTAGXHeunu/gq+ZV/uPrMJT+9KS1hC6fnjLYCJcTObWH91d2cL4g8eyQK9GjePQbwdEZ5yHNspRnTSRdCcFghWxlfONZlH567uzW1Dpa2ywxAkYUO5aOCr8D7jmTcd7eBj5mDNnSOYMEma6aEdwkrKYd82+RVM4muJwU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=F20QgdiE; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="F20QgdiE" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-311f4f2e6baso1271439a91.0 for ; Thu, 12 Jun 2025 12:49:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749757784; x=1750362584; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yYXjS/TK8jfGSnydeZ1dK5T+dpqe3MEJcXcTnDyoht0=; b=F20QgdiElgHCP7ZXBXR16EXGexXeku68NO0Y+jO22wNmvFAIzzuq1jGFH5UEbZZmvG D+rMmYCR39hzxU4Prb5auOce1l+vgsPAE552ns4ialv6lqzky9c9ex9y4ddB6HM15/+T aZptf/wWZW5Jpp1IqsZ33mYAxsk07NsOOUG1F9VldMqUW63tITswQbInTAE78sArlfI1 VuQrEF6iIjaw7LAuZ4maPN43W4ppSCBA9TugzFsQioK91CGEydke/PoIy8EWzOXaXJGi t7DgGWHSH9Dil6dMqvzXVEaVYUzmIhpJtebTsVcYTaTovcUJkSBQb0V5wixSq4a5c5sa MIGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749757784; x=1750362584; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yYXjS/TK8jfGSnydeZ1dK5T+dpqe3MEJcXcTnDyoht0=; b=JhGKBI0VvLAUbTsbwjzHgYwnEyqsWtznhi4cUntl/UgXlIfBiYEDE36/sQmXKNitLP Qx66X04PIDg+XwssxWijbdnv0mVlBy8JKICA7Rf/RTeqNsvAeJb/3m5HozC2yTor3MZT JWgyzTPv0FN4Yq5yTDRkoAKJfGNVYFQoP48qta2/jhMAnpn8uMSpOCjgRj1cTUqOnZtd pHF+GWK+JeGlyUtas8nEGS8Gkaeu/nvArwLKQFiqvTCGPxVEha1T90weSmjF21SDR9vY DcGlWGZ3q/TPK7ks81i/GpF5VyyaJya3HcTiHRfVsfnQdH/7y0P6K5M1piFWIznjxYLI vnIQ== X-Forwarded-Encrypted: i=1; AJvYcCW4Q/meG+KD4eOaUQ5XXTo36NwiIO7mMTZ5EFTaPfGqJayS4z/PnBPCKG1+b3QDppgg5S4DW5Dl3HuQIjU=@vger.kernel.org X-Gm-Message-State: AOJu0Ywa6t9KUoBuXC3j43HIAE0wFtVP0WE6xsZCf4KtRq5bKL5qDdVi w5RToXF+uyowo6FB/FYU8+C/t8xOZ0WEZHJbnH3kn7TKz+AgPJRRJAn1SKWUrsYP4bqkJ3VV1Yl V5jRHP9q6eyzEWjexjv0QXg== X-Google-Smtp-Source: AGHT+IEIyY1JuTOelnsD6QIJH09dPoFDqlUdg6uKiJwl+kRH0n4MU6s3D73fdkkzPSwsOxlG8YH8nMFbcjmO/aUx X-Received: from pjbdb8.prod.google.com ([2002:a17:90a:d648:b0:30c:4b1f:78ca]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:e7c6:b0:311:e358:c4af with SMTP id 98e67ed59e1d1-313d9eac799mr726583a91.16.1749757784484; Thu, 12 Jun 2025 12:49:44 -0700 (PDT) Date: Thu, 12 Jun 2025 12:49:35 -0700 In-Reply-To: <20250612194939.162730-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250612194939.162730-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc1.591.g9c95f17f64-goog Message-ID: <20250612194939.162730-2-blakejones@google.com> Subject: [PATCH v4 1/5] perf: detect support for libbpf's emit_strings option From: Blake Jones To: Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Jiri Olsa , Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Adrian Hunter , Kan Liang , Steven Rostedt , Tomas Glozar , James Clark , Leo Yan , Guilherme Amadio , Yang Jihong , Charlie Jenkins , Chun-Tse Shao , Aditya Gupta , Athira Rajeev , Zhongqiu Han , Andi Kleen , Dmitry Vyukov , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This creates a config option that detects libbpf's ability to display character arrays as strings, which was just added to the BPF tree (https://git.kernel.org/bpf/bpf-next/c/87c9c79a02b4). To test this change, I built perf (from later in this patch set) with: - static libbpf (default, using source from kernel tree) - dynamic libbpf (LIBBPF_DYNAMIC=3D1 LIBBPF_INCLUDE=3D/usr/local/include) For both the static and dynamic versions, I used headers with and without the ".emit_strings" option. I verified that of the four resulting binaries, the two with ".emit_strings" would successfully record BPF_METADATA events, and the two without wouldn't. All four binaries would successfully display BPF_METADATA events, because the relevant bit of libbpf code is only used during "perf record". Signed-off-by: Blake Jones --- tools/build/Makefile.feature | 1 + tools/build/feature/Makefile | 4 ++++ tools/build/feature/test-libbpf-strings.c | 10 ++++++++++ tools/perf/Documentation/perf-check.txt | 1 + tools/perf/Makefile.config | 8 ++++++++ tools/perf/builtin-check.c | 1 + 6 files changed, 25 insertions(+) create mode 100644 tools/build/feature/test-libbpf-strings.c diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature index 3a1fddd38db0..2e5f4c8b6547 100644 --- a/tools/build/Makefile.feature +++ b/tools/build/Makefile.feature @@ -126,6 +126,7 @@ FEATURE_TESTS_EXTRA :=3D \ llvm \ clang \ libbpf \ + libbpf-strings \ libpfm4 \ libdebuginfod \ clang-bpf-co-re \ diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile index 4aa166d3eab6..0c4e541ed56e 100644 --- a/tools/build/feature/Makefile +++ b/tools/build/feature/Makefile @@ -59,6 +59,7 @@ FILES=3D \ test-lzma.bin \ test-bpf.bin \ test-libbpf.bin \ + test-libbpf-strings.bin \ test-get_cpuid.bin \ test-sdt.bin \ test-cxx.bin \ @@ -339,6 +340,9 @@ $(OUTPUT)test-bpf.bin: $(OUTPUT)test-libbpf.bin: $(BUILD) -lbpf =20 +$(OUTPUT)test-libbpf-strings.bin: + $(BUILD) + $(OUTPUT)test-sdt.bin: $(BUILD) =20 diff --git a/tools/build/feature/test-libbpf-strings.c b/tools/build/featur= e/test-libbpf-strings.c new file mode 100644 index 000000000000..83e6c45f5c85 --- /dev/null +++ b/tools/build/feature/test-libbpf-strings.c @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0 +#include + +int main(void) +{ + struct btf_dump_type_data_opts opts; + + opts.emit_strings =3D 0; + return opts.emit_strings; +} diff --git a/tools/perf/Documentation/perf-check.txt b/tools/perf/Documenta= tion/perf-check.txt index a764a4629220..799982d8d868 100644 --- a/tools/perf/Documentation/perf-check.txt +++ b/tools/perf/Documentation/perf-check.txt @@ -52,6 +52,7 @@ feature:: dwarf-unwind / HAVE_DWARF_UNWIND_SUPPORT auxtrace / HAVE_AUXTRACE_SUPPORT libbfd / HAVE_LIBBFD_SUPPORT + libbpf-strings / HAVE_LIBBPF_STRINGS_SUPPORT libcapstone / HAVE_LIBCAPSTONE_SUPPORT libcrypto / HAVE_LIBCRYPTO_SUPPORT libdw-dwarf-unwind / HAVE_LIBDW_SUPPORT diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index d1ea7bf44964..affe5e173920 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -595,8 +595,16 @@ ifndef NO_LIBELF LIBBPF_STATIC :=3D 1 $(call detected,CONFIG_LIBBPF) CFLAGS +=3D -DHAVE_LIBBPF_SUPPORT + LIBBPF_INCLUDE =3D $(LIBBPF_DIR)/.. endif endif + + FEATURE_CHECK_CFLAGS-libbpf-strings=3D"-I$(LIBBPF_INCLUDE)" + $(call feature_check,libbpf-strings) + ifeq ($(feature-libbpf-strings), 1) + $(call detected,CONFIG_LIBBPF_STRINGS) + CFLAGS +=3D -DHAVE_LIBBPF_STRINGS_SUPPORT + endif endif endif # NO_LIBBPF endif # NO_LIBELF diff --git a/tools/perf/builtin-check.c b/tools/perf/builtin-check.c index 9a509cb3bb9a..f4827f0ddb47 100644 --- a/tools/perf/builtin-check.c +++ b/tools/perf/builtin-check.c @@ -43,6 +43,7 @@ struct feature_status supported_features[] =3D { FEATURE_STATUS("dwarf-unwind", HAVE_DWARF_UNWIND_SUPPORT), FEATURE_STATUS("auxtrace", HAVE_AUXTRACE_SUPPORT), FEATURE_STATUS_TIP("libbfd", HAVE_LIBBFD_SUPPORT, "Deprecated, license in= compatibility, use BUILD_NONDISTRO=3D1 and install binutils-dev[el]"), + FEATURE_STATUS("libbpf-strings", HAVE_LIBBPF_STRINGS_SUPPORT), FEATURE_STATUS("libcapstone", HAVE_LIBCAPSTONE_SUPPORT), FEATURE_STATUS("libcrypto", HAVE_LIBCRYPTO_SUPPORT), FEATURE_STATUS("libdw-dwarf-unwind", HAVE_LIBDW_SUPPORT), --=20 2.50.0.rc1.591.g9c95f17f64-goog From nobody Fri Oct 10 21:10:20 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E9FF12D543E for ; Thu, 12 Jun 2025 19:49:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757789; cv=none; b=Z+2WU6LA6eIaOvCrs5/kZvgczQ9JejlxENY+n6Rcig45LMdo+ipTCAGvrVgcH7OgvzFdmnJlI6B4lDnPIOOj6myGTFRinczUGHG+QMMvxfj4w3VRctfJxPH8soPyYEUtsHVXnzOrhhWO/Ncra/fQpdCEHe9QkapGgWkjYUq8uN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757789; c=relaxed/simple; bh=sL0QzF3uQAud4FchfdGpAc1VAF/3gSxQsFo0+LfNj0Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=j0a87WjAiwjc18ieL79ryFE1WfhixuXrvb4g9N7raTTZGnAYL9wF6DvBZLGexqtxVa6wPqVgbrArM+4wMMvlqEqksBvfqpQUCjsNisjV+LZ/MYQKiqD+9i04PyrCnQ8PMXN6Y86AXmRW1lYnzMOkTWvHUmcdTiDDPWIPATF+etk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sG+fa8Pl; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sG+fa8Pl" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3138e64b3f1so1481699a91.3 for ; Thu, 12 Jun 2025 12:49:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749757786; x=1750362586; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=v3sg1BJGPzEyje7VYNfhPoJYsL8//xYzfwrTk24e9bY=; b=sG+fa8Pl1phImEbTqsWlMc4p22i82Fr3fTPtL1yp/8+XbXB5WbQslOScJUCYCDlrVy k1xKuat59v9gwywtm4ak0NP0/1TYx5u/54aRAQcWI0Y1XjfBOS43nW0OkKr6zNKA7LPw 2Ae3M5pPke+0XODBrdXh9Zpb3rszEzelweEHkkfRGh5WzT3N77jdfkUFgqReqLKQQ2/5 84moLJTfaQXFEtJ5I9H3+f+Yh7/UZ7JY5P4BEa32fz2J8JGDs4GpHXl+BeLCrt9B6o0+ FVlzZ9XHlDiXbzjDndXKSZ2zb+l74nvGBVzv+HFjPAKMY5zjwovsFkpPtzxclAsOLPaN aqPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749757786; x=1750362586; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=v3sg1BJGPzEyje7VYNfhPoJYsL8//xYzfwrTk24e9bY=; b=T6oek9pjyiyJiwDaNmMgbUQifZHwdWEraAvD1yqWpZTrQZBj2QWY8QTGQ6ROAzVylI 3ewQqvQ1wn7yq5wS9xiAPVioga2cCOtFe6kUphc57hQFZpqFDnjdzel9j6scQ1t39Vk6 6PY4EfI3VXiYJ4OAFayB1XkmZX+8EBmJ7LotFkKygvL5jTAz2QqxzmLh22TKBb+YCszv DSrDq5CO16kOjJKx93n9Y/3e7fZm9d97vZXi/erd02667KM2CZnPqIMaal50yF0aV6pS kqI5nlDqOz+lrBc973o1vddAnHE69C2X7clsoDXLJa2jyz1oYcYj4U2voSDNZaWtruT4 nelg== X-Forwarded-Encrypted: i=1; AJvYcCVPWAbwNksVD/naXE4nGGHmJG3P4U/LhDhVy37Y75MYtfXdXFe0qB5BElLxYc2kuwMT0tJBkHN7A5XcFao=@vger.kernel.org X-Gm-Message-State: AOJu0Yz38Pjol+H+H6NLyYg131pgICKWmob9xUeuE5mTWBX9Nh3dbgXP oOiidhT1BRmkSP0eCka49Vb6N+48mCkNmq+RMjqQI/3B7BDvDvd+uiPgG9k19QHKxHuwQJkjsmZ 1+uqVAj0vrYaE8ANGVdepxg== X-Google-Smtp-Source: AGHT+IFeGIi9ZOJZcfn/cJag5N0/xntrLUcO1I0ivEyMLAmYINbjbADQn3mDYhB3lHRLSvDGbBEjW1RYTi56KsmS X-Received: from pjtd9.prod.google.com ([2002:a17:90b:49:b0:313:1c10:3595]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1c12:b0:312:db8:dbd1 with SMTP id 98e67ed59e1d1-313d9c3a0bemr527852a91.5.1749757786375; Thu, 12 Jun 2025 12:49:46 -0700 (PDT) Date: Thu, 12 Jun 2025 12:49:36 -0700 In-Reply-To: <20250612194939.162730-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250612194939.162730-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc1.591.g9c95f17f64-goog Message-ID: <20250612194939.162730-3-blakejones@google.com> Subject: [PATCH v4 2/5] perf: collect BPF metadata from existing BPF programs From: Blake Jones To: Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Jiri Olsa , Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Adrian Hunter , Kan Liang , Steven Rostedt , Tomas Glozar , James Clark , Leo Yan , Guilherme Amadio , Yang Jihong , Charlie Jenkins , Chun-Tse Shao , Aditya Gupta , Athira Rajeev , Zhongqiu Han , Andi Kleen , Dmitry Vyukov , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Look for .rodata maps, find ones with 'bpf_metadata_' variables, extract their values as strings, and create a new PERF_RECORD_BPF_METADATA synthetic event using that data. The code gets invoked from the existing routine perf_event__synthesize_one_bpf_prog(). For example, a BPF program with the following variables: const char bpf_metadata_version[] SEC(".rodata") =3D "3.14159"; int bpf_metadata_value[] SEC(".rodata") =3D 42; would generate a PERF_RECORD_BPF_METADATA record with: .prog_name =3D .nr_entries =3D 2 .entries[0].key =3D "version" .entries[0].value =3D "3.14159" .entries[1].key =3D "value" .entries[1].value =3D "42" Each of the BPF programs and subprograms that share those variables would get a distinct PERF_RECORD_BPF_METADATA record, with the ".prog_name" showing the name of each program or subprogram. The prog_name is deliberately the same as the ".name" field in the corresponding PERF_RECORD_KSYMBOL record. This code only gets invoked if support for displaying BTF char arrays as strings is detected. Signed-off-by: Blake Jones --- tools/lib/perf/include/perf/event.h | 18 ++ tools/perf/util/bpf-event.c | 332 ++++++++++++++++++++++++++++ tools/perf/util/bpf-event.h | 12 + 3 files changed, 362 insertions(+) diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/p= erf/event.h index 09b7c643ddac..6608f1e3701b 100644 --- a/tools/lib/perf/include/perf/event.h +++ b/tools/lib/perf/include/perf/event.h @@ -467,6 +467,22 @@ struct perf_record_compressed2 { char data[]; }; =20 +#define BPF_METADATA_KEY_LEN 64 +#define BPF_METADATA_VALUE_LEN 256 +#define BPF_PROG_NAME_LEN KSYM_NAME_LEN + +struct perf_record_bpf_metadata_entry { + char key[BPF_METADATA_KEY_LEN]; + char value[BPF_METADATA_VALUE_LEN]; +}; + +struct perf_record_bpf_metadata { + struct perf_event_header header; + char prog_name[BPF_PROG_NAME_LEN]; + __u64 nr_entries; + struct perf_record_bpf_metadata_entry entries[]; +}; + enum perf_user_event_type { /* above any possible kernel type */ PERF_RECORD_USER_TYPE_START =3D 64, PERF_RECORD_HEADER_ATTR =3D 64, @@ -489,6 +505,7 @@ enum perf_user_event_type { /* above any possible kerne= l type */ PERF_RECORD_COMPRESSED =3D 81, PERF_RECORD_FINISHED_INIT =3D 82, PERF_RECORD_COMPRESSED2 =3D 83, + PERF_RECORD_BPF_METADATA =3D 84, PERF_RECORD_HEADER_MAX }; =20 @@ -530,6 +547,7 @@ union perf_event { struct perf_record_header_feature feat; struct perf_record_compressed pack; struct perf_record_compressed2 pack2; + struct perf_record_bpf_metadata bpf_metadata; }; =20 #endif /* __LIBPERF_EVENT_H */ diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index c81444059ad0..1f6e76ee6024 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -1,13 +1,21 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include +#include +#include #include +#include #include #include #include +#include #include #include +#include #include +#include #include +#include #include #include "bpf-event.h" #include "bpf-utils.h" @@ -151,6 +159,319 @@ static int synthesize_bpf_prog_name(char *buf, int si= ze, return name_len; } =20 +#ifdef HAVE_LIBBPF_STRINGS_SUPPORT + +#define BPF_METADATA_PREFIX "bpf_metadata_" +#define BPF_METADATA_PREFIX_LEN (sizeof(BPF_METADATA_PREFIX) - 1) + +static bool name_has_bpf_metadata_prefix(const char **s) +{ + if (strncmp(*s, BPF_METADATA_PREFIX, BPF_METADATA_PREFIX_LEN) !=3D 0) + return false; + *s +=3D BPF_METADATA_PREFIX_LEN; + return true; +} + +struct bpf_metadata_map { + struct btf *btf; + const struct btf_type *datasec; + void *rodata; + size_t rodata_size; + unsigned int num_vars; +}; + +static int bpf_metadata_read_map_data(__u32 map_id, struct bpf_metadata_ma= p *map) +{ + int map_fd; + struct bpf_map_info map_info; + __u32 map_info_len; + int key; + struct btf *btf; + const struct btf_type *datasec; + struct btf_var_secinfo *vsi; + unsigned int vlen, vars; + void *rodata; + + map_fd =3D bpf_map_get_fd_by_id(map_id); + if (map_fd < 0) + return -1; + + memset(&map_info, 0, sizeof(map_info)); + map_info_len =3D sizeof(map_info); + if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len) < 0) + goto out_close; + + /* If it's not an .rodata map, don't bother. */ + if (map_info.type !=3D BPF_MAP_TYPE_ARRAY || + map_info.key_size !=3D sizeof(int) || + map_info.max_entries !=3D 1 || + !map_info.btf_value_type_id || + !strstr(map_info.name, ".rodata")) { + goto out_close; + } + + btf =3D btf__load_from_kernel_by_id(map_info.btf_id); + if (!btf) + goto out_close; + datasec =3D btf__type_by_id(btf, map_info.btf_value_type_id); + if (!btf_is_datasec(datasec)) + goto out_free_btf; + + /* + * If there aren't any variables with the "bpf_metadata_" prefix, + * don't bother. + */ + vlen =3D btf_vlen(datasec); + vsi =3D btf_var_secinfos(datasec); + vars =3D 0; + for (unsigned int i =3D 0; i < vlen; i++, vsi++) { + const struct btf_type *t_var =3D btf__type_by_id(btf, vsi->type); + const char *name =3D btf__name_by_offset(btf, t_var->name_off); + + if (name_has_bpf_metadata_prefix(&name)) + vars++; + } + if (vars =3D=3D 0) + goto out_free_btf; + + rodata =3D zalloc(map_info.value_size); + if (!rodata) + goto out_free_btf; + key =3D 0; + if (bpf_map_lookup_elem(map_fd, &key, rodata)) { + free(rodata); + goto out_free_btf; + } + close(map_fd); + + map->btf =3D btf; + map->datasec =3D datasec; + map->rodata =3D rodata; + map->rodata_size =3D map_info.value_size; + map->num_vars =3D vars; + return 0; + +out_free_btf: + btf__free(btf); +out_close: + close(map_fd); + return -1; +} + +struct format_btf_ctx { + char *buf; + size_t buf_size; + size_t buf_idx; +}; + +static void format_btf_cb(void *arg, const char *fmt, va_list ap) +{ + int n; + struct format_btf_ctx *ctx =3D (struct format_btf_ctx *)arg; + + n =3D vsnprintf(ctx->buf + ctx->buf_idx, ctx->buf_size - ctx->buf_idx, + fmt, ap); + ctx->buf_idx +=3D n; + if (ctx->buf_idx >=3D ctx->buf_size) + ctx->buf_idx =3D ctx->buf_size; +} + +static void format_btf_variable(struct btf *btf, char *buf, size_t buf_siz= e, + const struct btf_type *t, const void *btf_data) +{ + struct format_btf_ctx ctx =3D { + .buf =3D buf, + .buf_idx =3D 0, + .buf_size =3D buf_size, + }; + const struct btf_dump_type_data_opts opts =3D { + .sz =3D sizeof(struct btf_dump_type_data_opts), + .skip_names =3D 1, + .compact =3D 1, + .emit_strings =3D 1, + }; + struct btf_dump *d; + size_t btf_size; + + d =3D btf_dump__new(btf, format_btf_cb, &ctx, NULL); + btf_size =3D btf__resolve_size(btf, t->type); + btf_dump__dump_type_data(d, t->type, btf_data, btf_size, &opts); + btf_dump__free(d); +} + +static void bpf_metadata_fill_event(struct bpf_metadata_map *map, + struct perf_record_bpf_metadata *bpf_metadata_event) +{ + struct btf_var_secinfo *vsi; + unsigned int i, vlen; + + memset(bpf_metadata_event->prog_name, 0, BPF_PROG_NAME_LEN); + vlen =3D btf_vlen(map->datasec); + vsi =3D btf_var_secinfos(map->datasec); + + for (i =3D 0; i < vlen; i++, vsi++) { + const struct btf_type *t_var =3D btf__type_by_id(map->btf, + vsi->type); + const char *name =3D btf__name_by_offset(map->btf, + t_var->name_off); + const __u64 nr_entries =3D bpf_metadata_event->nr_entries; + struct perf_record_bpf_metadata_entry *entry; + + if (!name_has_bpf_metadata_prefix(&name)) + continue; + + if (nr_entries >=3D (__u64)map->num_vars) + break; + + entry =3D &bpf_metadata_event->entries[nr_entries]; + memset(entry, 0, sizeof(*entry)); + snprintf(entry->key, BPF_METADATA_KEY_LEN, "%s", name); + format_btf_variable(map->btf, entry->value, + BPF_METADATA_VALUE_LEN, t_var, + map->rodata + vsi->offset); + bpf_metadata_event->nr_entries++; + } +} + +static void bpf_metadata_free_map_data(struct bpf_metadata_map *map) +{ + btf__free(map->btf); + free(map->rodata); +} + +static struct bpf_metadata *bpf_metadata_alloc(__u32 nr_prog_tags, + __u32 nr_variables) +{ + struct bpf_metadata *metadata; + size_t event_size; + + metadata =3D zalloc(sizeof(struct bpf_metadata)); + if (!metadata) + return NULL; + + metadata->prog_names =3D zalloc(nr_prog_tags * sizeof(char *)); + if (!metadata->prog_names) { + bpf_metadata_free(metadata); + return NULL; + } + for (__u32 prog_index =3D 0; prog_index < nr_prog_tags; prog_index++) { + metadata->prog_names[prog_index] =3D zalloc(BPF_PROG_NAME_LEN); + if (!metadata->prog_names[prog_index]) { + bpf_metadata_free(metadata); + return NULL; + } + metadata->nr_prog_names++; + } + + event_size =3D sizeof(metadata->event->bpf_metadata) + + nr_variables * sizeof(metadata->event->bpf_metadata.entries[0]); + metadata->event =3D zalloc(event_size); + if (!metadata->event) { + bpf_metadata_free(metadata); + return NULL; + } + metadata->event->bpf_metadata =3D (struct perf_record_bpf_metadata) { + .header =3D { + .type =3D PERF_RECORD_BPF_METADATA, + .size =3D event_size, + }, + .nr_entries =3D 0, + }; + + return metadata; +} + +static struct bpf_metadata *bpf_metadata_create(struct bpf_prog_info *info) +{ + struct bpf_metadata *metadata; + const __u32 *map_ids =3D (__u32 *)(uintptr_t)info->map_ids; + + for (__u32 map_index =3D 0; map_index < info->nr_map_ids; map_index++) { + struct bpf_metadata_map map; + + if (bpf_metadata_read_map_data(map_ids[map_index], &map) !=3D 0) + continue; + + metadata =3D bpf_metadata_alloc(info->nr_prog_tags, map.num_vars); + if (!metadata) + continue; + + bpf_metadata_fill_event(&map, &metadata->event->bpf_metadata); + + for (__u32 index =3D 0; index < info->nr_prog_tags; index++) { + synthesize_bpf_prog_name(metadata->prog_names[index], + BPF_PROG_NAME_LEN, info, + map.btf, index); + } + + bpf_metadata_free_map_data(&map); + + return metadata; + } + + return NULL; +} + +static int synthesize_perf_record_bpf_metadata(const struct bpf_metadata *= metadata, + const struct perf_tool *tool, + perf_event__handler_t process, + struct machine *machine) +{ + const size_t event_size =3D metadata->event->header.size; + union perf_event *event; + int err =3D 0; + + event =3D zalloc(event_size + machine->id_hdr_size); + if (!event) + return -1; + memcpy(event, metadata->event, event_size); + memset((void *)event + event->header.size, 0, machine->id_hdr_size); + event->header.size +=3D machine->id_hdr_size; + for (__u32 index =3D 0; index < metadata->nr_prog_names; index++) { + memcpy(event->bpf_metadata.prog_name, + metadata->prog_names[index], BPF_PROG_NAME_LEN); + err =3D perf_tool__process_synth_event(tool, event, machine, + process); + if (err !=3D 0) + break; + } + + free(event); + return err; +} + +void bpf_metadata_free(struct bpf_metadata *metadata) +{ + if (metadata =3D=3D NULL) + return; + for (__u32 index =3D 0; index < metadata->nr_prog_names; index++) + free(metadata->prog_names[index]); + free(metadata->prog_names); + free(metadata->event); + free(metadata); +} + +#else /* HAVE_LIBBPF_STRINGS_SUPPORT */ + +static struct bpf_metadata *bpf_metadata_create(struct bpf_prog_info *info= __maybe_unused) +{ + return NULL; +} + +static int synthesize_perf_record_bpf_metadata(const struct bpf_metadata *= metadata __maybe_unused, + const struct perf_tool *tool __maybe_unused, + perf_event__handler_t process __maybe_unused, + struct machine *machine __maybe_unused) +{ + return 0; +} + +void bpf_metadata_free(struct bpf_metadata *metadata __maybe_unused) +{ +} + +#endif /* HAVE_LIBBPF_STRINGS_SUPPORT */ + /* * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf * program. One PERF_RECORD_BPF_EVENT is generated for the program. And @@ -173,6 +494,7 @@ static int perf_event__synthesize_one_bpf_prog(struct p= erf_session *session, const struct perf_tool *tool =3D session->tool; struct bpf_prog_info_node *info_node; struct perf_bpil *info_linear; + struct bpf_metadata *metadata; struct bpf_prog_info *info; struct btf *btf =3D NULL; struct perf_env *env; @@ -193,6 +515,7 @@ static int perf_event__synthesize_one_bpf_prog(struct p= erf_session *session, arrays |=3D 1UL << PERF_BPIL_JITED_INSNS; arrays |=3D 1UL << PERF_BPIL_LINE_INFO; arrays |=3D 1UL << PERF_BPIL_JITED_LINE_INFO; + arrays |=3D 1UL << PERF_BPIL_MAP_IDS; =20 info_linear =3D get_bpf_prog_info_linear(fd, arrays); if (IS_ERR_OR_NULL(info_linear)) { @@ -301,6 +624,15 @@ static int perf_event__synthesize_one_bpf_prog(struct = perf_session *session, */ err =3D perf_tool__process_synth_event(tool, event, machine, process); + + /* Synthesize PERF_RECORD_BPF_METADATA */ + metadata =3D bpf_metadata_create(info); + if (metadata !=3D NULL) { + err =3D synthesize_perf_record_bpf_metadata(metadata, + tool, process, + machine); + bpf_metadata_free(metadata); + } } =20 out: diff --git a/tools/perf/util/bpf-event.h b/tools/perf/util/bpf-event.h index e2f0420905f5..ef2dd3f1619e 100644 --- a/tools/perf/util/bpf-event.h +++ b/tools/perf/util/bpf-event.h @@ -17,6 +17,12 @@ struct record_opts; struct evlist; struct target; =20 +struct bpf_metadata { + union perf_event *event; + char **prog_names; + __u64 nr_prog_names; +}; + struct bpf_prog_info_node { struct perf_bpil *info_linear; struct rb_node rb_node; @@ -36,6 +42,7 @@ int evlist__add_bpf_sb_event(struct evlist *evlist, struc= t perf_env *env); void __bpf_event__print_bpf_prog_info(struct bpf_prog_info *info, struct perf_env *env, FILE *fp); +void bpf_metadata_free(struct bpf_metadata *metadata); #else static inline int machine__process_bpf(struct machine *machine __maybe_unu= sed, union perf_event *event __maybe_unused, @@ -55,6 +62,11 @@ static inline void __bpf_event__print_bpf_prog_info(stru= ct bpf_prog_info *info _ FILE *fp __maybe_unused) { =20 +} + +static inline void bpf_metadata_free(struct bpf_metadata *metadata __maybe= _unused) +{ + } #endif // HAVE_LIBBPF_SUPPORT #endif --=20 2.50.0.rc1.591.g9c95f17f64-goog From nobody Fri Oct 10 21:10:20 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90E0E2D542D for ; Thu, 12 Jun 2025 19:49:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757790; cv=none; b=VoD9o+HZqudvu+VG672PyxbhOshZPG0mmphWc4zyL/eVAUCbVXCTvLUz2jBQ2GswIg4oq2wExMPAb5k9cwoN2IX8NR/2LRgUm7YuGV0w7ZLYayL1rJ3xHuUFE767uQYUJBhPWmbAeISjcxM1salzbvfdcSvfH8vqswXng8655Go= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757790; c=relaxed/simple; bh=ia2kNtooLXZtddxtzmjyLOZyIOrYzFIrc2YWBoyTzE0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DxprOFFXMign6UXYJ0UPmGg4jxAKFYbq2Pz/8Q5SM4RW7g+aYNCevLzIBqaGbauTOB0CTYzeKhhbOjHltfSPktmlgreNVxl2Ex0x6rvTNC6HduhJglLy/yfGZNMbqJ03lNfmm0T7BvJ3VG/tj38nuZyeYm5e2nXGECmG2QOpsss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1Ohk/U6M; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1Ohk/U6M" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7462aff55bfso1015547b3a.2 for ; Thu, 12 Jun 2025 12:49:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749757788; x=1750362588; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RwgwORwGUH9uFoqt6oU2k4z3IDDLnBCDkrf4pU2pZuE=; b=1Ohk/U6M+HM73qFP/xGDW255KKxSiq2eW1YSJzUK8AmKuRlERHf6M8TS9O5o+ke91q BN6Sg51MBNeKt3Xq+FhAZEb2WfQ2mZPyh7x9mYHJiBrBO329NiFlyStDyX5/THsLPg7G RL/tv0VKpoYZ6nuP7t3kMrHllKgQ4E2WjWE9rBQnQRkkAGrBdW++xuGVtNCPFcq37ABd Vr7byi/ULYmIWHrFFLBfsKWECy2qGFMXjRheh/EgYZ8SfeoDo5Ft1rvbV/1lpRPb57LG bbMTH6WhEHvmz5FS1fg6JKFwOH/6m1ghDjfgQLqurGSC5vSKTt38DZRYiOPQn0GkbMiK 4ZYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749757788; x=1750362588; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RwgwORwGUH9uFoqt6oU2k4z3IDDLnBCDkrf4pU2pZuE=; b=WxrG/Kx3LokJPMXb9127Ds2C8LkiWLMSn3lEY7AFLeeedetZZOt2KMsgaR9DhqFVCt ny1/yW8D+GpvSf51nuReZa3os7vEncIrJDnIjQeMKz93eUEu8P3NsKa3jxmHl5sQkEyT 3azAdXdpaSGC9nyIdNLaPyRZJFyKqo0z+A4Spwa3vJgCBukApMZhzkD+B7PqJb1XQNyD SDWkHf8O2FvX9uvPI5gBUJM/aXqZqyLNmi9EMvk7eCXCSy9xdAdRuW2fMIjUML0qwOxk opHB492DiRub/7bvCLkK6iL0sKGL5s9iP88pFEd1qKQR6tlPNeOAPm13T4FdtS4Nya5E TLUw== X-Forwarded-Encrypted: i=1; AJvYcCXFPwPRwVxLo6SS8orCBKId6wHedxLe+B/ME/JU+jV/diWszJrgXMoYGxuJQXC2yLFyXVSNFzn2ikTMhy0=@vger.kernel.org X-Gm-Message-State: AOJu0Ywx6ejGjl9oL/WvMLiRzKLEgSX/gymGAFi56cARAmn41TMN6/tt /MNRh1GLWKgfo4v/mF2DH9u5Ma6JmexSQRABA9Q6yreNHVugYxXJOk7AdfFk5xA6k1xxuQ+esGW 1PmHbWQt4uFqcwkY5iMXGCA== X-Google-Smtp-Source: AGHT+IGNw4lkpwjaf4v6bXxIOzfZmFO85pS43p7oo8MRgEqWKeMneT7/6nkmbDJ+tkgVhVSPF+Jk1alL6QtQUiX0 X-Received: from pfbkq16.prod.google.com ([2002:a05:6a00:4b10:b0:73c:29d8:b795]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:c89:b0:736:a540:c9ad with SMTP id d2e1a72fcca58-7488f71e782mr688264b3a.20.1749757787828; Thu, 12 Jun 2025 12:49:47 -0700 (PDT) Date: Thu, 12 Jun 2025 12:49:37 -0700 In-Reply-To: <20250612194939.162730-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250612194939.162730-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc1.591.g9c95f17f64-goog Message-ID: <20250612194939.162730-4-blakejones@google.com> Subject: [PATCH v4 3/5] perf: collect BPF metadata from new programs From: Blake Jones To: Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Jiri Olsa , Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Adrian Hunter , Kan Liang , Steven Rostedt , Tomas Glozar , James Clark , Leo Yan , Guilherme Amadio , Yang Jihong , Charlie Jenkins , Chun-Tse Shao , Aditya Gupta , Athira Rajeev , Zhongqiu Han , Andi Kleen , Dmitry Vyukov , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This collects metadata for any BPF programs that were loaded during a "perf record" run, and emits it at the end of the run. Signed-off-by: Blake Jones --- tools/perf/builtin-record.c | 10 +++++++ tools/perf/util/bpf-event.c | 46 ++++++++++++++++++++++++++++++ tools/perf/util/bpf-event.h | 1 + tools/perf/util/env.c | 19 +++++++++++- tools/perf/util/env.h | 6 ++++ tools/perf/util/header.c | 1 + tools/perf/util/synthetic-events.h | 2 ++ 7 files changed, 84 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 0b566f300569..53971b9de3ba 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -2162,6 +2162,14 @@ static int record__synthesize(struct record *rec, bo= ol tail) return err; } =20 +static void record__synthesize_final_bpf_metadata(struct record *rec __may= be_unused) +{ +#ifdef HAVE_LIBBPF_SUPPORT + perf_event__synthesize_final_bpf_metadata(rec->session, + process_synthesized_event); +#endif +} + static int record__process_signal_event(union perf_event *event __maybe_un= used, void *data) { struct record *rec =3D data; @@ -2807,6 +2815,8 @@ static int __cmd_record(struct record *rec, int argc,= const char **argv) trigger_off(&auxtrace_snapshot_trigger); trigger_off(&switch_output_trigger); =20 + record__synthesize_final_bpf_metadata(rec); + if (opts->auxtrace_snapshot_on_exit) record__auxtrace_snapshot_exit(rec); =20 diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 1f6e76ee6024..dc09a4730c50 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -472,6 +472,49 @@ void bpf_metadata_free(struct bpf_metadata *metadata _= _maybe_unused) =20 #endif /* HAVE_LIBBPF_STRINGS_SUPPORT */ =20 +struct bpf_metadata_final_ctx { + const struct perf_tool *tool; + perf_event__handler_t process; + struct machine *machine; +}; + +static void synthesize_final_bpf_metadata_cb(struct bpf_prog_info_node *no= de, + void *data) +{ + struct bpf_metadata_final_ctx *ctx =3D (struct bpf_metadata_final_ctx *)d= ata; + struct bpf_metadata *metadata =3D node->metadata; + int err; + + if (metadata =3D=3D NULL) + return; + err =3D synthesize_perf_record_bpf_metadata(metadata, ctx->tool, + ctx->process, ctx->machine); + if (err !=3D 0) { + const char *prog_name =3D metadata->prog_names[0]; + + if (prog_name !=3D NULL) + pr_warning("Couldn't synthesize final BPF metadata for %s.\n", prog_nam= e); + else + pr_warning("Couldn't synthesize final BPF metadata.\n"); + } + bpf_metadata_free(metadata); + node->metadata =3D NULL; +} + +void perf_event__synthesize_final_bpf_metadata(struct perf_session *sessio= n, + perf_event__handler_t process) +{ + struct perf_env *env =3D &session->header.env; + struct bpf_metadata_final_ctx ctx =3D { + .tool =3D session->tool, + .process =3D process, + .machine =3D &session->machines.host, + }; + + perf_env__iterate_bpf_prog_info(env, synthesize_final_bpf_metadata_cb, + &ctx); +} + /* * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf * program. One PERF_RECORD_BPF_EVENT is generated for the program. And @@ -612,6 +655,7 @@ static int perf_event__synthesize_one_bpf_prog(struct p= erf_session *session, } =20 info_node->info_linear =3D info_linear; + info_node->metadata =3D NULL; if (!perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); @@ -803,6 +847,7 @@ static void perf_env__add_bpf_info(struct perf_env *env= , u32 id) arrays |=3D 1UL << PERF_BPIL_JITED_INSNS; arrays |=3D 1UL << PERF_BPIL_LINE_INFO; arrays |=3D 1UL << PERF_BPIL_JITED_LINE_INFO; + arrays |=3D 1UL << PERF_BPIL_MAP_IDS; =20 info_linear =3D get_bpf_prog_info_linear(fd, arrays); if (IS_ERR_OR_NULL(info_linear)) { @@ -815,6 +860,7 @@ static void perf_env__add_bpf_info(struct perf_env *env= , u32 id) info_node =3D malloc(sizeof(struct bpf_prog_info_node)); if (info_node) { info_node->info_linear =3D info_linear; + info_node->metadata =3D bpf_metadata_create(&info_linear->info); if (!perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); diff --git a/tools/perf/util/bpf-event.h b/tools/perf/util/bpf-event.h index ef2dd3f1619e..60d2c6637af5 100644 --- a/tools/perf/util/bpf-event.h +++ b/tools/perf/util/bpf-event.h @@ -25,6 +25,7 @@ struct bpf_metadata { =20 struct bpf_prog_info_node { struct perf_bpil *info_linear; + struct bpf_metadata *metadata; struct rb_node rb_node; }; =20 diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 36411749e007..05a4f2657d72 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -3,8 +3,10 @@ #include "debug.h" #include "env.h" #include "util/header.h" -#include "linux/compiler.h" +#include "util/rwsem.h" +#include #include +#include #include #include #include "cgroup.h" @@ -89,6 +91,20 @@ struct bpf_prog_info_node *perf_env__find_bpf_prog_info(= struct perf_env *env, return node; } =20 +void perf_env__iterate_bpf_prog_info(struct perf_env *env, + void (*cb)(struct bpf_prog_info_node *node, + void *data), + void *data) +{ + struct rb_node *first; + + down_read(&env->bpf_progs.lock); + first =3D rb_first(&env->bpf_progs.infos); + for (struct rb_node *node =3D first; node !=3D NULL; node =3D rb_next(nod= e)) + (*cb)(rb_entry(node, struct bpf_prog_info_node, rb_node), data); + up_read(&env->bpf_progs.lock); +} + bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) { bool ret; @@ -174,6 +190,7 @@ static void perf_env__purge_bpf(struct perf_env *env) next =3D rb_next(&node->rb_node); rb_erase(&node->rb_node, root); zfree(&node->info_linear); + bpf_metadata_free(node->metadata); free(node); } =20 diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index d90e343cf1fa..c90c1d717e73 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -174,16 +174,22 @@ const char *perf_env__raw_arch(struct perf_env *env); int perf_env__nr_cpus_avail(struct perf_env *env); =20 void perf_env__init(struct perf_env *env); +#ifdef HAVE_LIBBPF_SUPPORT bool __perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); bool perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *e= nv, __u32 prog_id); +void perf_env__iterate_bpf_prog_info(struct perf_env *env, + void (*cb)(struct bpf_prog_info_node *node, + void *data), + void *data); bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node); bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_nod= e); struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id); struct btf_node *__perf_env__find_btf(struct perf_env *env, __u32 btf_id); +#endif // HAVE_LIBBPF_SUPPORT =20 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu); char *perf_env__find_pmu_cap(struct perf_env *env, const char *pmu_name, diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index e3cdc3b7b4ab..7c477e2a93b3 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -3161,6 +3161,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, = void *data __maybe_unused) /* after reading from file, translate offset to address */ bpil_offs_to_addr(info_linear); info_node->info_linear =3D info_linear; + info_node->metadata =3D NULL; if (!__perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); diff --git a/tools/perf/util/synthetic-events.h b/tools/perf/util/synthetic= -events.h index b9c936b5cfeb..ee29615d68e5 100644 --- a/tools/perf/util/synthetic-events.h +++ b/tools/perf/util/synthetic-events.h @@ -92,6 +92,8 @@ int perf_event__synthesize_threads(const struct perf_tool= *tool, perf_event__han int perf_event__synthesize_tracing_data(const struct perf_tool *tool, int = fd, struct evlist *evlist, perf_event__handler_t process); int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc, con= st struct perf_tool *tool, perf_event__handler_t process, struct machine *m= achine); pid_t perf_event__synthesize_comm(const struct perf_tool *tool, union perf= _event *event, pid_t pid, perf_event__handler_t process, struct machine *ma= chine); +void perf_event__synthesize_final_bpf_metadata(struct perf_session *sessio= n, + perf_event__handler_t process); =20 int perf_tool__process_synth_event(const struct perf_tool *tool, union per= f_event *event, struct machine *machine, perf_event__handler_t process); =20 --=20 2.50.0.rc1.591.g9c95f17f64-goog From nobody Fri Oct 10 21:10:20 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 594062D5417 for ; Thu, 12 Jun 2025 19:49:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757794; cv=none; b=naX34YGp+zLaCYRS9RUzOb2IR2DYCTwxxv3Jt0GulfpSRTh5tDNpdIlRobv7kOICtRTY5OWC223gSg1TtsXBP3FwOVRJA60kDp4AMoUH0yKX9xixqDc1nCjXDtksfqmAwTa4JNFjgn8Lhw9Gf/6Os+Z6OfuGo9TQB1E5VOrCHWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757794; c=relaxed/simple; bh=Zpl+gL2iTdwOtoVI5LL2teb9gquZyRVctFv0bvAwr08=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=D4P5JMdEFzfWcYtfSXyVB4r0Bg2po0HMGCuwuj9WpDzuc4ZDE0fZBB72RrcCfLUggyDAIRzHC2wb+grtQgSgjGRI311nfuiN8QAzcIMe7CChMJEEnPKh0nzidHCtInRPf+SiAzEO11bI15ZzxAsU7RZd5MbW2kd/GqLbDfU2bms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Mzv98nda; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Mzv98nda" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2362e130be8so11813505ad.0 for ; Thu, 12 Jun 2025 12:49:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749757789; x=1750362589; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CMS8RaOBfbTI0bE4f4YZc13pYHrZ07AfQzpRmzcnsGg=; b=Mzv98nda/DQZhtE2tTTwiRWOoxE/JMPye1u/9w2xVMerL3k7U31gRQ7WPI5jN62jSm WH5gkM3l9lVy1B+8vCeGDnYxGs41CIaq4n6FmSewfnbkgyhnBvmcqIJ1F+v8CWltRWUL sKYSaA2ftdTdEXtjrrEOrGuR8ZvhbR6vK3ujhGy6zzb0lipZgxyk+uLDRorDDhRaDRm1 l4YJLR/Dnur4SxWXAMkGeQH9aoKznojoPerzRoZokORTkp7X/fMJWrF49F6ZNfb1JrzO Il+41Gx394+mIq2Qs9PbnK/4fz7TDpLfprA4FiH7ADLXa9eShHXrzYmUWN9uF9B/0VGe +CbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749757789; x=1750362589; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CMS8RaOBfbTI0bE4f4YZc13pYHrZ07AfQzpRmzcnsGg=; b=syJgBJAqZ3DLq7QpIBNcTNzJDzjFBPf1Wcn7S24veHijTI4B3HHGqisDY/qvoQZJqe xNjKaRMT0hJgKHRdtFtTekWhgSMEV7hBRsl+HwYIkeicvgb02edRtblHcB/r2+8OHEKl Z3kvuTGbqBm/ujeKB6wcMGkXxasteyJF6bV04BU6foaRUnLIg49DUKbLBi8eFwjE3I51 Dl3+M4FPG4v5ow1Yf2axF62VGoaEsxL36g2vrgxGyfpfXnoPJz8q0uW3GpcoMnqLnVad FmT4t1y5kMeuEb5L0KyyGlvTVVHRodjqnAQAJb7hYY/A91Ajk6wlTWThsj5/xf1MgiIQ nKZg== X-Forwarded-Encrypted: i=1; AJvYcCVjRbKK+qcQw2FZ4TuwShYtctixyokepapTflgIWegzOIAl7X/uWdN0wo0jEJ2+wI7cDM4MMiCVNIipUtE=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1VYBRuXUMSgwXoUMKaxLAqGQso2ahwARfsioCO6015q+ddRmZ ctrNg89F6Tp9z1mKsXrFzWxxB27r5JhvoKYfMFCEKgH3GibxKSDBuz/mWdItPJfdZ3lCzeXhTyg ruZfKFzw8kZpgQtMKrvSxGA== X-Google-Smtp-Source: AGHT+IGijBJNdMzlmqcrnuW61J+eMb7bY/1t6JzudPAqfBJ4Jq8Dm1Qx+hCW1yYkPQ17bT9aX+jWCEUm1c6CuFer X-Received: from plbmm15.prod.google.com ([2002:a17:903:a0f:b0:234:9673:1d13]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f689:b0:235:ea0d:ae21 with SMTP id d9443c01a7336-2365dc09ad7mr4825965ad.35.1749757789645; Thu, 12 Jun 2025 12:49:49 -0700 (PDT) Date: Thu, 12 Jun 2025 12:49:38 -0700 In-Reply-To: <20250612194939.162730-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250612194939.162730-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc1.591.g9c95f17f64-goog Message-ID: <20250612194939.162730-5-blakejones@google.com> Subject: [PATCH v4 4/5] perf: display the new PERF_RECORD_BPF_METADATA event From: Blake Jones To: Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Jiri Olsa , Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Adrian Hunter , Kan Liang , Steven Rostedt , Tomas Glozar , James Clark , Leo Yan , Guilherme Amadio , Yang Jihong , Charlie Jenkins , Chun-Tse Shao , Aditya Gupta , Athira Rajeev , Zhongqiu Han , Andi Kleen , Dmitry Vyukov , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Here's some example "perf script -D" output for the new event type. The ": unhandled!" message is from tool.c, analogous to other behavior there. I've elided some rows with all NUL characters for brevity, and I wrapped one of the >75-column lines to fit in the commit guidelines. 0x50fc8@perf.data [0x260]: event: 84 =2E . ... raw event: size 608 bytes . 0000: 54 00 00 00 00 00 60 02 62 70 66 5f 70 72 6f 67 T.....`.bpf_prog . 0010: 5f 31 65 30 61 32 65 33 36 36 65 35 36 66 31 61 _1e0a2e366e56f1a . 0020: 32 5f 70 65 72 66 5f 73 61 6d 70 6c 65 5f 66 69 2_perf_sample_fi . 0030: 6c 74 65 72 00 00 00 00 00 00 00 00 00 00 00 00 lter............ . 0040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [...] . 0110: 74 65 73 74 5f 76 61 6c 75 65 00 00 00 00 00 00 test_value...... . 0120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [...] . 0150: 34 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42.............. . 0160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [...] 0 0x50fc8 [0x260]: PERF_RECORD_BPF_METADATA \ prog bpf_prog_1e0a2e366e56f1a2_perf_sample_filter entry 0: test_value =3D 42 : unhandled! Signed-off-by: Blake Jones --- tools/perf/builtin-inject.c | 1 + tools/perf/builtin-script.c | 15 +++++++++++++-- tools/perf/util/event.c | 21 +++++++++++++++++++++ tools/perf/util/event.h | 1 + tools/perf/util/session.c | 4 ++++ tools/perf/util/tool.c | 14 ++++++++++++++ tools/perf/util/tool.h | 3 ++- 7 files changed, 56 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 11e49cafa3af..b15eac0716f7 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -2530,6 +2530,7 @@ int cmd_inject(int argc, const char **argv) inject.tool.finished_init =3D perf_event__repipe_op2_synth; inject.tool.compressed =3D perf_event__repipe_op4_synth; inject.tool.auxtrace =3D perf_event__repipe_auxtrace; + inject.tool.bpf_metadata =3D perf_event__repipe_op2_synth; inject.tool.dont_split_sample_group =3D true; inject.session =3D __perf_session__new(&data, &inject.tool, /*trace_event_repipe=3D*/inject.output.is_pipe); diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 6c3bf74dd78c..4001e621b6cb 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -38,6 +38,7 @@ #include "print_insn.h" #include "archinsn.h" #include +#include #include #include #include @@ -50,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -2755,6 +2757,14 @@ process_bpf_events(const struct perf_tool *tool __ma= ybe_unused, sample->tid); } =20 +static int +process_bpf_metadata_event(struct perf_session *session __maybe_unused, + union perf_event *event) +{ + perf_event__fprintf(event, NULL, stdout); + return 0; +} + static int process_text_poke_events(const struct perf_tool *tool, union perf_event *event, struct perf_sample *sample, @@ -2877,8 +2887,9 @@ static int __cmd_script(struct perf_script *script) script->tool.finished_round =3D process_finished_round_event; } if (script->show_bpf_events) { - script->tool.ksymbol =3D process_bpf_events; - script->tool.bpf =3D process_bpf_events; + script->tool.ksymbol =3D process_bpf_events; + script->tool.bpf =3D process_bpf_events; + script->tool.bpf_metadata =3D process_bpf_metadata_event; } if (script->show_text_poke_events) { script->tool.ksymbol =3D process_bpf_events; diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 7544a3104e21..14b0d3689137 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -1,9 +1,12 @@ #include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -78,6 +81,7 @@ static const char *perf_event__names[] =3D { [PERF_RECORD_COMPRESSED] =3D "COMPRESSED", [PERF_RECORD_FINISHED_INIT] =3D "FINISHED_INIT", [PERF_RECORD_COMPRESSED2] =3D "COMPRESSED2", + [PERF_RECORD_BPF_METADATA] =3D "BPF_METADATA", }; =20 const char *perf_event__name(unsigned int id) @@ -505,6 +509,20 @@ size_t perf_event__fprintf_bpf(union perf_event *event= , FILE *fp) event->bpf.type, event->bpf.flags, event->bpf.id); } =20 +size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp) +{ + struct perf_record_bpf_metadata *metadata =3D &event->bpf_metadata; + size_t ret; + + ret =3D fprintf(fp, " prog %s\n", metadata->prog_name); + for (__u32 i =3D 0; i < metadata->nr_entries; i++) { + ret +=3D fprintf(fp, " entry %d: %20s =3D %s\n", i, + metadata->entries[i].key, + metadata->entries[i].value); + } + return ret; +} + static int text_poke_printer(enum binary_printer_ops op, unsigned int val, void *extra, FILE *fp) { @@ -602,6 +620,9 @@ size_t perf_event__fprintf(union perf_event *event, str= uct machine *machine, FIL case PERF_RECORD_AUX_OUTPUT_HW_ID: ret +=3D perf_event__fprintf_aux_output_hw_id(event, fp); break; + case PERF_RECORD_BPF_METADATA: + ret +=3D perf_event__fprintf_bpf_metadata(event, fp); + break; default: ret +=3D fprintf(fp, "\n"); } diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h index 664bf39567ce..67ad4a2014bc 100644 --- a/tools/perf/util/event.h +++ b/tools/perf/util/event.h @@ -370,6 +370,7 @@ size_t perf_event__fprintf_namespaces(union perf_event = *event, FILE *fp); size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp); size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp); size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp); +size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp); size_t perf_event__fprintf_text_poke(union perf_event *event, struct machi= ne *machine,FILE *fp); size_t perf_event__fprintf(union perf_event *event, struct machine *machin= e, FILE *fp); =20 diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index a320672c264e..38075059086c 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -12,6 +12,7 @@ #include #include #include +#include =20 #include "map_symbol.h" #include "branch.h" @@ -1491,6 +1492,9 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, case PERF_RECORD_FINISHED_INIT: err =3D tool->finished_init(session, event); break; + case PERF_RECORD_BPF_METADATA: + err =3D tool->bpf_metadata(session, event); + break; default: err =3D -EINVAL; break; diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c index 37bd8ac63b01..204ec03071bc 100644 --- a/tools/perf/util/tool.c +++ b/tools/perf/util/tool.c @@ -1,12 +1,15 @@ // SPDX-License-Identifier: GPL-2.0 #include "data.h" #include "debug.h" +#include "event.h" #include "header.h" #include "session.h" #include "stat.h" #include "tool.h" #include "tsc.h" +#include #include +#include #include =20 #ifdef HAVE_ZSTD_SUPPORT @@ -237,6 +240,16 @@ static int perf_session__process_compressed_event_stub= (struct perf_session *sess return 0; } =20 +static int perf_event__process_bpf_metadata_stub(struct perf_session *perf= _session __maybe_unused, + union perf_event *event) +{ + if (dump_trace) + perf_event__fprintf_bpf_metadata(event, stdout); + + dump_printf(": unhandled!\n"); + return 0; +} + void perf_tool__init(struct perf_tool *tool, bool ordered_events) { tool->ordered_events =3D ordered_events; @@ -293,6 +306,7 @@ void perf_tool__init(struct perf_tool *tool, bool order= ed_events) tool->compressed =3D perf_session__process_compressed_event_stub; #endif tool->finished_init =3D process_event_op2_stub; + tool->bpf_metadata =3D perf_event__process_bpf_metadata_stub; } =20 bool perf_tool__compressed_is_stub(const struct perf_tool *tool) diff --git a/tools/perf/util/tool.h b/tools/perf/util/tool.h index db1c7642b0d1..18b76ff0f26a 100644 --- a/tools/perf/util/tool.h +++ b/tools/perf/util/tool.h @@ -77,7 +77,8 @@ struct perf_tool { stat, stat_round, feature, - finished_init; + finished_init, + bpf_metadata; event_op4 compressed; event_op3 auxtrace; bool ordered_events; --=20 2.50.0.rc1.591.g9c95f17f64-goog From nobody Fri Oct 10 21:10:20 2025 Received: from mail-oa1-f73.google.com (mail-oa1-f73.google.com [209.85.160.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F4602D8789 for ; Thu, 12 Jun 2025 19:49:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757794; cv=none; b=dPQUbUdkZFJaIdlO1KEVycZd2st8agJnElpYG3T9hldvdPwj+/FVEo6uXPP5MLHcDtI4e2ATd205ZTatFQc5YWG2NlXe8c5LUAmJ8hFE78LqYvY0TesPQf+EC8oI4+yZXJr0sQI33IboGD5FKTC7CNU/qNQusfNBu4vIFe9goB4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749757794; c=relaxed/simple; bh=zMY1NElYNZzt1bGCpQPOES94FzbCwXlf9lyBYpGO4Zs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dph+dltAvt21pGVFb//w9to7HKdLIktuIkzG+KXSXjg7AgL0O+APYUa5yS4OH3SiK3mNJDWC8iVecx8oowHTsKhVNnkP0Q6A+JUmZ1msjLL1hWEFMDRqM1g7shWhtz+SCxN0Zcf+L1dzA+Zq8umP8xMBDEzaAZEQK74vRYKdrr4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pf/YFNdf; arc=none smtp.client-ip=209.85.160.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pf/YFNdf" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-2e922a13d49so1066259fac.2 for ; Thu, 12 Jun 2025 12:49:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749757791; x=1750362591; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tnqUhIyewIO5q6Zf7WHC0dqAJTno3LzWwwCSTFeopTU=; b=pf/YFNdf2TFfW8W83c3aYcwSTXRNJDv3IiR1eZN/O2v4IhP+nduHsTIIyYMUuRTEBQ 0IHtnZIGAAKux6EsiAibo7Cx7EYjgY2hW19ApTWr9sn1hmhtenpfCoWbT+rXVAsheCU7 7GfvmlwA/SfzF5b51VFFHPn1E33Cq+K6UQSS6ucoIwhqhuz5VL76DQCRyVg6/WwZYT8Q dX3fYRTqPHiLyGcqugEZmhsexeL6pDyeWaliA/BnGop2PbFqgA26kFnVWOi54n/YRWDt 8BzJe4GVKAyx5wHSnsLuCM/VoxDOfageIEzL1R8Zg4lyIxsitBO9cUUbTYxbPZIDVnsb XtOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749757791; x=1750362591; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tnqUhIyewIO5q6Zf7WHC0dqAJTno3LzWwwCSTFeopTU=; b=kr4/o9aRqPpJuYCZE04Z9LoFI6dEMJF5GdQBprLn4+AifMYzCdX/Iv9HT7HaEevuVh X6ohB0XrEME1Wq382rtUVcKinAsyQKwIR8OMrsy0wE8nWlipDKG9eXgvTHEUPJshPufj /HkFDGgUNcAksLnSHT85cO8L+oDzQZowtl5yo1fCMV3FPUt0jJI+vx2Jz5w/kJBvNafk mK9T7DiYCP4p5oTnrnWMTSjPZC5Mcjj5Ma6QY05YdfKLsoK7D4sfGI9lChqMVbYWCoGJ QGJKtydskrnFOwZN9TuMNtebPDX+3zBIzTQsk4PoQ1usw6qrxMCCCML/HFuX/pJsWoKT 2GXw== X-Forwarded-Encrypted: i=1; AJvYcCViWKxjJ/Y5m/Qtmocsi6XkSezieoXcT6aTBg3gDAqwpTZlS0CoQN1r6eXQD0ymgchScCPziUooBijOaik=@vger.kernel.org X-Gm-Message-State: AOJu0Ywq0ycDdxdJ91DQya9gOSAQaqXGhkuKlrPBZOnTFYuwE6Vuw3qb KESqadu+9Yiof7o5+Ra6pgXQ2b+DAAysbrBreiQ+KlRx04xqDau7rpQz4KxcxEhTg6zmS54qkB7 jV3UdycCZ8y2hPWJcoT9irw== X-Google-Smtp-Source: AGHT+IFNTXR/9UID8W6zRYCP3OwhC3x1U++y9rRlwJYFwQhSIGl+YEKkxpm2xUoLbRLXAHqqMRE3zncdhPzwhCzN X-Received: from oabqw11.prod.google.com ([2002:a05:6870:6f0b:b0:2d9:841d:6155]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6871:4002:b0:2b8:e4b9:47a3 with SMTP id 586e51a60fabf-2ead509b590mr228640fac.22.1749757791440; Thu, 12 Jun 2025 12:49:51 -0700 (PDT) Date: Thu, 12 Jun 2025 12:49:39 -0700 In-Reply-To: <20250612194939.162730-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250612194939.162730-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc1.591.g9c95f17f64-goog Message-ID: <20250612194939.162730-6-blakejones@google.com> Subject: [PATCH v4 5/5] perf: add test for PERF_RECORD_BPF_METADATA collection From: Blake Jones To: Arnaldo Carvalho de Melo , Namhyung Kim , Ian Rogers , Jiri Olsa , Peter Zijlstra , Ingo Molnar Cc: Mark Rutland , Alexander Shishkin , Adrian Hunter , Kan Liang , Steven Rostedt , Tomas Glozar , James Clark , Leo Yan , Guilherme Amadio , Yang Jihong , Charlie Jenkins , Chun-Tse Shao , Aditya Gupta , Athira Rajeev , Zhongqiu Han , Andi Kleen , Dmitry Vyukov , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This is an end-to-end test for the PERF_RECORD_BPF_METADATA support. It adds a new "bpf_metadata_perf_version" variable to perf's BPF programs, so that when they are loaded, there will be at least one BPF program with some metadata to parse. The test invokes "perf record" in a way that loads one of those BPF programs, and then sifts through the output to find its BPF metadata. Signed-off-by: Blake Jones --- tools/perf/Makefile.perf | 3 +- tools/perf/tests/shell/test_bpf_metadata.sh | 76 +++++++++++++++++++++ tools/perf/util/bpf_skel/perf_version.h | 17 +++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100755 tools/perf/tests/shell/test_bpf_metadata.sh create mode 100644 tools/perf/util/bpf_skel/perf_version.h diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index d4c7031b01a7..4f292edeca5a 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -1250,8 +1250,9 @@ else $(Q)cp "$(VMLINUX_H)" $@ endif =20 -$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(LIBBPF) $(SKEL_OUT)/vmlin= ux.h | $(SKEL_TMP_OUT) +$(SKEL_TMP_OUT)/%.bpf.o: util/bpf_skel/%.bpf.c $(OUTPUT)PERF-VERSION-FILE = util/bpf_skel/perf_version.h $(LIBBPF) $(SKEL_OUT)/vmlinux.h | $(SKEL_TMP_O= UT) $(QUIET_CLANG)$(CLANG) -g -O2 --target=3Dbpf $(CLANG_OPTIONS) $(BPF_INCLU= DE) $(TOOLS_UAPI_INCLUDE) \ + -include $(OUTPUT)PERF-VERSION-FILE -include util/bpf_skel/perf_version= .h \ -c $(filter util/bpf_skel/%.bpf.c,$^) -o $@ =20 $(SKEL_OUT)/%.skel.h: $(SKEL_TMP_OUT)/%.bpf.o | $(BPFTOOL) diff --git a/tools/perf/tests/shell/test_bpf_metadata.sh b/tools/perf/tests= /shell/test_bpf_metadata.sh new file mode 100755 index 000000000000..11df592fb661 --- /dev/null +++ b/tools/perf/tests/shell/test_bpf_metadata.sh @@ -0,0 +1,76 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# BPF metadata collection test. + +set -e + +err=3D0 +perfdata=3D$(mktemp /tmp/__perf_test.perf.data.XXXXX) + +cleanup() { + rm -f "${perfdata}" + rm -f "${perfdata}".old + trap - EXIT TERM INT +} + +trap_cleanup() { + cleanup + exit 1 +} +trap trap_cleanup EXIT TERM INT + +test_bpf_metadata() { + echo "Checking BPF metadata collection" + + if ! perf check -q feature libbpf-strings ; then + echo "Basic BPF metadata test [skipping - not supported]" + err=3D0 + return + fi + + # This is a basic invocation of perf record + # that invokes the perf_sample_filter BPF program. + if ! perf record -e task-clock --filter 'ip > 0' \ + -o "${perfdata}" sleep 1 2> /dev/null + then + echo "Basic BPF metadata test [Failed record]" + err=3D1 + return + fi + + # The BPF programs that ship with "perf" all have the following + # variable defined at compile time: + # + # const char bpf_metadata_perf_version[] SEC(".rodata") =3D <...>; + # + # This invocation looks for a PERF_RECORD_BPF_METADATA event, + # and checks that its content contains the string given by + # "perf version". + VERS=3D$(perf version | awk '{print $NF}') + if ! perf script --show-bpf-events -i "${perfdata}" | awk ' + /PERF_RECORD_BPF_METADATA.*perf_sample_filter/ { + header =3D 1; + } + /^ *entry/ { + if (header) { header =3D 0; entry =3D 1; } + } + $0 !~ /^ *entry/ { + entry =3D 0; + } + /perf_version/ { + if (entry) print $NF; + } + ' | egrep "$VERS" > /dev/null + then + echo "Basic BPF metadata test [Failed invalid output]" + err=3D1 + return + fi + echo "Basic BPF metadata test [Success]" +} + +test_bpf_metadata + +cleanup +exit $err diff --git a/tools/perf/util/bpf_skel/perf_version.h b/tools/perf/util/bpf_= skel/perf_version.h new file mode 100644 index 000000000000..1ed5b2e59bf5 --- /dev/null +++ b/tools/perf/util/bpf_skel/perf_version.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ + +#ifndef __PERF_VERSION_H__ +#define __PERF_VERSION_H__ + +#include "vmlinux.h" +#include + +/* + * This is used by tests/shell/record_bpf_metadata.sh + * to verify that BPF metadata generation works. + * + * PERF_VERSION is defined by a build rule at compile time. + */ +const char bpf_metadata_perf_version[] SEC(".rodata") =3D PERF_VERSION; + +#endif /* __PERF_VERSION_H__ */ --=20 2.50.0.rc1.591.g9c95f17f64-goog