summaryrefslogtreecommitdiff
path: root/gsk/gskprofiler.c
blob: 2a0970282646cd67b6de70d7da90bf0d6a6577c7 (plain)
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
#include "config.h"

#include "gskprofilerprivate.h"

#define MAX_SAMPLES     32

typedef struct {
  GQuark id;
  char *description;
  gint64 value;
  gint64 n_samples;
  gboolean can_reset : 1;
} NamedCounter;

typedef struct {
  GQuark id;
  char *description;
  gint64 value;
  gint64 start_time;
  gint64 min_value;
  gint64 max_value;
  gint64 avg_value;
  gint64 n_samples;
  gboolean in_flight : 1;
  gboolean can_reset : 1;
  gboolean invert : 1;
} NamedTimer;

typedef struct {
  GQuark id;
  gint64 value;
} Sample;

struct _GskProfiler
{
  GObject parent_instance;

  GHashTable *counters;
  GHashTable *timers;

  Sample timer_samples[MAX_SAMPLES];
  guint last_sample;
};

G_DEFINE_TYPE (GskProfiler, gsk_profiler, G_TYPE_OBJECT)

static void
named_counter_free (gpointer data)
{
  NamedCounter *counter = data;

  if (data == NULL)
    return;

  g_free (counter->description);

  g_slice_free (NamedCounter, counter);
}

static void
named_timer_free (gpointer data)
{
  NamedTimer *timer = data;

  if (data == NULL)
    return;

  g_free (timer->description);

  g_slice_free (NamedTimer, timer);
}

static void
gsk_profiler_finalize (GObject *gobject)
{
  GskProfiler *self = GSK_PROFILER (gobject);

  g_clear_pointer (&self->counters, g_hash_table_unref);
  g_clear_pointer (&self->timers, g_hash_table_unref);

  G_OBJECT_CLASS (gsk_profiler_parent_class)->finalize (gobject);
}

static void
gsk_profiler_class_init (GskProfilerClass *klass)
{
  G_OBJECT_CLASS (klass)->finalize = gsk_profiler_finalize;
}

static void
gsk_profiler_init (GskProfiler *self)
{
  self->counters = g_hash_table_new_full (g_direct_hash, g_direct_equal,
                                          NULL,
                                          named_counter_free);
  self->timers = g_hash_table_new_full (g_direct_hash, g_direct_equal,
                                        NULL,
                                        named_timer_free);
}

GskProfiler *
gsk_profiler_new (void)
{
  return g_object_new (GSK_TYPE_PROFILER, NULL);
}

static NamedCounter *
named_counter_new (GQuark      id,
                   const char *description,
                   gboolean    can_reset)
{
  NamedCounter *res = g_slice_new0 (NamedCounter);

  res->id = id;
  res->description = g_strdup (description);
  res->can_reset = can_reset;

  return res;
}

static NamedCounter *
gsk_profiler_get_counter (GskProfiler *profiler,
                          GQuark       id)
{
  return g_hash_table_lookup (profiler->counters, GINT_TO_POINTER (id));
}

GQuark
gsk_profiler_add_counter (GskProfiler *profiler,
                          const char  *counter_name,
                          const char  *description,
                          gboolean     can_reset)
{
  NamedCounter *counter;
  GQuark id;

  g_return_val_if_fail (GSK_IS_PROFILER (profiler), 0);

  id = g_quark_from_string (counter_name);
  counter = gsk_profiler_get_counter (profiler, id);
  if (counter != NULL)
    {
      g_critical ("Cannot add a counter '%s' as one already exists.", counter_name);
      return counter->id;
    }

  counter = named_counter_new (id, description, can_reset);
  g_hash_table_insert (profiler->counters, GINT_TO_POINTER (id), counter);

  return counter->id;
}

static NamedTimer *
named_timer_new (GQuark      id,
                 const char *description,
                 gboolean    invert,
                 gboolean    can_reset)
{
  NamedTimer *res = g_slice_new0 (NamedTimer);

  res->id = id;
  res->description = g_strdup (description);
  res->invert = invert;
  res->can_reset = can_reset;

  return res;
}

static NamedTimer *
gsk_profiler_get_timer (GskProfiler *profiler,
                        GQuark       id)
{
  return g_hash_table_lookup (profiler->timers, GINT_TO_POINTER (id));
}

