Skip to content

Commit b21cc23

Browse files
authored
refactor(profiling): remove locks and simplify PROFILER_NAME_STR/PROFILER_VERSION_STR (#3998)
* refactor(profiling): remove locks and simplify PROFILER_NAME_STR/PROFILER_VERSION_STR * style(profiling): run cargo fmt * style(profiling): remove needless borrows and redundant static lifetimes
1 parent a8cd140 commit b21cc23

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

profiling/src/allocation/allocation_ge84.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ unsafe fn alloc_prof_prev_alloc(len: size_t) -> *mut c_void {
327327
let alloc = tls_zend_mm_state_get!(prev_custom_mm_alloc).unwrap();
328328
#[cfg(php_debug)]
329329
{
330-
return alloc(len, ptr::null(), 0, ptr::null(), 0);
330+
alloc(len, ptr::null(), 0, ptr::null(), 0)
331331
}
332332
#[cfg(not(php_debug))]
333333
alloc(len)
@@ -407,7 +407,7 @@ unsafe fn alloc_prof_prev_free(ptr: *mut c_void) {
407407
let free = tls_zend_mm_state_get!(prev_custom_mm_free).unwrap();
408408
#[cfg(php_debug)]
409409
{
410-
return free(ptr, core::ptr::null(), 0, core::ptr::null(), 0);
410+
free(ptr, core::ptr::null(), 0, core::ptr::null(), 0)
411411
}
412412
#[cfg(not(php_debug))]
413413
free(ptr)
@@ -530,7 +530,7 @@ unsafe fn alloc_prof_prev_realloc(prev_ptr: *mut c_void, len: size_t) -> *mut c_
530530
let realloc = tls_zend_mm_state_get!(prev_custom_mm_realloc).unwrap();
531531
#[cfg(php_debug)]
532532
{
533-
return realloc(prev_ptr, len, ptr::null(), 0, ptr::null(), 0);
533+
realloc(prev_ptr, len, ptr::null(), 0, ptr::null(), 0)
534534
}
535535
#[cfg(not(php_debug))]
536536
realloc(prev_ptr, len)

profiling/src/lib.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,26 @@ use uuid::Uuid;
5252
/// interior null bytes and must be null terminated.
5353
static PROFILER_NAME: &CStr = c"datadog-profiling";
5454

55+
// SAFETY: PROFILER_NAME is a valid utf8 string.
56+
static PROFILER_NAME_STR: &str = match PROFILER_NAME.to_str() {
57+
Ok(s) => s,
58+
// Panic: we own this string and it should be UTF8 (see PROFILER_NAME above).
59+
Err(_) => panic!(""),
60+
};
61+
5562
/// Version of the profiling module and zend_extension. Must not contain any
5663
/// interior null bytes and must be null terminated.
5764
static PROFILER_VERSION: &[u8] = concat!(env!("PROFILER_VERSION"), "\0").as_bytes();
5865

66+
// SAFETY: PROFILER_VERSION is a byte slice that satisfies the safety requirements.
67+
static PROFILER_VERSION_STR: &str = const {
68+
match unsafe { CStr::from_ptr(PROFILER_VERSION.as_ptr() as *const c_char).to_str() } {
69+
Ok(v) => v,
70+
// Panic: we own this string and it should be UTF8 (see PROFILER_VERSION above).
71+
Err(_) => panic!("PROFILER_VERSION was not a valid utf-8 string"),
72+
}
73+
};
74+
5975
/// Version ID of PHP at run-time, not the version it was built against at
6076
/// compile-time. Its value is overwritten during minit.
6177
static RUNTIME_PHP_VERSION_ID: AtomicU32 = AtomicU32::new(zend::PHP_VERSION_ID);
@@ -130,18 +146,6 @@ static SAPI: LazyLock<Sapi> = LazyLock::new(|| {
130146
}
131147
});
132148

133-
// SAFETY: PROFILER_NAME is a byte slice that satisfies the safety requirements.
134-
// Panic: we own this string and it should be UTF8 (see PROFILER_NAME above).
135-
static PROFILER_NAME_STR: LazyLock<&'static str> = LazyLock::new(|| PROFILER_NAME.to_str().unwrap());
136-
137-
// SAFETY: PROFILER_VERSION is a byte slice that satisfies the safety requirements.
138-
static PROFILER_VERSION_STR: LazyLock<&'static str> = LazyLock::new(|| {
139-
unsafe { CStr::from_ptr(PROFILER_VERSION.as_ptr() as *const c_char) }
140-
.to_str()
141-
// Panic: we own this string and it should be UTF8 (see PROFILER_VERSION above).
142-
.unwrap()
143-
});
144-
145149
/// The runtime ID, which is basically a universally unique "pid". This makes
146150
/// it almost const, the exception being to re-initialize it from a child fork
147151
/// handler. We don't yet support forking, so we use OnceCell.
@@ -618,8 +622,9 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
618622
warn!("{err}");
619623
}
620624
locals.tags = tags;
621-
locals.profiling_experimental_heap_live_enabled =
622-
system_settings.as_ref().profiling_experimental_heap_live_enabled
625+
locals.profiling_experimental_heap_live_enabled = system_settings
626+
.as_ref()
627+
.profiling_experimental_heap_live_enabled
623628
&& config::profiling_experimental_heap_live_enabled_current();
624629
}
625630
locals.system_settings = system_settings;

profiling/src/profiling/uploader.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,13 @@ impl Uploader {
107107
let index = Arc::unwrap_or_clone(message.index);
108108
let profile = message.profile;
109109

110-
let profiling_library_name: &str = &PROFILER_NAME_STR;
111-
let profiling_library_version: &str = &PROFILER_VERSION_STR;
112110
let agent_endpoint = &self.endpoint;
113111
let endpoint = Endpoint::try_from(agent_endpoint)?;
114112

115113
let tags = Arc::unwrap_or_clone(index.tags);
116114
let mut exporter = libdd_profiling::exporter::ProfileExporter::new(
117-
profiling_library_name,
118-
profiling_library_version,
115+
PROFILER_NAME_STR,
116+
PROFILER_VERSION_STR,
119117
"php",
120118
tags,
121119
endpoint,

0 commit comments

Comments
 (0)