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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
#include "config.h"
#include <string.h>
#include "shell-perf-log.h"
typedef struct _ShellPerfEvent ShellPerfEvent;
typedef struct _ShellPerfStatistic ShellPerfStatistic;
typedef struct _ShellPerfStatisticsClosure ShellPerfStatisticsClosure;
typedef union _ShellPerfStatisticValue ShellPerfStatisticValue;
typedef struct _ShellPerfBlock ShellPerfBlock;
/**
* SECTION:shell-perf-log
* @short_description: Event recorder for performance measurement
*
* ShellPerfLog provides a way for different parts of the code to
* record information for subsequent analysis and interactive
* exploration. Events exist of a timestamp, an event ID, and
* arguments to the event.
*
* Emphasis is placed on storing recorded events in a compact
* fashion so log recording disturbs the execution of the program
* as little as possible, however events should not be recorded
* at too fine a granularity - an event that is recorded once
* per frame or once per user action is appropriate, an event that
* occurs many times per frame is not.
*
* Arguments are identified by a D-Bus style signature; at the moment
* only a limited number of event signatures are supported to
* simplify the code.
*/
struct _ShellPerfLog
{
GObject parent;
GPtrArray *events;
GHashTable *events_by_name;
GPtrArray *statistics;
GHashTable *statistics_by_name;
GPtrArray *statistics_closures;
GQueue *blocks;
gint64 start_time;
gint64 last_time;
guint statistics_timeout_id;
guint enabled : 1;
};
struct _ShellPerfEvent
{
guint16 id;
char *name;
char *description;
char *signature;
};
union _ShellPerfStatisticValue
{
int i;
gint64 x;
};
struct _ShellPerfStatistic
{
ShellPerfEvent *event;
ShellPerfStatisticValue current_value;
ShellPerfStatisticValue last_value;
guint initialized : 1;
guint recorded : 1;
};
struct _ShellPerfStatisticsClosure
{
ShellPerfStatisticsCallback callback;
gpointer user_data;
GDestroyNotify notify;
};
/* The events in the log are stored in a linked list of fixed size
* blocks.
*
* Note that the power-of-two nature of BLOCK_SIZE here is superficial
* since the allocated block has the 'bytes' field and malloc
* overhead. The current value is well below the size that will
* typically be independently mmapped by the malloc implementation so
* it doesn't matter. If we switched to mmapping blocks manually
* (perhaps to avoid polluting malloc statistics), we'd want to use a
* different value of BLOCK_SIZE.
*/
#define BLOCK_SIZE 8192
struct _ShellPerfBlock
{
guint32 bytes;
guchar buffer[BLOCK_SIZE];
};
/* Number of milliseconds between periodic statistics collection when
* events are enabled. Statistics collection can also be explicitly
* triggered.
*/
#define STATISTIC_COLLECTION_INTERVAL_MS 5000
/* Builtin events */
enum {
EVENT_SET_TIME,
EVENT_STATISTICS_COLLECTED
};
G_DEFINE_TYPE(ShellPerfLog, shell_perf_log, G_TYPE_OBJECT);
static gint64
get_time (void)
{
return g_get_monotonic_time ();
}
static void
shell_perf_log_init (ShellPerfLog *perf_log)
{
perf_log->events = g_ptr_array_new ();
perf_log->events_by_name = g_hash_table_new (g_str_hash, g_str_equal);
perf_log->statistics = g_ptr_array_new ();
perf_log->statistics_by_name = g_hash_table_new (g_str_hash, g_str_equal);
perf_log->statistics_closures = g_ptr_array_new ();
perf_log->blocks = g_queue_new ();
/* This event is used when timestamp deltas are greater than
* fits in a gint32. 0xffffffff microseconds is about 70 minutes, so this
* is not going to happen in normal usage. It might happen if performance
* logging is enabled some time after starting the shell */
shell_perf_log_define_event (perf_log, "perf.setTime", "", "x");
g_assert (perf_log->events->len == EVENT_SET_TIME + 1);
/* The purpose of this event is to allow us to optimize out storing
* statistics that haven't changed. We want to mark every time we
* collect statistics even if we don't record any individual
* statistics so that we can distinguish sudden changes from gradual changes.
*
* The argument is the number of microseconds that statistics collection
* took; we record that since statistics collection could start taking
* significant time if we do things like grub around in /proc/
*/
shell_perf_log_define_event (perf_log, "perf.statisticsCollected",
"Finished collecting statistics",
"x");
g_assert (perf_log->events->len == EVENT_STATISTICS_COLLECTED + 1);
perf_log->start_time = perf_log->last_time = get_time();
}
static void
shell_perf_log_class_init (ShellPerfLogClass *class)
{
}
/**
* shell_perf_log_get_default:
*
* Gets the global singleton performance log. This is initially disabled
* and must be explicitly enabled with shell_perf_log_set_enabled().
*
* Return value: (transfer none): the global singleton performance log
*/
ShellPerfLog *
shell_perf_log_get_default (void)
{
static ShellPerfLog *perf_log;
if (perf_log == NULL)
perf_log = g_object_new (SHELL_TYPE_PERF_LOG, NULL);
return perf_log;
}
static gboolean
statistics_timeout (gpointer data)
{
ShellPerfLog *perf_log = data;
shell_perf_log_collect_statistics (perf_log);
return TRUE;
}
/**
* shell_perf_log_set_enabled:
* @perf_log: a #ShellPerfLog
* @enabled: whether to record events
*
* Sets whether events are currently being recorded.
*/
void
shell_perf_log_set_enabled (ShellPerfLog *perf_log,
gboolean enabled)
{
enabled = enabled != FALSE;
if (enabled != perf_log->enabled)
{
perf_log->enabled = enabled;
if (enabled)
{
perf_log->statistics_timeout_id = g_timeout_add (STATISTIC_COLLECTION_INTERVAL_MS,
statistics_timeout,
perf_log);
g_source_set_name_by_id (perf_log->statistics_timeout_id, "[gnome-shell] statistics_timeout");
}
else
{
g_clear_handle_id (&perf_log->statistics_timeout_id, g_source_remove);
}
}
}
static ShellPerfEvent *
define_event (ShellPerfLog *perf_log,
const char *name,
const char *description,
const char *signature)
{
ShellPerfEvent *event;
if (strcmp (signature, "") != 0 &&
strcmp (signature, "s") != 0 &&
strcmp (signature, "i") != 0 &&
strcmp (signature, "x") != 0)
{
g_warning ("Only supported event signatures are '', 's', 'i', and 'x'\n");
return NULL;
}
if (perf_log->events->len == 65536)
{
g_warning ("Maximum number of events defined\n");
return NULL;
}
/* We could do stricter validation, but this will break our JSON dumps */
if (strchr (name, '"') != NULL)
{
g_warning ("Event names can't include '\"'");
return NULL;
}
if (g_hash_table_lookup (perf_log->events_by_name, name) != NULL)
{
g_warning ("Duplicate event event for '%s'\n", name);
return NULL;
}
event = g_slice_new (ShellPerfEvent);
event->id = perf_log->events->len;
event->name = g_strdup (name);
event->signature = g_strdup (signature);
event->description = g_strdup (description);
g_ptr_array_add (perf_log->events, event);
g_hash_table_insert (perf_log->events_by_name, event->name, event);
return event;
}
/**
* shell_perf_log_define_event:
* @perf_log: a #ShellPerfLog
* @name: name of the event. This should of the form
* '<namespace>.<specific eventf'>, for example
* 'clutter.stagePaintDone'.
* @description: human readable description of the event.
* @signature: signature defining the arguments that event takes.
* This is a string of type characters, using the same characters
* as D-Bus or GVariant. Only a very limited number of signatures
* are supported: , '', 's', 'i', and 'x'. This mean respectively:
* no arguments, one string, one 32-bit integer, and one 64-bit
* integer.
*
* Defines a performance event for later recording.
*/
void
shell_perf_log_define_event (ShellPerfLog *perf_log,
const char *name,
const char *description,
const char *signature)
{
define_event (perf_log, name, description, signature);
}
static ShellPerfEvent *
lookup_event (ShellPerfLog *perf_log,
const char *name,
const char *signature)
{
ShellPerfEvent *event = g_hash_table_lookup (perf_log->events_by_name, name);
if (G_UNLIKELY (event == NULL))
{
g_warning ("Discarding unknown event '%s'\n", name);
return NULL;
}
if (G_UNLIKELY (strcmp (event->signature, signature) != 0))
{
g_warning ("Event '%s'; defined with signature '%s', used with '%s'\n",
name, event->signature, signature);
return NULL;
}
return event;
}
static void
record_event (ShellPerfLog *perf_log,
gint64 event_time,
ShellPerfEvent *event,
const guchar *bytes,
size_t bytes_len)
{
ShellPerfBlock *block;
size_t total_bytes;
guint32 time_delta;
guint32 pos;
if (!perf_log->enabled)
return;
total_bytes = sizeof (gint32) + sizeof (gint16) + bytes_len;
if (G_UNLIKELY (bytes_len > BLOCK_SIZE || total_bytes > BLOCK_SIZE))
{
g_warning ("Discarding oversize event '%s'\n", event->name);
return;
}
if (event_time > perf_log->last_time + G_GINT64_CONSTANT(0xffffffff))
{
perf_log->last_time = event_time;
record_event (perf_log, event_time,
lookup_event (perf_log, "perf.setTime", "x"),
(const guchar *)&event_time, sizeof(gint64));
time_delta = 0;
}
else if (event_time < perf_log->last_time)
time_delta = 0;
else
time_delta = (guint32)(event_time - perf_log->last_time);
perf_log->last_time = event_time;
if (perf_log->blocks->tail == NULL ||
total_bytes + ((ShellPerfBlock *)perf_log->blocks->tail->data)->bytes > BLOCK_SIZE)
{
block = g_new (ShellPerfBlock, 1);
block->bytes = 0;
g_queue_push_tail (perf_log->blocks, block);
}
else
{
block = (ShellPerfBlock *)perf_log->blocks->tail->data;
}
pos = block->bytes;
memcpy (block->buffer + pos, &time_delta, sizeof (guint32));
pos += sizeof (guint32);
memcpy (block->buffer + pos, &event->id, sizeof (guint16));
pos += sizeof (guint16);
memcpy (block->buffer + pos, bytes, bytes_len);
pos += bytes_len;
block->bytes = pos;
}
/**
* shell_perf_log_event:
* @perf_log: a #ShellPerfLog
* @name: name of the event
*
* Records a performance event with no arguments.
*/
void
shell_perf_log_event (ShellPerfLog *perf_log,
const char *name)
{
ShellPerfEvent *event = lookup_event (perf_log, name, "");
if (G_UNLIKELY (event == NULL))
return;
record_event (perf_log, get_time(), event, NULL, 0);
}
/**
* shell_perf_log_event_i:
* @perf_log: a #ShellPerfLog
* @name: name of the event
* @arg: the argument
*
* Records a performance event with one 32-bit integer argument.
*/
void
shell_perf_log_event_i (ShellPerfLog *perf_log,
const char *name,
gint32 arg)
{
ShellPerfEvent *event = lookup_event (perf_log, name, "i");
if (G_UNLIKELY (event == NULL))
return;
record_event (perf_log, get_time(), event,
(const guchar *)&arg, sizeof (arg));
}
/**
* shell_perf_log_event_x:
* @perf_log: a #ShellPerfLog
* @name: name of the event
* @arg: the argument
*
* Records a performance event with one 64-bit integer argument.
*/
void
shell_perf_log_event_x (ShellPerfLog *perf_log,
const char *name,
gint64 arg)
{
ShellPerfEvent *event = lookup_event (perf_log, name, "x");
if (G_UNLIKELY (event == NULL))
return;
record_event (perf_log, get_time(), event,
(const guchar *)&arg, sizeof (arg));
}
/**
* shell_perf_log_event_s:
* @perf_log: a #ShellPerfLog
* @name: name of the event
* @arg: the argument
*
* Records a performance event with one string argument.
*/
void
shell_perf_log_event_s (ShellPerfLog *perf_log,
const char *name,
const char *arg)
{
ShellPerfEvent *event = lookup_event (perf_log, name, "s");
if (G_UNLIKELY (event == NULL))
return;
record_event (perf_log, get_time(), event,
(const guchar *)arg, strlen (arg) + 1);
}
/**
* shell_perf_log_define_statistic:
* @name: name of the statistic and of the corresponding event.
* This should follow the same guidelines as for shell_perf_log_define_event()
* @description: human readable description of the statistic.
* @signature: The type of the data stored for statistic. Must
* currently be 'i' or 'x'.
*
* Defines a statistic. A statistic is a numeric value that is stored
* by the performance log and recorded periodically or when
* shell_perf_log_collect_statistics() is called explicitly.
*
* Code that defines a statistic should update it by calling
* the update function for the particular data type of the statistic,
* such as shell_perf_log_update_statistic_i(). This can be done
* at any time, but would normally done inside a function registered
* with shell_perf_log_add_statistics_callback(). These functions
* are called immediately before statistics are recorded.
*/
void
shell_perf_log_define_statistic (ShellPerfLog *perf_log,
const char *name,
const char *description,
const char *signature)
{
ShellPerfEvent *event;
ShellPerfStatistic *statistic;
if (strcmp (signature, "i") != 0 &&
strcmp (signature, "x") != 0)
{
g_warning ("Only supported statistic signatures are 'i' and 'x'\n");
return;
}
event = define_event (perf_log, name, description, signature);
if (event == NULL)
return;
statistic = g_slice_new (ShellPerfStatistic);
statistic->event = event;
statistic->initialized = FALSE;
statistic->recorded = FALSE;
g_ptr_array_add (perf_log->statistics, statistic);
g_hash_table_insert (perf_log->statistics_by_name, event->name, statistic);
}
static ShellPerfStatistic *
lookup_statistic (ShellPerfLog *perf_log,
const char *name,
const char *signature)
{
ShellPerfStatistic *statistic = g_hash_table_lookup (perf_log->statistics_by_name, name);
if (G_UNLIKELY (statistic == NULL))
{
g_warning ("Unknown statistic '%s'\n", name);
return NULL;
}
if (G_UNLIKELY (strcmp (statistic->event->signature, signature) != 0))
{
g_warning ("Statistic '%s'; defined with signature '%s', used with '%s'\n",
name, statistic->event->signature, signature);
return NULL;
}
return statistic;
}
/**
* shell_perf_log_update_statistic_i:
* @perf_log: a #ShellPerfLog
* @name: name of the statistic
* @value: new value for the statistic
*
* Updates the current value of an 32-bit integer statistic.
*/
void
shell_perf_log_update_statistic_i (ShellPerfLog *perf_log,
const char *name,
gint32 value)
{
ShellPerfStatistic *statistic;
statistic = lookup_statistic (perf_log, name, "i");
if (G_UNLIKELY (statistic == NULL))
return;
statistic->current_value.i = value;
statistic->initialized = TRUE;
}
/**
* shell_perf_log_update_statistic_x:
* @perf_log: a #ShellPerfLog
* @name: name of the statistic
* @value: new value for the statistic
*
* Updates the current value of an 64-bit integer statistic.
*/
void
shell_perf_log_update_statistic_x (ShellPerfLog *perf_log,
const char *name,
gint64 value)
{
ShellPerfStatistic *statistic;
statistic = lookup_statistic (perf_log, name, "x");
if (G_UNLIKELY (statistic == NULL))
return;
statistic->current_value.x = value;
statistic->initialized = TRUE;
}
/**
* shell_perf_log_add_statistics_callback:
* @perf_log: a #ShellPerfLog
* @callback: function to call before recording statistics
* @user_data: data to pass to @callback
* @notify: function to call when @user_data is no longer needed
*
* Adds a function that will be called before statistics are recorded.
* The function would typically compute one or more statistics values
* and call a function such as shell_perf_log_update_statistic_i()
* to update the value that will be recorded.
*/
void
shell_perf_log_add_statistics_callback (ShellPerfLog *perf_log,
ShellPerfStatisticsCallback callback,
gpointer user_data,
GDestroyNotify notify)
{
ShellPerfStatisticsClosure *closure = g_slice_new (ShellPerfStatisticsClosure);
closure->callback = callback;
closure->user_data = user_data;
closure->notify = notify;
g_ptr_array_add (perf_log->statistics_closures, closure);
}
/**
* shell_perf_log_collect_statistics:
* @perf_log: a #ShellPerfLog
*
* Calls all the update functions added with
* shell_perf_log_add_statistics_callback() and then records events
* for all statistics, followed by a perf.statisticsCollected event.
*/
void
shell_perf_log_collect_statistics (ShellPerfLog *perf_log)
{
gint64 event_time = get_time ();
gint64 collection_time;
guint i;
if (!perf_log->enabled)
return;
for (i = 0; i < perf_log->statistics_closures->len; i++)
{
ShellPerfStatisticsClosure *closure;
closure = g_ptr_array_index (perf_log->statistics_closures, i);
closure->callback (perf_log, closure->user_data);
}
collection_time = get_time() - event_time;
for (i = 0; i < perf_log->statistics->len; i++)
{
ShellPerfStatistic *statistic = g_ptr_array_index (perf_log->statistics, i);
if (!statistic->initialized)
continue;
switch (statistic->event->signature[0])
{
case 'i':
if (!statistic->recorded ||
statistic->current_value.i != statistic->last_value.i)
{
record_event (perf_log, event_time, statistic->event,
(const guchar *)&statistic->current_value.i,
sizeof (gint32));
statistic->last_value.i = statistic->current_value.i;
statistic->recorded = TRUE;
}
break;
case 'x':
if (!statistic->recorded ||
statistic->current_value.x != statistic->last_value.x)
{
record_event (perf_log, event_time, statistic->event,
(const guchar *)&statistic->current_value.x,
sizeof (gint64));
statistic->last_value.x = statistic->current_value.x;
statistic->recorded = TRUE;
}
break;
default:
g_warning ("Unsupported signature in event");
break;
}
}
record_event (perf_log, event_time,
g_ptr_array_index (perf_log->events, EVENT_STATISTICS_COLLECTED),
(const guchar *)&collection_time, sizeof (gint64));
}
/**
* shell_perf_log_replay:
* @perf_log: a #ShellPerfLog
* @replay_function: (scope call): function to call for each event in the log
* @user_data: data to pass to @replay_function
*
* Replays the log by calling the given function for each event
* in the log.
*/
void
shell_perf_log_replay (ShellPerfLog *perf_log,
ShellPerfReplayFunction replay_function,
gpointer user_data)
{
gint64 event_time = perf_log->start_time;
GList *iter;
for (iter = perf_log->blocks->head; iter; iter = iter->next)
{
ShellPerfBlock *block = iter->data;
guint32 pos = 0;
while (pos < block->bytes)
{
ShellPerfEvent *event;
guint16 id;
guint32 time_delta;
GValue arg = { 0, };
memcpy (&time_delta, block->buffer + pos, sizeof (guint32));
pos += sizeof (guint32);
memcpy (&id, block->buffer + pos, sizeof (guint16));
pos += sizeof (guint16);
if (id == EVENT_SET_TIME)
{
/* Internal, we don't include in the replay */
memcpy (&event_time, block->buffer + pos, sizeof (gint64));
pos += sizeof (gint64);
continue;
}
else
{
event_time += time_delta;
}
event = g_ptr_array_index (perf_log->events, id);
if (strcmp (event->signature, "") == 0)
{
/* We need to pass something, so pass an empty string */
g_value_init (&arg, G_TYPE_STRING);
}
else if (strcmp (event->signature, "i") == 0)
{
gint32 l;
memcpy (&l, block->buffer + pos, sizeof (gint32));
pos += sizeof (gint32);
g_value_init (&arg, G_TYPE_INT);
g_value_set_int (&arg, l);
}
else if (strcmp (event->signature, "x") == 0)
{
gint64 l;
memcpy (&l, block->buffer + pos, sizeof (gint64));
pos += sizeof (gint64);
g_value_init (&arg, G_TYPE_INT64);
g_value_set_int64 (&arg, l);
}
else if (strcmp (event->signature, "s") == 0)
{
g_value_init (&arg, G_TYPE_STRING);
g_value_set_string (&arg, (char *)block->buffer + pos);
pos += strlen ((char *)(block->buffer + pos)) + 1;
}
replay_function (event_time, event->name, event->signature, &arg, user_data);
g_value_unset (&arg);
}
}
}
static char *
escape_quotes (const char *input)
{
GString *result;
const char *p;
if (strchr (input, '"') == NULL)
return (char *)input;
result = g_string_new (NULL);
for (p = input; *p; p++)
{
if (*p == '"')
g_string_append (result, "\\\"");
else
g_string_append_c (result, *p);
}
return g_string_free (result, FALSE);
}
static gboolean
write_string (GOutputStream *out,
const char *str,
GError **error)
{
return g_output_stream_write_all (out, str, strlen (str),
NULL, NULL,
error);
}
/**
* shell_perf_log_dump_events:
* @perf_log: a #ShellPerfLog
* @out: output stream into which to write the event definitions
* @error: location to store #GError, or %NULL
*
* Dump the definition of currently defined events and statistics, formatted
* as JSON, to the specified output stream. The JSON output is an array,
* with each element being a dictionary of the form:
*
* { name: <name of event>,
* description: <description of string,
* statistic: true } (only for statistics)
*
* Return value: %TRUE if the dump succeeded. %FALSE if an IO error occurred
*/
gboolean
shell_perf_log_dump_events (ShellPerfLog *perf_log,
GOutputStream *out,
GError **error)
{
GString *output;
guint i;
output = g_string_new (NULL);
g_string_append (output, "[ ");
for (i = 0; i < perf_log->events->len; i++)
{
ShellPerfEvent *event = g_ptr_array_index (perf_log->events, i);
char *escaped_description = escape_quotes (event->description);
gboolean is_statistic = g_hash_table_lookup (perf_log->statistics_by_name, event->name) != NULL;
if (i != 0)
g_string_append (output, ",\n ");
g_string_append_printf (output,
"{ \"name\": \"%s\",\n"
" \"description\": \"%s\"",
event->name, escaped_description);
if (is_statistic)
g_string_append (output, ",\n \"statistic\": true");
g_string_append (output, " }");
if (escaped_description != event->description)
g_free (escaped_description);
}
g_string_append (output, " ]");
return write_string (out, g_string_free (output, FALSE), error);
}
typedef struct {
GOutputStream *out;
GError *error;
gboolean first;
} ReplayToJsonClosure;
static void
replay_to_json (gint64 time,
const char *name,
const char *signature,
GValue *arg,
gpointer user_data)
{
ReplayToJsonClosure *closure = user_data;
char *event_str;
if (closure->error != NULL)
return;
if (!closure->first)
{
if (!write_string (closure->out, ",\n ", &closure->error))
return;
}
closure->first = FALSE;
if (strcmp (signature, "") == 0)
{
event_str = g_strdup_printf ("[%" G_GINT64_FORMAT ", \"%s\"]", time, name);
}
else if (strcmp (signature, "i") == 0)
{
event_str = g_strdup_printf ("[%" G_GINT64_FORMAT ", \"%s\", %i]",
time,
name,
g_value_get_int (arg));
}
else if (strcmp (signature, "x") == 0)
{
event_str = g_strdup_printf ("[%" G_GINT64_FORMAT ", \"%s\", %"G_GINT64_FORMAT "]",
time,
name,
g_value_get_int64 (arg));
}
else if (strcmp (signature, "s") == 0)
{
const char *arg_str = g_value_get_string (arg);
char *escaped = escape_quotes (arg_str);
event_str = g_strdup_printf ("[%" G_GINT64_FORMAT ", \"%s\", \"%s\"]",
time,
name,
g_value_get_string (arg));
if (escaped != arg_str)
g_free (escaped);
}
else
{
g_assert_not_reached ();
}
if (!write_string (closure->out, event_str, &closure->error))
return;
}
/**
* shell_perf_log_dump_log:
* @perf_log: a #ShellPerfLog
* @out: output stream into which to write the event log
* @error: location to store #GError, or %NULL
*
* Writes the performance event log, formatted as JSON, to the specified
* output stream. For performance reasons, the output stream passed
* in should generally be a buffered (or memory) output stream, since
* it will be written to in small pieces. The JSON output is an array
* with the elements of the array also being arrays, of the form
* '[' <time>, <event name> [, <event_arg>... ] ']'.
*
* Return value: %TRUE if the dump succeeded. %FALSE if an IO error occurred
*/
gboolean
shell_perf_log_dump_log (ShellPerfLog *perf_log,
GOutputStream *out,
GError **error)
{
ReplayToJsonClosure closure;
closure.out = out;
closure.error = NULL;
closure.first = TRUE;
if (!write_string (out, "[ ", &closure.error))
return FALSE;
shell_perf_log_replay (perf_log, replay_to_json, &closure);
if (closure.error != NULL)
{
g_propagate_error (error, closure.error);
return FALSE;
}
if (!write_string (out, " ]", &closure.error))
return FALSE;
return TRUE;
}
|