[PATCH v3 12/46] perf test: Test more sysfs events

Ian Rogers posted 46 patches 2 years, 7 months ago
There is a newer version of this series
[PATCH v3 12/46] perf test: Test more sysfs events
Posted by Ian Rogers 2 years, 7 months ago
Parse events for all PMUs, and not just cpu, in test "Parsing of all
PMU events from sysfs".

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/parse-events.c | 121 +++++++++++++++++---------------
 1 file changed, 65 insertions(+), 56 deletions(-)

diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-events.c
index 8068cfd89b84..35b35a5c795c 100644
--- a/tools/perf/tests/parse-events.c
+++ b/tools/perf/tests/parse-events.c
@@ -7,6 +7,7 @@
 #include "debug.h"
 #include "pmu.h"
 #include "pmu-hybrid.h"
+#include "pmus.h"
 #include <dirent.h>
 #include <errno.h>
 #include "fncache.h"
@@ -2225,74 +2226,82 @@ static int test_pmu(void)
 
 static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
 {
-	struct stat st;
-	char path[PATH_MAX];
-	struct dirent *ent;
-	DIR *dir;
-	int ret;
+	struct perf_pmu *pmu;
+	int ret = TEST_OK;
 
-	if (!test_pmu())
-		return TEST_SKIP;
+	perf_pmus__for_each_pmu(pmu) {
+		struct stat st;
+		char path[PATH_MAX];
+		struct dirent *ent;
+		DIR *dir;
+		int err;
 
-	snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/events/",
-		 sysfs__mountpoint());
+		snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/events/",
+			sysfs__mountpoint(), pmu->name);
 
-	ret = stat(path, &st);
-	if (ret) {
-		pr_debug("omitting PMU cpu events tests: %s\n", path);
-		return TEST_OK;
-	}
+		err = stat(path, &st);
+		if (err) {
+			pr_debug("skipping PMU %s events tests: %s\n", pmu->name, path);
+			ret = combine_test_results(ret, TEST_SKIP);
+			continue;
+		}
 
-	dir = opendir(path);
-	if (!dir) {
-		pr_debug("can't open pmu event dir: %s\n", path);
-		return TEST_FAIL;
-	}
+		dir = opendir(path);
+		if (!dir) {
+			pr_debug("can't open pmu event dir: %s\n", path);
+			ret = combine_test_results(ret, TEST_SKIP);
+			continue;
+		}
 
