xref: /openbmc/linux/tools/perf/tests/maps.c (revision e961f8c6)
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 static int check_maps(struct map_def *merged, unsigned int size, struct maps *maps)
18 {
19 	struct map_rb_node *rb_node;
20 	unsigned int i = 0;
21 	bool failed = false;
22 
23 	if (maps__nr_maps(maps) != size) {
24 		pr_debug("Expected %d maps, got %d", size, maps__nr_maps(maps));
25 		failed = true;
26 	} else {
27 		maps__for_each_entry(maps, rb_node) {
28 			struct map *map = rb_node->map;
29 
30 			if (map__start(map) != merged[i].start ||
31 			    map__end(map) != merged[i].end ||
32 			    strcmp(map__dso(map)->name, merged[i].name) ||
33 			    refcount_read(map__refcnt(map)) != 1) {
34 				failed = true;
35 			}
36 			i++;
37 		}
38 	}
39 	if (failed) {
40 		pr_debug("Expected:\n");
41 		for (i = 0; i < size; i++) {
42 			pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: 1\n",
43 				merged[i].start, merged[i].end, merged[i].name);
44 		}
45 		pr_debug("Got:\n");
46 		maps__for_each_entry(maps, rb_node) {
47 			struct map *map = rb_node->map;
48 
49 			pr_debug("\tstart: %" PRIu64 " end: %" PRIu64 " name: '%s' refcnt: %d\n",
50 				map__start(map),
51 				map__end(map),
52 				map__dso(map)->name,
53 				refcount_read(map__refcnt(map)));
54 		}
55 	}
56 	return failed ? TEST_FAIL : TEST_OK;
57 }
58 
59 static int test__maps__merge_in(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
60 {
61 	unsigned int i;
62 	struct map_def bpf_progs[] = {
63 		{ "bpf_prog_1", 200, 300 },
64 		{ "bpf_prog_2", 500, 600 },
65 		{ "bpf_prog_3", 800, 900 },
66 	};
67 	struct map_def merged12[] = {
68 		{ "kcore1",     100,  200 },
69 		{ "bpf_prog_1", 200,  300 },
70 		{ "kcore1",     300,  500 },
71 		{ "bpf_prog_2", 500,  600 },
72 		{ "kcore1",     600,  800 },
73 		{ "bpf_prog_3", 800,  900 },
74 		{ "kcore1",     900, 1000 },
75 	};
76 	struct map_def merged3[] = {
77 		{ "kcore1",      100,  200 },
78 		{ "bpf_prog_1",  200,  300 },
79 		{ "kcore1",      300,  500 },
80 		{ "bpf_prog_2",  500,  600 },
81 		{ "kcore1",      600,  800 },
82 		{ "bpf_prog_3",  800,  900 },
83 		{ "kcore1",      900, 1000 },
84 		{ "kcore3",     1000, 1100 },
85 	};
86 	struct map *map_kcore1, *map_kcore2, *map_kcore3;
87 	int ret;
88 	struct maps *maps = maps__new(NULL);
89 
90 	TEST_ASSERT_VAL("failed to create maps", maps);
91 
92 	for (i = 0; i < ARRAY_SIZE(bpf_progs); i++) {
93 		struct map *map;
94 
95 		map = dso__new_map(bpf_progs[i].name);
96 		TEST_ASSERT_VAL("failed to create map", map);
97 
98 		map__set_start(map, bpf_progs[i].start);
99 		map__set_end(map, bpf_progs[i].end);
100 		TEST_ASSERT_VAL("failed to insert map", maps__insert(maps, map) == 0);
101 		map__put(map);
102 	}
103 
104 	map_kcore1 = dso__new_map("kcore1");
105 	TEST_ASSERT_VAL("failed to create map", map_kcore1);
106 
107 	map_kcore2 = dso__new_map("kcore2");
108 	TEST_ASSERT_VAL("failed to create map", map_kcore2);
109 
110 	map_kcore3 = dso__new_map("kcore3");
111 	TEST_ASSERT_VAL("failed to create map", map_kcore3);
112 
113 	/* kcore1 map overlaps over all bpf maps */
114 	map__set_start(map_kcore1, 100);
115 	map__set_end(map_kcore1, 1000);
116 
117 	/* kcore2 map hides behind bpf_prog_2 */
118 	map__set_start(map_kcore2, 550);
119 	map__set_end(map_kcore2, 570);
120 
121 	/* kcore3 map hides behind bpf_prog_3, kcore1 and adds new map */
122 	map__set_start(map_kcore3, 880);
123 	map__set_end(map_kcore3, 1100);
124 
125 	ret = maps__merge_in(maps, map_kcore1);
126 	TEST_ASSERT_VAL("failed to merge map", !ret);
127 
128 	ret = check_maps(merged12, ARRAY_SIZE(merged12), maps);
129 	TEST_ASSERT_VAL("merge check failed", !ret);
130 
131 	ret = maps__merge_in(maps, map_kcore2);
132 	TEST_ASSERT_VAL("failed to merge map", !ret);
133 
134 	ret = check_maps(merged12, ARRAY_SIZE(merged12), maps);
135 	TEST_ASSERT_VAL("merge check failed", !ret);
136 
137 	ret = maps__merge_in(maps, map_kcore3);
138 	TEST_ASSERT_VAL("failed to merge map", !ret);
139 
140 	ret = check_maps(merged3, ARRAY_SIZE(merged3), maps);
141 	TEST_ASSERT_VAL("merge check failed", !ret);
142 
143 	maps__zput(maps);
144 	return TEST_OK;
145 }
146 
147 DEFINE_SUITE("maps__merge_in", maps__merge_in);
148