GQuark
gsk_profiler_add_timer (GskProfiler *profiler,
                        const char  *timer_name,
                        const char  *description,
                        gboolean     invert,
                        gboolean     can_reset)
{
  NamedTimer *timer;
  GQuark id;

  g_return_val_if_fail (GSK_IS_PROFILER (profiler), 0);

  id = g_quark_from_string (timer_name);
  timer = gsk_profiler_get_timer (profiler, id);
  if (timer != NULL)
    {
      g_critical ("Cannot add a timer '%s' as one already exists.", timer_name);
      return timer->id;
    }

  timer = named_timer_new (id, description, invert, can_reset);
  g_hash_table_insert (profiler->timers, GINT_TO_POINTER (id), timer);

  return timer->id;
}

void
gsk_profiler_counter_inc (GskProfiler *profiler,
                          GQuark       counter_id)
{
  gsk_profiler_counter_add (profiler, counter_id, 1);
}

void
gsk_profiler_counter_set (GskProfiler *profiler,
                          GQuark       counter_id,
                          gint64       value)
{
  NamedCounter *counter;

  g_return_if_fail (GSK_IS_PROFILER (profiler));

  counter = gsk_profiler_get_counter (profiler, counter_id);
  if (counter == NULL)
    {
      g_critical ("No counter '%s' (id:%d) found; did you forget to call gsk_profiler_add_counter()?",
                  g_quark_to_string (counter_id), counter_id);
      return;
    }

  counter->value = value;
}

void
gsk_profiler_counter_add (GskProfiler *profiler,
                          GQuark       counter_id,
                          gint64       increment)
{
  NamedCounter *counter;

  g_return_if_fail (GSK_IS_PROFILER (profiler));

  counter = gsk_profiler_get_counter (profiler, counter_id);
  if (counter == NULL)
    {
      g_critical ("No counter '%s' (id:%d) found; did you forget to call gsk_profiler_add_counter()?",
                  g_quark_to_string (counter_id), counter_id);
      return;
    }

  counter->value += increment;
}

void
gsk_profiler_timer_begin (GskProfiler *profiler,
                          GQuark       timer_id)
{
  NamedTimer *timer;

  g_return_if_fail (GSK_IS_PROFILER (profiler));

  timer = gsk_profiler_get_timer (profiler, timer_id);
  if (timer == NULL)
    return;

  if (timer->in_flight)
    return;

  timer->in_flight = TRUE;
  timer->start_time = g_get_monotonic_time () * 1000;
}

gint64
gsk_profiler_timer_end (GskProfiler *profiler,
                        GQuark       timer_id)
{
  NamedTimer *timer;
  gint64 diff;

  g_return_val_if_fail (GSK_IS_PROFILER (profiler), 0);

  timer = gsk_profiler_get_timer (profiler, timer_id);
  if (timer == NULL)
    {
      g_critical ("No timer '%s' (id:%d) found; did you forget to call gsk_profiler_add_timer()?",
                  g_quark_to_string (timer_id), timer_id);
      return 0;
    }

  if (!timer->in_flight)
    {
      g_critical ("Timer '%s' (id:%d) is not running; did you forget to call gsk_profiler_timer_begin()?",
                  g_quark_to_string (timer->id), timer->id);
      return 0;
    }

  diff = (g_get_monotonic_time () * 1000) - timer->start_time;

  timer->in_flight = FALSE;
  timer->value += diff; 

  return diff;
}

void
gsk_profiler_timer_set (GskProfiler *profiler,
                        GQuark       timer_id,
                        gint64       value)
{
  NamedTimer *timer;

  g_return_if_fail (GSK_IS_PROFILER (profiler));

  timer = gsk_profiler_get_timer (profiler, timer_id);
  if (timer == NULL)
    {
      g_critical ("No timer '%s' (id:%d) found; did you forget to call gsk_profiler_add_timer()?",
                  g_quark_to_string (timer_id), timer_id);
      return;
    }

  if (timer->in_flight)
    {
      g_critical ("Timer '%s' (id:%d) is running; are you sure you don't want to call "
                  "gsk_profiler_timer_end() instead of gsk_profiler_timer_set()?",
                  g_quark_to_string (timer_id), timer_id);
    }

  timer->value = value;
}

gint64
gsk_profiler_counter_get (GskProfiler *profiler,
                          GQuark       counter_id)
{
  NamedCounter *counter;

  g_return_val_if_fail (GSK_IS_PROFILER (profiler), 0);

  counter = gsk_profiler_get_counter (profiler, counter_id);
  if (counter == NULL)
    {
      g_critical ("No counter '%s' (id:%d) found; did you forget to call gsk_profiler_add_counter()?",
                  g_quark_to_string (counter_id), counter_id);
      return 0;
    }

  return counter->value;
}

