2024-04-29 20:02:52

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v3 0/6] Assume sysfs event names are always the same case

By assuming sysfs events are either upper or lower case, the case
insensitive event parsing can probe for the existence of files rather
then loading all events in a directory. When the event is a json event
like inst_retired.any on Intel, this reduces the number of openat
calls on a Tigerlake laptop from 325 down to 255.

v1 sent as an RFC:
https://lore.kernel.org/lkml/[email protected]/

v2: addresses review feedback from Kan Liang, by updating
documentation and adding tests.

v3: incorporate feedback from Thomas Richter <[email protected]>
that s390 event names are all upper case. Do a lower case probe
then an upper case probe, make documentation and tests also agree.

Ian Rogers (6):
perf test pmu-events: Make it clearer that pmu-events tests json
events
perf Document: Sysfs event names must be lower or upper case
perf test pmu: Refactor format test and exposed test APIs
perf test pmu: Add an eagerly loaded event test
perf test pmu: Test all sysfs PMU event names are the same case
perf pmu: Assume sysfs events are always the same case

.../sysfs-bus-event_source-devices-events | 6 +
tools/perf/tests/pmu-events.c | 2 +-
tools/perf/tests/pmu.c | 457 ++++++++++++------
tools/perf/util/parse-events.c | 2 +-
tools/perf/util/parse-events.h | 2 +-
tools/perf/util/pmu.c | 111 +++--
tools/perf/util/pmu.h | 4 +-
tools/perf/util/pmus.c | 16 +-
tools/perf/util/pmus.h | 2 +
9 files changed, 405 insertions(+), 197 deletions(-)

--
2.44.0.769.g3c40516874-goog



2024-04-29 20:03:03

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v3 1/6] perf test pmu-events: Make it clearer that pmu-events tests json events

Add json to the test name.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/tests/pmu-events.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/tests/pmu-events.c b/tools/perf/tests/pmu-events.c
index 47a7c3277540..7b2c34462fcb 100644
--- a/tools/perf/tests/pmu-events.c
+++ b/tools/perf/tests/pmu-events.c
@@ -1105,6 +1105,6 @@ static struct test_case pmu_events_tests[] = {
};

struct test_suite suite__pmu_events = {
- .desc = "PMU events",
+ .desc = "PMU json event tests",
.test_cases = pmu_events_tests,
};
--
2.44.0.769.g3c40516874-goog


2024-04-29 20:03:32

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v3 3/6] perf test pmu: Refactor format test and exposed test APIs

In tests/pmu.c, make a common utility that creates a PMU in mkdtemp
directory and uses regular PMU parsing logic to load that PMU. Formats
must still be eagerly loaded as by default the PMU code assumes
devices are going to be in sysfs. In util/pmu.[ch], hide
perf_pmu__format_parse but add the eager argument to perf_pmu__lookup
called by perf_pmus__add_test_pmu. Later patches will eagerly load
other non-sysfs files when eager loading is enabled.

In tests/pmu.c, rather than manually constructing a list of term
arguments, just use the term parsing code from a string.

Add more comments and debug logging.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/tests/pmu.c | 313 +++++++++++++++------------------
tools/perf/util/parse-events.c | 2 +-
tools/perf/util/parse-events.h | 2 +-
tools/perf/util/pmu.c | 11 +-
tools/perf/util/pmu.h | 4 +-
tools/perf/util/pmus.c | 16 +-
tools/perf/util/pmus.h | 2 +
7 files changed, 171 insertions(+), 179 deletions(-)

diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c
index 8f18127d876a..6b2d9adcc583 100644
--- a/tools/perf/tests/pmu.c
+++ b/tools/perf/tests/pmu.c
@@ -2,203 +2,180 @@
#include "parse-events.h"
#include "pmu.h"
#include "tests.h"
+#include "debug.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
-#include <linux/kernel.h>
-#include <linux/limits.h>
-#include <linux/zalloc.h>
-
-/* Simulated format definitions. */
-static struct test_format {
- const char *name;
- const char *value;
-} test_formats[] = {
- { "krava01", "config:0-1,62-63\n", },
- { "krava02", "config:10-17\n", },
- { "krava03", "config:5\n", },
- { "krava11", "config1:0,2,4,6,8,20-28\n", },
- { "krava12", "config1:63\n", },
- { "krava13", "config1:45-47\n", },
- { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
- { "krava22", "config2:8,18,48,58\n", },
- { "krava23", "config2:28-29,38\n", },
-};
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>

-/* Simulated users input. */
-static struct parse_events_term test_terms[] = {
- {
- .config = "krava01",
- .val.num = 15,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
- {
- .config = "krava02",
- .val.num = 170,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
- {
- .config = "krava03",
- .val.num = 1,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
- {
- .config = "krava11",
- .val.num = 27,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
- {
- .config = "krava12",
- .val.num = 1,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
- {
- .config = "krava13",
- .val.num = 2,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
- {
- .config = "krava21",
- .val.num = 119,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
- {
- .config = "krava22",
- .val.num = 11,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
- {
- .config = "krava23",
- .val.num = 2,
- .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
- .type_term = PARSE_EVENTS__TERM_TYPE_USER,
- },
-};
+/* Fake PMUs created in temp directory. */
+static LIST_HEAD(test_pmus);

-/*
- * Prepare format directory data, exported by kernel
- * at /sys/bus/event_source/devices/<dev>/format.
- */
-static char *test_format_dir_get(char *dir, size_t sz)
-{
- unsigned int i;
-
- snprintf(dir, sz, "/tmp/perf-pmu-test-format-XXXXXX");
- if (!mkdtemp(dir))
- return NULL;
-
- for (i = 0; i < ARRAY_SIZE(test_formats); i++) {
- char name[PATH_MAX];
- struct test_format *format = &test_formats[i];
- FILE *file;
-
- scnprintf(name, PATH_MAX, "%s/%s", dir, format->name);
-
- file = fopen(name, "w");
- if (!file)
- return NULL;
-
- if (1 != fwrite(format->value, strlen(format->value), 1, file))
- break;
-
- fclose(file);
- }
-
- return dir;
-}
-
-/* Cleanup format directory. */
-static int test_format_dir_put(char *dir)
+/* Cleanup test PMU directory. */
+static int test_pmu_put(const char *dir, struct perf_pmu *pmu)
{
char buf[PATH_MAX + 20];
+ int ret;

- snprintf(buf, sizeof(buf), "rm -f %s/*\n", dir);
- if (system(buf))
- return -1;
+ if (scnprintf(buf, sizeof(buf), "rm -fr %s", dir) < 0) {
+ pr_err("Failure to set up buffer for \"%s\"\n", dir);
+ return -EINVAL;
+ }
+ ret = system(buf);
+ if (ret)
+ pr_err("Failure to \"%s\"\n", buf);

- snprintf(buf, sizeof(buf), "rmdir %s\n", dir);
- return system(buf);
+ list_del(&pmu->list);
+ perf_pmu__delete(pmu);
+ return ret;
}

-static void add_test_terms(struct parse_events_terms *terms)
+/*
+ * Prepare test PMU directory data, normally exported by kernel at
+ * /sys/bus/event_source/devices/<pmu>/. Give as input a buffer to hold the file
+ * path, the result is PMU loaded using that directory.
+ */
+static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
{
- unsigned int i;
+ /* Simulated format definitions. */
+ const struct test_format {
+ const char *name;
+ const char *value;
+ } test_formats[] = {
+ { "krava01", "config:0-1,62-63\n", },
+ { "krava02", "config:10-17\n", },
+ { "krava03", "config:5\n", },
+ { "krava11", "config1:0,2,4,6,8,20-28\n", },
+ { "krava12", "config1:63\n", },
+ { "krava13", "config1:45-47\n", },
+ { "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
+ { "krava22", "config2:8,18,48,58\n", },
+ { "krava23", "config2:28-29,38\n", },
+ };
+ char name[PATH_MAX];
+ int dirfd, file;
+ struct perf_pmu *pmu = NULL;
+
+ /* Create equivalent of sysfs mount point. */
+ scnprintf(dir, sz, "/tmp/perf-pmu-test-XXXXXX");
+ if (!mkdtemp(dir)) {
+ pr_err("mkdtemp failed\n");
+ dir[0] = '\0';
+ return NULL;
+ }
+ dirfd = open(dir, O_DIRECTORY);
+ if (dirfd < 0) {
+ pr_err("Failed to open test directory \"%s\"\n", dir);
+ goto err_out;
+ }

- for (i = 0; i < ARRAY_SIZE(test_terms); i++) {
- struct parse_events_term *clone;
+ /* Create the test PMU directory and give it a perf_event_attr type number. */
+ if (mkdirat(dirfd, "perf-pmu-test", 0755) < 0) {
+ pr_err("Failed to mkdir PMU directory\n");
+ goto err_out;
+ }
+ file = openat(dirfd, "perf-pmu-test/type", O_WRONLY | O_CREAT, 0600);
+ if (!file) {
+ pr_err("Failed to open for writing file \"type\"\n");
+ goto err_out;
+ }
+ write(file, "9999\n", 5);
+ close(file);

- parse_events_term__clone(&clone, &test_terms[i]);
- list_add_tail(&clone->list, &terms->terms);
+ /* Create format directory and files. */
+ if (mkdirat(dirfd, "perf-pmu-test/format", 0755) < 0) {
+ pr_err("Failed to mkdir PMU format directory\n)");
+ goto err_out;
}
+ for (size_t i = 0; i < ARRAY_SIZE(test_formats); i++) {
+ const struct test_format *format = &test_formats[i];
+
+ if (scnprintf(name, PATH_MAX, "perf-pmu-test/format/%s", format->name) < 0) {
+ pr_err("Failure to set up path for \"%s\"\n", format->name);
+ goto err_out;
+ }
+ file = openat(dirfd, name, O_WRONLY | O_CREAT, 0600);
+ if (!file) {
+ pr_err("Failed to open for writing file \"%s\"\n", name);
+ goto err_out;
+ }
+
+ if (write(file, format->value, strlen(format->value)) < 0) {
+ pr_err("Failed to write to file \"%s\"\n", name);
+ close(file);
+ goto err_out;
+ }
+ close(file);
+ }
+
+ /* Make the PMU reading the files created above. */
+ pmu = perf_pmus__add_test_pmu(dirfd, "perf-pmu-test");
+ if (!pmu)
+ pr_err("Test PMU creation failed\n");
+
+err_out:
+ if (!pmu)
+ test_pmu_put(dir, pmu);
+ if (dirfd >= 0)
+ close(dirfd);
+ return pmu;
}

-static int test__pmu(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+static int test__pmu_format(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
char dir[PATH_MAX];
- char *format;
- struct parse_events_terms terms;
struct perf_event_attr attr;
- struct perf_pmu *pmu;
- int fd;
- int ret;
+ struct parse_events_terms terms;
+ int ret = TEST_FAIL;
+ struct perf_pmu *pmu = test_pmu_get(dir, sizeof(dir));

- parse_events_terms__init(&terms);
- add_test_terms(&terms);
- pmu = zalloc(sizeof(*pmu));
- if (!pmu) {
- parse_events_terms__exit(&terms);
- return -ENOMEM;
- }
+ if (!pmu)
+ return TEST_FAIL;

- INIT_LIST_HEAD(&pmu->format);
- INIT_LIST_HEAD(&pmu->aliases);
- INIT_LIST_HEAD(&pmu->caps);
- format = test_format_dir_get(dir, sizeof(dir));
- if (!format) {
- free(pmu);
- parse_events_terms__exit(&terms);
- return -EINVAL;
+ parse_events_terms__init(&terms);
+ if (parse_events_terms(&terms,
+ "krava01=15,krava02=170,krava03=1,krava11=27,krava12=1,"
+ "krava13=2,krava21=119,krava22=11,krava23=2",
+ NULL)) {
+ pr_err("Term parsing failed\n");
+ goto err_out;
}

memset(&attr, 0, sizeof(attr));
-
- fd = open(format, O_DIRECTORY);
- if (fd < 0) {
- ret = fd;
- goto out;
+ ret = perf_pmu__config_terms(pmu, &attr, &terms, /*zero=*/false, /*err=*/NULL);
+ if (ret) {
+ pr_err("perf_pmu__config_terms failed");
+ goto err_out;
}

- pmu->name = strdup("perf-pmu-test");
- ret = perf_pmu__format_parse(pmu, fd, /*eager_load=*/true);
- if (ret)
- goto out;
+ if (attr.config != 0xc00000000002a823) {
+ pr_err("Unexpected config value %llx\n", attr.config);
+ goto err_out;
+ }
+ if (attr.config1 != 0x8000400000000145) {
+ pr_err("Unexpected config1 value %llx\n", attr.config1);
+ goto err_out;
+ }
+ if (attr.config2 != 0x0400000020041d07) {
+ pr_err("Unexpected config2 value %llx\n", attr.config2);
+ goto err_out;
+ }

- ret = perf_pmu__config_terms(pmu, &attr, &terms, /*zero=*/false, /*err=*/NULL);
- if (ret)
- goto out;
-
- ret = -EINVAL;
- if (attr.config != 0xc00000000002a823)
- goto out;
- if (attr.config1 != 0x8000400000000145)
- goto out;
- if (attr.config2 != 0x0400000020041d07)
- goto out;
-
- ret = 0;
-out:
- test_format_dir_put(format);
- perf_pmu__delete(pmu);
+ ret = TEST_OK;
+err_out:
parse_events_terms__exit(&terms);
+ test_pmu_put(dir, pmu);
return ret;
}

-DEFINE_SUITE("Parse perf pmu format", pmu);
+static struct test_case tests__pmu[] = {
+ TEST_CASE("Parsing with PMU format directory", pmu_format),
+ { .name = NULL, }
+};
+
+struct test_suite suite__pmu = {
+ .desc = "Sysfs PMU tests",
+ .test_cases = tests__pmu,
+};
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 0f308b4db2b9..2b9ede311c31 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -2585,7 +2585,7 @@ int parse_events_term__term(struct parse_events_term **term,
}

int parse_events_term__clone(struct parse_events_term **new,
- struct parse_events_term *term)
+ const struct parse_events_term *term)
{
char *str;
struct parse_events_term temp = *term;
diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
index 5695308efab9..e7ac1f13376d 100644
--- a/tools/perf/util/parse-events.h
+++ b/tools/perf/util/parse-events.h
@@ -178,7 +178,7 @@ int parse_events_term__term(struct parse_events_term **term,
enum parse_events__term_type term_rhs,
void *loc_term, void *loc_val);
int parse_events_term__clone(struct parse_events_term **new,
- struct parse_events_term *term);
+ const struct parse_events_term *term);
void parse_events_term__delete(struct parse_events_term *term);

void parse_events_terms__delete(struct parse_events_terms *terms);
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 74dd5bd49d9a..fbbc535ed93f 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -182,7 +182,7 @@ static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_fo
* Parse & process all the sysfs attributes located under
* the directory specified in 'dir' parameter.
*/
-int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
+static int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
{
struct dirent *evt_ent;
DIR *format_dir;
@@ -232,7 +232,7 @@ int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load)
* located at:
* /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
*/
-static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
+static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name, bool eager_load)
{
int fd;

@@ -241,7 +241,7 @@ static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name)
return 0;

/* it'll close the fd */
- if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false))
+ if (perf_pmu__format_parse(pmu, fd, eager_load))
return -1;

return 0;
@@ -994,7 +994,8 @@ perf_pmu__arch_init(struct perf_pmu *pmu)
pmu->mem_events = perf_mem_events;
}

-struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name)
+struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name,
+ bool eager_load)
{
struct perf_pmu *pmu;
__u32 type;
@@ -1023,7 +1024,7 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
* type value and format definitions. Load both right
* now.
*/
- if (pmu_format(pmu, dirfd, name))
+ if (pmu_format(pmu, dirfd, name, eager_load))
goto err;

pmu->is_core = is_pmu_core(name);
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 93d03bd3ecbe..561716aa2b25 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -214,7 +214,6 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_
struct parse_events_error *err);
int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb);

-int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load);
void perf_pmu_format__set_value(void *format, int config, unsigned long *bits);
bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name);
int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb);
@@ -272,7 +271,8 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size,
int perf_pmu__event_source_devices_fd(void);
int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags);

-struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name);
+struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name,
+ bool eager_load);
struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
void perf_pmu__delete(struct perf_pmu *pmu);
struct perf_pmu *perf_pmus__find_core_pmu(void);
diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c
index 2fd369e45832..b9b4c5eb5002 100644
--- a/tools/perf/util/pmus.c
+++ b/tools/perf/util/pmus.c
@@ -124,7 +124,8 @@ struct perf_pmu *perf_pmus__find(const char *name)
return NULL;