-	ret = TEST_OK;
-	while ((ent = readdir(dir))) {
-		struct evlist_test e = { .name = NULL, };
-		char name[2 * NAME_MAX + 1 + 12 + 3];
-		int test_ret;
+		while ((ent = readdir(dir))) {
+			struct evlist_test e = { .name = NULL, };
+			char name[2 * NAME_MAX + 1 + 12 + 3];
+			int test_ret;
 
-		/* Names containing . are special and cannot be used directly */
-		if (strchr(ent->d_name, '.'))
-			continue;
+			/* Names containing . are special and cannot be used directly */
+			if (strchr(ent->d_name, '.'))
+				continue;
 
-		snprintf(name, sizeof(name), "cpu/event=%s/u", ent->d_name);
+			snprintf(name, sizeof(name), "%s/event=%s/u", pmu->name, ent->d_name);
 
-		e.name  = name;
-		e.check = test__checkevent_pmu_events;
+			e.name  = name;
+			e.check = test__checkevent_pmu_events;
 
-		test_ret = test_event(&e);
-		if (test_ret != TEST_OK) {
-			pr_debug("Test PMU event failed for '%s'", name);
-			ret = combine_test_results(ret, test_ret);
-		}
-		/*
-		 * Names containing '-' are recognized as prefixes and suffixes
-		 * due to '-' being a legacy PMU separator. This fails when the
-		 * prefix or suffix collides with an existing legacy token. For
-		 * example, branch-brs has a prefix (branch) that collides with
-		 * a PE_NAME_CACHE_TYPE token causing a parse error as a suffix
-		 * isn't expected after this. As event names in the config
-		 * slashes are allowed a '-' in the name we check this works
-		 * above.
-		 */
-		if (strchr(ent->d_name, '-'))
-			continue;
+			test_ret = test_event(&e);
+			if (test_ret != TEST_OK) {
+				pr_debug("Test PMU event failed for '%s'", name);
+				ret = combine_test_results(ret, test_ret);
+			}
 
-		snprintf(name, sizeof(name), "%s:u,cpu/event=%s/u", ent->d_name, ent->d_name);
-		e.name  = name;
-		e.check = test__checkevent_pmu_events_mix;
-		test_ret = test_event(&e);
-		if (test_ret != TEST_OK) {
-			pr_debug("Test PMU event failed for '%s'", name);
-			ret = combine_test_results(ret, test_ret);
+			if (!is_pmu_core(pmu->name))
+				continue;
+
+			/*
+			 * Names containing '-' are recognized as prefixes and suffixes
+			 * due to '-' being a legacy PMU separator. This fails when the
+			 * prefix or suffix collides with an existing legacy token. For
+			 * example, branch-brs has a prefix (branch) that collides with
+			 * a PE_NAME_CACHE_TYPE token causing a parse error as a suffix
+			 * isn't expected after this. As event names in the config
+			 * slashes are allowed a '-' in the name we check this works
+			 * above.
+			 */
+			if (strchr(ent->d_name, '-'))
+				continue;
+
+			snprintf(name, sizeof(name), "%s:u,%s/event=%s/u",
+				 ent->d_name, pmu->name, ent->d_name);
+			e.name  = name;
+			e.check = test__checkevent_pmu_events_mix;
+			test_ret = test_event(&e);
+			if (test_ret != TEST_OK) {
+				pr_debug("Test PMU event failed for '%s'", name);
+				ret = combine_test_results(ret, test_ret);
+			}
 		}
-	}
 
-	closedir(dir);
+		closedir(dir);
+	}
 	return ret;
 }
 
