Debian Codegraph @

src/libsysprof/sysprof-address-layout.c

raw
1 /* sysprof-address-layout.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 "timsort/gtktimsortprivate.h"
24
25 #include <gio/gio.h>
26
27 #include <eggbitset.h>
28
29 #include "sysprof-address-layout-private.h"
30
31 struct _SysprofAddressLayout
32 {
33 GObject parent_instance;
34 GPtrArray *mmaps;
35 guint mmaps_dirty : 1;
36 };
37
38 static guint
39 sysprof_address_layout_get_n_items (GListModel *model)
40 {
41 return SYSPROF_ADDRESS_LAYOUT (model)->mmaps->len;
42 }
43
44 static GType
45 sysprof_address_layout_get_item_type (GListModel *model)
46 {
47 return SYSPROF_TYPE_DOCUMENT_MMAP;
48 }
49
50 static gpointer
51 sysprof_address_layout_get_item (GListModel *model,
52 guint position)
53 {
54 SysprofAddressLayout *self = SYSPROF_ADDRESS_LAYOUT (model);
55
56 if (position >= self->mmaps->len)
57 return NULL;
58
59 return g_object_ref (g_ptr_array_index (self->mmaps, position));
60 }
61
62 static void
63 list_model_iface_init (GListModelInterface *iface)
64 {
65 iface->get_n_items = sysprof_address_layout_get_n_items;
66 iface->get_item = sysprof_address_layout_get_item;
67 iface->get_item_type = sysprof_address_layout_get_item_type;
68 }
69
70 G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofAddressLayout, sysprof_address_layout, G_TYPE_OBJECT,
71 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
72
73 static void
74 sysprof_address_layout_finalize (GObject *object)
75 {
76 SysprofAddressLayout *self = (SysprofAddressLayout *)object;
77
78 g_clear_pointer (&self->mmaps, g_ptr_array_unref);
79
80 G_OBJECT_CLASS (sysprof_address_layout_parent_class)->finalize (object);
81 }
82
83 static void
84 sysprof_address_layout_class_init (SysprofAddressLayoutClass *klass)
85 {
86 GObjectClass *object_class = G_OBJECT_CLASS (klass);
87
88 object_class->finalize = sysprof_address_layout_finalize;
89 }
90
91 static void
92 sysprof_address_layout_init (SysprofAddressLayout *self)
93 {
94 self->mmaps = g_ptr_array_new_with_free_func (g_object_unref);
95 }
96
97 SysprofAddressLayout *
98 sysprof_address_layout_new (void)
99 {
100 return g_object_new (SYSPROF_TYPE_ADDRESS_LAYOUT, NULL);
101 }
102
103 void
104 sysprof_address_layout_take (SysprofAddressLayout *self,
105 SysprofDocumentMmap *map)
106 {
107 g_return_if_fail (SYSPROF_IS_ADDRESS_LAYOUT (self));
108 g_return_if_fail (SYSPROF_IS_DOCUMENT_MMAP (map));
109
110 g_ptr_array_add (self->mmaps, map);
111
112 self->mmaps_dirty = TRUE;
113 }
114
115 static int
116 compare_mmaps (gconstpointer a,
117 gconstpointer b)
118 {
119 SysprofDocumentMmap *mmap_a = *(SysprofDocumentMmap * const *)a;
120 SysprofDocumentMmap *mmap_b = *(SysprofDocumentMmap * const *)b;
121 guint64 begin_a = sysprof_document_mmap_get_start_address (mmap_a);
122 guint64 begin_b = sysprof_document_mmap_get_start_address (mmap_b);
123 guint64 end_a = sysprof_document_mmap_get_end_address (mmap_a);
124 guint64 end_b = sysprof_document_mmap_get_end_address (mmap_b);
125
126 if (begin_a < begin_b)
127 return -1;
128
129 if (begin_a > begin_b)
130 return 1;
131
132 if (end_a < end_b)
133 return -1;
134
135 if (end_a > end_b)
136 return 1;
137
138 return 0;
139 }
140
141 static gboolean
142 mmaps_overlap (SysprofDocumentMmap *a,
143 SysprofDocumentMmap *b)
144 {
145 if ((sysprof_document_mmap_get_start_address (a) <= sysprof_document_mmap_get_start_address (b)) &&
146 (sysprof_document_mmap_get_end_address (a) > sysprof_document_mmap_get_start_address (b)))
147 return TRUE;
148
149 return FALSE;
150 }
151
152 static EggBitset *
153 find_duplicates (GPtrArray *sorted)
154 {
155 EggBitset *bitset = egg_bitset_new_empty ();
156
157 if (sorted->len == 0)
158 return bitset;
159
160 for (guint i = 0; i < sorted->len-1; i++)
161 {
162 SysprofDocumentMmap *map = g_ptr_array_index (sorted, i);
163 SysprofDocumentMmap *next = g_ptr_array_index (sorted, i+1);
164
165 /* Take the second one if they overlap, which is generally the large of
166 * the mmaps. That can happen when something like [stack] is resized into
167 * a larger mmap. It's useful to remove duplicates so we get more
168 * predictable bsearch results.
169 */
170 if (mmaps_overlap (map, next))
171 egg_bitset_add (bitset, i);
172 }
173
174 return bitset;
175 }
176
177 static int
178 find_by_address (gconstpointer a,
179 gconstpointer b)
180 {
181 const SysprofAddress *key = a;
182 SysprofDocumentMmap *map = *(SysprofDocumentMmap * const *)b;
183
184 if (*key < sysprof_document_mmap_get_start_address (map))
185 return -1;
186
187 if (*key >= sysprof_document_mmap_get_end_address (map))
188 return 1;
189
190 return 0;
191 }
192
193 SysprofDocumentMmap *
194 sysprof_address_layout_lookup (SysprofAddressLayout *self,
195 SysprofAddress address)
196 {
197 SysprofDocumentMmap **ret;
198
199 g_return_val_if_fail (SYSPROF_IS_ADDRESS_LAYOUT (self), NULL);
200
201 if (self->mmaps_dirty)
202 {
203 g_autoptr(EggBitset) dups = NULL;
204 EggBitsetIter iter;
205 guint old_len = self->mmaps->len;
206 guint i;
207
208 self->mmaps_dirty = FALSE;
209
210 gtk_tim_sort (self->mmaps->pdata,
211 self->mmaps->len,
212 sizeof (gpointer),
213 (GCompareDataFunc)compare_mmaps,
214 NULL);
215 dups = find_duplicates (self->mmaps);
216
217 if (egg_bitset_iter_init_last (&iter, dups, &i))
218 {
219 do
220 g_ptr_array_remove_index (self->mmaps, i);
221 while (egg_bitset_iter_previous (&iter, &i));
222 }
223
224 g_list_model_items_changed (G_LIST_MODEL (self), 0, old_len, self->mmaps->len);
225 }
226
227 if (self->mmaps->len > 0)
228 ret = bsearch (&address,
229 self->mmaps->pdata,
230 self->mmaps->len,
231 sizeof (gpointer),
232 find_by_address);
233 else
234 ret = NULL;
235
236 return ret ? *ret : NULL;
237 }