Debian Codegraph @

src/libsysprof/sysprof-callgraph.c

raw
1 /* sysprof-callgraph.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 "timsort/gtktimsortprivate.h"
24
25 #include "sysprof-callgraph-private.h"
26 #include "sysprof-callgraph-frame-private.h"
27 #include "sysprof-callgraph-symbol-private.h"
28 #include "sysprof-descendants-model-private.h"
29 #include "sysprof-document-bitset-index-private.h"
30 #include "sysprof-document-private.h"
31 #include "sysprof-document-traceable.h"
32 #include "sysprof-symbol-private.h"
33
34 #include "eggbitset.h"
35
36 #define MAX_STACK_DEPTH 1024
37 #define INLINE_AUGMENT_SIZE (GLIB_SIZEOF_VOID_P*2)
38
39 static GType
40 sysprof_callgraph_get_item_type (GListModel *model)
41 {
42 return SYSPROF_TYPE_CALLGRAPH_FRAME;
43 }
44
45 static guint
46 sysprof_callgraph_get_n_items (GListModel *model)
47 {
48 return 1;
49 }
50
51 static gpointer
52 sysprof_callgraph_get_item (GListModel *model,
53 guint position)
54 {
55 SysprofCallgraph *self = SYSPROF_CALLGRAPH (model);
56
57 if (position > 0)
58 return NULL;
59
60 return _sysprof_callgraph_frame_new_for_node (self, NULL, &self->root);
61 }
62
63 static void
64 list_model_iface_init (GListModelInterface *iface)
65 {
66 iface->get_item_type = sysprof_callgraph_get_item_type;
67 iface->get_n_items = sysprof_callgraph_get_n_items;
68 iface->get_item = sysprof_callgraph_get_item;
69 }
70
71 G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofCallgraph, sysprof_callgraph, G_TYPE_OBJECT,
72 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
73
74 static SysprofSymbol *everything;
75 static SysprofSymbol *untraceable;
76
77 static void
78 sysprof_callgraph_summary_free_all (SysprofCallgraphSummary *summary)
79 {
80 g_clear_pointer (&summary->augment[0], g_free);
81 summary->augment[1] = NULL;
82 g_clear_pointer (&summary->callers, g_ptr_array_unref);
83 g_clear_pointer (&summary->traceables, egg_bitset_unref);
84 g_free (summary);
85 }
86
87 static void
88 sysprof_callgraph_summary_free_self (SysprofCallgraphSummary *summary)
89 {
90 summary->augment[0] = NULL;
91 summary->augment[1] = NULL;
92 g_clear_pointer (&summary->callers, g_ptr_array_unref);
93 g_clear_pointer (&summary->traceables, egg_bitset_unref);
94 g_free (summary);
95 }
96
97 static inline SysprofCallgraphSummary *
98 sysprof_callgraph_get_summary (SysprofCallgraph *self,
99 SysprofSymbol *symbol)
100 {
101 SysprofCallgraphSummary *summary;
102
103 if G_UNLIKELY (!(summary = g_hash_table_lookup (self->symbol_to_summary, symbol)))
104 {
105 summary = g_new0 (SysprofCallgraphSummary, 1);
106 summary->traceables = egg_bitset_new_empty ();
107 summary->callers = g_ptr_array_new ();
108 summary->symbol = symbol;
109
110 g_hash_table_insert (self->symbol_to_summary, symbol, summary);
111 g_ptr_array_add (self->symbols, symbol);
112 }
113
114 return summary;
115 }
116
117 static void
118 sysprof_callgraph_dispose (GObject *object)
119 {
120 SysprofCallgraph *self = (SysprofCallgraph *)object;
121 GDestroyNotify notify = self->augment_func_data_destroy;
122 gpointer notify_data = self->augment_func_data;
123
124 self->augment_size = 0;
125 self->augment_func = NULL;
126 self->augment_func_data = NULL;
127 self->augment_func_data_destroy = NULL;
128
129 if (notify != NULL)
130 notify (notify_data);
131
132 G_OBJECT_CLASS (sysprof_callgraph_parent_class)->dispose (object);
133 }
134
135 static void
136 sysprof_callgraph_finalize (GObject *object)
137 {
138 SysprofCallgraph *self = (SysprofCallgraph *)object;
139
140 g_clear_pointer (&self->symbol_to_summary, g_hash_table_unref);
141 g_clear_pointer (&self->symbols, g_ptr_array_unref);
142
143 g_clear_object (&self->document);
144 g_clear_object (&self->traceables);
145
146 g_clear_pointer (&self->allocator, sysprof_allocator_unref);
147
148 G_OBJECT_CLASS (sysprof_callgraph_parent_class)->finalize (object);
149 }
150
151 static void
152 sysprof_callgraph_class_init (SysprofCallgraphClass *klass)
153 {
154 GObjectClass *object_class = G_OBJECT_CLASS (klass);
155
156 object_class->dispose = sysprof_callgraph_dispose;
157 object_class->finalize = sysprof_callgraph_finalize;
158
159 everything = _sysprof_symbol_new (g_ref_string_new_intern ("All Processes"),
160 NULL, NULL, 0, 0,
161 SYSPROF_SYMBOL_KIND_ROOT);
162 untraceable = _sysprof_symbol_new (g_ref_string_new_intern ("Unwindable"),
163 NULL, NULL, 0, 0,
164 SYSPROF_SYMBOL_KIND_UNWINDABLE);
165 }
166
167 static void
168 sysprof_callgraph_init (SysprofCallgraph *self)
169 {
170 self->allocator = sysprof_allocator_new ();
171 }
172
173 static void
174 sysprof_callgraph_populate_callers (SysprofCallgraph *self,
175 SysprofCallgraphNode *node,
176 guint list_model_index)
177 {
178 g_assert (SYSPROF_IS_CALLGRAPH (self));
179 g_assert (node != NULL);
180
181 for (const SysprofCallgraphNode *iter = node;
182 iter != NULL;
183 iter = iter->parent)
184 {
185 egg_bitset_add (iter->summary->traceables, list_model_index);
186
187 if (iter->parent != NULL)
188 {
189 SysprofSymbol *parent_symbol = iter->parent->summary->symbol;
190 SysprofSymbolKind parent_kind = sysprof_symbol_get_kind (parent_symbol);
191 guint pos;
192
193 if (parent_kind != SYSPROF_SYMBOL_KIND_PROCESS &&
194 parent_kind != SYSPROF_SYMBOL_KIND_ROOT &&
195 !g_ptr_array_find (iter->summary->callers, parent_symbol, &pos))
196 g_ptr_array_add (iter->summary->callers, parent_symbol);
197 }
198 }
199 }
200
201 static SysprofCallgraphNode *
202 sysprof_callgraph_add_trace (SysprofCallgraph *self,
203 SysprofSymbol **symbols,
204 guint n_symbols,
205 guint list_model_index,
206 gboolean hide_system_libraries)
207 {
208 SysprofCallgraphNode *parent = NULL;
209
210 g_assert (SYSPROF_IS_CALLGRAPH (self));
211 g_assert (n_symbols >= 2);
212 g_assert (symbols[n_symbols-1] == everything);
213
214 parent = &self->root;
215 parent->count++;
216
217 for (guint i = n_symbols - 1; i > 0; i--)
218 {
219 SysprofSymbol *symbol = symbols[i-1];
220 SysprofCallgraphNode *node = NULL;
221
222 if (hide_system_libraries && _sysprof_symbol_is_system_library (symbol))
223 continue;
224
225 /* Try to find @symbol within the children of @parent */
226 for (SysprofCallgraphNode *iter = parent->children;
227 iter != NULL;
228 iter = iter->next)
229 {
230 g_assert (iter != NULL);
231 g_assert (iter->summary != NULL);
232 g_assert (iter->summary->symbol != NULL);
233 g_assert (symbol != NULL);
234
235 if (_sysprof_symbol_equal (iter->summary->symbol, symbol))
236 {
237 node = iter;
238 node->count++;
239 goto next_symbol;
240 }
241 }
242
243 /* Otherwise create a new node */
244 node = sysprof_allocator_new0 (self->allocator, SysprofCallgraphNode);
245 node->summary = sysprof_callgraph_get_summary (self, symbol);
246 node->parent = parent;
247 node->next = parent->children;
248 node->count = 1;
249 if (parent->children)
250 parent->children->prev = node;
251 parent->children = node;
252
253 next_symbol:
254 parent = node;
255 }
256
257 sysprof_callgraph_populate_callers (self, parent, list_model_index);
258
259 return parent;
260 }
261
262 static void
263 reverse_symbols (SysprofSymbol **symbols,
264 guint n_symbols)
265 {
266 guint half = n_symbols / 2;
267
268 for (guint i = 0; i < half; i++)
269 {
270 SysprofSymbol *tmp = symbols[i];
271 symbols[i] = symbols[n_symbols-1-i];
272 symbols[n_symbols-1-i] = tmp;
273 }
274 }
275
276 void
277 _sysprof_callgraph_categorize (SysprofCallgraph *self,
278 SysprofCallgraphNode *node)
279 {
280 if (node->category)
281 return;
282
283 if (node->parent && node->parent->category == 0)
284 _sysprof_callgraph_categorize (self, node->parent);
285
286 switch (node->summary->symbol->kind)
287 {
288 case SYSPROF_SYMBOL_KIND_ROOT:
289 case SYSPROF_SYMBOL_KIND_THREAD:
290 case SYSPROF_SYMBOL_KIND_PROCESS:
291 node->category = SYSPROF_CALLGRAPH_CATEGORY_PRESENTATION;
292 break;
293
294 case SYSPROF_SYMBOL_KIND_CONTEXT_SWITCH:
295 node->category = SYSPROF_CALLGRAPH_CATEGORY_CONTEXT_SWITCH;
296 break;
297
298 case SYSPROF_SYMBOL_KIND_UNWINDABLE:
299 node->category = SYSPROF_CALLGRAPH_CATEGORY_UNWINDABLE;
300 break;
301
302 case SYSPROF_SYMBOL_KIND_KERNEL:
303 case SYSPROF_SYMBOL_KIND_USER:
304 node->category = _sysprof_callgraph_node_categorize (node);
305
306 if (node->category > SYSPROF_CALLGRAPH_CATEGORY_UNCATEGORIZED)
307 break;
308
309 G_GNUC_FALLTHROUGH;
310
311 default:
312 {
313 SysprofCallgraphNode *parent = node->parent;
314
315 while (parent != NULL)
316 {
317 if (parent->category & SYSPROF_CALLGRAPH_CATEGORY_INHERIT)
318 {
319 node->category = parent->category;
320 return;
321 }
322
323 parent = parent->parent;
324 }
325
326 node->category = SYSPROF_CALLGRAPH_CATEGORY_UNCATEGORIZED;
327 break;
328 }
329 }
330 }
331
332 static void
333 sysprof_callgraph_add_traceable (SysprofCallgraph *self,
334 SysprofDocumentTraceable *traceable,
335 guint list_model_index)
336 {
337 SysprofAddressContext final_context;
338 SysprofCallgraphNode *node;
339 SysprofSymbol **symbols;
340 SysprofSymbol *process_symbol;
341 guint stack_depth;
342 guint n_symbols;
343 int pid;
344 int tid;
345
346 g_assert (SYSPROF_IS_CALLGRAPH (self));
347 g_assert (SYSPROF_IS_DOCUMENT_TRACEABLE (traceable));
348
349 pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (traceable));
350
351 /* Ignore "Process 0" (the Idle process) if requested */
352 if (pid == 0 && (self->flags & SYSPROF_CALLGRAPH_FLAGS_IGNORE_PROCESS_0) != 0)
353 return;
354
355 /* Ignore kernel processes if requested */
356 process_symbol = _sysprof_document_process_symbol (self->document, pid, !!(self->flags & SYSPROF_CALLGRAPH_FLAGS_MERGE_SIMILAR_PROCESSES));
357 if (process_symbol->is_kernel_process && (self->flags & SYSPROF_CALLGRAPH_FLAGS_IGNORE_KERNEL_PROCESSES))
358 return;
359
360 tid = sysprof_document_traceable_get_thread_id (traceable);
361
362 /* Early ignore anything with empty or too large a stack */
363 stack_depth = sysprof_document_traceable_get_stack_depth (traceable);
364 if (stack_depth == 0 || stack_depth > MAX_STACK_DEPTH)
365 return;
366
367 symbols = g_newa (SysprofSymbol *, stack_depth + 4);
368 n_symbols = sysprof_document_symbolize_traceable (self->document,
369 traceable,
370 symbols,
371 stack_depth,
372 &final_context);
373 if (n_symbols == 0)
374 return;
375
376 g_assert (n_symbols <= stack_depth);
377
378 /* Sometimes we get a very unhelpful unwind from the capture
379 * which is basically a single frame of "user space context".
380 * That means we got no amount of the stack, but we should
381 * really account costs to something in the application other
382 * than the [Application] entry itself so that it's more clear
383 * that it was a corrupted unwind when recording.
384 */
385 if (n_symbols == 1 &&
386 _sysprof_symbol_is_context_switch (symbols[0]) &&
387 final_context == SYSPROF_ADDRESS_CONTEXT_USER)
388 symbols[0] = untraceable;
389
390 /* We saved 3 extra spaces for these above so that we can
391 * tack on the "Process" symbol and the "All Processes" symbol.
392 * If the final address context places us in Kernel, we want
393 * to add a "- - Kernel - -" symbol to ensure that we are
394 * accounting cost to the kernel for the process.
395 */
396 if (final_context == SYSPROF_ADDRESS_CONTEXT_KERNEL)
397 symbols[n_symbols++] = _sysprof_document_kernel_symbol (self->document);
398
399 /* If the first thing we see is a context switch, then there is
400 * nothing after it to account for. Just skip the symbol as it
401 * provides nothing to us in the callgraph.
402 */
403 if (_sysprof_symbol_is_context_switch (symbols[0]))
404 {
405 symbols++;
406 n_symbols--;
407 }
408
409 if ((self->flags & SYSPROF_CALLGRAPH_FLAGS_BOTTOM_UP) != 0)
410 reverse_symbols (symbols, n_symbols);
411
412 /* If the user requested thread-ids within each process, then
413 * insert a symbol for that before the real stacks.
414 */
415 if ((self->flags & SYSPROF_CALLGRAPH_FLAGS_INCLUDE_THREADS) != 0)
416 symbols[n_symbols++] = _sysprof_document_thread_symbol (self->document, pid, tid);
417
418 symbols[n_symbols++] = process_symbol;
419 symbols[n_symbols++] = everything;
420
421 if (n_symbols > self->height)
422 self->height = n_symbols;
423
424 node = sysprof_callgraph_add_trace (self,
425 symbols,
426 n_symbols,
427 list_model_index,
428 !!(self->flags & SYSPROF_CALLGRAPH_FLAGS_HIDE_SYSTEM_LIBRARIES));
429
430 if (node != NULL)
431 {
432 node->is_toplevel = TRUE;
433
434 if (self->augment_func)
435 self->augment_func (self,
436 node,
437 SYSPROF_DOCUMENT_FRAME (traceable),
438 TRUE,
439 self->augment_func_data);
440 }
441
442 if ((self->flags & SYSPROF_CALLGRAPH_FLAGS_CATEGORIZE_FRAMES) != 0)
443 _sysprof_callgraph_categorize (self, node);
444 }
445
446 static int
447 sort_by_symbol_name (gconstpointer a,
448 gconstpointer b)
449 {
450 const SysprofCallgraphNode *node_a = *(const SysprofCallgraphNode * const *)a;
451 const SysprofCallgraphNode *node_b = *(const SysprofCallgraphNode * const *)b;
452
453 return g_utf8_collate (node_a->summary->symbol->name, node_b->summary->symbol->name);
454 }
455
456 static int
457 sort_by_count (gconstpointer a,
458 gconstpointer b)
459 {
460 const SysprofCallgraphNode *node_a = *(const SysprofCallgraphNode * const *)a;
461 const SysprofCallgraphNode *node_b = *(const SysprofCallgraphNode * const *)b;
462
463 if (node_a->count > node_b->count)
464 return -1;
465
466 if (node_a->count < node_b->count)
467 return 1;
468
469 return 0;
470 }
471
472 static void
473 sort_children (SysprofCallgraphNode *node,
474 SysprofCallgraphFlags flags)
475 {
476 SysprofCallgraphNode **children;
477 guint n_children = 0;
478 guint i = 0;
479
480 if (node->children == NULL)
481 return;
482
483 for (SysprofCallgraphNode *child = node->children; child; child = child->next)
484 {
485 sort_children (child, flags);
486 n_children++;
487 }
488
489 children = g_alloca (sizeof (SysprofCallgraphNode *) * (n_children + 1));
490 for (SysprofCallgraphNode *child = node->children; child; child = child->next)
491 children[i++] = child;
492 children[i] = NULL;
493
494 if (flags & SYSPROF_CALLGRAPH_FLAGS_LEFT_HEAVY)
495 qsort (children, n_children, sizeof (SysprofCallgraphNode *), sort_by_count);
496 else
497 qsort (children, n_children, sizeof (SysprofCallgraphNode *), sort_by_symbol_name);
498
499 node->children = children[0];
500 node->children->prev = NULL;
501 node->children->next = children[1];
502
503 for (i = 1; i < n_children; i++)
504 {
505 children[i]->next = children[i+1];
506 children[i]->prev = children[i-1];
507 }
508 }
509
510 static void
511 sysprof_callgraph_new_worker (GTask *task,
512 gpointer source_object,
513 gpointer task_data,
514 GCancellable *cancellable)
515 {
516 SysprofCallgraph *self = task_data;
517 guint n_items;
518
519 g_assert (G_IS_TASK (task));
520 g_assert (source_object == NULL);
521 g_assert (SYSPROF_IS_CALLGRAPH (self));
522 g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
523
524 n_items = g_list_model_get_n_items (self->traceables);
525
526 for (guint i = 0; i < n_items; i++)
527 {
528 g_autoptr(SysprofDocumentTraceable) traceable = g_list_model_get_item (self->traceables, i);
529
530 /* Currently, we might be racing against changes in the model. And we
531 * definitely need to address that. But this at least keeps things
532 * recovering in the meantime.
533 */
534 if (traceable == NULL)
535 break;
536
537 sysprof_callgraph_add_traceable (self, traceable, i);
538 }
539
540 /* Sort callgraph nodes alphabetically so that we can use them in the
541 * flamegraph without any further processing.
542 */
543 sort_children (&self->root, self->flags);
544
545 g_task_return_pointer (task, g_object_ref (self), g_object_unref);
546 }
547
548 void
549 _sysprof_callgraph_new_async (SysprofDocument *document,
550 SysprofCallgraphFlags flags,
551 GListModel *traceables,
552 gsize augment_size,
553 SysprofAugmentationFunc augment_func,
554 gpointer augment_func_data,
555 GDestroyNotify augment_func_data_destroy,
556 GCancellable *cancellable,
557 GAsyncReadyCallback callback,
558 gpointer user_data)
559 {
560 g_autoptr(SysprofCallgraph) self = NULL;
561 g_autoptr(GTask) task = NULL;
562 GDestroyNotify summary_free;
563
564 g_return_if_fail (SYSPROF_IS_DOCUMENT (document));
565 g_return_if_fail (G_IS_LIST_MODEL (traceables));
566 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
567
568 if (augment_size > INLINE_AUGMENT_SIZE)
569 summary_free = (GDestroyNotify)sysprof_callgraph_summary_free_all;
570 else
571 summary_free = (GDestroyNotify)sysprof_callgraph_summary_free_self;
572
573 self = g_object_new (SYSPROF_TYPE_CALLGRAPH, NULL);
574 self->flags = flags;
575 self->document = g_object_ref (document);
576 self->traceables = g_object_ref (traceables);
577 self->augment_size = augment_size;
578 self->augment_func = augment_func;
579 self->augment_func_data = augment_func_data;
580 self->augment_func_data_destroy = augment_func_data_destroy;
581 self->symbol_to_summary = g_hash_table_new_full ((GHashFunc)sysprof_symbol_hash,
582 (GEqualFunc)sysprof_symbol_equal,
583 NULL,
584 summary_free);
585 self->symbols = g_ptr_array_new ();
586 self->root.summary = sysprof_callgraph_get_summary (self, everything);
587
588 task = g_task_new (NULL, cancellable, callback, user_data);
589 g_task_set_source_tag (task, _sysprof_callgraph_new_async);
590 g_task_set_task_data (task, g_object_ref (self), g_object_unref);
591 g_task_run_in_thread (task, sysprof_callgraph_new_worker);
592 }
593
594 SysprofCallgraph *
595 _sysprof_callgraph_new_finish (GAsyncResult *result,
596 GError **error)
597 {
598 g_return_val_if_fail (G_IS_TASK (result), NULL);
599
600 return g_task_propagate_pointer (G_TASK (result), error);
601 }
602
603 static inline gpointer
604 get_augmentation (SysprofCallgraph *self,
605 gpointer *augment_location)
606 {
607 if (self->augment_size == 0)
608 return NULL;
609
610 if (self->augment_size <= INLINE_AUGMENT_SIZE)
611 return augment_location;
612
613 if (*augment_location == NULL)
614 *augment_location = g_malloc0 (self->augment_size);
615
616 return *augment_location;
617 }
618
619 gpointer
620 sysprof_callgraph_get_augment (SysprofCallgraph *self,
621 SysprofCallgraphNode *node)
622 {
623 if (node == NULL)
624 node = &self->root;
625
626 return get_augmentation (self, &node->augment[0]);
627 }
628
629 gpointer
630 sysprof_callgraph_get_summary_augment (SysprofCallgraph *self,
631 SysprofCallgraphNode *node)
632 {
633 if (node == NULL)
634 node = &self->root;
635
636 return get_augmentation (self, &node->summary->augment[0]);
637 }
638
639 gpointer
640 _sysprof_callgraph_get_symbol_augment (SysprofCallgraph *self,
641 SysprofSymbol *symbol)
642 {
643 SysprofCallgraphSummary *summary;
644
645 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (self), NULL);
646 g_return_val_if_fail (SYSPROF_IS_SYMBOL (symbol), NULL);
647
648 if ((summary = g_hash_table_lookup (self->symbol_to_summary, symbol)))
649 return get_augmentation (self, &summary->augment[0]);
650
651 return NULL;
652 }
653
654 /**
655 * sysprof_callgraph_node_parent: (skip)
656 */
657 SysprofCallgraphNode *
658 sysprof_callgraph_node_parent (SysprofCallgraphNode *node)
659 {
660 return node->parent;
661 }
662
663 /**
664 * sysprof_callgraph_list_callers:
665 * @self: a #SysprofCallgraph
666 * @symbol: a #SysprofSymbol
667 *
668 * Gets a list of #SysprofSymbol that call @symbol.
669 *
670 * Returns: (transfer full): a #GListModel of #SysprofCallgraphSymbol
671 */
672 GListModel *
673 sysprof_callgraph_list_callers (SysprofCallgraph *self,
674 SysprofSymbol *symbol)
675 {
676 SysprofCallgraphSummary *summary;
677
678 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (self), NULL);
679 g_return_val_if_fail (SYSPROF_IS_SYMBOL (symbol), NULL);
680
681 if ((summary = g_hash_table_lookup (self->symbol_to_summary, symbol)))
682 return _sysprof_callgraph_symbol_list_model_new (self, summary->callers);
683
684 return G_LIST_MODEL (g_list_store_new (SYSPROF_TYPE_CALLGRAPH_SYMBOL));
685 }
686
687 /**
688 * sysprof_callgraph_list_traceables_for_symbol:
689 * @self: a #SysprofCallgraph
690 * @symbol: a #SysprofSymbol
691 *
692 * Gets a list of all the #SysprofTraceable within the callgraph
693 * that contain @symbol.
694 *
695 * Returns: (transfer full): a #GListModel of #SysprofDocumentTraceable
696 */
697 GListModel *
698 sysprof_callgraph_list_traceables_for_symbol (SysprofCallgraph *self,
699 SysprofSymbol *symbol)
700 {
701 SysprofCallgraphSummary *summary;
702
703 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (self), NULL);
704 g_return_val_if_fail (SYSPROF_IS_SYMBOL (symbol), NULL);
705
706 if ((summary = g_hash_table_lookup (self->symbol_to_summary, symbol)))
707 return _sysprof_document_bitset_index_new (self->traceables, summary->traceables);
708
709 return G_LIST_MODEL (g_list_store_new (SYSPROF_TYPE_DOCUMENT_TRACEABLE));
710 }
711
712 /**
713 * sysprof_callgraph_list_traceables_for_symbols_matching:
714 *
715 * Returns: (transfer full): a [[email protected]] of [[email protected]]
716 */
717 GListModel *
718 sysprof_callgraph_list_traceables_for_symbols_matching (SysprofCallgraph *self,
719 const char *pattern)
720 {
721 g_autoptr(GPatternSpec) pspec = NULL;
722 g_autoptr(EggBitset) bitset = NULL;
723
724 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (self), NULL);
725
726 if (pattern == NULL || pattern[0] == 0)
727 return g_object_ref (self->traceables);
728
729 pspec = g_pattern_spec_new (pattern);
730 bitset = egg_bitset_new_empty ();
731
732 for (guint i = 0; i < self->symbols->len; i++)
733 {
734 SysprofSymbol *symbol = g_ptr_array_index (self->symbols, i);
735 const char *name = sysprof_symbol_get_name (symbol);
736
737 if (g_pattern_spec_match (pspec, strlen (name), name, NULL))
738 {
739 SysprofCallgraphSummary *summary = g_hash_table_lookup (self->symbol_to_summary, symbol);
740
741 if (summary != NULL)
742 egg_bitset_union (bitset, summary->traceables);
743 }
744 }
745
746 return _sysprof_document_bitset_index_new (self->traceables, bitset);
747 }
748
749 /**
750 * sysprof_callgraph_list_symbols:
751 * @self: a #SysprofCallgraph
752 *
753 * Gets a #GListModel of #SysprofCallgraphSymbol that an be used to
754 * display a function list and associated augmentation data.
755 *
756 * Returns: (transfer full): a #GListModel of #SysprofCallgraphSymbol
757 */
758 GListModel *
759 sysprof_callgraph_list_symbols (SysprofCallgraph *self)
760 {
761 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (self), NULL);
762
763 return _sysprof_callgraph_symbol_list_model_new (self, self->symbols);
764 }
765
766 static void
767 sysprof_callgraph_descendants_worker (GTask *task,
768 gpointer source_object,
769 gpointer task_data,
770 GCancellable *cancellable)
771 {
772 SysprofCallgraph *self = source_object;
773 SysprofSymbol *symbol = task_data;
774
775 g_assert (G_IS_TASK (task));
776 g_assert (SYSPROF_IS_CALLGRAPH (self));
777 g_assert (SYSPROF_IS_SYMBOL (symbol));
778 g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
779
780 g_task_return_pointer (task,
781 _sysprof_descendants_model_new (self, symbol),
782 g_object_unref);
783 }
784
785 void
786 sysprof_callgraph_descendants_async (SysprofCallgraph *self,
787 SysprofSymbol *symbol,
788 GCancellable *cancellable,
789 GAsyncReadyCallback callback,
790 gpointer user_data)
791 {
792 g_autoptr(GTask) task = NULL;
793
794 g_return_if_fail (SYSPROF_IS_CALLGRAPH (self));
795 g_return_if_fail (SYSPROF_IS_SYMBOL (symbol));
796 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
797
798 task = g_task_new (self, cancellable, callback, user_data);
799 g_task_set_source_tag (task, sysprof_callgraph_descendants_async);
800 g_task_set_task_data (task, g_object_ref (symbol), g_object_unref);
801 g_task_run_in_thread (task, sysprof_callgraph_descendants_worker);
802 }
803
804 /**
805 * sysprof_callgraph_descendants_finish:
806 *
807 * Returns: (transfer full):
808 */
809 GListModel *
810 sysprof_callgraph_descendants_finish (SysprofCallgraph *self,
811 GAsyncResult *result,
812 GError **error)
813 {
814 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (self), NULL);
815 g_return_val_if_fail (G_IS_TASK (result), NULL);
816
817 return g_task_propagate_pointer (G_TASK (result), error);
818 }
819
820 typedef struct _FilterByPrefix
821 {
822 SysprofDocument *document;
823 GListModel *traceables;
824 GPtrArray *prefix;
825 EggBitset *bitset;
826 guint max_results;
827 } FilterByPrefix;
828
829 static void
830 filter_by_prefix_free (FilterByPrefix *state)
831 {
832 g_clear_object (&state->document);
833 g_clear_object (&state->traceables);
834 g_clear_pointer (&state->prefix, g_ptr_array_unref);
835 g_clear_pointer (&state->bitset, egg_bitset_unref);
836 g_free (state);
837 }
838
839 static gboolean
840 traceable_has_prefix (SysprofDocument *document,
841 SysprofDocumentTraceable *traceable,
842 GPtrArray *prefix)
843 {
844 SysprofAddressContext final_context;
845 SysprofSymbol **symbols;
846 SysprofAddress *addresses;
847 guint s = 0;
848 guint stack_depth;
849 guint n_symbols;
850
851 stack_depth = sysprof_document_traceable_get_stack_depth (traceable);
852 if (stack_depth > MAX_STACK_DEPTH)
853 return FALSE;
854
855 addresses = g_alloca (sizeof (SysprofAddress) * stack_depth);
856 sysprof_document_traceable_get_stack_addresses (traceable, addresses, stack_depth);
857
858 symbols = g_alloca (sizeof (SysprofSymbol *) * stack_depth);
859 n_symbols = sysprof_document_symbolize_traceable (document, traceable, symbols, stack_depth, &final_context);
860
861 if (n_symbols < prefix->len)
862 return FALSE;
863
864 for (guint p = 0; p < prefix->len; p++)
865 {
866 SysprofSymbol *prefix_symbol = g_ptr_array_index (prefix, p);
867 gboolean found = FALSE;
868
869 for (; !found && s < n_symbols; s++)
870 found = _sysprof_symbol_equal (prefix_symbol, symbols[s]);
871
872 if (!found)
873 return FALSE;
874 }
875
876 return TRUE;
877 }
878
879 static void
880 filter_by_prefix_worker (GTask *task,
881 gpointer source_object,
882 gpointer task_data,
883 GCancellable *cancellable)
884 {
885 FilterByPrefix *state = task_data;
886 g_autoptr(EggBitset) bitset = NULL;
887 SysprofDocument *document;
888 GListModel *model;
889 GPtrArray *prefix;
890 EggBitsetIter iter;
891 guint n_results = 0;
892 guint i;
893
894 g_assert (G_IS_TASK (task));
895 g_assert (SYSPROF_IS_CALLGRAPH (source_object));
896 g_assert (state != NULL);
897 g_assert (G_IS_LIST_MODEL (state->traceables));
898 g_assert (state->prefix != NULL);
899 g_assert (state->prefix->len > 0);
900 g_assert (state->bitset != NULL);
901 g_assert (!egg_bitset_is_empty (state->bitset));
902
903 bitset = egg_bitset_new_empty ();
904
905 model = state->traceables;
906 document = state->document;
907 prefix = state->prefix;
908
909 if (egg_bitset_iter_init_first (&iter, state->bitset, &i))
910 {
911 do
912 {
913 g_autoptr(SysprofDocumentTraceable) traceable = g_list_model_get_item (model, i);
914
915 if (traceable_has_prefix (document, traceable, prefix))
916 {
917 egg_bitset_add (bitset, i);
918 n_results++;
919 }
920 }
921 while (n_results < state->max_results &&
922 egg_bitset_iter_next (&iter, &i));
923 }
924
925 g_task_return_pointer (task,
926 _sysprof_document_bitset_index_new (model, bitset),
927 g_object_unref);
928 }
929
930 static int
931 sort_by_size_asc (gconstpointer a,
932 gconstpointer b)
933 {
934 const EggBitset *bitset_a = *(const EggBitset * const *)a;
935 const EggBitset *bitset_b = *(const EggBitset * const *)a;
936 gsize size_a = egg_bitset_get_size (bitset_a);
937 gsize size_b = egg_bitset_get_size (bitset_b);
938
939 if (size_a < size_b)
940 return -1;
941
942 if (size_a > size_b)
943 return 1;
944
945 return 0;
946 }
947
948 void
949 sysprof_callgraph_list_traceables_for_node_async (SysprofCallgraph *self,
950 SysprofCallgraphNode *node,
951 GCancellable *cancellable,
952 GAsyncReadyCallback callback,
953 gpointer user_data)
954 {
955 g_autoptr(GTask) task = NULL;
956 g_autoptr(GPtrArray) prefix = NULL;
957 g_autoptr(GPtrArray) bitsets = NULL;
958 g_autoptr(EggBitset) bitset = NULL;
959 FilterByPrefix *state;
960
961 g_return_if_fail (SYSPROF_IS_CALLGRAPH (self));
962 g_return_if_fail (node != NULL);
963 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
964
965 task = g_task_new (self, cancellable, callback, user_data);
966 g_task_set_source_tag (task, sysprof_callgraph_list_traceables_for_node_async);
967
968 prefix = g_ptr_array_new ();
969 bitsets = g_ptr_array_new ();
970
971 for (; node; node = node->parent)
972 {
973 SysprofCallgraphSummary *summary = node->summary;
974 SysprofSymbol *symbol = summary->symbol;
975
976 if (symbol->kind != SYSPROF_SYMBOL_KIND_USER &&
977 symbol->kind != SYSPROF_SYMBOL_KIND_KERNEL)
978 continue;
979
980 g_ptr_array_add (bitsets, summary->traceables);
981 g_ptr_array_add (prefix, symbol);
982 }
983
984 if (prefix->len == 0)
985 {
986 g_task_return_pointer (task,
987 g_list_store_new (SYSPROF_TYPE_DOCUMENT_TRACEABLE),
988 g_object_unref);
989 return;
990 }
991
992 /* Sort the bitsets by size to shrink potential interscetions */
993 gtk_tim_sort (bitsets->pdata,
994 bitsets->len,
995 sizeof (gpointer),
996 (GCompareDataFunc)sort_by_size_asc,
997 NULL);
998 bitset = egg_bitset_copy (g_ptr_array_index (bitsets, 0));
999 for (guint i = 1; i < bitsets->len; i++)
1000 {
1001 const EggBitset *other = g_ptr_array_index (bitsets, i);
1002 egg_bitset_intersect (bitset, other);
1003 }
1004
1005 if (egg_bitset_is_empty (bitset))
1006 {
1007 g_task_return_pointer (task,
1008 g_list_store_new (SYSPROF_TYPE_DOCUMENT_TRACEABLE),
1009 g_object_unref);
1010 return;
1011 }
1012
1013 state = g_new0 (FilterByPrefix, 1);
1014 state->document = g_object_ref (self->document);
1015 state->traceables = g_object_ref (self->traceables);
1016 state->prefix = g_steal_pointer (&prefix);
1017 state->bitset = g_steal_pointer (&bitset);
1018 state->max_results = 1000;
1019
1020 g_task_set_task_data (task, state, (GDestroyNotify)filter_by_prefix_free);
1021 g_task_run_in_thread (task, filter_by_prefix_worker);
1022 }
1023
1024 /**
1025 * sysprof_callgraph_list_traceables_for_node_finish:
1026 * @self: a [[email protected]]
1027 *
1028 * Returns: (transfer full):
1029 */
1030 GListModel *
1031 sysprof_callgraph_list_traceables_for_node_finish (SysprofCallgraph *self,
1032 GAsyncResult *result,
1033 GError **error)
1034 {
1035 GListModel *ret;
1036
1037 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (self), NULL);
1038 g_return_val_if_fail (G_IS_TASK (result), NULL);
1039
1040 ret = g_task_propagate_pointer (G_TASK (result), error);
1041
1042 g_return_val_if_fail (!ret || G_IS_LIST_MODEL (ret), NULL);
1043
1044 return ret;
1045 }