dirfd = perf_pmu__event_source_devices_fd();
- pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
+ pmu = perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name,
+ /*eager_load=*/false);
close(dirfd);

if (!pmu) {
@@ -159,7 +160,8 @@ static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name)
if (core_pmu && read_sysfs_core_pmus)
return NULL;

- return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name);
+ return perf_pmu__lookup(core_pmu ? &core_pmus : &other_pmus, dirfd, name,
+ /*eager_load=*/false);
}

static int pmus_cmp(void *priv __maybe_unused,
@@ -696,3 +698,13 @@ struct perf_pmu *perf_pmus__find_core_pmu(void)
{
return perf_pmus__scan_core(NULL);
}
+
+struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char *name)
+{
+ /*
+ * Some PMU functions read from the sysfs mount point, so care is
+ * needed, hence passing the eager_load flag to load things like the
+ * format files.
+ */
+ return perf_pmu__lookup(&other_pmus, test_sysfs_dirfd, name, /*eager_load=*/true);
+}
diff --git a/tools/perf/util/pmus.h b/tools/perf/util/pmus.h
index eec599d8aebd..9d4ded80b8e9 100644
--- a/tools/perf/util/pmus.h
+++ b/tools/perf/util/pmus.h
@@ -24,4 +24,6 @@ int perf_pmus__num_core_pmus(void);
bool perf_pmus__supports_extended_type(void);
char *perf_pmus__default_pmu_name(void);

+struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char *name);
+
#endif /* __PMUS_H */
--
2.44.0.769.g3c40516874-goog


2024-04-29 20:03:41

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v3 4/6] perf test pmu: Add an eagerly loaded event test

Allow events/aliases to be eagerly loaded for a PMU. Factor out the
pmu_aliases_parse to allow this. Parse a test event and check it
configures the attribute as expected. There is overlap with the
parse-events tests, but this test is done with a PMU created in a temp
directory and doesn't rely on PMUs in sysfs.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/tests/pmu.c | 72 ++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/pmu.c | 69 ++++++++++++++++++++++++++++------------
2 files changed, 120 insertions(+), 21 deletions(-)

diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c
index 6b2d9adcc583..f4e9a39534cb 100644
--- a/tools/perf/tests/pmu.c
+++ b/tools/perf/tests/pmu.c
@@ -1,4 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
+#include "evlist.h"
+#include "evsel.h"
#include "parse-events.h"
#include "pmu.h"
#include "tests.h"
@@ -54,6 +56,9 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
{ "krava22", "config2:8,18,48,58\n", },
{ "krava23", "config2:28-29,38\n", },
};
+ const char *test_event = "krava01=15,krava02=170,krava03=1,krava11=27,krava12=1,"
+ "krava13=2,krava21=119,krava22=11,krava23=2\n";
+
char name[PATH_MAX];
int dirfd, file;
struct perf_pmu *pmu = NULL;
@@ -110,6 +115,19 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
close(file);
}

