xref: /linux/tools/perf/tests/maps.c (revision 9e906a9dead17d81d6c2687f65e159231d0e3286)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <linux/compiler.h>
4 #include <linux/kernel.h>
5 #include "tests.h"
6 #include "map.h"
7 #include "maps.h"
8 #include "dso.h"
9 #include "debug.h"
10 
11 struct map_def {
12 	const char *name;
13 	u64 start;
14 	u64 end;
15 };
16 
17 struct check_maps_cb_args {
18 	struct map_def *merged;
19 	unsigned int i;
20 };
21 
check_maps_cb(struct map * map,void * data)22 static int check_maps_cb(struct map *map, void *data)
23 {
24 	struct check_maps_cb_args *args = data;
25 	struct map_def *merged = &args->merged[args->i];
26 
27 	if (map__start(map) != merged->start ||
28 	    map__end(map) != merged->end ||
29 	    strcmp(dso__name(map__dso(map)), merged->name) ||
30 	    refcount_read(map__refcnt(map)) != 1) {
31 		return 1;
32 	}
33 	args->i++;
34 	return 0;
35 }
36 
failed_cb(struct map * map,void * data __maybe_unused)37 static int failed_cb(struct map *map, void *data __maybe_unused)
38 {
39 	pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: %d\n",
40 		map__start(map),
41 		map__end(map),
42 		dso__name(map__dso(map)),
43 		refcount_read(map__refcnt(map)));
44 
45 	return 0;
46 }
47 
check_maps(struct map_def * merged,unsigned int size,struct maps * maps)48 static int check_maps(struct map_def *merged, unsigned int size, struct maps *maps)
49 {
50 	bool failed = false;
51 
52 	if (maps__nr_maps(maps) != size) {
53 		pr_debug("Expected %d maps, got %d", size, maps__nr_maps(maps));
54 		failed = true;
55 	} else {
56 		struct check_maps_cb_args args = {
57 			.merged = merged,
58 			.i = 0,
59 		};
60 		failed = maps__for_each_map(maps, check_maps_cb, &args);
61 	}
62 	if (failed) {
63 		pr_debug("Expected:\n");
64 		for (unsigned int i = 0; i < size; i++) {
65 			pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: 1\n",
66 				merged[i].start, merged[i].end, merged[i].name);
67 		}
68 		pr_debug("Got:\n");
69 		maps__for_each_map(maps, failed_cb, NULL);
70 	}
71 	return failed ? TEST_FAIL : TEST_OK;
72 }
73 
test__maps__merge_in(struct test_suite * t __maybe_unused,int subtest __maybe_unused)74 static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
75 {
76 	unsigned int i;
77 	struct map_def bpf_progs[] = {
78 		{ "bpf_prog_1", 200, 300 },
79 		{ "bpf_prog_2", 500, 600 },
80 		{ "bpf_prog_3", 800, 900 },
81 	};
82 	struct map_def merged12[] = {
83 		{ "kcore1",     100,  200 },
84 		{ "bpf_prog_1", 200,  300 },
85 		{ "kcore1",     300,  500 },
86 		{ "bpf_prog_2", 500,  600 },
87 		{ "kcore1",     600,  800 },
88 		{ "bpf_prog_3", 800,  900 },
89 		{ "kcore1",     900, 1000 },
90 	};
91 	struct map_def merged3[] = {
92 		{ "kcore1",      100,  200 },
93 		{ "bpf_prog_1",  200,  300 },
94 		{ "kcore1",      300,  500 },
95 		{ "bpf_prog_2",  500,  600 },
96 		{ "kcore1",      600,  800 },
97 		{ "bpf_prog_3",  800,  900 },
98 		{ "kcore1",      900, 1000 },
99 		{ "kcore3",     1000, 1100 },
100 	};
101 	struct map *map_kcore1, *map_kcore2, *map_kcore3;
102 	int ret;
103 	struct maps *maps = maps__new(NULL);
104 
105 	TEST_ASSERT_VAL("failed to create maps", maps);
106 
107 	for (i = 0; i < ARRAY_SIZE(bpf_progs); i++) {
108 		struct map *map;
109 
110 		map = dso__new_map(bpf_progs[i].name);
111 		TEST_ASSERT_VAL("failed to create map", map);
112 
113 		map__set_start(map, bpf_progs[i].start);
114 		map__set_end(map, bpf_progs[i].end);
115 		TEST_ASSERT_VAL("failed to insert map", maps__insert(maps, map) == 0);
116 		map__put(map);
117 	}
118 
119 	map_kcore1 = dso__new_map("kcore1");
120 	TEST_ASSERT_VAL("failed to create map", map_kcore1);
121 
122 	map_kcore2 = dso__new_map("kcore2");
123 	TEST_ASSERT_VAL("failed to create map", map_kcore2);
124 
125 	map_kcore3 = dso__new_map("kcore3");
126 	TEST_ASSERT_VAL("failed to create map", map_kcore3);
127 
128 	/* kcore1 map overlaps over all bpf maps */
129 	map__set_start(map_kcore1, 100);
130 	map__set_end(map_kcore1, 1000);
131 
132 	/* kcore2 map hides behind bpf_prog_2 */
133 	map__set_start(map_kcore2, 550);
134 	map__set_end(map_kcore2, 570);
135 
136 	/* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */
137 	map__set_start(map_kcore3, 880);
138 	map__set_end(map_kcore3, 1100);
139 
140 	ret = maps__merge_in(maps, map_kcore1);
141 	TEST_ASSERT_VAL("failed to merge map", !ret);
142 
143 	ret = check_maps(merged12, ARRAY_SIZE(merged12), maps);
144 	TEST_ASSERT_VAL("merge check failed", !ret);
145 
146 	ret = maps__merge_in(maps, map_kcore2);
147 	TEST_ASSERT_VAL("failed to merge map", !ret);
148 
149 	ret = check_maps(merged12, ARRAY_SIZE(merged12), maps);
150 	TEST_ASSERT_VAL("merge check failed", !ret);
151 
152 	ret = maps__merge_in(maps, map_kcore3);
153 	TEST_ASSERT_VAL("failed to merge map", !ret);
154 
155 	ret = check_maps(merged3, ARRAY_SIZE(merged3), maps);
156 	TEST_ASSERT_VAL("merge check failed", !ret);
157 
158 	maps__zput(maps);
159 	map__zput(map_kcore1);
160 	map__zput(map_kcore2);
161 	map__zput(map_kcore3);
162 	return TEST_OK;
163 }
164 
test__maps__fixup_overlap_and_insert(struct test_suite * t __maybe_unused,int subtest __maybe_unused)165 static int test__maps__fixup_overlap_and_insert(struct test_suite *t __maybe_unused,
166 						int subtest __maybe_unused)
167 {
168 	struct map_def initial_maps[] = {
169 		{ "target_map", 1000, 2000 },
170 		{ "next_map",   3000, 4000 },
171 	};
172 	struct map_def insert_split = { "split_map", 1400, 1600 };
173 	struct map_def expected_after_split[] = {
174 		{ "target_map", 1000, 1400 },
175 		{ "split_map",  1400, 1600 },
176 		{ "target_map", 1600, 2000 },
177 		{ "next_map",   3000, 4000 },
178 	};
179 
180 	struct map_def insert_eclipse = { "eclipse_map", 2500, 4500 };
181 	struct map_def expected_final[] = {
182 		{ "target_map",  1000, 1400 },
183 		{ "split_map",   1400, 1600 },
184 		{ "target_map",  1600, 2000 },
185 		{ "eclipse_map", 2500, 4500 },
186 		/* "next_map" (3000-4000) is removed */
187 	};
188 
189 	struct map *map_split, *map_eclipse;
190 	int ret;
191 	unsigned int i;
192 	struct maps *maps = maps__new(NULL);
193 
194 	TEST_ASSERT_VAL("failed to create maps", maps);
195 
196 	for (i = 0; i < ARRAY_SIZE(initial_maps); i++) {
197 		struct map *map = dso__new_map(initial_maps[i].name);
198 
199 		TEST_ASSERT_VAL("failed to create map", map);
200 		map__set_start(map, initial_maps[i].start);
201 		map__set_end(map, initial_maps[i].end);
202 		TEST_ASSERT_VAL("failed to insert map", maps__insert(maps, map) == 0);
203 		map__put(map);
204 	}
205 
206 	// Check splitting.
207 	map_split = dso__new_map(insert_split.name);
208 	TEST_ASSERT_VAL("failed to create split map", map_split);
209 	map__set_start(map_split, insert_split.start);
210 	map__set_end(map_split, insert_split.end);
211 
212 	ret = maps__fixup_overlap_and_insert(maps, map_split);
213 	TEST_ASSERT_VAL("failed to fixup and insert split map", !ret);
214 
215 	map__zput(map_split);
216 	ret = check_maps(expected_after_split, ARRAY_SIZE(expected_after_split), maps);
217 	TEST_ASSERT_VAL("split check failed", !ret);
218 
219 	// Check cover 1 map with another.
220 	map_eclipse = dso__new_map(insert_eclipse.name);
221 	TEST_ASSERT_VAL("failed to create eclipse map", map_eclipse);
222 	map__set_start(map_eclipse, insert_eclipse.start);
223 	map__set_end(map_eclipse, insert_eclipse.end);
224 
225 	ret = maps__fixup_overlap_and_insert(maps, map_eclipse);
226 	TEST_ASSERT_VAL("failed to fixup and insert eclipse map", !ret);
227 
228 	map__zput(map_eclipse);
229 	ret = check_maps(expected_final, ARRAY_SIZE(expected_final), maps);
230 	TEST_ASSERT_VAL("eclipse check failed", !ret);
231 
232 	maps__zput(maps);
233 	return TEST_OK;
234 }
235 
236 static struct test_case tests__maps[] = {
237 	TEST_CASE("Test merge_in interface", maps__merge_in),
238 	TEST_CASE("Test fix up overlap interface", maps__fixup_overlap_and_insert),
239 	{	.name = NULL, }
240 };
241 
242 struct test_suite suite__maps = {
243 	.desc = "Maps - per process mmaps abstraction",
244 	.test_cases = tests__maps,
245 };
246