Skip to content

Commit

Permalink
support FrankenPHP worker mode
Browse files Browse the repository at this point in the history
  • Loading branch information
realFlowControl committed Feb 5, 2024
1 parent 078468e commit 3184c0b
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions profiling/src/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,32 @@ static mut USLEEP_HANDLER: InternalFunctionHandler = None;
static mut TIME_NANOSLEEP_HANDLER: InternalFunctionHandler = None;
static mut TIME_SLEEP_UNTIL_HANDLER: InternalFunctionHandler = None;

static mut FRANKENPHP_HANDLE_REQUEST_HANDLER: InternalFunctionHandler = None;

thread_local! {
static IDLE_SINCE: RefCell<Instant> = RefCell::new(Instant::now());
}

enum SleepingState {
Idle,
Sleeping,
}

impl SleepingState {
fn as_str(&self) -> &'static str {
match self {
SleepingState::Idle => "idle",
SleepingState::Sleeping => "sleeping",
}
}
}

#[inline]
fn try_sleeping_fn(
func: unsafe extern "C" fn(execute_data: *mut zend_execute_data, return_value: *mut zval),
execute_data: *mut zend_execute_data,
return_value: *mut zval,
reason: SleepingState,
) -> anyhow::Result<()> {
let timeline_enabled = REQUEST_LOCALS.with(|cell| {
cell.try_borrow()
Expand Down Expand Up @@ -71,7 +88,7 @@ fn try_sleeping_fn(
if let Some(profiler) = guard.as_ref() {
let now = now.as_nanos() as i64;
let duration = duration.as_nanos() as i64;
profiler.collect_idle(now, duration, "sleeping")
profiler.collect_idle(now, duration, reason.as_str())
}
}
Err(err) => anyhow::bail!("profiler mutex: {err:#}"),
Expand All @@ -83,8 +100,9 @@ fn sleeping_fn(
func: unsafe extern "C" fn(execute_data: *mut zend_execute_data, return_value: *mut zval),
execute_data: *mut zend_execute_data,
return_value: *mut zval,
reason: SleepingState,
) {
if let Err(err) = try_sleeping_fn(func, execute_data, return_value) {
if let Err(err) = try_sleeping_fn(func, execute_data, return_value, reason) {
warn!("error creating profiling timeline sample for an internal function: {err:#}");
}
}
Expand All @@ -96,7 +114,7 @@ unsafe extern "C" fn ddog_php_prof_sleep(
return_value: *mut zval,
) {
if let Some(func) = SLEEP_HANDLER {
sleeping_fn(func, execute_data, return_value)
sleeping_fn(func, execute_data, return_value, SleepingState::Sleeping)
}
}

Expand All @@ -107,7 +125,7 @@ unsafe extern "C" fn ddog_php_prof_usleep(
return_value: *mut zval,
) {
if let Some(func) = USLEEP_HANDLER {
sleeping_fn(func, execute_data, return_value)
sleeping_fn(func, execute_data, return_value, SleepingState::Sleeping)
}
}

Expand All @@ -118,7 +136,7 @@ unsafe extern "C" fn ddog_php_prof_time_nanosleep(
return_value: *mut zval,
) {
if let Some(func) = TIME_NANOSLEEP_HANDLER {
sleeping_fn(func, execute_data, return_value)
sleeping_fn(func, execute_data, return_value, SleepingState::Sleeping)
}
}

Expand All @@ -129,7 +147,19 @@ unsafe extern "C" fn ddog_php_prof_time_sleep_until(
return_value: *mut zval,
) {
if let Some(func) = TIME_SLEEP_UNTIL_HANDLER {
sleeping_fn(func, execute_data, return_value)
sleeping_fn(func, execute_data, return_value, SleepingState::Sleeping)
}
}

/// Wrapping the FrankenPHP `frankenphp_handle_request()` function to take the time it is blocking
/// the current thread, or in this case waiting for another request to be assigned to this thread
#[no_mangle]
unsafe extern "C" fn ddog_php_prof_frankenphp_handle_request(
execute_data: *mut zend_execute_data,
return_value: *mut zval,
) {
if let Some(func) = FRANKENPHP_HANDLE_REQUEST_HANDLER {
sleeping_fn(func, execute_data, return_value, SleepingState::Idle)
}
}

Expand Down Expand Up @@ -174,6 +204,11 @@ pub unsafe fn timeline_startup() {
&mut TIME_SLEEP_UNTIL_HANDLER,
Some(ddog_php_prof_time_sleep_until),
),
zend::datadog_php_zif_handler::new(
CStr::from_bytes_with_nul_unchecked(b"frankenphp_handle_request\0"),
&mut FRANKENPHP_HANDLE_REQUEST_HANDLER,
Some(ddog_php_prof_frankenphp_handle_request),
),
];

for handler in handlers.into_iter() {
Expand Down

0 comments on commit 3184c0b

Please sign in to comment.