Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1554866

[PATCH 01/11] perf, tools: Factor out scale conversion code

From Andi Kleen <andi@firstfloor.org>
Newsgroups linux.kernel
Subject [PATCH 01/11] perf, tools: Factor out scale conversion code
Date 2017-01-10 02:40 +0100
Message-ID <sXTzQ-39s-33@gated-at.bofh.it> (permalink)
References <sXTzQ-39s-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Andi Kleen <ak@linux.intel.com>

Move the scale factor parsing code to an own function
to reuse it in an upcoming patch.

v2: Return error in case strdup returns NULL.
v3: Use ENOMEM, not -1.
Acked-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
 tools/perf/util/pmu.c | 62 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index dc6ccaa4e927..78b16100567d 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -94,32 +94,10 @@ static int pmu_format(const char *name, struct list_head *format)
 	return 0;
 }
 
-static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
+static int convert_scale(const char *scale, char **end, double *sval)
 {
-	struct stat st;
-	ssize_t sret;
-	char scale[128];
-	int fd, ret = -1;
-	char path[PATH_MAX];
 	char *lc;
-
-	snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
-
-	fd = open(path, O_RDONLY);
-	if (fd == -1)
-		return -1;
-
-	if (fstat(fd, &st) < 0)
-		goto error;
-
-	sret = read(fd, scale, sizeof(scale)-1);
-	if (sret < 0)
-		goto error;
-
-	if (scale[sret - 1] == '\n')
-		scale[sret - 1] = '\0';
-	else
-		scale[sret] = '\0';
+	int ret = 0;
 
 	/*
 	 * save current locale
@@ -134,7 +112,7 @@ static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *
 	lc = strdup(lc);
 	if (!lc) {
 		ret = -ENOMEM;
-		goto error;
+		goto out;
 	}
 
 	/*
@@ -144,14 +122,42 @@ static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *
 	 */
 	setlocale(LC_NUMERIC, "C");
 
-	alias->scale = strtod(scale, NULL);
+	*sval = strtod(scale, end);
 
+out:
 	/* restore locale */
 	setlocale(LC_NUMERIC, lc);
-
 	free(lc);
+	return ret;
+}
+
+static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
+{
+	struct stat st;
+	ssize_t sret;
+	char scale[128];
+	int fd, ret = -1;
+	char path[PATH_MAX];
+
+	snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
+
+	fd = open(path, O_RDONLY);
+	if (fd == -1)
+		return -1;
+
+	if (fstat(fd, &st) < 0)
+		goto error;
+
+	sret = read(fd, scale, sizeof(scale)-1);
+	if (sret < 0)
+		goto error;
+
+	if (scale[sret - 1] == '\n')
+		scale[sret - 1] = '\0';
+	else
+		scale[sret] = '\0';
 
-	ret = 0;
+	ret = convert_scale(scale, NULL, &alias->scale);
 error:
 	close(fd);
 	return ret;
-- 
2.9.3

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Support Intel uncore event lists v4 Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 10/11] perf, tools: Support MetricExpr header in JSON event list Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 07/11] perf, tools: Collapse identically named events in perf stat Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 01/11] perf, tools: Factor out scale conversion code Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 09/11] perf, tools: Add a simple expression parser for JSON Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 02/11] perf, tools: Parse eventcode as number in jevents Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 05/11] perf, tools: Support event aliases for non cpu// pmus Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 03/11] perf, tools: Add support for parsing uncore json files Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 08/11] perf, tools: Expand PMU events by prefix match Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  [PATCH 11/11] perf, tools, stat: Output JSON MetricExpr metric Andi Kleen <andi@firstfloor.org> - 2017-01-10 02:40 +0100
  Re: Support Intel uncore event lists v4 Jiri Olsa <jolsa@redhat.com> - 2017-01-11 20:00 +0100
    Re: Support Intel uncore event lists v4 Andi Kleen <andi@firstfloor.org> - 2017-01-11 21:00 +0100
      Re: Support Intel uncore event lists v4 Jiri Olsa <jolsa@redhat.com> - 2017-01-11 22:50 +0100
        Re: Support Intel uncore event lists v4 Andi Kleen <andi@firstfloor.org> - 2017-01-11 23:00 +0100
          Re: Support Intel uncore event lists v4 Jiri Olsa <jolsa@redhat.com> - 2017-01-12 13:20 +0100
            Re: Support Intel uncore event lists v4 Andi Kleen <andi@firstfloor.org> - 2017-01-12 18:20 +0100

csiph-web