Debian Codegraph @

src/libsysprof/sysprof-symbol-cache.c

raw
1 /* sysprof-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 "config.h"
22
23 typedef struct _SysprofSymbolCacheNode SysprofSymbolCacheNode;
24 static void sysprof_symbol_cache_node_augment (SysprofSymbolCacheNode *node);
25 #define RB_AUGMENT(elem) sysprof_symbol_cache_node_augment(elem)
26
27 #include "tree.h"
28
29 #include "sysprof-bundled-symbolizer-private.h"
30 #include "sysprof-symbol-private.h"
31 #include "sysprof-symbol-cache-private.h"
32
33 struct _SysprofSymbolCacheNode
34 {
35 RB_ENTRY(_SysprofSymbolCacheNode) link;
36 SysprofSymbol *symbol;
37 guint64 low;
38 guint64 high;
39 guint64 max;
40 };
41
42 struct _SysprofSymbolCache
43 {
44 GObject parent_instance;
45 RB_HEAD(sysprof_symbol_cache, _SysprofSymbolCacheNode) head;
46 };
47
48 G_DEFINE_FINAL_TYPE (SysprofSymbolCache, sysprof_symbol_cache, G_TYPE_OBJECT)
49
50 static inline int
51 sysprof_symbol_cache_node_compare (SysprofSymbolCacheNode *a,
52 SysprofSymbolCacheNode *b)
53 {
54 if (a->low < b->low)
55 return -1;
56 else if (a->low > b->low)
57 return 1;
58 else
59 return 0;
60 }
61
62 RB_GENERATE_STATIC(sysprof_symbol_cache, _SysprofSymbolCacheNode, link, sysprof_symbol_cache_node_compare);
63
64 static void
65 sysprof_symbol_cache_node_augment (SysprofSymbolCacheNode *node)
66 {
67 node->max = node->high;
68
69 if (RB_LEFT(node, link) && RB_LEFT(node, link)->max > node->max)
70 node->max = RB_LEFT(node, link)->max;
71
72 if (RB_RIGHT(node, link) && RB_RIGHT(node, link)->max > node->max)
73 node->max = RB_RIGHT(node, link)->max;
74 }
75
76 static void
77 sysprof_symbol_cache_node_finalize (SysprofSymbolCacheNode *node)
78 {
79 g_clear_object (&node->symbol);
80 g_free (node);
81 }
82
83 static void
84 sysprof_symbol_cache_node_free (SysprofSymbolCacheNode *node)
85 {
86 SysprofSymbolCacheNode *right = RB_RIGHT(node, link);
87 SysprofSymbolCacheNode *left = RB_LEFT(node, link);
88
89 if (left != NULL)
90 sysprof_symbol_cache_node_free (left);
91
92 sysprof_symbol_cache_node_finalize (node);
93
94 if (right != NULL)
95 sysprof_symbol_cache_node_free (right);
96 }
97
98 static void
99 sysprof_symbol_cache_finalize (GObject *object)
100 {
101 SysprofSymbolCache *self = (SysprofSymbolCache *)object;
102 SysprofSymbolCacheNode *node = RB_ROOT(&self->head);
103
104 if (node != NULL)
105 sysprof_symbol_cache_node_free (node);
106
107 G_OBJECT_CLASS (sysprof_symbol_cache_parent_class)->finalize (object);
108 }
109
110 static void
111 sysprof_symbol_cache_class_init (SysprofSymbolCacheClass *klass)
112 {
113 GObjectClass *object_class = G_OBJECT_CLASS (klass);
114
115 object_class->finalize = sysprof_symbol_cache_finalize;
116 }
117
118 static void
119 sysprof_symbol_cache_init (SysprofSymbolCache *self)
120 {
121 RB_INIT (&self->head);
122 }
123
124 SysprofSymbolCache *
125 sysprof_symbol_cache_new (void)
126 {
127 return g_object_new (SYSPROF_TYPE_SYMBOL_CACHE, NULL);
128 }
129
130 void
131 sysprof_symbol_cache_take (SysprofSymbolCache *self,
132 SysprofSymbol *symbol)
133 {
134 SysprofSymbolCacheNode *node;
135 SysprofSymbolCacheNode *parent;
136 SysprofSymbolCacheNode *ret;
137
138 g_return_if_fail (SYSPROF_IS_SYMBOL_CACHE (self));
139 g_return_if_fail (SYSPROF_IS_SYMBOL (symbol));
140 g_return_if_fail (symbol->end_address > symbol->begin_address);
141
142 /* Some symbols are not suitable for our interval tree */
143 if (symbol->begin_address == 0 ||
144 symbol->end_address == 0 ||
145 symbol->begin_address == symbol->end_address)
146 {
147 g_object_unref (symbol);
148 return;
149 }
150
151 node = g_new0 (SysprofSymbolCacheNode, 1);
152 node->symbol = symbol;
153 node->low = symbol->begin_address;
154 node->high = symbol->end_address-1;
155 node->max = node->high;
156
157 /* If there is a collision, then the node is returned. Otherwise
158 * if the node was inserted, NULL is returned.
159 */
160 if ((ret = RB_INSERT(sysprof_symbol_cache, &self->head, node)))
161 {
162 sysprof_symbol_cache_node_free (node);
163 return;
164 }
165
166 parent = RB_PARENT(node, link);
167
168 while (parent != NULL)
169 {
170 if (node->max > parent->max)
171 parent->max = node->max;
172 node = parent;
173 parent = RB_PARENT(parent, link);
174 }
175 }
176
177 SysprofSymbol *
178 sysprof_symbol_cache_lookup (SysprofSymbolCache *self,
179 SysprofAddress address)
180 {
181 SysprofSymbolCacheNode *node;
182
183 g_return_val_if_fail (SYSPROF_IS_SYMBOL_CACHE (self), NULL);
184
185 if (address == 0)
186 return NULL;
187
188 node = RB_ROOT(&self->head);
189
190 /* The root node contains our calculated max as augmented in RBTree.
191 * Therefore, we can know if @address falls beyond the upper bound
192 * in O(1) without having to add a branch to the while loop below.
193 */
194 if (node == NULL || node->max < address)
195 return NULL;
196
197 while (node != NULL)
198 {
199 g_assert (RB_LEFT(node, link) == NULL ||
200 node->max >= RB_LEFT(node, link)->max);
201 g_assert (RB_RIGHT(node, link) == NULL ||
202 node->max >= RB_RIGHT(node, link)->max);
203
204 if (address >= node->low && address <= node->high)
205 return node->symbol;
206
207 if (RB_LEFT(node, link) && RB_LEFT(node, link)->max >= address)
208 node = RB_LEFT(node, link);
209 else
210 node = RB_RIGHT(node, link);
211 }
212
213 return NULL;
214 }
215
216 static guint
217 get_string (GByteArray *strings,
218 GHashTable *strings_offset,
219 const char *string)
220 {
221 guint pos;
222
223 if (string == NULL || string[0] == 0)
224 return 0;
225
226 pos = GPOINTER_TO_UINT (g_hash_table_lookup (strings_offset, string));
227
228 if (pos == 0)
229 {
230 pos = strings->len;
231 g_byte_array_append (strings, (const guint8 *)string, strlen (string) + 1);
232 g_hash_table_insert (strings_offset, (char *)string, GUINT_TO_POINTER (pos));
233 }
234
235 return pos;
236 }
237
238 void
239 sysprof_symbol_cache_populate_packed (SysprofSymbolCache *self,
240 GArray *array,
241 GByteArray *strings,
242 GHashTable *strings_offset,
243 int pid)
244 {
245 SysprofSymbolCacheNode *node;
246
247 g_return_if_fail (SYSPROF_IS_SYMBOL_CACHE (self));
248 g_return_if_fail (array != NULL);
249
250 RB_FOREACH(node, sysprof_symbol_cache, &self->head) {
251 SysprofPackedSymbol packed;
252 SysprofSymbol *symbol = node->symbol;
253
254 if (symbol->is_fallback)
255 continue;
256
257 packed.addr_begin = symbol->begin_address;
258 packed.addr_end = symbol->end_address;
259 packed.pid = pid;
260 packed.offset = get_string (strings, strings_offset, symbol->name);
261 packed.tag_offset = get_string (strings, strings_offset, symbol->binary_nick);
262
263 g_array_append_val (array, packed);
264 }
265 }