-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ep-buffer-manager.c
1465 lines (1217 loc) · 56.6 KB
/
ep-buffer-manager.c
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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ep-rt-config.h"
#ifdef ENABLE_PERFTRACING
#if !defined(EP_INCLUDE_SOURCE_FILES) || defined(EP_FORCE_INCLUDE_SOURCE_FILES)
#define EP_IMPL_BUFFER_MANAGER_GETTER_SETTER
#include "ep.h"
#include "ep-buffer.h"
#include "ep-buffer-manager.h"
#include "ep-event.h"
#include "ep-event-instance.h"
#include "ep-event-payload.h"
#include "ep-file.h"
#include "ep-session.h"
#include "ep-stack-contents.h"
#define EP_MAX(a,b) (((a) > (b)) ? (a) : (b))
#define EP_MIN(a,b) (((a) < (b)) ? (a) : (b))
#define EP_CLAMP(min,value,max) (EP_MIN(EP_MAX(min, value), max))
/*
* Forward declares of all static functions.
*/
static
void
buffer_list_fini (EventPipeBufferList *buffer_list);
// _Requires_lock_held (buffer_manager)
static
bool
buffer_manager_enqueue_sequence_point (
EventPipeBufferManager *buffer_manager,
EventPipeSequencePoint *sequence_point);
// _Requires_lock_held (buffer_manager)
static
void
buffer_manager_init_sequence_point_thread_list (
EventPipeBufferManager *buffer_manager,
EventPipeSequencePoint *sequence_point);
// _Requires_lock_held (buffer_manager)
static
void
buffer_manager_dequeue_sequence_point (EventPipeBufferManager *buffer_manager);
// _Requires_lock_held (buffer_manager)
static
bool
buffer_manager_try_peek_sequence_point (
EventPipeBufferManager *buffer_manager,
EventPipeSequencePoint **sequence_point);
// Allocate a new buffer for the specified thread.
// This function will store the buffer in the thread's buffer list for future use and also return it here.
// A NULL return value means that a buffer could not be allocated.
static
EventPipeBuffer *
buffer_manager_allocate_buffer_for_thread (
EventPipeBufferManager *buffer_manager,
EventPipeThreadSessionState *thread_session_state,
uint32_t request_size,
bool *write_suspended);
static
void
buffer_manager_deallocate_buffer (
EventPipeBufferManager *buffer_manager,
EventPipeBuffer *buffer);
// Attempt to reserve space for a buffer
static
bool
buffer_manager_try_reserve_buffer(
EventPipeBufferManager *buffer_manager,
uint32_t request_size);
// Release a reserved buffer budget
static
void
buffer_manager_release_buffer(
EventPipeBufferManager *buffer_manager,
uint32_t size);
// An iterator that can enumerate all the events which have been written into this buffer manager.
// Initially the iterator starts uninitialized and get_current_event () returns NULL. Calling move_next_xxx ()
// attempts to advance the cursor to the next event. If there is no event prior to stop_timestamp then
// the get_current_event () again returns NULL, otherwise it returns that event. The event pointer returned
// by get_current_event() is valid until move_next_xxx() is called again. Once all events in a buffer have
// been read the iterator will delete that buffer from the pool.
// Moves to the next oldest event searching across all threads. If there is no event older than
// stop_timestamp then get_current_event() will return NULL.
static
void
buffer_manager_move_next_event_any_thread (
EventPipeBufferManager *buffer_manager,
ep_timestamp_t stop_timestamp);
// Moves to the next oldest event from the same thread as the current event. If there is no event
// older than stopTimeStamp then GetCurrentEvent() will return NULL. This should only be called
// when GetCurrentEvent() is non-null (because we need to know what thread's events to iterate)
static
void
buffer_manager_move_next_event_same_thread (
EventPipeBufferManager *buffer_manager,
ep_timestamp_t stop_timestamp);
// Finds the first buffer in EventPipeBufferList that has a readable event prior to before_timestamp,
// starting with pBuffer
static
EventPipeBuffer *
buffer_manager_advance_to_non_empty_buffer (
EventPipeBufferManager *buffer_manager,
EventPipeBufferList *buffer_list,
EventPipeBuffer *buffer,
ep_timestamp_t before_timestamp);
// Detaches this buffer from an active writer thread and marks it read-only so that the reader
// thread can use it. If the writer thread has not yet stored the buffer into its thread-local
// slot it will not be converted, but such buffers have no events in them so there is no reason
// to read them.
static
bool
buffer_manager_try_convert_buffer_to_read_only (
EventPipeBufferManager *buffer_manager,
EventPipeBuffer *new_read_buffer);
/*
* EventPipeBufferList.
*/
static
void
buffer_list_fini (EventPipeBufferList *buffer_list)
{
EP_ASSERT (buffer_list != NULL);
ep_thread_holder_fini (&buffer_list->thread_holder);
}
EventPipeBufferList *
ep_buffer_list_alloc (
EventPipeBufferManager *manager,
EventPipeThread *thread)
{
EventPipeBufferList *instance = ep_rt_object_alloc (EventPipeBufferList);
ep_raise_error_if_nok (instance != NULL);
ep_raise_error_if_nok (ep_buffer_list_init (instance, manager, thread) != NULL);
ep_on_exit:
return instance;
ep_on_error:
ep_buffer_list_free (instance);
instance = NULL;
ep_exit_error_handler ();
}
EventPipeBufferList *
ep_buffer_list_init (
EventPipeBufferList *buffer_list,
EventPipeBufferManager *manager,
EventPipeThread *thread)
{
EP_ASSERT (buffer_list != NULL);
EP_ASSERT (manager != NULL);
EP_ASSERT (thread != NULL);
ep_thread_holder_init (&buffer_list->thread_holder, thread);
buffer_list->manager = manager;
buffer_list->head_buffer = NULL;
buffer_list->tail_buffer = NULL;
buffer_list->buffer_count = 0;
buffer_list->last_read_sequence_number = 0;
return buffer_list;
}
void
ep_buffer_list_fini (EventPipeBufferList *buffer_list)
{
ep_return_void_if_nok (buffer_list != NULL);
buffer_list_fini (buffer_list);
}
void
ep_buffer_list_free (EventPipeBufferList *buffer_list)
{
ep_return_void_if_nok (buffer_list != NULL);
buffer_list_fini (buffer_list);
ep_rt_object_free (buffer_list);
}
void
ep_buffer_list_insert_tail (
EventPipeBufferList *buffer_list,
EventPipeBuffer *buffer)
{
ep_return_void_if_nok (buffer_list != NULL);
EP_ASSERT (buffer != NULL);
EP_ASSERT (ep_buffer_list_ensure_consistency (buffer_list));
// Ensure that the input buffer didn't come from another list that was improperly cleaned up.
EP_ASSERT ((ep_buffer_get_next_buffer (buffer) == NULL) && (ep_buffer_get_prev_buffer (buffer) == NULL));
// First node in the list.
if (buffer_list->tail_buffer == NULL) {
buffer_list->head_buffer = buffer_list->tail_buffer = buffer;
} else {
// Set links between the old and new tail nodes.
ep_buffer_set_next_buffer (buffer_list->tail_buffer, buffer);
ep_buffer_set_prev_buffer (buffer, buffer_list->tail_buffer);
// Set the new tail node.
buffer_list->tail_buffer = buffer;
}
buffer_list->buffer_count++;
EP_ASSERT (ep_buffer_list_ensure_consistency (buffer_list));
}
EventPipeBuffer *
ep_buffer_list_get_and_remove_head (EventPipeBufferList *buffer_list)
{
ep_return_null_if_nok (buffer_list != NULL);
EP_ASSERT (ep_buffer_list_ensure_consistency (buffer_list));
EventPipeBuffer *ret_buffer = NULL;
if (buffer_list->head_buffer != NULL)
{
// Save the head node.
ret_buffer = buffer_list->head_buffer;
// Set the new head node.
buffer_list->head_buffer = ep_buffer_get_next_buffer (buffer_list->head_buffer);
// Update the head node's previous pointer.
if (buffer_list->head_buffer != NULL)
ep_buffer_set_prev_buffer (buffer_list->head_buffer, NULL);
else
// We just removed the last buffer from the list.
// Make sure both head and tail pointers are NULL.
buffer_list->tail_buffer = NULL;
// Clear the next pointer of the old head node.
ep_buffer_set_next_buffer (ret_buffer, NULL);
// Ensure that the old head node has no dangling references.
EP_ASSERT ((ep_buffer_get_next_buffer (ret_buffer) == NULL) && (ep_buffer_get_prev_buffer (ret_buffer) == NULL));
// Decrement the count of buffers in the list.
buffer_list->buffer_count--;
}
EP_ASSERT (ep_buffer_list_ensure_consistency (buffer_list));
return ret_buffer;
}
bool
buffer_manager_try_reserve_buffer(
EventPipeBufferManager *buffer_manager,
uint32_t request_size)
{
uint64_t iters = 0;
size_t old_size_of_all_buffers;
size_t new_size_of_all_buffers;
do {
old_size_of_all_buffers = buffer_manager->size_of_all_buffers;
new_size_of_all_buffers = old_size_of_all_buffers + request_size;
iters++;
if (iters % 64 == 0) {
ep_rt_thread_sleep (0); // yield the thread to the scheduler in case we're in high contention
}
} while (new_size_of_all_buffers <= buffer_manager->max_size_of_all_buffers && ep_rt_atomic_compare_exchange_size_t (&buffer_manager->size_of_all_buffers, old_size_of_all_buffers, new_size_of_all_buffers) != old_size_of_all_buffers);
return new_size_of_all_buffers <= buffer_manager->max_size_of_all_buffers;
}
void
buffer_manager_release_buffer(
EventPipeBufferManager *buffer_manager,
uint32_t size)
{
uint64_t iters = 0;
size_t old_size_of_all_buffers;
size_t new_size_of_all_buffers;
do {
old_size_of_all_buffers = buffer_manager->size_of_all_buffers;
new_size_of_all_buffers = old_size_of_all_buffers - size;
iters++;
if (iters % 64 == 0) {
ep_rt_thread_sleep (0); // yield the thread to the scheduler in case we're in high contention
}
} while (new_size_of_all_buffers >= 0 && ep_rt_atomic_compare_exchange_size_t (&buffer_manager->size_of_all_buffers, old_size_of_all_buffers, new_size_of_all_buffers) != old_size_of_all_buffers);
}
#ifdef EP_CHECKED_BUILD
bool
ep_buffer_list_ensure_consistency (EventPipeBufferList *buffer_list)
{
// Either the head and tail nodes are both NULL or both are non-NULL.
EP_ASSERT ((buffer_list->head_buffer == NULL && buffer_list->tail_buffer == NULL) ||
(buffer_list->head_buffer != NULL && buffer_list->tail_buffer != NULL));
// If the list is NULL, check the count and return.
if (buffer_list->head_buffer == NULL) {
EP_ASSERT (buffer_list->buffer_count == 0);
return true;
}
// If the list is non-NULL, walk the list forward until we get to the end.
uint32_t node_count = (buffer_list->head_buffer != NULL) ? 1 : 0;
EventPipeBuffer *iterator = buffer_list->head_buffer;
while (ep_buffer_get_next_buffer (iterator) != NULL) {
iterator = ep_buffer_get_next_buffer (iterator);
node_count++;
// Check for consistency of the buffer itself.
// NOTE: We can't check the last buffer because the owning thread could
// be writing to it, which could result in false asserts.
if (ep_buffer_get_next_buffer (iterator) != NULL)
EP_ASSERT (ep_buffer_ensure_consistency (iterator));
// Check for cycles.
EP_ASSERT (node_count <= buffer_list->buffer_count);
}
// When we're done with the walk, pIter must point to the tail node.
EP_ASSERT (iterator == buffer_list->tail_buffer);
// Node count must equal the buffer count.
EP_ASSERT (node_count == buffer_list->buffer_count);
// Now, walk the list in reverse.
iterator = buffer_list->tail_buffer;
node_count = (buffer_list->tail_buffer != NULL) ? 1 : 0;
while (ep_buffer_get_prev_buffer (iterator) != NULL) {
iterator = ep_buffer_get_prev_buffer (iterator);
node_count++;
// Check for cycles.
EP_ASSERT (node_count <= buffer_list->buffer_count);
}
// When we're done with the reverse walk, pIter must point to the head node.
EP_ASSERT (iterator == buffer_list->head_buffer);
// Node count must equal the buffer count.
EP_ASSERT (node_count == buffer_list->buffer_count);
// We're done.
return true;
}
#endif
/*
* EventPipeBufferManager.
*/
static
bool
buffer_manager_enqueue_sequence_point (
EventPipeBufferManager *buffer_manager,
EventPipeSequencePoint *sequence_point)
{
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (sequence_point != NULL);
ep_buffer_manager_requires_lock_held (buffer_manager);
return dn_list_push_back (buffer_manager->sequence_points, sequence_point);
}
static
void
buffer_manager_init_sequence_point_thread_list (
EventPipeBufferManager *buffer_manager,
EventPipeSequencePoint *sequence_point)
{
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (sequence_point != NULL);
ep_buffer_manager_requires_lock_held (buffer_manager);
DN_LIST_FOREACH_BEGIN (EventPipeThreadSessionState *, thread_session_state, buffer_manager->thread_session_state_list) {
// The sequence number captured here is not guaranteed to be the most recent sequence number, nor
// is it guaranteed to match the number of events we would observe in the thread's write buffer
// memory. This is only used as a lower bound on the number of events the thread has attempted to
// write at the timestamp we will capture below.
//
// The sequence number is the value that will be used by the next event, so the last written
// event is one less. Sequence numbers are allowed to overflow, so going backwards is allowed to
// underflow.
uint32_t sequence_number = ep_thread_session_state_get_volatile_sequence_number (thread_session_state) - 1;
dn_umap_ptr_uint32_insert (ep_sequence_point_get_thread_sequence_numbers (sequence_point), thread_session_state, sequence_number);
ep_thread_addref (ep_thread_holder_get_thread (ep_thread_session_state_get_thread_holder_ref (thread_session_state)));
} DN_LIST_FOREACH_END;
// This needs to come after querying the thread sequence numbers to ensure that any recorded
// sequence number is <= the actual sequence number at this timestamp
ep_buffer_manager_requires_lock_held (buffer_manager);
ep_sequence_point_set_timestamp (sequence_point, ep_perf_timestamp_get ());
}
static
void
buffer_manager_dequeue_sequence_point (EventPipeBufferManager *buffer_manager)
{
EP_ASSERT (buffer_manager != NULL);
ep_buffer_manager_requires_lock_held (buffer_manager);
ep_return_void_if_nok (!dn_list_empty (buffer_manager->sequence_points));
EventPipeSequencePoint *value = *dn_list_front_t (buffer_manager->sequence_points, EventPipeSequencePoint *);
dn_list_pop_front (buffer_manager->sequence_points);
ep_sequence_point_free (value);
}
static
bool
buffer_manager_try_peek_sequence_point (
EventPipeBufferManager *buffer_manager,
EventPipeSequencePoint **sequence_point)
{
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (sequence_point != NULL);
ep_buffer_manager_requires_lock_held (buffer_manager);
ep_return_false_if_nok (!dn_list_empty (buffer_manager->sequence_points));
*sequence_point = *dn_list_front_t (buffer_manager->sequence_points, EventPipeSequencePoint *);
return *sequence_point != NULL;
}
static
EventPipeBuffer *
buffer_manager_allocate_buffer_for_thread (
EventPipeBufferManager *buffer_manager,
EventPipeThreadSessionState *thread_session_state,
uint32_t request_size,
bool *write_suspended)
{
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (thread_session_state != NULL);
EP_ASSERT (request_size > 0);
EventPipeBuffer *new_buffer = NULL;
EventPipeBufferList *thread_buffer_list = NULL;
EventPipeSequencePoint* sequence_point = NULL;
uint32_t sequence_number = 0;
// Pick a buffer size by multiplying the base buffer size by the number of buffers already allocated for this thread.
uint32_t size_multiplier = ep_thread_session_state_get_buffer_count_estimate(thread_session_state) + 1;
EP_ASSERT(size_multiplier > 0);
// Pick the base buffer size. Checked builds have a smaller size to stress the allocate path more.
#ifdef EP_CHECKED_BUILD
uint32_t base_buffer_size = 30 * 1024; // 30K
#else
uint32_t base_buffer_size = 100 * 1024; // 100K
#endif
uint32_t buffer_size = base_buffer_size * size_multiplier;
EP_ASSERT(buffer_size > 0);
buffer_size = EP_MAX (request_size, buffer_size);
// Don't allow the buffer size to exceed 1MB.
const uint32_t max_buffer_size = 1024 * 1024;
buffer_size = EP_MIN (buffer_size, max_buffer_size);
// Make sure that buffer size >= request size so that the buffer size does not
// determine the max event size.
EP_ASSERT (request_size <= buffer_size);
// Make the buffer size fit into with pagesize-aligned block, since ep_rt_valloc0 expects page-aligned sizes to be passed as arguments
buffer_size = (buffer_size + ep_rt_system_get_alloc_granularity () - 1) & ~(uint32_t)(ep_rt_system_get_alloc_granularity () - 1);
// Attempt to reserve the necessary buffer size
EP_ASSERT(buffer_size > 0);
ep_return_null_if_nok(buffer_manager_try_reserve_buffer(buffer_manager, buffer_size));
// The sequence counter is exclusively mutated on this thread so this is a thread-local read.
sequence_number = ep_thread_session_state_get_volatile_sequence_number (thread_session_state);
new_buffer = ep_buffer_alloc (buffer_size, ep_thread_session_state_get_thread (thread_session_state), sequence_number);
ep_raise_error_if_nok (new_buffer != NULL);
// Adding a buffer to the buffer list requires us to take the lock.
EP_SPIN_LOCK_ENTER (&buffer_manager->rt_lock, section1)
thread_buffer_list = ep_thread_session_state_get_buffer_list (thread_session_state);
if (thread_buffer_list == NULL) {
thread_buffer_list = ep_buffer_list_alloc (buffer_manager, ep_thread_session_state_get_thread (thread_session_state));
ep_raise_error_if_nok_holding_spin_lock (thread_buffer_list != NULL, section1);
ep_raise_error_if_nok_holding_spin_lock (dn_list_push_back (buffer_manager->thread_session_state_list, thread_session_state), section1);
ep_thread_session_state_set_buffer_list (thread_session_state, thread_buffer_list);
thread_buffer_list = NULL;
}
if (buffer_manager->sequence_point_alloc_budget != 0) {
// sequence point bookkeeping
if (buffer_size >= buffer_manager->remaining_sequence_point_alloc_budget) {
sequence_point = ep_sequence_point_alloc ();
if (sequence_point) {
buffer_manager_init_sequence_point_thread_list (buffer_manager, sequence_point);
ep_raise_error_if_nok_holding_spin_lock (buffer_manager_enqueue_sequence_point (buffer_manager, sequence_point), section1);
sequence_point = NULL;
}
buffer_manager->remaining_sequence_point_alloc_budget = buffer_manager->sequence_point_alloc_budget;
} else {
buffer_manager->remaining_sequence_point_alloc_budget -= buffer_size;
}
}
#ifdef EP_CHECKED_BUILD
buffer_manager->num_buffers_allocated++;
#endif // EP_CHECKED_BUILD
// Set the buffer on the thread.
if (new_buffer != NULL)
ep_buffer_list_insert_tail (ep_thread_session_state_get_buffer_list (thread_session_state), new_buffer);
EP_SPIN_LOCK_EXIT (&buffer_manager->rt_lock, section1)
ep_on_exit:
return new_buffer;
ep_on_error:
ep_sequence_point_free (sequence_point);
sequence_point = NULL;
ep_buffer_list_free (thread_buffer_list);
thread_buffer_list = NULL;
ep_buffer_free (new_buffer);
new_buffer = NULL;
buffer_manager_release_buffer(buffer_manager, buffer_size);
ep_exit_error_handler ();
}
static
void
buffer_manager_deallocate_buffer (
EventPipeBufferManager *buffer_manager,
EventPipeBuffer *buffer)
{
EP_ASSERT (buffer_manager != NULL);
if (buffer) {
buffer_manager_release_buffer(buffer_manager, ep_buffer_get_size (buffer));
ep_buffer_free (buffer);
#ifdef EP_CHECKED_BUILD
buffer_manager->num_buffers_allocated--;
#endif
}
}
static
void
buffer_manager_move_next_event_any_thread (
EventPipeBufferManager *buffer_manager,
ep_timestamp_t stop_timestamp)
{
EP_ASSERT (buffer_manager != NULL);
ep_buffer_manager_requires_lock_not_held (buffer_manager);
if (buffer_manager->current_event != NULL)
ep_buffer_move_next_read_event (buffer_manager->current_buffer);
buffer_manager->current_event = NULL;
buffer_manager->current_buffer = NULL;
buffer_manager->current_buffer_list = NULL;
// We need to do this in two steps because we can't hold m_lock and EventPipeThread::m_lock
// at the same time.
// Step 1 - while holding m_lock get the oldest buffer from each thread
DN_DEFAULT_LOCAL_ALLOCATOR (allocator, dn_vector_ptr_default_local_allocator_byte_size * 2);
dn_vector_ptr_t buffer_array;
dn_vector_ptr_t buffer_list_array;
dn_vector_ptr_custom_init_params_t params = {0, };
params.allocator = (dn_allocator_t *)&allocator;
params.capacity = dn_vector_ptr_default_local_allocator_capacity_size;
ep_raise_error_if_nok (dn_vector_ptr_custom_init (&buffer_array, ¶ms));
ep_raise_error_if_nok (dn_vector_ptr_custom_init (&buffer_list_array, ¶ms));
EP_SPIN_LOCK_ENTER (&buffer_manager->rt_lock, section1)
EventPipeBufferList *buffer_list;
EventPipeBuffer *buffer;
DN_LIST_FOREACH_BEGIN (EventPipeThreadSessionState *, thread_session_state, buffer_manager->thread_session_state_list) {
buffer_list = ep_thread_session_state_get_buffer_list (thread_session_state);
buffer = buffer_list->head_buffer;
if (buffer && ep_buffer_get_creation_timestamp (buffer) < stop_timestamp) {
dn_vector_ptr_push_back (&buffer_list_array, buffer_list);
dn_vector_ptr_push_back (&buffer_array, buffer);
}
} DN_LIST_FOREACH_END;
EP_SPIN_LOCK_EXIT (&buffer_manager->rt_lock, section1)
// Step 2 - iterate the cached list to find the one with the oldest event. This may require
// converting some of the buffers from writable to readable, and that in turn requires
// taking the associated EventPipeThread lock for thread that was writing to that buffer.
ep_timestamp_t oldest_timestamp;
oldest_timestamp = stop_timestamp;
EventPipeBufferList *buffer_list;
EventPipeBuffer *head_buffer;
EventPipeBuffer *buffer;
EventPipeEventInstance *next_event;
for (uint32_t i = 0; i < dn_vector_ptr_size (&buffer_array) && i < dn_vector_ptr_size (&buffer_list_array); ++i) {
buffer_list = (EventPipeBufferList *)*dn_vector_ptr_index (&buffer_list_array, i);
head_buffer = (EventPipeBuffer *)*dn_vector_ptr_index (&buffer_array, i);
buffer = buffer_manager_advance_to_non_empty_buffer (buffer_manager, buffer_list, head_buffer, stop_timestamp);
if (buffer) {
// Peek the next event out of the buffer.
next_event = ep_buffer_get_current_read_event (buffer);
// If it's the oldest event we've seen, then save it.
if (next_event && ep_event_instance_get_timestamp (next_event) < oldest_timestamp) {
buffer_manager->current_event = next_event;
buffer_manager->current_buffer = buffer;
buffer_manager->current_buffer_list = buffer_list;
oldest_timestamp = ep_event_instance_get_timestamp (buffer_manager->current_event);
}
}
}
ep_on_exit:
ep_buffer_manager_requires_lock_not_held (buffer_manager);
dn_vector_ptr_dispose (&buffer_list_array);
dn_vector_ptr_dispose (&buffer_array);
return;
ep_on_error:
ep_exit_error_handler ();
}
static
void
buffer_manager_move_next_event_same_thread (
EventPipeBufferManager *buffer_manager,
ep_timestamp_t stop_timestamp)
{
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (buffer_manager->current_event != NULL);
EP_ASSERT (buffer_manager->current_buffer != NULL);
EP_ASSERT (buffer_manager->current_buffer_list != NULL);
ep_buffer_manager_requires_lock_not_held (buffer_manager);
//advance past the current event
buffer_manager->current_event = NULL;
ep_buffer_move_next_read_event (buffer_manager->current_buffer);
// Find the first buffer in the list, if any, which has an event in it
buffer_manager->current_buffer = buffer_manager_advance_to_non_empty_buffer (
buffer_manager,
buffer_manager->current_buffer_list,
buffer_manager->current_buffer,
stop_timestamp);
if (buffer_manager->current_buffer) {
// get the event from that buffer
EventPipeEventInstance *next_event = ep_buffer_get_current_read_event (buffer_manager->current_buffer);
ep_timestamp_t next_timestamp = ep_event_instance_get_timestamp (next_event);
if (next_timestamp >= stop_timestamp) {
// event exists, but isn't early enough
buffer_manager->current_event = NULL;
buffer_manager->current_buffer = NULL;
buffer_manager->current_buffer_list = NULL;
} else {
// event is early enough, set the new cursor
buffer_manager->current_event = next_event;
EP_ASSERT (buffer_manager->current_buffer != NULL);
EP_ASSERT (buffer_manager->current_buffer_list != NULL);
}
} else {
// no more buffers prior to before_timestamp
EP_ASSERT (buffer_manager->current_event == NULL);
EP_ASSERT (buffer_manager->current_buffer == NULL);
buffer_manager->current_buffer_list = NULL;
}
}
static
EventPipeBuffer *
buffer_manager_advance_to_non_empty_buffer (
EventPipeBufferManager *buffer_manager,
EventPipeBufferList *buffer_list,
EventPipeBuffer *buffer,
ep_timestamp_t before_timestamp)
{
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (buffer_list != NULL);
EP_ASSERT (buffer != NULL);
EP_ASSERT (buffer_list->head_buffer == buffer);
ep_buffer_manager_requires_lock_not_held (buffer_manager);
EventPipeBuffer *current_buffer = buffer;
bool done = false;
while (!done) {
if (!buffer_manager_try_convert_buffer_to_read_only (buffer_manager, current_buffer)) {
// the writer thread hasn't yet stored this buffer into the m_pWriteBuffer
// field (there is a small time window after allocation in this state).
// This should be the only buffer remaining in the list and it has no
// events written into it so we are done iterating.
current_buffer = NULL;
done = true;
} else if (ep_buffer_get_current_read_event (current_buffer) != NULL) {
// found a non-empty buffer
done = true;
} else {
EP_SPIN_LOCK_ENTER (&buffer_manager->rt_lock, section1)
// delete the empty buffer
EventPipeBuffer *removed_buffer = ep_buffer_list_get_and_remove_head (buffer_list);
EP_ASSERT (current_buffer == removed_buffer);
buffer_manager_deallocate_buffer (buffer_manager, removed_buffer);
// get the next buffer
current_buffer = buffer_list->head_buffer;
if (!current_buffer || ep_buffer_get_creation_timestamp (current_buffer) >= before_timestamp) {
// no more buffers in the list before this timestamp, we're done
current_buffer = NULL;
done = true;
}
EP_SPIN_LOCK_EXIT (&buffer_manager->rt_lock, section1)
}
}
ep_on_exit:
ep_buffer_manager_requires_lock_not_held (buffer_manager);
return current_buffer;
ep_on_error:
current_buffer = NULL;
ep_exit_error_handler ();
}
static
bool
buffer_manager_try_convert_buffer_to_read_only (
EventPipeBufferManager *buffer_manager,
EventPipeBuffer *new_read_buffer)
{
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (new_read_buffer != NULL);
ep_buffer_manager_requires_lock_not_held (buffer_manager);
bool result = false;
// if already readable, nothing to do
if (ep_buffer_get_volatile_state (new_read_buffer) == EP_BUFFER_STATE_READ_ONLY)
return true;
// if not yet readable, disable the thread from writing to it which causes
// it to become readable
EventPipeThread *thread = ep_buffer_get_writer_thread (new_read_buffer);
EP_SPIN_LOCK_ENTER (ep_thread_get_rt_lock_ref (thread), section1);
EventPipeThreadSessionState *thread_session_state = ep_thread_get_session_state (thread, buffer_manager->session);
EP_ASSERT(thread_session_state != NULL);
if (ep_thread_session_state_get_write_buffer (thread_session_state) == new_read_buffer) {
ep_thread_session_state_set_write_buffer (thread_session_state, NULL);
EP_ASSERT (ep_buffer_get_volatile_state (new_read_buffer) == EP_BUFFER_STATE_READ_ONLY);
result = true;
}
EP_SPIN_LOCK_EXIT (ep_thread_get_rt_lock_ref (thread), section1);
// It is possible that EventPipeBufferList returns a writable buffer
// yet it is not returned as ep_thread_get_write_buffer (). This is because
// ep_buffer_manager_allocate_buffer_for_thread () insert the new writable buffer into
// the EventPipeBufferList first, and then it is added to the writable buffer hash table
// by ep_thread_set_write_buffer () next. The two operations are not atomic so it is possible
// to observe this partial state.
if (!result)
result = (ep_buffer_get_volatile_state (new_read_buffer) == EP_BUFFER_STATE_READ_ONLY);
ep_on_exit:
ep_buffer_manager_requires_lock_not_held (buffer_manager);
return result;
ep_on_error:
EP_ASSERT (!result);
ep_exit_error_handler ();
}
EventPipeBufferManager *
ep_buffer_manager_alloc (
EventPipeSession *session,
size_t max_size_of_all_buffers,
size_t sequence_point_allocation_budget)
{
EventPipeBufferManager *instance = ep_rt_object_alloc (EventPipeBufferManager);
ep_raise_error_if_nok (instance != NULL);
instance->thread_session_state_list = dn_list_alloc ();
ep_raise_error_if_nok (instance->thread_session_state_list != NULL);
instance->sequence_points = dn_list_alloc ();
ep_raise_error_if_nok (instance->sequence_points != NULL);
ep_rt_spin_lock_alloc (&instance->rt_lock);
ep_raise_error_if_nok (ep_rt_spin_lock_is_valid (&instance->rt_lock));
ep_rt_wait_event_alloc (&instance->rt_wait_event, false, true);
ep_raise_error_if_nok (ep_rt_wait_event_is_valid (&instance->rt_wait_event));
instance->session = session;
instance->size_of_all_buffers = 0;
instance->num_oversized_events_dropped = 0;
#ifdef EP_CHECKED_BUILD
instance->num_buffers_allocated = 0;
instance->num_buffers_stolen = 0;
instance->num_buffers_leaked = 0;
instance->num_events_stored = 0;
ep_rt_volatile_store_int64_t (&instance->num_events_dropped, 0);
ep_rt_volatile_store_int64_t (&instance->num_events_written, 0);
#endif
instance->current_event = NULL;
instance->current_buffer = NULL;
instance->current_buffer_list = NULL;
instance->max_size_of_all_buffers = EP_CLAMP ((size_t)100 * 1024, max_size_of_all_buffers, (size_t)UINT32_MAX);
if (sequence_point_allocation_budget == 0) {
// sequence points disabled
instance->sequence_point_alloc_budget = 0;
instance->remaining_sequence_point_alloc_budget = 0;
} else {
instance->sequence_point_alloc_budget = EP_CLAMP ((size_t)1024 * 1024, sequence_point_allocation_budget, (size_t)1024 * 1024 * 1024);
instance->remaining_sequence_point_alloc_budget = sequence_point_allocation_budget;
}
ep_on_exit:
return instance;
ep_on_error:
ep_buffer_manager_free (instance);
instance = NULL;
ep_exit_error_handler ();
}
void
ep_buffer_manager_free (EventPipeBufferManager * buffer_manager)
{
ep_return_void_if_nok (buffer_manager != NULL);
ep_buffer_manager_deallocate_buffers (buffer_manager);
dn_list_free (buffer_manager->sequence_points);
dn_list_free (buffer_manager->thread_session_state_list);
ep_rt_wait_event_free (&buffer_manager->rt_wait_event);
ep_rt_spin_lock_free (&buffer_manager->rt_lock);
ep_rt_object_free (buffer_manager);
}
#ifdef EP_CHECKED_BUILD
void
ep_buffer_manager_requires_lock_held (const EventPipeBufferManager *buffer_manager)
{
ep_rt_spin_lock_requires_lock_held (&buffer_manager->rt_lock);
}
void
ep_buffer_manager_requires_lock_not_held (const EventPipeBufferManager *buffer_manager)
{
ep_rt_spin_lock_requires_lock_not_held (&buffer_manager->rt_lock);
}
#endif
void
ep_buffer_manager_init_sequence_point_thread_list (
EventPipeBufferManager *buffer_manager,
EventPipeSequencePoint *sequence_point)
{
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (sequence_point != NULL);
ep_buffer_manager_requires_lock_not_held (buffer_manager);
EP_SPIN_LOCK_ENTER (&buffer_manager->rt_lock, section1)
buffer_manager_init_sequence_point_thread_list (buffer_manager, sequence_point);
EP_SPIN_LOCK_EXIT (&buffer_manager->rt_lock, section1)
ep_on_exit:
ep_buffer_manager_requires_lock_not_held (buffer_manager);
return;
ep_on_error:
ep_exit_error_handler ();
}
bool
ep_buffer_manager_write_event (
EventPipeBufferManager *buffer_manager,
ep_rt_thread_handle_t thread,
EventPipeSession *session,
EventPipeEvent *ep_event,
EventPipeEventPayload *payload,
const uint8_t *activity_id,
const uint8_t *related_activity_id,
ep_rt_thread_handle_t event_thread,
EventPipeStackContents *stack)
{
bool result = false;
bool alloc_new_buffer = false;
EventPipeBuffer *buffer = NULL;
EventPipeThreadSessionState *session_state = NULL;
EventPipeStackContents stack_contents;
EventPipeStackContents *current_stack_contents = NULL;
EP_ASSERT (buffer_manager != NULL);
EP_ASSERT (ep_event != NULL);
// The input thread must match the current thread because no lock is taken on the buffer.
EP_ASSERT (thread == ep_rt_thread_get_handle ());
// Before we pick a buffer, make sure the event is enabled.
ep_return_false_if_nok (ep_event_is_enabled (ep_event));
// Check that the payload size is less than 64 KB (max size for ETW events)
if (ep_event_payload_get_size (payload) > 64 * 1024)
{
ep_rt_atomic_inc_int64_t (&buffer_manager->num_oversized_events_dropped);
EventPipeThread *current_thread = ep_thread_get();
ep_rt_spin_lock_handle_t *thread_lock = ep_thread_get_rt_lock_ref (current_thread);
EP_SPIN_LOCK_ENTER (thread_lock, section1)
session_state = ep_thread_get_or_create_session_state (current_thread, session);
ep_thread_session_state_increment_sequence_number (session_state);
EP_SPIN_LOCK_EXIT (thread_lock, section1)
return false;
}
// Check to see an event thread was specified. If not, then use the current thread.
if (event_thread == NULL)
event_thread = thread;
current_stack_contents = ep_stack_contents_init (&stack_contents);
if (stack == NULL && ep_session_get_enable_stackwalk (session) && ep_event_get_need_stack (ep_event) && !ep_session_get_rundown_enabled (session)) {
ep_walk_managed_stack_for_current_thread (current_stack_contents);
stack = current_stack_contents;
}
// See if the thread already has a buffer to try.
EventPipeThread *current_thread;
current_thread = ep_thread_get ();
ep_raise_error_if_nok (current_thread != NULL);
ep_rt_spin_lock_handle_t *thread_lock;
thread_lock = ep_thread_get_rt_lock_ref (current_thread);
EP_SPIN_LOCK_ENTER (thread_lock, section2)
session_state = ep_thread_get_or_create_session_state (current_thread, session);
ep_raise_error_if_nok_holding_spin_lock (session_state != NULL, section2);
buffer = ep_thread_session_state_get_write_buffer (session_state);
if (!buffer) {
alloc_new_buffer = true;
} else {
// Attempt to write the event to the buffer. If this fails, we should allocate a new buffer.
if (ep_buffer_write_event (buffer, event_thread, session, ep_event, payload, activity_id, related_activity_id, stack))
ep_thread_session_state_increment_sequence_number (session_state);
else
alloc_new_buffer = true;
}
EP_SPIN_LOCK_EXIT (thread_lock, section2)
// alloc_new_buffer is reused below to detect if overflow happened, so cache it here to see if we should
// signal the reader thread
bool should_signal_reader_thread;
should_signal_reader_thread = alloc_new_buffer;
// Check to see if we need to allocate a new buffer, and if so, do it here.
if (alloc_new_buffer) {
uint32_t request_size = sizeof (EventPipeEventInstance) + ep_event_payload_get_size (payload);
bool write_suspended = false;