gint64
gsk_profiler_timer_get (GskProfiler *profiler,
                        GQuark       timer_id)
{
  NamedTimer *timer;

  g_return_val_if_fail (GSK_IS_PROFILER (profiler), 0);

  timer = gsk_profiler_get_timer (profiler, timer_id);
  if (timer == NULL)
    {
      g_critical ("No timer '%s' (id:%d) found; did you forget to call gsk_profiler_add_timer()?",
                  g_quark_to_string (timer_id), timer_id);
      return 0;
    }

  if (timer->invert)
    return (gint64) (1000000000.0 / (double) timer->value);

  return timer->value;
}

void
gsk_profiler_reset (GskProfiler *profiler)
{
  GHashTableIter iter;
  gpointer value_p = NULL;

  g_return_if_fail (GSK_IS_PROFILER (profiler));

  g_hash_table_iter_init (&iter, profiler->counters);
  while (g_hash_table_iter_next (&iter, NULL, &value_p))
    {
      NamedCounter *counter = value_p;

      if (counter->can_reset)
        counter->value = 0;
    }

  g_hash_table_iter_init (&iter, profiler->timers);
  while (g_hash_table_iter_next (&iter, NULL, &value_p))
    {
      NamedTimer *timer = value_p;

      if (timer->can_reset)
        {
          timer->value = 0;
          timer->min_value = 0;
          timer->max_value = 0;
          timer->avg_value = 0;
          timer->n_samples = 0;
        }
    }

  profiler->last_sample = 0;
}

void
gsk_profiler_push_samples (GskProfiler *profiler)
{
  GHashTableIter iter;
  gpointer value_p = NULL;
  guint last_sample;

  g_return_if_fail (GSK_IS_PROFILER (profiler));

  g_hash_table_iter_init (&iter, profiler->timers);
  while (g_hash_table_iter_next (&iter, NULL, &value_p))
    {
      NamedTimer *timer = value_p;
      Sample *s;

      last_sample = profiler->last_sample;
      profiler->last_sample += 1;
      if (profiler->last_sample == MAX_SAMPLES)
        profiler->last_sample = 0;

      s = &(profiler->timer_samples[last_sample]);
      s->id = timer->id;

      if (timer->invert)
        s->value = (gint64) (1000000000.0 / (double) timer->value);
      else
        s->value = timer->value;
    }
}

void
gsk_profiler_append_counters (GskProfiler *profiler,
                              GString     *buffer)
{
  GHashTableIter iter;
  gpointer value_p = NULL;

  g_return_if_fail (GSK_IS_PROFILER (profiler));
  g_return_if_fail (buffer != NULL);

  g_hash_table_iter_init (&iter, profiler->counters);
  while (g_hash_table_iter_next (&iter, NULL, &value_p))
    {
      NamedCounter *counter = value_p;

      g_string_append_printf (buffer, "%s: %" G_GINT64_FORMAT "\n",
                              counter->description,
                              counter->value);
    }
}

void
gsk_profiler_append_timers (GskProfiler *profiler,
                            GString     *buffer)
{
  GHashTableIter iter;
  gpointer value_p = NULL;
  int i;

  g_return_if_fail (GSK_IS_PROFILER (profiler));
  g_return_if_fail (buffer != NULL);

  g_hash_table_iter_init (&iter, profiler->timers);
  while (g_hash_table_iter_next (&iter, NULL, &value_p))
    {
      NamedTimer *timer = value_p;

      timer->min_value = G_MAXINT64;
      timer->max_value = G_MININT64;
      timer->avg_value = 0;
      timer->n_samples = 0;
    }

  for (i = 0; i < profiler->last_sample; i++)
    {
      Sample *s = &(profiler->timer_samples[i]);
      NamedTimer *timer;

      if (s->id == 0)
        continue;

      timer = gsk_profiler_get_timer (profiler, s->id);
      timer->min_value = MIN (timer->min_value, s->value);
      timer->max_value = MAX (timer->max_value, s->value);
      timer->avg_value += s->value;
      timer->n_samples += 1;
    }

  g_hash_table_iter_init (&iter, profiler->timers);
  while (g_hash_table_iter_next (&iter, NULL, &value_p))
    {
      NamedTimer *timer = value_p;
      const char *unit = timer->invert ? "" : "usec";
      double scale = timer->invert ? 1.0 : 1000.0;

      g_string_append_printf (buffer, "%s (%s): %.2f",
                              timer->description,
                              unit,
                              (double) timer->value / scale);

      if (timer->n_samples > 1)
        {
          timer->avg_value = timer->avg_value / timer->n_samples;
          g_string_append_printf (buffer, " Min: %.2f Avg: %.2f Max: %.2f (%ld samples)",
                                  (double) timer->min_value / scale,
                                  (double) timer->avg_value / scale,
                                  (double) timer->max_value / scale,
                                  timer->n_samples);
        }

      g_string_append (buffer, "\n");
    }
}