Debian Codegraph @

src/libsysprof/tests/test-leak-detector.c

raw
1 /* test-leak-detector.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 <glib/gi18n.h>
24 #include <locale.h>
25
26 #include <sysprof.h>
27
28 #include "sysprof-document-private.h"
29 #include "sysprof-document-bitset-index-private.h"
30 #include "sysprof-leak-detector-private.h"
31
32 typedef struct _Augment
33 {
34 guint64 toplevel : 1;
35 guint64 size : 63;
36 guint32 total;
37 } Augment;
38
39 static char *kallsyms_path;
40 static gboolean include_threads;
41 static GEnumClass *category_class;
42 static gboolean summary_only;
43 static const GOptionEntry entries[] = {
44 { "kallsyms", 'k', 0, G_OPTION_ARG_FILENAME, &kallsyms_path, "The path to kallsyms to use for decoding", "PATH" },
45 { "threads", 't', 0, G_OPTION_ARG_NONE, &include_threads, "Include threads in the stack traces" },
46 { "summary", 's', 0, G_OPTION_ARG_NONE, &summary_only, "Only show summary" },
47 { 0 }
48 };
49
50 static void
51 print_callgraph (GListModel *model,
52 guint depth,
53 guint total)
54 {
55 guint n_items = g_list_model_get_n_items (model);
56
57 for (guint i = 0; i < n_items; i++)
58 {
59 g_autoptr(SysprofCallgraphFrame) frame = g_list_model_get_item (model, i);
60 SysprofSymbol *symbol = sysprof_callgraph_frame_get_symbol (frame);
61 Augment *aug = sysprof_callgraph_frame_get_augment (frame);
62 SysprofCallgraphCategory category = sysprof_callgraph_frame_get_category (frame);
63 const char*category_name = g_enum_get_value (category_class, category)->value_nick;
64 char tstr[16];
65
66 g_snprintf (tstr, sizeof tstr, "%.2lf%%", 100. * (aug->total / (double)total));
67
68 g_print (" [%6u] [%8s] ", aug->total, tstr);
69 for (guint j = 0; j < depth; j++)
70 g_print (" ");
71 g_print ("%s [%s]\n", sysprof_symbol_get_name (symbol), category_name);
72
73 print_callgraph (G_LIST_MODEL (frame), depth+1, total);
74 }
75 }
76
77 static void
78 summarize_cb (GObject *object,
79 GAsyncResult *result,
80 gpointer user_data)
81 {
82 SysprofCallgraphFrame *frame = SYSPROF_CALLGRAPH_FRAME (object);
83 GMainLoop *main_loop = user_data;
84 g_autoptr(GError) error = NULL;
85 g_autoptr(GListModel) model = NULL;
86 guint n_items;
87
88 model = sysprof_callgraph_frame_summarize_finish (frame, result, &error);
89 g_assert_no_error (error);
90 g_assert_nonnull (model);
91 g_assert_true (G_IS_LIST_MODEL (model));
92
93 g_print ("\n\nSummary\n");
94
95 n_items = g_list_model_get_n_items (model);
96
97 for (guint i = 0; i < n_items; i++)
98 {
99 g_autoptr(SysprofCategorySummary) summary = g_list_model_get_item (model, i);
100
101 g_assert_nonnull (summary);
102 g_assert_true (SYSPROF_IS_CATEGORY_SUMMARY (summary));
103
104 g_print ("%s: %lf\n",
105 g_enum_to_string (SYSPROF_TYPE_CALLGRAPH_CATEGORY,
106 sysprof_category_summary_get_category (summary)),
107 sysprof_category_summary_get_fraction (summary));
108 }
109
110 g_main_loop_quit (main_loop);
111 }
112
113 static void
114 callgraph_cb (GObject *object,
115 GAsyncResult *result,
116 gpointer user_data)
117 {
118 SysprofDocument *document = SYSPROF_DOCUMENT (object);
119 GMainLoop *main_loop = user_data;
120 g_autoptr(GError) error = NULL;
121 g_autoptr(SysprofCallgraph) callgraph = sysprof_document_callgraph_finish (document, result, &error);
122 g_autoptr(SysprofCallgraphFrame) root = NULL;
123 Augment *aug;
124
125 g_assert_no_error (error);
126 g_assert_true (SYSPROF_IS_CALLGRAPH (callgraph));
127
128 root = g_list_model_get_item (G_LIST_MODEL (callgraph), 0);
129 aug = sysprof_callgraph_frame_get_augment (root);
130
131 if (!summary_only)
132 {
133 g_print (" Size Percent\n");
134 print_callgraph (G_LIST_MODEL (callgraph), 0, aug->total);
135 }
136
137 sysprof_callgraph_frame_summarize_async (root, NULL, summarize_cb, main_loop);
138 }
139
140 static void
141 augment_cb (SysprofCallgraph *callgraph,
142 SysprofCallgraphNode *node,
143 SysprofDocumentFrame *frame,
144 gboolean summarize,
145 gpointer user_data)
146 {
147 Augment *aug;
148 gint64 size;
149
150 g_assert (SYSPROF_IS_CALLGRAPH (callgraph));
151 g_assert (node != NULL);
152 g_assert (SYSPROF_IS_DOCUMENT_ALLOCATION (frame));
153 g_assert (user_data == NULL);
154
155 size = sysprof_document_allocation_get_size (SYSPROF_DOCUMENT_ALLOCATION (frame));
156
157 aug = sysprof_callgraph_get_augment (callgraph, node);
158 aug->toplevel = TRUE;
159 aug->size += size;
160
161 for (; node ; node = sysprof_callgraph_node_parent (node))
162 {
163 aug = sysprof_callgraph_get_augment (callgraph, node);
164 aug->total += size;
165 }
166 }
167
168 int
169 main (int argc,
170 char *argv[])
171 {
172 g_autoptr(GOptionContext) context = g_option_context_new ("- test callgraph generation");
173 g_autoptr(GMainLoop) main_loop = g_main_loop_new (NULL, FALSE);
174 g_autoptr(SysprofDocumentLoader) loader = NULL;
175 g_autoptr(SysprofDocument) document = NULL;
176 g_autoptr(SysprofMultiSymbolizer) multi = NULL;
177 g_autoptr(GListModel) records = NULL;
178 g_autoptr(EggBitset) allocs = NULL;
179 g_autoptr(EggBitset) leaks = NULL;
180 g_autoptr(GTimer) timer = NULL;
181 g_autoptr(GError) error = NULL;
182 SysprofCallgraphFlags flags = 0;
183 double elapsed;
184
185 setlocale (LC_ALL, "");
186 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
187 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
188 textdomain (GETTEXT_PACKAGE);
189
190 g_option_context_add_main_entries (context, entries, NULL);
191 if (!g_option_context_parse (context, &argc, &argv, &error))
192 g_error ("%s", error->message);
193
194 if (argc < 2)
195 g_error ("usage: %s CAPTURE_FILE", argv[0]);
196
197 category_class = g_type_class_ref (SYSPROF_TYPE_CALLGRAPH_CATEGORY);
198
199 multi = sysprof_multi_symbolizer_new ();
200
201 if (kallsyms_path)
202 {
203 g_autoptr(GFile) kallsyms_file = g_file_new_for_path (kallsyms_path);
204 GFileInputStream *kallsyms_stream = g_file_read (kallsyms_file, NULL, NULL);
205
206 sysprof_multi_symbolizer_take (multi, sysprof_kallsyms_symbolizer_new_for_symbols (G_INPUT_STREAM (kallsyms_stream)));
207 }
208 else
209 {
210 sysprof_multi_symbolizer_take (multi, sysprof_kallsyms_symbolizer_new ());
211 }
212
213 sysprof_multi_symbolizer_take (multi, sysprof_elf_symbolizer_new ());
214 sysprof_multi_symbolizer_take (multi, sysprof_jitmap_symbolizer_new ());
215
216 loader = sysprof_document_loader_new (argv[1]);
217 sysprof_document_loader_set_symbolizer (loader, SYSPROF_SYMBOLIZER (multi));
218 if (!(document = sysprof_document_loader_load (loader, NULL, &error)))
219 g_error ("Failed to load document: %s", error->message);
220
221 allocs = _sysprof_document_get_allocations (document);
222
223 g_print ("Analyzing records for leaks...\n");
224 timer = g_timer_new ();
225 leaks = sysprof_leak_detector_detect (document, allocs);
226 elapsed = g_timer_elapsed (timer, NULL);
227 g_print ("Done in %lf seconds\n", elapsed);
228
229 records = _sysprof_document_bitset_index_new (G_LIST_MODEL (document), leaks);
230
231 if (include_threads)
232 flags |= SYSPROF_CALLGRAPH_FLAGS_INCLUDE_THREADS;
233
234 flags |= SYSPROF_CALLGRAPH_FLAGS_CATEGORIZE_FRAMES;
235
236 sysprof_document_callgraph_async (document,
237 flags,
238 records,
239 sizeof (Augment),
240 augment_cb, NULL, NULL,
241 NULL,
242 callgraph_cb,
243 main_loop);
244
245 g_main_loop_run (main_loop);
246
247 return 0;
248 }