Skip to content

Commit 268dcfb

Browse files
chore(profiling): clean up code and update Rust dependencies (#4038)
1 parent d004542 commit 268dcfb

14 files changed

Lines changed: 412 additions & 550 deletions

File tree

Cargo.lock

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

profiling/Cargo.toml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ bench = false # disables cargo build in libtest bench
1515
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1616

1717
[dependencies]
18-
# Avoid ahash 0.8.7 we run into https://github.com/tkaitchuck/aHash/issues/195.
19-
ahash = { version = "0.8.11" }
2018
anyhow = { version = "1.0" }
21-
cfg-if = { version = "1.0" }
2219
cpu-time = { version = "1.0" }
2320
chrono = { version = "0.4" }
2421
crossbeam-channel = { version = "0.5", default-features = false, features = ["std"] }
25-
dashmap = { version = "6.1" }
26-
dynasmrt = "2.0"
22+
dashmap = { version = "6.2.1" }
23+
dynasmrt = "5.0.0"
2724
http = { version = "1.4" }
2825
libdd-alloc = { path = "../libdatadog/libdd-alloc" }
2926
libdd-profiling = { path = "../libdatadog/libdd-profiling" }
@@ -34,15 +31,15 @@ libc = "0.2"
3431
# TRACE set to max to support runtime configuration.
3532
log = { version = "0.4", features = ["max_level_trace", "release_max_level_trace"]}
3633
serde_json = {version = "1.0"}
37-
rand = { version = "0.8.5" }
38-
rand_distr = { version = "0.4.3" }
39-
rustc-hash = "1.1.0"
34+
rand = { version = "0.9.4" }
35+
rand_distr = { version = "0.5.1" }
36+
rustc-hash = "2.1.3"
4037
thiserror = "2"
4138
tracing = { version = "0.1", optional = true }
4239
uuid = { version = "1.0", features = ["v4"] }
4340

4441
[target.'cfg(target_vendor = "apple")'.dependencies]
45-
mach2 = "0.5" # version 0.6 requires edition="2024"
42+
mach2 = "0.6.0"
4643

4744
[dependencies.tracing-subscriber]
4845
version = "0.3"
@@ -52,7 +49,6 @@ default-features = false
5249
features = ["env-filter", "fmt", "smallvec", "std"]
5350

5451
[dev-dependencies]
55-
allocator-api2 = { version = "0.2", default-features = false, features = ["alloc"] }
5652
criterion = { version = "0.5.1" }
5753
datadog-php-profiling = { path = ".", features = ["test"] }
5854
static_assertions = "1"
@@ -78,7 +74,7 @@ tracing-subscriber = ["tracing", "dep:tracing-subscriber"]
7874
trigger_time_sample = []
7975

8076
[build-dependencies]
81-
bindgen = { version = "0.69.4" }
77+
bindgen = { version = "0.72.1" }
8278
cc = { version = "1.0" }
8379

8480
# profiling release options in root Cargo.toml

profiling/benches/fakeapp/vendor/FakeApp/FakeRoute/src/MatchedRoute.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

profiling/build.rs

Lines changed: 22 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ fn main() {
5555

5656
cfg_php_major_version(vernum);
5757
cfg_php_feature_flags(vernum);
58-
cfg_zts();
59-
cfg_php_debug();
58+
cfg_php_build();
6059
apple_linker_flags();
6160
}
6261

@@ -407,8 +406,9 @@ fn cfg_php_feature_flags(vernum: u64) {
407406
}
408407
}
409408

