linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf metric: Code cleanup with map_for_each_event()
@ 2020-09-17  2:44 Wei Li
  2020-09-17  5:19 ` Namhyung Kim
  0 siblings, 1 reply; 3+ messages in thread
From: Wei Li @ 2020-09-17  2:44 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Ian Rogers, Kajol Jain, Kan Liang
  Cc: linux-kernel, huawei.libin, guohanjun

Since we have introduced map_for_each_event() to walk the 'pmu_events_map',
clean up metricgroup__print() and metricgroup__has_metric() with it.

Signed-off-by: Wei Li <liwei391@huawei.com>
---
 tools/perf/util/metricgroup.c | 33 +++++++++++++--------------------
 1 file changed, 13 insertions(+), 20 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 8831b964288f..3734cbb2c456 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -26,6 +26,17 @@
 #include "util.h"
 #include <asm/bug.h>
 
+#define map_for_each_event(__pe, __idx, __map)				\
+	for (__idx = 0, __pe = &__map->table[__idx];			\
+	     __pe->name || __pe->metric_group || __pe->metric_name;	\
+	     __pe = &__map->table[++__idx])
+
+#define map_for_each_metric(__pe, __idx, __map, __metric)		\
+	map_for_each_event(__pe, __idx, __map)				\
+		if (__pe->metric_expr &&				\
+		    (match_metric(__pe->metric_group, __metric) ||	\
+		     match_metric(__pe->metric_name, __metric)))
+
 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
 					 struct evsel *evsel,
 					 bool create)
@@ -475,12 +486,9 @@ void metricgroup__print(bool metrics, bool metricgroups, char *filter,
 	groups.node_new = mep_new;
 	groups.node_cmp = mep_cmp;
 	groups.node_delete = mep_delete;
-	for (i = 0; ; i++) {
+	map_for_each_event(pe, i, map) {
 		const char *g;
-		pe = &map->table[i];
 
-		if (!pe->name && !pe->metric_group && !pe->metric_name)
-			break;
 		if (!pe->metric_expr)
 			continue;
 		g = pe->metric_group;
@@ -745,17 +753,6 @@ static int __add_metric(struct list_head *metric_list,
 	return 0;
 }
 
-#define map_for_each_event(__pe, __idx, __map)				\
-	for (__idx = 0, __pe = &__map->table[__idx];			\
-	     __pe->name || __pe->metric_group || __pe->metric_name;	\
-	     __pe = &__map->table[++__idx])
-
-#define map_for_each_metric(__pe, __idx, __map, __metric)		\
-	map_for_each_event(__pe, __idx, __map)				\
-		if (__pe->metric_expr &&				\
-		    (match_metric(__pe->metric_group, __metric) ||	\
-		     match_metric(__pe->metric_name, __metric)))
-
 static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map)
 {
 	struct pmu_event *pe;
@@ -1092,11 +1089,7 @@ bool metricgroup__has_metric(const char *metric)
 	if (!map)
 		return false;
 
-	for (i = 0; ; i++) {
-		pe = &map->table[i];
-
-		if (!pe->name && !pe->metric_group && !pe->metric_name)
-			break;
+	map_for_each_event(pe, i, map) {
 		if (!pe->metric_expr)
 			continue;
 		if (match_metric(pe->metric_name, metric))
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf metric: Code cleanup with map_for_each_event()
  2020-09-17  2:44 [PATCH] perf metric: Code cleanup with map_for_each_event() Wei Li
@ 2020-09-17  5:19 ` Namhyung Kim
  2020-09-17 11:53   ` liwei (GF)
  0 siblings, 1 reply; 3+ messages in thread
From: Namhyung Kim @ 2020-09-17  5:19 UTC (permalink / raw)
  To: Wei Li
  Cc: Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Ian Rogers, Kajol Jain, Kan Liang, linux-kernel,
	Li Bin, guohanjun

Hello,

On Thu, Sep 17, 2020 at 11:45 AM Wei Li <liwei391@huawei.com> wrote:
>
> Since we have introduced map_for_each_event() to walk the 'pmu_events_map',
> clean up metricgroup__print() and metricgroup__has_metric() with it.
>
> Signed-off-by: Wei Li <liwei391@huawei.com>

Acked-by: Namhyung Kim <namhyung@kernel.org>

A nit-pick below:


> ---
>  tools/perf/util/metricgroup.c | 33 +++++++++++++--------------------
>  1 file changed, 13 insertions(+), 20 deletions(-)
>
> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
> index 8831b964288f..3734cbb2c456 100644
> --- a/tools/perf/util/metricgroup.c
> +++ b/tools/perf/util/metricgroup.c
> @@ -26,6 +26,17 @@
>  #include "util.h"
>  #include <asm/bug.h>
>
> +#define map_for_each_event(__pe, __idx, __map)                         \
> +       for (__idx = 0, __pe = &__map->table[__idx];                    \
> +            __pe->name || __pe->metric_group || __pe->metric_name;     \
> +            __pe = &__map->table[++__idx])
> +
> +#define map_for_each_metric(__pe, __idx, __map, __metric)              \
> +       map_for_each_event(__pe, __idx, __map)                          \
> +               if (__pe->metric_expr &&                                \
> +                   (match_metric(__pe->metric_group, __metric) ||      \
> +                    match_metric(__pe->metric_name, __metric)))
> +

You may consider adding a declaration of match_metric() here.
Right now, all users are below the function so it's ok but
having the macro here can enable future addition above IMHO.

Thanks
Namhyung


>  struct metric_event *metricgroup__lookup(struct rblist *metric_events,
>                                          struct evsel *evsel,
>                                          bool create)
> @@ -475,12 +486,9 @@ void metricgroup__print(bool metrics, bool metricgroups, char *filter,
>         groups.node_new = mep_new;
>         groups.node_cmp = mep_cmp;
>         groups.node_delete = mep_delete;
> -       for (i = 0; ; i++) {
> +       map_for_each_event(pe, i, map) {
>                 const char *g;
> -               pe = &map->table[i];
>
> -               if (!pe->name && !pe->metric_group && !pe->metric_name)
> -                       break;
>                 if (!pe->metric_expr)
>                         continue;
>                 g = pe->metric_group;
> @@ -745,17 +753,6 @@ static int __add_metric(struct list_head *metric_list,
>         return 0;
>  }
>
> -#define map_for_each_event(__pe, __idx, __map)                         \
> -       for (__idx = 0, __pe = &__map->table[__idx];                    \
> -            __pe->name || __pe->metric_group || __pe->metric_name;     \
> -            __pe = &__map->table[++__idx])
> -
> -#define map_for_each_metric(__pe, __idx, __map, __metric)              \
> -       map_for_each_event(__pe, __idx, __map)                          \
> -               if (__pe->metric_expr &&                                \
> -                   (match_metric(__pe->metric_group, __metric) ||      \
> -                    match_metric(__pe->metric_name, __metric)))
> -
>  static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map)
>  {
>         struct pmu_event *pe;
> @@ -1092,11 +1089,7 @@ bool metricgroup__has_metric(const char *metric)
>         if (!map)
>                 return false;
>
> -       for (i = 0; ; i++) {
> -               pe = &map->table[i];
> -
> -               if (!pe->name && !pe->metric_group && !pe->metric_name)
> -                       break;
> +       map_for_each_event(pe, i, map) {
>                 if (!pe->metric_expr)
>                         continue;
>                 if (match_metric(pe->metric_name, metric))
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf metric: Code cleanup with map_for_each_event()
  2020-09-17  5:19 ` Namhyung Kim
