Skip to content

Commit b0ce8e9

Browse files
remove stats from hot path
1 parent d84a013 commit b0ce8e9

6 files changed

Lines changed: 38 additions & 14 deletions

File tree

profiling/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ harness = false
6262

6363
[features]
6464
default = ["io_profiling"]
65+
debug_stats = []
6566
io_profiling = []
6667
stack_walking_tests = []
6768
test = []

profiling/src/allocation/allocation_ge84.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
use crate::allocation::{
2-
collect_allocation, ALLOCATION_PROFILING_COUNT, ALLOCATION_PROFILING_SIZE,
3-
ALLOCATION_PROFILING_STATS,
4-
};
1+
use crate::allocation::{collect_allocation, ALLOCATION_PROFILING_STATS};
52
use crate::bindings::{self as zend};
63
use crate::{RefCellExt, PROFILER_NAME};
74
use core::{cell::Cell, ptr};
85
use lazy_static::lazy_static;
96
use libc::{c_char, c_void, size_t};
107
use log::{debug, error, trace, warn};
11-
use std::sync::atomic::Ordering::{Relaxed, SeqCst};
8+
use std::sync::atomic::Ordering::Relaxed;
9+
10+
#[cfg(feature = "debug_stats")]
11+
use crate::allocation::{ALLOCATION_PROFILING_COUNT, ALLOCATION_PROFILING_SIZE};
12+
#[cfg(feature = "debug_stats")]
13+
use std::sync::atomic::Ordering::SeqCst;
1214

