Skip to content

Commit

Permalink
feat(profiling) PHP ZTS support for CPU- and walltime profiling (#2470)
Browse files Browse the repository at this point in the history
Co-authored-by: Levi Morrison <[email protected]>
  • Loading branch information
realFlowControl and morrisonlevi authored Feb 14, 2024
1 parent 8ed91bf commit d8a8624
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
38 changes: 36 additions & 2 deletions .circleci/continue_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ commands:
type: string
steps:
- run:
name: Build Profiler
name: Build Profiler NTS
command: |
if [ -d '/opt/rh/devtoolset-7' ] ; then
set +eo pipefail
Expand All @@ -545,6 +545,24 @@ commands:
cargo build --release
cd -
cp -v "${CARGO_TARGET_DIR}/release/libdatadog_php_profiling.so" "${prefix}/datadog-profiling.so"
- run:
name: Build Profiler ZTS
command: |
if [ -d '/opt/rh/devtoolset-7' ] ; then
set +eo pipefail
source scl_source enable devtoolset-7
set -eo pipefail
fi
set -u
prefix="<< parameters.prefix >>"
mkdir -vp "${prefix}"
command -v switch-php && switch-php "${PHP_VERSION}-zts"
cd profiling
echo "${CARGO_TARGET_DIR}"
touch build.rs #make sure `build.rs` gets executed after `switch-php` call
cargo build --release
cd -
cp -v "${CARGO_TARGET_DIR}/release/libdatadog_php_profiling.so" "${prefix}/datadog-profiling-zts.so"
executors:
with_agent:
Expand Down Expand Up @@ -2749,7 +2767,7 @@ jobs:
cd profiling
cargo test --release --all-features
- run:
name: phpt tests
name: phpt tests NTS
command: |
set -u
command -v switch-php && switch-php "${PHP_VERSION}"
Expand All @@ -2763,6 +2781,22 @@ jobs:
cp -v "${run_tests_php}" .
export TEST_PHP_EXECUTABLE=$(which php)
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"
- run:
name: phpt tests ZTS
command: |
set -u
command -v switch-php && switch-php "${PHP_VERSION}-zts"
set -e
libdir="/tmp/datadog-profiling"
cd profiling
touch build.rs #make sure `build.rs` gets executed after `switch-php` call
cargo build --release --all-features
cd tests
# don't anticipate there being more than one
run_tests_php=$(find $(php-config --prefix) -name run-tests.php)
cp -v "${run_tests_php}" .
export TEST_PHP_EXECUTABLE=$(which php)
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"
compile_extension_centos:
working_directory: ~/datadog
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/prof_correctness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
done
- name: Check profiler correctness for allocations
if: matrix.phpts != 'zts'
uses: Datadog/prof-correctness/analyze@main
with:
expected_json: profiling/tests/correctness/allocations.json
Expand Down
20 changes: 16 additions & 4 deletions profiling/src/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ static mut PREV_CUSTOM_MM_FREE: Option<zend::VmMmCustomFreeFn> = None;
/// The heap installed in ZendMM at the time we install our custom handlers
static mut HEAP: Option<*mut zend::_zend_mm_heap> = None;

pub fn allocation_profiling_minit() {
unsafe { zend::ddog_php_opcache_init_handle() };
}

/// take a sample every 4096 KiB
pub const ALLOCATION_PROFILING_INTERVAL: f64 = 1024.0 * 4096.0;

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

pub fn allocation_profiling_minit() {
#[cfg(php_zts)]
return;

unsafe { zend::ddog_php_opcache_init_handle() };
}

pub fn first_rinit_should_disable_due_to_jit() -> bool {
if NEEDS_RUN_TIME_CHECK_FOR_ENABLED_JIT
&& allocation_profiling_needs_disabled_for_jit(unsafe { crate::PHP_VERSION_ID })
Expand All @@ -122,6 +125,9 @@ pub fn first_rinit_should_disable_due_to_jit() -> bool {
}

pub fn allocation_profiling_rinit() {
#[cfg(php_zts)]
return;

let allocation_profiling: bool = REQUEST_LOCALS.with(|cell| {
match cell.try_borrow() {
Ok(locals) => {
Expand Down Expand Up @@ -192,6 +198,9 @@ pub fn allocation_profiling_rinit() {
}

pub fn allocation_profiling_rshutdown() {
#[cfg(php_zts)]
return;

let allocation_profiling = REQUEST_LOCALS.with(|cell| {
cell.try_borrow()
.map(|locals| locals.system_settings().profiling_allocation_enabled)
Expand Down Expand Up @@ -258,6 +267,9 @@ pub fn allocation_profiling_rshutdown() {
}

pub fn allocation_profiling_startup() {
#[cfg(php_zts)]
return;

unsafe {
let handle = datadog_php_zif_handler::new(
CStr::from_bytes_with_nul_unchecked(b"gc_mem_caches\0"),
Expand Down
10 changes: 8 additions & 2 deletions profiling/src/profiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,8 @@ impl Profiler {
&self,
frames: Vec<ZendFrame>,
samples: SampleValues,
#[cfg(php_has_fibers)] mut labels: Vec<Label>,
#[cfg(not(php_has_fibers))] labels: Vec<Label>,
#[cfg(any(php_has_fibers, php_zts))] mut labels: Vec<Label>,
#[cfg(not(any(php_has_fibers, php_zts)))] labels: Vec<Label>,
timestamp: i64,
) -> SampleMessage {
// If profiling is disabled, these will naturally return empty Vec.
Expand All @@ -1028,6 +1028,12 @@ impl Profiler {
}
}

#[cfg(php_zts)]
labels.push(Label {
key: "thread id",
value: LabelValue::Num(unsafe { libc::pthread_self() as i64 }, "id".into()),
});

let tags = TAGS.with(|cell| Arc::clone(&cell.borrow()));

SampleMessage {
Expand Down
5 changes: 5 additions & 0 deletions profiling/tests/phpt/jit_01.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ if (!extension_loaded('datadog-profiling'))
$arch = php_uname('m');
if (PHP_VERSION_ID < 80100 && in_array($arch, ['aarch64', 'arm64']))
echo "skip: JIT not available on aarch64 on PHP 8.0", PHP_EOL;

// TODO: remove once ZTS support for allocation profiling is done
if (PHP_ZTS) {
echo "skip: not support on ZTS builds at the moment";
}
?>
--INI--
datadog.profiling.enabled=yes
Expand Down
6 changes: 6 additions & 0 deletions profiling/tests/phpt/jit_02.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ if (!extension_loaded('datadog-profiling'))
$arch = php_uname('m');
if (PHP_VERSION_ID < 80100 && in_array($arch, ['aarch64', 'arm64']))
echo "skip: JIT not available on aarch64 on PHP 8.0", PHP_EOL;

// TODO: remove once ZTS support for allocation profiling is done
if (PHP_ZTS) {
echo "skip: not support on ZTS builds at the moment";
}

?>
--INI--
datadog.profiling.enabled=yes
Expand Down
5 changes: 5 additions & 0 deletions profiling/tests/phpt/phpinfo_02.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ if (!extension_loaded('datadog-profiling'))
$arch = php_uname('m');
if (PHP_VERSION_ID < 80100 && in_array($arch, ['aarch64', 'arm64']))
echo "skip: JIT not available on aarch64 on PHP 8.0", PHP_EOL;

// TODO: remove once ZTS support for allocation profiling is done
if (PHP_ZTS) {
echo "skip: not support on ZTS builds at the moment";
}
?>
--ENV--
DD_PROFILING_ENABLED=yes
Expand Down
6 changes: 6 additions & 0 deletions tooling/bin/generate-final-artifact.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ for architecture in "${architectures[@]}"; do
cp -v \
$tmp_folder_profiling/datadog-profiling/$architecture-unknown-linux-gnu/lib/php/$version/datadog-profiling.so \
$tmp_folder_final/$architecture-linux-gnu/dd-library-php/profiling/ext/$version/datadog-profiling.so
cp -v \
$tmp_folder_profiling/datadog-profiling/$architecture-unknown-linux-gnu/lib/php/$version/datadog-profiling-zts.so \
$tmp_folder_final/$architecture-linux-gnu/dd-library-php/profiling/ext/$version/datadog-profiling-zts.so

cp -v \
$tmp_folder_profiling/datadog-profiling/$architecture-alpine-linux-musl/lib/php/$version/datadog-profiling.so \
$tmp_folder_final/$architecture-linux-musl/dd-library-php/profiling/ext/$version/datadog-profiling.so
cp -v \
$tmp_folder_profiling/datadog-profiling/$architecture-alpine-linux-musl/lib/php/$version/datadog-profiling-zts.so \
$tmp_folder_final/$architecture-linux-musl/dd-library-php/profiling/ext/$version/datadog-profiling-zts.so
done

# Licenses
Expand Down

0 comments on commit d8a8624

Please sign in to comment.