+ /* Create test event. */
+ if (mkdirat(dirfd, "perf-pmu-test/events", 0755) < 0) {
+ pr_err("Failed to mkdir PMU events directory\n");
+ goto err_out;
+ }
+ file = openat(dirfd, "perf-pmu-test/events/test-event", O_WRONLY | O_CREAT, 0600);
+ if (!file) {
+ pr_err("Failed to open for writing file \"type\"\n");
+ goto err_out;
+ }
+ write(file, test_event, strlen(test_event));
+ close(file);
+
/* Make the PMU reading the files created above. */
pmu = perf_pmus__add_test_pmu(dirfd, "perf-pmu-test");
if (!pmu)
@@ -170,8 +188,62 @@ static int test__pmu_format(struct test_suite *test __maybe_unused, int subtest
return ret;
}

+static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+{
+ char dir[PATH_MAX];
+ struct parse_events_error err;
+ struct evlist *evlist;
+ struct evsel *evsel;
+ struct perf_event_attr *attr;
+ int ret;
+ struct perf_pmu *pmu = test_pmu_get(dir, sizeof(dir));
+ const char *event = "perf-pmu-test/test-event/";
+
+
+ if (!pmu)
+ return TEST_FAIL;
+
+ evlist = evlist__new();
+ if (evlist == NULL) {
+ pr_err("Failed allocation");
+ goto err_out;
+ }
+ parse_events_error__init(&err);
+ 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);
+ ret = TEST_FAIL;
+ if (parse_events_error__contains(&err, "can't access trace events"))
+ ret = TEST_SKIP;
+ goto err_out;
+ }
+ evsel = evlist__first(evlist);
+ attr = &evsel->core.attr;
+ if (attr->config != 0xc00000000002a823) {
+ pr_err("Unexpected config value %llx\n", attr->config);
+ goto err_out;
+ }
+ if (attr->config1 != 0x8000400000000145) {
+ pr_err("Unexpected config1 value %llx\n", attr->config1);
+ goto err_out;
+ }
+ if (attr->config2 != 0x0400000020041d07) {
+ pr_err("Unexpected config2 value %llx\n", attr->config2);
+ goto err_out;
+ }
+
+ ret = TEST_OK;
+err_out:
+ parse_events_error__exit(&err);
+ evlist__delete(evlist);
+ test_pmu_put(dir, pmu);
+ return ret;
+}
+
static struct test_case tests__pmu[] = {
TEST_CASE("Parsing with PMU format directory", pmu_format),
+ TEST_CASE("Parsing with PMU event", pmu_events),
{ .name = NULL, }
};

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index fbbc535ed93f..7849be4bfea1 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -597,33 +597,18 @@ static inline bool pmu_alias_info_file(const char *name)
* Reading the pmu event aliases definition, which should be located at:
* /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
*/
-static int pmu_aliases_parse(struct perf_pmu *pmu)
+static int __pmu_aliases_parse(struct perf_pmu *pmu, int events_dir_fd)
{
- char path[PATH_MAX];
struct dirent *evt_ent;
DIR *event_dir;
- size_t len;
- int fd, dir_fd;

- len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
- if (!len)
- return 0;
- scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
-
- dir_fd = open(path, O_DIRECTORY);
- if (dir_fd == -1) {
- pmu->sysfs_aliases_loaded = true;
- return 0;
- }
-
- event_dir = fdopendir(dir_fd);
- if (!event_dir){
- close (dir_fd);
+ event_dir = fdopendir(events_dir_fd);
+ if (!event_dir)
return -EINVAL;
- }

while ((evt_ent = readdir(event_dir))) {
char *name = evt_ent->d_name;
+ int fd;
FILE *file;

if (!strcmp(name, ".") || !strcmp(name, ".."))
@@ -635,7 +620,7 @@ static int pmu_aliases_parse(struct perf_pmu *pmu)
if (pmu_alias_info_file(name))
continue;

- fd = openat(dir_fd, name, O_RDONLY);
+ fd = openat(events_dir_fd, name, O_RDONLY);
if (fd == -1) {
pr_debug("Cannot open %s\n", name);
continue;
@@ -653,11 +638,50 @@ static int pmu_aliases_parse(struct perf_pmu *pmu)
}

closedir(event_dir);
- close (dir_fd);
pmu->sysfs_aliases_loaded = true;
return 0;
}

+static int pmu_aliases_parse(struct perf_pmu *pmu)
+{
+ char path[PATH_MAX];
+ size_t len;
+ int events_dir_fd, ret;
+
+ if (pmu->sysfs_aliases_loaded)
+ return 0;
+
+ len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
+ if (!len)
+ return 0;
+ scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name);
+
+ events_dir_fd = open(path, O_DIRECTORY);
+ if (events_dir_fd == -1) {
+ pmu->sysfs_aliases_loaded = true;
+ return 0;
+ }
+ ret = __pmu_aliases_parse(pmu, events_dir_fd);
+ close(events_dir_fd);
+ return ret;
+}
+
+static int pmu_aliases_parse_eager(struct perf_pmu *pmu, int sysfs_fd)
+{
+ char path[FILENAME_MAX + 7];
+ int ret, events_dir_fd;
+
+ scnprintf(path, sizeof(path), "%s/events", pmu->name);
+ events_dir_fd = openat(sysfs_fd, path, O_DIRECTORY, 0);
+ if (events_dir_fd == -1) {
+ pmu->sysfs_aliases_loaded = true;
+ return 0;
+ }
+ ret = __pmu_aliases_parse(pmu, events_dir_fd);
+ close(events_dir_fd);
+ return ret;
+}
+
static int pmu_alias_terms(struct perf_pmu_alias *alias, int err_loc, struct list_head *terms)
{
struct parse_events_term *term, *cloned;
@@ -1042,6 +1066,9 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char

perf_pmu__arch_init(pmu);

+ if (eager_load)
+ pmu_aliases_parse_eager(pmu, dirfd);
+
return pmu;
err:
zfree(&pmu->name);
--
2.44.0.769.g3c40516874-goog


2024-04-29 20:03:51

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v3 5/6] perf test pmu: Test all sysfs PMU event names are the same case

