Debian Codegraph @

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

raw
1 /* sysprof-capture-condition.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 <stdbool.h>
61 #include <stdlib.h>
62 #include <string.h>
63
64 #include "sysprof-capture-condition.h"
65 #include "sysprof-capture-util-private.h"
66 #include "sysprof-macros-internal.h"
67
68 /**
69 * SECTION:sysprof-capture-condition
70 * @title: SysprofCaptureCondition
71 *
72 * The #SysprofCaptureCondition type is an abstraction on an operation
73 * for a sort of AST to the #SysprofCaptureCursor. The goal is that if
74 * we abstract the types of fields we want to match in the cursor
75 * that we can opportunistically add indexes to speed up the operation
76 * later on without changing the implementation of cursor consumers.
77 */
78
79 typedef enum
80 {
81 SYSPROF_CAPTURE_CONDITION_AND,
82 SYSPROF_CAPTURE_CONDITION_OR,
83 SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN,
84 SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN,
85 SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN,
86 SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN,
87 SYSPROF_CAPTURE_CONDITION_WHERE_FILE,
88 } SysprofCaptureConditionType;
89
90 struct _SysprofCaptureCondition
91 {
92 volatile int ref_count;
93 SysprofCaptureConditionType type;
94 union {
95 struct {
96 SysprofCaptureFrameType *data;
97 size_t len;
98 } where_type_in;
99 struct {
100 int64_t begin;
101 int64_t end;
102 } where_time_between;
103 struct {
104 int32_t *data;
105 size_t len;
106 } where_pid_in;
107 struct {
108 unsigned int *data;
109 size_t len;
110 } where_counter_in;
111 struct {
112 SysprofCaptureCondition *left;
113 SysprofCaptureCondition *right;
114 } and, or;
115 char *where_file;
116 } u;
117 };
118
119 bool
120 sysprof_capture_condition_match (const SysprofCaptureCondition *self,
121 const SysprofCaptureFrame *frame)
122 {
123 assert (self != NULL);
124 assert (frame != NULL);
125
126 switch (self->type)
127 {
128 case SYSPROF_CAPTURE_CONDITION_AND:
129 return sysprof_capture_condition_match (self->u.and.left, frame) &&
130 sysprof_capture_condition_match (self->u.and.right, frame);
131
132 case SYSPROF_CAPTURE_CONDITION_OR:
133 return sysprof_capture_condition_match (self->u.or.left, frame) ||
134 sysprof_capture_condition_match (self->u.or.right, frame);
135
136 case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
137 for (size_t i = 0; i < self->u.where_type_in.len; i++)
138 {
139 if (frame->type == self->u.where_type_in.data[i])
140 return true;
141 }
142 return false;
143
144 case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN:
145 return (frame->time >= self->u.where_time_between.begin && frame->time <= self->u.where_time_between.end);
146
147 case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
148 for (size_t i = 0; i < self->u.where_pid_in.len; i++)
149 {
150 if (frame->pid == self->u.where_pid_in.data[i])
151 return true;
152 }
153 return false;
154
155 case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN:
156 if (frame->type == SYSPROF_CAPTURE_FRAME_CTRSET)
157 {
158 const SysprofCaptureCounterSet *set = (SysprofCaptureCounterSet *)frame;
159
160 for (size_t i = 0; i < self->u.where_counter_in.len; i++)
161 {
162 unsigned int counter = self->u.where_counter_in.data[i];
163
164 for (unsigned int j = 0; j < set->n_values; j++)
165 {
166 if (counter == set->values[j].ids[0] ||
167 counter == set->values[j].ids[1] ||
168 counter == set->values[j].ids[2] ||
169 counter == set->values[j].ids[3] ||
170 counter == set->values[j].ids[4] ||
171 counter == set->values[j].ids[5] ||
172 counter == set->values[j].ids[6] ||
173 counter == set->values[j].ids[7])
174 return true;
175 }
176 }
177 }
178 else if (frame->type == SYSPROF_CAPTURE_FRAME_CTRDEF)
179 {
180 const SysprofCaptureCounterDefine *def = (SysprofCaptureCounterDefine *)frame;
181
182 for (size_t i = 0; i < self->u.where_counter_in.len; i++)
183 {
184 unsigned int counter = self->u.where_counter_in.data[i];
185
186 for (unsigned int j = 0; j < def->n_counters; j++)
187 {
188 if (def->counters[j].id == counter)
189 return true;
190 }
191 }
192 }
193
194 return false;
195
196 case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
197 if (frame->type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
198 return false;
199
200 if (self->u.where_file == NULL)
201 return false;
202
203 return strcmp (((const SysprofCaptureFileChunk *)frame)->path, self->u.where_file) == 0;
204
205 default:
206 break;
207 }
208
209 sysprof_assert_not_reached ();
210
211 return false;
212 }
213
214 static SysprofCaptureCondition *
215 sysprof_capture_condition_init (void)
216 {
217 SysprofCaptureCondition *self;
218
219 self = sysprof_malloc0 (sizeof (SysprofCaptureCondition));
220 if (self == NULL)
221 return NULL;
222
223 self->ref_count = 1;
224
225 return sysprof_steal_pointer (&self);
226 }
227
228 /* Returns NULL on allocation failure. */
229 SysprofCaptureCondition *
230 sysprof_capture_condition_copy (const SysprofCaptureCondition *self)
231 {
232 switch (self->type)
233 {
234 case SYSPROF_CAPTURE_CONDITION_AND:
235 return sysprof_capture_condition_new_and (
236 sysprof_capture_condition_copy (self->u.and.left),
237 sysprof_capture_condition_copy (self->u.and.right));
238
239 case SYSPROF_CAPTURE_CONDITION_OR:
240 return sysprof_capture_condition_new_or (
241 sysprof_capture_condition_copy (self->u.or.left),
242 sysprof_capture_condition_copy (self->u.or.right));
243
244 case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
245 return sysprof_capture_condition_new_where_type_in (
246 self->u.where_type_in.len,
247 self->u.where_type_in.data);
248
249 case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN:
250 return sysprof_capture_condition_new_where_time_between (
251 self->u.where_time_between.begin,
252 self->u.where_time_between.end);
253
254 case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
255 return sysprof_capture_condition_new_where_pid_in (
256 self->u.where_pid_in.len,
257 self->u.where_pid_in.data);
258
259 case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN:
260 return sysprof_capture_condition_new_where_counter_in (
261 self->u.where_counter_in.len,
262 self->u.where_counter_in.data);
263
264 case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
265 return sysprof_capture_condition_new_where_file (self->u.where_file);
266
267 default:
268 break;
269 }
270
271 sysprof_assert_not_reached ();
272 return NULL;
273 }
274
275 static void
276 sysprof_capture_condition_finalize (SysprofCaptureCondition *self)
277 {
278 switch (self->type)
279 {
280 case SYSPROF_CAPTURE_CONDITION_AND:
281 sysprof_capture_condition_unref (self->u.and.left);
282 sysprof_capture_condition_unref (self->u.and.right);
283 break;
284
285 case SYSPROF_CAPTURE_CONDITION_OR:
286 sysprof_capture_condition_unref (self->u.or.left);
287 sysprof_capture_condition_unref (self->u.or.right);
288 break;
289
290 case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
291 free (self->u.where_type_in.data);
292 break;
293
294 case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN:
295 break;
296
297 case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
298 free (self->u.where_pid_in.data);
299 break;
300
301 case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN:
302 free (self->u.where_counter_in.data);
303 break;
304
305 case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
306 free (self->u.where_file);
307 break;
308
309 default:
310 sysprof_assert_not_reached ();
311 break;
312 }
313
314 free (self);
315 }
316
317 SysprofCaptureCondition *
318 sysprof_capture_condition_ref (SysprofCaptureCondition *self)
319 {
320 assert (self != NULL);
321 assert (self->ref_count > 0);
322
323 __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
324 return self;
325 }
326
327 void
328 sysprof_capture_condition_unref (SysprofCaptureCondition *self)
329 {
330 assert (self != NULL);
331 assert (self->ref_count > 0);
332
333 if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
334 sysprof_capture_condition_finalize (self);
335 }
336
337 /* Returns NULL on allocation failure. */
338 SysprofCaptureCondition *
339 sysprof_capture_condition_new_where_type_in (unsigned int n_types,
340 const SysprofCaptureFrameType *types)
341 {
342 SysprofCaptureCondition *self;
343
344 assert (types != NULL);
345
346 self = sysprof_capture_condition_init ();
347 if (self == NULL)
348 return NULL;
349
350 self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN;
351 self->u.where_type_in.data = calloc (n_types, sizeof (SysprofCaptureFrameType));
352 if (self->u.where_type_in.data == NULL)
353 return NULL;
354 self->u.where_type_in.len = n_types;
355 memcpy (self->u.where_type_in.data, types, sizeof (SysprofCaptureFrameType) * n_types);
356
357 return self;
358 }
359
360 /* Returns NULL on allocation failure. */
361 SysprofCaptureCondition *
362 sysprof_capture_condition_new_where_time_between (int64_t begin_time,
363 int64_t end_time)
364 {
365 SysprofCaptureCondition *self;
366
367 if SYSPROF_UNLIKELY (begin_time > end_time)
368 {
369 int64_t tmp = begin_time;
370 begin_time = end_time;
371 end_time = tmp;
372 }
373
374 self = sysprof_capture_condition_init ();
375 if (self == NULL)
376 return NULL;
377
378 self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN;
379 self->u.where_time_between.begin = begin_time;
380 self->u.where_time_between.end = end_time;
381
382 return self;
383 }
384
385 /* Returns NULL on allocation failure. */
386 SysprofCaptureCondition *
387 sysprof_capture_condition_new_where_pid_in (unsigned int n_pids,
388 const int32_t *pids)
389 {
390 SysprofCaptureCondition *self;
391
392 assert (pids != NULL);
393
394 self = sysprof_capture_condition_init ();
395 if (self == NULL)
396 return NULL;
397
398 self->type = SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN;
399 self->u.where_pid_in.data = calloc (n_pids, sizeof (int32_t));
400 if (self->u.where_pid_in.data == NULL)
401 {
402 free (self);
403 return NULL;
404 }
405 self->u.where_pid_in.len = n_pids;
406 memcpy (self->u.where_pid_in.data, pids, sizeof (int32_t) * n_pids);
407
408 return self;
409 }
410
411 /* Returns NULL on allocation failure. */
412 SysprofCaptureCondition *
413 sysprof_capture_condition_new_where_counter_in (unsigned int n_counters,
414 const unsigned int *counters)
415 {
416 SysprofCaptureCondition *self;
417
418 assert (counters != NULL || n_counters == 0);
419
420 self = sysprof_capture_condition_init ();
421 if (self == NULL)
422 return NULL;
423
424 self->type = SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN;
425 self->u.where_counter_in.data = calloc (n_counters, sizeof (unsigned int));
426 if (n_counters > 0 && self->u.where_counter_in.data == NULL)
427 {
428 free (self);
429 return NULL;
430 }
431 self->u.where_counter_in.len = n_counters;
432
433 if (n_counters > 0)
434 memcpy (self->u.where_counter_in.data, counters, sizeof (unsigned int) * n_counters);
435
436 return self;
437 }
438
439 /**
440 * sysprof_capture_condition_new_and:
441 * @left: (transfer full): An #SysprofCaptureCondition
442 * @right: (transfer full): An #SysprofCaptureCondition
443 *
444 * Creates a new #SysprofCaptureCondition that requires both left and right
445 * to evaluate to %TRUE.
446 *
447 * Returns: (transfer full) (nullable): A new #SysprofCaptureCondition, or %NULL
448 * on allocation failure.
449 */
450 SysprofCaptureCondition *
451 sysprof_capture_condition_new_and (SysprofCaptureCondition *left,
452 SysprofCaptureCondition *right)
453 {
454 SysprofCaptureCondition *self;
455
456 assert (left != NULL);
457 assert (right != NULL);
458
459 self = sysprof_capture_condition_init ();
460 if (self == NULL)
461 return NULL;
462
463 self->type = SYSPROF_CAPTURE_CONDITION_AND;
464 self->u.and.left = left;
465 self->u.and.right = right;
466
467 return self;
468 }
469
470 /**
471 * sysprof_capture_condition_new_or:
472 * @left: (transfer full): An #SysprofCaptureCondition
473 * @right: (transfer full): An #SysprofCaptureCondition
474 *
475 * Creates a new #SysprofCaptureCondition that requires either left and right
476 * to evaluate to %TRUE.
477 *
478 * Returns: (transfer full) (nullable): A new #SysprofCaptureCondition, or %NULL
479 * on allocation failure.
480 */
481 SysprofCaptureCondition *
482 sysprof_capture_condition_new_or (SysprofCaptureCondition *left,
483 SysprofCaptureCondition *right)
484 {
485 SysprofCaptureCondition *self;
486
487 assert (left != NULL);
488 assert (right != NULL);
489
490 self = sysprof_capture_condition_init ();
491 if (self == NULL)
492 return NULL;
493
494 self->type = SYSPROF_CAPTURE_CONDITION_OR;
495 self->u.or.left = left;
496 self->u.or.right = right;
497
498 return self;
499 }
500
501 /**
502 * sysprof_capture_condition_new_where_file:
503 * @path: a file path to lookup
504 *
505 * Creates a new condition that matches #SysprofCaptureFileChunk frames
506 * which contain the path @path.
507 *
508 * Returns: (transfer full) (nullable): a new #SysprofCaptureCondition, or %NULL
509 * on allocation failure.
510 */
511 SysprofCaptureCondition *
512 sysprof_capture_condition_new_where_file (const char *path)
513 {
514 SysprofCaptureCondition *self;
515
516 assert (path != NULL);
517
518 self = sysprof_capture_condition_init ();
519 if (self == NULL)
520 return NULL;
521
522 self->type = SYSPROF_CAPTURE_CONDITION_WHERE_FILE;
523 self->u.where_file = sysprof_strdup (path);
524 if (self->u.where_file == NULL)
525 {
526 free (self);
527 return NULL;
528 }
529
530 return self;
531 }