Debian Codegraph @

src/libsysprof-capture/sysprof-capture-cursor.c

raw
1 /* sysprof-capture-cursor.c
2 *
3 * Copyright 2016-2019 Christian Hergert <[email protected]>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Subject to the terms and conditions of this license, each copyright holder
17 * and contributor hereby grants to those receiving rights under this license
18 * a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
19 * irrevocable (except for failure to satisfy the conditions of this license)
20 * patent license to make, have made, use, offer to sell, sell, import, and
21 * otherwise transfer this software, where such license applies only to those
22 * patent claims, already acquired or hereafter acquired, licensable by such
23 * copyright holder or contributor that are necessarily infringed by:
24 *
25 * (a) their Contribution(s) (the licensed copyrights of copyright holders
26 * and non-copyrightable additions of contributors, in source or binary
27 * form) alone; or
28 *
29 * (b) combination of their Contribution(s) with the work of authorship to
30 * which such Contribution(s) was added by such copyright holder or
31 * contributor, if, at the time the Contribution is added, such addition
32 * causes such combination to be necessarily infringed. The patent license
33 * shall not apply to any other combinations which include the
34 * Contribution.
35 *
36 * Except as expressly stated above, no rights or licenses from any copyright
37 * holder or contributor is granted under this license, whether expressly, by
38 * implication, estoppel or otherwise.
39 *
40 * DISCLAIMER
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
43 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
44 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
46 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
48 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
49 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 *
54 * SPDX-License-Identifier: BSD-2-Clause-Patent
55 */
56
57 #include "config.h"
58
59 #include <assert.h>
60 #include <stdlib.h>
61
62 #include "sysprof-capture-condition.h"
63 #include "sysprof-capture-cursor.h"
64 #include "sysprof-capture-reader.h"
65 #include "sysprof-capture-util-private.h"
66 #include "sysprof-macros-internal.h"
67
68 #define READ_DELEGATE(f) ((ReadDelegate)(f))
69
70 typedef const SysprofCaptureFrame *(*ReadDelegate) (SysprofCaptureReader *);
71
72 struct _SysprofCaptureCursor
73 {
74 volatile int ref_count;
75 SysprofCaptureCondition **conditions; /* (nullable) (owned) */
76 size_t n_conditions;
77 SysprofCaptureReader *reader;
78 unsigned int reversed : 1;
79 };
80
81 static void
82 sysprof_capture_cursor_finalize (SysprofCaptureCursor *self)
83 {
84 for (size_t i = 0; i < self->n_conditions; i++)
85 sysprof_capture_condition_unref (self->conditions[i]);
86 sysprof_clear_pointer (&self->conditions, free);
87 sysprof_clear_pointer (&self->reader, sysprof_capture_reader_unref);
88 free (self);
89 }
90
91 static SysprofCaptureCursor *
92 sysprof_capture_cursor_init (void)
93 {
94 SysprofCaptureCursor *self;
95
96 self = sysprof_malloc0 (sizeof (SysprofCaptureCursor));
97 if (self == NULL)
98 return NULL;
99
100 self->conditions = NULL;
101 self->n_conditions = 0;
102 self->ref_count = 1;
103
104 return sysprof_steal_pointer (&self);
105 }
106
107 /**
108 * sysprof_capture_cursor_ref:
109 * @self: a #SysprofCaptureCursor
110 *
111 * Returns: (transfer full): @self
112 *
113 * Since: 3.34
114 */
115 SysprofCaptureCursor *
116 sysprof_capture_cursor_ref (SysprofCaptureCursor *self)
117 {
118 assert (self != NULL);
119 assert (self->ref_count > 0);
120
121 __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
122 return self;
123 }
124
125 /**
126 * sysprof_capture_cursor_unref:
127 * @self: a #SysprofCaptureCursor
128 *
129 * Since: 3.34
130 */
131 void
132 sysprof_capture_cursor_unref (SysprofCaptureCursor *self)
133 {
134 assert (self != NULL);
135 assert (self->ref_count > 0);
136
137 if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
138 sysprof_capture_cursor_finalize (self);
139 }
140
141 /**
142 * sysprof_capture_cursor_foreach:
143 * @self: a #SysprofCaptureCursor
144 * @callback: (scope call): a closure to execute
145 * @user_data: user data for @callback
146 *
147 */
148 void
149 sysprof_capture_cursor_foreach (SysprofCaptureCursor *self,
150 SysprofCaptureCursorCallback callback,
151 void *user_data)
152 {
153 assert (self != NULL);
154 assert (callback != NULL);
155
156 if (self->reader == NULL)
157 return;
158
159 for (;;)
160 {
161 const SysprofCaptureFrame *frame;
162 SysprofCaptureFrameType type = 0;
163 ReadDelegate delegate = NULL;
164
165 if (!sysprof_capture_reader_peek_type (self->reader, &type))
166 return;
167
168 switch (type)
169 {
170 case SYSPROF_CAPTURE_FRAME_TIMESTAMP:
171 delegate = READ_DELEGATE (sysprof_capture_reader_read_timestamp);
172 break;
173
174 case SYSPROF_CAPTURE_FRAME_SAMPLE:
175 delegate = READ_DELEGATE (sysprof_capture_reader_read_sample);
176 break;
177
178 case SYSPROF_CAPTURE_FRAME_TRACE:
179 delegate = READ_DELEGATE (sysprof_capture_reader_read_trace);
180 break;
181
182 case SYSPROF_CAPTURE_FRAME_LOG:
183 delegate = READ_DELEGATE (sysprof_capture_reader_read_log);
184 break;
185
186 case SYSPROF_CAPTURE_FRAME_MAP:
187 delegate = READ_DELEGATE (sysprof_capture_reader_read_map);
188 break;
189
190 case SYSPROF_CAPTURE_FRAME_MARK:
191 delegate = READ_DELEGATE (sysprof_capture_reader_read_mark);
192 break;
193
194 case SYSPROF_CAPTURE_FRAME_PROCESS:
195 delegate = READ_DELEGATE (sysprof_capture_reader_read_process);
196 break;
197
198 case SYSPROF_CAPTURE_FRAME_FORK:
199 delegate = READ_DELEGATE (sysprof_capture_reader_read_fork);
200 break;
201
202 case SYSPROF_CAPTURE_FRAME_EXIT:
203 delegate = READ_DELEGATE (sysprof_capture_reader_read_exit);
204 break;
205
206 case SYSPROF_CAPTURE_FRAME_JITMAP:
207 delegate = READ_DELEGATE (sysprof_capture_reader_read_jitmap);
208 break;
209
210 case SYSPROF_CAPTURE_FRAME_CTRDEF:
211 delegate = READ_DELEGATE (sysprof_capture_reader_read_counter_define);
212 break;
213
214 case SYSPROF_CAPTURE_FRAME_CTRSET:
215 delegate = READ_DELEGATE (sysprof_capture_reader_read_counter_set);
216 break;
217
218 case SYSPROF_CAPTURE_FRAME_METADATA:
219 delegate = READ_DELEGATE (sysprof_capture_reader_read_metadata);
220 break;
221
222 case SYSPROF_CAPTURE_FRAME_DBUS_MESSAGE:
223 delegate = READ_DELEGATE (sysprof_capture_reader_read_dbus_message);
224 break;
225
226 case SYSPROF_CAPTURE_FRAME_FILE_CHUNK:
227 delegate = READ_DELEGATE (sysprof_capture_reader_read_file);
228 break;
229
230 case SYSPROF_CAPTURE_FRAME_ALLOCATION:
231 delegate = READ_DELEGATE (sysprof_capture_reader_read_allocation);
232 break;
233
234 case SYSPROF_CAPTURE_FRAME_OVERLAY:
235 delegate = READ_DELEGATE (sysprof_capture_reader_read_overlay);
236 break;
237
238 default:
239 if (!sysprof_capture_reader_skip (self->reader))
240 return;
241 delegate = NULL;
242 break;
243 }
244
245 if (delegate == NULL)
246 continue;
247
248 if (NULL == (frame = delegate (self->reader)))
249 return;
250
251 if (self->n_conditions == 0)
252 {
253 if (!callback (frame, user_data))
254 return;
255 }
256 else
257 {
258 for (size_t i = 0; i < self->n_conditions; i++)
259 {
260 const SysprofCaptureCondition *condition = self->conditions[i];
261
262 if (sysprof_capture_condition_match (condition, frame))
263 {
264 if (!callback (frame, user_data))
265 return;
266 break;
267 }
268 }
269 }
270 }
271 }
272
273 void
274 sysprof_capture_cursor_reset (SysprofCaptureCursor *self)
275 {
276 assert (self != NULL);
277
278 if (self->reader != NULL)
279 sysprof_capture_reader_reset (self->reader);
280 }
281
282 void
283 sysprof_capture_cursor_reverse (SysprofCaptureCursor *self)
284 {
285 assert (self != NULL);
286
287 self->reversed = !self->reversed;
288 }
289
290 /**
291 * sysprof_capture_cursor_add_condition:
292 * @self: An #SysprofCaptureCursor
293 * @condition: (transfer full): An #SysprofCaptureCondition
294 *
295 * Adds the condition to the cursor. This condition must evaluate to
296 * true or a given #SysprofCapureFrame will not be matched.
297 */
298 void
299 sysprof_capture_cursor_add_condition (SysprofCaptureCursor *self,
300 SysprofCaptureCondition *condition)
301 {
302 assert (self != NULL);
303 assert (condition != NULL);
304
305 /* Grow the array linearly to keep the code simple: there are typically 0 or 1
306 * conditions applied to a given cursor, just after it’s constructed.
307 *
308 * FIXME: There’s currently no error reporting from this function, so ENOMEM
309 * results in an abort. */
310 self->conditions = _sysprof_reallocarray (self->conditions,
311 ++self->n_conditions,
312 sizeof (*self->conditions));
313 assert (self->conditions != NULL);
314
315 self->conditions[self->n_conditions - 1] = sysprof_steal_pointer (&condition);
316 }
317
318 /**
319 * sysprof_capture_cursor_new:
320 * @self: a #SysprofCaptureCursor
321 *
322 * Returns: (transfer full) (nullable): a new cursor for @reader
323 */
324 SysprofCaptureCursor *
325 sysprof_capture_cursor_new (SysprofCaptureReader *reader)
326 {
327 SysprofCaptureCursor *self;
328
329 self = sysprof_capture_cursor_init ();
330
331 if (reader != NULL)
332 {
333 self->reader = sysprof_capture_reader_copy (reader);
334 sysprof_capture_reader_reset (self->reader);
335 }
336
337 return self;
338 }
339
340 /**
341 * sysprof_capture_cursor_get_reader:
342 *
343 * Gets the underlying reader that is used by the cursor.
344 *
345 * Returns: (transfer none) (nullable): An #SysprofCaptureReader
346 */
347 SysprofCaptureReader *
348 sysprof_capture_cursor_get_reader (SysprofCaptureCursor *self)
349 {
350 assert (self != NULL);
351
352 return self->reader;
353 }