blob: 89a155092f853b3ca1a980d2e3010b38ffa1e5fa [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"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -03006#include "util/synthetic-events.h"
Arnaldo Carvalho de Melo3ac55b12016-07-07 11:32:29 -03007#include <string.h>
8#include <linux/bitops.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +02009#include <perf/cpumap.h>
Arnaldo Carvalho de Melo3ac55b12016-07-07 11:32:29 -030010#include "debug.h"
11
12struct machine;
Jiri Olsa6c872902015-10-25 15:51:24 +010013
14static int process_event_mask(struct perf_tool *tool __maybe_unused,
15 union perf_event *event,
16 struct perf_sample *sample __maybe_unused,
17 struct machine *machine __maybe_unused)
18{
Jiri Olsa72932372019-08-28 15:57:16 +020019 struct perf_record_cpu_map *map_event = &event->cpu_map;
20 struct perf_record_record_cpu_map *mask;
21 struct perf_record_cpu_map_data *data;
Jiri Olsaf8548392019-07-21 13:23:49 +020022 struct perf_cpu_map *map;
Jiri Olsa6c872902015-10-25 15:51:24 +010023 int i;
24
Jiri Olsaf77b57a2015-10-25 15:51:25 +010025 data = &map_event->data;
Jiri Olsa6c872902015-10-25 15:51:24 +010026
27 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
28
Jiri Olsa72932372019-08-28 15:57:16 +020029 mask = (struct perf_record_record_cpu_map *)data->data;
Jiri Olsa6c872902015-10-25 15:51:24 +010030
31 TEST_ASSERT_VAL("wrong nr", mask->nr == 1);
32
33 for (i = 0; i < 20; i++) {
34 TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask));
35 }
36
Jiri Olsaf77b57a2015-10-25 15:51:25 +010037 map = cpu_map__new_data(data);
38 TEST_ASSERT_VAL("wrong nr", map->nr == 20);
39
40 for (i = 0; i < 20; i++) {
41 TEST_ASSERT_VAL("wrong cpu", map->map[i] == i);
42 }
43
Jiri Olsa38f01d82019-07-21 13:24:17 +020044 perf_cpu_map__put(map);
Jiri Olsa6c872902015-10-25 15:51:24 +010045 return 0;
46}
47
48static int process_event_cpus(struct perf_tool *tool __maybe_unused,
49 union perf_event *event,
50 struct perf_sample *sample __maybe_unused,
51 struct machine *machine __maybe_unused)
52{
Jiri Olsa72932372019-08-28 15:57:16 +020053 struct perf_record_cpu_map *map_event = &event->cpu_map;
Jiri Olsa6c872902015-10-25 15:51:24 +010054 struct cpu_map_entries *cpus;
Jiri Olsa72932372019-08-28 15:57:16 +020055 struct perf_record_cpu_map_data *data;
Jiri Olsaf8548392019-07-21 13:23:49 +020056 struct perf_cpu_map *map;
Jiri Olsa6c872902015-10-25 15:51:24 +010057
Jiri Olsaf77b57a2015-10-25 15:51:25 +010058 data = &map_event->data;
Jiri Olsa6c872902015-10-25 15:51:24 +010059
60 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
61
62 cpus = (struct cpu_map_entries *)data->data;
63
64 TEST_ASSERT_VAL("wrong nr", cpus->nr == 2);
65 TEST_ASSERT_VAL("wrong cpu", cpus->cpu[0] == 1);
66 TEST_ASSERT_VAL("wrong cpu", cpus->cpu[1] == 256);
Jiri Olsaf77b57a2015-10-25 15:51:25 +010067
68 map = cpu_map__new_data(data);
69 TEST_ASSERT_VAL("wrong nr", map->nr == 2);
70 TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
71 TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256);
Elena Reshetovaec09a422017-02-21 17:34:56 +020072 TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
Jiri Olsa38f01d82019-07-21 13:24:17 +020073 perf_cpu_map__put(map);
Jiri Olsa6c872902015-10-25 15:51:24 +010074 return 0;
75}
76
77
Ian Rogers33f44bf2021-11-03 23:41:51 -070078static int test__cpu_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsa6c872902015-10-25 15:51:24 +010079{
Jiri Olsaf8548392019-07-21 13:23:49 +020080 struct perf_cpu_map *cpus;
Jiri Olsa6c872902015-10-25 15:51:24 +010081
82 /* This one is better stores in mask. */
Jiri Olsa9c3516d2019-07-21 13:24:30 +020083 cpus = perf_cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19");
Jiri Olsa6c872902015-10-25 15:51:24 +010084
85 TEST_ASSERT_VAL("failed to synthesize map",
86 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
87
Jiri Olsa38f01d82019-07-21 13:24:17 +020088 perf_cpu_map__put(cpus);
Jiri Olsa6c872902015-10-25 15:51:24 +010089
90 /* This one is better stores in cpu values. */
Jiri Olsa9c3516d2019-07-21 13:24:30 +020091 cpus = perf_cpu_map__new("1,256");
Jiri Olsa6c872902015-10-25 15:51:24 +010092
93 TEST_ASSERT_VAL("failed to synthesize map",
94 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
95
Jiri Olsa38f01d82019-07-21 13:24:17 +020096 perf_cpu_map__put(cpus);
Jiri Olsa6c872902015-10-25 15:51:24 +010097 return 0;
98}
Jiri Olsaa24020e2016-06-28 13:29:04 +020099
100static int cpu_map_print(const char *str)
101{
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200102 struct perf_cpu_map *map = perf_cpu_map__new(str);
Jiri Olsaa24020e2016-06-28 13:29:04 +0200103 char buf[100];
104
105 if (!map)
106 return -1;
107
108 cpu_map__snprint(map, buf, sizeof(buf));
Namhyung Kim690d91f2021-03-01 23:04:08 +0900109 perf_cpu_map__put(map);
110
Jiri Olsaa24020e2016-06-28 13:29:04 +0200111 return !strcmp(buf, str);
112}
113
Ian Rogers33f44bf2021-11-03 23:41:51 -0700114static int test__cpu_map_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsaa24020e2016-06-28 13:29:04 +0200115{
116 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
117 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
118 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
119 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
120 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
121 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
122 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
123 return 0;
124}
Andi Kleena2408a72019-11-20 16:15:14 -0800125
Ian Rogers33f44bf2021-11-03 23:41:51 -0700126static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
Andi Kleena2408a72019-11-20 16:15:14 -0800127{
128 struct perf_cpu_map *a = perf_cpu_map__new("4,2,1");
129 struct perf_cpu_map *b = perf_cpu_map__new("4,5,7");
130 struct perf_cpu_map *c = perf_cpu_map__merge(a, b);
131 char buf[100];
132
133 TEST_ASSERT_VAL("failed to merge map: bad nr", c->nr == 5);
134 cpu_map__snprint(c, buf, sizeof(buf));
135 TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,7"));
Andi Kleena2408a72019-11-20 16:15:14 -0800136 perf_cpu_map__put(b);
137 perf_cpu_map__put(c);
138 return 0;
139}
Ian Rogersd68f0362021-11-03 23:41:50 -0700140
141DEFINE_SUITE("Synthesize cpu map", cpu_map_synthesize);
142DEFINE_SUITE("Print cpu map", cpu_map_print);
143DEFINE_SUITE("Merge cpu map", cpu_map_merge);