summaryrefslogtreecommitdiff
path: root/endless/eosprofile.c
blob: 1f104d9acb249501fab26d71b27b65692afeb953 (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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/* Copyright 2017 Endless Mobile, Inc. */

#include "config.h"

#include "eosprofile-private.h"

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <math.h>

#include "gvdb/gvdb-builder.h"

/**
 * SECTION:profiling
 * @Title: Profiling
 * @Short_description: Profiling tools for applications
 *
 * The profiling API provided by the Endless SDK is a simple tool for defining
 * profiling probes and collecting data over multiple calls.
 *
 * ### Enabling profiling
 *
 * Profile probes try to be as close to zero-cost as possible; they are only
 * enabled if the `EOS_PROFILE` environment variable is set. This means that
 * you can leave the profile probes in your code, and they will be inert until
 * the environment is set up for profiling.
 *
 * ### Using profiling probes
 *
 * Typically, you want to declare a profiling probe at the beginning of the
 * section of the code you wish to measure, and stop it at the end.
 *
 * The profiling probes are identified by a unique name, typically expressed
 * as a path, like `/com/example/ExampleProbe`; this allows creating a tree of
 * probes.
 *
 * If you are using the C API and a GCC-compatible compiler, you will want to
 * use the `g_autoptr()` macro to declare the profiling probe, and have it
 * automatically collected at the end of the scope, for instance:
 *
 * |[<!-- language="C" -->
 * static void
 * some_function (SomeObject *obj)
 * {
 *   some_set_up (obj);
 *
 *   // Here begins the section we wish to profile
 *   g_autoptr(EosProfileProbe) outer = EOS_PROFILE ("/com/example/some-function");
 *
 *   some_expensive_computation (obj);
 *   some_additional_work (obj);
 *
 *   if (some_state (obj))
 *     {
 *       g_autoptr(EosProfileProbe) inner = EOS_PROFILE ("/com/example/some-function/state");
 *
 *       some_more_computation (obj);
 *     }
 * }
 * ]|
 *
 * In the example above, the `outer` probe is created after we performed some
 * operation; since we are not interested into its cost, we are going to
 * ignore it. Additionally, the `inner` probe is created conditionally on some
 * state, so we can also gather information on the actual number of times the
 * inner function is called. In either cases, both the `outer` and `inner`
 * probes are automatically stopped once they get out of scope.
 *
 * ### Capturing profiling data
 *
 * By default, when the `EOS_PROFILE` environment variable is set, you will
 * get a summary at the end of the process, sent to the standard output.
 *
 * It is also possible to redirect the profiling data to a capture file, by
 * setting the `EOS_PROFILE` environment variable to the `capture` value. In
 * that case, the profiling data will be stored in a binary format at the
 * end of the process, and you can use the `eos-profile` tool to extract the
 * probes, timings, and generate a summary. The default filename for the
 * captured data is based on the name of the binary and the process ID, and
 * it's saved under the `$XDG_CACHE_HOME` directory (see: g_get_user_cache_dir()).
 *
 * You can also specify the name of the capture file, by setting the
 * `EOS_PROFILE` environment variable to `capture:/path/to/file`.
 */

static int
sample_compare (gconstpointer a,
                gconstpointer b)
{
  const ProfileSample *sample_a = a;
  const ProfileSample *sample_b = b;

  /* Times are monotonic, so this is always positive */
  gint64 delta_a = sample_a->end_time - sample_a->start_time;
  gint64 delta_b = sample_b->end_time - sample_b->start_time;

  if (delta_a < delta_b)
    return -1;

  if (delta_a > delta_b)
    return 1;

  return 0;
}

#define N_SAMPLES       64

struct _EosProfileProbe {
  volatile int ref_count;

  char *file;
  gint32 line;
  char *function;
  char *name;

  GArray *samples;

  GMutex probe_lock;
};

static EosProfileProbe eos_profile_dummy_probe;

static EosProfileProbe *
eos_profile_probe_new (const char *file,
                       gsize       line,
                       const char *function,
                       const char *name)
{
  EosProfileProbe *res = g_new0 (EosProfileProbe, 1);

  res->ref_count = 1;

  res->name = g_strdup (name);
  res->function = g_strdup (function);
  res->file = g_strdup (file);
  res->line = line;

  res->samples = g_array_sized_new (FALSE, FALSE, sizeof (ProfileSample), N_SAMPLES);

  g_mutex_init (&res->probe_lock);

  return res;
}

static void
eos_profile_probe_destroy (gpointer data)
{
  EosProfileProbe *probe = data;

  g_hash_table_remove (profile_state->probes, probe->name);

  g_array_unref (probe->samples);
  g_free (probe->name);
  g_free (probe->function);
  g_free (probe->file);

  g_free (probe);
}

static EosProfileProbe *
eos_profile_probe_copy (EosProfileProbe *probe)
{
  return probe;
}

static void
eos_profile_probe_free (EosProfileProbe *probe)
{
  /* no-op */
}

G_DEFINE_BOXED_TYPE (EosProfileProbe, eos_profile_probe,
                     eos_profile_probe_copy,
                     eos_profile_probe_free)

/**
 * eos_profile_probe_start:
 * @file: the source file for the probe, typically represented by %__FILE__
 * @line: the line in the source @file, typically represented by %__LINE__
 * @function: the function for the probe, typically represented by %G_STRFUNC
 * @name: a unique name for the probe
 *
 * Starts a profiling probe for @name, creating it if necessary.
 *
 * Returns: (transfer none): a profile probe identifier; use eos_profile_probe_stop()
 *   to stop the profiling on the returned probe
 *
 * Since: 0.6
 */
EosProfileProbe *
eos_profile_probe_start (const char *file,
                         gsize       line,
                         const char *function,
                         const char *name)
{
  /* Don't measure the lock */
  gint64 sample_time = g_get_monotonic_time ();

  /* We can take this out of the lock because by the time we reach
   * eos_profile_probe_stop() the profile state is guaranteed to
   * either always exist, or not
   */
  if (profile_state == NULL)
    return &eos_profile_dummy_probe;

  G_LOCK (profile_state);

  EosProfileProbe *res = g_hash_table_lookup (profile_state->probes, name);
  if (res == NULL)
    {
      res = eos_profile_probe_new (file, line, function, name);

      g_hash_table_insert (profile_state->probes, res->name, res);
    }

  g_array_append_vals (res->samples,
                       &(ProfileSample) {
                         .start_time = sample_time,
                         .end_time = -1
                       },
                       1);

  G_UNLOCK (profile_state);

  return (EosProfileProbe *) res;
}

/**
 * eos_profile_probe_stop:
 * @probe: a #EosProfileProbe
 *
 * Stops a profiling probe started using eos_profile_probe_start().
 *
 * Since: 0.6
 */
void
eos_profile_probe_stop (EosProfileProbe *probe)
{
  if (probe == &eos_profile_dummy_probe)
    return;

  /* Don't measure the lock */
  gint64 sample_time = g_get_monotonic_time ();

  g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&probe->probe_lock);

  /* Ideally, we just want to update the sample we just created, which means
   * picking the last slot in the samples array; in practice, this is what
   * should happen most of the time, unless we end up recursing. In that case
   * we end up with creating a sample while the outer sample is still in
   * flight.
   *
   * If we just recorded the inner samples, we'd end up with a skewed capture,
   * as those samples would inevitably be masking the timing of the outer
   * samples.
   *
   * The easiest approach is to discard the inner samples until we reach the
   * outermost live sample
   */
  int first_in_flight = probe->samples->len - 1;
  for (int i = probe->samples->len - 1; i >= 0; i--)
    {
      const ProfileSample *sample = &g_array_index (probe->samples, ProfileSample, i);

      if (sample->end_time > 0)
        break;

      first_in_flight = i;
    }

  ProfileSample *sample = &g_array_index (probe->samples, ProfileSample, first_in_flight);

  sample->end_time = sample_time;

  if (first_in_flight != probe->samples->len - 1)
    {
      int range = probe->samples->len - first_in_flight - 1;

      g_array_remove_range (probe->samples, first_in_flight + 1, range);
    }
}

