Skip to content

Commit 6e42e4c

Browse files
fix(prof): follow PHP globals model in allocation profiler (#3175)
Co-authored-by: Levi Morrison <[email protected]>
1 parent eae6668 commit 6e42e4c

File tree

12 files changed

+582
-423
lines changed

12 files changed

+582
-423
lines changed

Cargo.lock

Lines changed: 10 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,7 @@ trigger_time_sample = []
7676
bindgen = { version = "0.69.4" }
7777
cc = { version = "1.0" }
7878

79+
# We can remove this when we're using Rust 1.80+
80+
rustc_version = "0.4"
81+
7982
# profiling release options in root Cargo.toml

profiling/build.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bindgen::callbacks::IntKind;
2+
use rustc_version::version_meta;
23
use std::collections::HashSet;
34
use std::path::Path;
45
use std::path::PathBuf;
@@ -13,7 +14,7 @@ fn main() {
1314

1415
if !php_config_includes_output.status.success() {
1516
match String::from_utf8(php_config_includes_output.stderr) {
16-
Ok(stderr) => panic!("`php-config failed: {}", stderr),
17+
Ok(stderr) => panic!("`php-config failed: {stderr}"),
1718
Err(err) => panic!(
1819
"`php-config` failed, not utf8: {}",
1920
String::from_utf8_lossy(err.as_bytes())
@@ -26,7 +27,7 @@ fn main() {
2627
.expect("Failed to read VERSION file")
2728
.trim()
2829
.to_string();
29-
println!("cargo:rustc-env=PROFILER_VERSION={}", version);
30+
println!("cargo:rustc-env=PROFILER_VERSION={version}");
3031
println!("cargo:rerun-if-changed=../VERSION");
3132

3233
let php_config_includes = std::str::from_utf8(php_config_includes_output.stdout.as_slice())
@@ -65,7 +66,7 @@ fn php_config_vernum() -> u64 {
6566

6667
if !output.status.success() {
6768
match String::from_utf8(output.stderr) {
68-
Ok(stderr) => panic!("`php-config --vernum` failed: {}", stderr),
69+
Ok(stderr) => panic!("`php-config --vernum` failed: {stderr}"),
6970
Err(err) => panic!(
7071
"`php-config --vernum` failed, not utf8: {}",
7172
String::from_utf8_lossy(err.as_bytes())
@@ -134,7 +135,7 @@ fn build_zend_php_ffis(
134135
];
135136

136137
for file in zai_c_files.iter().chain(ZAI_H_FILES.iter()) {
137-
println!("cargo:rerun-if-changed={}", *file);
138+
println!("cargo:rerun-if-changed={file}");
138139
}
139140

140141
let output = Command::new("php-config")
@@ -143,7 +144,10 @@ fn build_zend_php_ffis(
143144
.expect("Unable to run `php-config`. Is it in your PATH?");
144145

145146
let prefix = String::from_utf8(output.stdout).expect("only utf8 chars work");
146-
println!("cargo:rustc-link-search=native={}/lib", prefix.trim());
147+
println!(
148+
"cargo:rustc-link-search=native={prefix}/lib",
149+
prefix = prefix.trim()
150+
);
147151

148152
let files = ["src/php_ffi.c", "../ext/handlers_api.c"];
149153
let post_startup_cb = if post_startup_cb { "1" } else { "0" };
@@ -296,7 +300,18 @@ fn generate_bindings(php_config_includes: &str, fibers: bool, zend_error_observe
296300
.expect("bindings to be written successfully");
297301
}
298302

303+
// See https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html
304+
// We can remove this when we're on Rust 1.80+.
305+
fn has_check_cfg() -> bool {
306+
let meta = version_meta().unwrap();
307+
assert_eq!(1, meta.semver.major);
308+
meta.semver.minor >= 80
309+
}
310+
299311
fn cfg_post_startup_cb(vernum: u64) -> bool {
312+
if has_check_cfg() {
313+
println!("cargo::rustc-check-cfg=cfg(php_post_startup_cb)");
314+
}
300315
if vernum >= 70300 {
301316
println!("cargo:rustc-cfg=php_post_startup_cb");
302317
true
@@ -306,6 +321,9 @@ fn cfg_post_startup_cb(vernum: u64) -> bool {
306321
}
307322

308323
fn cfg_preload(vernum: u64) -> bool {
324+
if has_check_cfg() {
325+
println!("cargo::rustc-check-cfg=cfg(php_preload)");
326+
}
309327
if vernum >= 70400 {
310328
println!("cargo:rustc-cfg=php_preload");
311329
true
@@ -315,6 +333,9 @@ fn cfg_preload(vernum: u64) -> bool {
315333
}
316334

317335
fn cfg_run_time_cache(vernum: u64) -> bool {
336+
if has_check_cfg() {
337+
println!("cargo::rustc-check-cfg=cfg(php_run_time_cache)");
338+
}
318339
if vernum >= 80000 {
319340
println!("cargo:rustc-cfg=php_run_time_cache");
320341
true
@@ -328,6 +349,9 @@ fn cfg_trigger_time_sample() -> bool {
328349
}
329350

330351
fn cfg_zend_error_observer(vernum: u64) -> bool {
352+
if has_check_cfg() {
353+
println!("cargo::rustc-check-cfg=cfg(zend_error_observer, zend_error_observer_80)");
354+
}
331355
if vernum >= 80000 {
332356
println!("cargo:rustc-cfg=zend_error_observer");
333357
if vernum < 80100 {
@@ -340,6 +364,10 @@ fn cfg_zend_error_observer(vernum: u64) -> bool {
340364
}
341365

342366
fn cfg_php_major_version(vernum: u64) {
367+
if has_check_cfg() {
368+
println!("cargo::rustc-check-cfg=cfg(php7, php8)");
369+
}
370+
343371
let major_version = match vernum {
344372
70000..=79999 => 7,
345373
80000..=89999 => 8,
@@ -354,6 +382,9 @@ fn cfg_php_major_version(vernum: u64) {
354382
}
355383

356384
fn cfg_fibers(vernum: u64) -> bool {
385+
if has_check_cfg() {
386+
println!("cargo::rustc-check-cfg=cfg(php_has_fibers)");
387+
}
357388
if vernum >= 80100 {
358389
println!("cargo:rustc-cfg=php_has_fibers");
359390
true
@@ -363,6 +394,10 @@ fn cfg_fibers(vernum: u64) -> bool {
363394
}
364395

365396
fn cfg_php_feature_flags(vernum: u64) {
397+
if has_check_cfg() {
398+
println!("cargo::rustc-check-cfg=cfg(php_gc_status, php_zend_compile_string_has_position, php_gc_status_extended, php_frameless, php_opcache_restart_hook, php_zend_mm_set_custom_handlers_ex)");
399+
}
400+
366401
if vernum >= 70300 {
367402
println!("cargo:rustc-cfg=php_gc_status");
368403
}
@@ -380,6 +415,10 @@ fn cfg_php_feature_flags(vernum: u64) {
380415
}
381416

382417
fn cfg_zts() {
418+
if has_check_cfg() {
419+
println!("cargo::rustc-check-cfg=cfg(php_zts)");
420+
}
421+
383422
let output = Command::new("php")
384423
.arg("-n")
385424
.arg("-r")
@@ -389,8 +428,8 @@ fn cfg_zts() {
389428

390429
if !output.status.success() {
391430
match String::from_utf8(output.stderr) {
392-
Ok(stderr) => panic!("`php failed: {}", stderr),
393-
Err(err) => panic!("`php` failed, not utf8: {}", err),
431+
Ok(stderr) => panic!("`php failed: {stderr}"),
432+
Err(err) => panic!("`php` failed, not utf8: {err}"),
394433
}
395434
}
396435

0 commit comments

Comments
 (0)