[PATCH v2 04/14] perf test: Support dynamic test suites with setup callback and private data

From: Ian Rogers

Date: Sun May 31 2026 - 01:28:13 EST


Add void *priv to struct test_case to allow passing per-test context.
Add int (*setup)(struct test_suite *) to struct test_suite to allow
dynamic generation of test cases.
Update build_suites() to invoke the setup callback for each suite if
present, ensuring dynamic cases are available before listing or running.

Assisted-by: Gemini-CLI:Google Gemini 3
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/tests/builtin-test.c | 15 ++++++++++++++-
tools/perf/tests/tests.h | 2 ++
2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 7946878195b7..2ccb52a776cc 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -765,10 +765,19 @@ static struct test_suite **build_suites(void)
for (size_t i = 0, j = 0; i < ARRAY_SIZE(suites); i++, j = 0) \
while ((suite = suites[i][j++]) != NULL)

- for_each_suite(t)
+ for_each_suite(t) {
+ if (t->setup) {
+ int ret = t->setup(t);
+
+ if (ret < 0)
+ return NULL;
+ }
num_suites++;
+ }

result = calloc(num_suites + 1, sizeof(struct test_suite *));
+ if (!result)
+ return NULL;

for (int pass = 1; pass <= 2; pass++) {
for_each_suite(t) {
@@ -831,6 +840,8 @@ int cmd_test(int argc, const char **argv)
argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
if (argc >= 1 && !strcmp(argv[0], "list")) {
suites = build_suites();
+ if (!suites)
+ return -ENOMEM;
ret = perf_test__list(stdout, suites, argc - 1, argv + 1);
free(suites);
return ret;
@@ -863,6 +874,8 @@ int cmd_test(int argc, const char **argv)
rlimit__bump_memlock();

suites = build_suites();
+ if (!suites)
+ return -ENOMEM;
ret = __cmd_test(suites, argc, argv, skiplist);
free(suites);
return ret;
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index ee00518bf36f..9bcf1dbb0663 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -38,12 +38,14 @@ struct test_case {
const char *skip_reason;
test_fnptr run_case;
bool exclusive;
+ void *priv;
};

struct test_suite {
const char *desc;
struct test_case *test_cases;
void *priv;
+ int (*setup)(struct test_suite *suite);
};

#define DECLARE_SUITE(name) \
--
2.54.0.823.g6e5bcc1fc9-goog