Debian Codegraph @

src/libsysprof-capture/tests/cross-thread-frees.c

raw
1 /* cross-thread-frees.c
2 *
3 * Copyright 2020 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 <errno.h>
24 #include <glib.h>
25 #include <stddef.h>
26
27 #include <sysprof-capture.h>
28
29 typedef struct
30 {
31 gint tid;
32 guint n_addrs;
33 gint64 size;
34 SysprofCaptureAddress addrs[0];
35 } Stack;
36
37 static void
38 stack_free (gpointer ptr)
39 {
40 Stack *stack = ptr;
41 gsize size = sizeof *stack + (stack->n_addrs * sizeof (SysprofCaptureAddress));
42 g_slice_free1 (size, stack);
43 }
44
45 static Stack *
46 stack_new (gint tid,
47 gint64 size,
48 guint n_addrs,
49 const SysprofCaptureAddress *addrs)
50 {
51 Stack *stack;
52
53 stack = g_slice_alloc (sizeof *stack + (n_addrs * sizeof (SysprofCaptureAddress)));
54 stack->tid = tid;
55 stack->size = size;
56 stack->n_addrs = n_addrs;
57 for (guint i = 0; i < n_addrs; i++)
58 stack->addrs[i] = addrs[i];
59
60 return stack;
61 }
62
63 static void
64 cross_thread_frees (SysprofCaptureReader *reader)
65 {
66 SysprofCaptureFrameType type;
67 g_autoptr(GHashTable) stacks = NULL;
68
69 stacks = g_hash_table_new_full (NULL, NULL, NULL, stack_free);
70
71 while (sysprof_capture_reader_peek_type (reader, &type))
72 {
73 if (type == SYSPROF_CAPTURE_FRAME_ALLOCATION)
74 {
75 const SysprofCaptureAllocation *ev = sysprof_capture_reader_read_allocation (reader);
76 gpointer key;
77
78 if (ev == NULL)
79 break;
80
81 key = GINT_TO_POINTER (ev->alloc_addr);
82
83 if (ev->alloc_size > 0)
84 {
85 g_hash_table_insert (stacks,
86 key,
87 stack_new (ev->tid, ev->alloc_size, ev->n_addrs, ev->addrs));
88 }
89 else
90 {
91 Stack *stack;
92
93 stack = g_hash_table_lookup (stacks, key);
94 if (stack == NULL)
95 continue;
96
97 if (ev->tid != stack->tid)
98 {
99 g_print ("Alloc-Thread=%d Free-Thread=%d Size=%"G_GUINT64_FORMAT"\n",
100 stack->tid, ev->tid, stack->size);
101 }
102
103 g_hash_table_remove (stacks, key);
104 }
105 }
106 else
107 {
108 if (!sysprof_capture_reader_skip (reader))
109 break;
110 }
111 }
112 }
113
114 gint
115 main (gint argc,
116 gchar *argv[])
117 {
118 SysprofCaptureReader *reader;
119 const gchar *filename = argv[1];
120
121 if (argc < 2)
122 {
123 g_printerr ("usage: %s FILENAME\n", argv[0]);
124 return EXIT_FAILURE;
125 }
126
127 if (!(reader = sysprof_capture_reader_new (filename)))
128 {
129 int errsv = errno;
130 g_printerr ("%s\n", g_strerror (errsv));
131 return EXIT_FAILURE;
132 }
133
134 cross_thread_frees (reader);
135
136 sysprof_capture_reader_unref (reader);
137
138 return EXIT_SUCCESS;
139 }