Being either lower or upper case means event name probes can avoid
scanning the directory doing case insensitive comparisons, just the
lower or upper case version of the name can be checked for
existence. For the majority of PMUs event names are all lower case,
upper case names are present on S390.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/tests/pmu.c | 90 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 90 insertions(+)

diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c
index f4e9a39534cb..ea21b4db10e1 100644
--- a/tools/perf/tests/pmu.c
+++ b/tools/perf/tests/pmu.c
@@ -5,12 +5,17 @@
#include "pmu.h"
#include "tests.h"
#include "debug.h"
+#include "fncache.h"
+#include <api/fs/fs.h>
+#include <ctype.h>
+#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
+#include <sys/types.h>

/* Fake PMUs created in temp directory. */
static LIST_HEAD(test_pmus);
@@ -241,9 +246,94 @@ static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest
return ret;
}

+static bool permitted_event_name(const char *name)
+{
+ bool has_lower = false, has_upper = false;
+
+ for (size_t i = 0; i < strlen(name); i++) {
+ char c = name[i];
+
+ if (islower(c)) {
+ if (has_upper)
+ return false;
+ has_lower = true;
+ continue;
+ }
+ if (isupper(c)) {
+ if (has_lower)
+ return false;
+ has_upper = true;
+ continue;
+ }
+ if (!isdigit(c) && c != '.' && c != '_' && c != '-')
+ return false;
+ }
+ return true;
+}
+
+static int test__pmu_event_names(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ char path[PATH_MAX];
+ DIR *pmu_dir, *event_dir;
+ struct dirent *pmu_dent, *event_dent;
+ const char *sysfs = sysfs__mountpoint();
+ int ret = TEST_OK;
+
+ if (!sysfs) {
+ pr_err("Sysfs not mounted\n");
+ return TEST_FAIL;
+ }
+
+ snprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
+ pmu_dir = opendir(path);
+ if (!pmu_dir) {
+ pr_err("Error opening \"%s\"\n", path);
+ return TEST_FAIL;
+ }
+ while ((pmu_dent = readdir(pmu_dir))) {
+ if (!strcmp(pmu_dent->d_name, ".") ||
+ !strcmp(pmu_dent->d_name, ".."))
+ continue;
+
+ snprintf(path, sizeof(path), "%s/bus/event_source/devices/%s/type",
+ sysfs, pmu_dent->d_name);
+
+ /* Does it look like a PMU? */
+ if (!file_available(path))
+ continue;
+
+ /* Process events. */
+ snprintf(path, sizeof(path), "%s/bus/event_source/devices/%s/events",
+ sysfs, pmu_dent->d_name);
+
+ event_dir = opendir(path);
+ if (!event_dir) {
+ pr_debug("Skipping as no event directory \"%s\"\n", path);
+ continue;
+ }
+ while ((event_dent = readdir(event_dir))) {
+ const char *event_name = event_dent->d_name;
+
+ if (!strcmp(event_name, ".") || !strcmp(event_name, ".."))
+ continue;
+
+ if (!permitted_event_name(event_name)) {
+ pr_err("Invalid sysfs event name: %s/%s\n",
+ pmu_dent->d_name, event_name);
+ ret = TEST_FAIL;
+ }
+ }
+ closedir(event_dir);
+ }
+ closedir(pmu_dir);
+ return ret;
+}
+
static struct test_case tests__pmu[] = {
TEST_CASE("Parsing with PMU format directory", pmu_format),
TEST_CASE("Parsing with PMU event", pmu_events),
+ TEST_CASE("PMU event names", pmu_event_names),
{ .name = NULL, }
};

