perf tools: Change cpu_map__fprintf output
authorJiri Olsa <jolsa@kernel.org>
Tue, 28 Jun 2016 11:29:04 +0000 (13:29 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 30 Jun 2016 21:27:45 +0000 (18:27 -0300)
Display cpu map in standard list form.  (perf report -D output on perf stat data).

before:
  0x590 [0x18]: PERF_RECORD_CPU_MAP nr: 4 cpus: 0, 1, 2, 3

after:
  0x590 [0x18]: PERF_RECORD_CPU_MAP: 0-3

Adding automated testcase.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1467113345-12669-4-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/builtin-test.c
tools/perf/tests/cpumap.c
tools/perf/tests/tests.h
tools/perf/util/cpumap.c
tools/perf/util/cpumap.h
tools/perf/util/event.c

index 5781c1640eae03e926f94d24cf8d1074a8425160..07c14e9f654664ca488a00bcc13a5714b3fa20b8 100644 (file)
@@ -213,6 +213,10 @@ static struct test generic_tests[] = {
                .desc = "Test backward reading from ring buffer",
                .func = test__backward_ring_buffer,
        },
+       {
+               .desc = "Test cpu map print",
+               .func = test__cpu_map_print,
+       },
        {
                .func = NULL,
        },
index 4cb6418a8ffc33d6c4b71c66c112420191c68956..c9ec5f83e42c98258b12beda4362210f794aa1a6 100644 (file)
@@ -86,3 +86,27 @@ int test__cpu_map_synthesize(int subtest __maybe_unused)
        cpu_map__put(cpus);
        return 0;
 }
+
+static int cpu_map_print(const char *str)
+{
+       struct cpu_map *map = cpu_map__new(str);
+       char buf[100];
+
+       if (!map)
+               return -1;
+
+       cpu_map__snprint(map, buf, sizeof(buf));
+       return !strcmp(buf, str);
+}
+
+int test__cpu_map_print(int subtest __maybe_unused)
+{
+       TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
+       TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
+       TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
+       TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
+       TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
+       TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
+       TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
+       return 0;
+}
index c57e72c826d23588279963553d619565f97bc8be..52f969570c97c3dd0afe58fa6197e48197ab3782 100644 (file)
@@ -87,6 +87,7 @@ int test__synthesize_stat_round(int subtest);
 int test__event_update(int subtest);
 int test__event_times(int subtest);
 int test__backward_ring_buffer(int subtest);
+int test__cpu_map_print(int subtest);
 
 #if defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
index 02d801670f30053fa1f6344f7a944bc8e2047842..15f83acac1b85d777ca17c9588526fe31f86343a 100644 (file)
@@ -236,13 +236,12 @@ struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
 
 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
 {
-       int i;
-       size_t printed = fprintf(fp, "%d cpu%s: ",
-                                map->nr, map->nr > 1 ? "s" : "");
-       for (i = 0; i < map->nr; ++i)
-               printed += fprintf(fp, "%s%d", i ? ", " : "", map->map[i]);
+#define BUFSIZE 1024
+       char buf[BUFSIZE];
 
-       return printed + fprintf(fp, "\n");
+       cpu_map__snprint(map, buf, sizeof(buf));
+       return fprintf(fp, "%s\n", buf);
+#undef BUFSIZE
 }
 
 struct cpu_map *cpu_map__dummy_new(void)
@@ -599,3 +598,46 @@ bool cpu_map__has(struct cpu_map *cpus, int cpu)
 
        return false;
 }
+
+size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size)
+{
+       int i, cpu, start = -1;
+       bool first = true;
+       size_t ret = 0;
+
+#define COMMA first ? "" : ","
+
+       for (i = 0; i < map->nr + 1; i++) {
+               bool last = i == map->nr;
+
+               cpu = last ? INT_MAX : map->map[i];
+
+               if (start == -1) {
+                       start = i;
+                       if (last) {
+                               ret += snprintf(buf + ret, size - ret,
+                                               "%s%d", COMMA,
+                                               map->map[i]);
+                       }
+               } else if (((i - start) != (cpu - map->map[start])) || last) {
+                       int end = i - 1;
+
+                       if (start == end) {
+                               ret += snprintf(buf + ret, size - ret,
+                                               "%s%d", COMMA,
+                                               map->map[start]);
+                       } else {
+                               ret += snprintf(buf + ret, size - ret,
+                                               "%s%d-%d", COMMA,
+                                               map->map[start], map->map[end]);
+                       }
+                       first = false;
+                       start = i;
+               }
+       }
+
+#undef COMMA
+
+       pr_debug("cpumask list: %s\n", buf);
+       return ret;
+}
index 1a0a35073ce1e8b52ce7a4bc07efdcadefbb45a3..206dc550354a65fb0f4ab81d05dd5cfbe910d1b6 100644 (file)
@@ -19,6 +19,7 @@ struct cpu_map *cpu_map__empty_new(int nr);
 struct cpu_map *cpu_map__dummy_new(void);
 struct cpu_map *cpu_map__new_data(struct cpu_map_data *data);
 struct cpu_map *cpu_map__read(FILE *file);
+size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size);
 size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp);
 int cpu_map__get_socket_id(int cpu);
 int cpu_map__get_socket(struct cpu_map *map, int idx, void *data);
index 9b141f12329edc750de5eefd47c3c7f2cb46e503..e20438b784bed4019e50080e54fd06fad2c0776e 100644 (file)
@@ -1092,7 +1092,7 @@ size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
        struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
        size_t ret;
 
-       ret = fprintf(fp, " nr: ");
+       ret = fprintf(fp, ": ");
 
        if (cpus)
                ret += cpu_map__fprintf(cpus, fp);