File: sysprof-capture-condition.c

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (531 lines) | stat: -rw-r--r-- 16,117 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/* sysprof-capture-condition.c
 *
 * Copyright 2016-2019 Christian Hergert <chergert@redhat.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * Subject to the terms and conditions of this license, each copyright holder
 * and contributor hereby grants to those receiving rights under this license
 * a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
 * irrevocable (except for failure to satisfy the conditions of this license)
 * patent license to make, have made, use, offer to sell, sell, import, and
 * otherwise transfer this software, where such license applies only to those
 * patent claims, already acquired or hereafter acquired, licensable by such
 * copyright holder or contributor that are necessarily infringed by:
 *
 * (a) their Contribution(s) (the licensed copyrights of copyright holders
 *     and non-copyrightable additions of contributors, in source or binary
 *     form) alone; or
 *
 * (b) combination of their Contribution(s) with the work of authorship to
 *     which such Contribution(s) was added by such copyright holder or
 *     contributor, if, at the time the Contribution is added, such addition
 *     causes such combination to be necessarily infringed. The patent license
 *     shall not apply to any other combinations which include the
 *     Contribution.
 *
 * Except as expressly stated above, no rights or licenses from any copyright
 * holder or contributor is granted under this license, whether expressly, by
 * implication, estoppel or otherwise.
 *
 * DISCLAIMER
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * SPDX-License-Identifier: BSD-2-Clause-Patent
 */

#include "config.h"

#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "sysprof-capture-condition.h"
#include "sysprof-capture-util-private.h"
#include "sysprof-macros-internal.h"

/**
 * SECTION:sysprof-capture-condition
 * @title: SysprofCaptureCondition
 *
 * The #SysprofCaptureCondition type is an abstraction on an operation
 * for a sort of AST to the #SysprofCaptureCursor. The goal is that if
 * we abstract the types of fields we want to match in the cursor
 * that we can opportunistically add indexes to speed up the operation
 * later on without changing the implementation of cursor consumers.
 */

typedef enum
{
  SYSPROF_CAPTURE_CONDITION_AND,
  SYSPROF_CAPTURE_CONDITION_OR,
  SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN,
  SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN,
  SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN,
  SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN,
  SYSPROF_CAPTURE_CONDITION_WHERE_FILE,
} SysprofCaptureConditionType;

struct _SysprofCaptureCondition
{
  volatile int ref_count;
  SysprofCaptureConditionType type;
  union {
    struct {
      SysprofCaptureFrameType *data;
      size_t len;
    } where_type_in;
    struct {
      int64_t begin;
      int64_t end;
    } where_time_between;
    struct {
      int32_t *data;
      size_t len;
    } where_pid_in;
    struct {
      unsigned int *data;
      size_t len;
    } where_counter_in;
    struct {
      SysprofCaptureCondition *left;
      SysprofCaptureCondition *right;
    } and, or;
    char *where_file;
  } u;
};

