From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Leo Yan <leo.yan@linux.dev>, Dmitry Vyukov <dvyukov@google.com>,
"Steinar H . Gunderson" <sesse@google.com>,
Stephen Brennan <stephen.s.brennan@oracle.com>,
Thomas Falcon <thomas.falcon@intel.com>,
linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH v3 18/19] perf test: Add BUILD_ID_OFFSET C unit tests
Date: Sun, 15 Nov 2026 23:44:13 -0800 [thread overview]
Message-ID: <20261116074414.3174737-19-irogers@google.com> (raw)
In-Reply-To: <20261116074414.3174737-1-irogers@google.com>
Add C unit test coverage for PERF_SAMPLE_BUILD_ID_OFFSET and
PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET:
- sample-parsing: round-trip synthesis and parsing
- mmap-thread-lookup: map lookup by build ID and DSO file offset
- bid-offset: sys_perf_event_open() attribute probing
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/Build | 1 +
tools/perf/tests/bid-offset.c | 61 +++++++++++++++++++++++++++
tools/perf/tests/builtin-test.c | 1 +
tools/perf/tests/mmap-thread-lookup.c | 49 ++++++++++++++++++++-
tools/perf/tests/sample-parsing.c | 60 +++++++++++++++++++++++++-
tools/perf/tests/tests.h | 1 +
6 files changed, 170 insertions(+), 3 deletions(-)
create mode 100644 tools/perf/tests/bid-offset.c
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 5eebb2ec5dde..d6fe1ba57a79 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -66,6 +66,7 @@ perf-test-y += expand-cgroup.o
perf-test-y += perf-time-to-tsc.o
perf-test-y += dlfilter-test.o
perf-test-y += sigtrap.o
+perf-test-y += bid-offset.o
perf-test-y += event_groups.o
perf-test-y += hybrid-merge.o
perf-test-y += symbols.o
diff --git a/tools/perf/tests/bid-offset.c b/tools/perf/tests/bid-offset.c
new file mode 100644
index 000000000000..0cf52caae047
--- /dev/null
+++ b/tools/perf/tests/bid-offset.c
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "tests.h"
+#include "debug.h"
+#include "evlist.h"
+#include "evsel.h"
+#include <linux/hw_breakpoint.h>
+#include <linux/perf_event.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include <errno.h>
+#include "../perf-sys.h"
+
+static int test__bid_offset(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+{
+ struct perf_event_attr attr = {
+ .type = PERF_TYPE_SOFTWARE,
+ .config = PERF_COUNT_SW_DUMMY,
+ .size = sizeof(attr),
+ .sample_type = PERF_SAMPLE_BUILD_ID_OFFSET,
+ .exclude_kernel = 1,
+ .exclude_hv = 1,
+ };
+ int fd;
+
+ fd = sys_perf_event_open(&attr, /*pid=*/0, /*cpu=*/-1, /*group_fd=*/-1, /*flags=*/0);
+ if (fd < 0) {
+ pr_debug("sys_perf_event_open failed with %d\n", errno);
+ if (errno == EINVAL || errno == EACCES || errno == EPERM) {
+ pr_debug("Kernel does not support or permit PERF_SAMPLE_BUILD_ID_OFFSET\n");
+ return TEST_SKIP;
+ }
+ return TEST_FAIL;
+ }
+ close(fd);
+
+ attr.sample_type = PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET;
+ attr.exclude_callchain_kernel = 1;
+ fd = sys_perf_event_open(&attr, /*pid=*/0, /*cpu=*/-1, /*group_fd=*/-1, /*flags=*/0);
+ if (fd < 0) {
+ pr_debug("sys_perf_event_open with callchain failed with %d\n", errno);
+ if (errno == EINVAL || errno == EACCES || errno == EPERM) {
+ pr_debug("Kernel does not support or permit PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET\n");
+ return TEST_SKIP;
+ }
+ return TEST_FAIL;
+ }
+ close(fd);
+
+ return TEST_OK;
+}
+
+static struct test_case bid_offset_tests[] = {
+ TEST_CASE_REASON("Test PERF_SAMPLE_BUILD_ID_OFFSET", bid_offset,
+ "not supported by kernel"),
+ { .name = NULL, }
+};
+
+struct test_suite suite__bid_offset = {
+ .desc = "Test PERF_SAMPLE_BUILD_ID_OFFSET",
+ .test_cases = bid_offset_tests,
+};
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 327483f96401..501d72fc7dcb 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -186,6 +186,7 @@ static struct test_suite *generic_tests[] = {
&suite__perf_time_to_tsc,
&suite__dlfilter,
&suite__sigtrap,
+ &suite__bid_offset,
&suite__event_groups,
&suite__hybrid_merge,
&suite__symbols,
diff --git a/tools/perf/tests/mmap-thread-lookup.c b/tools/perf/tests/mmap-thread-lookup.c
index dca8bbfe780a..7e312d5a1ba3 100644
--- a/tools/perf/tests/mmap-thread-lookup.c
+++ b/tools/perf/tests/mmap-thread-lookup.c
@@ -7,6 +7,7 @@
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include "debug.h"
#include "env.h"
#include "event.h"
@@ -17,7 +18,10 @@
#include "symbol.h"
#include "util/synthetic-events.h"
#include "thread.h"
+#include "dso.h"
+#include "build-id.h"
#include <internal/lib.h> // page_size
+#include <internal/rc_check.h>
#define THREADS 4
@@ -200,17 +204,58 @@ static int mmap_events(synth_cb synth)
.ip = (unsigned long) (td->map + 1),
}, &al);
- thread__put(thread);
-
if (!al.map) {
pr_debug("failed, couldn't find map\n");
err = -1;
+ thread__put(thread);
addr_location__exit(&al);
break;
}
pr_debug("map %p, addr %" PRIx64 "\n", al.map, map__start(al.map));
+ /* Verify lookup by build ID and offset */
+ {
+ struct build_id test_bid = {
+ .data = { [0] = i + 1 },
+ .size = BUILD_ID_SIZE,
+ };
+ struct dso *dso = dso__new("test_dso");
+ struct addr_location al2;
+ struct perf_build_id pbid = { .size = test_bid.size };
+ u64 offset = map__dso_map_ip(al.map,
+ (unsigned long)(td->map + 1));
+
+ if (!dso) {
+ pr_debug("failed to create test dso\n");
+ err = -1;
+ thread__put(thread);
+ addr_location__exit(&al);
+ break;
+ }
+ dso__set_build_id(dso, &test_bid);
+ dso__put(map__dso(al.map));
+ map__set_dso(al.map, dso);
+
+ memcpy(pbid.data, test_bid.data, test_bid.size);
+ addr_location__init(&al2);
+ thread__find_map(thread, &(struct perf_sample){
+ .cpumode = PERF_RECORD_MISC_USER,
+ .ip = 0,
+ .bid = {
+ .bid = &pbid,
+ .offset = offset,
+ },
+ }, &al2);
+
+ if (!RC_CHK_EQUAL(al2.map, al.map) || al2.addr != offset) {
+ pr_debug("failed, couldn't find map by build id and offset\n");
+ err = -1;
+ }
+ addr_location__exit(&al2);
+ }
+
+ thread__put(thread);
addr_location__exit(&al);
if (err)
break;
diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-parsing.c
index bd30f6d4c31b..41e8933bc2fc 100644
--- a/tools/perf/tests/sample-parsing.c
+++ b/tools/perf/tests/sample-parsing.c
@@ -218,6 +218,33 @@ static bool samples_same(struct perf_sample *s1,
}
}
+ if (type & PERF_SAMPLE_BUILD_ID_OFFSET) {
+ if (!s1->bid.bid || !s2->bid.bid ||
+ memcmp(s1->bid.bid, s2->bid.bid, sizeof(*s1->bid.bid))) {
+ pr_debug("Samples differ at 'bid.bid'\n");
+ return false;
+ }
+ COMP(bid.offset);
+ }
+
+ if (type & PERF_SAMPLE_CALLCHAIN_BUILD_ID_OFFSET) {
+ COMP(callchain_bid.nr_build_ids);
+ COMP(callchain_bid.nr_offsets);
+ COMP(callchain_bid.build_ids_len);
+ if (!s1->callchain_bid.build_ids || !s2->callchain_bid.build_ids ||
+ memcmp(s1->callchain_bid.build_ids, s2->callchain_bid.build_ids,
+ s1->callchain_bid.build_ids_len)) {
+ pr_debug("Samples differ at 'callchain_bid.build_ids'\n");
+ return false;
+ }
+ if (!s1->callchain_bid.offsets || !s2->callchain_bid.offsets ||
+ memcmp(s1->callchain_bid.offsets, s2->callchain_bid.offsets,
+ s1->callchain_bid.nr_offsets * sizeof(u64))) {
+ pr_debug("Samples differ at 'callchain_bid.offsets'\n");
+ return false;
+ }
+ }
+
return true;
}
@@ -247,6 +274,26 @@ static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
const u32 raw_data[] = {0x12345678, 0x0a0b0c0d, 0x11020304, 0x05060708, 0 };
const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
const u64 aux_data[] = {0xa55a, 0, 0xeeddee, 0x0282028202820282};
+ const struct perf_build_id bid = {
+ .size = 20,
+ .data = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ }
+ };
+ const u8 callchain_build_ids[48] = {
+ 20,
+ '9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
+ '9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
+ 20,
+ 'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd',
+ 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b',
+ };
+ const u64 callchain_offsets[] = {
+ (1ULL << 56) | 201,
+ (2ULL << 56) | 202,
+ (1ULL << 56) | 203,
+ };
struct regs_dump user_regs = {
.abi = PERF_SAMPLE_REGS_ABI_64,
.mask = sample_regs,
@@ -295,6 +342,17 @@ static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
.size = sizeof(aux_data),
.data = (void *)aux_data,
},
+ .bid = {
+ .bid = (struct perf_build_id *)&bid,
+ .offset = 119,
+ },
+ .callchain_bid = {
+ .nr_build_ids = 2,
+ .nr_offsets = ARRAY_SIZE(callchain_offsets),
+ .build_ids_len = sizeof(callchain_build_ids),
+ .build_ids = (u8 *)callchain_build_ids,
+ .offsets = (u64 *)callchain_offsets,
+ },
};
struct sample_read_value values[] = {{1, 5, 0}, {9, 3, 0}, {2, 7, 0}, {6, 4, 1},};
struct sample_read_value packed_values[ARRAY_SIZE(values)];
@@ -547,7 +605,7 @@ static int test__sample_parsing(struct test_suite *test __maybe_unused, int subt
* were added. Please actually update the test rather than just change
* the condition below.
*/
- if (PERF_SAMPLE_MAX > PERF_SAMPLE_WEIGHT_STRUCT << 1) {
+ if (PERF_SAMPLE_WEIGHT_STRUCT << 3 < PERF_SAMPLE_MAX) {
pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
return -1;
}
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index af822885890d..a65d209b649f 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -196,6 +196,7 @@ DECLARE_SUITE(expand_cgroup_events);
DECLARE_SUITE(perf_time_to_tsc);
DECLARE_SUITE(dlfilter);
DECLARE_SUITE(sigtrap);
+DECLARE_SUITE(bid_offset);
DECLARE_SUITE(event_groups);
DECLARE_SUITE(hybrid_merge);
DECLARE_SUITE(symbols);
--
2.56.1.703.gf848ed807c-goog
next prev parent reply other threads:[~2026-11-16 7:45 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 7:43 [PATCH v3 00/19] perf/core, perf/tools: Add PERF_SAMPLE_BUILD_ID_OFFSET support Ian Rogers
2026-11-16 7:43 ` [PATCH v3 01/19] perf/core: Protect per-CPU callchain buffer across bpf_overflow_handler() Ian Rogers
2026-11-16 7:43 ` [PATCH v3 02/19] perf/uapi: Factor perf_build_id out into its own top-level struct Ian Rogers
2026-11-16 7:43 ` [PATCH v3 03/19] perf/uapi: Add BUILD_ID_OFFSET sample types to UAPI Ian Rogers
2026-11-16 7:43 ` [PATCH v3 04/19] bpf: Export stack_map_get_build_id_offset() for perf events Ian Rogers
2026-11-16 7:44 ` [PATCH v3 05/19] perf/core: Implement BUILD_ID_OFFSET and CALLCHAIN_BUILD_ID_OFFSET sample types Ian Rogers
2026-11-16 7:44 ` [PATCH v3 06/19] perf/core: Support deferred unwinding for BUILD_ID_OFFSET " Ian Rogers
2026-11-16 7:44 ` [PATCH v3 07/19] perf inject: Set ordered_events and ordering_requires_timestamps for --aslr Ian Rogers
2026-11-16 7:44 ` [PATCH v3 08/19] perf session: Avoid double byte-swapping re-parsed deferred samples Ian Rogers
2026-11-16 7:44 ` [PATCH v3 09/19] perf inject: Fix PERF_SAMPLE_READ layout and bounds checks in aslr_tool__process_sample() Ian Rogers
2026-11-16 7:44 ` [PATCH v3 10/19] tools headers UAPI: Sync linux/perf_event.h for struct perf_build_id Ian Rogers
2026-11-16 7:44 ` [PATCH v3 11/19] tools headers UAPI: Sync linux/perf_event.h for BUILD_ID_OFFSET Ian Rogers
2026-11-16 7:44 ` [PATCH v3 12/19] perf: Refactor thread map and symbol APIs to take perf_sample Ian Rogers
2026-11-16 7:44 ` [PATCH v3 13/19] perf maps: Add dsos__find_by_build_id() and maps__find_by_build_id() Ian Rogers
2026-11-16 7:44 ` [PATCH v3 14/19] perf evsel: Parse, synthesize, and dump BUILD_ID_OFFSET sample types Ian Rogers
2026-11-16 7:44 ` [PATCH v3 15/19] perf symbol: Resolve symbols and callchains using build ID and offset Ian Rogers
2026-11-16 7:44 ` [PATCH v3 16/19] perf inject: Add --sample-buildids to convert samples to build ID + offset Ian Rogers
2026-11-16 7:44 ` [PATCH v3 17/19] perf record: Add --buildid-offset option Ian Rogers
2026-11-16 7:44 ` Ian Rogers [this message]
2026-11-16 7:44 ` [PATCH v3 19/19] perf test: Add inject --sample-buildids and record --buildid-offset shell tests Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116074414.3174737-19-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dvyukov@google.com \
--cc=eddyz87@gmail.com \
--cc=ihor.solodrai@linux.dev \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=leo.yan@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=martin.lau@linux.dev \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=sesse@google.com \
--cc=song@kernel.org \
--cc=stephen.s.brennan@oracle.com \
--cc=thomas.falcon@intel.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox