Skip to content

Commit 0994638

Browse files
authored
fix(profiling): revert unsafe optimization (#3541)
1 parent ba46f8b commit 0994638

File tree

5 files changed

+34
-132
lines changed

5 files changed

+34
-132
lines changed

profiling/src/allocation/allocation_ge84.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::allocation::{
2-
allocation_profiling_stats_mut, allocation_profiling_stats_should_collect, collect_allocation,
3-
};
1+
use crate::allocation::{collect_allocation, ALLOCATION_PROFILING_STATS};
42
use crate::bindings::{self as zend};
53
use crate::{RefCellExt, PROFILER_NAME};
64
use core::{cell::Cell, ptr};
@@ -368,7 +366,9 @@ unsafe extern "C" fn alloc_prof_malloc(len: size_t) -> *mut c_void {
368366
return ptr;
369367
}
370368

371-
if allocation_profiling_stats_should_collect(len) {
369+
if ALLOCATION_PROFILING_STATS
370+
.borrow_mut_or_false(|allocations| allocations.should_collect_allocation(len))
371+
{
372372
collect_allocation(len);
373373
}
374374

@@ -437,7 +437,9 @@ unsafe extern "C" fn alloc_prof_realloc(prev_ptr: *mut c_void, len: size_t) -> *
437437
return ptr;
438438
}
439439

440-
if allocation_profiling_stats_should_collect(len) {
440+
if ALLOCATION_PROFILING_STATS
441+
.borrow_mut_or_false(|allocations| allocations.should_collect_allocation(len))
442+
{
441443
collect_allocation(len);
442444
}
443445

profiling/src/allocation/allocation_le83.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::allocation::{allocation_profiling_stats_should_collect, collect_allocation};
1+
use crate::allocation::{collect_allocation, ALLOCATION_PROFILING_STATS};
22
use crate::bindings::{
33
self as zend, datadog_php_install_handler, datadog_php_zif_handler,
44
ddog_php_prof_copy_long_into_zval,
@@ -357,7 +357,9 @@ unsafe extern "C" fn alloc_prof_malloc(len: size_t) -> *mut c_void {
357357
return ptr;
358358
}
359359

360-
if allocation_profiling_stats_should_collect(len) {
360+
if ALLOCATION_PROFILING_STATS
361+
.borrow_mut_or_false(|allocations| allocations.should_collect_allocation(len))
362+
{
361363
collect_allocation(len);
362364
}
363365

@@ -416,7 +418,9 @@ unsafe extern "C" fn alloc_prof_realloc(prev_ptr: *mut c_void, len: size_t) -> *
416418
return ptr;
417419
}
418420

419-
if allocation_profiling_stats_should_collect(len) {
421+
if ALLOCATION_PROFILING_STATS
422+
.borrow_mut_or_false(|allocations| allocations.should_collect_allocation(len))
423+
{
420424
collect_allocation(len);
421425
}
422426

profiling/src/allocation/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
mod tls_allocation_profiling_stats;
2-
31
#[cfg(php_zend_mm_set_custom_handlers_ex)]
42
pub mod allocation_ge84;
53
#[cfg(not(php_zend_mm_set_custom_handlers_ex))]
64
pub mod allocation_le83;
75

8-
pub use tls_allocation_profiling_stats::*;
9-
106
use crate::bindings::{self as zend};
117
use crate::profiling::Profiler;
128
use crate::{RefCellExt, REQUEST_LOCALS};
139
use libc::size_t;
1410
use log::{debug, error, trace};
1511
use rand::rngs::ThreadRng;
1612
use rand_distr::{Distribution, Poisson};
13+
use std::cell::RefCell;
1714
use std::ffi::c_void;
1815
use std::sync::atomic::{AtomicU64, Ordering};
1916

@@ -88,6 +85,23 @@ pub fn collect_allocation(len: size_t) {
8885
}
8986
}
9087

88+
thread_local! {
89+
static ALLOCATION_PROFILING_STATS: RefCell<AllocationProfilingStats> =
90+
RefCell::new(AllocationProfilingStats::new());
91+
}
92+
93+
pub fn alloc_prof_ginit() {
94+
#[cfg(not(php_zend_mm_set_custom_handlers_ex))]
95+
allocation_le83::alloc_prof_ginit();
96+
#[cfg(php_zend_mm_set_custom_handlers_ex)]
97+
allocation_ge84::alloc_prof_ginit();
98+
}
99+
100+
pub fn alloc_prof_gshutdown() {
101+
#[cfg(php_zend_mm_set_custom_handlers_ex)]
102+
allocation_ge84::alloc_prof_gshutdown();
103+
}
104+
91105
#[cfg(not(php_zend_mm_set_custom_handlers_ex))]
92106
pub fn alloc_prof_startup() {
93107
allocation_le83::alloc_prof_startup();

profiling/src/allocation/tls_allocation_profiling_stats.rs

Lines changed: 0 additions & 118 deletions
This file was deleted.

profiling/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ unsafe extern "C" fn ginit(_globals_ptr: *mut c_void) {
215215
timeline::timeline_ginit();
216216

217217
// SAFETY: this is called in thread ginit as expected, and no other places.
218-
allocation::ginit();
218+
allocation::alloc_prof_ginit();
219219
}
220220

221221
unsafe extern "C" fn gshutdown(_globals_ptr: *mut c_void) {
222222
#[cfg(php_zts)]
223223
timeline::timeline_gshutdown();
224224

225225
// SAFETY: this is called in thread gshutdown as expected, no other places.
226-
allocation::gshutdown();
226+
allocation::alloc_prof_gshutdown();
227227
}
228228

229229
// Important note on the PHP lifecycle:

0 commit comments

Comments
 (0)