[PATCH 16/47] perf tools: Add cpu_map event synthesize function

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

Introduce perf_event__synthesize_cpu_map function to
sythesize struct cpu_map.

Link: http://lkml.kernel.org/n/tip-miidn8vqsx3udu4ct8103v5f@git.kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 4 ++++
tools/perf/tests/cpumap.c | 29 +++++++++++++++++++++++++++++
tools/perf/tests/tests.h | 1 +
tools/perf/util/event.c | 28 ++++++++++++++++++++++++++++
tools/perf/util/event.h | 5 +++++
6 files changed, 68 insertions(+)
create mode 100644 tools/perf/tests/cpumap.c

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index d20d6e6ab65b..05faf02ed1ee 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -32,6 +32,7 @@ perf-y += sample-parsing.o
perf-y += parse-no-sample-id-all.o
perf-y += kmod-path.o
perf-y += thread-map.o
+perf-y += cpumap.o

perf-$(CONFIG_X86) += perf-time-to-tsc.o

diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 7b289d20bd8b..d4cfc0592f6b 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -179,6 +179,10 @@ static struct test {
.func = test__thread_map_synthesize,
},
{
+ .desc = "Test cpu map synthesize",
+ .func = test__cpu_map_synthesize,
+ },
+ {
.func = NULL,
},
};
diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c
new file mode 100644
index 000000000000..475c040f8a8d
--- /dev/null
+++ b/tools/perf/tests/cpumap.c
@@ -0,0 +1,29 @@
+#include "tests.h"
+#include "cpumap.h"
+
+static int process_event(struct perf_tool *tool __maybe_unused,
+ union perf_event *event,
+ struct perf_sample *sample __maybe_unused,
+ struct machine *machine __maybe_unused)
+{
+ struct cpu_map_event *map = &event->cpu_map;
+
+ 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);
+ return 0;
+}
+
+int test__cpu_map_synthesize(void)
+{
+ struct cpu_map *cpus;
+
+ cpus = cpu_map__new("1,2,4");
+
+
+ TEST_ASSERT_VAL("failed to synthesize map",
+ !perf_event__synthesize_cpu_map(NULL, cpus, process_event, NULL));
+
+ return 0;
+}
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 14e2d821d0fd..c251d5a21e0c 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -63,6 +63,7 @@ int test__fdarray__add(void);
int test__kmod_path__parse(void);
int test__thread_map(void);
int test__thread_map_synthesize(void);
+int test__cpu_map_synthesize(void);

#if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__)
#ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index 9f8ffbfc5cd4..7bbe64c51d85 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -735,6 +735,34 @@ int perf_event__synthesize_thread_map2(struct perf_tool *tool,
return err;
}

+int perf_event__synthesize_cpu_map(struct perf_tool *tool,
+ struct cpu_map *cpus,
+ perf_event__handler_t process,
+ struct machine *machine)
+{
+ union perf_event *event;
+ int i, err, size;
+
+ size = sizeof(event->cpu_map);
+ size += cpus->nr * sizeof(event->cpu_map.cpu[0]);
+
+ event = zalloc(size);
+ if (!event)
+ return -ENOMEM;
+
+ event->header.type = PERF_RECORD_CPU_MAP;
+ event->header.size = size;
+ event->cpu_map.nr = cpus->nr;
+
+ for (i = 0; i < cpus->nr; i++)
+ event->cpu_map.cpu[i] = cpus->map[i];
+
+ err = process(tool, event, NULL, machine);
+
+ free(event);
+ return err;
+}
+
size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
{
const char *s;
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index f05c4d868162..6600da608782 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -396,6 +396,7 @@ void perf_event__print_totals(void);

struct perf_tool;
struct thread_map;
+struct cpu_map;

typedef int (*perf_event__handler_t)(struct perf_tool *tool,
union perf_event *event,
@@ -411,6 +412,10 @@ int perf_event__synthesize_thread_map2(struct perf_tool *tool,
struct thread_map *threads,
perf_event__handler_t process,
struct machine *machine);
+int perf_event__synthesize_cpu_map(struct perf_tool *tool,
+ struct cpu_map *cpus,
+ perf_event__handler_t process,
+ struct machine *machine);
int perf_event__synthesize_threads(struct perf_tool *tool,
perf_event__handler_t process,
struct machine *machine, bool mmap_data,
--
2.4.3