-
Notifications
You must be signed in to change notification settings - Fork 149
/
coms.c
1404 lines (1149 loc) · 49.7 KB
/
coms.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 <SAPI.h>
#include <curl/curl.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
// For reasons it doesn't find asprintf() if this isn't included later...
#include "coms.h"
#include <components-rs/ddtrace.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_LINUX_SECUREBITS_H
#include <linux/securebits.h>
#include <sys/prctl.h>
#endif
#if HAVE_LINUX_CAPABILITY_H
#include <linux/capability.h>
#include <sys/syscall.h>
#endif
#include "compatibility.h"
#include "configuration.h"
#include "ddshared.h"
#include "ext/version.h"
#include "logging.h"
#include "mpack/mpack.h"
#include "sidecar.h"
#include "zend_smart_str.h"
extern inline bool ddtrace_coms_is_stack_unused(ddtrace_coms_stack_t *stack);
extern inline bool ddtrace_coms_is_stack_free(ddtrace_coms_stack_t *stack);
typedef uint32_t group_id_t;
#define GROUP_ID_PROCESSED (1UL << 31UL)
ddtrace_coms_state_t ddtrace_coms_globals = {.stacks = NULL};
static struct ddog_AgentRemoteConfigWriter_ShmHandle *dd_agent_config_writer;
struct ddog_ShmHandle *ddtrace_coms_agent_config_handle;
static bool _dd_is_memory_pressure_high(void) {
ddtrace_coms_stack_t *stack = atomic_load(&ddtrace_coms_globals.current_stack);
if (stack) {
int64_t used = (((double)atomic_load(&stack->position) / (double)stack->size) * 100);
return used > get_global_DD_TRACE_BETA_HIGH_MEMORY_PRESSURE_PERCENT();
} else {
return false;
}
}
/* Is called internally in the context of the PHP thread and actually attempts at storing the payload to send. If
* there is not enough memory left in the currently active stack, it does not attempt to generate a new stack, instead
* it returns a `ENOMEM` code that should be read by the invoker that can manually decide to ask for a larger stack.
*/
static uint32_t _dd_store_data(group_id_t group_id, const char *src, size_t size) {
ddtrace_coms_stack_t *stack = atomic_load(&ddtrace_coms_globals.current_stack);
if (stack == NULL) {
// no stack to save data to
return ENOMEM;
}
size_t size_to_alloc = size + sizeof(size_t) + sizeof(group_id_t);
atomic_fetch_add(&stack->refcount, 1);
size_t position = atomic_fetch_add(&stack->position, size_to_alloc);
if ((position + size_to_alloc) > stack->size) {
// allocation failed
atomic_fetch_sub(&stack->refcount, 1);
return ENOMEM;
}
memcpy(stack->data + position, &size, sizeof(size_t));
position += sizeof(size_t);
memcpy(stack->data + position, &group_id, sizeof(group_id_t));
position += sizeof(group_id_t);
memcpy(stack->data + position, src, size);
atomic_fetch_add(&stack->bytes_written, size_to_alloc);
atomic_fetch_sub(&stack->refcount, 1);
return 0;
}
/* Allocates a new stack of the minimum possible size. Only if `min_size` (which is the size required by the user to
* fit in a given payload) is larger than the currently active stack size, then new sizes are attempted attemptiong
* to double at each iteration, up to `DDTRACE_COMS_STACK_MAX_SIZE`.
* The rationale behind this is that once we know that at least one single trace can be larger than X bytes, then
* all the subsequent stacks are allocated at least as large as that size.
*/
static ddtrace_coms_stack_t *_dd_new_stack(size_t min_size) {
size_t initial_size = atomic_load(&ddtrace_coms_globals.stack_size);
size_t size = initial_size;
while (min_size > size && size <= (ddtrace_coms_globals.max_payload_size/2)) {
size *= 2;
}
if (size != initial_size) {
// If we fail to update the global twice in a row, we can just rely on dynamic size allocation in the future
int i = 2;
while (!atomic_compare_exchange_weak(&ddtrace_coms_globals.stack_size, &initial_size, size) && i--) {
if (initial_size > size) {
size = initial_size;
break;
}
};
}
ddtrace_coms_stack_t *stack = calloc(1, sizeof(ddtrace_coms_stack_t));
stack->size = size;
stack->data = calloc(1, size);
return stack;
}
static void _dd_coms_free_stack(ddtrace_coms_stack_t *stack) {
free(stack->data);
free(stack);
}
static void _dd_recycle_stack(ddtrace_coms_stack_t *stack) {
char *data = stack->data;
size_t size = stack->size;
memset(stack, 0, sizeof(ddtrace_coms_stack_t));
memset(data, 0, size);
stack->data = data;
stack->size = size;
}
static void (*_dd_ptr_at_exit_callback)(void) = 0;
static void _dd_at_exit_callback() { ddtrace_coms_flush_shutdown_writer_synchronous(); }
static void _dd_at_exit_hook() {
if (_dd_ptr_at_exit_callback) {
_dd_ptr_at_exit_callback();
}
}
bool ddtrace_coms_minit(size_t initial_stack_size, size_t max_stack_size, size_t max_backlog_size) {
ddtrace_coms_globals.initial_stack_size = initial_stack_size;
ddtrace_coms_globals.max_payload_size = max_stack_size;
ddtrace_coms_globals.max_backlog_size = max_backlog_size;
atomic_store(&ddtrace_coms_globals.stack_size, initial_stack_size);
ddtrace_coms_stack_t *stack = _dd_new_stack(initial_stack_size);
if (!ddtrace_coms_globals.stacks) {
ddtrace_coms_globals.stacks = calloc(max_backlog_size, sizeof(ddtrace_coms_stack_t *));
}
atomic_store(&ddtrace_coms_globals.next_group_id, 1);
atomic_store(&ddtrace_coms_globals.current_stack, stack);
_dd_ptr_at_exit_callback = _dd_at_exit_callback;
atexit(_dd_at_exit_hook);
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
return false;
}
return true;
}
void ddtrace_coms_mshutdown(void) { _dd_ptr_at_exit_callback = NULL; }
static void _dd_coms_stack_shutdown(void) {
ddtrace_coms_stack_t *current_stack = atomic_load(&ddtrace_coms_globals.current_stack);
if (current_stack) {
if (current_stack->data) {
free(current_stack->data);
}
free(current_stack);
ddtrace_coms_globals.current_stack = NULL;
}
if (ddtrace_coms_globals.stacks) {
free(ddtrace_coms_globals.stacks);
ddtrace_coms_globals.stacks = NULL;
}
}
#if 0
static void printf_stack_info(ddtrace_coms_stack_t *stack) {
printf("stack (%p) refcount: (%d) bytes_written: (%lu)\n", stack, atomic_load(&stack->refcount),
atomic_load(&stack->bytes_written));
}
#endif
static void _dd_unsafe_store_or_discard_stack(ddtrace_coms_stack_t *stack) {
for (size_t i = 0; i < ddtrace_coms_globals.max_backlog_size; i++) {
ddtrace_coms_stack_t *stack_tmp = ddtrace_coms_globals.stacks[i];
if (stack_tmp == stack) {
return;
}
if (stack_tmp == NULL) {
ddtrace_coms_globals.stacks[i] = stack;
return;
}
}
_dd_coms_free_stack(stack);
}
static void _dd_unsafe_cleanup_dirty_stack_area(void) {
ddtrace_coms_stack_t *current_stack = atomic_load(&ddtrace_coms_globals.current_stack);
if (!ddtrace_coms_globals.tmp_stack) {
return;
}
if (ddtrace_coms_globals.tmp_stack != current_stack) {
ddtrace_coms_stack_t *stack = ddtrace_coms_globals.tmp_stack;
atomic_store(&stack->refcount, 0);
_dd_unsafe_store_or_discard_stack(stack);
}
ddtrace_coms_globals.tmp_stack = NULL;
}
/* Stores the global current stack (if any) into the global `stacks` array. The writer will pick from this array when
* sending payloads to the backend. This function is invoked when a global current stack is "filled enough" to be sent
* to the backend.
* This function has a side effect: it tries to save memory by using as the "next" global current stack one of the
* stacks in the global `stack` array mentioned above. This is possible if a payload contained in one of the elements of
* the array has already been sent to the backend and, as a consequence, that element is ready for reuse. Keep in mind
* that in order to be ready for reuse it has to be at least of size `min_size`.
* It is possible that the global current stack is set to `NULL` by this function. In this case it means that there were
* no available existing stacks that could store `min_size` bytes and it is the invoker's own responsibility to allocate
* a new stack of the desired size and assign it to the global current stack.
*/
static void _dd_unsafe_store_or_swap_current_stack_for_empty_stack(size_t min_size) {
_dd_unsafe_cleanup_dirty_stack_area();
// store the temp variable if we ever need to recover it
ddtrace_coms_stack_t **current_stack = &ddtrace_coms_globals.tmp_stack;
*current_stack = atomic_load(&ddtrace_coms_globals.current_stack);
if (*current_stack && (*current_stack)->size >= min_size && ddtrace_coms_is_stack_free(*current_stack)) {
*current_stack = NULL;
return; // stack is empty and unusued - no need to swap it out
}
if (*current_stack) {
// try to swap out current stack for an empty stack
for (size_t i = 0; i < ddtrace_coms_globals.max_backlog_size; i++) {
ddtrace_coms_stack_t *stack_tmp = ddtrace_coms_globals.stacks[i];
if (stack_tmp && stack_tmp->size >= min_size && ddtrace_coms_is_stack_free(stack_tmp)) {
// order is important due to ability to restore state on thread restart
_dd_recycle_stack(stack_tmp);
atomic_store(&ddtrace_coms_globals.current_stack, stack_tmp);
ddtrace_coms_globals.stacks[i] = *current_stack;
*current_stack = NULL;
break;
}
}
}
// if we couldn't swap for a empty stack lets store it
if (*current_stack) {
for (size_t i = 0; i < ddtrace_coms_globals.max_backlog_size; i++) {
ddtrace_coms_stack_t *stack_tmp = ddtrace_coms_globals.stacks[i];
if (!stack_tmp) {
atomic_store(&ddtrace_coms_globals.current_stack, NULL);
ddtrace_coms_globals.stacks[i] = *current_stack;
*current_stack = NULL;
break;
}
}
}
*current_stack = NULL;
}
static bool _dd_coms_unsafe_rotate_stack(bool attempt_allocate_new, size_t min_size) {
_dd_unsafe_store_or_swap_current_stack_for_empty_stack(min_size);
ddtrace_coms_stack_t *current_stack = atomic_load(&ddtrace_coms_globals.current_stack);
/* In this case we are reusing a stack (from the global `stacks` array) that has been already sent and was
* recycled, and `current_stack` is just a pointer to one of `<global>.stacks[i]`.
*/
if (current_stack && current_stack->size >= min_size && ddtrace_coms_is_stack_free(current_stack)) {
return true;
}
/* In this case it wasn't possible to reuse an existing stack, for one of two reasons:
* 1. All the N currently allocated stacks (with N <= `DDTRACE_COMS_STACKS_BACKLOG_SIZE`) are filled and waiting
* to be sent by the writer; or
* 2. None of the available stacks in the global `stacks` array that could be reused has size >= `min_size`.
*/
if (!current_stack) {
if (attempt_allocate_new) {
ddtrace_coms_stack_t **next_stack = &ddtrace_coms_globals.tmp_stack;
*next_stack = _dd_new_stack(min_size);
atomic_store(&ddtrace_coms_globals.current_stack, *next_stack);
*next_stack = NULL;
return true;
}
}
// we couldn't store old stack or allocate a new one so we cannot provide new empty stack
return false;
}
struct _writer_thread_variables_t {
pthread_t self;
pthread_mutex_t interval_flush_mutex, finished_flush_mutex, stack_rotation_mutex;
pthread_mutex_t writer_shutdown_signal_mutex;
pthread_cond_t writer_shutdown_signal_condition;
pthread_cond_t interval_flush_condition, finished_flush_condition;
};
struct _writer_loop_data_t {
CURL *curl;
_Atomic(struct curl_slist *)headers;
ddtrace_coms_stack_t *tmp_stack;
struct _writer_thread_variables_t *thread;
bool set_secbit;
_Atomic(bool) running, starting_up;
_Atomic(pid_t) current_pid;
_Atomic(bool) shutdown_when_idle, suspended, sending, allocate_new_stacks;
_Atomic(uint32_t) flush_interval, request_counter, flush_processed_stacks_total, writer_cycle,
requests_since_last_flush;
};
static struct _writer_loop_data_t global_writer = {.thread = NULL,
.set_secbit = 0,
.running = ATOMIC_VAR_INIT(0),
.current_pid = ATOMIC_VAR_INIT(0),
.shutdown_when_idle = ATOMIC_VAR_INIT(0),
.suspended = ATOMIC_VAR_INIT(0),
.allocate_new_stacks = ATOMIC_VAR_INIT(0),
.sending = ATOMIC_VAR_INIT(0)};
static struct _writer_loop_data_t *_dd_get_writer() { return &global_writer; }
static bool ddtrace_coms_threadsafe_rotate_stack(bool attempt_allocate_new, size_t min_size) {
struct _writer_loop_data_t *writer = _dd_get_writer();
bool rv = false;
if (writer->thread) {
pthread_mutex_lock(&writer->thread->stack_rotation_mutex);
rv = _dd_coms_unsafe_rotate_stack(attempt_allocate_new, min_size);
pthread_mutex_unlock(&writer->thread->stack_rotation_mutex);
}
return rv;
}
bool ddtrace_coms_buffer_data(uint32_t group_id, const char *data, size_t size) {
if (!data || size > ddtrace_coms_globals.max_payload_size) {
return false;
}
if (size == 0) {
size = strlen(data);
if (size == 0) {
return false;
}
}
uint32_t store_result = _dd_store_data(group_id, data, size);
if (_dd_is_memory_pressure_high()) {
ddtrace_coms_trigger_writer_flush();
}
if (store_result == ENOMEM) {
size_t padding = 2;
ddtrace_coms_threadsafe_rotate_stack(true, size + padding);
ddtrace_coms_trigger_writer_flush();
store_result = _dd_store_data(group_id, data, size);
}
return store_result == 0;
}
group_id_t ddtrace_coms_next_group_id(void) { return atomic_fetch_add(&ddtrace_coms_globals.next_group_id, 1); }
struct _grouped_stack_t {
size_t position, total_bytes, total_groups;
size_t bytes_to_write;
char *dest_data;
size_t dest_size;
};
static size_t _dd_write_array_header(char *buffer, size_t buffer_size, size_t position, uint32_t array_size) {
size_t free_space = buffer_size - position;
char *data = buffer + position;
if (array_size < 16) {
if (free_space >= 1) {
mpack_store_u8(data, (uint8_t)(0x90u | array_size));
return 1;
}
} else if (array_size < UINT16_MAX) {
if (free_space >= 3) {
mpack_store_u8(data, 0xdc);
mpack_store_u16(data + 1, array_size);
return 3;
}
} else {
if (free_space >= 5) {
mpack_store_u8(data, 0xdd);
mpack_store_u32(data + 1, array_size);
return 5;
}
}
return 0;
}
static size_t _dd_write_to_buffer(char *buffer, size_t buffer_size, size_t position, struct _grouped_stack_t *read) {
size_t write_size = read->bytes_to_write;
if (write_size > 0) {
if (write_size > (buffer_size - position)) {
write_size = buffer_size - position;
}
if (write_size > (read->total_bytes - read->position)) {
write_size = read->total_bytes - read->position;
}
memcpy(buffer + position, read->dest_data + read->position, write_size);
read->position += write_size;
read->bytes_to_write -= write_size;
}
return write_size;
}
static bool _dd_ensure_correct_dest_capacity(struct _grouped_stack_t *dest, size_t position, size_t write_size) {
size_t requested_size = position + write_size;
if (requested_size > dest->dest_size) {
requested_size += requested_size / 10; // addd 10% to reduce possible reallocations on next data chunk
char *new_ptr = realloc(dest->dest_data, requested_size);
if (new_ptr) {
dest->dest_data = new_ptr;
dest->dest_size = requested_size;
} else {
return false;
}
}
return true;
}
static void _dd_write_metadata(struct _grouped_stack_t *dest, size_t position, size_t elements_in_group,
size_t bytes_in_group) {
_dd_ensure_correct_dest_capacity(dest, position, sizeof(size_t) * 2);
memcpy(dest->dest_data + position, &elements_in_group, sizeof(size_t));
position += sizeof(size_t);
memcpy(dest->dest_data + position, &bytes_in_group, sizeof(size_t));
}
static void _dd_read_metadata(struct _grouped_stack_t *dest, size_t position, size_t *elements_in_group,
size_t *bytes_in_group) {
memcpy(elements_in_group, dest->dest_data + position, sizeof(size_t));
position += sizeof(size_t);
memcpy(bytes_in_group, dest->dest_data + position, sizeof(size_t));
}
static size_t _dd_coms_read_callback(char *buffer, size_t size, size_t nitems, void *userdata) {
if (!userdata) {
return 0;
}
struct _grouped_stack_t *read = userdata;
size_t written = 0;
size_t buffer_size = size * nitems;
if (read->total_groups > 0) {
written += _dd_write_array_header(buffer, buffer_size, written, read->total_groups);
read->total_groups = 0;
}
// write the remainder from previous iteration
written += _dd_write_to_buffer(buffer, buffer_size, written, read);
while (written < buffer_size) {
// safe read size check position + metadata
if ((read->position + sizeof(size_t) * 2) > read->total_bytes) {
break;
}
size_t num_elements = 0;
_dd_read_metadata(read, read->position, &num_elements, &read->bytes_to_write);
if (read->bytes_to_write == 0) {
break;
}
// written += _dd_write_array_header(buffer, buffer_size, written, num_elements);
read->position += sizeof(size_t) * 2;
written += _dd_write_to_buffer(buffer, buffer_size, written, read);
}
return written;
}
struct _entry_t {
size_t size;
group_id_t group_id;
size_t next_entry_offset;
char *data;
char *raw_entry;
};
static struct _entry_t _dd_create_entry(ddtrace_coms_stack_t *stack, size_t position) {
struct _entry_t rv = {.size = 0, .group_id = 0, .data = NULL, .next_entry_offset = 0};
size_t bytes_written = atomic_load(&stack->bytes_written);
if ((position + sizeof(size_t) + sizeof(group_id_t)) > bytes_written) {
// wrong size available skip this entry
return rv;
}
rv.raw_entry = stack->data + position; // set pointer to beginning of the whole entry containing metadata
memcpy(&rv.size, stack->data + position, sizeof(size_t));
position += sizeof(size_t);
memcpy(&rv.group_id, stack->data + position, sizeof(group_id_t));
position += sizeof(group_id_t);
if (rv.size > 0 && (rv.size + position) <= bytes_written) {
// size is valid - save entry
rv.data = stack->data + position;
rv.next_entry_offset = sizeof(size_t) + sizeof(group_id_t) + rv.size;
}
return rv;
}
static void _dd_mark_entry_as_processed(struct _entry_t *entry) {
group_id_t processed_special_id = GROUP_ID_PROCESSED;
memcpy(entry->raw_entry + sizeof(size_t), &processed_special_id, sizeof(group_id_t));
}
static size_t _dd_append_entry(struct _entry_t *entry, struct _grouped_stack_t *dest, size_t position) {
if (_dd_ensure_correct_dest_capacity(dest, position, entry->size)) {
memcpy(dest->dest_data + position, entry->data, entry->size);
return entry->size;
} else {
return 0;
}
}
static void _dd_msgpack_group_stack_by_id(ddtrace_coms_stack_t *stack, struct _grouped_stack_t *dest) {
// perform an insertion sort by group_id
uint32_t current_group_id = 0;
struct _entry_t first_entry = _dd_create_entry(stack, 0);
dest->total_bytes = 0;
dest->total_groups = 0;
if (!first_entry.data) {
return; // no entries
}
struct _entry_t next_group_entry = first_entry;
current_group_id = first_entry.group_id;
dest->total_groups++;
size_t current_src_beginning = 0, next_src_beginning = 0, group_dest_beginning_position = 0;
size_t bytes_written = atomic_load(&stack->bytes_written);
while (current_src_beginning < bytes_written) {
size_t current_src_position = current_src_beginning;
size_t group_dest_position = group_dest_beginning_position;
// group metadata
size_t elements_in_group = 0;
size_t bytes_in_group = 0;
group_dest_position += sizeof(size_t) * 2; // leave place for group meta data
size_t i = 0;
while (current_src_position < bytes_written) {
struct _entry_t entry = _dd_create_entry(stack, current_src_position);
i++;
if (entry.size == 0) {
break;
}
if (entry.group_id == current_group_id) {
size_t copied = _dd_append_entry(&entry, dest, group_dest_position);
if (copied > 0) {
_dd_mark_entry_as_processed(&entry);
elements_in_group++;
group_dest_position += copied;
bytes_in_group += copied;
}
} else if (next_group_entry.group_id == current_group_id && entry.group_id != GROUP_ID_PROCESSED) {
dest->total_groups++; // add unique group count
next_group_entry = entry;
next_src_beginning = current_src_position;
}
current_src_position += entry.next_entry_offset;
}
_dd_write_metadata(dest, group_dest_beginning_position, elements_in_group, bytes_in_group);
group_dest_beginning_position = group_dest_position;
// no new groups - exit loop
if (next_group_entry.group_id == current_group_id) {
break;
}
current_group_id = next_group_entry.group_id;
current_src_beginning = next_src_beginning;
}
dest->total_bytes = group_dest_beginning_position; // save total bytes count after conversion
}
static void *_dd_init_read_userdata(ddtrace_coms_stack_t *stack) {
size_t total_bytes = atomic_load(&stack->bytes_written);
struct _grouped_stack_t *readstack = calloc(1, sizeof(struct _grouped_stack_t));
readstack->total_bytes = total_bytes;
readstack->dest_size = atomic_load(&stack->bytes_written) + 2000;
readstack->dest_data = malloc(readstack->dest_size);
_dd_msgpack_group_stack_by_id(stack, readstack);
return readstack;
}
static void _dd_deinit_read_userdata(void *userdata) {
struct _grouped_stack_t *data = userdata;
if (data->dest_data) {
free(data->dest_data);
}
free(userdata);
}
static ddtrace_coms_stack_t *_dd_coms_attempt_acquire_stack(void) {
ddtrace_coms_stack_t *stack = NULL;
for (size_t i = 0; i < ddtrace_coms_globals.max_backlog_size; i++) {
ddtrace_coms_stack_t *stack_tmp = ddtrace_coms_globals.stacks[i];
if (stack_tmp && atomic_load(&stack_tmp->refcount) == 0 && atomic_load(&stack_tmp->bytes_written) > 0) {
stack = stack_tmp;
ddtrace_coms_globals.stacks[i] = NULL;
break;
}
}
return stack;
}
#define TRACE_PATH_STR "/v0.4/traces"
#define HOST_V6_FORMAT_STR "http://[%s]:%u"
#define HOST_V4_FORMAT_STR "http://%s:%u"
#define DEFAULT_UDS_PATH "/var/run/datadog/apm.socket"
static struct curl_slist *dd_agent_curl_headers = NULL;
static void dd_append_header(struct curl_slist **list, const char *key, const char *val) {
/* The longest Agent header should be:
* Datadog-Container-Id: <64-char-hash>
* So 256 should give us plenty of wiggle room.
*/
char header[256];
size_t len = snprintf(header, sizeof header, "%s: %s", key, val);
if (len > 0 && len < sizeof header) {
*list = curl_slist_append(*list, header);
}
}
static struct curl_slist *dd_agent_headers_alloc(void) {
struct curl_slist *list = NULL;
dd_append_header(&list, "Datadog-Meta-Lang", "php");
dd_append_header(&list, "Datadog-Meta-Lang-Interpreter", sapi_module.name);
dd_append_header(&list, "Datadog-Meta-Lang-Version", ZSTR_VAL(ddtrace_php_version));
dd_append_header(&list, "Datadog-Meta-Tracer-Version", PHP_DDTRACE_VERSION);
ddog_CharSlice id = ddtrace_get_container_id();
if (id.len) {
char header[256];
sprintf(header, "Datadog-Container-Id: %.*s", (int)id.len, id.ptr);
list = curl_slist_append(list, header);
}
/* Curl will add Expect: 100-continue if it is a POST over a certain size. The trouble is that CURL will
* wait for *1 second* for 100 Continue response before sending the rest of the data. This wait is
* configurable, but requires a newer curl than we have on CentOS 6. So instead we send an empty Expect.
*/
dd_append_header(&list, "Expect", "");
return list;
}
static void dd_agent_headers_free(struct curl_slist *list) {
if (list != NULL) {
curl_slist_free_all(list);
}
}
void ddtrace_coms_curl_shutdown(void) {
dd_agent_headers_free(dd_agent_curl_headers);
if (dd_agent_config_writer) {
ddog_agent_remote_config_writer_drop(dd_agent_config_writer);
ddog_drop_anon_shm_handle(ddtrace_coms_agent_config_handle);
}
}
static long _dd_max_long(long a, long b) { return a >= b ? a : b; }
void ddtrace_curl_set_timeout(CURL *curl) {
long timeout = _dd_max_long(get_global_DD_TRACE_BGS_TIMEOUT(), get_global_DD_TRACE_AGENT_TIMEOUT());
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout);
}
void ddtrace_curl_set_connect_timeout(CURL *curl) {
long timeout = _dd_max_long(get_global_DD_TRACE_BGS_CONNECT_TIMEOUT(), get_global_DD_TRACE_AGENT_CONNECT_TIMEOUT());
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, timeout);
}
char *ddtrace_agent_url(void) {
zend_string *url = get_global_DD_TRACE_AGENT_URL();
if (ZSTR_LEN(url) > 0) {
return zend_strndup(ZSTR_VAL(url), ZSTR_LEN(url));
}
zend_string *hostname = get_global_DD_AGENT_HOST();
if (ZSTR_LEN(hostname) > 7 && strncmp(ZSTR_VAL(hostname), "unix://", 7) == 0) {
return zend_strndup(ZSTR_VAL(hostname), ZSTR_LEN(hostname));
}
if (ZSTR_LEN(hostname) > 0) {
bool isIPv6 = memchr(ZSTR_VAL(hostname), ':', ZSTR_LEN(hostname));
int64_t port = get_global_DD_TRACE_AGENT_PORT();
if (port <= 0 || port > 65535) {
port = 8126;
}
char *formatted_url;
asprintf(&formatted_url, isIPv6 ? HOST_V6_FORMAT_STR : HOST_V4_FORMAT_STR, ZSTR_VAL(hostname), (uint32_t)port);
return formatted_url;
}
if (access(DEFAULT_UDS_PATH, F_OK) == SUCCESS) {
return zend_strndup(ZEND_STRL("unix://" DEFAULT_UDS_PATH));
}
int64_t port = get_global_DD_TRACE_AGENT_PORT();
if (port <= 0 || port > 65535) {
port = 8126;
}
char *formatted_url;
asprintf(&formatted_url, HOST_V4_FORMAT_STR, "localhost", (uint32_t)port);
return formatted_url;
}
void ddtrace_curl_set_hostname(CURL *curl) {
char *url = ddtrace_agent_url();
if (url && url[0]) {
char *http_url = url;
if (strlen(url) > 7 && strncmp(url, "unix://", 7) == 0) {
curl_easy_setopt(curl, CURLOPT_UNIX_SOCKET_PATH, url + 7);
http_url = "http://localhost";
}
size_t agent_url_len = strlen(http_url) + sizeof(TRACE_PATH_STR);
char *agent_url = malloc(agent_url_len);
sprintf(agent_url, "%s%s", http_url, TRACE_PATH_STR);
curl_easy_setopt(curl, CURLOPT_URL, agent_url);
free(agent_url);
}
free(url);
}
static struct timespec _dd_deadline_in_ms(uint32_t ms) {
struct timespec deadline;
struct timeval now;
gettimeofday(&now, NULL);
uint32_t sec = ms / 1000UL;
uint32_t msec = ms % 1000UL;
deadline.tv_sec = now.tv_sec + sec;
deadline.tv_nsec = ((now.tv_usec + 1000UL * msec) * 1000UL);
// carry over full seconds from nsec
deadline.tv_sec += deadline.tv_nsec / (1000 * 1000 * 1000);
deadline.tv_nsec %= (1000 * 1000 * 1000);
return deadline;
}
static size_t _dd_dummy_write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
UNUSED(userdata);
size_t data_length = size * nmemb;
ddtrace_bgs_logf("%s", ptr);
return data_length;
}
static void _dd_curl_reset_headers(struct _writer_loop_data_t *writer) {
struct curl_slist *headers = atomic_exchange(&writer->headers, NULL);
if (headers) {
curl_slist_free_all(headers);
}
}
#define DD_TRACE_COUNT_HEADER "X-Datadog-Trace-Count: "
static void _dd_curl_set_headers(struct _writer_loop_data_t *writer, size_t trace_count) {
struct curl_slist *headers = NULL;
for (struct curl_slist *current = dd_agent_curl_headers; current; current = current->next) {
headers = curl_slist_append(headers, current->data);
}
headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
headers = curl_slist_append(headers, "Content-Type: application/msgpack");
char buffer[64];
int bytes_written = snprintf(buffer, sizeof buffer, DD_TRACE_COUNT_HEADER "%zu", trace_count);
if (bytes_written > ((int)sizeof(DD_TRACE_COUNT_HEADER)) - 1 && bytes_written < ((int)sizeof buffer)) {
headers = curl_slist_append(headers, buffer);
}
_dd_curl_reset_headers(writer);
curl_easy_setopt(writer->curl, CURLOPT_HTTPHEADER, headers);
writer->headers = headers;
}
static size_t _dd_curl_writefunc(char *ptr, size_t size, size_t nmemb, void *s) {
smart_str_appendl_ex((smart_str *)s, ptr, size * nmemb, true);
return size * nmemb;
}
static void _dd_curl_send_stack(struct _writer_loop_data_t *writer, ddtrace_coms_stack_t *stack) {
if (!writer->curl) {
ddtrace_bgs_logf("[bgs] no curl session - dropping the current stack.\n", NULL);
}
if (writer->curl) {
CURLcode res;
void *read_data = _dd_init_read_userdata(stack);
struct _grouped_stack_t *kData = read_data;
_dd_curl_set_headers(writer, kData->total_groups);
curl_easy_setopt(writer->curl, CURLOPT_READDATA, read_data);
ddtrace_curl_set_hostname(writer->curl);
ddtrace_curl_set_timeout(writer->curl);
ddtrace_curl_set_connect_timeout(writer->curl);
smart_str response = {0};
curl_easy_setopt(writer->curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(writer->curl, CURLOPT_VERBOSE, (long)get_global_DD_TRACE_AGENT_DEBUG_VERBOSE_CURL());
curl_easy_setopt(writer->curl, CURLOPT_WRITEFUNCTION, _dd_curl_writefunc);
curl_easy_setopt(writer->curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(writer->curl);
if (res != CURLE_OK) {
ddtrace_bgs_logf("[bgs] curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
if (get_global_DD_TRACE_DEBUG_CURL_OUTPUT()) {
double uploaded;
// only deprecated on relatively new libcurl versions
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
curl_easy_getinfo(writer->curl, CURLINFO_SIZE_UPLOAD, &uploaded);
#pragma GCC diagnostic pop
ddtrace_bgs_logf("[bgs] uploaded %.0f bytes\n", uploaded);
}
// No response happens with test agents for example
if (response.s) {
ddog_agent_remote_config_write(dd_agent_config_writer, dd_zend_string_to_CharSlice(response.s));
smart_str_free_ex(&response, true);
}
}
_dd_deinit_read_userdata(read_data);
_dd_curl_reset_headers(writer);
}
}
static void _dd_signal_writer_started(struct _writer_loop_data_t *writer) {
if (writer->thread) {
// at the moment no actual signal is sent but we will set a threadsafe state variable
// ordering is important to correctly state that writer is either running or stil is starting up
atomic_store(&writer->running, true);
atomic_store(&writer->starting_up, false);
}
}
static void _dd_signal_writer_finished(struct _writer_loop_data_t *writer) {
if (writer->thread) {
pthread_mutex_lock(&writer->thread->writer_shutdown_signal_mutex);
atomic_store(&writer->running, false);
pthread_cond_signal(&writer->thread->writer_shutdown_signal_condition);
pthread_mutex_unlock(&writer->thread->writer_shutdown_signal_mutex);
}
}
static void _dd_signal_data_processed(struct _writer_loop_data_t *writer) {
if (writer->thread) {
pthread_mutex_lock(&writer->thread->finished_flush_mutex);
pthread_cond_signal(&writer->thread->finished_flush_condition);
pthread_mutex_unlock(&writer->thread->finished_flush_mutex);
}
}
#ifdef __CYGWIN__
#define TIMEOUT_SIG SIGALRM
#else
#define TIMEOUT_SIG SIGPROF
#endif
static void _dd_writer_loop_cleanup(void *ctx) { _dd_signal_writer_finished((struct _writer_loop_data_t *)ctx); }
static void *_dd_writer_loop(void *_) {
UNUSED(_);
/* This thread must not handle signals intended for the PHP threads.
* See Zend/zend_signal.c for which signals it registers.
*/
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, TIMEOUT_SIG);
sigaddset(&sigset, SIGHUP);
sigaddset(&sigset, SIGINT);
sigaddset(&sigset, SIGQUIT);
sigaddset(&sigset, SIGTERM);
sigaddset(&sigset, SIGUSR1);
sigaddset(&sigset, SIGUSR2);
pthread_sigmask(SIG_BLOCK, &sigset, NULL);
struct _writer_loop_data_t *volatile writer = _dd_get_writer();
#if HAVE_LINUX_SECUREBITS_H
if (writer->set_secbit) {
// prevent setuid from messing with our effective capabilities
// this is necessary to handle scenarios where setuid is only called after starting our thread
prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP);
}
#endif
#if HAVE_LINUX_CAPABILITY_H
// restore the permitted capabilities to the effective set
// some applications may call setuid(2) with prctl(PR_SET_KEEPCAPS) active, but this will still clear all the
// effective capabilities To ensure proper functionality under these circumstances, we need to undo the effective
// capability clearing. This is safe.
struct __user_cap_header_struct caphdrp = {.version = _LINUX_CAPABILITY_VERSION_3};
struct __user_cap_data_struct capdatap[_LINUX_CAPABILITY_U32S_3];
if (syscall(SYS_capget, &caphdrp, &capdatap) == 0) {
for (int i = 0; i < _LINUX_CAPABILITY_U32S_3; ++i) {
capdatap[i].effective = capdatap[i].permitted;
}
syscall(SYS_capset, &caphdrp, &capdatap);
}
#endif
// nothing we can do here, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61118
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wclobbered"
pthread_cleanup_push(_dd_writer_loop_cleanup, writer);
#pragma GCC diagnostic pop
bool running = true;
_dd_signal_writer_started(writer);
do {
atomic_fetch_add(&writer->writer_cycle, 1);
uint32_t interval = atomic_load(&writer->flush_interval);
// fprintf(stderr, "interval %lu\n", interval);
if (interval > 0) {
struct timespec wait_deadline = _dd_deadline_in_ms(interval);
if (writer->thread) {
pthread_mutex_lock(&writer->thread->interval_flush_mutex);
pthread_cond_timedwait(&writer->thread->interval_flush_condition, &writer->thread->interval_flush_mutex,
&wait_deadline);
pthread_mutex_unlock(&writer->thread->interval_flush_mutex);
}
}
if (atomic_load(&writer->suspended)) {
continue;
}
atomic_store(&writer->requests_since_last_flush, 0);
ddtrace_coms_stack_t **stack = &writer->tmp_stack;
ddtrace_coms_threadsafe_rotate_stack(atomic_load(&writer->allocate_new_stacks),
ddtrace_coms_globals.initial_stack_size);