Debian Codegraph @

src/libsysprof/tests/test-list-functions-by-weight.c

raw
1 /* test-list-functions-by-weight.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 <sysprof.h>
24
25 #include "sysprof-callgraph-private.h"
26
27 typedef struct _Augment
28 {
29 guint64 count;
30 } Augment;
31
32 static gboolean
33 seen_symbol (const SysprofCallgraphNode *node,
34 const SysprofCallgraphNode *toplevel)
35 {
36 for (const SysprofCallgraphNode *iter = toplevel;
37 iter != node;
38 iter = iter->parent)
39 if (iter->summary == node->summary)
40 return TRUE;
41 return FALSE;
42 }
43
44 static void
45 augment_cb (SysprofCallgraph *callgraph,
46 SysprofCallgraphNode *node,
47 SysprofDocumentFrame *frame,
48 gboolean summarize,
49 gpointer user_data)
50 {
51 Augment *aug = sysprof_callgraph_get_summary_augment (callgraph, node);
52
53 aug->count++;
54
55 for (SysprofCallgraphNode *iter = node->parent; iter; iter = iter->parent)
56 {
57 if (!seen_symbol (iter, node))
58 {
59 aug = sysprof_callgraph_get_summary_augment (callgraph, iter);
60 aug->count++;
61 }
62 }
63 }
64
65 static void
66 callgraph_cb (GObject *object,
67 GAsyncResult *result,
68 gpointer user_data)
69 {
70 SysprofDocument *document = SYSPROF_DOCUMENT (object);
71 GMainLoop *main_loop = user_data;
72 g_autoptr(GError) error = NULL;
73 g_autoptr(SysprofCallgraph) callgraph = sysprof_document_callgraph_finish (document, result, &error);
74 g_autoptr(GListModel) symbols = NULL;
75 guint n_items;
76
77 g_assert_no_error (error);
78 g_assert_true (SYSPROF_IS_CALLGRAPH (callgraph));
79
80 symbols = sysprof_callgraph_list_symbols (callgraph);
81 n_items = g_list_model_get_n_items (symbols);
82
83 for (guint i = 0; i < n_items; i++)
84 {
85 g_autoptr(SysprofCallgraphSymbol) symbol = g_list_model_get_item (symbols, i);
86 Augment *aug = sysprof_callgraph_symbol_get_summary_augment (symbol);
87 const char *name = sysprof_symbol_get_name (sysprof_callgraph_symbol_get_symbol (symbol));
88
89 g_print ("%6"G_GINT64_FORMAT": %s\n", aug->count, name);
90 }
91
92 g_main_loop_quit (main_loop);
93 }
94
95 int
96 main (int argc,
97 char *argv[])
98 {
99 g_autoptr(GOptionContext) context = g_option_context_new ("- list functions sorted by weight");
100 g_autoptr(GMainLoop) main_loop = g_main_loop_new (NULL, FALSE);
101 g_autoptr(SysprofDocumentLoader) loader = NULL;
102 g_autoptr(SysprofDocument) document = NULL;
103 g_autoptr(GListModel) samples = NULL;
104 g_autoptr(GError) error = NULL;
105 SysprofCallgraphFlags flags = 0;
106
107 if (!g_option_context_parse (context, &argc, &argv, &error))
108 g_error ("%s", error->message);
109
110 if (argc < 2)
111 {
112 g_printerr ("usage: %s CAPTURE_FILE", argv[0]);
113 return 1;
114 }
115
116 loader = sysprof_document_loader_new (argv[1]);
117 if (!(document = sysprof_document_loader_load (loader, NULL, &error)))
118 g_error ("Failed to load document: %s", error->message);
119
120 samples = sysprof_document_list_samples (document);
121
122 sysprof_document_callgraph_async (document,
123 flags,
124 samples,
125 sizeof (Augment),
126 augment_cb, NULL, NULL,
127 NULL,
128 callgraph_cb,
129 main_loop);
130
131 g_main_loop_run (main_loop);
132
133 return 0;
134 }