void
eos_profile_state_init (void)
{
  static gboolean profile_state_inited;

  if (profile_state_inited)
    return;

  profile_state_inited = TRUE;

  const char *str = getenv ("EOS_PROFILE");
  if (str != NULL)
    {
      profile_state = g_new0 (ProfileState, 1);
      profile_state->probes = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                     NULL,
                                                     eos_profile_probe_destroy);

      int capture_prefix_len = strlen ("capture");

      if (g_ascii_strncasecmp (str, "capture", capture_prefix_len) == 0)
        {
          profile_state->capture = TRUE;

          const char *filename = str + capture_prefix_len;
          if (*filename == ':')
            {
              filename += 1;

              if (*filename != '\0')
                profile_state->capture_file = g_strdup (filename);
            }

          if (profile_state->capture_file == NULL || *profile_state->capture_file == '\0')
            {
              g_autofree char *capture_dir = g_build_filename (g_get_user_cache_dir (),
                                                               "com.endlessm.Sdk.Profile",
                                                               NULL);

              if (g_mkdir_with_parents (capture_dir, 0700) < 0)
                capture_dir = g_get_current_dir ();

              profile_state->capture_file = g_strdup_printf ("%s%s%s.db",
                                                             capture_dir,
                                                             G_DIR_SEPARATOR_S,
                                                             g_get_prgname ());
            }
        }
    }
}

