Skip to contentSkip to conversation

perf metricgroup: Avoid scanning unnecessary PMUs for identifier match

PATCH Superseded Ian Rogers 4 participants 10 messages Mar 26 – May 14 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf metricgroup: Avoid scanning unnecessary PMUs for identifier match

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

---
 tools/perf/util/metricgroup.c |  8 ++------
 tools/perf/util/pmus.c        | 18 +++++++++++++++++-
 tools/perf/util/pmus.h        |  1 +
 3 files changed, 20 insertions(+), 7 deletions(-)
tools/perf/util/metricgroup.c +2 -6
index 7e39d469111b..769b38400832 100644
@@ -410,13 +410,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
 	if (!pm->metric_expr || !pm->compat)
 		return 0;
 
-	while ((pmu = perf_pmus__scan(pmu))) {
-
-		if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-			continue;
-
+	while ((pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat)))
 		return d->fn(pm, table, d->data);
-	}
+
 	return 0;
 }
 
tools/perf/util/pmus.c +17 -1
index 98be2eb8f1f0..9859d7056fb7 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
 	if (!pmu) {
 		/*
 		 * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-		 * aren't wother optimizing for.
+		 * aren't worth optimizing for.
 		 */
 		unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
 			PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
 	return NULL;
 }
 
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+	if (!pmu) {
+		/* Only uncore PMUs can have identifiers. */
+		unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+		pmu_read_sysfs(to_read_pmus);
+		pmu = list_prepare_entry(pmu, &other_pmus, list);
+	}
+	list_for_each_entry_continue(pmu, &other_pmus, list) {
+		if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+			return pmu;
+	}
+	return NULL;
+}
+
 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
 {
 	struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +1 -0
index 7cb36863711a..0d55edb3f2fc 100644
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
 struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
 struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
 struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
 
 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
 
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Ian Rogers

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

Quoted 74 lines
---
  tools/perf/util/metricgroup.c |  8 ++------
  tools/perf/util/pmus.c        | 18 +++++++++++++++++-
  tools/perf/util/pmus.h        |  1 +
  3 files changed, 20 insertions(+), 7 deletions(-)
tools/perf/util/metricgroup.c +2 -6
index 7e39d469111b..769b38400832 100644
@@ -410,13 +410,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
  	if (!pm->metric_expr || !pm->compat)
  		return 0;
 
-	while ((pmu = perf_pmus__scan(pmu))) {
-
-		if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-			continue;
-
+	while ((pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat)))
  		return d->fn(pm, table, d->data);
-	}
+
  	return 0;
  }
 
tools/perf/util/pmus.c +17 -1
index 98be2eb8f1f0..9859d7056fb7 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
  	if (!pmu) {
  		/*
  		 * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-		 * aren't wother optimizing for.
+		 * aren't worth optimizing for.
  		 */
  		unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
  			PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
  	return NULL;
  }
 
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+	if (!pmu) {
+		/* Only uncore PMUs can have identifiers. */
+		unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+		pmu_read_sysfs(to_read_pmus);
+		pmu = list_prepare_entry(pmu, &other_pmus, list);
+	}
+	list_for_each_entry_continue(pmu, &other_pmus, list) {
+		if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+			return pmu;
+	}
+	return NULL;
+}
+
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
  {
  	struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +1 -0
index 7cb36863711a..0d55edb3f2fc 100644
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
  struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
 
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
 
maintainer
#2 lore raw
Quoted 17 lines · James Clark
Quoted 5 lines · Ian Rogers

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

Quoted 6 lines
---
  tools/perf/util/metricgroup.c |  8 ++------
  tools/perf/util/pmus.c        | 18 +++++++++++++++++-
  tools/perf/util/pmus.h        |  1 +
  3 files changed, 20 insertions(+), 7 deletions(-)
Quoted 14 lines
Quoted 5 lines
tools/perf/util/metricgroup.c +0 -0
index 7e39d469111b..769b38400832 100644

tools/perf/util/metricgroup.c static int metricgroup__sys_event_iter(const struct pmu_metric *pm, · line 413

Quoted 9 lines
tools/perf/util/metricgroup.c +1 -5
@@ -410,13 +410,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
  	if (!pm->metric_expr || !pm->compat)
  		return 0;
-	while ((pmu = perf_pmus__scan(pmu))) {
-
-		if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-			continue;
-
+	while ((pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat)))
  		return d->fn(pm, table, d->data);

Sashiko review: it's natural to convert it to 'if'.

Thanks,
Namhyung

Quoted 50 lines
Quoted 49 lines
tools/perf/util/metricgroup.c +1 -1
-	}
+
  	return 0;
  }