1315
#[derive(Copy, Clone)]
1416
struct ZendMMState {
@@ -353,7 +355,9 @@ pub fn alloc_prof_rshutdown() {
353355
}
354356

355357
unsafe extern "C" fn alloc_prof_malloc(len: size_t) -> *mut c_void {
358+
#[cfg(feature = "debug_stats")]
356359
ALLOCATION_PROFILING_COUNT.fetch_add(1, SeqCst);
360+
#[cfg(feature = "debug_stats")]
357361
ALLOCATION_PROFILING_SIZE.fetch_add(len as u64, SeqCst);
358362

359363
let ptr = tls_zend_mm_state_get!(alloc)(len);
@@ -425,7 +429,9 @@ unsafe fn alloc_prof_orig_free(ptr: *mut c_void) {
425429
}
426430

427431
unsafe extern "C" fn alloc_prof_realloc(prev_ptr: *mut c_void, len: size_t) -> *mut c_void {
432+
#[cfg(feature = "debug_stats")]
428433
ALLOCATION_PROFILING_COUNT.fetch_add(1, SeqCst);
434+
#[cfg(feature = "debug_stats")]
429435
ALLOCATION_PROFILING_SIZE.fetch_add(len as u64, SeqCst);
430436

431437
let ptr = tls_zend_mm_state_get!(realloc)(prev_ptr, len);

profiling/src/allocation/allocation_le83.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::allocation::{
2-
collect_allocation, ALLOCATION_PROFILING_COUNT, ALLOCATION_PROFILING_SIZE,
3-
ALLOCATION_PROFILING_STATS,
4-
};
1+
use crate::allocation::{collect_allocation, ALLOCATION_PROFILING_STATS};
52
use crate::bindings::{
63
self as zend, datadog_php_install_handler, datadog_php_zif_handler,
74
ddog_php_prof_copy_long_into_zval,
@@ -11,7 +8,12 @@ use core::{cell::Cell, ptr};
118
use lazy_static::lazy_static;
129
use libc::{c_char, c_int, c_void, size_t};
1310
use log::{debug, error, trace, warn};
14-
use std::sync::atomic::Ordering::{Relaxed, SeqCst};
11+
use std::sync::atomic::Ordering::Relaxed;
12+
13+
#[cfg(feature = "debug_stats")]
14+
use crate::allocation::{ALLOCATION_PROFILING_COUNT, ALLOCATION_PROFILING_SIZE};
15+
#[cfg(feature = "debug_stats")]
16+
use std::sync::atomic::Ordering::SeqCst;
1517

1618
static mut GC_MEM_CACHES_HANDLER: zend::InternalFunctionHandler = None;
1719

@@ -344,7 +346,9 @@ unsafe extern "C" fn alloc_prof_gc_mem_caches(
344346
}
345347

346348
unsafe extern "C" fn alloc_prof_malloc(len: size_t) -> *mut c_void {
349+
#[cfg(feature = "debug_stats")]
347350
ALLOCATION_PROFILING_COUNT.fetch_add(1, SeqCst);
351+
#[cfg(feature = "debug_stats")]
348352
ALLOCATION_PROFILING_SIZE.fetch_add(len as u64, SeqCst);
349353

350354
let ptr = tls_zend_mm_state_get!(alloc)(len);
@@ -406,7 +410,9 @@ unsafe fn alloc_prof_orig_free(ptr: *mut c_void) {
406410
}
407411

408412
unsafe extern "C" fn alloc_prof_realloc(prev_ptr: *mut c_void, len: size_t) -> *mut c_void {
413+
#[cfg(feature = "debug_stats")]
409414
ALLOCATION_PROFILING_COUNT.fetch_add(1, SeqCst);
415+
#[cfg(feature = "debug_stats")]
410416
ALLOCATION_PROFILING_SIZE.fetch_add(len as u64, SeqCst);
411417

412418
let ptr = tls_zend_mm_state_get!(realloc)(prev_ptr, len);

profiling/src/allocation/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ pub static ALLOCATION_PROFILING_INTERVAL: AtomicU64 =
2525
/// This will store the count of allocations (including reallocations) during
2626
/// a profiling period. This will overflow when doing more than u64::MAX
2727
/// allocations, which seems big enough to ignore.
28+
#[cfg(feature = "debug_stats")]
2829
pub static ALLOCATION_PROFILING_COUNT: AtomicU64 = AtomicU64::new(0);
2930

3031
/// This will store the accumulated size of all allocations in bytes during the
3132
/// profiling period. This will overflow when allocating more than 18 exabyte
3233
/// of memory (u64::MAX) which might not happen, so we can ignore this.
34+
#[cfg(feature = "debug_stats")]
3335
pub static ALLOCATION_PROFILING_SIZE: AtomicU64 = AtomicU64::new(0);
3436

3537
pub struct AllocationProfilingStats {

profiling/src/exception.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub static EXCEPTION_PROFILING_INTERVAL: AtomicU32 =
2626
/// This will store the number of exceptions thrown during a profiling period. It will overflow
2727
/// when throwing more then 4_294_967_295 exceptions during this period which we currently
2828
/// believe will bring down your application anyway, so accurate numbers are not a problem.
29+
#[cfg(feature = "debug_stats")]
2930
pub static EXCEPTION_PROFILING_EXCEPTION_COUNT: AtomicU32 = AtomicU32::new(0);
3031

3132
pub struct ExceptionProfilingStats {
@@ -184,6 +185,7 @@ unsafe extern "C" fn exception_profiling_throw_exception_hook(
184185
#[cfg(php7)] exception: *mut zend::zval,
185186
#[cfg(php8)] exception: *mut zend::zend_object,
186187
) {
188+
#[cfg(feature = "debug_stats")]
187189
EXCEPTION_PROFILING_EXCEPTION_COUNT.fetch_add(1, Ordering::SeqCst);
188190

189191
let exception_enabled = REQUEST_LOCALS

profiling/src/profiling/uploader.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::allocation::{ALLOCATION_PROFILING_COUNT, ALLOCATION_PROFILING_SIZE};
21
use crate::config::AgentEndpoint;
3-
use crate::exception::EXCEPTION_PROFILING_EXCEPTION_COUNT;
42
use crate::profiling::{UploadMessage, UploadRequest};
53
use crate::{PROFILER_NAME_STR, PROFILER_VERSION_STR};
64
use chrono::{DateTime, Utc};
@@ -10,9 +8,15 @@ use log::{debug, info, warn};
108
use serde_json::json;
119
use std::borrow::Cow;
1210
use std::str;
13-
use std::sync::atomic::Ordering;
1411
use std::sync::{Arc, Barrier};
1512

13+
#[cfg(feature = "debug_stats")]
14+
use crate::allocation::{ALLOCATION_PROFILING_COUNT, ALLOCATION_PROFILING_SIZE};
15+
#[cfg(feature = "debug_stats")]
16+
use crate::exception::EXCEPTION_PROFILING_EXCEPTION_COUNT;
17+
#[cfg(feature = "debug_stats")]
18+
use std::sync::atomic::Ordering;
19+
1620
pub struct Uploader {
1721
fork_barrier: Arc<Barrier>,
1822
receiver: Receiver<UploadMessage>,
@@ -40,6 +44,7 @@ impl Uploader {
4044

4145
/// This function will not only create the internal metadata JSON representation, but is also
4246
/// in charge to reset all those counters back to 0.
47+
#[cfg(feature = "debug_stats")]
4348
fn create_internal_metadata() -> Option<serde_json::Value> {
4449
Some(json!({
4550
"exceptions_count": EXCEPTION_PROFILING_EXCEPTION_COUNT.swap(0, Ordering::SeqCst),
@@ -83,7 +88,8 @@ impl Uploader {
8388
&[],
8489
&[],
8590
None,
86-
Self::create_internal_metadata(),
91+
#[cfg(feature = "debug_stats")] Self::create_internal_metadata(),
92+
#[cfg(not(feature = "debug_stats"))] None,
8793
self.create_profiler_info(),
8894
)?;
8995
debug!("Sending profile to: {agent_endpoint}");
@@ -158,6 +164,7 @@ mod tests {
158164
use super::*;
159165

160166
#[test]
167+
#[cfg(feature = "debug_stats")]
161168
fn test_create_internal_metadata() {
162169
// Set up all counters with known values
163170
EXCEPTION_PROFILING_EXCEPTION_COUNT.store(42, Ordering::SeqCst);

0 commit comments

Comments
 (0)