static const double
scale_val (double val)
{
  if (val >= G_USEC_PER_SEC)
    return val / G_USEC_PER_SEC;

  if (val >= 1000)
    return val / 1000.0;

  return val;
}

static const char *
unit_for (double val)
{
  enum {
    SECONDS,
    MILLISECONDS,
    MICROSECONDS
  };

  const char *units[] = {
    [SECONDS] = "s",
    [MILLISECONDS] = "ms",
    [MICROSECONDS] = "µs",
  };

  if (val >= G_USEC_PER_SEC)
    return units[SECONDS];

  if (val >= 1000)
    return units[MILLISECONDS];

  return units[MICROSECONDS];
}

static void
profile_state_dump_to_console (void)
{
  gushort max_columns = 256;

  if (isatty (STDOUT_FILENO))
    {
      struct winsize w;

      ioctl (STDOUT_FILENO, TIOCGWINSZ, &w);

      max_columns = w.ws_col;
    }

  GHashTableIter iter;
  gpointer value;

  g_hash_table_iter_init (&iter, profile_state->probes);
  while (g_hash_table_iter_next (&iter, NULL, &value))
    {
      EosProfileProbe *probe = value;

      /* Take ownership of the samples in order to sort them; we want to
       * pre-sort so that we can easily discard the outliers when doing
       * our analysis, later on
       */
      GArray *sorted_samples = g_steal_pointer (&probe->samples);
      g_array_sort (sorted_samples, sample_compare);

      gint64 min_sample = G_MAXINT64, max_sample = 0;
      gint64 total = 0;

      g_autoptr(GArray) valid_samples = g_array_new (FALSE, FALSE, sizeof (guint));

      for (int i = 0; i < sorted_samples->len; i++)
        {
          const ProfileSample *sample = &g_array_index (sorted_samples, ProfileSample, i);

          gint64 delta = sample->end_time - sample->start_time;

          /* If the probe never got stopped we need to skip this sample */
          if (delta < 0)
            continue;

          g_array_append_val (valid_samples, i);

          if (delta < min_sample)
            min_sample = delta;
          if (delta > max_sample)
            max_sample = delta;

          total += delta;
        }

      g_autofree char *msg = NULL;

      if (valid_samples->len > 1)
        {
          double avg = total / (double) valid_samples->len;
          double s = 0;
          double s_part = 0;

          for (int i = 1; i < valid_samples->len - 1; i++)
            {
              guint idx = g_array_index (valid_samples, guint, i);
              const ProfileSample *sample = &g_array_index (sorted_samples, ProfileSample, idx);

              gint64 delta = sample->end_time - sample->start_time;
              g_assert (delta >= 0);

              double deviation = delta - avg;
              s_part += (deviation * deviation);
            }

          if (valid_samples->len > 1)
            s = sqrt (s_part / (double) valid_samples->len - 1);
          else
            s = 0.0;

          g_autofree char *stddev = g_strdup_printf (", σ:%g", s);

          msg =
            g_strdup_printf ("%d samples: avg:%g %s, min:%d %s, max:%d %s%s)",
                             valid_samples->len,
                             scale_val (avg), unit_for (avg),
                             (int) scale_val (min_sample), unit_for (min_sample),
                             (int) scale_val (max_sample), unit_for (max_sample),
                             s == 0.0 ? "" : stddev);
        }
      else
        {
          msg = g_strdup ("not enough valid samples found");
        }

      g_autofree char *probe_name = NULL;

      int probe_len = strlen (probe->name);
      int msg_len = strlen (msg);
      if (probe_len + msg_len >= (int) max_columns - 2)
        {
          gsize name_len = MAX ((int) max_columns - msg_len, 2);
          probe_name = g_strndup (probe->name, name_len);
          probe_name[name_len - 1] = '~';
        }
      else
        probe_name = g_strdup (probe->name);

      g_print ("%s%*c%s\n",
               probe_name,
               max_columns - strlen (probe_name) - msg_len, ' ',
               msg);
      g_print ("  %s at %s:%d\n\n",
               probe->function,
               probe->file, probe->line);
    }
}

