Skip to content

Commit d8a8624

Browse files
feat(profiling) PHP ZTS support for CPU- and walltime profiling (#2470)
Co-authored-by: Levi Morrison <[email protected]>
1 parent 8ed91bf commit d8a8624

File tree

8 files changed

+83
-8
lines changed

8 files changed

+83
-8
lines changed

.circleci/continue_config.yml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ commands:
529529
type: string
530530
steps:
531531
- run:
532-
name: Build Profiler
532+
name: Build Profiler NTS
533533
command: |
534534
if [ -d '/opt/rh/devtoolset-7' ] ; then
535535
set +eo pipefail
@@ -545,6 +545,24 @@ commands:
545545
cargo build --release
546546
cd -
547547
cp -v "${CARGO_TARGET_DIR}/release/libdatadog_php_profiling.so" "${prefix}/datadog-profiling.so"
548+
- run:
549+
name: Build Profiler ZTS
550+
command: |
551+
if [ -d '/opt/rh/devtoolset-7' ] ; then
552+
set +eo pipefail
553+
source scl_source enable devtoolset-7
554+
set -eo pipefail
555+
fi
556+
set -u
557+
prefix="<< parameters.prefix >>"
558+
mkdir -vp "${prefix}"
559+
command -v switch-php && switch-php "${PHP_VERSION}-zts"
560+
cd profiling
561+
echo "${CARGO_TARGET_DIR}"
562+
touch build.rs #make sure `build.rs` gets executed after `switch-php` call
563+
cargo build --release
564+
cd -
565+
cp -v "${CARGO_TARGET_DIR}/release/libdatadog_php_profiling.so" "${prefix}/datadog-profiling-zts.so"
548566
549567
executors:
550568
with_agent:
@@ -2749,7 +2767,7 @@ jobs:
27492767
cd profiling
27502768
cargo test --release --all-features
27512769
- run:
2752-
name: phpt tests
2770+
name: phpt tests NTS
27532771
command: |
27542772
set -u
27552773
command -v switch-php && switch-php "${PHP_VERSION}"
@@ -2763,6 +2781,22 @@ jobs:
27632781
cp -v "${run_tests_php}" .
27642782
export TEST_PHP_EXECUTABLE=$(which php)
27652783
php run-tests.php -d "extension=/mnt/ramdisk/cargo/release/libdatadog_php_profiling.so" --show-diff -g "FAIL,XFAIL,BORK,WARN,LEAK,XLEAK,SKIP" "phpt"
2784+
- run:
2785+
name: phpt tests ZTS
2786+
command: |
2787+
set -u
2788+
command -v switch-php && switch-php "${PHP_VERSION}-zts"
2789+
set -e
2790+
libdir="/tmp/datadog-profiling"
2791+
cd profiling
2792+
touch build.rs #make sure `build.rs` gets executed after `switch-php` call
2793+
cargo build --release --all-features
2794+
cd tests
2795+
# don't anticipate there being more than one
2796+
run_tests_php=$(find $(php-config --prefix) -name run-tests.php)
2797+
cp -v "${run_tests_php}" .
2798+
export TEST_PHP_EXECUTABLE=$(which php)
2799+
php run-tests.php -d "extension=/mnt/ramdisk/cargo/release/libdatadog_php_profiling.so" --show-diff -g "FAIL,XFAIL,BORK,WARN,LEAK,XLEAK,SKIP" "phpt"
27662800
27672801
compile_extension_centos:
27682802
working_directory: ~/datadog

.github/workflows/prof_correctness.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ jobs:
9393
done
9494
9595
- name: Check profiler correctness for allocations
96+
if: matrix.phpts != 'zts'
9697
uses: Datadog/prof-correctness/analyze@main
9798
with:
9899
expected_json: profiling/tests/correctness/allocations.json

profiling/src/allocation.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ static mut PREV_CUSTOM_MM_FREE: Option<zend::VmMmCustomFreeFn> = None;
3131
/// The heap installed in ZendMM at the time we install our custom handlers
3232
static mut HEAP: Option<*mut zend::_zend_mm_heap> = None;
3333

34-
pub fn allocation_profiling_minit() {
35-
unsafe { zend::ddog_php_opcache_init_handle() };
36-
}
37-
3834
/// take a sample every 4096 KiB
3935
pub const ALLOCATION_PROFILING_INTERVAL: f64 = 1024.0 * 4096.0;
4036

@@ -109,6 +105,13 @@ lazy_static! {
109105
static ref JIT_ENABLED: bool = unsafe { zend::ddog_php_jit_enabled() };
110106
}
111107

108+
pub fn allocation_profiling_minit() {
109+
#[cfg(php_zts)]
110+
return;
111+
112+
unsafe { zend::ddog_php_opcache_init_handle() };
113+
}
114+
112115
pub fn first_rinit_should_disable_due_to_jit() -> bool {
113116
if NEEDS_RUN_TIME_CHECK_FOR_ENABLED_JIT
114117
&& allocation_profiling_needs_disabled_for_jit(unsafe { crate::PHP_VERSION_ID })
@@ -122,6 +125,9 @@ pub fn first_rinit_should_disable_due_to_jit() -> bool {
122125
}
123126

124127
pub fn allocation_profiling_rinit() {
128+
#[cfg(php_zts)]
129+
return;
130+
125131
let allocation_profiling: bool = REQUEST_LOCALS.with(|cell| {
126132
match cell.try_borrow() {
127133
Ok(locals) => {
@@ -192,6 +198,9 @@ pub fn allocation_profiling_rinit() {
192198
}
193199

194200
pub fn allocation_profiling_rshutdown() {
201+
#[cfg(php_zts)]
202+
return;
203+
195204
let allocation_profiling = REQUEST_LOCALS.with(|cell| {
196205
cell.try_borrow()
197206
.map(|locals| locals.system_settings().profiling_allocation_enabled)
@@ -258,6 +267,9 @@ pub fn allocation_profiling_rshutdown() {
258267
}
259268

260269
pub fn allocation_profiling_startup() {
270+
#[cfg(php_zts)]
271+
return;
272+
261273
unsafe {
262274
let handle = datadog_php_zif_handler::new(
263275
CStr::from_bytes_with_nul_unchecked(b"gc_mem_caches\0"),

profiling/src/profiling/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,8 @@ impl Profiler {
10011001
&self,
10021002
frames: Vec<ZendFrame>,
10031003
samples: SampleValues,
1004-
#[cfg(php_has_fibers)] mut labels: Vec<Label>,
1005-
#[cfg(not(php_has_fibers))] labels: Vec<Label>,
1004+
#[cfg(any(php_has_fibers, php_zts))] mut labels: Vec<Label>,
1005+
#[cfg(not(any(php_has_fibers, php_zts)))] labels: Vec<Label>,
10061006
timestamp: i64,
10071007
) -> SampleMessage {
10081008
// If profiling is disabled, these will naturally return empty Vec.
@@ -1028,6 +1028,12 @@ impl Profiler {
10281028
}
10291029
}
10301030

1031+
#[cfg(php_zts)]
1032+
labels.push(Label {
1033+
key: "thread id",
1034+
value: LabelValue::Num(unsafe { libc::pthread_self() as i64 }, "id".into()),
1035+
});
1036+
10311037
let tags = TAGS.with(|cell| Arc::clone(&cell.borrow()));
10321038

10331039
SampleMessage {

profiling/tests/phpt/jit_01.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ if (!extension_loaded('datadog-profiling'))
1515
$arch = php_uname('m');
1616
if (PHP_VERSION_ID < 80100 && in_array($arch, ['aarch64', 'arm64']))
1717
echo "skip: JIT not available on aarch64 on PHP 8.0", PHP_EOL;
18+
19+
// TODO: remove once ZTS support for allocation profiling is done
20+
if (PHP_ZTS) {
21+
echo "skip: not support on ZTS builds at the moment";
22+
}
1823
?>
1924
--INI--
2025
datadog.profiling.enabled=yes

profiling/tests/phpt/jit_02.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ if (!extension_loaded('datadog-profiling'))
1414
$arch = php_uname('m');
1515
if (PHP_VERSION_ID < 80100 && in_array($arch, ['aarch64', 'arm64']))
1616
echo "skip: JIT not available on aarch64 on PHP 8.0", PHP_EOL;
17+
18+
// TODO: remove once ZTS support for allocation profiling is done
19+
if (PHP_ZTS) {
20+
echo "skip: not support on ZTS builds at the moment";
21+
}
22+
1723
?>
1824
--INI--
1925
datadog.profiling.enabled=yes

profiling/tests/phpt/phpinfo_02.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ if (!extension_loaded('datadog-profiling'))
1414
$arch = php_uname('m');
1515
if (PHP_VERSION_ID < 80100 && in_array($arch, ['aarch64', 'arm64']))
1616
echo "skip: JIT not available on aarch64 on PHP 8.0", PHP_EOL;
17+
18+
// TODO: remove once ZTS support for allocation profiling is done
19+
if (PHP_ZTS) {
20+
echo "skip: not support on ZTS builds at the moment";
21+
}
1722
?>
1823
--ENV--
1924
DD_PROFILING_ENABLED=yes

tooling/bin/generate-final-artifact.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,16 @@ for architecture in "${architectures[@]}"; do
7979
cp -v \
8080
$tmp_folder_profiling/datadog-profiling/$architecture-unknown-linux-gnu/lib/php/$version/datadog-profiling.so \
8181
$tmp_folder_final/$architecture-linux-gnu/dd-library-php/profiling/ext/$version/datadog-profiling.so
82+
cp -v \
83+
$tmp_folder_profiling/datadog-profiling/$architecture-unknown-linux-gnu/lib/php/$version/datadog-profiling-zts.so \
84+
$tmp_folder_final/$architecture-linux-gnu/dd-library-php/profiling/ext/$version/datadog-profiling-zts.so
8285

8386
cp -v \
8487
$tmp_folder_profiling/datadog-profiling/$architecture-alpine-linux-musl/lib/php/$version/datadog-profiling.so \
8588
$tmp_folder_final/$architecture-linux-musl/dd-library-php/profiling/ext/$version/datadog-profiling.so
89+
cp -v \
90+
$tmp_folder_profiling/datadog-profiling/$architecture-alpine-linux-musl/lib/php/$version/datadog-profiling-zts.so \
91+
$tmp_folder_final/$architecture-linux-musl/dd-library-php/profiling/ext/$version/datadog-profiling-zts.so
8692
done
8793

8894
# Licenses

0 commit comments

Comments
 (0)