Skip to content

Commit 4352c47

Browse files
committed
experiment with SystemSettingsState tracking
1 parent f6261b9 commit 4352c47

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

profiling/src/config.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,30 @@ use core::str::FromStr;
1414
use libc::{c_char, c_int};
1515
use libdd_common::tag::{parse_tags, Tag};
1616
pub use libdd_profiling::exporter::Uri;
17-
use log::{warn, LevelFilter};
17+
use log::{debug, warn, LevelFilter};
1818
use std::borrow::Cow;
1919
use std::ffi::CString;
2020
use std::path::{Path, PathBuf};
2121

22+
#[derive(Copy, Clone, Debug, Default)]
23+
pub enum SystemSettingsState {
24+
/// Indicates the system settings are not aware of the configuration at
25+
/// the moment.
26+
#[default]
27+
ConfigUnaware,
28+
29+
/// Indicates the system settings _are_ aware of configuration at the
30+
/// moment.
31+
ConfigAware,
32+
33+
/// Expressly disabled, such as a child process post-fork (forks are not
34+
/// currently profiled, except certain forks made by SAPIs).
35+
Disabled,
36+
}
37+
2238
#[derive(Clone, Debug)]
2339
pub struct SystemSettings {
40+
pub state: SystemSettingsState,
2441
pub profiling_enabled: bool,
2542
pub profiling_experimental_features_enabled: bool,
2643
pub profiling_endpoint_collection_enabled: bool,
@@ -44,6 +61,7 @@ impl SystemSettings {
4461
/// Provides "initial" settings, which are all "off"-like values.
4562
pub const fn initial() -> SystemSettings {
4663
SystemSettings {
64+
state: SystemSettingsState::ConfigUnaware,
4765
profiling_enabled: false,
4866
profiling_experimental_features_enabled: false,
4967
profiling_endpoint_collection_enabled: false,
@@ -73,6 +91,7 @@ impl SystemSettings {
7391
let trace_agent_url = trace_agent_url();
7492
let uri = detect_uri_from_config(trace_agent_url, agent_host, trace_agent_port);
7593
Self {
94+
state: SystemSettingsState::ConfigAware,
7695
profiling_enabled: profiling_enabled(),
7796
profiling_experimental_features_enabled: profiling_experimental_features_enabled(),
7897
profiling_endpoint_collection_enabled: profiling_endpoint_collection_enabled(),
@@ -130,20 +149,47 @@ impl SystemSettings {
130149
system_settings.profiling_allocation_enabled = false;
131150
}
132151

152+
SystemSettings::log_state(
153+
(*ptr::addr_of!(SYSTEM_SETTINGS)).state,
154+
system_settings.state,
155+
"the first request was received",
156+
);
133157
ptr::addr_of_mut!(SYSTEM_SETTINGS).swap(&mut system_settings);
134158
}
135159

160+
fn log_state(from: SystemSettingsState, to: SystemSettingsState, reason: &str) {
161+
debug!("SystemSettings state transitioned from {from:?} to {to:?} because {reason}.");
162+
}
163+
136164
/// # Safety
137165
/// Must be called exactly once per shutdown in either mshutdown or
138166
/// shutdown, before zai config is shutdown.
139167
unsafe fn on_shutdown() {
140168
let system_settings = &mut *ptr::addr_of_mut!(SYSTEM_SETTINGS);
141-
*system_settings = SystemSettings::initial();
169+
let state = SystemSettingsState::ConfigUnaware;
170+
SystemSettings::log_state(
171+
system_settings.state,
172+
state,
173+
"a shutdown command was received",
174+
);
175+
*system_settings = SystemSettings {
176+
state,
177+
..SystemSettings::initial()
178+
};
142179
}
143180

144181
unsafe fn on_fork_in_child() {
145182
let system_settings = &mut *ptr::addr_of_mut!(SYSTEM_SETTINGS);
146-
*system_settings = SystemSettings::initial();
183+
let state = SystemSettingsState::Disabled;
184+
SystemSettings::log_state(
185+
system_settings.state,
186+
state,
187+
"the processed forked, and child processes are not profiled",
188+
);
189+
*system_settings = SystemSettings {
190+
state,
191+
..SystemSettings::initial()
192+
};
147193
}
148194
}
149195

@@ -423,6 +469,7 @@ impl ConfigId {
423469

424470
/// Keep these in sync with the INI defaults.
425471
static DEFAULT_SYSTEM_SETTINGS: SystemSettings = SystemSettings {
472+
state: SystemSettingsState::ConfigUnaware,
426473
profiling_enabled: true,
427474
profiling_experimental_features_enabled: false,
428475
profiling_endpoint_collection_enabled: true,

profiling/src/profiling/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,7 @@ mod tests {
15411541
use crate::{allocation::DEFAULT_ALLOCATION_SAMPLING_INTERVAL, config::AgentEndpoint};
15421542
use libdd_profiling::exporter::Uri;
15431543
use log::LevelFilter;
1544+
use crate::config::SystemSettingsState;
15441545

15451546
fn get_frames() -> Vec<ZendFrame> {
15461547
vec![ZendFrame {
@@ -1552,6 +1553,7 @@ mod tests {
15521553

15531554
pub fn get_system_settings() -> SystemSettings {
15541555
SystemSettings {
1556+
state: SystemSettingsState::ConfigAware,
15551557
profiling_enabled: true,
15561558
profiling_experimental_features_enabled: false,
15571559
profiling_endpoint_collection_enabled: false,

0 commit comments

Comments
 (0)