[PATCH 17/47] perf tools: Add cpu_map__new_event function

Jiri Olsa
2015-07-21T12:32:45Z

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

Link: http://lkml.kernel.org/n/tip-tepv49tbjsqeprmf8k05lc6r@git.kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
tools/perf/tests/cpumap.c | 10 ++++++++++
tools/perf/util/cpumap.c | 27 +++++++++++++++++++++++++++
tools/perf/util/cpumap.h | 3 +++
3 files changed, 40 insertions(+)

diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c
index 475c040f8a8d..af8a3f520a2a 100644
--- a/tools/perf/tests/cpumap.c
+++ b/tools/perf/tests/cpumap.c
@@ -7,11 +7,21 @@ static int process_event(struct perf_tool *tool __maybe_unused,
struct machine *machine __maybe_unused)
{
struct cpu_map_event *map = &event->cpu_map;
+ struct cpu_map *cpus;

TEST_ASSERT_VAL("wrong nr", map->nr == 3);
TEST_ASSERT_VAL("wrong cpu", map->cpu[0] == 1);
TEST_ASSERT_VAL("wrong cpu", map->cpu[1] == 2);
TEST_ASSERT_VAL("wrong cpu", map->cpu[2] == 4);
+
+ cpus = cpu_map__new_event(&event->cpu_map);
+ TEST_ASSERT_VAL("wrong nr", cpus->nr == 3);
+ TEST_ASSERT_VAL("wrong cpu", cpus->map[0] == 1);
+ TEST_ASSERT_VAL("wrong cpu", cpus->map[1] == 2);
+ TEST_ASSERT_VAL("wrong cpu", cpus->map[2] == 4);
+ TEST_ASSERT_VAL("wrong refcnt",
+ atomic_read(&cpus->refcnt) == 1);
+ cpu_map__put(cpus);
return 0;
}

diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
index 3667e2123e5b..81e82cadda50 100644
--- a/tools/perf/util/cpumap.c
+++ b/tools/perf/util/cpumap.c
@@ -179,6 +179,33 @@ out:
return cpus;
}

+static void cpu_map__copy_event(struct cpu_map *cpus,
+ struct cpu_map_event *event)
+{
+ unsigned i;
+
+ cpus->nr = event->nr;
+
+ for (i = 0; i < event->nr; i++)
+ cpus->map[i] = (int) event->cpu[i];
+
+ atomic_set(&cpus->refcnt, 1);
+}
+
+struct cpu_map *cpu_map__new_event(struct cpu_map_event *event)
+{
+ struct cpu_map *cpus;
+ int size;
+
+ size = sizeof(cpus) + (event->nr * sizeof(cpus->map[0]));
+
+ cpus = zalloc(size);
+ if (cpus)
+ cpu_map__copy_event(cpus, event);
+
+ return cpus;
+}
+
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 0af9cecb4c51..44a68b63bf48 100644
--- a/tools/perf/util/cpumap.h
+++ b/tools/perf/util/cpumap.h
@@ -14,8 +14,11 @@ struct cpu_map {
int map[];
};

+struct cpu_map_event;
+
struct cpu_map *cpu_map__new(const char *cpu_list);
struct cpu_map *cpu_map__dummy_new(void);
+struct cpu_map *cpu_map__new_event(struct cpu_map_event *event);
struct cpu_map *cpu_map__read(FILE *file);
size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp);
int cpu_map__get_socket(struct cpu_map *map, int idx);
--
2.4.3