tools/perf/util/pmus.c +17 -1
index 98be2eb8f1f0..9859d7056fb7 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
  	if (!pmu) {
  		/*
  		 * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-		 * aren't wother optimizing for.
+		 * aren't worth optimizing for.
  		 */
  		unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
  			PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
  	return NULL;
  }
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+	if (!pmu) {
+		/* Only uncore PMUs can have identifiers. */
+		unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+		pmu_read_sysfs(to_read_pmus);
+		pmu = list_prepare_entry(pmu, &other_pmus, list);
+	}
+	list_for_each_entry_continue(pmu, &other_pmus, list) {
+		if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+			return pmu;
+	}
+	return NULL;
+}
+
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
  {
  	struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +1 -0
index 7cb36863711a..0d55edb3f2fc 100644
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
  struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
author reviewer
#3 lore raw
Quoted 19 lines · Namhyung Kim
Quoted 17 lines · James Clark
Quoted 5 lines · Ian Rogers

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

Quoted 6 lines
---
  tools/perf/util/metricgroup.c |  8 ++------
  tools/perf/util/pmus.c        | 18 +++++++++++++++++-
  tools/perf/util/pmus.h        |  1 +
  3 files changed, 20 insertions(+), 7 deletions(-)
Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
tools/perf/util/metricgroup.c +0 -0
index 7e39d469111b..769b38400832 100644
@@ -410,13 +410,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
    if (!pm->metric_expr || !pm->compat)
Quoted 8 lines

tools/perf/util/metricgroup.c static int metricgroup__sys_event_iter(const struct pmu_metric *pm, · line 414

Quoted 8 lines
tools/perf/util/metricgroup.c +1 -5
            return 0;
-   while ((pmu = perf_pmus__scan(pmu))) {
-
-           if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-                   continue;
-
+   while ((pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat)))
            return d->fn(pm, table, d->data);

Sashiko review: it's natural to convert it to 'if'.

Yeah, I saw and disagreed. The pattern with the "while ((pmu = perf_pmus__scan...(pmu)))" functions is for them to be a while loop. With an "if" it would read:

```
pmu = perf_pmus__scan...(pmu);
if (pmu)
   return ...
```

and we lose the consistency of having while loops in the code.

Thanks,
Ian

Quoted 54 lines

Thanks,
Namhyung

Quoted 50 lines
Quoted 49 lines
tools/perf/util/metricgroup.c +1 -1
-   }
+
    return 0;
  }
