Debian Codegraph @

src/libsysprof/tests/test-symbol-cache.c

raw
1 /* test-symbol-cache.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include <stdlib.h>
22
23 #include <sysprof.h>
24
25 #include "sysprof-symbol-private.h"
26 #include "sysprof-symbol-cache-private.h"
27
28 typedef struct _SymbolInfo
29 {
30 const char *name;
31 guint64 begin;
32 guint64 end;
33 int position;
34 int sort;
35 SysprofSymbol *symbol;
36 } SymbolInfo;
37
38 static SysprofSymbol *
39 create_symbol (const char *name,
40 guint64 begin,
41 guint64 end)
42 {
43 g_assert (begin < end);
44
45 return _sysprof_symbol_new (g_ref_string_new (name), NULL, NULL, begin, end, SYSPROF_SYMBOL_KIND_USER);
46 }
47
48 static int
49 sort_by_key (gconstpointer a,
50 gconstpointer b)
51 {
52 const SymbolInfo *info_a = a;
53 const SymbolInfo *info_b = b;
54
55 if (info_a->sort < info_b->sort)
56 return -1;
57 else if (info_a->sort > info_b->sort)
58 return 1;
59 else
60 return 0;
61 }
62
63 static int
64 sort_by_position (gconstpointer a,
65 gconstpointer b)
66 {
67 const SymbolInfo *info_a = a;
68 const SymbolInfo *info_b = b;
69
70 if (info_a->position < info_b->position)
71 return -1;
72 else if (info_a->position > info_b->position)
73 return 1;
74 else
75 return 0;
76 }
77
78 static void
79 test_interval_tree (void)
80 {
81 SysprofSymbolCache *symbol_cache = sysprof_symbol_cache_new ();
82 SymbolInfo symbols[] = {
83 { "symbol1", 0x10000, 0x20000 },
84 { "symbol2", 0x20000, 0x30000 },
85 { "symbol3", 0x30000, 0x40000 },
86 { "symbol4", 0x90000, 0xa0000 },
87 { "symbol5", 0xb0000, 0xb0001 },
88 { "symbol6", 0xb0001, 0xb0002 },
89 };
90
91 /* Add some randomness on insertion */
92 for (guint i = 0; i < G_N_ELEMENTS (symbols); i++)
93 {
94 symbols[i].position = i;
95 symbols[i].sort = g_random_int ();
96 }
97
98 /* Sort randomly for insertion */
99 qsort (symbols, G_N_ELEMENTS (symbols), sizeof (SymbolInfo), sort_by_key);
100 for (guint i = 0; i < G_N_ELEMENTS (symbols); i++)
101 {
102 SymbolInfo *info = &symbols[i];
103
104 g_assert_cmpint (info->begin, <, info->end);
105
106 info->symbol = create_symbol (info->name, info->begin, info->end);
107
108 g_assert_nonnull (info->symbol);
109 g_assert_true (SYSPROF_IS_SYMBOL (info->symbol));
110
111 sysprof_symbol_cache_take (symbol_cache, g_object_ref (info->symbol));
112 }
113
114 /* Now resort to do lookups with edge checking */
115 qsort (symbols, G_N_ELEMENTS (symbols), sizeof (SymbolInfo), sort_by_position);
116 for (guint i = 0; i < G_N_ELEMENTS (symbols); i++)
117 {
118 const SymbolInfo *info = &symbols[i];
119 const SymbolInfo *prev = i > 0 ? &symbols[i-1] : NULL;
120 const SymbolInfo *next = i + 1 < G_N_ELEMENTS (symbols) ? &symbols[i+1] : NULL;
121 SysprofSymbol *lookup;
122
123 g_assert_cmpint (info->position, ==, i);
124
125 lookup = sysprof_symbol_cache_lookup (symbol_cache, info->begin-1);
126 if (prev && info->begin == prev->end)
127 g_assert_true (lookup == prev->symbol);
128 else
129 g_assert_null (lookup);
130
131 lookup = sysprof_symbol_cache_lookup (symbol_cache, info->begin);
132 g_assert_nonnull (lookup);
133 g_assert_true (lookup == info->symbol);
134
135 lookup = sysprof_symbol_cache_lookup (symbol_cache, info->end);
136 if (next == NULL || next->begin > info->end)
137 g_assert_null (lookup);
138 else
139 g_assert_true (lookup == next->symbol);
140
141 if (info->begin+1 != info->end)
142 {
143 lookup = sysprof_symbol_cache_lookup (symbol_cache, info->begin+1);
144 g_assert_nonnull (lookup);
145 g_assert_true (lookup == info->symbol);
146 }
147
148 lookup = sysprof_symbol_cache_lookup (symbol_cache, info->end-1);
149 g_assert_nonnull (lookup);
150 g_assert_true (lookup == info->symbol);
151
152 lookup = sysprof_symbol_cache_lookup (symbol_cache, info->begin + ((info->end-info->begin)/2));
153 g_assert_nonnull (lookup);
154 g_assert_true (lookup == info->symbol);
155 }
156
157 g_assert_finalize_object (symbol_cache);
158
159 for (guint i = 0; i < G_N_ELEMENTS (symbols); i++)
160 g_assert_finalize_object (symbols[i].symbol);
161 }
162
163 static void
164 test_jitmap (void)
165 {
166 SysprofSymbolCache *symbol_cache = sysprof_symbol_cache_new ();
167
168 for (guint i = 1; i <= 10000; i++)
169 {
170 SysprofAddress begin = 0xE000000000000000 + i;
171 g_autofree char *name = g_strdup_printf ("%u", i);
172 SysprofSymbol *symbol = create_symbol (name, begin, begin+1);
173
174 sysprof_symbol_cache_take (symbol_cache, symbol);
175 }
176
177 g_assert_null (sysprof_symbol_cache_lookup (symbol_cache, 0));
178 g_assert_null (sysprof_symbol_cache_lookup (symbol_cache, 10001));
179
180 for (guint i = 1; i <= 10000; i++)
181 {
182 SysprofAddress begin = 0xE000000000000000 + i;
183 SysprofSymbol *symbol = sysprof_symbol_cache_lookup (symbol_cache, begin);
184 g_autofree char *name = g_strdup_printf ("%u", i);
185
186 g_assert_nonnull (symbol);
187 g_assert_cmpint (begin, ==, symbol->begin_address);
188 g_assert_cmpint (begin+1, ==, symbol->end_address);
189 g_assert_cmpstr (name, ==, symbol->name);
190 }
191
192 g_assert_finalize_object (symbol_cache);
193 }
194
195 static void
196 test_collision (void)
197 {
198 SysprofSymbolCache *symbol_cache = sysprof_symbol_cache_new ();
199 SysprofSymbol *first = NULL;
200
201 for (guint i = 1; i <= 10000; i++)
202 {
203 SysprofAddress begin = 0xe000000000000000 + i;
204 g_autofree char *name = g_strdup_printf ("%u", i);
205 SysprofSymbol *symbol = create_symbol (name, begin, begin+1);
206
207 if (first == NULL)
208 first = g_object_ref (symbol);
209
210 sysprof_symbol_cache_take (symbol_cache, symbol);
211 }
212
213 g_assert_true (SYSPROF_IS_SYMBOL (first));
214
215 for (guint i = 1; i <= 10000; i++)
216 {
217 SysprofAddress begin = 0xE000000000000000 + i;
218 g_autofree char *name = g_strdup_printf ("%u", i);
219 SysprofSymbol *symbol = create_symbol (name, begin, begin+1);
220
221 sysprof_symbol_cache_take (symbol_cache, symbol);
222 }
223
224 g_assert_finalize_object (symbol_cache);
225 g_assert_finalize_object (first);
226
227 /* To test this fully, you need `-Db_sanitize=address` so that
228 * you can detect any leaks from RB_INSERT().
229 */
230 }
231
232 int
233 main (int argc,
234 char *argv[])
235 {
236 g_test_init (&argc, &argv, NULL);
237 g_test_add_func ("/libsysprof/SysprofSymbolCache/interval-tree",
238 test_interval_tree);
239 g_test_add_func ("/libsysprof/SysprofSymbolCache/jitmap",
240 test_jitmap);
241 g_test_add_func ("/libsysprof/SysprofSymbolCache/collision",
242 test_collision);
243 return g_test_run ();
244 }