--
2.44.0.769.g3c40516874-goog


2024-04-29 20:04:11

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v3 2/6] perf Document: Sysfs event names must be lower or upper case

To avoid directory scans in perf it is going to be assumed that sysfs
event names are either lower or upper case.

Signed-off-by: Ian Rogers <[email protected]>
---
.../ABI/testing/sysfs-bus-event_source-devices-events | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-bus-event_source-devices-events b/Documentation/ABI/testing/sysfs-bus-event_source-devices-events
index 77de58d03822..e7efeab2ee83 100644
--- a/Documentation/ABI/testing/sysfs-bus-event_source-devices-events
+++ b/Documentation/ABI/testing/sysfs-bus-event_source-devices-events
@@ -37,6 +37,12 @@ Description: Per-pmu performance monitoring events specific to the running syste
performance monitoring event supported by the <pmu>. The name
of the file is the name of the event.

+ As performance monitoring event names are case
+ insensitive in the perf tool, the perf tool only looks
+ for lower or upper case event names in sysfs to avoid
+ scanning the directory. It is therefore required the
+ name of the event here is either lower or upper case.
+
File contents:

<term>[=<value>][,<term>[=<value>]]...
--
2.44.0.769.g3c40516874-goog


2024-04-29 20:04:13

by Ian Rogers

[permalink] [raw]
Subject: [PATCH v3 6/6] perf pmu: Assume sysfs events are always the same case

Perf event names aren't case sensitive. For sysfs events the entire
directory of events is read then iterated comparing names in a case
insensitive way, most often to see if an event is present.

Consider:
$ perf stat -e inst_retired.any true

The event inst_retired.any may be present in any PMU, so every PMU's
sysfs events are loaded and then searched with strcasecmp to see if
any match. This event is only present on the cpu PMU as a json event
so a lot of events were loaded from sysfs unnecessarily just to prove
an event didn't exist there.

This change avoids loading all the events by assuming sysfs event
names are always either lower or uppercase. It uses file exists and
only loads the events when the desired event is present.

For the example above, the number of openat calls measured by perf
trace on a tigerlake laptop goes from 325 down to 255. The reduction
will be larger for machines with many PMUs, particularly replicated
uncore PMUs.

Ensure pmu_aliases_parse is called before all uses of the aliases
list, but remove some "pmu->sysfs_aliases_loaded" tests as they are
now part of the function.