-- 
2.40.1.495.gc816e09b53d-goog
Re: [PATCH v3 12/46] perf test: Test more sysfs events
Posted by Ravi Bangoria 2 years, 7 months ago
> @@ -2225,74 +2226,82 @@ static int test_pmu(void)
>  
>  static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
>  {
> -	struct stat st;
> -	char path[PATH_MAX];
> -	struct dirent *ent;
> -	DIR *dir;
> -	int ret;
> +	struct perf_pmu *pmu;
> +	int ret = TEST_OK;
>  
> -	if (!test_pmu())
> -		return TEST_SKIP;
> +	perf_pmus__for_each_pmu(pmu) {

'pmus' list might be empty and, if so, we need to fill it first with:

        if (list_empty(&pmus))
                perf_pmu__scan(NULL);

before iterating over it. Other option is to add this code to
perf_pmus__for_each_pmu() macro itself.

With that, the test fails for me:

$ sudo ./perf test 6
  6: Parse event definition strings                                  :
  6.1: Test event parsing                                            : Ok
  6.2: Test parsing of "hybrid" CPU events                           : Skip (not hybrid)
  6.3: Parsing of all PMU events from sysfs                          : FAILED!

> +		struct stat st;
> +		char path[PATH_MAX];
> +		struct dirent *ent;
> +		DIR *dir;
> +		int err;
>  
> -	snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/events/",
> -		 sysfs__mountpoint());
> +		snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/events/",
> +			sysfs__mountpoint(), pmu->name);
>  
> -	ret = stat(path, &st);
> -	if (ret) {
> -		pr_debug("omitting PMU cpu events tests: %s\n", path);
> -		return TEST_OK;
> -	}
> +		err = stat(path, &st);
> +		if (err) {
> +			pr_debug("skipping PMU %s events tests: %s\n", pmu->name, path);
> +			ret = combine_test_results(ret, TEST_SKIP);

combine_test_results(ret, TEST_OK); probably? Since many pmus don't expose
events via sysfs.
Re: [PATCH v3 12/46] perf test: Test more sysfs events
Posted by Ian Rogers 2 years, 7 months ago
On Tue, May 2, 2023 at 3:27 AM Ravi Bangoria <ravi.bangoria@amd.com> wrote:
>
> > @@ -2225,74 +2226,82 @@ static int test_pmu(void)
> >
> >  static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
> >  {
> > -     struct stat st;
> > -     char path[PATH_MAX];
> > -     struct dirent *ent;
> > -     DIR *dir;
> > -     int ret;
> > +     struct perf_pmu *pmu;
> > +     int ret = TEST_OK;
> >
> > -     if (!test_pmu())
> > -             return TEST_SKIP;
> > +     perf_pmus__for_each_pmu(pmu) {
>
> 'pmus' list might be empty and, if so, we need to fill it first with:
>
>         if (list_empty(&pmus))
>                 perf_pmu__scan(NULL);
>
> before iterating over it. Other option is to add this code to
> perf_pmus__for_each_pmu() macro itself.
>
> With that, the test fails for me:
>
> $ sudo ./perf test 6
>   6: Parse event definition strings                                  :
>   6.1: Test event parsing                                            : Ok
>   6.2: Test parsing of "hybrid" CPU events                           : Skip (not hybrid)
>   6.3: Parsing of all PMU events from sysfs                          : FAILED!
>
> > +             struct stat st;
> > +             char path[PATH_MAX];
> > +             struct dirent *ent;
> > +             DIR *dir;
> > +             int err;
> >
> > -     snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/events/",
> > -              sysfs__mountpoint());
> > +             snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/events/",
> > +                     sysfs__mountpoint(), pmu->name);
> >
> > -     ret = stat(path, &st);
> > -     if (ret) {
> > -             pr_debug("omitting PMU cpu events tests: %s\n", path);
> > -             return TEST_OK;
> > -     }
> > +             err = stat(path, &st);
> > +             if (err) {
> > +                     pr_debug("skipping PMU %s events tests: %s\n", pmu->name, path);
> > +                     ret = combine_test_results(ret, TEST_SKIP);
>
> combine_test_results(ret, TEST_OK); probably? Since many pmus don't expose
> events via sysfs.

Thanks Ravi! The following looks to address the issues and I'll add it to v4.

'''
diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-events.c
index 35b35a5c795c..2ff61ee8f970 100644
--- a/tools/perf/tests/parse-events.c
+++ b/tools/perf/tests/parse-events.c
@@ -559,7 +559,8 @@ static int test__checkevent_pmu_events(struct
evlist *evlist)
       struct evsel *evsel = evlist__first(evlist);

       TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
+       TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type ||
+                                     strncmp(evsel->pmu_name, "cpu/", 4));
       TEST_ASSERT_VAL("wrong exclude_user",
                       !evsel->core.attr.exclude_user);
       TEST_ASSERT_VAL("wrong exclude_kernel",
@@ -2229,6 +2230,9 @@ static int test__pmu_events(struct test_suite
*test __maybe_unused, int subtes
t
       struct perf_pmu *pmu;
       int ret = TEST_OK;

+       if (list_empty(&pmus))
+                perf_pmu__scan(NULL);
+
       perf_pmus__for_each_pmu(pmu) {
               struct stat st;
               char path[PATH_MAX];
@@ -2242,7 +2246,6 @@ static int test__pmu_events(struct test_suite
*test __maybe_unused, int subtes
t
               err = stat(path, &st);
               if (err) {
                       pr_debug("skipping PMU %s events tests: %s\n",
pmu->name, path);
-                       ret = combine_test_results(ret, TEST_SKIP);
                       continue;
               }
'''
Re: [PATCH v3 12/46] perf test: Test more sysfs events
Posted by Ian Rogers 2 years, 7 months ago
On Tue, May 2, 2023 at 8:16 AM Ian Rogers <irogers@google.com> wrote:
>
> On Tue, May 2, 2023 at 3:27 AM Ravi Bangoria <ravi.bangoria@amd.com> wrote:
> >
> > > @@ -2225,74 +2226,82 @@ static int test_pmu(void)
> > >
> > >  static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
> > >  {
> > > -     struct stat st;
> > > -     char path[PATH_MAX];
> > > -     struct dirent *ent;
> > > -     DIR *dir;
> > > -     int ret;
> > > +     struct perf_pmu *pmu;
> > > +     int ret = TEST_OK;
> > >
> > > -     if (!test_pmu())
> > > -             return TEST_SKIP;
> > > +     perf_pmus__for_each_pmu(pmu) {
> >
> > 'pmus' list might be empty and, if so, we need to fill it first with:
> >
> >         if (list_empty(&pmus))
> >                 perf_pmu__scan(NULL);
> >
> > before iterating over it. Other option is to add this code to
> > perf_pmus__for_each_pmu() macro itself.
> >
> > With that, the test fails for me:
> >
> > $ sudo ./perf test 6
> >   6: Parse event definition strings                                  :
> >   6.1: Test event parsing                                            : Ok
> >   6.2: Test parsing of "hybrid" CPU events                           : Skip (not hybrid)
> >   6.3: Parsing of all PMU events from sysfs                          : FAILED!
> >
> > > +             struct stat st;
> > > +             char path[PATH_MAX];
> > > +             struct dirent *ent;
> > > +             DIR *dir;
> > > +             int err;
> > >
> > > -     snprintf(path, PATH_MAX, "%s/bus/event_source/devices/cpu/events/",
> > > -              sysfs__mountpoint());
> > > +             snprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/events/",
> > > +                     sysfs__mountpoint(), pmu->name);
> > >
> > > -     ret = stat(path, &st);
> > > -     if (ret) {
> > > -             pr_debug("omitting PMU cpu events tests: %s\n", path);
> > > -             return TEST_OK;
> > > -     }
> > > +             err = stat(path, &st);
> > > +             if (err) {
> > > +                     pr_debug("skipping PMU %s events tests: %s\n", pmu->name, path);
> > > +                     ret = combine_test_results(ret, TEST_SKIP);
> >
> > combine_test_results(ret, TEST_OK); probably? Since many pmus don't expose
> > events via sysfs.
>
> Thanks Ravi! The following looks to address the issues and I'll add it to v4.
>
> '''
> diff --git a/tools/perf/tests/parse-events.c b/tools/perf/tests/parse-events.c
> index 35b35a5c795c..2ff61ee8f970 100644
> --- a/tools/perf/tests/parse-events.c
> +++ b/tools/perf/tests/parse-events.c
> @@ -559,7 +559,8 @@ static int test__checkevent_pmu_events(struct
> evlist *evlist)
>        struct evsel *evsel = evlist__first(evlist);
>
>        TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
> -       TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type);
> +       TEST_ASSERT_VAL("wrong type", PERF_TYPE_RAW == evsel->core.attr.type ||
> +                                     strncmp(evsel->pmu_name, "cpu/", 4));

This should be strcmp(evsel->pmu_name, "cpu"). Or we could drop the
assert. I'll try to keep what's covered currently by the test while
extending the PMUs.

Thanks,
Ian

>        TEST_ASSERT_VAL("wrong exclude_user",
>                        !evsel->core.attr.exclude_user);
>        TEST_ASSERT_VAL("wrong exclude_kernel",
> @@ -2229,6 +2230,9 @@ static int test__pmu_events(struct test_suite
> *test __maybe_unused, int subtes
> t
>        struct perf_pmu *pmu;
>        int ret = TEST_OK;
>
> +       if (list_empty(&pmus))
> +                perf_pmu__scan(NULL);
> +
>        perf_pmus__for_each_pmu(pmu) {
>                struct stat st;
>                char path[PATH_MAX];
> @@ -2242,7 +2246,6 @@ static int test__pmu_events(struct test_suite
> *test __maybe_unused, int subtes
> t
>                err = stat(path, &st);
>                if (err) {
>                        pr_debug("skipping PMU %s events tests: %s\n",
> pmu->name, path);
> -                       ret = combine_test_results(ret, TEST_SKIP);
>                        continue;
>                }
> '''