[PATCH v3 07/12] perf tests: Test evsel__set_config_if_unset() and config change tracking

James Clark
2025-12-12T15:32:58Z

Test that evsel__set_config_if_unset() behaves as expected. This also
tests the user config change tracking mechanism as it depends on it.

Signed-off-by: James Clark <james.clark@linaro.org>
---
tools/perf/tests/pmu.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 91 insertions(+)

diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c
index cbded2c6faa4d5b422936e81bde83bc328fcdfe7..9e313a9a44fdebd0d417c7558b15e5db89241867 100644
--- a/tools/perf/tests/pmu.c
+++ b/tools/perf/tests/pmu.c
@@ -192,12 +192,102 @@ static int test__pmu_format(struct test_suite *test __maybe_unused, int subtest
}
if (attr.config2 != 0x0400000020041d07) {
pr_err("Unexpected config2 value %llx\n", attr.config2);
+ }
+
+ ret = TEST_OK;
+err_out:
+ parse_events_terms__exit(&terms);
+ test_pmu_put(dir, pmu);
+ return ret;
+}
+
+static int test__pmu_usr_chgs(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+{
+ const char *event = "perf-pmu-test/config=15,config1=4,krava02=170,"
+ "krava03=1,krava11=27,krava12=1/";
+ struct parse_events_terms terms;
+ struct parse_events_error err;
+ LIST_HEAD(config_terms);
+ struct evlist *evlist;
+ struct perf_pmu *pmu;
+ struct evsel *evsel;
+ int ret = TEST_FAIL;
+ char dir[PATH_MAX];
+ u64 val;
+
+ pmu = test_pmu_get(dir, sizeof(dir));
+ if (!pmu)
+ return TEST_FAIL;
+
+ evlist = evlist__new();
+ if (evlist == NULL) {
+ pr_err("Failed allocation");
+ goto err_out;
+ }
+
+ parse_events_terms__init(&terms);
+ ret = parse_events(evlist, event, &err);
+ if (ret) {
+ pr_debug("failed to parse event '%s', err %d\n", event, ret);
+ parse_events_error__print(&err, event);
+ if (parse_events_error__contains(&err, "can't access trace events"))
+ ret = TEST_SKIP;
goto err_out;
}
+ evsel = evlist__first(evlist);
+
+ /*
+ * Set via config=15, krava01 bits 0-1
+ * Set via config1=4, krava11 bit 1
+ * Set values: krava02=170, krava03=1, krava11=27, krava12=1
+ *
+ * Test that already set values aren't overwritten.
+ */
+ evsel__set_config_if_unset(pmu, evsel, "krava01", 16);
+ evsel__get_config_val(pmu, evsel, "krava01", &val);
+ TEST_ASSERT_EQUAL("krava01 overwritten", (int) val, (15 & 0b11));
+
+ evsel__set_config_if_unset(pmu, evsel, "krava11", 45);
+ evsel__get_config_val(pmu, evsel, "krava11", &val);
+ TEST_ASSERT_EQUAL("krava11 overwritten", (int) val, (27 | (4 << 1)));
+
+ evsel__set_config_if_unset(pmu, evsel, "krava02", 32);
+ evsel__get_config_val(pmu, evsel, "krava02", &val);
+ TEST_ASSERT_EQUAL("krava02 overwritten", (int) val, 170);
+
+ evsel__set_config_if_unset(pmu, evsel, "krava03", 0);
+ evsel__get_config_val(pmu, evsel, "krava03", &val);
+ TEST_ASSERT_EQUAL("krava03 overwritten", (int) val, 1);
+
+ /*
+ * krava13 doesn't have any bits set by either krava13= or config1=
+ * but setting _any_ raw value for config1 implies that krava13
+ * shouldn't be overwritten. So it's value should remain as 0.
+ */
+ evsel__set_config_if_unset(pmu, evsel, "krava13", 5);
+ evsel__get_config_val(pmu, evsel, "krava13", &val);
+ TEST_ASSERT_EQUAL("krava13 overwritten", (int) val, 0);
+
+ /*
+ * Unset values: krava21, krava22, krava23
+ *
+ * Test that unset values are overwritten.
+ */
+ evsel__set_config_if_unset(pmu, evsel, "krava21", 13905);
+ evsel__get_config_val(pmu, evsel, "krava21", &val);
+ TEST_ASSERT_EQUAL("krava21 not overwritten", (int) val, 13905);
+
+ evsel__set_config_if_unset(pmu, evsel, "krava22", 11);
+ evsel__get_config_val(pmu, evsel, "krava22", &val);
+ TEST_ASSERT_EQUAL("krava22 not overwritten", (int) val, 11);

+ evsel__set_config_if_unset(pmu, evsel, "krava23", 0);
+ evsel__get_config_val(pmu, evsel, "krava23", &val);
+ TEST_ASSERT_EQUAL("krava23 not overwritten", (int) val, 0);
ret = TEST_OK;
err_out:
parse_events_terms__exit(&terms);
+ evlist__delete(evlist);
test_pmu_put(dir, pmu);
return ret;
}
@@ -539,6 +629,7 @@ static struct test_case tests__pmu[] = {
TEST_CASE("PMU name combining", name_len),
TEST_CASE("PMU name comparison", name_cmp),
TEST_CASE("PMU cmdline match", pmu_match),
+ TEST_CASE("PMU user config changes", pmu_usr_chgs),
{ .name = NULL, }
};

--
2.34.1