-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathmod.rs
1176 lines (1047 loc) · 40.3 KB
/
mod.rs
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
mod interrupts;
pub mod stalk_walking;
mod thread_utils;
mod uploader;
pub use interrupts::*;
pub use stalk_walking::*;
use uploader::*;
#[cfg(all(php_has_fibers, not(test)))]
use crate::bindings::ddog_php_prof_get_active_fiber;
#[cfg(all(php_has_fibers, test))]
use crate::bindings::ddog_php_prof_get_active_fiber_test as ddog_php_prof_get_active_fiber;
use crate::bindings::{datadog_php_profiling_get_profiling_context, zend_execute_data};
use crate::{AgentEndpoint, RequestLocals};
use crossbeam_channel::{Receiver, Sender, TrySendError};
use datadog_profiling::exporter::Tag;
use datadog_profiling::profile;
use datadog_profiling::profile::api::{Function, Line, Location, Period, Sample};
use log::{debug, error, info, trace, warn};
use std::borrow::{Borrow, Cow};
use std::collections::HashMap;
use std::hash::Hash;
use std::intrinsics::transmute;
use std::str;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Barrier};
use std::thread::JoinHandle;
use std::time::{Duration, Instant, SystemTime};
#[cfg(feature = "timeline")]
use lazy_static::lazy_static;
#[cfg(feature = "timeline")]
use std::time::UNIX_EPOCH;
#[cfg(feature = "allocation_profiling")]
use crate::allocation::ALLOCATION_PROFILING_INTERVAL;
#[cfg(feature = "allocation_profiling")]
use datadog_profiling::profile::api::UpscalingInfo;
const UPLOAD_PERIOD: Duration = Duration::from_secs(67);
// Guide: upload period / upload timeout should give about the order of
// magnitude for the capacity.
const UPLOAD_CHANNEL_CAPACITY: usize = 8;
/// Order this array this way:
/// 1. Always enabled types.
/// 2. On by default types.
/// 3. Off by default types.
#[derive(Default)]
struct SampleValues {
interrupt_count: i64,
wall_time: i64,
cpu_time: i64,
alloc_samples: i64,
alloc_size: i64,
timeline: i64,
}
const WALL_TIME_PERIOD: Duration = Duration::from_millis(10);
const WALL_TIME_PERIOD_TYPE: ValueType = ValueType {
r#type: "wall-time",
unit: "nanoseconds",
};
#[derive(Debug, Clone)]
struct WallTime {
instant: Instant,
systemtime: SystemTime,
}
impl WallTime {
fn now() -> Self {
Self {
instant: Instant::now(),
systemtime: SystemTime::now(),
}
}
}
#[derive(Debug, Clone)]
pub enum LabelValue {
Str(Cow<'static, str>),
Num(i64, Option<&'static str>),
}
#[derive(Debug, Clone)]
pub struct Label {
pub key: &'static str,
pub value: LabelValue,
}
impl<'a> From<&'a Label> for profile::api::Label<'a> {
fn from(label: &'a Label) -> Self {
let key = label.key;
match &label.value {
LabelValue::Str(str) => Self {
key,
str: Some(str),
num: 0,
num_unit: None,
},
LabelValue::Num(num, num_unit) => Self {
key,
str: None,
num: *num,
num_unit: num_unit.as_deref(),
},
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ValueType {
pub r#type: &'static str,
pub unit: &'static str,
}
impl ValueType {
pub const fn new(r#type: &'static str, unit: &'static str) -> Self {
Self { r#type, unit }
}
}
/// A ProfileIndex contains the fields that factor into the uniqueness of a
/// profile when we aggregate it. It's mostly based on the upload protocols,
/// because we cannot mix profiles belonging to different services into the
/// same upload.
/// This information is expected to be mostly stable for a process, but it may
/// not be if an Apache reload occurs and it adjusts the service name, or if
/// Apache per-dir settings use different service name, etc.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ProfileIndex {
pub sample_types: Vec<ValueType>,
pub tags: Arc<Vec<Tag>>,
pub endpoint: Box<AgentEndpoint>,
}
#[derive(Debug)]
pub struct SampleData {
pub frames: Vec<ZendFrame>,
pub labels: Vec<Label>,
pub sample_values: Vec<i64>,
}
#[derive(Debug)]
pub struct SampleMessage {
pub key: ProfileIndex,
pub value: SampleData,
}
#[derive(Debug)]
pub struct LocalRootSpanResourceMessage {
pub local_root_span_id: u64,
pub resource: String,
}
#[derive(Debug)]
pub enum ProfilerMessage {
Cancel,
Sample(SampleMessage),
LocalRootSpanResource(LocalRootSpanResourceMessage),
/// Used to put the helper thread into a barrier for caller so it can fork.
Pause,
/// Used to wake the helper thread so it can synchronize the fact a
/// request is being served.
Wake,
}
pub struct Globals {
pub interrupt_count: AtomicU32,
pub last_interrupt: SystemTime,
// todo: current_profile
}
impl Default for Globals {
fn default() -> Self {
Self {
interrupt_count: AtomicU32::new(0),
last_interrupt: SystemTime::now(),
}
}
}
pub struct Profiler {
fork_barrier: Arc<Barrier>,
interrupt_manager: Arc<InterruptManager>,
message_sender: Sender<ProfilerMessage>,
upload_sender: Sender<UploadMessage>,
time_collector_handle: JoinHandle<()>,
uploader_handle: JoinHandle<()>,
should_join: AtomicBool,
}
struct TimeCollector {
fork_barrier: Arc<Barrier>,
interrupt_manager: Arc<InterruptManager>,
message_receiver: Receiver<ProfilerMessage>,
upload_sender: Sender<UploadMessage>,
upload_period: Duration,
}
impl TimeCollector {
fn handle_timeout(
&self,
profiles: &mut HashMap<ProfileIndex, profile::Profile>,
last_export: &WallTime,
) -> WallTime {
let wall_export = WallTime::now();
if profiles.is_empty() {
info!("No profiles to upload.");
return wall_export;
}
let duration = wall_export
.instant
.checked_duration_since(last_export.instant);
let end_time = wall_export.systemtime;
for (index, profile) in profiles.drain() {
let message = UploadMessage::Upload(UploadRequest {
index,
profile,
end_time,
duration,
});
if let Err(err) = self.upload_sender.try_send(message) {
warn!("Failed to upload profile: {err}");
}
}
wall_export
}
/// Create a profile based on the message and start time. Note that it
/// makes sense to use an older time than now because if the profiler was
/// running 4 seconds ago and we're only creating a profile now, that means
/// we didn't collect any samples during that 4 seconds.
fn create_profile(message: &SampleMessage, started_at: SystemTime) -> profile::Profile {
let sample_types: Vec<profile::api::ValueType> = message
.key
.sample_types
.iter()
.map(|sample_type| profile::api::ValueType {
r#type: sample_type.r#type.borrow(),
unit: sample_type.unit.borrow(),
})
.collect();
// check if we have the `alloc-size` and `alloc-samples` sample types
#[cfg(feature = "allocation_profiling")]
let alloc_samples_offset = sample_types
.iter()
.position(|&x| x.r#type == "alloc-samples");
#[cfg(feature = "allocation_profiling")]
let alloc_size_offset = sample_types.iter().position(|&x| x.r#type == "alloc-size");
let period = WALL_TIME_PERIOD.as_nanos();
let mut profile = profile::ProfileBuilder::new()
.period(Some(Period {
r#type: profile::api::ValueType {
r#type: WALL_TIME_PERIOD_TYPE.r#type.borrow(),
unit: WALL_TIME_PERIOD_TYPE.unit.borrow(),
},
value: period.min(i64::MAX as u128) as i64,
}))
.start_time(Some(started_at))
.sample_types(sample_types)
.build();
#[cfg(feature = "allocation_profiling")]
if let (Some(alloc_size_offset), Some(alloc_samples_offset)) =
(alloc_size_offset, alloc_samples_offset)
{
let upscaling_info = UpscalingInfo::Poisson {
sum_value_offset: alloc_size_offset,
count_value_offset: alloc_samples_offset,
sampling_distance: ALLOCATION_PROFILING_INTERVAL as u64,
};
let values_offset: Vec<usize> = vec![alloc_size_offset, alloc_samples_offset];
match profile.add_upscaling_rule(values_offset.as_slice(), "", "", upscaling_info) {
Ok(_id) => {}
Err(err) => {
warn!("Failed to add upscaling rule for allocation samples, allocation samples reported will be wrong: {err}")
}
}
}
profile
}
fn handle_resource_message(
message: LocalRootSpanResourceMessage,
profiles: &mut HashMap<ProfileIndex, profile::Profile>,
) {
trace!(
"Received Endpoint Profiling message for span id {}.",
message.local_root_span_id
);
let local_root_span_id = message.local_root_span_id;
for (_, profile) in profiles.iter_mut() {
let endpoint = Cow::Borrowed(message.resource.as_str());
profile.add_endpoint(local_root_span_id, endpoint.clone());
profile.add_endpoint_count(endpoint, 1);
}
}
fn handle_sample_message(
message: SampleMessage,
profiles: &mut HashMap<ProfileIndex, profile::Profile>,
started_at: &WallTime,
) {
let profile: &mut profile::Profile = if let Some(value) = profiles.get_mut(&message.key) {
value
} else {
profiles.insert(
message.key.clone(),
Self::create_profile(&message, started_at.systemtime),
);
profiles
.get_mut(&message.key)
.expect("entry to exist; just inserted it")
};
let mut locations = Vec::with_capacity(message.value.frames.len());
let values = message.value.sample_values;
let labels: Vec<profile::api::Label> = message
.value
.labels
.iter()
.map(profile::api::Label::from)
.collect();
for frame in &message.value.frames {
let location = Location {
lines: vec![Line {
function: Function {
name: frame.function.as_ref(),
system_name: "",
filename: frame.file.as_deref().unwrap_or(""),
start_line: 0,
},
line: frame.line as i64,
}],
..Default::default()
};
locations.push(location);
}
let sample = Sample {
locations,
values,
labels,
};
match profile.add(sample) {
Ok(_id) => {}
Err(err) => {
warn!("Failed to add sample to the profile: {err}")
}
}
}
pub fn run(self) {
let mut last_wall_export = WallTime::now();
let mut profiles: HashMap<ProfileIndex, profile::Profile> = HashMap::with_capacity(1);
debug!(
"Started with an upload period of {} seconds and approximate wall-time period of {} milliseconds.",
UPLOAD_PERIOD.as_secs(),
WALL_TIME_PERIOD.as_millis());
let wall_timer = crossbeam_channel::tick(WALL_TIME_PERIOD);
let upload_tick = crossbeam_channel::tick(self.upload_period);
let never = crossbeam_channel::never();
let mut running = true;
while running {
// The crossbeam_channel::select! doesn't have the ability to
// optionally recv something. Instead, if the tick channel
// shouldn't be selected on, then pass the never channel for that
// iteration instead, keeping the code structure of the recvs the
// same. Since the never channel will never be ready, this
// effectively makes that branch optional for that loop iteration.
let timer = if self.interrupt_manager.has_interrupts() {
&wall_timer
} else {
&never
};
crossbeam_channel::select! {
recv(self.message_receiver) -> result => {
match result {
Ok(message) => match message {
ProfilerMessage::Sample(sample) =>
Self::handle_sample_message(sample, &mut profiles, &last_wall_export),
ProfilerMessage::LocalRootSpanResource(message) =>
Self::handle_resource_message(message, &mut profiles),
ProfilerMessage::Cancel => {
// flush what we have before exiting
last_wall_export = self.handle_timeout(&mut profiles, &last_wall_export);
running = false;
},
ProfilerMessage::Pause => {
// First, wait for every thread to finish what
// they are currently doing.
self.fork_barrier.wait();
// Then, wait for the fork to be completed.
self.fork_barrier.wait();
},
// The purpose is to wake up and sync the state of
// the interrupt manager.
ProfilerMessage::Wake => {}
},
Err(_) => {
/* Docs say:
* > A message could not be received because the
* > channel is empty and disconnected.
* If this happens, let's just break and end.
*/
break;
}
}
},
recv(timer) -> message => match message {
Ok(_) => self.interrupt_manager.trigger_interrupts(),
Err(err) => {
warn!("{err}");
break;
},
},
recv(upload_tick) -> message => {
if message.is_ok() {
last_wall_export = self.handle_timeout(&mut profiles, &last_wall_export);
}
},
}
}
}
}
pub struct UploadRequest {
index: ProfileIndex,
profile: profile::Profile,
end_time: SystemTime,
duration: Option<Duration>,
}
pub enum UploadMessage {
Pause,
Upload(UploadRequest),
}
impl Profiler {
pub fn new(output_pprof: Option<Cow<'static, str>>) -> Self {
let fork_barrier = Arc::new(Barrier::new(3));
let interrupt_manager = Arc::new(InterruptManager::new());
let (message_sender, message_receiver) = crossbeam_channel::bounded(100);
let (upload_sender, upload_receiver) = crossbeam_channel::bounded(UPLOAD_CHANNEL_CAPACITY);
let time_collector = TimeCollector {
fork_barrier: fork_barrier.clone(),
interrupt_manager: interrupt_manager.clone(),
message_receiver,
upload_sender: upload_sender.clone(),
upload_period: UPLOAD_PERIOD,
};
let uploader = Uploader::new(fork_barrier.clone(), upload_receiver, output_pprof);
let ddprof_time = "ddprof_time";
let ddprof_upload = "ddprof_upload";
Profiler {
fork_barrier,
interrupt_manager,
message_sender,
upload_sender,
time_collector_handle: thread_utils::spawn(ddprof_time, move || {
time_collector.run();
trace!("thread {ddprof_time} complete, shutting down");
}),
uploader_handle: thread_utils::spawn(ddprof_upload, move || {
uploader.run();
trace!("thread {ddprof_upload} complete, shutting down");
}),
should_join: AtomicBool::new(true),
}
}
pub fn add_interrupt(&self, interrupt: VmInterrupt) {
// First, add the interrupt to the set.
self.interrupt_manager.add_interrupt(interrupt);
// Second, make a best-effort attempt to wake the helper thread so
// that it is aware another PHP request is in flight.
_ = self.message_sender.try_send(ProfilerMessage::Wake);
}
pub fn remove_interrupt(&self, interrupt: VmInterrupt) {
// First, remove the interrupt to the set.
self.interrupt_manager.remove_interrupt(interrupt)
// Second, do not try to wake the helper thread. In NTS mode, the next
// request may come before the timer expires anyway, and if not, at
// worst we had 1 wake-up outside of a request, which is the same as
// if we wake it now.
// In ZTS mode, this would just be unnecessary wake-ups, as there are
// likely to be other threads serving requests.
}
/// Call before a fork, on the thread of the parent process that will fork.
pub fn fork_prepare(&self) {
// Send the message to the uploader first, as it has a longer worst-
// case time to wait.
let uploader_result = self.upload_sender.send(UploadMessage::Pause);
let profiler_result = self.message_sender.send(ProfilerMessage::Pause);
// todo: handle fails more gracefully, but it's tricky to sync 3
// threads, any of which could have crashed or be delayed. This
// could also deadlock.
match (uploader_result, profiler_result) {
(Ok(_), Ok(_)) => {
self.fork_barrier.wait();
}
(_, _) => {
error!("failed to prepare the profiler for forking, a deadlock could occur")
}
}
}
/// Call after a fork, but only on the thread of the parent process that forked.
pub fn post_fork_parent(&self) {
self.fork_barrier.wait();
}
pub fn send_sample(&self, message: SampleMessage) -> Result<(), TrySendError<ProfilerMessage>> {
self.message_sender
.try_send(ProfilerMessage::Sample(message))
}
pub fn send_local_root_span_resource(
&self,
message: LocalRootSpanResourceMessage,
) -> Result<(), TrySendError<ProfilerMessage>> {
self.message_sender
.try_send(ProfilerMessage::LocalRootSpanResource(message))
}
/// Begins the shutdown process. To complete it, call [Profiler::shutdown].
/// Note that you must call [Profiler::shutdown] afterwards; it's two
/// parts of the same operation. It's split so you (or other extensions)
/// can do something while the other threads finish up.
pub fn stop(&mut self, timeout: Duration) {
debug!("Stopping profiler.");
let sent = match self
.message_sender
.send_timeout(ProfilerMessage::Cancel, timeout)
{
Err(err) => {
warn!("Recent samples are most likely lost: Failed to notify other threads of cancellation: {err}.");
false
}
Ok(_) => {
debug!("Notified other threads of cancellation.");
true
}
};
self.should_join.store(sent, Ordering::SeqCst);
// Drop the sender to the uploader channel to reduce its refcount. At
// this state, only the ddprof_time thread will have a sender to the
// uploader. Once the sender over there is closed, then the uploader
// can quit.
// The sender is replaced with one that has a disconnected receiver, so
// the sender can't send any messages.
let (mut empty_sender, _) = crossbeam_channel::unbounded();
std::mem::swap(&mut self.upload_sender, &mut empty_sender);
}
/// Completes the shutdown process; to start it, call [Profiler::stop]
/// before calling [Profiler::shutdown].
/// Note the timeout is per thread, and there may be multiple threads.
pub fn shutdown(self, timeout: Duration) {
if self.should_join.load(Ordering::SeqCst) {
thread_utils::join_timeout(
self.time_collector_handle,
timeout,
"Recent samples may be lost.",
);
// Wait for the time_collector to join, since that will drop
// the sender half of the channel that the uploader is
// holding, allowing it to finish.
thread_utils::join_timeout(
self.uploader_handle,
timeout,
"Recent samples are most likely lost.",
);
}
}
fn cpu_sub(now: cpu_time::ThreadTime, prev: cpu_time::ThreadTime) -> i64 {
let now = now.as_duration();
let prev = prev.as_duration();
match now.checked_sub(prev) {
// If a 128 bit value doesn't fit in 64 bits, use the max.
Some(duration) => duration.as_nanos().try_into().unwrap_or(i64::MAX),
// If this happened, then either the programmer screwed up and
// passed args in backwards, or cpu time has gone backward... ish.
// Supposedly it can happen if the thread migrates CPUs:
// https://www.percona.com/blog/what-time-18446744073709550000-means/
// Regardless of why, a customer hit this:
// https://github.com/DataDog/dd-trace-php/issues/1880
// In these cases, zero is much closer to reality than i64::MAX.
None => 0,
}
}
/// Collect a stack sample with elapsed wall time. Collects CPU time if
/// it's enabled and available.
pub unsafe fn collect_time(
&self,
execute_data: *mut zend_execute_data,
interrupt_count: u32,
locals: &mut RequestLocals,
) {
// todo: should probably exclude the wall and CPU time used by collecting the sample.
let interrupt_count = interrupt_count as i64;
let result = collect_stack_sample(execute_data);
match result {
Ok(frames) => {
let depth = frames.len();
let now = Instant::now();
let wall_time = now.duration_since(locals.last_wall_time);
locals.last_wall_time = now;
let wall_time: i64 = wall_time.as_nanos().try_into().unwrap_or(i64::MAX);
/* If CPU time is disabled, or if it's enabled but not available on the platform,
* then `locals.last_cpu_time` will be None.
*/
let cpu_time = if let Some(last_cpu_time) = locals.last_cpu_time {
let now = cpu_time::ThreadTime::try_now()
.expect("CPU time to work since it's worked before during this process");
let cpu_time = Self::cpu_sub(now, last_cpu_time);
locals.last_cpu_time = Some(now);
cpu_time
} else {
0
};
let mut labels = Profiler::message_labels();
#[cfg(feature = "timeline")]
if locals.profiling_experimental_timeline_enabled {
if let Ok(now) = SystemTime::now().duration_since(UNIX_EPOCH) {
labels.push(Label {
key: "end_timestamp_ns",
value: LabelValue::Num(now.as_nanos() as i64, Some("nanoseconds")),
});
}
}
let n_labels = labels.len();
match self.send_sample(Profiler::prepare_sample_message(
frames,
SampleValues {
interrupt_count,
wall_time,
cpu_time,
..Default::default()
},
labels,
locals,
)) {
Ok(_) => trace!(
"Sent stack sample of {depth} frames and {n_labels} labels to profiler."
),
Err(err) => warn!(
"Failed to send stack sample of {depth} frames and {n_labels} labels to profiler: {err}"
),
}
}
Err(err) => {
warn!("Failed to collect stack sample: {err}")
}
}
}
#[cfg(feature = "allocation_profiling")]
/// Collect a stack sample with memory allocations
pub unsafe fn collect_allocations(
&self,
execute_data: *mut zend_execute_data,
alloc_samples: i64,
alloc_size: i64,
locals: &RequestLocals,
) {
let result = collect_stack_sample(execute_data);
match result {
Ok(frames) => {
let depth = frames.len();
let labels = Profiler::message_labels();
let n_labels = labels.len();
match self.send_sample(Profiler::prepare_sample_message(
frames,
SampleValues {
alloc_size,
alloc_samples,
..Default::default()
},
labels,
locals
)) {
Ok(_) => trace!(
"Sent stack sample of {depth} frames, {n_labels} labels, {alloc_size} bytes allocated, and {alloc_samples} allocations to profiler."
),
Err(err) => warn!(
"Failed to send stack sample of {depth} frames, {n_labels} labels, {alloc_size} bytes allocated, and {alloc_samples} allocations to profiler: {err}"
),
}
}
Err(err) => {
warn!("Failed to collect stack sample: {err}")
}
}
}
#[cfg(feature = "timeline")]
pub fn collect_compile_string(
&self,
duration: i64,
filename: String,
line: u32,
locals: &RequestLocals,
) {
let mut labels = Profiler::message_labels();
lazy_static! {
static ref TIMELINE_COMPILE_FILE_LABELS: Vec<Label> = vec![Label {
key: "event",
value: LabelValue::Str("compilation".into()),
},];
}
labels.extend_from_slice(&TIMELINE_COMPILE_FILE_LABELS);
let n_labels = labels.len();
match self.send_sample(Profiler::prepare_sample_message(
vec![ZendFrame {
function: COW_EVAL,
file: Some(filename),
line,
}],
SampleValues {
timeline: duration,
..Default::default()
},
labels,
locals,
)) {
Ok(_) => {
trace!("Sent event 'compile eval' with {n_labels} labels to profiler.")
}
Err(err) => {
warn!(
"Failed to send event 'compile eval' with {n_labels} labels to profiler: {err}"
)
}
}
}
#[cfg(feature = "timeline")]
pub fn collect_compile_file(
&self,
now: i64,
duration: i64,
filename: String,
include_type: &str,
locals: &RequestLocals,
) {
let mut labels = Profiler::message_labels();
lazy_static! {
static ref TIMELINE_COMPILE_FILE_LABELS: Vec<Label> = vec![Label {
key: "event",
value: LabelValue::Str("compilation".into()),
},];
}
labels.extend_from_slice(&TIMELINE_COMPILE_FILE_LABELS);
labels.push(Label {
key: "filename",
value: LabelValue::Str(Cow::from(filename)),
});
labels.push(Label {
key: "end_timestamp_ns",
value: LabelValue::Num(now, Some("nanoseconds")),
});
let n_labels = labels.len();
match self.send_sample(Profiler::prepare_sample_message(
vec![ZendFrame {
function: format!("[{include_type}]").into(),
file: None,
line: 0,
}],
SampleValues {
timeline: duration,
..Default::default()
},
labels,
locals,
)) {
Ok(_) => {
trace!("Sent event 'compile file' with {n_labels} labels to profiler.")
}
Err(err) => {
warn!(
"Failed to send event 'compile file' with {n_labels} labels to profiler: {err}"
)
}
}
}
#[cfg(feature = "timeline")]
/// collect a stack frame for garbage collection.
/// as we do not know about the overhead currently, we only collect a fake frame.
pub fn collect_garbage_collection(
&self,
now: i64,
duration: i64,
reason: &'static str,
collected: i64,
#[cfg(php_gc_status)] runs: i64,
locals: &RequestLocals,
) {
let mut labels = Profiler::message_labels();
lazy_static! {
static ref TIMELINE_GC_LABELS: Vec<Label> = vec![Label {
key: "event",
value: LabelValue::Str("gc".into()),
},];
}
labels.extend_from_slice(&TIMELINE_GC_LABELS);
labels.push(Label {
key: "gc reason",
value: LabelValue::Str(Cow::from(reason)),
});
labels.push(Label {
key: "end_timestamp_ns",
value: LabelValue::Num(now, Some("nanoseconds")),
});
#[cfg(php_gc_status)]
labels.push(Label {
key: "gc runs",
value: LabelValue::Num(runs, Some("count")),
});
labels.push(Label {
key: "gc collected",
value: LabelValue::Num(collected, Some("count")),
});
let n_labels = labels.len();
match self.send_sample(Profiler::prepare_sample_message(
vec![ZendFrame {
function: "[gc]".into(),
file: None,
line: 0,
}],
SampleValues {
timeline: duration,
..Default::default()
},
labels,
locals,
)) {
Ok(_) => {
trace!("Sent event 'gc' with {n_labels} labels and reason {reason} to profiler.")
}
Err(err) => {
warn!("Failed to send event 'gc' with {n_labels} and reason {reason} labels to profiler: {err}")
}
}
}
fn message_labels() -> Vec<Label> {
let gpc = unsafe { datadog_php_profiling_get_profiling_context };
if let Some(get_profiling_context) = gpc {
let context = unsafe { get_profiling_context() };
if context.local_root_span_id != 0 {
/* Safety: PProf only has signed integers for label.num.
* We bit-cast u64 to i64, and the backend does the
* reverse so the conversion is lossless.
*/
let local_root_span_id: i64 = unsafe { transmute(context.local_root_span_id) };
let span_id: i64 = unsafe { transmute(context.span_id) };
return vec![
Label {
key: "local root span id",
value: LabelValue::Num(local_root_span_id, None),
},
Label {
key: "span id",
value: LabelValue::Num(span_id, None),
},
];
}
}
vec![]
}
fn prepare_sample_message(
frames: Vec<ZendFrame>,
samples: SampleValues,
#[cfg(php_has_fibers)] mut labels: Vec<Label>,
#[cfg(not(php_has_fibers))] labels: Vec<Label>,
locals: &RequestLocals,
) -> SampleMessage {
// Lay this out in the same order as SampleValues
static SAMPLE_TYPES: &[ValueType; 6] = &[
ValueType::new("sample", "count"),
ValueType::new("wall-time", "nanoseconds"),
ValueType::new("cpu-time", "nanoseconds"),
ValueType::new("alloc-samples", "count"),
ValueType::new("alloc-size", "bytes"),
ValueType::new("timeline", "nanoseconds"),
];
// Allows us to slice the SampleValues as if they were an array.
let values: [i64; 6] = [
samples.interrupt_count,
samples.wall_time,
samples.cpu_time,
samples.alloc_samples,
samples.alloc_size,
samples.timeline,
];
let mut sample_types = Vec::with_capacity(SAMPLE_TYPES.len());
let mut sample_values = Vec::with_capacity(SAMPLE_TYPES.len());
if locals.profiling_enabled {
// sample, wall-time, cpu-time
let len = 2 + locals.profiling_experimental_cpu_time_enabled as usize;
sample_types.extend_from_slice(&SAMPLE_TYPES[0..len]);
sample_values.extend_from_slice(&values[0..len]);
// alloc-samples, alloc-size
if locals.profiling_allocation_enabled {
sample_types.extend_from_slice(&SAMPLE_TYPES[3..5]);
sample_values.extend_from_slice(&values[3..5]);
}
#[cfg(feature = "timeline")]
if locals.profiling_experimental_timeline_enabled {
sample_types.push(SAMPLE_TYPES[5]);
sample_values.push(values[5]);
}
#[cfg(php_has_fibers)]
if let Some(fiber) = unsafe { ddog_php_prof_get_active_fiber().as_mut() } {
// Safety: the fcc is set by Fiber::__construct as part of zpp,
// which will always set the function_handler on success, and
// there's nothing changing that value in all of fibers
// afterwards, from start to destruction of the fiber itself.
let func = unsafe { &*fiber.fci_cache.function_handler };
if let Some(functionname) = unsafe { extract_function_name(func) } {
labels.push(Label {
key: "fiber",
value: LabelValue::Str(functionname.into()),
});
}
}
}
let tags = Arc::clone(&locals.tags);
SampleMessage {
key: ProfileIndex {