blob: e78b897677bd44136f88ccb072ae6de6f03d251c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsa6c872902015-10-25 15:51:24 +01002#include "tests.h"
Arnaldo Carvalho de Melo3ac55b12016-07-07 11:32:29 -03003#include <stdio.h>
Jiri Olsa6c872902015-10-25 15:51:24 +01004#include "cpumap.h"
Arnaldo Carvalho de Melo3ac55b12016-07-07 11:32:29 -03005#include "event.h"
6#include <string.h>
7#include <linux/bitops.h>
8#include "debug.h"
9
10struct machine;
Jiri Olsa6c872902015-10-25 15:51:24 +010011
12static int process_event_mask(struct perf_tool *tool __maybe_unused,
13 union perf_event *event,
14 struct perf_sample *sample __maybe_unused,
15 struct machine *machine __maybe_unused)
16{
Jiri Olsaf77b57a2015-10-25 15:51:25 +010017 struct cpu_map_event *map_event = &event->cpu_map;
Jiri Olsa6c872902015-10-25 15:51:24 +010018 struct cpu_map_mask *mask;
19 struct cpu_map_data *data;
Jiri Olsaf77b57a2015-10-25 15:51:25 +010020 struct cpu_map *map;
Jiri Olsa6c872902015-10-25 15:51:24 +010021 int i;
22
Jiri Olsaf77b57a2015-10-25 15:51:25 +010023 data = &map_event->data;
Jiri Olsa6c872902015-10-25 15:51:24 +010024
25 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
26
27 mask = (struct cpu_map_mask *)data->data;
28
29 TEST_ASSERT_VAL("wrong nr", mask->nr == 1);
30
31 for (i = 0; i < 20; i++) {
32 TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask));
33 }
34
Jiri Olsaf77b57a2015-10-25 15:51:25 +010035 map = cpu_map__new_data(data);
36 TEST_ASSERT_VAL("wrong nr", map->nr == 20);
37
38 for (i = 0; i < 20; i++) {
39 TEST_ASSERT_VAL("wrong cpu", map->map[i] == i);
40 }
41
42 cpu_map__put(map);
Jiri Olsa6c872902015-10-25 15:51:24 +010043 return 0;
44}
45
46static int process_event_cpus(struct perf_tool *tool __maybe_unused,
47 union perf_event *event,
48 struct perf_sample *sample __maybe_unused,
49 struct machine *machine __maybe_unused)
50{
Jiri Olsaf77b57a2015-10-25 15:51:25 +010051 struct cpu_map_event *map_event = &event->cpu_map;
Jiri Olsa6c872902015-10-25 15:51:24 +010052 struct cpu_map_entries *cpus;
53 struct cpu_map_data *data;
Jiri Olsaf77b57a2015-10-25 15:51:25 +010054 struct cpu_map *map;
Jiri Olsa6c872902015-10-25 15:51:24 +010055
Jiri Olsaf77b57a2015-10-25 15:51:25 +010056 data = &map_event->data;
Jiri Olsa6c872902015-10-25 15:51:24 +010057
58 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
59
60 cpus = (struct cpu_map_entries *)data->data;
61
62 TEST_ASSERT_VAL("wrong nr", cpus->nr == 2);
63 TEST_ASSERT_VAL("wrong cpu", cpus->cpu[0] == 1);
64 TEST_ASSERT_VAL("wrong cpu", cpus->cpu[1] == 256);
Jiri Olsaf77b57a2015-10-25 15:51:25 +010065
66 map = cpu_map__new_data(data);
67 TEST_ASSERT_VAL("wrong nr", map->nr == 2);
68 TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
69 TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256);
Elena Reshetovaec09a422017-02-21 17:34:56 +020070 TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
Jiri Olsaf77b57a2015-10-25 15:51:25 +010071 cpu_map__put(map);
Jiri Olsa6c872902015-10-25 15:51:24 +010072 return 0;
73}
74
75
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030076int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsa6c872902015-10-25 15:51:24 +010077{
78 struct cpu_map *cpus;
79
80 /* This one is better stores in mask. */
81 cpus = cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19");
82
83 TEST_ASSERT_VAL("failed to synthesize map",
84 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
85
86 cpu_map__put(cpus);
87
88 /* This one is better stores in cpu values. */
89 cpus = cpu_map__new("1,256");
90
91 TEST_ASSERT_VAL("failed to synthesize map",
92 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
93
94 cpu_map__put(cpus);
95 return 0;
96}
Jiri Olsaa24020e2016-06-28 13:29:04 +020097
98static int cpu_map_print(const char *str)
99{
100 struct cpu_map *map = cpu_map__new(str);
101 char buf[100];
102
103 if (!map)
104 return -1;
105
106 cpu_map__snprint(map, buf, sizeof(buf));
107 return !strcmp(buf, str);
108}
109
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300110int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsaa24020e2016-06-28 13:29:04 +0200111{
112 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
113 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
114 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
115 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
116 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
117 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
118 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
119 return 0;
120}