Debian Codegraph @

src/libsysprof/sysprof-callgraph-frame.c

raw
1 /* sysprof-callgraph-frame.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 #include <gio/gio.h>
24
25 #include "sysprof-callgraph-private.h"
26 #include "sysprof-callgraph-frame-private.h"
27 #include "sysprof-category-summary-private.h"
28 #include "sysprof-enums.h"
29 #include "sysprof-symbol-private.h"
30 #include "sysprof-document-bitset-index-private.h"
31
32 #include "eggbitset.h"
33
34 #define MAX_STACK_DEPTH 128
35
36 struct _SysprofCallgraphFrame
37 {
38 GObject parent_instance;
39 SysprofCallgraph *callgraph;
40 GObject *owner;
41 SysprofCallgraphNode *node;
42 guint n_children;
43 };
44
45 enum {
46 PROP_0,
47 PROP_CALLGRAPH,
48 PROP_CATEGORY,
49 PROP_SYMBOL,
50 PROP_N_ITEMS,
51 N_PROPS
52 };
53
54 static GType
55 sysprof_callgraph_frame_get_item_type (GListModel *model)
56 {
57 return SYSPROF_TYPE_CALLGRAPH_FRAME;
58 }
59
60 static guint
61 sysprof_callgraph_frame_get_n_items (GListModel *model)
62 {
63 return SYSPROF_CALLGRAPH_FRAME (model)->n_children;
64 }
65
66 static gpointer
67 sysprof_callgraph_frame_get_item (GListModel *model,
68 guint position)
69 {
70 SysprofCallgraphFrame *self = SYSPROF_CALLGRAPH_FRAME (model);
71 SysprofCallgraphNode *iter;
72
73 if (self->callgraph == NULL)
74 return NULL;
75
76 iter = self->node->children;
77
78 while (iter != NULL && position > 0)
79 {
80 iter = iter->next;
81 position--;
82 }
83
84 if (iter == NULL)
85 return NULL;
86
87 return _sysprof_callgraph_frame_new_for_node (self->callgraph, self->owner, iter);
88 }
89
90 static void
91 list_model_iface_init (GListModelInterface *iface)
92 {
93 iface->get_item_type = sysprof_callgraph_frame_get_item_type;
94 iface->get_n_items = sysprof_callgraph_frame_get_n_items;
95 iface->get_item = sysprof_callgraph_frame_get_item;
96 }
97
98 G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofCallgraphFrame, sysprof_callgraph_frame, G_TYPE_OBJECT,
99 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
100
101 static GParamSpec *properties [N_PROPS];
102
103 static void
104 sysprof_callgraph_frame_finalize (GObject *object)
105 {
106 SysprofCallgraphFrame *self = (SysprofCallgraphFrame *)object;
107
108 g_clear_weak_pointer (&self->callgraph);
109 g_clear_object (&self->owner);
110 self->node = NULL;
111
112 G_OBJECT_CLASS (sysprof_callgraph_frame_parent_class)->finalize (object);
113 }
114
115 static void
116 sysprof_callgraph_frame_get_property (GObject *object,
117 guint prop_id,
118 GValue *value,
119 GParamSpec *pspec)
120 {
121 SysprofCallgraphFrame *self = SYSPROF_CALLGRAPH_FRAME (object);
122
123 switch (prop_id)
124 {
125 case PROP_CALLGRAPH:
126 g_value_set_object (value, self->callgraph);
127 break;
128
129 case PROP_CATEGORY:
130 g_value_set_enum (value, sysprof_callgraph_frame_get_category (self));
131 break;
132
133 case PROP_N_ITEMS:
134 g_value_set_uint (value, g_list_model_get_n_items (G_LIST_MODEL (self)));
135 break;
136
137 case PROP_SYMBOL:
138 g_value_set_object (value, sysprof_callgraph_frame_get_symbol (self));
139 break;
140
141 default:
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 }
144 }
145
146 static void
147 sysprof_callgraph_frame_class_init (SysprofCallgraphFrameClass *klass)
148 {
149 GObjectClass *object_class = G_OBJECT_CLASS (klass);
150
151 object_class->finalize = sysprof_callgraph_frame_finalize;
152 object_class->get_property = sysprof_callgraph_frame_get_property;
153
154 properties [PROP_CALLGRAPH] =
155 g_param_spec_object ("callgraph", NULL, NULL,
156 SYSPROF_TYPE_CALLGRAPH,
157 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
158
159 properties [PROP_CATEGORY] =
160 g_param_spec_enum ("category", NULL, NULL,
161 SYSPROF_TYPE_CALLGRAPH_CATEGORY,
162 SYSPROF_CALLGRAPH_CATEGORY_UNCATEGORIZED,
163 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
164
165 properties [PROP_N_ITEMS] =
166 g_param_spec_uint ("n-items", NULL, NULL,
167 0, G_MAXUINT, 0,
168 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
169
170 properties [PROP_SYMBOL] =
171 g_param_spec_object ("symbol", NULL, NULL,
172 SYSPROF_TYPE_SYMBOL,
173 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
174
175 g_object_class_install_properties (object_class, N_PROPS, properties);
176 }
177
178 static void
179 sysprof_callgraph_frame_init (SysprofCallgraphFrame *self)
180 {
181 }
182
183 SysprofCallgraphFrame *
184 _sysprof_callgraph_frame_new_for_node (SysprofCallgraph *callgraph,
185 GObject *owner,
186 SysprofCallgraphNode *node)
187 {
188 SysprofCallgraphFrame *self;
189
190 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (callgraph), NULL);
191 g_return_val_if_fail (node != NULL, NULL);
192
193 self = g_object_new (SYSPROF_TYPE_CALLGRAPH_FRAME, NULL);
194 g_set_weak_pointer (&self->callgraph, callgraph);
195 g_set_object (&self->owner, owner);
196 self->node = node;
197
198 for (const SysprofCallgraphNode *iter = node->children;
199 iter != NULL;
200 iter = iter->next)
201 self->n_children++;
202
203 return self;
204 }
205
206 /**
207 * sysprof_callgraph_frame_get_symbol:
208 * @self: a #SysprofCallgraphFrame
209 *
210 * Gets the symbol for the frame.
211 *
212 * Returns: (nullable) (transfer none): a #SysprofSymbol
213 */
214 SysprofSymbol *
215 sysprof_callgraph_frame_get_symbol (SysprofCallgraphFrame *self)
216 {
217 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self), NULL);
218
219 if (self->callgraph == NULL)
220 return NULL;
221
222 return self->node->summary->symbol;
223 }
224
225 /**
226 * sysprof_callgraph_frame_get_augment: (skip)
227 * @self: a #SysprofCallgraphFrame
228 *
229 * Gets the augmentation that was attached to the callgrpah node.
230 *
231 * Returns: (nullable) (transfer none): the augmentation data
232 */
233 gpointer
234 sysprof_callgraph_frame_get_augment (SysprofCallgraphFrame *self)
235 {
236 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self), NULL);
237
238 if (self->callgraph == NULL)
239 return NULL;
240
241 return sysprof_callgraph_get_augment (self->callgraph, self->node);
242 }
243
244 /**
245 * sysprof_callgraph_frame_get_summary_augment: (skip)
246 * @self: a #SysprofCallgraphFrame
247 *
248 * Gets the augmentation that was attached to the summary for
249 * the callgraph node's symbol.
250 *
251 * Returns: (nullable) (transfer none): the augmentation data
252 */
253 gpointer
254 sysprof_callgraph_frame_get_summary_augment (SysprofCallgraphFrame *self)
255 {
256 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self), NULL);
257
258 if (self->callgraph == NULL)
259 return NULL;
260
261 return sysprof_callgraph_get_summary_augment (self->callgraph, self->node);
262 }
263
264 /**
265 * sysprof_callgraph_frame_get_callgraph:
266 * @self: a #SysprofCallgraphFrame
267 *
268 * Gets the callgraph the frame belongs to.
269 *
270 * Returns: (transfer none) (nullable): a #SysprofCallgraph, or %NULL
271 */
272 SysprofCallgraph *
273 sysprof_callgraph_frame_get_callgraph (SysprofCallgraphFrame *self)
274 {
275 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self), NULL);
276
277 return self->callgraph;
278 }
279
280 static void
281 sysprof_callgraph_frame_list_traceables_cb (GObject *object,
282 GAsyncResult *result,
283 gpointer user_data)
284 {
285 SysprofCallgraph *callgraph = (SysprofCallgraph *)object;
286 g_autoptr(GListModel) model = NULL;
287 g_autoptr(GError) error = NULL;
288 g_autoptr(GTask) task = user_data;
289
290 g_assert (SYSPROF_IS_CALLGRAPH (callgraph));
291 g_assert (G_IS_ASYNC_RESULT (result));
292 g_assert (G_IS_TASK (task));
293
294 if (!(model = sysprof_callgraph_list_traceables_for_node_finish (callgraph, result, &error)))
295 g_task_return_error (task, g_steal_pointer (&error));
296 else
297 g_task_return_pointer (task, g_steal_pointer (&model), g_object_unref);
298 }
299
300 /**
301 * sysprof_callgraph_frame_list_traceables:
302 * @self: a #SysprofCallgraphFrame
303 * @cancellable: (nullable): a #GCancellable or %NULL
304 * @callback: a #GAsyncReadyCallback
305 * @user_data: closure data for @callback
306 *
307 * Asynchronously lists the traceables that contain @self.
308 */
309 void
310 sysprof_callgraph_frame_list_traceables_async (SysprofCallgraphFrame *self,
311 GCancellable *cancellable,
312 GAsyncReadyCallback callback,
313 gpointer user_data)
314 {
315 g_autoptr(GTask) task = NULL;
316
317 g_return_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self));
318 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
319
320 task = g_task_new (self, cancellable, callback, user_data);
321 g_task_set_source_tag (task, sysprof_callgraph_frame_list_traceables_async);
322
323 if (self->callgraph == NULL)
324 g_task_return_new_error (task,
325 G_IO_ERROR,
326 G_IO_ERROR_FAILED,
327 "Callgraph already disposed");
328 else
329 sysprof_callgraph_list_traceables_for_node_async (self->callgraph,
330 self->node,
331 cancellable,
332 sysprof_callgraph_frame_list_traceables_cb,
333 g_steal_pointer (&task));
334 }
335
336 /**
337 * sysprof_callgraph_frame_list_traceables_finish:
338 * @self: a #SysprofCallgraphFrame
339 *
340 * Completes an asynchronous request to list traceables.
341 *
342 * Returns: (transfer full): a #GListModel of #SysprofDocumentTraceable if
343 * successful; otherwise %NULL and @error is set.
344 */
345 GListModel *
346 sysprof_callgraph_frame_list_traceables_finish (SysprofCallgraphFrame *self,
347 GAsyncResult *result,
348 GError **error)
349 {
350 GListModel *ret;
351
352 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self), NULL);
353 g_return_val_if_fail (G_IS_TASK (result), NULL);
354
355 ret = g_task_propagate_pointer (G_TASK (result), error);
356
357 g_return_val_if_fail (!ret || G_IS_LIST_MODEL (ret), NULL);
358
359 return ret;
360 }
361
362 gboolean
363 sysprof_callgraph_frame_is_leaf (SysprofCallgraphFrame *self)
364 {
365 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self), 0);
366
367 return self->n_children == 0;
368 }
369
370 /**
371 * sysprof_callgraph_frame_get_category:
372 * @self: a #SysprofCallgraphFrame
373 *
374 * Gets the category of the node if %SYSPROF_CALLGRAPH_FLAGS_CATEGORIZE_FRAMES
375 * was set when generating the callgraph. Otherwise
376 * %SYSPROF_CALLGRAPH_CATEGORY_UNCATEGORIZED.
377 *
378 * Returns: callgraph category
379 */
380 SysprofCallgraphCategory
381 sysprof_callgraph_frame_get_category (SysprofCallgraphFrame *self)
382 {
383 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self), 0);
384
385 if (self->callgraph != NULL && self->node != NULL && self->node->category)
386 return self->node->category & ~SYSPROF_CALLGRAPH_CATEGORY_INHERIT;
387
388 return SYSPROF_CALLGRAPH_CATEGORY_UNCATEGORIZED;
389 }
390
391 typedef struct _Summary
392 {
393 guint64 count;
394 } Summary;
395
396 static void
397 summarize_node (const SysprofCallgraphNode *node,
398 Summary *summaries)
399 {
400 guint category = SYSPROF_CALLGRAPH_CATEGORY_UNMASK(node->category);
401
402 if (node->is_toplevel &&
403 category != 0 &&
404 category != SYSPROF_CALLGRAPH_CATEGORY_PRESENTATION)
405 {
406 gboolean seen[SYSPROF_CALLGRAPH_CATEGORY_LAST] = {0};
407
408 /* Track total count in 0 */
409 summaries[0].count += node->count;
410
411 seen[category] = TRUE;
412 summaries[category].count += node->count;
413
414 for (const SysprofCallgraphNode *parent = node->parent; parent; parent = parent->parent)
415 {
416 guint parent_category = SYSPROF_CALLGRAPH_CATEGORY_UNMASK (parent->category);
417
418 if (!seen[parent_category] && (parent->category & SYSPROF_CALLGRAPH_CATEGORY_INHERIT) != 0)
419 {
420 seen[parent_category] = TRUE;
421 summaries[parent_category].count += node->count;
422 }
423 }
424 }
425
426 for (const SysprofCallgraphNode *iter = node->children; iter; iter = iter->next)
427 summarize_node (iter, summaries);
428 }
429
430 static void
431 sysprof_callgraph_frame_summarize (GTask *task,
432 gpointer source_object,
433 gpointer task_data,
434 GCancellable *cancellable)
435 {
436 SysprofCallgraphFrame *self = source_object;
437 g_autofree Summary *summaries = NULL;
438 g_autoptr(GListStore) store = NULL;
439 guint64 total = 0;
440
441 g_assert (G_IS_TASK (task));
442 g_assert (SYSPROF_IS_CALLGRAPH_FRAME (self));
443 g_assert (SYSPROF_IS_CALLGRAPH (task_data));
444
445 summaries = g_new0 (Summary, SYSPROF_CALLGRAPH_CATEGORY_LAST);
446 summarize_node (self->node, summaries);
447
448 store = g_list_store_new (G_TYPE_OBJECT);
449 total = summaries[0].count;
450
451 for (guint i = 1; i < SYSPROF_CALLGRAPH_CATEGORY_LAST; i++)
452 {
453 g_autoptr(SysprofCategorySummary) summary = NULL;
454
455 if (summaries[i].count == 0)
456 continue;
457
458 summary = g_object_new (SYSPROF_TYPE_CATEGORY_SUMMARY, NULL);
459 summary->category = i;
460 summary->total = total;
461 summary->count = summaries[i].count;
462
463 g_list_store_append (store, summary);
464 }
465
466 g_task_return_pointer (task, g_steal_pointer (&store), g_object_unref);
467 }
468
469 void
470 sysprof_callgraph_frame_summarize_async (SysprofCallgraphFrame *self,
471 GCancellable *cancellable,
472 GAsyncReadyCallback callback,
473 gpointer user_data)
474 {
475 g_autoptr(GTask) task = NULL;
476
477 g_return_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self));
478
479 task = g_task_new (self, cancellable, callback, user_data);
480 g_task_set_source_tag (task, sysprof_callgraph_frame_summarize_async);
481
482 if (self->callgraph == NULL)
483 {
484 g_task_return_new_error (task,
485 G_IO_ERROR,
486 G_IO_ERROR_FAILED,
487 "Callgraph disposed");
488 return;
489 }
490
491 g_task_set_task_data (task, g_object_ref (self->callgraph), g_object_unref);
492 g_task_run_in_thread (task, sysprof_callgraph_frame_summarize);
493 }
494
495 /**
496 * sysprof_callgraph_frame_summarize_finish:
497 *
498 * Returns: (transfer full): a #GListModel of #SysprofCategorySummary
499 */
500 GListModel *
501 sysprof_callgraph_frame_summarize_finish (SysprofCallgraphFrame *self,
502 GAsyncResult *result,
503 GError **error)
504 {
505 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_FRAME (self), NULL);
506 g_return_val_if_fail (G_IS_TASK (result), NULL);
507
508 return g_task_propagate_pointer (G_TASK (result), error);
509 }