From nobody Fri Dec 19 07:50:24 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 C5A0123C8BE for ; Fri, 6 Jun 2025 21:52:53 +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=1749246775; cv=none; b=JhfgS7XFt7P3hm5/2JSxwR93N1TpQaLwk92TMhK7JW68hjerOTtMZ+foeNTaszqekiv7Xnx3Ah/Zcc9xzTd5mz8sDlUbY9IL2cc/TD6TmFeOasVO899epc/PdlS0QuymgkeaIxEz+wM0g306riMC4Oa1v2djTCsexinc8QhZkrY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749246775; c=relaxed/simple; bh=fdk50WYueaBLzSwBgidVg7pckEa/1AfhDiTbjBNVjvo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QXVckRjwWkRQi+Wbwgxaq7BKDwpq30SGiN8IV2PoUbwHKbEMCk6cl2EVSHCn3JPlyeEIIXegje0qQD5dq/OeEyTLZP93W5Rg0GHpm40tl37eD+hAotm2aFipZ/nJDR64u/ZDzw7+0IVabccsW2bUES6rJu0bm+qeZ0GQ/dXu6ig= 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=uK/aKTd/; 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="uK/aKTd/" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2350804a43eso37104515ad.0 for ; Fri, 06 Jun 2025 14:52:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749246773; x=1749851573; 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=D+kZBatEUrJrZZ6PE0Z1l/q9xeZJJ3Utgr+97hvf5fQ=; b=uK/aKTd/Adr+/dFRFxIfcWYYJ00FUhuc0ocrZJKSq5kXY/09xgScHNf4OK6ylS48hf ihlOqcmixyysf7LSwErV7gm/xMaMClXYpnhbnF6MGBL5GKYtSfzuWEG+l9TGICe/QGOz Z5TjyIYXzIMo+3GpAUz8ynJGjA/keqSMtYyMCj7Bp+zkgqrOP5xVeXGkBSgXhYtxRJpA OnSgbG2p76EIpJfog9lOqw4Z1OgdMAra9aSWJEbFOjKpPeCb4DCZNhfMz2c1tzfh5k5o 7bN+E2LfUapjXbUPlwLTYNiyTu/8rfD8Ek7gLm0Bzkm2eIAS1eCi2ftqTLAruZY5bPzb As8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749246773; x=1749851573; 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=D+kZBatEUrJrZZ6PE0Z1l/q9xeZJJ3Utgr+97hvf5fQ=; b=ZnxEqVbua0xg0JIOb4kFq/3Dpzx+gJ5Rn2yeZae1wmmAGjz+QEb/ZGNFSVE8iJTDzi hqSdQhRf/4ihOOY/ErspjXb+o7odujmDsNJMTlWnUyxzqBqoIZTCnm9btQ7vSFihT6qt 1Bl2qI+rfZDe0JYNxc/j3r4jqoKfu4OZIFZ6OqknG82UtPfQ/2iUZJbAT4praFejyJ+Y ZdsFiPKxrjsVB72LtfGJA+uaIkFPfcFzZ/Ku6n4LyoE9UTtXHQ9AdIZd+2ksHlR+ilF4 Kiigf32vOU1tDuUt2DLovBobyuhy9XMo+bbit4AsjebdTnRNpsd3TtEsMpjPC5tXwaxA 404A== X-Forwarded-Encrypted: i=1; AJvYcCU7KMfbIdU5vCc1b+6yZ0dDPmvfR8cnCsyq+noU4AydJeFerasqAFGrNQWssXsg1v/rWl+XERX7SOmFoys=@vger.kernel.org X-Gm-Message-State: AOJu0Yynz71RnaGjzMMIkufj2t6dkVlw9UK7OjKtjq4uP6K5+b/WmXTN quHvnyB5nUR3xftMW7jyOrpT5q4K9/QVzWcLauxnQGMHEO2nTkYs2wCeB9FMUDuAF1lNRrCFoqv ETOFCoqSWtjTgbWkqrfqIhw== X-Google-Smtp-Source: AGHT+IGUB45yrNx9LuqRMIDTKKRwgWQUuD4w9NE1Ld77LnJv215C9tYBR+DSbX0KgD3MHda8qh5AmDJxb4IqBytr X-Received: from pjm5.prod.google.com ([2002:a17:90b:2fc5:b0:313:245:8921]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1dcb:b0:312:639:a064 with SMTP id 98e67ed59e1d1-3134768d9cbmr7402437a91.28.1749246773007; Fri, 06 Jun 2025 14:52:53 -0700 (PDT) Date: Fri, 6 Jun 2025 14:52:42 -0700 In-Reply-To: <20250606215246.2419387-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: <20250606215246.2419387-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc0.604.gd4ff7b7c86-goog Message-ID: <20250606215246.2419387-2-blakejones@google.com> Subject: [PATCH v3 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 | 12 ++++++++++++ tools/perf/builtin-check.c | 1 + 6 files changed, 29 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..647ade45e4e5 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -595,8 +595,20 @@ ifndef NO_LIBELF LIBBPF_STATIC :=3D 1 $(call detected,CONFIG_LIBBPF) CFLAGS +=3D -DHAVE_LIBBPF_SUPPORT + ifneq ($(OUTPUT),) + LIBBPF_INCLUDE =3D $(abspath $(OUTPUT))/libbpf/include + else + LIBBPF_INCLUDE =3D $(CURDIR)/libbpf/include + endif 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.rc0.604.gd4ff7b7c86-goog From nobody Fri Dec 19 07:50:24 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 3A7C523D298 for ; Fri, 6 Jun 2025 21:52:55 +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=1749246777; cv=none; b=cZtk3ndxrlp9DZN0ge093WGBSf6HU3updzYxlE//tLqLvP222g5h/ABlpdT5e2oGJVz7rqjGVByftj6TBC4nJjGAiBIjxXHeeGJRoKAmdzPI8id3bGGnUQQF1p25PIo0Tt4vI9HauunJUVhsyabKd5DUjOAjPoTddsMvzdvVoS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749246777; c=relaxed/simple; bh=lv+bM4moTnKOGlGoG2cAgW1utmnJXzA7idT/X6DTndA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=S1DZhI+mqdnEqzXomtIZOILGaOpgsP59RKvyF5Th86nebDwFziviiLzaceU8CwKHLColNOzkFdqqoHYMSgwZjHOD/g3DHHnM4sYuNO2M4eBua1fqVbIN1hBPbRmotKBi/JQNbiM63hT0d4CsbBcZcfLug6jSCxaLSPpaQVQcKjU= 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=kFqnDDXj; 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="kFqnDDXj" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-31171a736b2so4056996a91.1 for ; Fri, 06 Jun 2025 14:52:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749246774; x=1749851574; 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=slKzUD48tkq9i10Iz9b2HZco4LcIFVLTatkKbkE7Sdg=; b=kFqnDDXjxgrkpGIJE4QVRIuH3T9oUGNwkLFeO8SKY1UMB850XHTqntDA09hu3XvnS2 nrgCwLytlQ3c5XvL2hvomgB43YMgwICPPDYnuWQE5wACVKqKDPf9kkX1FQ6RoUrvosPY RsPpDp/9BU30T7ugSHxb5uG7JwThprZhzRKJSIBP0hgtkbqpax12NBz3SgLMZQ1WP/k1 E35nb6E1AG067qxF6er+gCivzIS16nkkcvybAf8YetUr3PLOjAT/bzDsBe1TQ7dUfLA/ BOofQHUkurTw3PF63fbyud6VSRzaqC5HADvsrgx0PKiOtm9keXQw+l61ri2WcK4qxV55 h6YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749246774; x=1749851574; 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=slKzUD48tkq9i10Iz9b2HZco4LcIFVLTatkKbkE7Sdg=; b=FUCdkKeWhFRYWLijbbNOGQjKyfBt5bMZUFwzOZq3KKcAuZhXYaAVbV1t2Kq+UgkuBq yjHMm3MJVvPrz6bGAteWeaMivYhnVFLlXsHR9mGKmQEML5qQvyYtvP0niXcdC1qhogVz QfHoNDGvkASp7mQ/l49DFfMa0ypaqbe/Fd6m9plqxsl4YZ/bhx33G+pHvwIu2FzFOUzY gwM5V1gH/8Ja710XGAOpFxqMqDcEAXu9G20JTbN5zFFM/5jJMXNKN+/HtxcqMGfEMbZm aDU5yOcIhTFAtx+eAqQ2KUhAzrmSKJD0GcgKt0g/JQXAlFE685KrDGcgTlA7FnxdDsd+ jvvQ== X-Forwarded-Encrypted: i=1; AJvYcCUy3eb6TN070zQ+Sp5fKb7w9kNowuQg+tQ5qypuKPdr9NCxZzU6X856LzBorLx/xu0s2/7lLYqa8NG3iUY=@vger.kernel.org X-Gm-Message-State: AOJu0YwPEdKNpOmWtqzxJ7WmDB8scJB3p/Kbar1sE9+bfP/TOHxE27TH aHkfTLx8mgjmZYpJ94HEmajw29J9U7j7lRta2llylgrovrmu8Yfs9usvvL8SG24r7pe7loVqI9J T0xmulHbC4pj4kDAfJL1meg== X-Google-Smtp-Source: AGHT+IH9OKDmiuyxsq7eGthfr+1SBBAtvBz2KikWHiT6h2hbtjF4zNjHaav0zRN18sqs2hVDsbWG2mWLzbOwmKOs X-Received: from pjbsi11.prod.google.com ([2002:a17:90b:528b:b0:312:1c59:43a6]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:dfc6:b0:311:b6d2:4c36 with SMTP id 98e67ed59e1d1-3134768b6c7mr5977145a91.26.1749246774667; Fri, 06 Jun 2025 14:52:54 -0700 (PDT) Date: Fri, 6 Jun 2025 14:52:43 -0700 In-Reply-To: <20250606215246.2419387-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: <20250606215246.2419387-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc0.604.gd4ff7b7c86-goog Message-ID: <20250606215246.2419387-3-blakejones@google.com> Subject: [PATCH v3 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..16644b3aaba1 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) +{ + } #endif // HAVE_LIBBPF_SUPPORT #endif --=20 2.50.0.rc0.604.gd4ff7b7c86-goog From nobody Fri Dec 19 07:50:24 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 13F9023E34F for ; Fri, 6 Jun 2025 21:52:56 +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=1749246778; cv=none; b=fJav6VAEi66cAANs1Tj82ccDkEFLj+4legnFteqIEXgmOZ2/okOMf8QAIqaoJ44N5ir4K27Ot58zv6jJammZQP9sqmhu+HUfg99gBOd7/Z0eF18AYd0iA3/rt99pmd9/WUL7PL5QiQKLCuwfsUcnUo/mjdRqQ2WSDcmUlEyW/ZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749246778; c=relaxed/simple; bh=OhUv3r9YK7S2vWeklKY7wXIyczrwBkYk68z5KVxE38M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZCfnAMpvmuW3bTGV85uTwcpsrmqYuAhc8+BM/xwuLVOVY7CYIAUWm49rSv6u7Xe+8mh5BwS4S0cWdVFWovWHsPl3nGLT1TUMqogUiOXuWVSRJn9MsU0qConIsHnueVZeoTk16BZKcKTakOQ38WGbNzL5izYz4ltClSjStzsOjTU= 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=4qPF8LeJ; 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="4qPF8LeJ" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-234f1acc707so22378385ad.3 for ; Fri, 06 Jun 2025 14:52:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749246776; x=1749851576; 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=JS/Db2SUjK5/g3LbclVsWOHT1Smx8t5HOzo/CvKXbp4=; b=4qPF8LeJGIuGPOEENYNF/0NRGd0KoYHWcEFG7U3ywnkDjaD1Hd4OyKdRSlBgwzCEh7 OiSvEHG2lAjh/Ak2x4mPQ0Ylqe3m7EkiZQ5Dq8TAXJB39jjb3hgplQy+6iGtB18MB5zy m8KXh8MZxEc5g+466Qx5aUngHFKsPLM5QleIQ+PRh+ktmI0MrUZ0lZFfOpmI/uuO2Ha6 iNq25gFrPFg+IY6hUl9Y8IzTawYzE0fZuHbJ1kdacV0pyza8hBbSgnWcwrZ22F9ikcci bA1BIdo3ktOzLTtrO3ACI+cBaVoazqUx1I6BdFNI2XqCHYh8oHNGLoc1jYAjC1TpT/OZ FUaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749246776; x=1749851576; 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=JS/Db2SUjK5/g3LbclVsWOHT1Smx8t5HOzo/CvKXbp4=; b=sNt7gqf2Q7Tf9IZVtEOYTTbDyQ/vpL9Ecx/1lBomfonkqcKTjge4rGfi3iChXbuJq+ gC6AiDeIHYYrFKJPppYKfnxZUct8a5+LCOonyplHz0b0wTLdJsntAQyTXQrWSjFPwg6w WhlMT4ToNPS2P4ot3QJz1KxcDZfJ6TAJp9pSmWkJahQxp2ukqk0uoAr0r9t+SOa825h5 MKJXn0gbMSfbTa7yfG8Jv+Nrnj5L9VJMHaAIqFWYAO0WP8lWOyordO9E20Sh+4J3KWTa TfE5SD6Vr9x9jIZel6z1CSj7V0FF0BrH2h9zD4YScs6rQi+gy09nMArMGxb9xOLnRwSQ mP1A== X-Forwarded-Encrypted: i=1; AJvYcCWo1np2/QbLg3NumSXg7hIRILmexBKMZpPqPJoPeOIRsko4l3UkhucImWZdx3dq6NUUE87SayjWROphfM0=@vger.kernel.org X-Gm-Message-State: AOJu0Yyb/eBFYA9ptI7VeHOtw1tmceBYwkMbAqFi9XRF+TBSuj8st6d0 0jLSGwKXZul+/uFhxPJtstWy8hAw5zLnBRJ7fiOWL2NKt6qWuBYWw+4bD8xFYqLI3qBn7Hp0vmb JapTI5CsBFwGUkcVvP/sCsw== X-Google-Smtp-Source: AGHT+IGlHDIsXQ6iCILcEd7JatNtcpZ/R5Fn6/EzRFCZispX4sWEu4cmFjgXt5Xg05e2D5V/72S4Vg3WxP0+mCk4 X-Received: from plnz4.prod.google.com ([2002:a17:902:8344:b0:234:d2de:e601]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:244c:b0:235:f3df:bc1f with SMTP id d9443c01a7336-23601dc4688mr76887345ad.36.1749246776340; Fri, 06 Jun 2025 14:52:56 -0700 (PDT) Date: Fri, 6 Jun 2025 14:52:44 -0700 In-Reply-To: <20250606215246.2419387-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: <20250606215246.2419387-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc0.604.gd4ff7b7c86-goog Message-ID: <20250606215246.2419387-4-blakejones@google.com> Subject: [PATCH v3 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 | 8 ++++++ 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 | 4 +++ tools/perf/util/header.c | 1 + tools/perf/util/synthetic-events.h | 2 ++ 7 files changed, 80 insertions(+), 1 deletion(-) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 0b566f300569..47adee0c2a1c 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -2162,6 +2162,12 @@ static int record__synthesize(struct record *rec, bo= ol tail) return err; } =20 +static void record__synthesize_final_bpf_metadata(struct record *rec) +{ + perf_event__synthesize_final_bpf_metadata(rec->session, + process_synthesized_event); +} + static int record__process_signal_event(union perf_event *event __maybe_un= used, void *data) { struct record *rec =3D data; @@ -2807,6 +2813,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 16644b3aaba1..1ed0b36dc3b8 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..6819cb9b99ff 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -180,6 +180,10 @@ bool perf_env__insert_bpf_prog_info(struct perf_env *e= nv, 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); 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.rc0.604.gd4ff7b7c86-goog From nobody Fri Dec 19 07:50:24 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 D11EF23F40F for ; Fri, 6 Jun 2025 21:52:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749246781; cv=none; b=uy8qWMVlAErRJX9AsORQJH8G/E78LguaWibt2NMMTIn0XyZb7UFDE1XIhxkIIyCJUAlHPv3oC72+ov3V5xxcdEbYuCsah9FDH3t0dZmDNFp+H+tKtva5ZGFoZt2N8hLJBf895BxeV0dKUU+MF6y7vDt+ndd3AC4MxYqeFIZE2b8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749246781; c=relaxed/simple; bh=56RIbi1wHtAnGWt9TO+tS+DbBV7lYKLepiYAO8j9APg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AqXsDT03zIaADQ6fLQS9NQ72OOGO0WZan670ChGU7dgs0fytxq2EcOTPzoqkhdnO2Kh0kTDTqCpMFLoqec/i6sIWllBM5G5SE0ylJTDuuA78OdCoSw6w9+++dD8NBRlkNfvH6c8Y0S+E/xMeyfZb/0os2f2WQ1K+Zgph4IDg6P8= 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=S23e44vL; arc=none smtp.client-ip=209.85.215.201 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="S23e44vL" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2eb60594e8so1465914a12.1 for ; Fri, 06 Jun 2025 14:52:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749246778; x=1749851578; 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=62r4G4HXTwI4+axBAzatF9lbY5pnGvZEQZG3dQIurTE=; b=S23e44vLNzU5+XjMiIBWvLjJJhIYKgnNL4fQ91CiUbpSc767OJH8tQ7ILoKv3pe82y dSasTPIi2ziEtL6+R9V3bqMQbA8/0Po5Fh0UGBf60StpITKBupAQpBtvMeH2BULHAmiX GzsLPzXnqopZIyV0L2Na6dYFDbA3AdvXw2SiNZod/YdQwTy+LqxeCeStQVHo0K+xI6+9 Ft9ewsotdfEcu2x/wfkeGsndj3FWAjRjYszLhI9NrXaMcFsk61xFKS7ntGGecwuRjxCG 8yVXpUePpQd6clu7WdpdFOC/fnKuXWzvuYmy+avuvis4trKwQ89euZPMDRaPoHXojxTk b8KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749246778; x=1749851578; 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=62r4G4HXTwI4+axBAzatF9lbY5pnGvZEQZG3dQIurTE=; b=l7pJMyGw6cL8d468jTDczpXD+RO8Z+IKnrhcb4TKgjYUUNK3gpxgRee1I9m4yLMhWw Q74xJAVXC0xTOxgdZUOVsrxXBtoqwEgHLOj0YNi3cTOEZtSEW2BLublKhVav9smjkwYl H4wTUurg34A1YZndwhwo7gidnxHfRuot98FRVhAWvizhahbswFbAWKhd5x4SrIcQ8W5J BTydj/Db+rCYG56WtGAsQFfJDSsD+vzZE65g36LunklsUPRkfrDXH4LmGG535LLwMhQS 1XN4/O8pUpGiZAJtmSNLBcrX3Dm0Eq9L57ZUwOi4tGYsm8Y6pSwBetMVQz2tiKbT9iLr qzbw== X-Forwarded-Encrypted: i=1; AJvYcCVKD5tRLh03b6uMjnla1Sf9obC9cuRN9OTgbb439oH/iCh6SVva72MXG+mYjgbArfuVrW1BfEtDhi9FMW4=@vger.kernel.org X-Gm-Message-State: AOJu0Yww1LTSguG3u7PFOVE2UfT1PpNfTci5uRVH7CIH9fHWOAj8pa5u IQM1xCBI7Wf5CJx7pFxW65CtjNEIsdkVYmZPgBucJhmFNOMKls30u9KSxMHHYBfIRfKS7blhsWE ycbyZ+d+kwTfRQZYoA0ozxQ== X-Google-Smtp-Source: AGHT+IFb+oRo7Gdo3AEE2CFdv4bRIuCYeWjr1d5KKTlL5l68rd4hpdZ+CwC4ht/p1O/Mdo46ekdtvOqFHgzmN9FX X-Received: from pjbsq3.prod.google.com ([2002:a17:90b:5303:b0:312:1af5:98c9]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4c48:b0:313:287c:74bd with SMTP id 98e67ed59e1d1-3134769724dmr7743384a91.33.1749246778095; Fri, 06 Jun 2025 14:52:58 -0700 (PDT) Date: Fri, 6 Jun 2025 14:52:45 -0700 In-Reply-To: <20250606215246.2419387-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: <20250606215246.2419387-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc0.604.gd4ff7b7c86-goog Message-ID: <20250606215246.2419387-5-blakejones@google.com> Subject: [PATCH v3 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.rc0.604.gd4ff7b7c86-goog From nobody Fri Dec 19 07:50:24 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 7B72F2405E4 for ; Fri, 6 Jun 2025 21:53:00 +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=1749246782; cv=none; b=ZkEm/yJpi1UIFKrkNcotPkODNTQI9kvw7GIgZeY3F+JJcdGNWIUP839FvaMt5b4j+HB6Ehstkcn5FBsqolmpIst1c4BJfUwutP0582ngDCpgo+YEdk+1h1z961rXXhqM3ZMI7f3cGrTfHauFP9QoSlnN9T6VIIx5c/gmjo56c4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749246782; c=relaxed/simple; bh=b5qIz6P3lrcv3sGS7KxAxKzdqooPdqfn8oztpbwmp10=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cJto53BDWW7PnlklSj0+TOjPovwV6uiD3NXExg4hcHlkudck8Cn/ljpLr6Uoik9OAUj1tGS/Gc+7kEjCF3i2WwjV5Nj9JPu82ADaNE5qY6+407YzW9pPZWaaNSr2csBzK1VMPqAbhFaup2IPXG/yEJMDlTGAgSfiJvRwRQenSF4= 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=vTQqpHtw; 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="vTQqpHtw" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7377139d8b1so2117771b3a.0 for ; Fri, 06 Jun 2025 14:53:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749246780; x=1749851580; 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=8iFfeaMcLuzx3qT6HoU1lCQOc9APQy0cingwV7epWcY=; b=vTQqpHtwu0I2MBWW74Kg8c1dRSl/Hks2f3rIoPlcGVaQPIDicU5R2YMjvLRuYqpjUE Mc43D/+XV2EV/7XwdH7uswZTHcQDqI8sGYyW4yyrImlZofkNO2A5vQFSvPuoabIrRQwS ggEqqUL/avBfJ9vfKuASpCNmuD/WUdmgC6Yi80KpubFSv/V0Y7zG1MrFl7Gf2F+7nJ/y 2k4lGWkhUP7Hcu/ycoGoFBs+Uj1asmwkqJvAKx6fAV8JgOhAmrz36CHTxicv3Sw6Mikb h+OqkURj4pwXOFW3zWL5Sw4CiewZwcm7f96I9G014iMWPEO8BNGyYZGjJc/HotEd2rqD Ov2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749246780; x=1749851580; 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=8iFfeaMcLuzx3qT6HoU1lCQOc9APQy0cingwV7epWcY=; b=EYDiPLy6ximisszUJjAvO/zmVVYjf19FaKicHDSPD+NeFWDA6trUsexLAfaqrbwzTR cj3vdIUTYU/7mn6yChj7yWRMysff1OoAwp5aOL1tuWtSAA5uGupsYE4kaB1IXRjE23ky a0gEdjQec32UtWHlPH29IeELuNTXVE2Y3hsqG+BFUYWh8ffqv5WKAEJ1ai+98WKqY+8Q aXk3uO/X2Q+BKqmGh8b3fvj2IQMxIHjYmFcQGm7D0MVidg6/5oK+iCIkvyevyyhDTIrS a8DSMaOSy2KnO2qE3Obkjv0Tq5kU5AHTHBR5P2n8CNq/U9GACx36+GiKEJV4kChFE4Eu HehA== X-Forwarded-Encrypted: i=1; AJvYcCVyosnvpBhreR7CaMGy5UzNJfSLLa1Gi1FeD7PT5y2D3Ws+oRmSbqLFfP6jwmtuAJ54nRHrrUonrwYYSRY=@vger.kernel.org X-Gm-Message-State: AOJu0YzgTQZ8G1QI1G+dUOCHRrl0jg3re0XwbcTSc5FVld3eRN2aePDu qjLsn2aclJHsCO0myCZneZJdO+moNncDNU3GP29jW7B93hRJgA1/btzPJbJkh6u4FegdkzpQ5Pw L3732DDrzd5eFsISpPuaytQ== X-Google-Smtp-Source: AGHT+IFWSsiTLCA9zysSvmxqFq5C2iXTQ62Am4lukNek4+xAO1R68sTOy8bTcsuDDhcx31/Pe7r+yAxD6oKYSLrH X-Received: from pgbfq11.prod.google.com ([2002:a05:6a02:298b:b0:b2c:4fb0:bc64]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6009:b0:218:c22:e3e6 with SMTP id adf61e73a8af0-21ee2555021mr6431282637.12.1749246779784; Fri, 06 Jun 2025 14:52:59 -0700 (PDT) Date: Fri, 6 Jun 2025 14:52:46 -0700 In-Reply-To: <20250606215246.2419387-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: <20250606215246.2419387-1-blakejones@google.com> X-Mailer: git-send-email 2.50.0.rc0.604.gd4ff7b7c86-goog Message-ID: <20250606215246.2419387-6-blakejones@google.com> Subject: [PATCH v3 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.rc0.604.gd4ff7b7c86-goog