/* Get the immediate parent table in the GVDB table, using the
 * key separator '/' to determine the nesting level. If needed,
 * this function will create the intermediate tables
 */
static GvdbItem *
get_parent (GHashTable *table,
            char       *key,
            int         length)
{
  GvdbItem *grandparent, *parent;

  if (length == 1)
    return NULL;

  while (key[--length - 1] != '/')
    ;

  key[length] = '\0';

  parent = g_hash_table_lookup (table, key);

  if (parent == NULL)
    {
      parent = gvdb_hash_table_insert (table, key);

      grandparent = get_parent (table, key, length);

      if (grandparent != NULL)
        gvdb_item_set_parent (parent, grandparent);
    }

  return parent;
}

void
eos_profile_state_dump (void)
{
  if (profile_state == NULL)
    return;

  if (!profile_state->capture)
    {
      profile_state_dump_to_console ();
      return;
    }

  g_autoptr(GHashTable) db_table = gvdb_hash_table_new (NULL, NULL);

  /* Metadata for the DB */
  g_autofree char *version_key = g_strdup (PROBE_DB_META_VERSION_KEY);
  gsize version_key_len = strlen (version_key);
  GvdbItem *meta = gvdb_hash_table_insert (db_table, PROBE_DB_META_VERSION_KEY);
  gvdb_item_set_parent (meta, get_parent (db_table, version_key, version_key_len));
  gvdb_item_set_value (meta, g_variant_new_int32 (PROBE_DB_VERSION));

  /* Iterate over the probes */
  GHashTableIter iter;
  gpointer value;
  g_hash_table_iter_init (&iter, profile_state->probes);
  while (g_hash_table_iter_next (&iter, NULL, &value))
    {
      EosProfileProbe *probe = value;

      g_autofree char *key = g_strdup (probe->name);
      gsize key_len = strlen (probe->name);

      GvdbItem *item = gvdb_hash_table_insert (db_table, key);
      gvdb_item_set_parent (item, get_parent (db_table, key, key_len));

      GVariantBuilder builder;

      g_variant_builder_init (&builder, G_VARIANT_TYPE ("(sssuua(xx))"));

      g_variant_builder_add (&builder, "s", probe->name);
      g_variant_builder_add (&builder, "s", probe->function);
      g_variant_builder_add (&builder, "s", probe->file);
      g_variant_builder_add (&builder, "u", probe->line);

      g_variant_builder_add (&builder, "u", probe->samples->len);

      /* Take ownership of the samples in order to sort them; we want to
       * pre-sort so that we can easily discard the outliers when doing
       * our analysis, later on
       */
      GArray *sorted_samples = g_steal_pointer (&(probe->samples));
      g_array_sort (sorted_samples, sample_compare);

      g_variant_builder_open (&builder, G_VARIANT_TYPE ("a(xx)"));

      for (int i = 0; i < sorted_samples->len; i++)
        {
          const ProfileSample *sample = &g_array_index (sorted_samples, ProfileSample, i);

          g_variant_builder_open (&builder, G_VARIANT_TYPE ("(xx)"));
          g_variant_builder_add (&builder, "x", sample->start_time);
          g_variant_builder_add (&builder, "x", sample->end_time);
          g_variant_builder_close (&builder);
        }

      g_variant_builder_close (&builder);

      g_array_free (sorted_samples, TRUE);

      gvdb_item_set_value (item, g_variant_builder_end (&builder));
    }

  /* Clean up */
  g_hash_table_unref (profile_state->probes);
  g_free (profile_state->capture_file);
  g_free (profile_state);

  g_autoptr(GError) error = NULL;
  gvdb_table_write_contents (db_table, profile_state->capture_file,
                             G_BYTE_ORDER != G_LITTLE_ENDIAN,
                             &error);

  if (error != NULL)
    g_printerr ("PROFILE: %s\n", error->message);
}