public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: lkml <linux-kernel@vger.kernel.org>,
	Ingo Molnar <mingo@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Michael Petlan <mpetlan@redhat.com>,
	Joe Mario <jmario@redhat.com>, Andi Kleen <ak@linux.intel.com>,
	Kajol Jain <kjain@linux.ibm.com>
Subject: [PATCH 1/3] perf tools: Factor metric addition into add_metric function
Date: Wed, 11 Dec 2019 23:47:58 +0100	[thread overview]
Message-ID: <20191211224800.9066-2-jolsa@kernel.org> (raw)
In-Reply-To: <20191211224800.9066-1-jolsa@kernel.org>

Factoring metric addition into add_metric function,
so it can be used to add metric from different sources
in following patches.

Link: https://lkml.kernel.org/n/tip-qw1727kbewu315mz2h0y5xfm@git.kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/perf/util/metricgroup.c | 110 +++++++++++++++++++---------------
 1 file changed, 62 insertions(+), 48 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 6a4d350d5cdb..1d01958c148d 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -396,13 +396,65 @@ void metricgroup__print(bool metrics, bool metricgroups, char *filter,
 	strlist__delete(metriclist);
 }
 
+static int add_metric(const char *name, const char *expr, const char *unit,
+		      struct strbuf *events, struct list_head *group_list)
+{
+	bool no_group = false;
+	struct egroup *eg;
+	const char **ids;
+	int idnum, j;
+
+	pr_debug("metric expr %s for %s\n", expr, name);
+
+	if (expr__find_other(expr, NULL, &ids, &idnum) < 0)
+		return -1;
+
+	if (events->len > 0)
+		strbuf_addf(events, ",");
+
+	for (j = 0; j < idnum; j++) {
+		pr_debug("found event %s\n", ids[j]);
+		/*
+		 * Duration time maps to a software event and can make
+		 * groups not count. Always use it outside a
+		 * group.
+		 */
+		if (!strcmp(ids[j], "duration_time")) {
+			if (j > 0)
+				strbuf_addf(events, "}:W,");
+			strbuf_addf(events, "duration_time");
+			no_group = true;
+			continue;
+		}
+		strbuf_addf(events, "%s%s",
+			j == 0 || no_group ? "{" : ",",
+			ids[j]);
+		no_group = false;
+	}
+
+	if (!no_group)
+		strbuf_addf(events, "}:W");
+
+	eg = malloc(sizeof(struct egroup));
+	if (!eg)
+		return -ENOMEM;
+
+	eg->ids         = ids;
+	eg->idnum       = idnum;
+	eg->metric_name = name;
+	eg->metric_expr = expr;
+	eg->metric_unit = unit;
+	list_add_tail(&eg->nd, group_list);
+	return 0;
+}
+
 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
 				   struct list_head *group_list)
 {
 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
 	struct pmu_event *pe;
 	int ret = -EINVAL;
-	int i, j;
+	int i;
 
 	if (!map)
 		return 0;
@@ -414,55 +466,17 @@ static int metricgroup__add_metric(const char *metric, struct strbuf *events,
 			break;
 		if (!pe->metric_expr)
 			continue;
-		if (match_metric(pe->metric_group, metric) ||
-		    match_metric(pe->metric_name, metric)) {
-			const char **ids;
-			int idnum;
-			struct egroup *eg;
-			bool no_group = false;
 
-			pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
-
-			if (expr__find_other(pe->metric_expr,
-					     NULL, &ids, &idnum) < 0)
-				continue;
-			if (events->len > 0)
-				strbuf_addf(events, ",");
-			for (j = 0; j < idnum; j++) {
-				pr_debug("found event %s\n", ids[j]);
-				/*
-				 * Duration time maps to a software event and can make
-				 * groups not count. Always use it outside a
-				 * group.
-				 */
-				if (!strcmp(ids[j], "duration_time")) {
-					if (j > 0)
-						strbuf_addf(events, "}:W,");
-					strbuf_addf(events, "duration_time");
-					no_group = true;
-					continue;
-				}
-				strbuf_addf(events, "%s%s",
-					j == 0 || no_group ? "{" : ",",
-					ids[j]);
-				no_group = false;
-			}
-			if (!no_group)
-				strbuf_addf(events, "}:W");
+		if (!match_metric(pe->metric_group, metric) &&
+		    !match_metric(pe->metric_name, metric))
+			continue;
 
-			eg = malloc(sizeof(struct egroup));
-			if (!eg) {
-				ret = -ENOMEM;
-				break;
-			}
-			eg->ids = ids;
-			eg->idnum = idnum;
-			eg->metric_name = pe->metric_name;
-			eg->metric_expr = pe->metric_expr;
-			eg->metric_unit = pe->unit;
-			list_add_tail(&eg->nd, group_list);
-			ret = 0;
-		}
+		ret = add_metric(pe->metric_name,
+				 pe->metric_expr,
+				 pe->unit,
+				 events, group_list);
+		if (ret)
+			break;
 	}
 	return ret;
 }
-- 
2.21.1


  reply	other threads:[~2019-12-11 22:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-11 22:47 [RFC 0/3] perf tools: Add support for used defined metric Jiri Olsa
2019-12-11 22:47 ` Jiri Olsa [this message]
2019-12-11 22:47 ` [PATCH 2/3] perf tools: Factor metric setup code into metricgroup__setup function Jiri Olsa
2019-12-11 22:48 ` [PATCH 3/3] perf stat: Add --metric option Jiri Olsa
2019-12-11 23:02   ` Andi Kleen
2019-12-12  9:41     ` Jiri Olsa
2019-12-11 23:01 ` [RFC 0/3] perf tools: Add support for used defined metric Andi Kleen
2019-12-12  9:37   ` Jiri Olsa

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=20191211224800.9066-2-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jmario@redhat.com \
    --cc=kjain@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=mpetlan@redhat.com \
    --cc=namhyung@kernel.org \
    /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