Skip to content

Commit

Permalink
allow to enable all experimental features
Browse files Browse the repository at this point in the history
  • Loading branch information
realFlowControl committed Dec 6, 2023
1 parent 9688e0f commit 3d127bc
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 6 deletions.
23 changes: 23 additions & 0 deletions profiling/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub(crate) unsafe fn get_value(id: ConfigId) -> &'static mut zval {
#[derive(Clone, Copy)]
pub(crate) enum ConfigId {
ProfilingEnabled = 0,
ProfilingExperimentalFeaturesEnabled,
ProfilingEndpointCollectionEnabled,
ProfilingExperimentalCpuTimeEnabled,
ProfilingAllocationEnabled,
Expand All @@ -162,6 +163,7 @@ impl ConfigId {
const fn env_var_name(&self) -> ZaiStr {
let bytes: &'static [u8] = match self {
ProfilingEnabled => b"DD_PROFILING_ENABLED\0",
ProfilingExperimentalFeaturesEnabled => b"DD_PROFILING_EXPERIMENTAL_FEATURES_ENABLED\0",
ProfilingEndpointCollectionEnabled => b"DD_PROFILING_ENDPOINT_COLLECTION_ENABLED\0",
ProfilingExperimentalCpuTimeEnabled => b"DD_PROFILING_EXPERIMENTAL_CPU_TIME_ENABLED\0",
ProfilingAllocationEnabled => b"DD_PROFILING_ALLOCATION_ENABLED\0",
Expand Down Expand Up @@ -200,6 +202,13 @@ pub(crate) unsafe fn profiling_enabled() -> bool {
get_bool(ProfilingEnabled, true)
}

/// # Safety
/// This function must only be called after config has been initialized in
/// rinit, and before it is uninitialized in mshutdown.
pub(crate) unsafe fn profiling_experimental_features_enabled() -> bool {
get_bool(ProfilingExperimentalFeaturesEnabled, false)
}

/// # Safety
/// This function must only be called after config has been initialized in
/// rinit, and before it is uninitialized in mshutdown.
Expand Down Expand Up @@ -449,6 +458,16 @@ pub(crate) fn minit(module_number: libc::c_int) {
ini_change: None,
parser: None,
},
zai_config_entry {
id: transmute(ProfilingExperimentalFeaturesEnabled),
name: ProfilingExperimentalFeaturesEnabled.env_var_name(),
type_: ZAI_CONFIG_TYPE_BOOL,
default_encoded_value: ZaiStr::literal(b"0\0"),
aliases: std::ptr::null_mut(),
aliases_count: 0,
ini_change: None,
parser: None,
},
zai_config_entry {
id: transmute(ProfilingEndpointCollectionEnabled),
name: ProfilingEndpointCollectionEnabled.env_var_name(),
Expand Down Expand Up @@ -628,6 +647,10 @@ mod tests {
(b"DD_TRACE_AGENT_PORT\0", "datadog.trace.agent_port"),
(b"DD_AGENT_HOST\0", "datadog.agent_host"),
(b"DD_PROFILING_ENABLED\0", "datadog.profiling.enabled"),
(
b"DD_PROFILING_EXPERIMENTAL_FEATURES_ENABLED\0",
"datadog.profiling.experimental_features_enabled",
),
(
b"DD_PROFILING_ENDPOINT_COLLECTION_ENABLED\0",
"datadog.profiling.endpoint_collection_enabled",
Expand Down
48 changes: 42 additions & 6 deletions profiling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ pub struct RequestLocals {
pub env: Option<Cow<'static, str>>,
pub interrupt_count: AtomicU32,
pub profiling_enabled: bool,
pub profiling_experimental_features_enabled: bool,
pub profiling_endpoint_collection_enabled: bool,
pub profiling_experimental_cpu_time_enabled: bool,
pub profiling_allocation_enabled: bool,
Expand All @@ -387,6 +388,7 @@ pub struct RequestLocals {
impl RequestLocals {
pub fn disable(&mut self) {
self.profiling_enabled = false;
self.profiling_experimental_features_enabled = false;
self.profiling_endpoint_collection_enabled = false;
self.profiling_experimental_cpu_time_enabled = false;
self.profiling_allocation_enabled = false;
Expand All @@ -401,6 +403,7 @@ impl Default for RequestLocals {
env: None,
interrupt_count: AtomicU32::new(0),
profiling_enabled: false,
profiling_experimental_features_enabled: false,
profiling_endpoint_collection_enabled: true,
profiling_experimental_cpu_time_enabled: true,
profiling_allocation_enabled: true,
Expand Down Expand Up @@ -465,6 +468,7 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
// Safety: We are after first rinit and before mshutdown.
let (
profiling_enabled,
profiling_experimental_features_enabled,
profiling_endpoint_collection_enabled,
profiling_experimental_cpu_time_enabled,
profiling_allocation_enabled,
Expand All @@ -475,13 +479,19 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
output_pprof,
) = unsafe {
let profiling_enabled = config::profiling_enabled();
let profiling_experimental_features_enabled =
profiling_enabled && config::profiling_experimental_features_enabled();
(
profiling_enabled,
profiling_experimental_features_enabled,
profiling_enabled && config::profiling_endpoint_collection_enabled(),
profiling_enabled && config::profiling_experimental_cpu_time_enabled(),
profiling_experimental_features_enabled
|| profiling_enabled && config::profiling_experimental_cpu_time_enabled(),
profiling_enabled && config::profiling_allocation_enabled(),
profiling_enabled && config::profiling_experimental_timeline_enabled(),
profiling_enabled && config::profiling_experimental_exception_enabled(),
profiling_experimental_features_enabled
|| profiling_enabled && config::profiling_experimental_timeline_enabled(),
profiling_experimental_features_enabled
|| profiling_enabled && config::profiling_experimental_exception_enabled(),
config::profiling_experimental_exception_sampling_distance(),
config::profiling_log_level(),
config::profiling_output_pprof(),
Expand All @@ -496,6 +506,7 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
locals.interrupt_count.store(0, Ordering::SeqCst);

locals.profiling_enabled = profiling_enabled;
locals.profiling_experimental_features_enabled = profiling_experimental_features_enabled;
locals.profiling_endpoint_collection_enabled = profiling_endpoint_collection_enabled;
locals.profiling_experimental_cpu_time_enabled = profiling_experimental_cpu_time_enabled;
locals.profiling_allocation_enabled = profiling_allocation_enabled;
Expand Down Expand Up @@ -766,6 +777,7 @@ unsafe extern "C" fn minfo(module_ptr: *mut zend::ModuleEntry) {
REQUEST_LOCALS.with(|cell| {
let locals = cell.borrow();
let yes: &[u8] = b"true\0";
let yes_exp: &[u8] = b"true (all experimental features enabled)\0";
let no: &[u8] = b"false\0";
let no_all: &[u8] = b"false (profiling disabled)\0";
zend::php_info_print_table_start();
Expand All @@ -776,11 +788,27 @@ unsafe extern "C" fn minfo(module_ptr: *mut zend::ModuleEntry) {
if locals.profiling_enabled { yes } else { no },
);

zend::php_info_print_table_row(
2,
b"Profiling Experimental Features Enabled\0".as_ptr(),
if locals.profiling_experimental_features_enabled {
yes
} else if locals.profiling_enabled {
no
} else {
no_all
},
);

zend::php_info_print_table_row(
2,
b"Experimental CPU Time Profiling Enabled\0".as_ptr(),
if locals.profiling_experimental_cpu_time_enabled {
yes
if locals.profiling_experimental_features_enabled {
yes_exp
} else {
yes
}
} else if locals.profiling_enabled {
no
} else {
Expand Down Expand Up @@ -818,7 +846,11 @@ unsafe extern "C" fn minfo(module_ptr: *mut zend::ModuleEntry) {
2,
b"Experimental Timeline Enabled\0".as_ptr(),
if locals.profiling_experimental_timeline_enabled {
yes
if locals.profiling_experimental_features_enabled {
yes_exp
} else {
yes
}
} else if locals.profiling_enabled {
no
} else {
Expand All @@ -840,7 +872,11 @@ unsafe extern "C" fn minfo(module_ptr: *mut zend::ModuleEntry) {
2,
b"Experimental Exception Profiling Enabled\0".as_ptr(),
if locals.profiling_experimental_exception_enabled {
yes
if locals.profiling_experimental_features_enabled {
yes_exp
} else {
yes
}
} else if locals.profiling_enabled {
no
} else {
Expand Down
1 change: 1 addition & 0 deletions profiling/src/profiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ mod tests {
env: None,
interrupt_count: AtomicU32::new(0),
profiling_enabled: true,
profiling_experimental_features_enabled: false,
profiling_endpoint_collection_enabled: true,
profiling_experimental_cpu_time_enabled: false,
profiling_allocation_enabled: false,
Expand Down
64 changes: 64 additions & 0 deletions profiling/tests/phpt/phpinfo_05.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
[profiling] test profiler's extension info with experimental feature override
--DESCRIPTION--
The profiler's phpinfo section contains important debugging information. This
test verifies that certain information is present.
--SKIPIF--
<?php
if (!extension_loaded('datadog-profiling'))
echo "skip: test requires Datadog Continuous Profiler\n";
?>
--ENV--
DD_PROFILING_ENABLED=yes
DD_PROFILING_EXPERIMENTAL_FEATURES_ENABLED=yes
DD_PROFILING_LOG_LEVEL=off
DD_PROFILING_EXPERIMENTAL_CPU_TIME_ENABLED=no
DD_PROFILING_ALLOCATION_ENABLED=no
DD_PROFILING_EXPERIMENTAL_EXCEPTION_ENABLED=no
DD_PROFILING_EXPERIMENTAL_TIMELINE_ENABLED=no
--INI--
assert.exception=1
opcache.jit=off
--FILE--
<?php

ob_start();
$extension = new ReflectionExtension('datadog-profiling');
$extension->info();
$output = ob_get_clean();

$lines = preg_split("/\R/", $output);
$values = [];
foreach ($lines as $line) {
$pair = explode("=>", $line);
if (count($pair) != 2) {
continue;
}
$values[trim($pair[0])] = trim($pair[1]);
}

// Check that Version exists, but not its value
assert(isset($values["Version"]));

// Check exact values for this set
$sections = [
["Profiling Enabled", "true"],
["Profiling Experimental Features Enabled", "true"],
["Experimental CPU Time Profiling Enabled", "true (all experimental features enabled)"],
["Allocation Profiling Enabled", "false"],
["Experimental Exception Profiling Enabled", "true (all experimental features enabled)"],
["Experimental Timeline Enabled", "true (all experimental features enabled)"],
];

foreach ($sections as [$key, $expected]) {
assert(
$values[$key] === $expected,
"Expected '{$expected}', found '{$values[$key]}', for key '{$key}'"
);
}

echo "Done.";

?>
--EXPECT--
Done.
64 changes: 64 additions & 0 deletions profiling/tests/phpt/phpinfo_06.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
[profiling] test profiler's extension info with experimental feature override
--DESCRIPTION--
The profiler's phpinfo section contains important debugging information. This
test verifies that certain information is present.
--SKIPIF--
<?php
if (!extension_loaded('datadog-profiling'))
echo "skip: test requires Datadog Continuous Profiler\n";
?>
--ENV--
DD_PROFILING_ENABLED=no
DD_PROFILING_EXPERIMENTAL_FEATURES_ENABLED=yes
DD_PROFILING_LOG_LEVEL=off
DD_PROFILING_EXPERIMENTAL_CPU_TIME_ENABLED=no
DD_PROFILING_ALLOCATION_ENABLED=no
DD_PROFILING_EXPERIMENTAL_EXCEPTION_ENABLED=no
DD_PROFILING_EXPERIMENTAL_TIMELINE_ENABLED=no
--INI--
assert.exception=1
opcache.jit=off
--FILE--
<?php

ob_start();
$extension = new ReflectionExtension('datadog-profiling');
$extension->info();
$output = ob_get_clean();

$lines = preg_split("/\R/", $output);
$values = [];
foreach ($lines as $line) {
$pair = explode("=>", $line);
if (count($pair) != 2) {
continue;
}
$values[trim($pair[0])] = trim($pair[1]);
}

// Check that Version exists, but not its value
assert(isset($values["Version"]));

// Check exact values for this set
$sections = [
["Profiling Enabled", "false"],
["Profiling Experimental Features Enabled", "false (profiling disabled)"],
["Experimental CPU Time Profiling Enabled", "false (profiling disabled)"],
["Allocation Profiling Enabled", "false (profiling disabled)"],
["Experimental Exception Profiling Enabled", "false (profiling disabled)"],
["Experimental Timeline Enabled", "false (profiling disabled)"],
];

foreach ($sections as [$key, $expected]) {
assert(
$values[$key] === $expected,
"Expected '{$expected}', found '{$values[$key]}', for key '{$key}'"
);
}

echo "Done.";

?>
--EXPECT--
Done.

0 comments on commit 3d127bc

Please sign in to comment.