Debian Codegraph @

src/sysprof-cat/sysprof-cat.c

raw
1 /* sysprof-cat.c
2 *
3 * Copyright 2024 Igalia S.L.
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 * Authors: Georges Basile Stavracas Neto <[email protected]>
19 *
20 * SPDX-License-Identifier: GPL-3.0-or-later
21 */
22
23 #include "config.h"
24
25 #include <libdex.h>
26
27 #include <sysprof.h>
28
29 #include "sysprof-callgraph-private.h"
30 #include "sysprof-symbol-private.h"
31
32 static GMainLoop *main_loop;
33 static int exit_code = -1;
34 static gboolean opt_skip_callgraph = FALSE;
35 static gboolean opt_skip_marks = FALSE;
36 static gboolean opt_skip_counters = FALSE;
37 static gboolean opt_skip_metadata = FALSE;
38 static const GOptionEntry options[] = {
39 { "no-callgraph", 0, 0, G_OPTION_ARG_NONE, &opt_skip_callgraph, "Do not dump the callgraph" , NULL },
40 { "no-counters", 0, 0, G_OPTION_ARG_NONE, &opt_skip_counters, "Do not dump counters" , NULL },
41 { "no-marks", 0, 0, G_OPTION_ARG_NONE, &opt_skip_marks, "Do not dump marks" , NULL },
42 { "no-metadata", 0, 0, G_OPTION_ARG_NONE, &opt_skip_metadata, "Do not dump capture metadata" , NULL },
43 { NULL }
44 };
45
46 static inline void
47 begin_document (SysprofDocument *document)
48 {
49 const SysprofTimeSpan *timespan;
50 char *aux;
51
52 g_print ("document {\n");
53 if ((aux = sysprof_document_dup_title (document)))
54 {
55 g_autofree char *markup = g_markup_escape_text (aux, -1);
56 g_print (" title: \"%s\";\n", markup);
57 g_clear_pointer (&aux, g_free);
58 }
59
60 if ((aux = sysprof_document_dup_subtitle (document)))
61 {
62 g_autofree char *markup = g_markup_escape_text (aux, -1);
63 g_print (" subtitle: \"%s\";\n", markup);
64 g_clear_pointer (&aux, g_free);
65 }
66
67 timespan = sysprof_document_get_time_span (document);
68 g_print (" timespan: %" G_GINT64_FORMAT " %" G_GINT64_FORMAT ";\n",
69 timespan->begin_nsec,
70 timespan->end_nsec);
71
72 }
73
74 typedef struct
75 {
76 guint size;
77 guint total;
78 } Augment;
79
80 static void
81 augment_cb (SysprofCallgraph *callgraph,
82 SysprofCallgraphNode *node,
83 SysprofDocumentFrame *frame,
84 gboolean summarize,
85 gpointer user_data)
86 {
87 Augment *aug;
88
89 g_assert (SYSPROF_IS_CALLGRAPH (callgraph));
90 g_assert (node != NULL);
91 g_assert (SYSPROF_IS_DOCUMENT_SAMPLE (frame));
92 g_assert (user_data == NULL);
93
94 aug = sysprof_callgraph_get_augment (callgraph, node);
95 aug->size += 1;
96 aug->total += 1;
97
98 for (SysprofCallgraphNode *iter = node->parent; iter; iter = iter->parent)
99 {
100 aug = sysprof_callgraph_get_augment (callgraph, iter);
101 aug->total += 1;
102 }
103 }
104
105 static void
106 load_callgraph_cb (GObject *object,
107 GAsyncResult *result,
108 gpointer user_data)
109 {
110 g_autoptr(DexPromise) promise = user_data;
111 SysprofCallgraph *callgraph;
112 GError *error = NULL;
113
114 if ((callgraph = sysprof_document_callgraph_finish (SYSPROF_DOCUMENT (object), result, &error)))
115 dex_promise_resolve_object (promise, callgraph);
116 else
117 dex_promise_reject (promise, error);
118 }
119
120 static void
121 print_node_recursively (SysprofCallgraphNode *node,
122 int depth)
123 {
124 const Augment *aug;
125
126 if (!node)
127 return;
128
129 aug = (const Augment *) &node->augment[0];
130
131 g_print ("%*cnode {\n", depth * 2, ' ');
132 g_print ("%*csymbol: \"%s\",\n", (depth + 1) * 2, ' ', node->summary->symbol->name);
133 g_print ("%*cself: %u,\n", (depth + 1) * 2, ' ', aug->size);
134 g_print ("%*ctotal: %u,\n", (depth + 1) * 2, ' ', aug->total);
135
136 for (SysprofCallgraphNode *child = node->children; child != NULL; child = child->next)
137 {
138 if (!child->prev)
139 g_print ("\n");
140
141 print_node_recursively (child, depth + 1);
142 }
143
144 g_print ("%*c}\n", depth * 2, ' ');
145 }
146
147 static void
148 dump_callgraph (SysprofDocument *document)
149 {
150 g_autoptr(SysprofCallgraph) callgraph = NULL;
151 g_autoptr(GListModel) traceables = NULL;
152 g_autoptr(GError) error = NULL;
153 SysprofCallgraphFlags flags = 0;
154 DexPromise *promise;
155
156 if (opt_skip_callgraph)
157 return;
158
159 traceables = sysprof_document_list_samples (document);
160
161 promise = dex_promise_new ();
162
163 sysprof_document_callgraph_async (document,
164 flags,
165 traceables,
166 sizeof (Augment),
167 augment_cb,
168 NULL, NULL,
169 dex_promise_get_cancellable (promise),
170 load_callgraph_cb,
171 dex_ref (promise));
172
173 callgraph = dex_await_object (DEX_FUTURE (promise), &error);
174
175 if (error)
176 return;
177
178 g_print ("\n");
179 g_print (" callgraph {\n");
180
181 print_node_recursively (&callgraph->root, 2);
182
183 g_print (" }\n");
184 }
185
186 static void
187 dump_counters (SysprofDocument *document)
188 {
189 g_autoptr(GListModel) counters = NULL;
190 unsigned int n_counters;
191
192 if (opt_skip_counters)
193 return;
194
195 counters = sysprof_document_list_counters (document);
196 g_assert (G_IS_LIST_MODEL (counters));
197
198 n_counters = g_list_model_get_n_items (counters);
199
200 if (n_counters == 0)
201 return;
202
203 g_print ("\n");
204 g_print (" counters {\n");
205
206 for (unsigned int i = 0; i < n_counters; i++)
207 {
208 g_autoptr(SysprofDocumentCounter) counter = NULL;
209 unsigned int n_values;
210 const char *aux;
211
212 counter = g_list_model_get_item (counters, i);
213 g_assert (SYSPROF_IS_DOCUMENT_COUNTER (counter));
214
215 g_print (" counter {\n");
216
217 if ((aux = sysprof_document_counter_get_category (counter)))
218 {
219 g_autofree char *markup = g_markup_escape_text (aux, -1);
220 g_print (" category: \"%s\";\n", markup);
221 }
222
223 if ((aux = sysprof_document_counter_get_name (counter)))
224 {
225 g_autofree char *markup = g_markup_escape_text (aux, -1);
226 g_print (" name: \"%s\";\n", markup);
227 }
228
229 if ((aux = sysprof_document_counter_get_description (counter)))
230 {
231 g_autofree char *markup = g_markup_escape_text (aux, -1);
232 g_print (" description: \"%s\";\n", markup);
233 }
234
235 n_values = sysprof_document_counter_get_n_values (counter);
236 if (n_values)
237 {
238 g_print ("\n");
239 g_print (" values {\n");
240 for (unsigned int j = 0; j < n_values; j++)
241 {
242 g_autoptr(SysprofDocumentCounterValue) value = NULL;
243 g_autofree char *formatted_value = NULL;
244
245 value = g_list_model_get_item (G_LIST_MODEL (counter), j);
246 g_assert (SYSPROF_IS_DOCUMENT_COUNTER_VALUE (value));
247
248 formatted_value = sysprof_document_counter_value_format (value);
249
250 g_print (" value {\n");
251 g_print (" time: %" G_GINT64_FORMAT ";\n", sysprof_document_counter_value_get_time (value));
252 g_print (" offset: %" G_GINT64_FORMAT ";\n", sysprof_document_counter_value_get_time (value));
253 g_print (" value: %s;\n", formatted_value);
254 g_print (" }\n");
255 }
256 g_print (" }\n");
257 }
258
259 g_print (" }\n");
260 }
261
262 g_print (" }\n");
263 }
264
265 static void
266 dump_marks (SysprofDocument *document)
267 {
268 g_autoptr(GListModel) groups = NULL;
269 unsigned int n_groups;
270
271 if (opt_skip_marks)
272 return;
273
274 groups = sysprof_document_catalog_marks (document);
275 g_assert (G_IS_LIST_MODEL (groups));
276
277 n_groups = g_list_model_get_n_items (groups);
278
279 if (n_groups == 0)
280 return;
281
282 g_print ("\n");
283 g_print (" marks {\n");
284
285 for (unsigned int i = 0; i < n_groups; i++)
286 {
287 g_autoptr(GListModel) catalogs = NULL;
288 unsigned int n_catalogs;
289
290 catalogs = g_list_model_get_item (groups, i);
291 g_assert (G_IS_LIST_MODEL (catalogs));
292
293 g_print ("\n");
294 g_print (" group {\n");
295
296 n_catalogs = g_list_model_get_n_items (catalogs);
297 for (unsigned int j = 0; j < n_catalogs; j++)
298 {
299 g_autoptr(SysprofMarkCatalog) catalog = NULL;
300 unsigned int n_marks;
301 const char *aux;
302
303 catalog = g_list_model_get_item (catalogs, j);
304 g_assert (SYSPROF_IS_MARK_CATALOG (catalog));
305
306 if (j == 0 &&
307 (aux = sysprof_mark_catalog_get_group (catalog)))
308 {
309 g_autofree char *markup = g_markup_escape_text (aux, -1);
310 g_print (" name: \"%s\";\n", markup);
311 g_print ("\n");
312 }
313
314 n_marks = g_list_model_get_n_items (G_LIST_MODEL (catalog));
315 for (unsigned int k = 0; k < n_marks; k++)
316 {
317 g_autoptr(SysprofDocumentMark) mark = NULL;
318
319 mark = g_list_model_get_item (G_LIST_MODEL (catalog), k);
320 g_assert (SYSPROF_IS_DOCUMENT_MARK (mark));
321
322 g_print (" mark {\n");
323
324 if ((aux = sysprof_document_mark_get_name (mark)))
325 {
326 g_autofree char *markup = g_markup_escape_text (aux, -1);
327 g_print (" name: \"%s\";\n", markup);
328 }
329
330 if ((aux = sysprof_document_mark_get_message (mark)))
331 {
332 g_autofree char *markup = g_markup_escape_text (aux, -1);
333 g_print (" message: \"%s\";\n", markup);
334 }
335
336 g_print (" duration: %" G_GINT64_FORMAT ";\n", sysprof_document_mark_get_duration (mark));
337 g_print (" end-time: %" G_GINT64_FORMAT ";\n", sysprof_document_mark_get_end_time (mark));
338 g_print (" }\n");
339 }
340 }
341
342 g_print (" }\n");
343 }
344
345 g_print (" }\n");
346 }
347
348 static void
349 dump_metadata (SysprofDocument *document)
350 {
351 g_autoptr(GListModel) metadatas = NULL;
352 unsigned int n_metadatas;
353
354 if (opt_skip_metadata)
355 return;
356
357 metadatas = sysprof_document_list_metadata (document);
358 g_assert (G_IS_LIST_MODEL (metadatas));
359
360 n_metadatas = g_list_model_get_n_items (metadatas);
361
362 if (metadatas == 0)
363 return;
364
365 g_print ("\n");
366 g_print (" metadata {\n");
367
368 for (unsigned int i = 0; i < n_metadatas; i++)
369 {
370 g_autoptr(SysprofDocumentMetadata) metadata = NULL;
371 g_autofree char *value = NULL;
372 g_autofree char *id = NULL;
373
374 metadata = g_list_model_get_item (metadatas, i);
375 g_assert (SYSPROF_IS_DOCUMENT_METADATA (metadata));
376
377 id = g_markup_escape_text (sysprof_document_metadata_get_id (metadata), -1);
378 value = g_markup_escape_text (sysprof_document_metadata_get_value (metadata), -1);
379
380 g_print (" \"%s\": \"%s\";\n", id, value);
381 }
382
383 g_print (" }\n");
384 }
385
386 static inline void
387 end_document (SysprofDocument *document)
388 {
389 g_print ("}\n");
390 }
391
392 static void
393 load_document_cb (GObject *object,
394 GAsyncResult *result,
395 gpointer user_data)
396 {
397 g_autoptr(DexPromise) promise = user_data;
398 SysprofDocument *document;
399 GError *error = NULL;
400
401 if ((document = sysprof_document_loader_load_finish (SYSPROF_DOCUMENT_LOADER (object), result, &error)))
402 dex_promise_resolve_object (promise, document);
403 else
404 dex_promise_reject (promise, error);
405 }
406
407 static DexFuture *
408 load_document (SysprofDocumentLoader *loader)
409 {
410 DexPromise *promise = dex_promise_new ();
411 sysprof_document_loader_load_async (loader, NULL, load_document_cb, dex_ref (promise));
412 return DEX_FUTURE (promise);
413 }
414
415 static DexFuture *
416 sysprof_cat_fiber (gpointer data)
417 {
418 g_autoptr(SysprofDocumentLoader) loader = NULL;
419 g_autoptr(SysprofDocument) document = NULL;
420 g_autoptr(GError) error = NULL;
421 const char *filename = data;
422
423 g_assert (filename != NULL);
424
425 loader = sysprof_document_loader_new (filename);
426
427 if (!(document = dex_await_object (load_document (loader), &error)))
428 return dex_future_new_for_error (g_steal_pointer (&error));
429
430 begin_document (document);
431 {
432 dump_metadata (document);
433 dump_callgraph (document);
434 dump_marks (document);
435 dump_counters (document);
436 }
437 end_document (document);
438
439 exit_code = EXIT_SUCCESS;
440
441 return dex_future_new_for_boolean (TRUE);
442 }
443
444 static DexFuture *
445 sysprof_cat_finally_cb (DexFuture *future,
446 gpointer user_data)
447 {
448 g_autoptr(GError) error = NULL;
449
450 if (!dex_await (dex_ref (future), &error))
451 {
452 g_printerr ("Error: %s\n", error->message);
453 exit_code = EXIT_FAILURE;
454 }
455
456 g_main_loop_quit (main_loop);
457
458 return NULL;
459 }
460
461 int
462 main (int argc,
463 char *argv[])
464 {
465 g_autoptr(GOptionContext) context = NULL;
466 g_autoptr(GError) error = NULL;
467
468 sysprof_clock_init ();
469 dex_init ();
470
471 g_set_prgname ("sysprof-cat");
472 g_set_application_name ("sysprof-cat");
473
474 main_loop = g_main_loop_new (NULL, FALSE);
475
476 context = g_option_context_new ("CAPTURE -- Dump the contents of a capture");
477 g_option_context_add_main_entries (context, options, NULL);
478
479 if (!g_option_context_parse (context, &argc, &argv, &error))
480 {
481 g_printerr ("%s\n", error->message);
482 return EXIT_FAILURE;
483 }
484
485 if (argc < 2)
486 {
487 g_printerr ("usage: %s CAPTURE\n", argv[0]);
488 return EXIT_FAILURE;
489 }
490
491 dex_future_disown (
492 dex_future_finally (
493 dex_scheduler_spawn (NULL, 0,
494 sysprof_cat_fiber,
495 g_strdup (argv[1]),
496 g_free),
497 sysprof_cat_finally_cb,
498 NULL, NULL));
499
500 if (exit_code == -1)
501 g_main_loop_run (main_loop);
502
503 return exit_code;
504 }