Signed-off-by: Ian Rogers <[email protected]>
---
tools/perf/util/pmu.c | 31 ++++++++++++++++++++++++++-----
1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 7849be4bfea1..7275076e6d83 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -425,9 +425,30 @@ static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
{
struct perf_pmu_alias *alias;

- if (load && !pmu->sysfs_aliases_loaded)
- pmu_aliases_parse(pmu);
+ if (load && !pmu->sysfs_aliases_loaded) {
+ bool has_sysfs_event;
+ char event_file_name[FILENAME_MAX + 8];

+ /*
+ * Test if alias/event 'name' exists in the PMU's sysfs/events
+ * directory. If not skip parsing the sysfs aliases. Syfs event
+ * name must be all lower or all upper case.
+ */
+ scnprintf(event_file_name, sizeof(event_file_name), "events/%s", name);
+ for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
+ event_file_name[i] = tolower(event_file_name[i]);
+
+ has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
+ if (!has_sysfs_event) {
+ for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
+ event_file_name[i] = toupper(event_file_name[i]);
+
+ has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
+ }
+ if (has_sysfs_event)
+ pmu_aliases_parse(pmu);
+
+ }
list_for_each_entry(alias, &pmu->aliases, list) {
if (!strcasecmp(alias->name, name))
return alias;
@@ -1717,9 +1738,7 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
{
size_t nr;

- if (!pmu->sysfs_aliases_loaded)
- pmu_aliases_parse(pmu);
-
+ pmu_aliases_parse(pmu);
nr = pmu->sysfs_aliases;

if (pmu->cpu_aliases_added)
@@ -1778,6 +1797,7 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
struct strbuf sb;

strbuf_init(&sb, /*hint=*/ 0);
+ pmu_aliases_parse(pmu);
pmu_add_cpu_aliases(pmu);
list_for_each_entry(event, &pmu->aliases, list) {
size_t buf_used;
@@ -2193,6 +2213,7 @@ const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config)
if (!pmu)
return NULL;

+ pmu_aliases_parse(pmu);
pmu_add_cpu_aliases(pmu);
list_for_each_entry(event, &pmu->aliases, list) {
struct perf_event_attr attr = {.config = 0,};
--
2.44.0.769.g3c40516874-goog


2024-05-01 03:06:14

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v3 4/6] perf test pmu: Add an eagerly loaded event test

Hi Ian,

kernel test robot noticed the following build errors:

[auto build test ERROR on perf-tools-next/perf-tools-next]
[also build test ERROR on tip/perf/core perf-tools/perf-tools linus/master v6.9-rc6 next-20240430]
[cannot apply to acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Ian-Rogers/perf-test-pmu-events-Make-it-clearer-that-pmu-events-tests-json-events/20240430-040959
base: https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git perf-tools-next
patch link: https://lore.kernel.org/r/20240429200225.1271876-5-irogers%40google.com
patch subject: [PATCH v3 4/6] perf test pmu: Add an eagerly loaded event test
:::::: branch date: 23 hours ago
:::::: commit date: 23 hours ago
compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240501/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings

All errors (new ones prefixed by >>):

PERF_VERSION = 6.9.rc5.gea45a6cddbea
tests/pmu.c: In function 'test_pmu_get':
tests/pmu.c:89:9: error: ignoring return value of 'write' declared with attribute 'warn_unused_result' [-Werror=unused-result]
89 | write(file, "9999\n", 5);
| ^~~~~~~~~~~~~~~~~~~~~~~~
tests/pmu.c:128:9: error: ignoring return value of 'write' declared with attribute 'warn_unused_result' [-Werror=unused-result]
128 | write(file, test_event, strlen(test_event));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tests/pmu.c: In function 'test__pmu_events':
>> tests/pmu.c:241:16: error: 'ret' may be used uninitialized [-Werror=maybe-uninitialized]
241 | return ret;
| ^~~
tests/pmu.c:198:13: note: 'ret' was declared here
198 | int ret;
| ^~~
cc1: all warnings being treated as errors
make[7]: *** [tools/build/Makefile.build:106: tools/perf/tests/pmu.o] Error 1
make[7]: *** Waiting for unfinished jobs....
make[6]: *** [tools/build/Makefile.build:158: tests] Error 2
make[6]: *** Waiting for unfinished jobs....
make[5]: *** [Makefile.perf:727: tools/perf/perf-in.o] Error 2
make[5]: *** Waiting for unfinished jobs....
make[4]: *** [Makefile.perf:264: sub-make] Error 2
make[3]: *** [Makefile:70: all] Error 2

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki


2024-05-01 03:07:45

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v3 3/6] perf test pmu: Refactor format test and exposed test APIs

Hi Ian,

kernel test robot noticed the following build errors:

[auto build test ERROR on perf-tools-next/perf-tools-next]
[also build test ERROR on tip/perf/core perf-tools/perf-tools linus/master v6.9-rc6 next-20240430]
[cannot apply to acme/perf/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Ian-Rogers/perf-test-pmu-events-Make-it-clearer-that-pmu-events-tests-json-events/20240430-040959
base: https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git perf-tools-next
patch link: https://lore.kernel.org/r/20240429200225.1271876-4-irogers%40google.com
patch subject: [PATCH v3 3/6] perf test pmu: Refactor format test and exposed test APIs
:::::: branch date: 20 hours ago
:::::: commit date: 20 hours ago
compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240430/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

All errors (new ones prefixed by >>):

PERF_VERSION = 6.9.rc5.g00252012d0a2
tests/pmu.c: In function 'test_pmu_get':
>> tests/pmu.c:84:9: error: ignoring return value of 'write' declared with attribute 'warn_unused_result' [-Werror=unused-result]
84 | write(file, "9999\n", 5);
| ^~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[7]: *** [tools/build/Makefile.build:106: tools/perf/tests/pmu.o] Error 1
make[7]: *** Waiting for unfinished jobs....
make[6]: *** [tools/build/Makefile.build:158: tests] Error 2
make[6]: *** Waiting for unfinished jobs....
make[5]: *** [Makefile.perf:727: tools/perf/perf-in.o] Error 2
make[5]: *** Waiting for unfinished jobs....
make[4]: *** [Makefile.perf:264: sub-make] Error 2
make[3]: *** [Makefile:70: all] Error 2

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki