Skip to content

Commit 085d216

Browse files
committed
perf: reduce memory usage in profiling
Utilizes ThinStr in the ZendFrame to shrink memory.
1 parent 53a8da7 commit 085d216

File tree

9 files changed

+202
-284
lines changed

9 files changed

+202
-284
lines changed

Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

profiling/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ chrono = { version = "0.4" }
2121
crossbeam-channel = { version = "0.5", default-features = false, features = ["std"] }
2222
datadog-alloc = { git = "https://github.com/DataDog/libdatadog", tag = "v10.0.0" }
2323
datadog-profiling = { git = "https://github.com/DataDog/libdatadog", tag = "v10.0.0" }
24+
datadog-thin-str = { version = "1", path = "../thin-str", features = ["std"] }
2425
ddcommon = { git = "https://github.com/DataDog/libdatadog", tag = "v10.0.0" }
2526
env_logger = { version = "0.11", default-features = false }
2627
indexmap = { version = "2.2" }

profiling/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ mod logging;
66
mod pcntl;
77
pub mod profiling;
88
mod sapi;
9-
mod thin_str;
109
mod wall_time;
10+
mod well_known;
1111

1212
#[cfg(php_run_time_cache)]
1313
mod string_set;

profiling/src/profiling/mod.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::bindings::ddog_php_prof_get_active_fiber_test as ddog_php_prof_get_ac
1717

1818
use crate::bindings::{datadog_php_profiling_get_profiling_context, zend_execute_data};
1919
use crate::config::SystemSettings;
20+
use crate::well_known::WellKnown;
2021
use crate::{CLOCKS, TAGS};
2122
use chrono::Utc;
2223
use core::{ptr, str};
@@ -26,6 +27,7 @@ use datadog_profiling::api::{
2627
};
2728
use datadog_profiling::exporter::Tag;
2829
use datadog_profiling::internal::Profile as InternalProfile;
30+
use datadog_thin_str::ThinString;
2931
use log::{debug, info, trace, warn};
3032
use once_cell::sync::OnceCell;
3133
use std::borrow::Cow;
@@ -49,6 +51,8 @@ use datadog_profiling::api::UpscalingInfo;
4951

5052
#[cfg(feature = "exception_profiling")]
5153
use crate::exception::EXCEPTION_PROFILING_INTERVAL;
54+
#[cfg(feature = "timeline")]
55+
use crate::timeline::IncludeType;
5256

5357
const UPLOAD_PERIOD: Duration = Duration::from_secs(67);
5458

@@ -514,9 +518,6 @@ pub enum UploadMessage {
514518
Upload(Box<UploadRequest>),
515519
}
516520

517-
#[cfg(feature = "timeline")]
518-
const COW_EVAL: Cow<str> = Cow::Borrowed("[eval]");
519-
520521
const DDPROF_TIME: &str = "ddprof_time";
521522
const DDPROF_UPLOAD: &str = "ddprof_upload";
522523

@@ -907,14 +908,14 @@ impl Profiler {
907908
}];
908909