410-
fn cfg_zts() {
409+
fn cfg_php_build() {
411410
println!("cargo::rustc-check-cfg=cfg(php_zts)");
411+
println!("cargo::rustc-check-cfg=cfg(php_debug)");
412412

413413
let output = Command::new("php-config")
414414
.arg("--include-dir")
@@ -426,9 +426,8 @@ fn cfg_zts() {
426426
.expect("`php-config`'s stdout to be valid utf8")
427427
.trim();
428428

429-
// Create a temporary C file to probe ZTS
430429
let out_dir = env::var("OUT_DIR").unwrap();
431-
let probe_path = Path::new(&out_dir).join("zts_probe.c");
430+
let probe_path = Path::new(&out_dir).join("php_build_probe.c");
432431
fs::write(
433432
&probe_path,
434433
r#"
@@ -440,70 +439,6 @@ int main() {
440439
#else
441440
printf("0");
442441
#endif
443-
return 0;
444-
}
445-
"#,
446-
)
447-
.expect("Failed to write ZTS probe file");
448-
449-
// Get the C compiler from cc crate
450-
let compiler = cc::Build::new().get_compiler();
451-
452-
// Compile the probe to an executable
453-
let probe_exe = Path::new(&out_dir).join("zts_probe");
454-
let compile_status = Command::new(compiler.path())
455-
.arg(format!("-I{}", include_dir))
456-
.arg(&probe_path)
457-
.arg("-o")
458-
.arg(&probe_exe)
459-
.status()
460-
.expect("Failed to compile ZTS probe");
461-
462-
if !compile_status.success() {
463-
panic!("Failed to compile ZTS probe");
464-
}
465-
466-
// Run the probe
467-
let probe_output = Command::new(&probe_exe)
468-
.output()
469-
.expect("Failed to run ZTS probe");
470-
471-
let zts_value = std::str::from_utf8(&probe_output.stdout)
472-
.expect("ZTS probe output not UTF-8")
473-
.trim();
474-
475-
if zts_value == "1" {
476-
println!("cargo:rustc-cfg=php_zts");
477-
}
478-
}
479-
480-
fn cfg_php_debug() {
481-
println!("cargo::rustc-check-cfg=cfg(php_debug)");
482-
483-
let output = Command::new("php-config")
484-
.arg("--include-dir")
485-
.output()
486-
.expect("Unable to run `php-config`. Is it in your PATH?");
487-
488-
if !output.status.success() {
489-
match String::from_utf8(output.stderr) {
490-
Ok(stderr) => panic!("`php-config --include-dir` failed: {stderr}"),
491-
Err(err) => panic!("`php-config --include-dir` failed, not utf8: {err}"),
492-
}
493-
}
494-
495-
let include_dir = std::str::from_utf8(output.stdout.as_slice())
496-
.expect("`php-config`'s stdout to be valid utf8")
497-
.trim();
498-
499-
let out_dir = env::var("OUT_DIR").unwrap();
500-
let probe_path = Path::new(&out_dir).join("php_debug_probe.c");
501-
fs::write(
502-
&probe_path,
503-
r#"
504-
#include "main/php_config.h"
505-
#include <stdio.h>
506-
int main() {
507442
#if ZEND_DEBUG
508443
printf("1");
509444
#else
@@ -513,31 +448,41 @@ int main() {
513448
}
514449
"#,
515450
)
516-
.expect("Failed to write PHP debug probe file");
451+
.expect("Failed to write PHP build probe file");
517452

518453
let compiler = cc::Build::new().get_compiler();
519-
let probe_exe = Path::new(&out_dir).join("php_debug_probe");
454+
let probe_exe = Path::new(&out_dir).join("php_build_probe");
520455
let compile_status = Command::new(compiler.path())
521456
.arg(format!("-I{}", include_dir))
522457
.arg(&probe_path)
523458
.arg("-o")
524459
.arg(&probe_exe)
525460
.status()
526-
.expect("Failed to compile PHP debug probe");
461+
.expect("Failed to compile PHP build probe");
527462

528463
if !compile_status.success() {
529-
panic!("Failed to compile PHP debug probe");
464+
panic!("Failed to compile PHP build probe");
530465
}
531466

532467
let probe_output = Command::new(&probe_exe)
533468
.output()
534-
.expect("Failed to run PHP debug probe");
469+
.expect("Failed to run PHP build probe");
535470

536-
let debug_value = std::str::from_utf8(&probe_output.stdout)
537-
.expect("PHP debug probe output not UTF-8")
471+
let probe_value = std::str::from_utf8(&probe_output.stdout)
472+
.expect("PHP build probe output not UTF-8")
538473
.trim();
474+
let (zts, debug) = match probe_value {
475+
"00" => (false, false),
476+
"01" => (false, true),
477+
"10" => (true, false),
478+
"11" => (true, true),
479+
_ => panic!("Unexpected PHP build probe output: {probe_value:?}"),
480+
};
539481

540-
if debug_value == "1" {
482+
if zts {
483+
println!("cargo:rustc-cfg=php_zts");
484+
}
485+
if debug {
541486
println!("cargo:rustc-cfg=php_debug");
542487
}
543488
}

profiling/src/allocation/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ impl AllocationProfilingStats {
108108
next_sample: 0,
109109
poisson,
110110
#[cfg(php_zts)]
111-
rng: rand::thread_rng(),
111+
rng: rand::rng(),
112112
#[cfg(not(php_zts))]
113-
rng: StdRng::from_entropy(),
113+
rng: StdRng::from_os_rng(),
114114
};
115115
stats.next_sampling_interval();
116116
stats

profiling/src/config.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,13 +1324,10 @@ pub(crate) fn minit(module_number: libc::c_int) {
13241324
let mut system_settings = SystemSettings::new();
13251325

13261326
// Initialize logging before allocation's rinit, as it logs.
1327-
cfg_if::cfg_if! {
1328-
if #[cfg(debug_assertions)] {
1329-
log::set_max_level(system_settings.profiling_log_level);
1330-
} else {
1331-
crate::logging::log_init(system_settings.profiling_log_level);
1332-
}
1333-
}
1327+
#[cfg(debug_assertions)]
1328+
log::set_max_level(system_settings.profiling_log_level);
1329+
#[cfg(not(debug_assertions))]
1330+
crate::logging::log_init(system_settings.profiling_log_level);
13341331

13351332
SystemSettings::log_state(
13361333
(*ptr::addr_of!(SYSTEM_SETTINGS)).state,

profiling/src/exception.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl ExceptionProfilingStats {
4444
let mut stats = ExceptionProfilingStats {
4545
next_sample: 0,
4646
poisson,
47-
rng: rand::thread_rng(),
47+
rng: rand::rng(),
4848
};
4949
stats.next_sampling_interval();
5050
stats

profiling/src/io/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ pub mod got_macho;
55

66
use crate::profiling::Profiler;
77
use crate::{zend, RefCellExt, REQUEST_LOCALS};
8-
use ahash::{HashMap, HashMapExt};
98
use libc::{c_int, c_void, fstat, stat, S_IFMT, S_IFSOCK};
109
use rand::rngs::ThreadRng;
1110
use rand_distr::{Distribution, Poisson};
11+
use rustc_hash::FxHashMap;
1212
use std::cell::RefCell;
1313
use std::mem::MaybeUninit;
1414
use std::os::unix::io::RawFd;
@@ -408,19 +408,19 @@ static mut ORIG_CLOSE: unsafe extern "C" fn(i32) -> i32 = libc::close;
408408
unsafe extern "C" fn observed_close(fd: i32) -> i32 {
409409
let ret = ORIG_CLOSE(fd);
410410
let _errno_backup = ErrnoBackup::new();
411-
let cache = FD_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
411+
let cache = FD_CACHE.get_or_init(|| Mutex::new(FxHashMap::default()));
412412
let mut cache = cache.lock().unwrap();
413413
cache.remove(&fd);
414414
ret
415415
}
416416

417417
/// "Is socket"-cache for `read()`/`write()` calls
418-
static FD_CACHE: OnceLock<Mutex<HashMap<RawFd, bool>>> = OnceLock::new();
418+
static FD_CACHE: OnceLock<Mutex<FxHashMap<RawFd, bool>>> = OnceLock::new();
419419

420420
/// Returns `true` if the given `fd` is a socket. It could also be a regular file, directory, pipe,
421421
/// character or block device, in which case we declare this as file I/O and not socket I/O.
422422
fn fd_is_socket(fd: RawFd) -> bool {
423-
let cache = FD_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
423+
let cache = FD_CACHE.get_or_init(|| Mutex::new(FxHashMap::default()));
424424
if let Some(&is_socket) = cache.lock().unwrap().get(&fd) {
425425
return is_socket;
426426
}
@@ -574,7 +574,7 @@ impl IOProfilingStats {
574574
let mut stats = IOProfilingStats {
575575
next_sample: 0,
576576
poisson,
577-
rng: rand::thread_rng(),
577+
rng: rand::rng(),
578578
};
579579
stats.next_sampling_interval();
580580
stats

profiling/src/lib.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -920,27 +920,24 @@ unsafe extern "C" fn minfo(module_ptr: *mut zend::ModuleEntry) {
920920
);
921921

922922

923-
cfg_if::cfg_if! {
924-
if #[cfg(feature = "io_profiling")] {
925-
zend::php_info_print_table_row(
926-
2,
927-
c"I/O Profiling Enabled".as_ptr(),
928-
if system_settings.profiling_io_enabled {
929-
yes
930-
} else if system_settings.profiling_enabled {
931-
no
932-
} else {
933-
no_all
934-
},
935-
);
923+
#[cfg(feature = "io_profiling")]
924+
zend::php_info_print_table_row(
925+
2,
926+
c"I/O Profiling Enabled".as_ptr(),
927+
if system_settings.profiling_io_enabled {
928+
yes
929+
} else if system_settings.profiling_enabled {
930+
no
936931
} else {
937-
zend::php_info_print_table_row(
938-
2,
939-
c"I/O Profiling Enabled".as_ptr(),
940-
c"Not available. The profiler was built without I/O profiling support.".as_ptr()
941-
);
942-
}
943-
}
932+
no_all
933+
},
934+
);
935+
#[cfg(not(feature = "io_profiling"))]
936+
zend::php_info_print_table_row(
937+
2,
938+
c"I/O Profiling Enabled".as_ptr(),
939+
c"Not available. The profiler was built without I/O profiling support.".as_ptr(),
940+
);
944941

945942
zend::php_info_print_table_row(
946943
2,

0 commit comments

Comments
 (0)