bool
sysprof_capture_condition_match (const SysprofCaptureCondition *self,
                                 const SysprofCaptureFrame     *frame)
{
  assert (self != NULL);
  assert (frame != NULL);

  switch (self->type)
    {
    case SYSPROF_CAPTURE_CONDITION_AND:
      return sysprof_capture_condition_match (self->u.and.left, frame) &&
             sysprof_capture_condition_match (self->u.and.right, frame);

    case SYSPROF_CAPTURE_CONDITION_OR:
      return sysprof_capture_condition_match (self->u.or.left, frame) ||
             sysprof_capture_condition_match (self->u.or.right, frame);

    case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
      for (size_t i = 0; i < self->u.where_type_in.len; i++)
        {
          if (frame->type == self->u.where_type_in.data[i])
            return true;
        }
      return false;

    case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN:
      return (frame->time >= self->u.where_time_between.begin && frame->time <= self->u.where_time_between.end);

    case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
      for (size_t i = 0; i < self->u.where_pid_in.len; i++)
        {
          if (frame->pid == self->u.where_pid_in.data[i])
            return true;
        }
      return false;

    case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN:
      if (frame->type == SYSPROF_CAPTURE_FRAME_CTRSET)
        {
          const SysprofCaptureCounterSet *set = (SysprofCaptureCounterSet *)frame;

          for (size_t i = 0; i < self->u.where_counter_in.len; i++)
            {
              unsigned int counter = self->u.where_counter_in.data[i];

              for (unsigned int j = 0; j < set->n_values; j++)
                {
                  if (counter == set->values[j].ids[0] ||
                      counter == set->values[j].ids[1] ||
                      counter == set->values[j].ids[2] ||
                      counter == set->values[j].ids[3] ||
                      counter == set->values[j].ids[4] ||
                      counter == set->values[j].ids[5] ||
                      counter == set->values[j].ids[6] ||
                      counter == set->values[j].ids[7])
                    return true;
                }
            }
        }
      else if (frame->type == SYSPROF_CAPTURE_FRAME_CTRDEF)
        {
          const SysprofCaptureCounterDefine *def = (SysprofCaptureCounterDefine *)frame;

          for (size_t i = 0; i < self->u.where_counter_in.len; i++)
            {
              unsigned int counter = self->u.where_counter_in.data[i];

              for (unsigned int j = 0; j < def->n_counters; j++)
                {
                  if (def->counters[j].id == counter)
                    return true;
                }
            }
        }

      return false;

    case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
      if (frame->type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
        return false;

      if (self->u.where_file == NULL)
        return false;

      return strcmp (((const SysprofCaptureFileChunk *)frame)->path, self->u.where_file) == 0;

    default:
      break;
    }

  sysprof_assert_not_reached ();

  return false;
}

static SysprofCaptureCondition *
sysprof_capture_condition_init (void)
{
  SysprofCaptureCondition *self;

  self = sysprof_malloc0 (sizeof (SysprofCaptureCondition));
  if (self == NULL)
    return NULL;

  self->ref_count = 1;

  return sysprof_steal_pointer (&self);
}

/* Returns NULL on allocation failure. */
SysprofCaptureCondition *
sysprof_capture_condition_copy (const SysprofCaptureCondition *self)
{
  switch (self->type)
    {
    case SYSPROF_CAPTURE_CONDITION_AND:
      return sysprof_capture_condition_new_and (
        sysprof_capture_condition_copy (self->u.and.left),
        sysprof_capture_condition_copy (self->u.and.right));

    case SYSPROF_CAPTURE_CONDITION_OR:
      return sysprof_capture_condition_new_or (
        sysprof_capture_condition_copy (self->u.or.left),
        sysprof_capture_condition_copy (self->u.or.right));

    case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
      return sysprof_capture_condition_new_where_type_in (
          self->u.where_type_in.len,
          self->u.where_type_in.data);

    case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN:
      return sysprof_capture_condition_new_where_time_between (
        self->u.where_time_between.begin,
        self->u.where_time_between.end);

    case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
      return sysprof_capture_condition_new_where_pid_in (
          self->u.where_pid_in.len,
          self->u.where_pid_in.data);

    case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN:
      return sysprof_capture_condition_new_where_counter_in (
          self->u.where_counter_in.len,
          self->u.where_counter_in.data);

    case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
      return sysprof_capture_condition_new_where_file (self->u.where_file);

    default:
      break;
    }

  sysprof_assert_not_reached ();
  return NULL;
}

static void
sysprof_capture_condition_finalize (SysprofCaptureCondition *self)
{
  switch (self->type)
    {
    case SYSPROF_CAPTURE_CONDITION_AND:
      sysprof_capture_condition_unref (self->u.and.left);
      sysprof_capture_condition_unref (self->u.and.right);
      break;

    case SYSPROF_CAPTURE_CONDITION_OR:
      sysprof_capture_condition_unref (self->u.or.left);
      sysprof_capture_condition_unref (self->u.or.right);
      break;

    case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
      free (self->u.where_type_in.data);
      break;

    case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN:
      break;

    case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
      free (self->u.where_pid_in.data);
      break;

    case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN:
      free (self->u.where_counter_in.data);
      break;

    case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
      free (self->u.where_file);
      break;

    default:
      sysprof_assert_not_reached ();
      break;
    }

  free (self);
}

SysprofCaptureCondition *
sysprof_capture_condition_ref (SysprofCaptureCondition *self)
{
  assert (self != NULL);
  assert (self->ref_count > 0);

  __atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
  return self;
}

void
sysprof_capture_condition_unref (SysprofCaptureCondition *self)
{
  assert (self != NULL);
  assert (self->ref_count > 0);

  if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
    sysprof_capture_condition_finalize (self);
}

/* Returns NULL on allocation failure. */
SysprofCaptureCondition *
sysprof_capture_condition_new_where_type_in (unsigned int                   n_types,
                                             const SysprofCaptureFrameType *types)
{
  SysprofCaptureCondition *self;

  assert (types != NULL);

  self = sysprof_capture_condition_init ();
  if (self == NULL)
    return NULL;

  self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN;
  self->u.where_type_in.data = calloc (n_types, sizeof (SysprofCaptureFrameType));
  if (self->u.where_type_in.data == NULL)
    return NULL;
  self->u.where_type_in.len = n_types;
  memcpy (self->u.where_type_in.data, types, sizeof (SysprofCaptureFrameType) * n_types);

  return self;
}

/* Returns NULL on allocation failure. */
SysprofCaptureCondition *
sysprof_capture_condition_new_where_time_between (int64_t begin_time,
                                                  int64_t end_time)
{
  SysprofCaptureCondition *self;

  if SYSPROF_UNLIKELY (begin_time > end_time)
    {
      int64_t tmp = begin_time;
      begin_time = end_time;
      end_time = tmp;
    }

  self = sysprof_capture_condition_init ();
  if (self == NULL)
    return NULL;

  self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN;
  self->u.where_time_between.begin = begin_time;
  self->u.where_time_between.end = end_time;

  return self;
}

/* Returns NULL on allocation failure. */
SysprofCaptureCondition *
sysprof_capture_condition_new_where_pid_in (unsigned int   n_pids,
                                            const int32_t *pids)
{
  SysprofCaptureCondition *self;

  assert (pids != NULL);

  self = sysprof_capture_condition_init ();
  if (self == NULL)
    return NULL;

  self->type = SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN;
  self->u.where_pid_in.data = calloc (n_pids, sizeof (int32_t));
  if (self->u.where_pid_in.data == NULL)
    {
      free (self);
      return NULL;
    }
  self->u.where_pid_in.len = n_pids;
  memcpy (self->u.where_pid_in.data, pids, sizeof (int32_t) * n_pids);

  return self;
}

/* Returns NULL on allocation failure. */
SysprofCaptureCondition *
sysprof_capture_condition_new_where_counter_in (unsigned int        n_counters,
                                                const unsigned int *counters)
{
  SysprofCaptureCondition *self;

  assert (counters != NULL || n_counters == 0);

  self = sysprof_capture_condition_init ();
  if (self == NULL)
    return NULL;

  self->type = SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN;
  self->u.where_counter_in.data = calloc (n_counters, sizeof (unsigned int));
  if (n_counters > 0 && self->u.where_counter_in.data == NULL)
    {
      free (self);
      return NULL;
    }
  self->u.where_counter_in.len = n_counters;

  if (n_counters > 0)
    memcpy (self->u.where_counter_in.data, counters, sizeof (unsigned int) * n_counters);

  return self;
}

/**
 * sysprof_capture_condition_new_and:
 * @left: (transfer full): An #SysprofCaptureCondition
 * @right: (transfer full): An #SysprofCaptureCondition
 *
 * Creates a new #SysprofCaptureCondition that requires both left and right
 * to evaluate to %TRUE.
 *
 * Returns: (transfer full) (nullable): A new #SysprofCaptureCondition, or %NULL
 *    on allocation failure.
 */
SysprofCaptureCondition *
sysprof_capture_condition_new_and (SysprofCaptureCondition *left,
                                   SysprofCaptureCondition *right)
{
  SysprofCaptureCondition *self;

  assert (left != NULL);
  assert (right != NULL);

  self = sysprof_capture_condition_init ();
  if (self == NULL)
    return NULL;

  self->type = SYSPROF_CAPTURE_CONDITION_AND;
  self->u.and.left = left;
  self->u.and.right = right;

  return self;
}

/**
 * sysprof_capture_condition_new_or:
 * @left: (transfer full): An #SysprofCaptureCondition
 * @right: (transfer full): An #SysprofCaptureCondition
 *
 * Creates a new #SysprofCaptureCondition that requires either left and right
 * to evaluate to %TRUE.
 *
 * Returns: (transfer full) (nullable): A new #SysprofCaptureCondition, or %NULL
 *    on allocation failure.
 */
SysprofCaptureCondition *
sysprof_capture_condition_new_or (SysprofCaptureCondition *left,
                                  SysprofCaptureCondition *right)
{
  SysprofCaptureCondition *self;

  assert (left != NULL);
  assert (right != NULL);

  self = sysprof_capture_condition_init ();
  if (self == NULL)
    return NULL;

  self->type = SYSPROF_CAPTURE_CONDITION_OR;
  self->u.or.left = left;
  self->u.or.right = right;

  return self;
}

/**
 * sysprof_capture_condition_new_where_file:
 * @path: a file path to lookup
 *
 * Creates a new condition that matches #SysprofCaptureFileChunk frames
 * which contain the path @path.
 *
 * Returns: (transfer full) (nullable): a new #SysprofCaptureCondition, or %NULL
 *    on allocation failure.
 */
SysprofCaptureCondition *
sysprof_capture_condition_new_where_file (const char *path)
{
  SysprofCaptureCondition *self;

  assert (path != NULL);

  self = sysprof_capture_condition_init ();
  if (self == NULL)
    return NULL;

  self->type = SYSPROF_CAPTURE_CONDITION_WHERE_FILE;
  self->u.where_file = sysprof_strdup (path);
  if (self->u.where_file == NULL)
    {
      free (self);
      return NULL;
    }

  return self;
}