Merge remote-tracking branch 'torvalds/master' into perf/core
[linux-2.6-block.git] / tools / perf / tests / cpumap.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
6c872901 2#include "tests.h"
3ac55b1d 3#include <stdio.h>
6c872901 4#include "cpumap.h"
3ac55b1d
ACM
5#include "event.h"
6#include <string.h>
7#include <linux/bitops.h>
9c3516d1 8#include <perf/cpumap.h>
3ac55b1d
ACM
9#include "debug.h"
10
11struct machine;
6c872901
JO
12
13static int process_event_mask(struct perf_tool *tool __maybe_unused,
14 union perf_event *event,
15 struct perf_sample *sample __maybe_unused,
16 struct machine *machine __maybe_unused)
17{
f77b57ad 18 struct cpu_map_event *map_event = &event->cpu_map;
6c872901
JO
19 struct cpu_map_mask *mask;
20 struct cpu_map_data *data;
f854839b 21 struct perf_cpu_map *map;
6c872901
JO
22 int i;
23
f77b57ad 24 data = &map_event->data;
6c872901
JO
25
26 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
27
28 mask = (struct cpu_map_mask *)data->data;
29
30 TEST_ASSERT_VAL("wrong nr", mask->nr == 1);
31
32 for (i = 0; i < 20; i++) {
33 TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask));
34 }
35
f77b57ad
JO
36 map = cpu_map__new_data(data);
37 TEST_ASSERT_VAL("wrong nr", map->nr == 20);
38
39 for (i = 0; i < 20; i++) {
40 TEST_ASSERT_VAL("wrong cpu", map->map[i] == i);
41 }
42
38f01d8d 43 perf_cpu_map__put(map);
6c872901
JO
44 return 0;
45}
46
47static int process_event_cpus(struct perf_tool *tool __maybe_unused,
48 union perf_event *event,
49 struct perf_sample *sample __maybe_unused,
50 struct machine *machine __maybe_unused)
51{
f77b57ad 52 struct cpu_map_event *map_event = &event->cpu_map;
6c872901
JO
53 struct cpu_map_entries *cpus;
54 struct cpu_map_data *data;
f854839b 55 struct perf_cpu_map *map;
6c872901 56
f77b57ad 57 data = &map_event->data;
6c872901
JO
58
59 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
60
61 cpus = (struct cpu_map_entries *)data->data;
62
63 TEST_ASSERT_VAL("wrong nr", cpus->nr == 2);
64 TEST_ASSERT_VAL("wrong cpu", cpus->cpu[0] == 1);
65 TEST_ASSERT_VAL("wrong cpu", cpus->cpu[1] == 256);
f77b57ad
JO
66
67 map = cpu_map__new_data(data);
68 TEST_ASSERT_VAL("wrong nr", map->nr == 2);
69 TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
70 TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256);
ec09a42a 71 TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
38f01d8d 72 perf_cpu_map__put(map);
6c872901
JO
73 return 0;
74}
75
76
81f17c90 77int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
6c872901 78{
f854839b 79 struct perf_cpu_map *cpus;
6c872901
JO
80
81 /* This one is better stores in mask. */
9c3516d1 82 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");
6c872901
JO
83
84 TEST_ASSERT_VAL("failed to synthesize map",
85 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
86
38f01d8d 87 perf_cpu_map__put(cpus);
6c872901
JO
88
89 /* This one is better stores in cpu values. */
9c3516d1 90 cpus = perf_cpu_map__new("1,256");
6c872901
JO
91
92 TEST_ASSERT_VAL("failed to synthesize map",
93 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
94
38f01d8d 95 perf_cpu_map__put(cpus);
6c872901
JO
96 return 0;
97}
a24020e6
JO
98
99static int cpu_map_print(const char *str)
100{
9c3516d1 101 struct perf_cpu_map *map = perf_cpu_map__new(str);
a24020e6
JO
102 char buf[100];
103
104 if (!map)
105 return -1;
106
107 cpu_map__snprint(map, buf, sizeof(buf));
108 return !strcmp(buf, str);
109}
110
81f17c90 111int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused)
a24020e6
JO
112{
113 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
114 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
115 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
116 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
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,3-6,8-10,24,35-37"));
119 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
120 return 0;
121}