@ 2020-09-17 11:53   ` liwei (GF)
  0 siblings, 0 replies; 3+ messages in thread
From: liwei (GF) @ 2020-09-17 11:53 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Ian Rogers, Kajol Jain, Kan Liang, linux-kernel,
	Li Bin, guohanjun

Hi Namhyung,

On 2020/9/17 13:19, Namhyung Kim wrote:
> Hello,
> 
> On Thu, Sep 17, 2020 at 11:45 AM Wei Li <liwei391@huawei.com> wrote:
>>
>> Since we have introduced map_for_each_event() to walk the 'pmu_events_map',
>> clean up metricgroup__print() and metricgroup__has_metric() with it.
>>
>> Signed-off-by: Wei Li <liwei391@huawei.com>
> 
> Acked-by: Namhyung Kim <namhyung@kernel.org>
> 
> A nit-pick below:
> 
> 
>> ---
>>  tools/perf/util/metricgroup.c | 33 +++++++++++++--------------------
>>  1 file changed, 13 insertions(+), 20 deletions(-)
>>
>> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
>> index 8831b964288f..3734cbb2c456 100644
>> --- a/tools/perf/util/metricgroup.c
>> +++ b/tools/perf/util/metricgroup.c
>> @@ -26,6 +26,17 @@
>>  #include "util.h"
>>  #include <asm/bug.h>
>>
>> +#define map_for_each_event(__pe, __idx, __map)                         \
>> +       for (__idx = 0, __pe = &__map->table[__idx];                    \
>> +            __pe->name || __pe->metric_group || __pe->metric_name;     \
>> +            __pe = &__map->table[++__idx])
>> +
>> +#define map_for_each_metric(__pe, __idx, __map, __metric)              \
>> +       map_for_each_event(__pe, __idx, __map)                          \
>> +               if (__pe->metric_expr &&                                \
>> +                   (match_metric(__pe->metric_group, __metric) ||      \
>> +                    match_metric(__pe->metric_name, __metric)))
>> +
> 
> You may consider adding a declaration of match_metric() here.
> Right now, all users are below the function so it's ok but
> having the macro here can enable future addition above IMHO.
> 

Thanks for spotting that.
I'd like to move them just below match_metric() for now, when we really need
to use them somewhere else, we may move them to header file then.

Thanks,
Wei

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-17 11:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17  2:44 [PATCH] perf metric: Code cleanup with map_for_each_event() Wei Li
2020-09-17  5:19 ` Namhyung Kim
2020-09-17 11:53   ` liwei (GF)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).