|
| 1 | +use crate::bindings::ddog_php_prof_get_current_execute_data; |
| 2 | +use crate::Profiler; |
| 3 | +use libc::{c_int, c_void, sigaction, siginfo_t, SA_RESTART, SA_SIGINFO}; |
| 4 | +use std::mem; |
| 5 | +use std::ptr; |
| 6 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 7 | + |
| 8 | +static SIGNAL_HANDLER_ACTIVE: AtomicBool = AtomicBool::new(false); |
| 9 | + |
| 10 | +extern "C" fn profiling_signal_handler(_signum: c_int, _info: *mut siginfo_t, _context: *mut c_void) { |
| 11 | + // Avoid re-entrancy if possible, though strict re-entrancy isn't guaranteed with this logic alone |
| 12 | + // in a signal handler. However, for profiling, we usually just skip if busy. |
| 13 | + // For now, let's just try to collect. |
| 14 | + |
| 15 | + // Safety: Accessing global state in a signal handler is risky. |
| 16 | + // Profiler::get() reads a static OnceCell. |
| 17 | + if let Some(profiler) = Profiler::get() { |
| 18 | + let execute_data = unsafe { ddog_php_prof_get_current_execute_data() }; |
| 19 | + if !execute_data.is_null() { |
| 20 | + profiler.collect_time(execute_data, 1); |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +pub fn start_profiling_mechanism() { |
| 26 | + if SIGNAL_HANDLER_ACTIVE.swap(true, Ordering::SeqCst) { |
| 27 | + return; // Already started |
| 28 | + } |
| 29 | + |
| 30 | + #[cfg(target_os = "linux")] |
| 31 | + start_linux_timer(); |
| 32 | + |
| 33 | + #[cfg(target_os = "macos")] |
| 34 | + start_macos_sidecar(); |
| 35 | +} |
| 36 | + |
| 37 | +#[cfg(target_os = "linux")] |
| 38 | +fn start_linux_timer() { |
| 39 | + use libc::{ |
| 40 | + timer_create, timer_settime, sigevent, itimerspec, timespec, |
| 41 | + CLOCK_MONOTONIC, SIGEV_THREAD, SIGRTMIN, syscall, SYS_gettid |
| 42 | + }; |
| 43 | + |
| 44 | + unsafe { |
| 45 | + // 1. Pick a signal: SIGRTMIN + 1 |
| 46 | + // SIGRTMIN is a macro or function in libc usually. |
| 47 | + let signo = SIGRTMIN() + 1; |
| 48 | + |
| 49 | + // 2. Register Signal Handler |
| 50 | + let mut sa: sigaction = mem::zeroed(); |
| 51 | + sa.sa_sigaction = profiling_signal_handler as usize; |
| 52 | + sa.sa_flags = SA_SIGINFO | SA_RESTART; |
| 53 | + libc::sigemptyset(&mut sa.sa_mask); |
| 54 | + libc::sigaction(signo, &sa, ptr::null_mut()); |
| 55 | + |
| 56 | + // 3. Create Timer |
| 57 | + let mut sev: sigevent = mem::zeroed(); |
| 58 | + sev.sigev_notify = SIGEV_THREAD; |
| 59 | + sev.sigev_signo = signo; |
| 60 | + sev.sigev_value.sival_ptr = ptr::null_mut(); |
| 61 | + // Target the current thread (the PHP main thread) |
| 62 | + sev.sigev_notify_thread_id = syscall(SYS_gettid) as i32; |
| 63 | + |
| 64 | + let mut timer_id: libc::timer_t = mem::zeroed(); |
| 65 | + if timer_create(CLOCK_MONOTONIC, &mut sev, &mut timer_id) == 0 { |
| 66 | + // 4. Arm Timer (10ms) |
| 67 | + let interval = timespec { |
| 68 | + tv_sec: 0, |
| 69 | + tv_nsec: 10 * 1_000_000, // 10ms |
| 70 | + }; |
| 71 | + let its = itimerspec { |
| 72 | + it_interval: interval, |
| 73 | + it_value: interval, |
| 74 | + }; |
| 75 | + timer_settime(timer_id, 0, &its, ptr::null_mut()); |
| 76 | + |
| 77 | + // We might want to store timer_id somewhere to delete it later, |
| 78 | + // but for this example we leak it or let it die with the process. |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(target_os = "macos")] |
| 84 | +fn start_macos_sidecar() { |
| 85 | + use libc::{SIGURG, pthread_self, pthread_t}; |
| 86 | + use std::thread; |
| 87 | + use std::time::Duration; |
| 88 | + |
| 89 | + unsafe { |
| 90 | + // 1. Register Signal Handler |
| 91 | + let signo = SIGURG; |
| 92 | + let mut sa: sigaction = mem::zeroed(); |
| 93 | + sa.sa_sigaction = profiling_signal_handler as usize; |
| 94 | + // SA_RESTART is important so we don't interrupt blocking syscalls like read() with EINTR too often |
| 95 | + // unless we want to? For profiling, we generally want to see what's happening. |
| 96 | + sa.sa_flags = SA_SIGINFO | SA_RESTART; |
| 97 | + libc::sigemptyset(&mut sa.sa_mask); |
| 98 | + libc::sigaction(signo, &sa, ptr::null_mut()); |
| 99 | + |
| 100 | + // 2. Capture Main Thread ID |
| 101 | + let main_thread: pthread_t = pthread_self(); |
| 102 | + |
| 103 | + // 3. Spawn Sidecar Thread |
| 104 | + thread::Builder::new() |
| 105 | + .name("ddprof_sidecar".to_string()) |
| 106 | + .spawn(move || { |
| 107 | + loop { |
| 108 | + thread::sleep(Duration::from_millis(10)); |
| 109 | + // Send signal to main thread |
| 110 | + let ret = libc::pthread_kill(main_thread, signo); |
| 111 | + if ret != 0 { |
| 112 | + // Main thread might have exited? |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + }) |
| 117 | + .expect("Failed to spawn sidecar thread"); |
| 118 | + } |
| 119 | +} |
0 commit comments