tools/perf/util/pmus.c +17 -1
index 98be2eb8f1f0..9859d7056fb7 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
    if (!pmu) {
            /*
             * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-            * aren't wother optimizing for.
+            * aren't worth optimizing for.
             */
            unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
                    PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
    return NULL;
  }
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+   if (!pmu) {
+           /* Only uncore PMUs can have identifiers. */
+           unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+           pmu_read_sysfs(to_read_pmus);
+           pmu = list_prepare_entry(pmu, &other_pmus, list);
+   }
+   list_for_each_entry_continue(pmu, &other_pmus, list) {
+           if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+                   return pmu;
+   }
+   return NULL;
+}
+
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
  {
    struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +1 -0
index 7cb36863711a..0d55edb3f2fc 100644
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
  struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
maintainer
#4 lore raw
Quoted 40 lines · Ian Rogers
Quoted 19 lines · Namhyung Kim
Quoted 17 lines · James Clark
Quoted 5 lines · Ian Rogers

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

Quoted 6 lines
---
  tools/perf/util/metricgroup.c |  8 ++------
  tools/perf/util/pmus.c        | 18 +++++++++++++++++-
  tools/perf/util/pmus.h        |  1 +
  3 files changed, 20 insertions(+), 7 deletions(-)
Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
tools/perf/util/metricgroup.c +0 -0
index 7e39d469111b..769b38400832 100644
@@ -410,13 +410,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
    if (!pm->metric_expr || !pm->compat)
Quoted 8 lines

tools/perf/util/metricgroup.c static int metricgroup__sys_event_iter(const struct pmu_metric *pm, · line 414

Quoted 8 lines
tools/perf/util/metricgroup.c +1 -5
            return 0;
-   while ((pmu = perf_pmus__scan(pmu))) {
-
-           if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-                   continue;
-
+   while ((pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat)))
            return d->fn(pm, table, d->data);

Sashiko review: it's natural to convert it to 'if'.

Yeah, I saw and disagreed. The pattern with the "while ((pmu = perf_pmus__scan...(pmu)))" functions is for them to be a while loop. With an "if" it would read:

Quoted 6 lines
```
pmu = perf_pmus__scan...(pmu);
if (pmu)
   return ...
```

and we lose the consistency of having while loops in the code.

I understand it's a pattern to scan PMUs but having an unused variable assignment in a while loop with a return statement seems unnatural and maybe someone would send a patch for it later.

At least we can add a comment saying it's intended?

Thanks,
Namhyung

author reviewer
#5 lore raw

PATCH v2 1/1 perf metricgroup: Avoid scanning unnecessary PMUs for identifier match

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

---
 tools/perf/util/metricgroup.c | 11 +++--------
 tools/perf/util/pmus.c        | 18 +++++++++++++++++-
 tools/perf/util/pmus.h        |  1 +
 3 files changed, 21 insertions(+), 9 deletions(-)
tools/perf/util/metricgroup.c +3 -8
index 4db9578efd81..5a489e97c413 100644
@@ -415,14 +415,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
 	if (!pm->metric_expr || !pm->compat)
 		return 0;
 
-	while ((pmu = perf_pmus__scan(pmu))) {
-
-		if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-			continue;
-
-		return d->fn(pm, table, d->data);
-	}
-	return 0;
+	/* Only process with the iterator if there is a a PMU that matches the ID. */
+	pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat);
+	return pmu ? d->fn(pm, table, d->data) : 0;
 }
 
 int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
tools/perf/util/pmus.c +17 -1
index 9a2023ceeefd..5e3f571450fe 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
 	if (!pmu) {
 		/*
 		 * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-		 * aren't wother optimizing for.
+		 * aren't worth optimizing for.
 		 */
 		unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
 			PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
 	return NULL;
 }
 
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+	if (!pmu) {
+		/* Only uncore PMUs can have identifiers. */
+		unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+		pmu_read_sysfs(to_read_pmus);
+		pmu = list_prepare_entry(pmu, &other_pmus, list);
+	}
+	list_for_each_entry_continue(pmu, &other_pmus, list) {
+		if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+			return pmu;
+	}
+	return NULL;
+}
+
 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
 {
 	struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +1 -0
index 7cb36863711a..0d55edb3f2fc 100644
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
 struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
 struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
 struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
 
 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
 
Signature · 1 line
2.54.0.545.g6539524ca2-goog
author reviewer
#6 lore raw
Quoted 6 lines · Ian Rogers

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

This passes Sashiko review (green). I didn't copy James Clark's reviewed-by tag given the metricgroup code rewrite to avoid the loop, but I think this is ready to land.

Thanks,
Ian

Quoted 79 lines
---
 tools/perf/util/metricgroup.c | 11 +++--------
 tools/perf/util/pmus.c        | 18 +++++++++++++++++-
 tools/perf/util/pmus.h        |  1 +
 3 files changed, 21 insertions(+), 9 deletions(-)
tools/perf/util/metricgroup.c +3 -8
index 4db9578efd81..5a489e97c413 100644
@@ -415,14 +415,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
        if (!pm->metric_expr || !pm->compat)
                return 0;
 
-       while ((pmu = perf_pmus__scan(pmu))) {
-
-               if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-                       continue;
-
-               return d->fn(pm, table, d->data);
-       }
-       return 0;
+       /* Only process with the iterator if there is a a PMU that matches the ID. */
+       pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat);
+       return pmu ? d->fn(pm, table, d->data) : 0;
 }
 
 int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
