Debian Codegraph @

src/libsysprof/sysprof-document.c

raw
1 /* sysprof-document.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 <fcntl.h>
24
25 #include <glib/gstdio.h>
26 #include <glib/gi18n.h>
27
28 #include <libdex.h>
29
30 #include "timsort/gtktimsortprivate.h"
31
32 #include "sysprof-document-private.h"
33
34 #include "sysprof-bundled-symbolizer-private.h"
35 #include "sysprof-callgraph-private.h"
36 #include "sysprof-cpu-info-private.h"
37 #include "sysprof-document-allocation.h"
38 #include "sysprof-document-bitset-index-private.h"
39 #include "sysprof-document-counter-private.h"
40 #include "sysprof-document-ctrdef.h"
41 #include "sysprof-document-ctrset.h"
42 #include "sysprof-document-file-chunk.h"
43 #include "sysprof-document-file-private.h"
44 #include "sysprof-document-frame-private.h"
45 #include "sysprof-document-jitmap.h"
46 #include "sysprof-document-mmap.h"
47 #include "sysprof-document-overlay.h"
48 #include "sysprof-document-process-private.h"
49 #include "sysprof-document-sample.h"
50 #include "sysprof-document-symbols-private.h"
51 #include "sysprof-mark-catalog-private.h"
52 #include "sysprof-mount-private.h"
53 #include "sysprof-mount-device-private.h"
54 #include "sysprof-mount-namespace-private.h"
55 #include "sysprof-process-info-private.h"
56 #include "sysprof-strings-private.h"
57 #include "sysprof-symbol-private.h"
58 #include "sysprof-symbolizer-private.h"
59
60 #include "line-reader-private.h"
61
62 #define MAX_STACK_DEPTH 128
63
64 struct _SysprofDocument
65 {
66 GObject parent_instance;
67
68 SysprofTimeSpan time_span;
69
70 char *title;
71
72 GArray *frames;
73 GMappedFile *mapped_file;
74 const guint8 *base;
75
76 GListStore *cpu_info;
77
78 GListStore *counters;
79 GHashTable *counter_id_to_values;
80
81 SysprofStrings *strings;
82 SysprofSymbol *missing_process;
83
84 EggBitset *allocations;
85 EggBitset *ctrdefs;
86 EggBitset *ctrsets;
87 EggBitset *dbus_messages;
88 EggBitset *file_chunks;
89 EggBitset *jitmaps;
90 EggBitset *logs;
91 EggBitset *marks;
92 EggBitset *metadata;
93 EggBitset *mmaps;
94 EggBitset *overlays;
95 EggBitset *pids;
96 EggBitset *processes;
97 EggBitset *samples;
98 EggBitset *samples_with_context_switch;
99 EggBitset *traceables;
100
101 GHashTable *files_first_position;
102 GHashTable *pid_to_process_info;
103 GHashTable *tid_to_symbol;
104 GHashTable *mark_groups;
105
106 SysprofMountNamespace *mount_namespace;
107
108 SysprofDocumentSymbols *symbols;
109
110 guint busy_count;
111
112 SysprofCaptureFileHeader header;
113 guint needs_swap : 1;
114 };
115
116 enum {
117 PROP_0,
118 PROP_ALLOCATIONS,
119 PROP_BUSY,
120 PROP_COUNTERS,
121 PROP_CPU_INFO,
122 PROP_DBUS_MESSAGES,
123 PROP_FILES,
124 PROP_LOGS,
125 PROP_MARKS,
126 PROP_MARKS_CATALOG,
127 PROP_METADATA,
128 PROP_PROCESSES,
129 PROP_SAMPLES,
130 PROP_TIME_SPAN,
131 PROP_TITLE,
132 PROP_SUBTITLE,
133 N_PROPS
134 };
135
136 static GParamSpec *properties[N_PROPS];
137
138 static inline guint16
139 swap_uint16 (gboolean needs_swap,
140 guint16 value)
141 {
142 if (!needs_swap)
143 return value;
144
145 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
146 return GUINT16_FROM_BE (value);
147 #else
148 return GUINT16_FROM_LE (value);
149 #endif
150 }
151
152 static inline guint32
153 swap_uint32 (gboolean needs_swap,
154 guint32 value)
155 {
156 if (!needs_swap)
157 return value;
158
159 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
160 return GUINT32_FROM_BE (value);
161 #else
162 return GUINT32_FROM_LE (value);
163 #endif
164 }
165
166 static inline int
167 swap_int32 (gboolean needs_swap,
168 int value)
169 {
170 if (!needs_swap)
171 return value;
172
173 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
174 return GINT32_FROM_BE (value);
175 #else
176 return GINT32_FROM_LE (value);
177 #endif
178 }
179
180 static inline gint64
181 swap_int64 (gboolean needs_swap,
182 gint64 value)
183 {
184 if G_LIKELY (!needs_swap)
185 return value;
186
187 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
188 return GINT64_FROM_BE (value);
189 #else
190 return GINT64_FROM_LE (value);
191 #endif
192 }
193
194 static inline guint64
195 swap_uint64 (gboolean needs_swap,
196 guint64 value)
197 {
198 if G_LIKELY (!needs_swap)
199 return value;
200
201 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
202 return GUINT64_FROM_BE (value);
203 #else
204 return GUINT64_FROM_LE (value);
205 #endif
206 }
207
208 static GType
209 sysprof_document_get_item_type (GListModel *model)
210 {
211 return SYSPROF_TYPE_DOCUMENT_FRAME;
212 }
213
214 static guint
215 sysprof_document_get_n_items (GListModel *model)
216 {
217 return SYSPROF_DOCUMENT (model)->frames->len;
218 }
219
220 static gpointer
221 sysprof_document_get_item (GListModel *model,
222 guint position)
223 {
224 SysprofDocument *self = SYSPROF_DOCUMENT (model);
225 SysprofDocumentFramePointer *ptr;
226 SysprofDocumentFrame *ret;
227
228 if (position >= self->frames->len)
229 return NULL;
230
231 ptr = &g_array_index (self->frames, SysprofDocumentFramePointer, position);
232 ret = _sysprof_document_frame_new (self->mapped_file,
233 (gconstpointer)&self->base[ptr->offset],
234 ptr->length,
235 self->needs_swap,
236 self->header.time,
237 self->header.end_time);
238
239 /* Annotate processes with pre-calculated info */
240 if (SYSPROF_IS_DOCUMENT_PROCESS (ret))
241 {
242 int pid = sysprof_document_frame_get_pid (ret);
243 SysprofProcessInfo *process_info = g_hash_table_lookup (self->pid_to_process_info, GINT_TO_POINTER (pid));
244
245 if (process_info != NULL)
246 _sysprof_document_process_set_info (SYSPROF_DOCUMENT_PROCESS (ret), process_info);
247 }
248
249 return ret;
250 }
251
252 static void
253 list_model_iface_init (GListModelInterface *iface)
254 {
255 iface->get_item_type = sysprof_document_get_item_type;
256 iface->get_n_items = sysprof_document_get_n_items;
257 iface->get_item = sysprof_document_get_item;
258 }
259
260 G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDocument, sysprof_document, G_TYPE_OBJECT,
261 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
262
263 static void
264 sysprof_document_mark_busy (SysprofDocument *self)
265 {
266 g_assert (SYSPROF_IS_DOCUMENT (self));
267
268 if (++self->busy_count == 1)
269 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BUSY]);
270 }
271
272 static void
273 sysprof_document_unmark_busy (SysprofDocument *self)
274 {
275 g_assert (SYSPROF_IS_DOCUMENT (self));
276
277 if (--self->busy_count == 0)
278 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_BUSY]);
279 }
280
281 static void
282 sysprof_document_mark_busy_for_task (SysprofDocument *self,
283 GTask *task)
284 {
285 g_assert (SYSPROF_IS_DOCUMENT (self));
286 g_assert (G_IS_TASK (task));
287
288 g_signal_connect_object (task,
289 "notify::completed",
290 G_CALLBACK (sysprof_document_unmark_busy),
291 self,
292 G_CONNECT_SWAPPED);
293
294 sysprof_document_mark_busy (self);
295 }
296
297
298 EggBitset *
299 _sysprof_document_traceables (SysprofDocument *self)
300 {
301 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
302
303 return self->traceables;
304 }
305
306 SysprofProcessInfo *
307 _sysprof_document_process_info (SysprofDocument *self,
308 int pid,
309 gboolean may_create)
310 {
311 SysprofProcessInfo *process_info;
312
313 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
314
315 if (pid < 0)
316 return NULL;
317
318 process_info = g_hash_table_lookup (self->pid_to_process_info, GINT_TO_POINTER (pid));
319
320 if (process_info == NULL && may_create)
321 {
322 process_info = sysprof_process_info_new (sysprof_mount_namespace_copy (self->mount_namespace), pid);
323 g_hash_table_insert (self->pid_to_process_info, GINT_TO_POINTER (pid), process_info);
324 }
325
326 return process_info;
327 }
328
329 static void
330 decode_space (gchar **str)
331 {
332 /* Replace encoded space "\040" with ' ' */
333 if (strstr (*str, "\\040"))
334 {
335 g_auto(GStrv) parts = g_strsplit (*str, "\\040", 0);
336 g_free (*str);
337 *str = g_strjoinv (" ", parts);
338 }
339 }
340
341 static inline gboolean
342 has_null_byte (const char *str,
343 const char *endptr)
344 {
345 for (const char *c = str; c < endptr; c++)
346 {
347 if (*c == '\0')
348 return TRUE;
349 }
350
351 return FALSE;
352 }
353
354 static void
355 sysprof_document_finalize (GObject *object)
356 {
357 SysprofDocument *self = (SysprofDocument *)object;
358
359 g_clear_pointer (&self->strings, sysprof_strings_unref);
360
361 g_clear_pointer (&self->title, g_free);
362
363 g_clear_pointer (&self->pid_to_process_info, g_hash_table_unref);
364 g_clear_pointer (&self->tid_to_symbol, g_hash_table_unref);
365 g_clear_pointer (&self->mapped_file, g_mapped_file_unref);
366 g_clear_pointer (&self->frames, g_array_unref);
367
368 g_clear_pointer (&self->allocations, egg_bitset_unref);
369 g_clear_pointer (&self->ctrdefs, egg_bitset_unref);
370 g_clear_pointer (&self->ctrsets, egg_bitset_unref);
371 g_clear_pointer (&self->dbus_messages, egg_bitset_unref);
372 g_clear_pointer (&self->marks, egg_bitset_unref);
373 g_clear_pointer (&self->metadata, egg_bitset_unref);
374 g_clear_pointer (&self->file_chunks, egg_bitset_unref);
375 g_clear_pointer (&self->jitmaps, egg_bitset_unref);
376 g_clear_pointer (&self->logs, egg_bitset_unref);
377 g_clear_pointer (&self->mmaps, egg_bitset_unref);
378 g_clear_pointer (&self->overlays, egg_bitset_unref);
379 g_clear_pointer (&self->pids, egg_bitset_unref);
380 g_clear_pointer (&self->processes, egg_bitset_unref);
381 g_clear_pointer (&self->samples, egg_bitset_unref);
382 g_clear_pointer (&self->samples_with_context_switch, egg_bitset_unref);
383 g_clear_pointer (&self->traceables, egg_bitset_unref);
384
385 g_clear_pointer (&self->mark_groups, g_hash_table_unref);
386
387 g_clear_object (&self->counters);
388 g_clear_pointer (&self->counter_id_to_values, g_hash_table_unref);
389
390 g_clear_object (&self->cpu_info);
391
392 g_clear_object (&self->mount_namespace);
393 g_clear_object (&self->symbols);
394
395 g_clear_pointer (&self->files_first_position, g_hash_table_unref);
396
397 G_OBJECT_CLASS (sysprof_document_parent_class)->finalize (object);
398 }
399
400 static void
401 sysprof_document_get_property (GObject *object,
402 guint prop_id,
403 GValue *value,
404 GParamSpec *pspec)
405 {
406 SysprofDocument *self = SYSPROF_DOCUMENT (object);
407
408 switch (prop_id)
409 {
410 case PROP_ALLOCATIONS:
411 g_value_take_object (value, sysprof_document_list_allocations (self));
412 break;
413
414 case PROP_BUSY:
415 g_value_set_boolean (value, sysprof_document_get_busy (self));
416 break;
417
418 case PROP_COUNTERS:
419 g_value_take_object (value, sysprof_document_list_counters (self));
420 break;
421
422 case PROP_CPU_INFO:
423 g_value_take_object (value, sysprof_document_list_cpu_info (self));
424 break;
425
426 case PROP_DBUS_MESSAGES:
427 g_value_take_object (value, sysprof_document_list_dbus_messages (self));
428 break;
429
430 case PROP_FILES:
431 g_value_take_object (value, sysprof_document_list_files (self));
432 break;
433
434 case PROP_LOGS:
435 g_value_take_object (value, sysprof_document_list_logs (self));
436 break;
437
438 case PROP_MARKS:
439 g_value_take_object (value, sysprof_document_list_marks (self));
440 break;
441
442 case PROP_MARKS_CATALOG:
443 g_value_take_object (value, sysprof_document_catalog_marks (self));
444 break;
445
446 case PROP_METADATA:
447 g_value_take_object (value, sysprof_document_list_metadata (self));
448 break;
449
450 case PROP_PROCESSES:
451 g_value_take_object (value, sysprof_document_list_processes (self));
452 break;
453
454 case PROP_SAMPLES:
455 g_value_take_object (value, sysprof_document_list_samples (self));
456 break;
457
458 case PROP_TIME_SPAN:
459 g_value_set_boxed (value, sysprof_document_get_time_span (self));
460 break;
461
462 case PROP_TITLE:
463 g_value_take_string (value, sysprof_document_dup_title (self));
464 break;
465
466 case PROP_SUBTITLE:
467 g_value_take_string (value, sysprof_document_dup_subtitle (self));
468 break;
469
470 default:
471 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
472 }
473 }
474
475 static void
476 sysprof_document_class_init (SysprofDocumentClass *klass)
477 {
478 GObjectClass *object_class = G_OBJECT_CLASS (klass);
479
480 object_class->finalize = sysprof_document_finalize;
481 object_class->get_property = sysprof_document_get_property;
482
483 properties [PROP_ALLOCATIONS] =
484 g_param_spec_object ("allocations", NULL, NULL,
485 G_TYPE_LIST_MODEL,
486 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
487
488 properties[PROP_BUSY] =
489 g_param_spec_boolean ("busy", NULL, NULL,
490 FALSE,
491 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
492
493 properties [PROP_COUNTERS] =
494 g_param_spec_object ("counters", NULL, NULL,
495 G_TYPE_LIST_MODEL,
496 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
497
498 properties [PROP_CPU_INFO] =
499 g_param_spec_object ("cpu-info", NULL, NULL,
500 G_TYPE_LIST_MODEL,
501 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
502
503 properties [PROP_DBUS_MESSAGES] =
504 g_param_spec_object ("dbus-messages", NULL, NULL,
505 G_TYPE_LIST_MODEL,
506 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
507
508 properties [PROP_FILES] =
509 g_param_spec_object ("files", NULL, NULL,
510 G_TYPE_LIST_MODEL,
511 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
512
513 properties [PROP_LOGS] =
514 g_param_spec_object ("logs", NULL, NULL,
515 G_TYPE_LIST_MODEL,
516 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
517
518 properties [PROP_MARKS] =
519 g_param_spec_object ("marks", NULL, NULL,
520 G_TYPE_LIST_MODEL,
521 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
522
523 properties [PROP_MARKS_CATALOG] =
524 g_param_spec_object ("marks-catalog", NULL, NULL,
525 G_TYPE_LIST_MODEL,
526 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
527
528 properties [PROP_METADATA] =
529 g_param_spec_object ("metadata", NULL, NULL,
530 G_TYPE_LIST_MODEL,
531 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
532
533 properties [PROP_PROCESSES] =
534 g_param_spec_object ("processes", NULL, NULL,
535 G_TYPE_LIST_MODEL,
536 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
537
538 properties [PROP_SAMPLES] =
539 g_param_spec_object ("samples", NULL, NULL,
540 G_TYPE_LIST_MODEL,
541 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
542
543 properties [PROP_TIME_SPAN] =
544 g_param_spec_boxed ("time-span", NULL, NULL,
545 SYSPROF_TYPE_TIME_SPAN,
546 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
547
548 properties [PROP_TITLE] =
549 g_param_spec_string ("title", NULL, NULL,
550 NULL,
551 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
552
553 properties [PROP_SUBTITLE] =
554 g_param_spec_string ("subtitle", NULL, NULL,
555 NULL,
556 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
557
558 g_object_class_install_properties (object_class, N_PROPS, properties);
559 }
560
561 static void
562 sysprof_document_init (SysprofDocument *self)
563 {
564 self->strings = sysprof_strings_new ();
565
566 self->frames = g_array_new (FALSE, FALSE, sizeof (SysprofDocumentFramePointer));
567
568 self->cpu_info = g_list_store_new (SYSPROF_TYPE_CPU_INFO);
569
570 self->missing_process = _sysprof_symbol_new (g_ref_string_new (_("Unknown Process")),
571 NULL, NULL, 0, 0, SYSPROF_SYMBOL_KIND_UNWINDABLE);
572
573 self->counters = g_list_store_new (SYSPROF_TYPE_DOCUMENT_COUNTER);
574 self->counter_id_to_values = g_hash_table_new_full (NULL, NULL, NULL,
575 (GDestroyNotify)g_array_unref);
576
577 self->allocations = egg_bitset_new_empty ();
578 self->ctrdefs = egg_bitset_new_empty ();
579 self->ctrsets = egg_bitset_new_empty ();
580 self->dbus_messages = egg_bitset_new_empty ();
581 self->marks = egg_bitset_new_empty ();
582 self->metadata = egg_bitset_new_empty ();
583 self->file_chunks = egg_bitset_new_empty ();
584 self->jitmaps = egg_bitset_new_empty ();
585 self->logs = egg_bitset_new_empty ();
586 self->mmaps = egg_bitset_new_empty ();
587 self->overlays = egg_bitset_new_empty ();
588 self->pids = egg_bitset_new_empty ();
589 self->processes = egg_bitset_new_empty ();
590 self->samples = egg_bitset_new_empty ();
591 self->samples_with_context_switch = egg_bitset_new_empty ();
592 self->traceables = egg_bitset_new_empty ();
593
594 self->files_first_position = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
595 self->pid_to_process_info = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)sysprof_process_info_unref);
596 self->tid_to_symbol = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
597 self->mark_groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
598
599 self->mount_namespace = sysprof_mount_namespace_new ();
600 }
601
602 static void
603 sysprof_document_load_memory_maps (SysprofDocument *self)
604 {
605 EggBitsetIter iter;
606 guint i;
607
608 g_assert (SYSPROF_IS_DOCUMENT (self));
609
610 if (egg_bitset_iter_init_first (&iter, self->mmaps, &i))
611 {
612 do
613 {
614 g_autoptr(SysprofDocumentMmap) map = sysprof_document_get_item ((GListModel *)self, i);
615 int pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (map));
616 SysprofProcessInfo *process_info = _sysprof_document_process_info (self, pid, TRUE);
617
618 sysprof_address_layout_take (process_info->address_layout, g_steal_pointer (&map));
619 }
620 while (egg_bitset_iter_next (&iter, &i));
621 }
622 }
623
624 static void
625 sysprof_document_load_mounts (SysprofDocument *self)
626 {
627 g_autoptr(SysprofDocumentFile) file = NULL;
628 g_autoptr(GBytes) bytes = NULL;
629 LineReader reader;
630 GHashTableIter iter;
631 const char *contents;
632 gsize contents_len;
633 gsize line_len;
634 char *line;
635
636 g_assert (SYSPROF_IS_DOCUMENT (self));
637
638 if (!(file = sysprof_document_lookup_file (self, "/proc/mounts")) ||
639 !(bytes = sysprof_document_file_dup_bytes (file)))
640 return;
641
642 contents = g_bytes_get_data (bytes, &contents_len);
643
644 g_assert (contents != NULL);
645 g_assert (contents[contents_len] == 0);
646
647 line_reader_init (&reader, (char *)contents, contents_len);
648 while ((line = line_reader_next (&reader, &line_len)))
649 {
650 g_autoptr(SysprofMountDevice) mount_device = NULL;
651 SysprofProcessInfo *process_info = NULL;
652 g_autofree char *subvol = NULL;
653 g_auto(GStrv) parts = NULL;
654 const char *filesystem;
655 const char *mountpoint;
656 const char *device;
657 const char *options;
658
659 line[line_len] = 0;
660
661 parts = g_strsplit (line, " ", 5);
662
663 /* Field 0: device
664 * Field 1: mountpoint
665 * Field 2: filesystem
666 * Field 3: Options
667 * .. Ignored ..
668 */
669 if (g_strv_length (parts) != 5)
670 continue;
671
672 for (guint j = 0; parts[j]; j++)
673 decode_space (&parts[j]);
674
675 device = parts[0];
676 mountpoint = parts[1];
677 filesystem = parts[2];
678 options = parts[3];
679
680 if (g_strcmp0 (filesystem, "btrfs") == 0)
681 {
682 g_auto(GStrv) opts = g_strsplit (options, ",", 0);
683
684 for (guint k = 0; opts[k]; k++)
685 {
686 if (g_str_has_prefix (opts[k], "subvol="))
687 {
688 subvol = g_strdup (opts[k] + strlen ("subvol="));
689 break;
690 }
691 }
692 }
693
694 mount_device =
695 sysprof_mount_device_new (sysprof_strings_get (self->strings, device),
696 sysprof_strings_get (self->strings, mountpoint),
697 sysprof_strings_get (self->strings, subvol));
698
699 g_hash_table_iter_init (&iter, self->pid_to_process_info);
700 while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&process_info))
701 sysprof_mount_namespace_add_device (process_info->mount_namespace,
702 g_object_ref (mount_device));
703
704 sysprof_mount_namespace_add_device (self->mount_namespace, g_object_ref (mount_device));
705 }
706 }
707
708 static void
709 sysprof_document_load_mountinfo (SysprofDocument *self,
710 int pid,
711 GBytes *bytes)
712 {
713 SysprofProcessInfo *process_info;
714 const char *contents;
715 LineReader reader;
716 gsize contents_len;
717 gsize line_len;
718 char *line;
719
720 g_assert (SYSPROF_IS_DOCUMENT (self));
721 g_assert (bytes != NULL);
722
723 contents = g_bytes_get_data (bytes, &contents_len);
724
725 g_assert (contents != NULL);
726 g_assert (contents[contents_len] == 0);
727
728 process_info = _sysprof_document_process_info (self, pid, TRUE);
729
730 g_assert (process_info != NULL);
731 g_assert (process_info->mount_namespace != NULL);
732
733 line_reader_init (&reader, (char *)contents, contents_len);
734 while ((line = line_reader_next (&reader, &line_len)))
735 {
736 g_autoptr(SysprofMount) mount = NULL;
737
738 line[line_len] = 0;
739
740 if ((mount = _sysprof_mount_new_for_mountinfo (self->strings, line)))
741 sysprof_mount_namespace_add_mount (process_info->mount_namespace, g_steal_pointer (&mount));
742 }
743 }
744
745 static void
746 sysprof_document_load_mountinfos (SysprofDocument *self)
747 {
748 EggBitsetIter iter;
749 guint pid;
750
751 g_assert (SYSPROF_IS_DOCUMENT (self));
752
753 if (egg_bitset_iter_init_first (&iter, self->pids, &pid))
754 {
755 do
756 {
757 g_autofree char *path = g_strdup_printf ("/proc/%u/mountinfo", pid);
758 g_autoptr(SysprofDocumentFile) file = sysprof_document_lookup_file (self, path);
759
760 if (file != NULL)
761 {
762 g_autoptr(GBytes) bytes = sysprof_document_file_dup_bytes (file);
763
764 sysprof_document_load_mountinfo (self, pid, bytes);
765 }
766 }
767 while (egg_bitset_iter_next (&iter, &pid));
768 }
769 }
770
771 static void
772 sysprof_document_load_overlays (SysprofDocument *self)
773 {
774 EggBitsetIter iter;
775 guint i;
776
777 g_assert (SYSPROF_IS_DOCUMENT (self));
778
779 if (egg_bitset_iter_init_first (&iter, self->overlays, &i))
780 {
781 do
782 {
783 g_autoptr(SysprofDocumentOverlay) overlay = g_list_model_get_item (G_LIST_MODEL (self), i);
784 int pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (overlay));
785 SysprofProcessInfo *process_info = _sysprof_document_process_info (self, pid, TRUE);
786
787 if (process_info != NULL)
788 {
789 const char *mount_point = sysprof_document_overlay_get_destination (overlay);
790 const char *host_path = sysprof_document_overlay_get_source (overlay);
791 int layer = sysprof_document_overlay_get_layer (overlay);
792 g_autoptr(SysprofMount) mount = _sysprof_mount_new_for_overlay (self->strings, mount_point, host_path, layer);
793
794 sysprof_mount_namespace_add_mount (process_info->mount_namespace,
795 g_steal_pointer (&mount));
796 }
797 }
798 while (egg_bitset_iter_next (&iter, &i));
799 }
800 }
801
802 static void
803 sysprof_document_load_processes (SysprofDocument *self)
804 {
805 G_GNUC_UNUSED SysprofProcessInfo *pid0;
806 g_autoptr(GHashTable) seen = NULL;
807 g_autoptr(EggBitset) duplicate = NULL;
808 EggBitsetIter iter;
809 guint i;
810
811 g_assert (SYSPROF_IS_DOCUMENT (self));
812
813 seen = g_hash_table_new (NULL, NULL);
814 duplicate = egg_bitset_new_empty ();
815
816 /* Always create PID 0 info */
817 pid0 = _sysprof_document_process_info (self, 0, TRUE);
818
819 if (egg_bitset_iter_init_first (&iter, self->processes, &i))
820 {
821 do
822 {
823 g_autoptr(SysprofDocumentProcess) process = g_list_model_get_item (G_LIST_MODEL (self), i);
824 int pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (process));
825 SysprofProcessInfo *process_info = _sysprof_document_process_info (self, pid, TRUE);
826 const char *cmdline = _sysprof_document_process_get_comm (process);
827
828 if (cmdline != NULL)
829 {
830 GRefString *nick = g_ref_string_acquire (process_info->fallback_symbol->binary_nick);
831 g_auto(GStrv) split = g_strsplit (cmdline, " ", 2);
832 const char *shared_name = split[0] ? split[0] : cmdline;
833
834 g_clear_object (&process_info->symbol);
835 process_info->symbol =
836 _sysprof_symbol_new (sysprof_strings_get (self->strings, cmdline),
837 NULL, g_steal_pointer (&nick), 0, 0,
838 SYSPROF_SYMBOL_KIND_PROCESS);
839
840 g_clear_object (&process_info->shared_symbol);
841 process_info->shared_symbol =
842 _sysprof_symbol_new (sysprof_strings_get (self->strings, shared_name),
843 NULL, NULL, 0, 0,
844 SYSPROF_SYMBOL_KIND_PROCESS);
845 }
846
847 /* Only include the first process discover for this process,
848 * ignore all secondary notations which are just comm[] updates.
849 */
850 if (g_hash_table_contains (seen, GINT_TO_POINTER (pid)))
851 egg_bitset_add (duplicate, i);
852 else
853 g_hash_table_add (seen, GINT_TO_POINTER (pid));
854 }
855 while (egg_bitset_iter_next (&iter, &i));
856 }
857
858 egg_bitset_subtract (self->processes, duplicate);
859 }
860
861 static void
862 sysprof_document_load_counters (SysprofDocument *self)
863 {
864 g_autoptr(GPtrArray) counters = NULL;
865 g_autoptr(EggBitset) swap_ids = NULL;
866 GListModel *model;
867 EggBitsetIter iter;
868 guint n_counters;
869 guint i;
870
871 g_assert (SYSPROF_IS_DOCUMENT (self));
872
873 /* Cast up front to avoid many type checks later */
874 model = G_LIST_MODEL (self);
875
876 /* If we need to swap int64 (double does not need swapping), then keep
877 * a bitset of which counters need swapping. That way we have a fast
878 * lookup below we can use when reading in counter values.
879 */
880 if (self->needs_swap)
881 swap_ids = egg_bitset_new_empty ();
882
883 /* First create our counter objects which we will use to hold values that
884 * were extracted from the capture file. We create the array first so that
885 * we can use g_list_store_splice() rather than have signal emission for
886 * each and every added counter.
887 */
888 counters = g_ptr_array_new_with_free_func (g_object_unref);
889 if (egg_bitset_iter_init_first (&iter, self->ctrdefs, &i))
890 {
891 do
892 {
893 g_autoptr(SysprofDocumentCtrdef) ctrdef = g_list_model_get_item (model, i);
894
895 n_counters = sysprof_document_ctrdef_get_n_counters (ctrdef);
896
897 for (guint j = 0; j < n_counters; j++)
898 {
899 g_autoptr(GArray) values = g_array_new (FALSE, FALSE, sizeof (SysprofDocumentTimedValue));
900 const char *category;
901 const char *name;
902 const char *description;
903 guint id;
904 guint type;
905
906 sysprof_document_ctrdef_get_counter (ctrdef, j, &id, &type, &category, &name, &description);
907
908 /* Keep track if this counter will need int64 endian swaps */
909 if (swap_ids != NULL && type == SYSPROF_CAPTURE_COUNTER_INT64)
910 egg_bitset_add (swap_ids, id);
911
912 g_hash_table_insert (self->counter_id_to_values,
913 GUINT_TO_POINTER (id),
914 g_array_ref (values));
915
916 g_ptr_array_add (counters,
917 _sysprof_document_counter_new (id,
918 type,
919 sysprof_strings_get (self->strings, category),
920 sysprof_strings_get (self->strings, name),
921 sysprof_strings_get (self->strings, description),
922 g_steal_pointer (&values),
923 self->time_span.begin_nsec));
924 }
925 }
926 while (egg_bitset_iter_next (&iter, &i));
927
928 if (counters->len > 0)
929 g_list_store_splice (self->counters, 0, 0, counters->pdata, counters->len);
930 }
931
932 /* Now find all the counter values and associate them with the counters
933 * that were previously defined.
934 */
935 if (egg_bitset_iter_init_first (&iter, self->ctrsets, &i))
936 {
937 do
938 {
939 g_autoptr(SysprofDocumentCtrset) ctrset = g_list_model_get_item (model, i);
940 guint n_values = sysprof_document_ctrset_get_n_values (ctrset);
941 SysprofDocumentTimedValue ctrval;
942
943 ctrval.time = sysprof_document_frame_get_time (SYSPROF_DOCUMENT_FRAME (ctrset));
944
945 for (guint j = 0; j < n_values; j++)
946 {
947 GArray *values;
948 guint id;
949
950 sysprof_document_ctrset_get_raw_value (ctrset, j, &id, ctrval.v_raw);
951
952 if (swap_ids != NULL && egg_bitset_contains (swap_ids, id))
953 {
954 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
955 ctrval.v_int64 = GINT64_FROM_BE (ctrval.v_int64);
956 #else
957 ctrval.v_int64 = GINT64_FROM_LE (ctrval.v_int64);
958 #endif
959 }
960
961 if ((values = g_hash_table_lookup (self->counter_id_to_values, GUINT_TO_POINTER (id))))
962 g_array_append_val (values, ctrval);
963 }
964 }
965 while (egg_bitset_iter_next (&iter, &i));
966 }
967
968 n_counters = g_list_model_get_n_items (G_LIST_MODEL (self->counters));
969
970 for (i = 0; i < n_counters; i++)
971 {
972 g_autoptr(SysprofDocumentCounter) counter = g_list_model_get_item (G_LIST_MODEL (self->counters), i);
973
974 _sysprof_document_counter_calculate_range (counter);
975 }
976 }
977
978 static inline gboolean
979 is_data_type (SysprofCaptureFrameType type)
980 {
981 switch (type)
982 {
983 case SYSPROF_CAPTURE_FRAME_ALLOCATION:
984 case SYSPROF_CAPTURE_FRAME_CTRSET:
985 case SYSPROF_CAPTURE_FRAME_DBUS_MESSAGE:
986 case SYSPROF_CAPTURE_FRAME_EXIT:
987 case SYSPROF_CAPTURE_FRAME_FORK:
988 case SYSPROF_CAPTURE_FRAME_LOG:
989 case SYSPROF_CAPTURE_FRAME_MAP:
990 case SYSPROF_CAPTURE_FRAME_MARK:
991 case SYSPROF_CAPTURE_FRAME_PROCESS:
992 case SYSPROF_CAPTURE_FRAME_SAMPLE:
993 case SYSPROF_CAPTURE_FRAME_TRACE:
994 return TRUE;
995
996 case SYSPROF_CAPTURE_FRAME_CTRDEF:
997 case SYSPROF_CAPTURE_FRAME_FILE_CHUNK:
998 case SYSPROF_CAPTURE_FRAME_JITMAP:
999 case SYSPROF_CAPTURE_FRAME_METADATA:
1000 case SYSPROF_CAPTURE_FRAME_OVERLAY:
1001 case SYSPROF_CAPTURE_FRAME_TIMESTAMP:
1002 default:
1003 return FALSE;
1004 }
1005 }
1006
1007 typedef struct _Load
1008 {
1009 GMappedFile *mapped_file;
1010 ProgressFunc progress;
1011 gpointer progress_data;
1012 GDestroyNotify progress_data_destroy;
1013 } Load;
1014
1015 static void
1016 load_free (Load *load)
1017 {
1018 g_clear_pointer (&load->mapped_file, g_mapped_file_unref);
1019
1020 if (load->progress_data_destroy)
1021 load->progress_data_destroy (load->progress_data);
1022
1023 load->progress = NULL;
1024 load->progress_data = NULL;
1025 load->progress_data_destroy = NULL;
1026
1027 g_free (load);
1028 }
1029
1030 static inline void
1031 load_progress (Load *load,
1032 double fraction,
1033 const char *message)
1034 {
1035 if (load->progress)
1036 load->progress (fraction, message, load->progress_data);
1037 }
1038
1039 static int
1040 sort_by_time (gconstpointer a,
1041 gconstpointer b,
1042 gpointer base)
1043 {
1044 const SysprofDocumentFramePointer *aptr = a;
1045 const SysprofDocumentFramePointer *bptr = b;
1046 const SysprofCaptureFrame *aframe = (const SysprofCaptureFrame *)((const guint8 *)base + aptr->offset);
1047 const SysprofCaptureFrame *bframe = (const SysprofCaptureFrame *)((const guint8 *)base + bptr->offset);
1048
1049 if (aframe->time < bframe->time)
1050 return -1;
1051
1052 if (aframe->time > bframe->time)
1053 return 1;
1054
1055 if (aframe->type == SYSPROF_CAPTURE_FRAME_MARK && bframe->type == SYSPROF_CAPTURE_FRAME_MARK)
1056 {
1057 const SysprofCaptureMark *amark = (const SysprofCaptureMark *)aframe;
1058 const SysprofCaptureMark *bmark = (const SysprofCaptureMark *)bframe;
1059
1060 if (amark->duration > bmark->duration)
1061 return -1;
1062 else if (amark->duration < bmark->duration)
1063 return 1;
1064 }
1065
1066 return 0;
1067 }
1068
1069 static int
1070 sort_by_time_swapped (gconstpointer a,
1071 gconstpointer b,
1072 gpointer base)
1073 {
1074 const SysprofDocumentFramePointer *aptr = a;
1075 const SysprofDocumentFramePointer *bptr = b;
1076 const SysprofCaptureFrame *aframe = (const SysprofCaptureFrame *)((const guint8 *)base + aptr->offset);
1077 const SysprofCaptureFrame *bframe = (const SysprofCaptureFrame *)((const guint8 *)base + bptr->offset);
1078 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1079 gint64 atime = GINT64_FROM_BE (aframe->time);
1080 gint64 btime = GINT64_FROM_BE (bframe->time);
1081 #else
1082 gint64 atime = GINT64_FROM_LE (aframe->time);
1083 gint64 btime = GINT64_FROM_LE (bframe->time);
1084 #endif
1085
1086 if (atime < btime)
1087 return -1;
1088
1089 if (atime > btime)
1090 return 1;
1091
1092 if (aframe->type == SYSPROF_CAPTURE_FRAME_MARK && bframe->type == SYSPROF_CAPTURE_FRAME_MARK)
1093 {
1094 const SysprofCaptureMark *amark = (const SysprofCaptureMark *)aframe;
1095 const SysprofCaptureMark *bmark = (const SysprofCaptureMark *)bframe;
1096 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1097 gint64 aduration = GINT64_FROM_BE (amark->duration);
1098 gint64 bduration = GINT64_FROM_BE (bmark->duration);
1099 #else
1100 gint64 aduration = GINT64_FROM_LE (amark->duration);
1101 gint64 bduration = GINT64_FROM_LE (bmark->duration);
1102 #endif
1103
1104 if (aduration > bduration)
1105 return -1;
1106 else if (aduration < bduration)
1107 return 1;
1108 }
1109
1110 return 0;
1111 }
1112
1113 static void
1114 sysprof_document_update_process_exit_times (SysprofDocument *self)
1115 {
1116 GHashTableIter iter;
1117 SysprofProcessInfo *info;
1118
1119 g_assert (SYSPROF_IS_DOCUMENT (self));
1120
1121 g_hash_table_iter_init (&iter, self->pid_to_process_info);
1122 while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&info))
1123 {
1124 if (info->exit_time == 0)
1125 info->exit_time = self->time_span.end_nsec;
1126 }
1127 }
1128
1129 static void
1130 sysprof_document_load_cpu (SysprofDocument *self)
1131 {
1132 const gsize model_len = strlen ("Model\t\t: ");
1133 g_autoptr(SysprofDocumentFile) file = NULL;
1134 g_autoptr(GBytes) bytes = NULL;
1135 g_autoptr(SysprofCpuInfo) cpu_info = NULL;
1136 g_autofree char *model = NULL;
1137 const char *str;
1138 const char *line;
1139 LineReader reader;
1140 gsize line_len;
1141 gsize len;
1142
1143 g_assert (SYSPROF_IS_DOCUMENT (self));
1144
1145 if (!(file = sysprof_document_lookup_file (self, "/proc/cpuinfo")) ||
1146 !(bytes = sysprof_document_file_dup_bytes (file)))
1147 return;
1148
1149 str = (const char *)g_bytes_get_data (bytes, &len);
1150
1151 line_reader_init (&reader, (char *)str, len);
1152 while ((line = line_reader_next (&reader, &line_len)))
1153 {
1154 if (g_str_has_prefix (line, "processor\t: "))
1155 {
1156 gint64 id = g_ascii_strtoll (line+strlen("processor\t: "), NULL, 10);
1157
1158 if (cpu_info != NULL)
1159 g_list_store_append (self->cpu_info, cpu_info);
1160
1161 g_clear_object (&cpu_info);
1162
1163 cpu_info = g_object_new (SYSPROF_TYPE_CPU_INFO,
1164 "id", id,
1165 NULL);
1166 }
1167
1168 if (g_str_has_prefix (line, "core id\t\t: "))
1169 {
1170 gint64 core_id = g_ascii_strtoll (line+strlen("core id\t\t: "), NULL, 10);
1171
1172 if (core_id > 0)
1173 _sysprof_cpu_info_set_core_id (cpu_info, core_id);
1174 }
1175
1176 if (g_str_has_prefix (line, "model name\t: "))
1177 {
1178 const gsize model_name_len = strlen ("model name\t: ");
1179 g_autofree char *model_name = g_strndup (line+model_name_len, line_len-model_name_len);
1180
1181 if (cpu_info != NULL)
1182 _sysprof_cpu_info_set_model_name (cpu_info, model_name);
1183 }
1184
1185 if (!model && g_str_has_prefix (line, "Model\t\t: "))
1186 model = g_strndup (line+model_len, line_len-model_len);
1187 }
1188
1189 if (cpu_info != NULL)
1190 g_list_store_append (self->cpu_info, cpu_info);
1191
1192 if (model != NULL)
1193 {
1194 guint n_items = g_list_model_get_n_items (G_LIST_MODEL (self->cpu_info));
1195
1196 for (guint i = 0; i < n_items; i++)
1197 {
1198 g_autoptr(SysprofCpuInfo) item = g_list_model_get_item (G_LIST_MODEL (self->cpu_info), i);
1199
1200 _sysprof_cpu_info_set_model_name (item, model);
1201 }
1202 }
1203 }
1204
1205 static void
1206 sysprof_document_load_worker (GTask *task,
1207 gpointer source_object,
1208 gpointer task_data,
1209 GCancellable *cancellable)
1210 {
1211 g_autoptr(SysprofDocument) self = NULL;
1212 g_autoptr(GHashTable) files = NULL;
1213 Load *load = task_data;
1214 gint64 guessed_end_nsec = 0;
1215 goffset pos;
1216 gsize len;
1217 guint count;
1218
1219 g_assert (source_object == NULL);
1220 g_assert (load != NULL);
1221
1222 self = g_object_new (SYSPROF_TYPE_DOCUMENT, NULL);
1223 self->mapped_file = g_mapped_file_ref (load->mapped_file);
1224 self->base = (const guint8 *)g_mapped_file_get_contents (load->mapped_file);
1225 len = g_mapped_file_get_length (load->mapped_file);
1226
1227 if (len < sizeof self->header)
1228 {
1229 g_task_return_new_error (task,
1230 G_IO_ERROR,
1231 G_IO_ERROR_INVALID_DATA,
1232 "File header too short");
1233 return;
1234 }
1235
1236 /* Keep a copy of our header */
1237 memcpy (&self->header, self->base, sizeof self->header);
1238 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1239 self->needs_swap = !self->header.little_endian;
1240 #else
1241 self->needs_swap = !!self->header.little_endian;
1242 #endif
1243
1244 self->header.time = swap_uint64 (self->needs_swap, self->header.time);
1245 self->header.end_time = swap_uint64 (self->needs_swap, self->header.end_time);
1246 self->header.capture_time[sizeof self->header.capture_time-1] = 0;
1247
1248 self->time_span.begin_nsec = self->header.time;
1249 self->time_span.end_nsec = self->header.end_time;
1250
1251 load_progress (load, .1, _("Indexing capture data frames"));
1252
1253 count = 0;
1254 pos = sizeof self->header;
1255 while (pos < (len - sizeof(guint16)))
1256 {
1257 SysprofDocumentFramePointer ptr;
1258 guint16 frame_len;
1259
1260 memcpy (&frame_len, &self->base[pos], sizeof frame_len);
1261 frame_len = swap_uint16 (self->needs_swap, frame_len);
1262
1263 if (frame_len < sizeof (SysprofCaptureFrame))
1264 {
1265 g_warning ("Capture contained implausibly short frame");
1266 break;
1267 }
1268
1269 if (frame_len % SYSPROF_CAPTURE_ALIGN != 0)
1270 {
1271 g_warning ("Capture contained frame not aligned to %u",
1272 (guint)SYSPROF_CAPTURE_ALIGN);
1273 break;
1274 }
1275
1276 ptr.offset = pos;
1277 ptr.length = frame_len;
1278
1279 pos += frame_len;
1280 count++;
1281
1282 g_array_append_val (self->frames, ptr);
1283
1284 if (count % 100 == 0)
1285 load_progress (load,
1286 (pos / (double)len) * .4,
1287 _("Indexing capture data frames"));
1288 }
1289
1290 /* Now sort all the items by their respective frame times as frames
1291 * cat into the writer may be tacked on at the end even though they
1292 * have state which belongs earlier in the capture.
1293 */
1294 if G_UNLIKELY (self->needs_swap)
1295 gtk_tim_sort (self->frames->data,
1296 self->frames->len,
1297 sizeof (SysprofDocumentFramePointer),
1298 sort_by_time_swapped,
1299 (gpointer)self->base);
1300 else
1301 gtk_tim_sort (self->frames->data,
1302 self->frames->len,
1303 sizeof (SysprofDocumentFramePointer),
1304 sort_by_time,
1305 (gpointer)self->base);
1306
1307 for (guint f = 0; f < self->frames->len; f++)
1308 {
1309 SysprofDocumentFramePointer *ptr = &g_array_index (self->frames, SysprofDocumentFramePointer, f);
1310 const SysprofCaptureFrame *tainted = (const SysprofCaptureFrame *)(gpointer)&self->base[ptr->offset];
1311 gint64 t = swap_int64 (self->needs_swap, tainted->time);
1312 int pid = swap_int32 (self->needs_swap, tainted->pid);
1313
1314 if (t > guessed_end_nsec && is_data_type (tainted->type))
1315 guessed_end_nsec = t;
1316
1317 egg_bitset_add (self->pids, pid);
1318
1319 switch ((int)tainted->type)
1320 {
1321 case SYSPROF_CAPTURE_FRAME_ALLOCATION:
1322 egg_bitset_add (self->allocations, f);
1323 egg_bitset_add (self->traceables, f);
1324 break;
1325
1326 case SYSPROF_CAPTURE_FRAME_SAMPLE:
1327 egg_bitset_add (self->samples, f);
1328 egg_bitset_add (self->traceables, f);
1329 break;
1330
1331 case SYSPROF_CAPTURE_FRAME_PROCESS:
1332 egg_bitset_add (self->processes, f);
1333 break;
1334
1335 case SYSPROF_CAPTURE_FRAME_FILE_CHUNK:
1336 egg_bitset_add (self->file_chunks, f);
1337 break;
1338
1339 case SYSPROF_CAPTURE_FRAME_CTRDEF:
1340 egg_bitset_add (self->ctrdefs, f);
1341 break;
1342
1343 case SYSPROF_CAPTURE_FRAME_CTRSET:
1344 egg_bitset_add (self->ctrsets, f);
1345 break;
1346
1347 case SYSPROF_CAPTURE_FRAME_DBUS_MESSAGE:
1348 egg_bitset_add (self->dbus_messages, f);
1349 break;
1350
1351 case SYSPROF_CAPTURE_FRAME_MARK:
1352 egg_bitset_add (self->marks, f);
1353 break;
1354
1355 case SYSPROF_CAPTURE_FRAME_METADATA:
1356 egg_bitset_add (self->metadata, f);
1357 break;
1358
1359 case SYSPROF_CAPTURE_FRAME_JITMAP:
1360 egg_bitset_add (self->jitmaps, f);
1361 break;
1362
1363 case SYSPROF_CAPTURE_FRAME_LOG:
1364 egg_bitset_add (self->logs, f);
1365 break;
1366
1367 case SYSPROF_CAPTURE_FRAME_MAP:
1368 egg_bitset_add (self->mmaps, f);
1369 break;
1370
1371 case SYSPROF_CAPTURE_FRAME_OVERLAY:
1372 egg_bitset_add (self->overlays, f);
1373 break;
1374
1375 default:
1376 break;
1377 }
1378
1379 if (tainted->type == SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
1380 {
1381 const SysprofCaptureFileChunk *file_chunk = (const SysprofCaptureFileChunk *)tainted;
1382
1383 if (has_null_byte (file_chunk->path, (const char *)file_chunk->data) &&
1384 !g_hash_table_contains (self->files_first_position, file_chunk->path))
1385 g_hash_table_insert (self->files_first_position,
1386 g_strdup (file_chunk->path),
1387 GUINT_TO_POINTER (f));
1388 }
1389 else if (tainted->type == SYSPROF_CAPTURE_FRAME_SAMPLE)
1390 {
1391 const SysprofCaptureSample *sample = (const SysprofCaptureSample *)tainted;
1392 guint n_addrs = swap_uint16 (self->needs_swap, sample->n_addrs);
1393 const guint8 *endptr = (const guint8 *)tainted + ptr->length;
1394
1395 /* If the sample contains a context-switch, record it */
1396 if ((const guint8 *)sample + (n_addrs * sizeof (SysprofAddress)) <= endptr)
1397 {
1398 SysprofAddressContext last_context = SYSPROF_ADDRESS_CONTEXT_USER;
1399
1400 for (guint i = 0; i < n_addrs; i++)
1401 {
1402 SysprofAddress addr = swap_uint64 (self->needs_swap, sample->addrs[i]);
1403
1404 if (sysprof_address_is_context_switch (addr, &last_context) &&
1405 last_context == SYSPROF_ADDRESS_CONTEXT_KERNEL)
1406 {
1407 egg_bitset_add (self->samples_with_context_switch, f);
1408 break;
1409 }
1410 }
1411 }
1412
1413 if (sample->tid != tainted->pid)
1414 {
1415 SysprofProcessInfo *info = _sysprof_document_process_info (self, pid, TRUE);
1416 sysprof_process_info_seen_thread (info, swap_int32 (self->needs_swap, sample->tid));
1417 }
1418 }
1419 else if (tainted->type == SYSPROF_CAPTURE_FRAME_ALLOCATION)
1420 {
1421 const SysprofCaptureAllocation *alloc = (const SysprofCaptureAllocation *)tainted;
1422 SysprofProcessInfo *info = _sysprof_document_process_info (self, pid, TRUE);
1423 sysprof_process_info_seen_thread (info, swap_int32 (self->needs_swap, alloc->tid));
1424 }
1425 else if (tainted->type == SYSPROF_CAPTURE_FRAME_EXIT)
1426 {
1427 SysprofProcessInfo *info = _sysprof_document_process_info (self, pid, TRUE);
1428
1429 info->exit_time = t;
1430 }
1431 else if (tainted->type == SYSPROF_CAPTURE_FRAME_MARK)
1432 {
1433 const SysprofCaptureMark *mark = (const SysprofCaptureMark *)tainted;
1434 const char *endptr = (const char *)tainted + ptr->length;
1435 gint64 duration = swap_int64 (self->needs_swap, mark->duration);
1436
1437 if (t + duration > guessed_end_nsec)
1438 guessed_end_nsec = t + duration;
1439
1440 if (has_null_byte (mark->group, endptr) &&
1441 has_null_byte (mark->name, endptr))
1442 {
1443 const char *group = mark->group;
1444 const char *name = mark->name;
1445 GHashTable *names;
1446 EggBitset *bitset;
1447
1448 if (!(names = g_hash_table_lookup (self->mark_groups, group)))
1449 {
1450 names = g_hash_table_new_full (g_str_hash,
1451 g_str_equal,
1452 g_free,
1453 (GDestroyNotify)egg_bitset_unref);
1454 g_hash_table_insert (self->mark_groups, g_strdup (group), names);
1455 }
1456
1457 if (!(bitset = g_hash_table_lookup (names, name)))
1458 {
1459 bitset = egg_bitset_new_empty ();
1460 g_hash_table_insert (names, g_strdup (name), bitset);
1461 }
1462
1463 egg_bitset_add (bitset, f);
1464 }
1465 }
1466 }
1467
1468 if (guessed_end_nsec > self->time_span.begin_nsec)
1469 self->time_span.end_nsec = guessed_end_nsec;
1470
1471 sysprof_document_load_cpu (self);
1472
1473 load_progress (load, .6, _("Discovering file system mounts"));
1474 sysprof_document_load_mounts (self);
1475
1476 load_progress (load, .65, _("Discovering process mount namespaces"));
1477 sysprof_document_load_mountinfos (self);
1478
1479 load_progress (load, .7, _("Analyzing process address layouts"));
1480 sysprof_document_load_memory_maps (self);
1481
1482 load_progress (load, .75, _("Analyzing process command line"));
1483 sysprof_document_load_processes (self);
1484
1485 load_progress (load, .8, _("Analyzing file system overlays"));
1486 sysprof_document_load_overlays (self);
1487
1488 load_progress (load, .85, _("Processing counters"));
1489 sysprof_document_load_counters (self);
1490
1491 /* Ensure all our process have an exit_time set */
1492 sysprof_document_update_process_exit_times (self);
1493
1494 g_task_return_pointer (task, g_steal_pointer (&self), g_object_unref);
1495 }
1496
1497 void
1498 _sysprof_document_new_async (GMappedFile *mapped_file,
1499 ProgressFunc progress,
1500 gpointer progress_data,
1501 GDestroyNotify progress_data_destroy,
1502 GCancellable *cancellable,
1503 GAsyncReadyCallback callback,
1504 gpointer user_data)
1505 {
1506 g_autoptr(GTask) task = NULL;
1507 Load *load;
1508
1509 g_return_if_fail (mapped_file != NULL);
1510 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
1511
1512 load = g_new0 (Load, 1);
1513 load->mapped_file = g_mapped_file_ref (mapped_file);
1514 load->progress = progress;
1515 load->progress_data = progress_data;
1516 load->progress_data_destroy = progress_data_destroy;
1517
1518 task = g_task_new (NULL, cancellable, callback, user_data);
1519 g_task_set_source_tag (task, _sysprof_document_new_async);
1520 g_task_set_task_data (task, load, (GDestroyNotify)load_free);
1521 g_task_run_in_thread (task, sysprof_document_load_worker);
1522 }
1523
1524 SysprofDocument *
1525 _sysprof_document_new_finish (GAsyncResult *result,
1526 GError **error)
1527 {
1528 SysprofDocument *ret;
1529
1530 g_return_val_if_fail (G_IS_TASK (result), NULL);
1531 ret = g_task_propagate_pointer (G_TASK (result), error);
1532 g_return_val_if_fail (!ret || SYSPROF_IS_DOCUMENT (ret), NULL);
1533
1534 return ret;
1535 }
1536
1537 GRefString *
1538 _sysprof_document_ref_string (SysprofDocument *self,
1539 const char *name)
1540 {
1541 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1542
1543 return sysprof_strings_get (self->strings, name);
1544 }
1545
1546 static void
1547 sysprof_document_symbolize_symbols_cb (GObject *object,
1548 GAsyncResult *result,
1549 gpointer user_data)
1550 {
1551 g_autoptr(SysprofDocumentSymbols) symbols = NULL;
1552 g_autoptr(GTask) task = user_data;
1553 g_autoptr(GError) error = NULL;
1554 SysprofDocument *self;
1555
1556 g_assert (G_IS_ASYNC_RESULT (result));
1557 g_assert (G_IS_TASK (task));
1558
1559 if (!(symbols = _sysprof_document_symbols_new_finish (result, &error)))
1560 {
1561 g_task_return_error (task, g_steal_pointer (&error));
1562 return;
1563 }
1564
1565 self = g_task_get_source_object (task);
1566
1567 g_assert (self != NULL);
1568 g_assert (SYSPROF_IS_DOCUMENT (self));
1569
1570 g_set_object (&self->symbols, symbols);
1571
1572 g_task_return_boolean (task, TRUE);
1573 }
1574
1575 typedef struct _Symbolize
1576 {
1577 ProgressFunc progress_func;
1578 gpointer progress_data;
1579 GDestroyNotify progress_data_destroy;
1580 } Symbolize;
1581
1582 static void
1583 symbolize_free (Symbolize *state)
1584 {
1585 if (state->progress_data_destroy)
1586 state->progress_data_destroy (state->progress_data);
1587 g_free (state);
1588 }
1589
1590 static void
1591 sysprof_document_symbolize_prepare_cb (GObject *object,
1592 GAsyncResult *result,
1593 gpointer user_data)
1594 {
1595 SysprofSymbolizer *symbolizer = (SysprofSymbolizer *)object;
1596 g_autoptr(GTask) task = user_data;
1597 g_autoptr(GError) error = NULL;
1598 SysprofDocument *self;
1599 Symbolize *state;
1600
1601 g_assert (SYSPROF_IS_SYMBOLIZER (symbolizer));
1602 g_assert (G_IS_ASYNC_RESULT (result));
1603 g_assert (G_IS_TASK (task));
1604
1605 self = g_task_get_source_object (task);
1606
1607 g_assert (self != NULL);
1608 g_assert (SYSPROF_IS_DOCUMENT (self));
1609 g_assert (self->pid_to_process_info != NULL);
1610
1611 state = g_task_get_task_data (task);
1612
1613 if (!_sysprof_symbolizer_prepare_finish (symbolizer, result, &error))
1614 g_task_return_error (task, g_steal_pointer (&error));
1615 else
1616 _sysprof_document_symbols_new (g_task_get_source_object (task),
1617 self->strings,
1618 symbolizer,
1619 self->pid_to_process_info,
1620 state->progress_func,
1621 state->progress_data,
1622 NULL,
1623 g_task_get_cancellable (task),
1624 sysprof_document_symbolize_symbols_cb,
1625 g_object_ref (task));
1626 }
1627
1628 void
1629 _sysprof_document_symbolize_async (SysprofDocument *self,
1630 SysprofSymbolizer *symbolizer,
1631 ProgressFunc progress_func,
1632 gpointer progress_data,
1633 GDestroyNotify progress_data_destroy,
1634 GCancellable *cancellable,
1635 GAsyncReadyCallback callback,
1636 gpointer user_data)
1637 {
1638 g_autoptr(GTask) task = NULL;
1639 Symbolize *state;
1640
1641 g_return_if_fail (SYSPROF_IS_DOCUMENT (self));
1642 g_return_if_fail (SYSPROF_IS_SYMBOLIZER (symbolizer));
1643 g_return_if_fail (!cancellable || SYSPROF_IS_SYMBOLIZER (symbolizer));
1644
1645 task = g_task_new (self, cancellable, callback, user_data);
1646 g_task_set_source_tag (task, _sysprof_document_symbolize_async);
1647
1648 state = g_new0 (Symbolize, 1);
1649 state->progress_func = progress_func;
1650 state->progress_data = progress_data;
1651 state->progress_data_destroy = progress_data_destroy;
1652 g_task_set_task_data(task, state, (GDestroyNotify)symbolize_free);
1653
1654 _sysprof_symbolizer_prepare_async (symbolizer,
1655 self,
1656 cancellable,
1657 sysprof_document_symbolize_prepare_cb,
1658 g_steal_pointer (&task));
1659 }
1660
1661 gboolean
1662 _sysprof_document_symbolize_finish (SysprofDocument *self,
1663 GAsyncResult *result,
1664 GError **error)
1665 {
1666 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), FALSE);
1667 g_return_val_if_fail (G_IS_TASK (result), FALSE);
1668 g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
1669 g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) == _sysprof_document_symbolize_async, FALSE);
1670
1671 return g_task_propagate_boolean (G_TASK (result), error);
1672 }
1673
1674 gboolean
1675 _sysprof_document_is_native (SysprofDocument *self)
1676 {
1677 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), FALSE);
1678
1679 return self->needs_swap == FALSE;
1680 }
1681
1682 /**
1683 * sysprof_document_lookup_file:
1684 * @self: a #SysprofDocument
1685 * @path: the path of the file
1686 *
1687 * Locates @path within the document and returns a #SysprofDocumentFile if
1688 * it was found which can be used to access the contents.
1689 *
1690 * Returns: (transfer full) (nullable): a #SysprofDocumentFile
1691 */
1692 SysprofDocumentFile *
1693 sysprof_document_lookup_file (SysprofDocument *self,
1694 const char *path)
1695 {
1696 g_autofree char *gz_path = NULL;
1697 gpointer key, value;
1698
1699 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1700 g_return_val_if_fail (path != NULL, NULL);
1701
1702 gz_path = g_strdup_printf ("%s.gz", path);
1703
1704 if (g_hash_table_lookup_extended (self->files_first_position, path, &key, &value) ||
1705 g_hash_table_lookup_extended (self->files_first_position, gz_path, &key, &value))
1706 {
1707 g_autoptr(GPtrArray) file_chunks = g_ptr_array_new_with_free_func (g_object_unref);
1708 const char *real_path = key;
1709 EggBitsetIter iter;
1710 guint target = GPOINTER_TO_SIZE (value);
1711 guint i;
1712
1713 if (egg_bitset_iter_init_at (&iter, self->file_chunks, target, &i))
1714 {
1715 do
1716 {
1717 g_autoptr(SysprofDocumentFileChunk) file_chunk = sysprof_document_get_item ((GListModel *)self, i);
1718
1719 if (g_strcmp0 (real_path, sysprof_document_file_chunk_get_path (file_chunk)) == 0)
1720 {
1721 gboolean is_last = sysprof_document_file_chunk_get_is_last (file_chunk);
1722
1723 g_ptr_array_add (file_chunks, g_steal_pointer (&file_chunk));
1724
1725 if (is_last)
1726 break;
1727 }
1728 }
1729 while (egg_bitset_iter_next (&iter, &i));
1730 }
1731
1732 return _sysprof_document_file_new (path,
1733 g_steal_pointer (&file_chunks),
1734 g_strcmp0 (real_path, gz_path) == 0);
1735 }
1736
1737 return NULL;
1738 }
1739
1740 /**
1741 * sysprof_document_list_files:
1742 * @self: a #SysprofDocument
1743 *
1744 * Gets a #GListModel of #SysprofDocumentFile
1745 *
1746 * Returns: (transfer full): a #GListModel of #SysprofDocumentFile
1747 */
1748 GListModel *
1749 sysprof_document_list_files (SysprofDocument *self)
1750 {
1751 GHashTableIter hiter;
1752 EggBitsetIter iter;
1753 GListStore *model;
1754 gpointer key, value;
1755
1756 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1757
1758 model = g_list_store_new (SYSPROF_TYPE_DOCUMENT_FILE);
1759
1760 g_hash_table_iter_init (&hiter, self->files_first_position);
1761 while (g_hash_table_iter_next (&hiter, &key, &value))
1762 {
1763 g_autoptr(SysprofDocumentFile) file = NULL;
1764 g_autoptr(GPtrArray) file_chunks = g_ptr_array_new_with_free_func (g_object_unref);
1765 g_autofree char *no_gz_path = NULL;
1766 const char *path = key;
1767 guint target = GPOINTER_TO_SIZE (value);
1768 guint i;
1769
1770 if (egg_bitset_iter_init_at (&iter, self->file_chunks, target, &i))
1771 {
1772 do
1773 {
1774 g_autoptr(SysprofDocumentFileChunk) file_chunk = sysprof_document_get_item ((GListModel *)self, i);
1775
1776 if (g_strcmp0 (path, sysprof_document_file_chunk_get_path (file_chunk)) == 0)
1777 {
1778 gboolean is_last = sysprof_document_file_chunk_get_is_last (file_chunk);
1779
1780 g_ptr_array_add (file_chunks, g_steal_pointer (&file_chunk));
1781
1782 if (is_last)
1783 break;
1784 }
1785 }
1786 while (egg_bitset_iter_next (&iter, &i));
1787 }
1788
1789 if (g_str_has_suffix (path, ".gz"))
1790 path = no_gz_path = g_strndup (path, strlen (path) - 3);
1791
1792 file = _sysprof_document_file_new (path,
1793 g_steal_pointer (&file_chunks),
1794 no_gz_path != NULL);
1795
1796 g_list_store_append (model, file);
1797 }
1798
1799 return G_LIST_MODEL (model);
1800 }
1801
1802 /**
1803 * sysprof_document_list_traceables:
1804 * @self: a #SysprofDocument
1805 *
1806 * Gets a #GListModel containing #SysprofDocumentTraceable found within
1807 * the #SysprofDocument.
1808 *
1809 * Returns: (transfer full): a #GListModel of #SysprofDocumentTraceable
1810 */
1811 GListModel *
1812 sysprof_document_list_traceables (SysprofDocument *self)
1813 {
1814 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1815
1816 return _sysprof_document_bitset_index_new (G_LIST_MODEL (self), self->traceables);
1817 }
1818
1819 /**
1820 * sysprof_document_list_marks:
1821 * @self: a #SysprofDocument
1822 *
1823 * Gets a #GListModel containing #SysprofDocumentMark found within
1824 * the #SysprofDocument.
1825 *
1826 * Returns: (transfer full): a #GListModel of #SysprofDocumentMark
1827 */
1828 GListModel *
1829 sysprof_document_list_marks (SysprofDocument *self)
1830 {
1831 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1832
1833 return _sysprof_document_bitset_index_new (G_LIST_MODEL (self), self->marks);
1834 }
1835
1836 /**
1837 * sysprof_document_list_marks_by_group:
1838 * @self: a #SysprofDocument
1839 * @group: the name of the group
1840 *
1841 * Gets a #GListModel containing #SysprofDocumentMark found within
1842 * the #SysprofDocument which are part of @group.
1843 *
1844 * Returns: (transfer full): a #GListModel of #SysprofDocumentMark
1845 */
1846 GListModel *
1847 sysprof_document_list_marks_by_group (SysprofDocument *self,
1848 const char *group)
1849 {
1850 g_autoptr(EggBitset) bitset = NULL;
1851 GHashTable *names;
1852
1853 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1854 g_return_val_if_fail (group != NULL, NULL);
1855
1856 bitset = egg_bitset_new_empty ();
1857
1858 if ((names = g_hash_table_lookup (self->mark_groups, group)))
1859 {
1860 GHashTableIter iter;
1861 const char *name;
1862 EggBitset *indices;
1863
1864 g_hash_table_iter_init (&iter, names);
1865
1866 while (g_hash_table_iter_next (&iter, (gpointer *)&name, (gpointer *)&indices))
1867 egg_bitset_union (bitset, indices);
1868 }
1869
1870 return _sysprof_document_bitset_index_new (G_LIST_MODEL (self), bitset);
1871 }
1872
1873 /**
1874 * sysprof_document_list_allocations:
1875 * @self: a #SysprofDocument
1876 *
1877 * Gets a #GListModel containing #SysprofDocumentAllocation found within
1878 * the #SysprofDocument.
1879 *
1880 * Returns: (transfer full): a #GListModel of #SysprofDocumentAllocation
1881 */
1882 GListModel *
1883 sysprof_document_list_allocations (SysprofDocument *self)
1884 {
1885 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1886
1887 return _sysprof_document_bitset_index_new_full (G_LIST_MODEL (self), self->allocations, SYSPROF_TYPE_DOCUMENT_ALLOCATION);
1888 }
1889
1890 /**
1891 * sysprof_document_list_samples:
1892 * @self: a #SysprofDocument
1893 *
1894 * Gets a #GListModel containing #SysprofDocumentSample found within
1895 * the #SysprofDocument.
1896 *
1897 * Returns: (transfer full): a #GListModel of #SysprofDocumentSample
1898 */
1899 GListModel *
1900 sysprof_document_list_samples (SysprofDocument *self)
1901 {
1902 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1903
1904 return _sysprof_document_bitset_index_new_full (G_LIST_MODEL (self), self->samples, SYSPROF_TYPE_DOCUMENT_SAMPLE);
1905 }
1906
1907 /**
1908 * sysprof_document_list_samples_with_context_switch:
1909 * @self: a #SysprofDocument
1910 *
1911 * Gets a #GListModel containing #SysprofDocumentSample found within
1912 * the #SysprofDocument which contain a context switch.
1913 *
1914 * Returns: (transfer full): a #GListModel of #SysprofDocumentSample
1915 */
1916 GListModel *
1917 sysprof_document_list_samples_with_context_switch (SysprofDocument *self)
1918 {
1919 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1920
1921 return _sysprof_document_bitset_index_new_full (G_LIST_MODEL (self), self->samples_with_context_switch, SYSPROF_TYPE_DOCUMENT_SAMPLE);
1922 }
1923
1924 /**
1925 * sysprof_document_list_samples_without_context_switch:
1926 * @self: a #SysprofDocument
1927 *
1928 * Gets a #GListModel containing #SysprofDocumentSample found within
1929 * the #SysprofDocument which contain a context switch.
1930 *
1931 * Returns: (transfer full): a #GListModel of #SysprofDocumentSample
1932 */
1933 GListModel *
1934 sysprof_document_list_samples_without_context_switch (SysprofDocument *self)
1935 {
1936 g_autoptr(EggBitset) bitset = NULL;
1937
1938 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1939
1940 bitset = egg_bitset_copy (self->samples);
1941 egg_bitset_subtract (bitset, self->samples_with_context_switch);
1942
1943 return _sysprof_document_bitset_index_new_full (G_LIST_MODEL (self), bitset, SYSPROF_TYPE_DOCUMENT_SAMPLE);
1944 }
1945
1946 /**
1947 * sysprof_document_list_processes:
1948 * @self: a #SysprofDocument
1949 *
1950 * Gets a #GListModel containing #SysprofDocumentProcess found within
1951 * the #SysprofDocument.
1952 *
1953 * Returns: (transfer full): a #GListModel of #SysprofDocumentProcess
1954 */
1955 GListModel *
1956 sysprof_document_list_processes (SysprofDocument *self)
1957 {
1958 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1959
1960 return _sysprof_document_bitset_index_new (G_LIST_MODEL (self), self->processes);
1961 }
1962
1963 /**
1964 * sysprof_document_list_jitmaps:
1965 * @self: a #SysprofDocument
1966 *
1967 * Gets a #GListModel containing #SysprofDocumentJitmap found within
1968 * the #SysprofDocument.
1969 *
1970 * Returns: (transfer full): a #GListModel of #SysprofDocumentJitmap
1971 */
1972 GListModel *
1973 sysprof_document_list_jitmaps (SysprofDocument *self)
1974 {
1975 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
1976
1977 return _sysprof_document_bitset_index_new (G_LIST_MODEL (self), self->jitmaps);
1978 }
1979
1980 /**
1981 * sysprof_document_symbolize_traceable: (skip)
1982 * @self: a #SysprofDocument
1983 * @traceable: the traceable to extract symbols for
1984 * @symbols: an array to store #SysprofSymbols
1985 * @n_symbols: the number of elements in @symbols
1986 * @final_context: (out) (nullable): a location to store the last address
1987 * context of the stack trace.
1988 *
1989 * Batch symbolizing of a traceable.
1990 *
1991 * No ownership is transfered into @symbols and may be cheaply
1992 * discarded if using the stack for storage.
1993 *
1994 * Returns: The number of symbols or NULL set in @symbols.
1995 */
1996 guint
1997 sysprof_document_symbolize_traceable (SysprofDocument *self,
1998 SysprofDocumentTraceable *traceable,
1999 SysprofSymbol **symbols,
2000 guint n_symbols,
2001 SysprofAddressContext *final_context)
2002 {
2003 SysprofAddressContext last_context = SYSPROF_ADDRESS_CONTEXT_NONE;
2004 const SysprofProcessInfo *process_info;
2005 SysprofAddress *addresses;
2006 guint n_addresses;
2007 guint n_symbolized = 0;
2008 int pid;
2009
2010 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), 0);
2011 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_TRACEABLE (traceable), 0);
2012
2013 if (n_symbols == 0 || symbols == NULL)
2014 goto finish;
2015
2016 pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (traceable));
2017 process_info = g_hash_table_lookup (self->pid_to_process_info, GINT_TO_POINTER (pid));
2018 addresses = g_alloca (sizeof (SysprofAddress) * n_symbols);
2019 n_addresses = sysprof_document_traceable_get_stack_addresses (traceable, addresses, n_symbols);
2020
2021 for (guint i = 0; i < n_addresses; i++)
2022 {
2023 SysprofAddressContext context;
2024
2025 symbols[n_symbolized] = _sysprof_document_symbols_lookup (self->symbols, process_info, last_context, addresses[i]);
2026
2027 if (symbols[n_symbolized] != NULL &&
2028 /* if we symbolized recursively, skip this one */
2029 (n_symbolized == 0 || symbols[n_symbolized-1] != symbols[n_symbolized]))
2030 n_symbolized++;
2031
2032 if (sysprof_address_is_context_switch (addresses[i], &context))
2033 last_context = context;
2034 }
2035
2036 finish:
2037 if (final_context)
2038 *final_context = last_context;
2039
2040 return n_symbolized;
2041 }
2042
2043 /**
2044 * sysprof_document_list_logs:
2045 * @self: a #SysprofDocument
2046 *
2047 * Returns: (transfer full): a #GListModel of #SysprofDocumentLog
2048 */
2049 GListModel *
2050 sysprof_document_list_logs (SysprofDocument *self)
2051 {
2052 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2053
2054 return _sysprof_document_bitset_index_new (G_LIST_MODEL (self), self->logs);
2055 }
2056
2057 /**
2058 * sysprof_document_list_metadata:
2059 * @self: a #SysprofDocument
2060 *
2061 * Returns: (transfer full): a #GListModel of #SysprofDocumentMetadata
2062 */
2063 GListModel *
2064 sysprof_document_list_metadata (SysprofDocument *self)
2065 {
2066 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2067
2068 return _sysprof_document_bitset_index_new (G_LIST_MODEL (self), self->metadata);
2069 }
2070
2071 /**
2072 * sysprof_document_list_counters:
2073 * @self: a #SysprofDocument
2074 *
2075 * Returns: (transfer full): a #GListModel of #SysprofDocumentCounter
2076 */
2077 GListModel *
2078 sysprof_document_list_counters (SysprofDocument *self)
2079 {
2080 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2081
2082 return g_object_ref (G_LIST_MODEL (self->counters));
2083 }
2084
2085 static void
2086 sysprof_document_callgraph_cb (GObject *object,
2087 GAsyncResult *result,
2088 gpointer user_data)
2089 {
2090 g_autoptr(SysprofCallgraph) callgraph = NULL;
2091 g_autoptr(GError) error = NULL;
2092 g_autoptr(GTask) task = user_data;
2093
2094 g_assert (G_IS_ASYNC_RESULT (result));
2095 g_assert (G_IS_TASK (task));
2096
2097 if (!(callgraph = _sysprof_callgraph_new_finish (result, &error)))
2098 g_task_return_error (task, g_steal_pointer (&error));
2099 else
2100 g_task_return_pointer (task, g_steal_pointer (&callgraph), g_object_unref);
2101 }
2102
2103 /**
2104 * sysprof_document_callgraph_async:
2105 * @self: a #SysprofDocument
2106 * @flags: flags for generating the callgraph
2107 * @traceables: a list model of traceables for the callgraph
2108 * @augment_size: the size of data to reserve for augmentation in
2109 * the callgraph.
2110 * @augment_func: (nullable) (scope notified) (closure augment_func_data):
2111 * an optional callback
2112 * to be executed for each node in the callgraph traces to augment
2113 * as the callgraph is generated.
2114 * @augment_func_data: (nullable): the closure data for @augment_func
2115 * @augment_func_data_destroy: (destroy augment_func) (nullable): the
2116 * destroy callback for @augment_func_data.
2117 * @cancellable: (nullable): a #GCancellable or %NULL
2118 * @callback: a callback to execute upon completion
2119 * @user_data: closure data for @callback
2120 *
2121 * Asynchronously generates a callgraph using the @traceables to get
2122 * the call stacks.
2123 *
2124 * Ideally you want @augment_size to be <= the size of a pointer to
2125 * avoid extra allocations per callgraph node.
2126 */
2127 void
2128 sysprof_document_callgraph_async (SysprofDocument *self,
2129 SysprofCallgraphFlags flags,
2130 GListModel *traceables,
2131 gsize augment_size,
2132 SysprofAugmentationFunc augment_func,
2133 gpointer augment_func_data,
2134 GDestroyNotify augment_func_data_destroy,
2135 GCancellable *cancellable,
2136 GAsyncReadyCallback callback,
2137 gpointer user_data)
2138 {
2139 g_autoptr(GTask) task = NULL;
2140
2141 g_return_if_fail (SYSPROF_IS_DOCUMENT (self));
2142 g_return_if_fail (G_IS_LIST_MODEL (traceables));
2143 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
2144
2145 task = g_task_new (self, cancellable, callback, user_data);
2146 g_task_set_source_tag (task, sysprof_document_callgraph_async);
2147 sysprof_document_mark_busy_for_task (self, task);
2148
2149 _sysprof_callgraph_new_async (self,
2150 flags,
2151 traceables,
2152 augment_size,
2153 augment_func,
2154 augment_func_data,
2155 augment_func_data_destroy,
2156 cancellable,
2157 sysprof_document_callgraph_cb,
2158 g_steal_pointer (&task));
2159 }
2160
2161 /**
2162 * sysprof_document_callgraph_finish:
2163 * @self: a #SysprofDocument
2164 * @result: the #GAsyncResult provided to callback
2165 * @error: a location for a #GError
2166 *
2167 * Completes a request to generate a callgraph.
2168 *
2169 * Returns: (transfer full): a #SysprofCallgraph if successful; otherwise %NULL
2170 * and @error is set.
2171 */
2172 SysprofCallgraph *
2173 sysprof_document_callgraph_finish (SysprofDocument *self,
2174 GAsyncResult *result,
2175 GError **error)
2176 {
2177 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2178 g_return_val_if_fail (G_IS_TASK (result), NULL);
2179
2180 return g_task_propagate_pointer (G_TASK (result), error);
2181 }
2182
2183 SysprofSymbol *
2184 _sysprof_document_process_symbol (SysprofDocument *self,
2185 int pid,
2186 gboolean want_shared)
2187 {
2188 SysprofProcessInfo *info;
2189
2190 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2191
2192 if (pid < 0)
2193 pid = 0;
2194
2195 info = _sysprof_document_process_info (self, pid, FALSE);
2196
2197 if (info == NULL)
2198 return self->missing_process;
2199
2200 if (want_shared && info->shared_symbol)
2201 return info->shared_symbol;
2202
2203 if (info->symbol)
2204 return info->symbol;
2205
2206 return info->fallback_symbol;
2207 }
2208
2209 SysprofSymbol *
2210 _sysprof_document_thread_symbol (SysprofDocument *self,
2211 int pid,
2212 int tid)
2213 {
2214 static GRWLock rwlock;
2215 SysprofSymbol *ret;
2216
2217 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2218
2219 g_rw_lock_reader_lock (&rwlock);
2220 ret = g_hash_table_lookup (self->tid_to_symbol, GINT_TO_POINTER (tid));
2221 g_rw_lock_reader_unlock (&rwlock);
2222
2223 if (ret == NULL)
2224 {
2225 g_rw_lock_writer_lock (&rwlock);
2226
2227 if (!(ret = g_hash_table_lookup (self->tid_to_symbol, GINT_TO_POINTER (tid))))
2228 {
2229 char pidstr[32];
2230 char tidstr[32];
2231
2232 g_snprintf (pidstr, sizeof pidstr, "(%d)", pid);
2233
2234 if (tid == pid)
2235 g_snprintf (tidstr, sizeof tidstr, "Thread-%d (Main)", tid);
2236 else
2237 g_snprintf (tidstr, sizeof tidstr, "Thread-%d", tid);
2238
2239 ret = _sysprof_symbol_new (g_ref_string_new (tidstr),
2240 NULL,
2241 g_ref_string_new (pidstr),
2242 0, 0,
2243 SYSPROF_SYMBOL_KIND_THREAD);
2244
2245 g_hash_table_insert (self->tid_to_symbol, GINT_TO_POINTER (tid), ret);
2246 }
2247
2248 g_rw_lock_writer_unlock (&rwlock);
2249 }
2250
2251 return ret;
2252 }
2253
2254 SysprofSymbol *
2255 _sysprof_document_kernel_symbol (SysprofDocument *self)
2256 {
2257 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2258
2259 return self->symbols->context_switches[SYSPROF_ADDRESS_CONTEXT_KERNEL];
2260 }
2261
2262 /**
2263 * sysprof_document_get_clock_at_start:
2264 * @self: a #SysprofDocument
2265 *
2266 * Gets the clock time when the recording started.
2267 *
2268 * Returns: the clock time, generally of `CLOCK_MONOTONIC`
2269 */
2270 gint64
2271 sysprof_document_get_clock_at_start (SysprofDocument *self)
2272 {
2273 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), 0);
2274
2275 return self->header.time;
2276 }
2277
2278 /**
2279 * sysprof_document_list_symbols_in_traceable:
2280 * @self: a #SysprofDocument
2281 * @traceable: a #SysprofDocumentTraceable
2282 *
2283 * Gets the symbols in @traceable as a list model.
2284 *
2285 * Returns: (transfer full): a #GListModel of #SysprofSymbol
2286 */
2287 GListModel *
2288 sysprof_document_list_symbols_in_traceable (SysprofDocument *self,
2289 SysprofDocumentTraceable *traceable)
2290 {
2291 SysprofAddressContext final_context;
2292 GListStore *ret = NULL;
2293 SysprofSymbol **symbols;
2294 guint stack_depth;
2295 guint n_symbols;
2296
2297 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2298 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_TRACEABLE (traceable), NULL);
2299
2300 ret = g_list_store_new (SYSPROF_TYPE_SYMBOL);
2301
2302 stack_depth = MIN (MAX_STACK_DEPTH, sysprof_document_traceable_get_stack_depth (traceable));
2303 symbols = g_alloca (sizeof (SysprofSymbol *) * stack_depth);
2304 n_symbols = sysprof_document_symbolize_traceable (self, traceable, symbols, stack_depth, &final_context);
2305
2306 if (n_symbols > 0 && _sysprof_symbol_is_context_switch (symbols[0]))
2307 {
2308 symbols++;
2309 n_symbols--;
2310 }
2311
2312 /* We must make a copy of the symbols because GtkListViewBase does not
2313 * deal with the same object being in a list gracefully. Realistically
2314 * we should deal with this there, but this gets things moving forward
2315 * for the time being.
2316 */
2317 for (guint i = 0; i < n_symbols; i++)
2318 {
2319 g_autoptr(SysprofSymbol) copy = _sysprof_symbol_copy (symbols[i]);
2320 g_list_store_append (ret, copy);
2321 }
2322
2323 return G_LIST_MODEL (ret);
2324 }
2325
2326 static int
2327 str_compare (gconstpointer a,
2328 gconstpointer b,
2329 gpointer user_data)
2330 {
2331 return g_strcmp0 (*(const char * const *)a, *(const char * const *)b);
2332 }
2333
2334 /**
2335 * sysprof_document_catalog_marks:
2336 * @self: a #SysprofDocument
2337 *
2338 * Get's a #GListModel of #GListModel of #SysprofMarkCatalog.
2339 *
2340 * You can use this to display sections in #GtkListView and similar
2341 * using #GtkFlattenListModel.
2342 *
2343 * Returns: (transfer full): a #GListModel of #GListModel of
2344 * #SysprofMarkCatalog grouped by mark group.
2345 */
2346 GListModel *
2347 sysprof_document_catalog_marks (SysprofDocument *self)
2348 {
2349 GListStore *store;
2350 GHashTableIter iter;
2351 gpointer key, value;
2352
2353 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2354
2355 store = g_list_store_new (G_TYPE_LIST_MODEL);
2356
2357 g_hash_table_iter_init (&iter, self->mark_groups);
2358
2359 while (g_hash_table_iter_next (&iter, &key, &value))
2360 {
2361 g_autoptr(GListStore) group = NULL;
2362 g_autoptr(GArray) median = g_array_new (FALSE, FALSE, sizeof (gint64));
2363 g_autofree const char **keys = NULL;
2364 const char *group_name = key;
2365 GHashTable *names = value;
2366 guint len;
2367
2368 group = g_list_store_new (SYSPROF_TYPE_MARK_CATALOG);
2369
2370 keys = (const char **)g_hash_table_get_keys_as_array (names, &len);
2371 g_qsort_with_data (keys, len, sizeof (char *), (GCompareDataFunc)str_compare, NULL);
2372
2373 for (guint i = 0; i < len; i++)
2374 {
2375 const char *name = keys[i];
2376 EggBitset *marks = g_hash_table_lookup (names, name);
2377 g_autoptr(GListModel) model = _sysprof_document_bitset_index_new (G_LIST_MODEL (self), marks);
2378 g_autoptr(SysprofMarkCatalog) names_catalog = NULL;
2379 gint64 min = G_MAXINT64;
2380 gint64 max = G_MININT64;
2381 gint64 total = 0;
2382 gint64 count = 0;
2383 EggBitsetIter bitset;
2384 guint pos;
2385
2386 median->len = 0;
2387
2388 if (egg_bitset_iter_init_first (&bitset, marks, &pos))
2389 {
2390 do
2391 {
2392 const SysprofDocumentFramePointer *ptr = &g_array_index (self->frames, SysprofDocumentFramePointer, pos);
2393 const SysprofCaptureMark *tainted = (const SysprofCaptureMark *)(gpointer)&self->base[ptr->offset];
2394 gint64 duration = swap_int64 (self->needs_swap, tainted->duration);
2395
2396 g_array_append_val (median, duration);
2397
2398 min = MIN (min, duration);
2399 max = MAX (max, duration);
2400 total += duration;
2401 count++;
2402 }
2403 while (egg_bitset_iter_next (&bitset, &pos));
2404 }
2405
2406 names_catalog = _sysprof_mark_catalog_new (group_name,
2407 name,
2408 model,
2409 min,
2410 max,
2411 count ? total/count : 0,
2412 median->len ? g_array_index (median, gint64, median->len/2) : 0);
2413 g_list_store_append (group, names_catalog);
2414 }
2415
2416 g_list_store_append (store, group);
2417 }
2418
2419 return G_LIST_MODEL (store);
2420 }
2421
2422 const SysprofTimeSpan *
2423 sysprof_document_get_time_span (SysprofDocument *self)
2424 {
2425 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2426
2427 return &self->time_span;
2428 }
2429
2430 /**
2431 * sysprof_document_find_counter:
2432 * @self: a #SysprofDocument
2433 * @category: the category of the counter
2434 * @name: the name of the counter
2435 *
2436 * Finds the first counter matching @category and @name.
2437 *
2438 * Returns: (transfer full) (nullable): a #SysprofDocumentCounter or %NULL
2439 */
2440 SysprofDocumentCounter *
2441 sysprof_document_find_counter (SysprofDocument *self,
2442 const char *category,
2443 const char *name)
2444 {
2445 g_autoptr(GListModel) counters = NULL;
2446 guint n_items;
2447
2448 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2449 g_return_val_if_fail (category != NULL, NULL);
2450 g_return_val_if_fail (name != NULL, NULL);
2451
2452 counters = sysprof_document_list_counters (self);
2453 n_items = g_list_model_get_n_items (counters);
2454
2455 for (guint i = 0; i < n_items; i++)
2456 {
2457 g_autoptr(SysprofDocumentCounter) counter = g_list_model_get_item (counters, i);
2458
2459 if (g_strcmp0 (category, sysprof_document_counter_get_category (counter)) == 0 &&
2460 g_strcmp0 (name, sysprof_document_counter_get_name (counter)) == 0)
2461 return g_steal_pointer (&counter);
2462 }
2463
2464 return NULL;
2465 }
2466
2467 char *
2468 sysprof_document_dup_title (SysprofDocument *self)
2469 {
2470 g_autoptr(GDateTime) date_time = NULL;
2471
2472 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2473
2474 if (self->title != NULL)
2475 return g_strdup (self->title);
2476
2477 if ((date_time = g_date_time_new_from_iso8601 (self->header.capture_time, NULL)))
2478 {
2479 g_autoptr(GDateTime) local_date_time = g_date_time_to_local(date_time);
2480 return g_date_time_format (local_date_time, _("Recording at %X %x"));
2481 }
2482
2483 return g_strdup_printf (_("Recording at %s"), self->header.capture_time);
2484 }
2485
2486 char *
2487 sysprof_document_dup_subtitle (SysprofDocument *self)
2488 {
2489 g_autoptr(GDateTime) date_time = NULL;
2490
2491 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2492
2493 if (self->title == NULL)
2494 return NULL;
2495
2496 if ((date_time = g_date_time_new_from_iso8601 (self->header.capture_time, NULL)))
2497 {
2498 g_autoptr(GDateTime) local_date_time = g_date_time_to_local(date_time);
2499 return g_date_time_format (local_date_time, _("Recording at %X %x"));
2500 }
2501
2502 return g_strdup_printf (_("Recording at %s"), self->header.capture_time);
2503 }
2504
2505 void
2506 _sysprof_document_set_title (SysprofDocument *self,
2507 const char *title)
2508 {
2509 g_return_if_fail (SYSPROF_IS_DOCUMENT (self));
2510
2511 if (g_set_str (&self->title, title))
2512 {
2513 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TITLE]);
2514 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SUBTITLE]);
2515 }
2516 }
2517
2518 /**
2519 * sysprof_document_list_cpu_info:
2520 * @self: a #SysprofDocument
2521 *
2522 * Gets the CPU that were discovered from the capture.
2523 *
2524 * Returns: (transfer full): a #GListModel of #SysprofCpuInfo
2525 */
2526 GListModel *
2527 sysprof_document_list_cpu_info (SysprofDocument *self)
2528 {
2529 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2530
2531 return g_object_ref (G_LIST_MODEL (self->cpu_info));
2532 }
2533
2534 static int
2535 sort_symbols_for_bsearch (gconstpointer a,
2536 gconstpointer b)
2537 {
2538 const SysprofPackedSymbol *packed_a = a;
2539 const SysprofPackedSymbol *packed_b = b;
2540
2541 if (packed_a->pid < packed_b->pid)
2542 return -1;
2543
2544 if (packed_a->pid > packed_b->pid)
2545 return 1;
2546
2547 if (packed_a->addr_begin < packed_b->addr_begin)
2548 return -1;
2549
2550 if (packed_a->addr_begin > packed_b->addr_begin)
2551 return 1;
2552
2553 return 0;
2554 }
2555
2556 static DexFuture *
2557 sysprof_document_serialize_symbols_fiber (gpointer user_data)
2558 {
2559 static const guint8 empty_string[1] = {0};
2560 static const SysprofPackedSymbol empty_symbol = {0};
2561 SysprofDocument *self = user_data;
2562 g_autoptr(GByteArray) strings = NULL;
2563 g_autoptr(GHashTable) strings_offset = NULL;
2564 g_autoptr(GBytes) bytes = NULL;
2565 g_autoptr(GArray) packed_symbols = NULL;
2566 GHashTableIter iter;
2567 gpointer value;
2568 char *data;
2569 gsize packed_len;
2570
2571 g_assert (SYSPROF_IS_DOCUMENT (self));
2572
2573 packed_symbols = g_array_new (FALSE, FALSE, sizeof (SysprofPackedSymbol));
2574 strings = g_byte_array_new ();
2575 strings_offset = g_hash_table_new (g_str_hash, g_str_equal);
2576
2577 /* Always put empty string at head so 0 is never a valid
2578 * offset for the document entries except for "".
2579 */
2580 g_byte_array_append (strings, empty_string, sizeof empty_string);
2581 g_hash_table_insert (strings_offset, "", 0);
2582
2583 g_hash_table_iter_init (&iter, self->pid_to_process_info);
2584 while (g_hash_table_iter_next (&iter, NULL, &value))
2585 {
2586 const SysprofProcessInfo *process_info = value;
2587
2588 if (process_info->symbol_cache != NULL)
2589 sysprof_symbol_cache_populate_packed (process_info->symbol_cache,
2590 packed_symbols,
2591 strings,
2592 strings_offset,
2593 process_info->pid);
2594 }
2595
2596 gtk_tim_sort (packed_symbols->data,
2597 packed_symbols->len,
2598 sizeof (SysprofPackedSymbol),
2599 (GCompareDataFunc)sort_symbols_for_bsearch,
2600 NULL);
2601 g_array_append_val (packed_symbols, empty_symbol);
2602
2603 packed_len = sizeof (SysprofPackedSymbol) * packed_symbols->len;
2604
2605 /* Update the offsets to be relative to the beginning of the
2606 * section containing our packed symbols. Ignore the last symbol
2607 * so that it stays all zero.
2608 */
2609 for (guint i = 0; i < packed_symbols->len-1; i++)
2610 {
2611 g_array_index (packed_symbols, SysprofPackedSymbol, i).offset += packed_len;
2612 g_array_index (packed_symbols, SysprofPackedSymbol, i).tag_offset += packed_len;
2613 }
2614
2615 if (G_MAXSSIZE - packed_len < strings->len)
2616 return dex_future_new_for_errno (ENOMEM);
2617
2618 data = g_malloc (packed_len + strings->len);
2619 memcpy (data, packed_symbols->data, packed_len);
2620 memcpy (data+packed_len, strings->data, strings->len);
2621
2622 bytes = g_bytes_new_take (data, packed_len + strings->len);
2623
2624 return dex_future_new_take_boxed (G_TYPE_BYTES, g_steal_pointer (&bytes));
2625 }
2626
2627 void
2628 sysprof_document_serialize_symbols_async (SysprofDocument *self,
2629 GCancellable *cancellable,
2630 GAsyncReadyCallback callback,
2631 gpointer user_data)
2632 {
2633 g_autoptr(DexAsyncResult) result = NULL;
2634
2635 g_return_if_fail (SYSPROF_IS_DOCUMENT (self));
2636 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
2637
2638 result = dex_async_result_new (self, cancellable, callback, user_data);
2639
2640 dex_async_result_await (result,
2641 dex_scheduler_spawn (dex_thread_pool_scheduler_get_default (), 0,
2642 sysprof_document_serialize_symbols_fiber,
2643 g_object_ref (self),
2644 g_object_unref));
2645 }
2646
2647 /**
2648 * sysprof_document_serialize_symbols_finish:
2649 * @self: a #SysprofDocument
2650 * @result: a #GAsyncResult
2651 * @error: a location for a #GError
2652 *
2653 * Completes a request to serialize the symbols of the document
2654 * encoded in a format that Sysprof understands.
2655 *
2656 * Returns: (transfer full): a #GBytes if successful; otherwise %NULL
2657 * and @error is set.
2658 */
2659 GBytes *
2660 sysprof_document_serialize_symbols_finish (SysprofDocument *self,
2661 GAsyncResult *result,
2662 GError **error)
2663 {
2664 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2665 g_return_val_if_fail (DEX_IS_ASYNC_RESULT (result), NULL);
2666
2667 return dex_async_result_propagate_pointer (DEX_ASYNC_RESULT (result), error);
2668 }
2669
2670 /**
2671 * sysprof_document_lookup_process:
2672 * @self: a #SysprofDocument
2673 * @pid: the process identifier
2674 *
2675 * Looks up a process by PID.
2676 *
2677 * If multiple copies of the process exist, the result is undefined.
2678 *
2679 * Returns: (transfer full) (nullable): a #SysprofDocumentProcess or %NULL
2680 */
2681 SysprofDocumentProcess *
2682 sysprof_document_lookup_process (SysprofDocument *self,
2683 int pid)
2684 {
2685 EggBitsetIter iter;
2686 guint position;
2687
2688 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2689
2690 if (egg_bitset_iter_init_first (&iter, self->processes, &position))
2691 {
2692 do
2693 {
2694 g_autoptr(SysprofDocumentProcess) process = g_list_model_get_item (G_LIST_MODEL (self), position);
2695
2696 g_assert (SYSPROF_IS_DOCUMENT_PROCESS (process));
2697
2698 if (sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (process)) == pid)
2699 return g_steal_pointer (&process);
2700 }
2701 while (egg_bitset_iter_next (&iter, &position));
2702 }
2703
2704 return NULL;
2705 }
2706
2707 /**
2708 * sysprof_document_list_dbus_messages:
2709 * @self: a #SysprofDocument
2710 *
2711 * Gets a #GListModel of #SysprofDocumentDBusMessage.
2712 *
2713 * Returns: (transfer full): a #GListModel
2714 */
2715 GListModel *
2716 sysprof_document_list_dbus_messages (SysprofDocument *self)
2717 {
2718 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2719
2720 return _sysprof_document_bitset_index_new (G_LIST_MODEL (self), self->dbus_messages);
2721 }
2722
2723 static void
2724 sysprof_document_save_cb (GObject *object,
2725 GAsyncResult *result,
2726 gpointer user_data)
2727 {
2728 GFile *file = (GFile *)object;
2729 g_autoptr(GError) error = NULL;
2730 g_autoptr(GTask) task = user_data;
2731
2732 g_assert (G_IS_FILE (file));
2733 g_assert (G_IS_ASYNC_RESULT (result));
2734 g_assert (G_IS_TASK (task));
2735
2736 if (!g_file_replace_contents_finish (file, result, NULL, &error))
2737 g_task_return_error (task, g_steal_pointer (&error));
2738 else
2739 g_task_return_boolean (task, TRUE);
2740 }
2741
2742 /**
2743 * sysprof_document_save_async:
2744 * @self: a #SysprofDocument
2745 * @file: a #GFile
2746 * @cancellable: (nullable): a #GCancellable or %NULL
2747 *
2748 */
2749 void
2750 sysprof_document_save_async (SysprofDocument *self,
2751 GFile *file,
2752 GCancellable *cancellable,
2753 GAsyncReadyCallback callback,
2754 gpointer user_data)
2755 {
2756 g_autoptr(GTask) task = NULL;
2757 g_autoptr(GBytes) bytes = NULL;
2758 g_autofree char *basename = NULL;
2759
2760 g_return_if_fail (SYSPROF_IS_DOCUMENT (self));
2761 g_return_if_fail (G_IS_FILE (file));
2762 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
2763
2764 task = g_task_new (self, cancellable, callback, user_data);
2765 g_task_set_source_tag (task, sysprof_document_save_async);
2766 sysprof_document_mark_busy_for_task (self, task);
2767
2768 basename = g_file_get_basename (file);
2769 _sysprof_document_set_title (self, basename);
2770
2771 bytes = g_mapped_file_get_bytes (self->mapped_file);
2772
2773 g_file_replace_contents_bytes_async (file,
2774 bytes,
2775 NULL,
2776 FALSE,
2777 G_FILE_CREATE_REPLACE_DESTINATION,
2778 cancellable,
2779 sysprof_document_save_cb,
2780 g_steal_pointer (&task));
2781 }
2782
2783 gboolean
2784 sysprof_document_save_finish (SysprofDocument *self,
2785 GAsyncResult *result,
2786 GError **error)
2787 {
2788 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), FALSE);
2789 g_return_val_if_fail (G_IS_TASK (result), FALSE);
2790
2791 return g_task_propagate_boolean (G_TASK (result), error);
2792 }
2793
2794 GArray *
2795 _sysprof_document_get_frames (SysprofDocument *self)
2796 {
2797 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2798
2799 return g_array_ref (self->frames);
2800 }
2801
2802 EggBitset *
2803 _sysprof_document_get_allocations (SysprofDocument *self)
2804 {
2805 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
2806
2807 return egg_bitset_ref (self->allocations);
2808 }
2809
2810 gboolean
2811 sysprof_document_get_busy (SysprofDocument *self)
2812 {
2813 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), FALSE);
2814
2815 return self->busy_count > 0;
2816 }
2817
2818 static void
2819 sysprof_document_serialize_symbols_cb (GObject *object,
2820 GAsyncResult *result,
2821 gpointer user_data)
2822 {
2823 SysprofDocument *document = (SysprofDocument *)object;
2824 g_autoptr(DexPromise) promise = user_data;
2825 g_autoptr(GBytes) bytes = NULL;
2826 g_autoptr(GError) error = NULL;
2827
2828 g_assert (SYSPROF_IS_DOCUMENT (document));
2829 g_assert (G_IS_ASYNC_RESULT (result));
2830 g_assert (DEX_IS_PROMISE (promise));
2831
2832 if ((bytes = sysprof_document_serialize_symbols_finish (document, result, &error)))
2833 {
2834 GValue value = G_VALUE_INIT;
2835
2836 g_value_init (&value, G_TYPE_BYTES);
2837 g_value_take_boxed (&value, g_steal_pointer (&bytes));
2838
2839 dex_promise_resolve (promise, &value);
2840 }
2841 else
2842 dex_promise_reject (promise, g_steal_pointer (&error));
2843 }
2844
2845 DexFuture *
2846 _sysprof_document_serialize_symbols (SysprofDocument *document)
2847 {
2848 DexPromise *promise;
2849
2850 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (document), NULL);
2851
2852 promise = dex_promise_new ();
2853 sysprof_document_serialize_symbols_async (document,
2854 NULL,
2855 sysprof_document_serialize_symbols_cb,
2856 dex_ref (promise));
2857 return DEX_FUTURE (promise);
2858 }