[PATCH 08/57] perf tools: Add cpu_map__new_event function

From: Jiri Olsa
Date: Fri Oct 16 2015 - 06:42:06 EST


Introducing cpu_map__new_event function to create
struct cpu_map object from cpu_map event.

Link: http://lkml.kernel.org/n/tip-tepv49tbjsqeprmf8k05lc6r@xxxxxxxxxxxxxx
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/tests/cpumap.c | 28 ++++++++++++++++++++++++----
tools/perf/util/cpumap.c | 42 ++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/cpumap.h | 1 +
3 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c
index cebb251402fa..7576e3c65e66 100644
--- a/tools/perf/tests/cpumap.c
+++ b/tools/perf/tests/cpumap.c
@@ -6,11 +6,12 @@ static int process_event_mask(struct perf_tool *tool __maybe_unused,
struct perf_sample *sample __maybe_unused,
struct machine *machine __maybe_unused)
{
- struct cpu_map_event *map = &event->cpu_map;
+ struct cpu_map_event *map_event = &event->cpu_map;
struct cpu_map_data_mask *mask;
struct cpu_map_data *data;
+ struct cpu_map *map;

- data = &map->data;
+ data = &map_event->data;

TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);

@@ -23,6 +24,17 @@ static int process_event_mask(struct perf_tool *tool __maybe_unused,
TEST_ASSERT_VAL("wrong cpu", test_bit(4, mask->mask));
TEST_ASSERT_VAL("wrong cpu", test_bit(5, mask->mask));
TEST_ASSERT_VAL("wrong cpu", test_bit(6, mask->mask));
+
+ map = cpu_map__new_data(data);
+ TEST_ASSERT_VAL("wrong nr", map->nr == 6);
+ TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
+ TEST_ASSERT_VAL("wrong cpu", map->map[1] == 2);
+ TEST_ASSERT_VAL("wrong cpu", map->map[2] == 3);
+ TEST_ASSERT_VAL("wrong cpu", map->map[3] == 4);
+ TEST_ASSERT_VAL("wrong cpu", map->map[4] == 5);
+ TEST_ASSERT_VAL("wrong cpu", map->map[5] == 6);
+ TEST_ASSERT_VAL("wrong refcnt", atomic_read(&map->refcnt) == 1);
+ cpu_map__put(map);
return 0;
}

@@ -31,11 +43,12 @@ static int process_event_cpus(struct perf_tool *tool __maybe_unused,
struct perf_sample *sample __maybe_unused,
struct machine *machine __maybe_unused)
{
- struct cpu_map_event *map = &event->cpu_map;
+ struct cpu_map_event *map_event = &event->cpu_map;
struct cpu_map_data_cpus *cpus;
struct cpu_map_data *data;
+ struct cpu_map *map;

- data = &map->data;
+ data = &map_event->data;

TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);

@@ -44,6 +57,13 @@ static int process_event_cpus(struct perf_tool *tool __maybe_unused,
TEST_ASSERT_VAL("wrong nr", cpus->nr == 2);
TEST_ASSERT_VAL("wrong cpu", cpus->cpu[0] == 1);
TEST_ASSERT_VAL("wrong cpu", cpus->cpu[1] == 256);
+
+ map = cpu_map__new_data(data);
+ TEST_ASSERT_VAL("wrong nr", map->nr == 2);
+ TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
+ TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256);
+ TEST_ASSERT_VAL("wrong refcnt", atomic_read(&map->refcnt) == 1);
+ cpu_map__put(map);
return 0;
}

diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
index 94d30c8d7255..511f028ee7a6 100644
--- a/tools/perf/util/cpumap.c
+++ b/tools/perf/util/cpumap.c
@@ -5,6 +5,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
+#include <linux/bitmap.h>
#include "asm/bug.h"

static struct cpu_map *cpu_map__default_new(void)
@@ -179,6 +180,47 @@ out:
return cpus;
}

+static struct cpu_map *cpu_map__from_cpus(struct cpu_map_data_cpus *cpus)
+{
+ struct cpu_map *map;
+
+ map = cpu_map__empty_new(cpus->nr);
+ if (map) {
+ unsigned i;
+
+ for (i = 0; i < cpus->nr; i++)
+ map->map[i] = (int) cpus->cpu[i];
+ }
+
+ return map;
+}
+
+static struct cpu_map *cpu_map__from_mask(struct cpu_map_data_mask *mask)
+{
+ struct cpu_map *map;
+ int nr, nbits = mask->nr * mask->long_size * BITS_PER_BYTE;
+
+ nr = bitmap_weight(mask->mask, nbits);
+
+ map = cpu_map__empty_new(nr);
+ if (map) {
+ int cpu, i = 0;
+
+ for_each_set_bit(cpu, mask->mask, nbits)
+ map->map[i++] = cpu;
+ }
+ return map;
+
+}
+
+struct cpu_map *cpu_map__new_data(struct cpu_map_data *data)
+{
+ if (data->type == PERF_CPU_MAP__CPUS)
+ return cpu_map__from_cpus((struct cpu_map_data_cpus *) data->data);
+ else
+ return cpu_map__from_mask((struct cpu_map_data_mask *) data->data);
+}
+
size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
{
int i;
diff --git a/tools/perf/util/cpumap.h b/tools/perf/util/cpumap.h
index 91fd5d1a4cdc..43a64a3fecdf 100644
--- a/tools/perf/util/cpumap.h
+++ b/tools/perf/util/cpumap.h
@@ -17,6 +17,7 @@ struct cpu_map {
struct cpu_map *cpu_map__new(const char *cpu_list);
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__fprintf(struct cpu_map *map, FILE *fp);
int cpu_map__get_socket_id(int cpu);
--
2.4.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/