tools/perf/util/pmus.c +17 -1
index 9a2023ceeefd..5e3f571450fe 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
        if (!pmu) {
                /*
                 * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-                * aren't wother optimizing for.
+                * aren't worth optimizing for.
                 */
                unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
                        PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
        return NULL;
 }
 
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+       if (!pmu) {
+               /* Only uncore PMUs can have identifiers. */
+               unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+               pmu_read_sysfs(to_read_pmus);
+               pmu = list_prepare_entry(pmu, &other_pmus, list);
+       }
+       list_for_each_entry_continue(pmu, &other_pmus, list) {
+               if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+                       return pmu;
+       }
+       return NULL;
+}
+
 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
 {
        struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +1 -1
index 7cb36863711a..0d55edb3f2fc 100644
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
 struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
 struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
 struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
 
 const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
 
--

2.54.0.545.g6539524ca2-goog

reviewer Reviewed-by on v2 1/1
#7 lore raw
In reply to Ian Rogers
Quoted 74 lines · Ian Rogers

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

---
  tools/perf/util/metricgroup.c | 11 +++--------
  tools/perf/util/pmus.c        | 18 +++++++++++++++++-
  tools/perf/util/pmus.h        |  1 +
  3 files changed, 21 insertions(+), 9 deletions(-)
tools/perf/util/metricgroup.c +3 -8
index 4db9578efd81..5a489e97c413 100644
@@ -415,14 +415,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
  	if (!pm->metric_expr || !pm->compat)
  		return 0;
 
-	while ((pmu = perf_pmus__scan(pmu))) {
-
-		if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-			continue;
-
-		return d->fn(pm, table, d->data);
-	}
-	return 0;
+	/* Only process with the iterator if there is a a PMU that matches the ID. */
+	pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat);
+	return pmu ? d->fn(pm, table, d->data) : 0;
  }
 
  int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
tools/perf/util/pmus.c +17 -1
index 9a2023ceeefd..5e3f571450fe 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
  	if (!pmu) {
  		/*
  		 * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-		 * aren't wother optimizing for.
+		 * aren't worth optimizing for.
  		 */
  		unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
  			PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
  	return NULL;
  }
 
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+	if (!pmu) {
+		/* Only uncore PMUs can have identifiers. */
+		unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+		pmu_read_sysfs(to_read_pmus);
+		pmu = list_prepare_entry(pmu, &other_pmus, list);
+	}
+	list_for_each_entry_continue(pmu, &other_pmus, list) {
+		if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+			return pmu;
+	}
+	return NULL;
+}
+
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
  {
  	struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +0 -0
index 7cb36863711a..0d55edb3f2fc 100644

tools/perf/util/pmus.h struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu); · line 29

Quoted 7 lines
tools/perf/util/pmus.h +1 -0
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
  struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
 
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
 
author reviewer
#8 lore raw
Quoted 83 lines · James Clark
Quoted 79 lines · Ian Rogers

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

---
  tools/perf/util/metricgroup.c | 11 +++--------
  tools/perf/util/pmus.c        | 18 +++++++++++++++++-
  tools/perf/util/pmus.h        |  1 +
  3 files changed, 21 insertions(+), 9 deletions(-)
tools/perf/util/metricgroup.c +3 -8
index 4db9578efd81..5a489e97c413 100644
@@ -415,14 +415,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
      if (!pm->metric_expr || !pm->compat)
              return 0;
 
-     while ((pmu = perf_pmus__scan(pmu))) {
-
-             if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-                     continue;
-
-             return d->fn(pm, table, d->data);
-     }
-     return 0;
+     /* Only process with the iterator if there is a a PMU that matches the ID. */
+     pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat);
+     return pmu ? d->fn(pm, table, d->data) : 0;
  }
 
  int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
tools/perf/util/pmus.c +17 -1
index 9a2023ceeefd..5e3f571450fe 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
      if (!pmu) {
              /*
               * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-              * aren't wother optimizing for.
+              * aren't worth optimizing for.
               */
              unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
                      PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
      return NULL;
  }
 
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+     if (!pmu) {
+             /* Only uncore PMUs can have identifiers. */
+             unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+             pmu_read_sysfs(to_read_pmus);
+             pmu = list_prepare_entry(pmu, &other_pmus, list);
+     }
+     list_for_each_entry_continue(pmu, &other_pmus, list) {
+             if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+                     return pmu;
+     }
+     return NULL;
+}
+
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
  {
      struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +1 -0
index 7cb36863711a..0d55edb3f2fc 100644
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
  struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
 
Quoted 4 lines

tools/perf/util/pmus.h struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu); · line 29

Quoted 2 lines
tools/perf/util/pmus.h +0 -0
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
 

Ping.

Thanks,
Ian

maintainer Applied
#9 lore raw
Quoted 81 lines · Ian Rogers
Quoted 80 lines · James Clark
Quoted 79 lines · Ian Rogers

Only uncore PMUs can have an identifier, so add an optimized perf_pmus__scan routine for that case to avoid all PMU types being created.

---
  tools/perf/util/metricgroup.c | 11 +++--------
  tools/perf/util/pmus.c        | 18 +++++++++++++++++-
  tools/perf/util/pmus.h        |  1 +
  3 files changed, 21 insertions(+), 9 deletions(-)
tools/perf/util/metricgroup.c +3 -8
index 4db9578efd81..5a489e97c413 100644
@@ -415,14 +415,9 @@ static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
      if (!pm->metric_expr || !pm->compat)
              return 0;
 
-     while ((pmu = perf_pmus__scan(pmu))) {
-
-             if (!pmu->id || !pmu_uncore_identifier_match(pm->compat, pmu->id))
-                     continue;
-
-             return d->fn(pm, table, d->data);
-     }
-     return 0;
+     /* Only process with the iterator if there is a a PMU that matches the ID. */
+     pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat);
+     return pmu ? d->fn(pm, table, d->data) : 0;
  }
 
  int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
tools/perf/util/pmus.c +17 -1
index 9a2023ceeefd..5e3f571450fe 100644
@@ -409,7 +409,7 @@ struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const c
      if (!pmu) {
              /*
               * Core PMUs, other sysfs PMUs and tool PMU can have any name or
-              * aren't wother optimizing for.
+              * aren't worth optimizing for.
               */
              unsigned int to_read_pmus =  PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
                      PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
@@ -486,6 +486,22 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
      return NULL;
  }
 
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat)
+{
+     if (!pmu) {
+             /* Only uncore PMUs can have identifiers. */
+             unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_OTHER_MASK;
+
+             pmu_read_sysfs(to_read_pmus);
+             pmu = list_prepare_entry(pmu, &other_pmus, list);
+     }
+     list_for_each_entry_continue(pmu, &other_pmus, list) {
+             if (pmu->id && pmu_uncore_identifier_match(compat, pmu->id))
+                     return pmu;
+     }
+     return NULL;
+}
+
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
  {
      struct perf_pmu *pmu = NULL;
tools/perf/util/pmus.h +1 -0
index 7cb36863711a..0d55edb3f2fc 100644
@@ -23,6 +23,7 @@ struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
  struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
  struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
+struct perf_pmu *perf_pmus__scan_for_uncore_id(struct perf_pmu *pmu, const char *compat);
 
Quoted 6 lines
Quoted 4 lines

tools/perf/util/pmus.h struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu); · line 29

Quoted 2 lines
tools/perf/util/pmus.h +0 -0
  const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);
 

Ping.

Thanks, applied to perf-tools-next, for v7.2.

- Arnaldo