From: Ian Rogers <irogers@google.com>
To: Kan Liang <kan.liang@linux.intel.com>,
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>,
Jonathan Corbet <corbet@lwn.net>,
Bjorn Helgaas <bhelgaas@google.com>,
Randy Dunlap <rdunlap@infradead.org>,
Jing Zhang <renyu.zj@linux.alibaba.com>,
James Clark <james.clark@arm.com>,
Ravi Bangoria <ravi.bangoria@amd.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: [PATCH v2 5/6] perf test pmu: Test all sysfs PMU event names are lowercase
Date: Mon, 22 Apr 2024 20:17:18 -0700 [thread overview]
Message-ID: <20240423031719.1941141-6-irogers@google.com> (raw)
In-Reply-To: <20240423031719.1941141-1-irogers@google.com>
Being lowercase means event name probes can avoid scanning the
directory doing case insensitive comparisons, just the lowercase
version of the name can be checked for existence.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/pmu.c | 75 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)
diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c
index f4e9a39534cb..c49e790248cd 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,79 @@ static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest
return ret;
}
+static bool permitted_event_name_char(char c)
+{
+ if (islower(c) || isdigit(c))
+ return true;
+
+ return c == '.' || c == '_' || c == '-';
+}
+
+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("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;
+
+ for (size_t i = 0, n = strlen(event_name); i < n; i++) {
+ if (!permitted_event_name_char(event_name[i])) {
+ pr_err("Sysfs event names should be lower case \"%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
next prev parent reply other threads:[~2024-04-23 3:17 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-23 3:17 [PATCH v2 0/6] Assume sysfs event names are always lowercase Ian Rogers
2024-04-23 3:17 ` [PATCH v2 1/6] perf test pmu-events: Make it clearer that pmu-events tests json events Ian Rogers
2024-04-23 3:17 ` [PATCH v2 2/6] perf Document: Capture that sysfs event names must be lower case Ian Rogers
2024-04-23 3:17 ` [PATCH v2 3/6] perf test pmu: Refactor format test and exposed test APIs Ian Rogers
2024-04-24 2:51 ` kernel test robot
2024-04-23 3:17 ` [PATCH v2 4/6] perf test pmu: Add an eagerly loaded event test Ian Rogers
2024-04-24 2:57 ` kernel test robot
2024-04-23 3:17 ` Ian Rogers [this message]
2024-04-23 3:17 ` [PATCH v2 6/6] perf pmu: Assume sysfs events are always lowercase Ian Rogers
2024-04-23 6:53 ` [PATCH v2 0/6] Assume sysfs event names " Thomas Richter
2024-04-23 15:15 ` 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=20240423031719.1941141-6-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bhelgaas@google.com \
--cc=corbet@lwn.net \
--cc=james.clark@arm.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=ravi.bangoria@amd.com \
--cc=rdunlap@infradead.org \
--cc=renyu.zj@linux.alibaba.com \
/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