909910
#[cfg(feature = "timeline")]
910-
pub fn collect_compile_string(&self, now: i64, duration: i64, filename: String, line: u32) {
911+
pub fn collect_compile_string(&self, now: i64, duration: i64, filename: ThinString, line: u32) {
911912
let mut labels = Profiler::common_labels(Self::TIMELINE_COMPILE_FILE_LABELS.len());
912913
labels.extend_from_slice(Self::TIMELINE_COMPILE_FILE_LABELS);
913914
let n_labels = labels.len();
914915

915916
match self.prepare_and_send_message(
916917
vec![ZendFrame {
917-
function: COW_EVAL,
918+
function: WellKnown::Eval.into(),
918919
file: Some(filename),
919920
line,
920921
}],
@@ -942,7 +943,7 @@ impl Profiler {
942943
now: i64,
943944
duration: i64,
944945
filename: String,
945-
include_type: &str,
946+
include_type: IncludeType,
946947
) {
947948
let mut labels = Profiler::common_labels(Self::TIMELINE_COMPILE_FILE_LABELS.len() + 1);
948949
labels.extend_from_slice(Self::TIMELINE_COMPILE_FILE_LABELS);
@@ -955,7 +956,11 @@ impl Profiler {
955956

956957
match self.prepare_and_send_message(
957958
vec![ZendFrame {
958-
function: format!("[{include_type}]").into(),
959+
function: match include_type {
960+
IncludeType::Include => WellKnown::Include.into(),
961+
IncludeType::Require => WellKnown::Require.into(),
962+
IncludeType::Unknown => WellKnown::UnknownIncludeType.into(),
963+
},
959964
file: None,
960965
line: 0,
961966
}],
@@ -984,7 +989,7 @@ impl Profiler {
984989

985990
labels.push(Label {
986991
key: "event",
987-
value: LabelValue::Str(std::borrow::Cow::Borrowed(event)),
992+
value: LabelValue::Str(Cow::Borrowed(event)),
988993
});
989994

990995
let n_labels = labels.len();
@@ -1013,7 +1018,7 @@ impl Profiler {
10131018

10141019
/// This function can be called to collect any fatal errors
10151020
#[cfg(feature = "timeline")]
1016-
pub fn collect_fatal(&self, now: i64, file: String, line: u32, message: String) {
1021+
pub fn collect_fatal(&self, now: i64, file: ThinString, line: u32, message: String) {
10171022
let mut labels = Profiler::common_labels(2);
10181023

10191024
labels.push(Label {
@@ -1029,7 +1034,7 @@ impl Profiler {
10291034

10301035
match self.prepare_and_send_message(
10311036
vec![ZendFrame {
1032-
function: "[fatal]".into(),
1037+
function: WellKnown::Fatal.into(),
10331038
file: Some(file),
10341039
line,
10351040
}],
@@ -1056,7 +1061,7 @@ impl Profiler {
10561061
pub(crate) fn collect_opcache_restart(
10571062
&self,
10581063
now: i64,
1059-
file: String,
1064+
file: ThinString,
10601065
line: u32,
10611066
reason: &'static str,
10621067
) {
@@ -1109,7 +1114,7 @@ impl Profiler {
11091114

11101115
match self.prepare_and_send_message(
11111116
vec![ZendFrame {
1112-
function: "[idle]".into(),
1117+
function: WellKnown::Idle.into(),
11131118
file: None,
11141119
line: 0,
11151120
}],
@@ -1165,7 +1170,7 @@ impl Profiler {
11651170

11661171
match self.prepare_and_send_message(
11671172
vec![ZendFrame {
1168-
function: "[gc]".into(),
1173+
function: WellKnown::Gc.into(),
11691174
file: None,
11701175
line: 0,
11711176
}],
@@ -1233,7 +1238,7 @@ impl Profiler {
12331238
if let Some(functionname) = extract_function_name(func) {
12341239
labels.push(Label {
12351240
key: "fiber",
1236-
value: LabelValue::Str(functionname.into()),
1241+
value: LabelValue::Str(Cow::from(functionname)),
12371242
});
12381243
}
12391244
}
@@ -1287,12 +1292,16 @@ mod tests {
12871292
use super::*;
12881293
use crate::config::AgentEndpoint;
12891294
use datadog_profiling::exporter::Uri;
1295+
use datadog_thin_str::ConstStorage;
12901296
use log::LevelFilter;
12911297

12921298
fn get_frames() -> Vec<ZendFrame> {
1299+
static FOOBAR: ConstStorage<8> = ConstStorage::from_str("foobar()");
1300+
static FOOBAR_PHP: ConstStorage<10> = ConstStorage::from_str("foobar.php");
1301+
12931302
vec![ZendFrame {
1294-
function: "foobar()".into(),
1295-
file: Some("foobar.php".into()),
1303+
function: ThinString::from(&FOOBAR),
1304+
file: Some(ThinString::from(&FOOBAR_PHP)),
12961305
line: 42,
12971306
}]
12981307
}

0 commit comments

Comments
 (0)