Debian Codegraph @

src/libsysprof-capture/tests/find-temp-allocs.c

raw
1 /* find-temp-allocs.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 <sysprof-capture.h>
26
27 static struct {
28 gint64 total;
29 gint64 temp;
30 gint64 leaked;
31 } allocinfo;
32
33 typedef struct
34 {
35 gint pid;
36 gint tid;
37 gint64 time;
38 SysprofCaptureAddress addr;
39 gint64 size;
40 } Alloc;
41
42 static gint
43 compare_alloc (gconstpointer a,
44 gconstpointer b)
45 {
46 const Alloc *aptr = a;
47 const Alloc *bptr = b;
48
49 if (aptr->pid < bptr->pid)
50 return -1;
51 else if (aptr->pid > bptr->pid)
52 return 1;
53
54 if (aptr->tid < bptr->tid)
55 return -1;
56 else if (aptr->tid > bptr->tid)
57 return 1;
58
59 if (aptr->time < bptr->time)
60 return -1;
61 else if (aptr->time > bptr->time)
62 return 1;
63
64 if (aptr->addr < bptr->addr)
65 return -1;
66 else if (aptr->addr > bptr->addr)
67 return 1;
68 else
69 return 0;
70 }
71
72 static void
73 find_temp_allocs (SysprofCaptureReader *reader)
74 {
75 g_autoptr(GArray) ar = NULL;
76 SysprofCaptureFrameType type;
77 SysprofCaptureAddress last_addr = 0;
78
79 g_assert (reader != NULL);
80
81 ar = g_array_new (FALSE, FALSE, sizeof (Alloc));
82
83 while (sysprof_capture_reader_peek_type (reader, &type))
84 {
85 if (type == SYSPROF_CAPTURE_FRAME_ALLOCATION)
86 {
87 const SysprofCaptureAllocation *ev;
88 Alloc a;
89
90 if (!(ev = sysprof_capture_reader_read_allocation (reader)))
91 break;
92
93 a.pid = ev->frame.pid;
94 a.tid = ev->tid;
95 a.time = ev->frame.time;
96 a.addr = ev->alloc_addr;
97 a.size = ev->alloc_size;
98
99 g_array_append_val (ar, a);
100 }
101 else
102 {
103 if (!sysprof_capture_reader_skip (reader))
104 break;
105 }
106 }
107
108 /* Ensure items are in order because threads may have
109 * reordered things and raced to write out malloc data.
110 */
111 g_array_sort (ar, compare_alloc);
112
113 for (guint i = 0; i < ar->len; i++)
114 {
115 const Alloc *a = &g_array_index (ar, Alloc, i);
116
117 if (a->size <= 0)
118 {
119 if (last_addr == a->addr)
120 allocinfo.temp++;
121
122 allocinfo.leaked--;
123 last_addr = 0;
124 }
125 else
126 {
127 allocinfo.total++;
128 allocinfo.leaked++;
129 last_addr = a->addr;
130 }
131 }
132
133 g_printerr ("Allocations: %"G_GINT64_FORMAT"\n", allocinfo.total);
134 g_printerr (" Temporary: %"G_GINT64_FORMAT" (%lf%%)\n",
135 allocinfo.temp, allocinfo.temp / (gdouble)allocinfo.total * 100.0);
136 g_printerr (" Leaked: %"G_GINT64_FORMAT"\n", allocinfo.leaked);
137 }
138
139 gint
140 main (gint argc,
141 gchar *argv[])
142 {
143 SysprofCaptureReader *reader;
144 const gchar *filename = argv[1];
145
146 if (argc < 2)
147 {
148 g_printerr ("usage: %s FILENAME\n", argv[0]);
149 return EXIT_FAILURE;
150 }
151
152 if (!(reader = sysprof_capture_reader_new (filename)))
153 {
154 int errsv = errno;
155 g_printerr ("%s\n", g_strerror (errsv));
156 return EXIT_FAILURE;
157 }
158
159 find_temp_allocs (reader);
160
161 sysprof_capture_reader_unref (reader);
162
163 return EXIT_SUCCESS;
164 }