From d9ee74489a7492fb3d416cf7353b9154db6d1d7a Mon Sep 17 00:00:00 2001 From: Slach Date: Sun, 29 Apr 2018 22:14:39 +0500 Subject: [PATCH] make sample interval and sample depth configurable options see https://github.com/humanmade/xhprof/pull/2 and https://github.com/phacility/xhprof/pull/80 Signed-off-by: Slach --- extension/php_xhprof.h | 6 ++++-- extension/xhprof.c | 22 ++++++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/extension/php_xhprof.h b/extension/php_xhprof.h index 1ae3a2a4..a04ce850 100644 --- a/extension/php_xhprof.h +++ b/extension/php_xhprof.h @@ -64,7 +64,8 @@ extern zend_module_entry xhprof_module_entry; #define XHPROF_FLAGS_MEMORY 0x0004 /* gather memory usage for funcs */ /* Constants for XHPROF_MODE_SAMPLED */ -#define XHPROF_SAMPLING_INTERVAL 100000 /* In microsecs */ +#define XHPROF_DEFAULT_SAMPLING_INTERVAL 100000 /* In microsecs */ +#define XHPROF_MINIMAL_SAMPLING_INTERVAL 100 /* In microsecs */ /* Constant for ignoring functions, transparent to hierarchical profile */ #define XHPROF_MAX_IGNORED_FUNCTIONS 256 @@ -269,8 +270,9 @@ ZEND_BEGIN_MODULE_GLOBALS(xhprof) struct timeval last_sample_time; uint64 last_sample_tsc; /* XHPROF_SAMPLING_INTERVAL in ticks */ + long sampling_interval; uint64 sampling_interval_tsc; - + int sampling_depth; /* XHProf flags */ uint32 xhprof_flags; diff --git a/extension/xhprof.c b/extension/xhprof.c index f59781ab..53e9891f 100644 --- a/extension/xhprof.c +++ b/extension/xhprof.c @@ -103,7 +103,18 @@ PHP_INI_BEGIN() * directory specified by this ini setting. */ PHP_INI_ENTRY("xhprof.output_dir", "", PHP_INI_ALL, NULL) +/* sampling_interval: + * Sampling interval to be used by the sampling profiler, in microseconds. + */ +#define STRINGIFY_(X) #X +#define STRINGIFY(X) STRINGIFY_(X) + +STD_PHP_INI_ENTRY("xhprof.sampling_interval", STRINGIFY(XHPROF_DEFAULT_SAMPLING_INTERVAL), PHP_INI_ALL, OnUpdateLong, sampling_interval, xhprof_global_t, xhprof_globals) +/* sampling_depth: + * Depth to trace call-chain by the sampling profiler + */ +STD_PHP_INI_ENTRY("xhprof.sampling_depth", STRINGIFY(INT_MAX), PHP_INI_ALL, OnUpdateLong, sampling_depth, xhprof_global_t, xhprof_globals) PHP_INI_END() /* Init module */ @@ -221,6 +232,9 @@ PHP_MINIT_FUNCTION(xhprof) for (i = 0; i < 256; i++) { XHPROF_G(func_hash_counters[i]) = 0; } + if (XHPROF_G(sampling_interval) < XHPROF_MINIMAL_SAMPLING_INTERVAL) { + XHPROF_G(sampling_interval) = XHPROF_MINIMAL_SAMPLING_INTERVAL; + } /* Replace zend_compile with our proxy */ _zend_compile_file = zend_compile_file; @@ -769,7 +783,7 @@ void hp_sample_stack(hp_entry_t **entries) snprintf(key, sizeof(key), "%d.%06d", XHPROF_G(last_sample_time).tv_sec, XHPROF_G(last_sample_time).tv_usec); /* Init stats in the global stats_count hashtable */ - symbol = hp_get_function_stack(*entries, INT_MAX); + symbol = hp_get_function_stack(*entries, XHPROF_G(sampling_depth)); add_assoc_string(&XHPROF_G(stats_count), key, symbol); @@ -801,7 +815,7 @@ void hp_sample_check(hp_entry_t **entries) XHPROF_G(last_sample_tsc) += XHPROF_G(sampling_interval_tsc); /* bump last_sample_time - HAS TO BE UPDATED BEFORE calling hp_sample_stack */ - incr_us_interval(&XHPROF_G(last_sample_time), XHPROF_SAMPLING_INTERVAL); + incr_us_interval(&XHPROF_G(last_sample_time), XHPROF_G(sample_interval)); /* sample the stack */ hp_sample_stack(entries); @@ -946,10 +960,10 @@ void hp_mode_sampled_init_cb() /* Find the microseconds that need to be truncated */ gettimeofday(&XHPROF_G(last_sample_time), 0); now = XHPROF_G(last_sample_time); - hp_trunc_time(&XHPROF_G(last_sample_time), XHPROF_SAMPLING_INTERVAL); + hp_trunc_time(&XHPROF_G(last_sample_time), XHPROF_G(sample_interval)); /* Convert sampling interval to ticks */ - XHPROF_G(sampling_interval_tsc) = XHPROF_SAMPLING_INTERVAL; + XHPROF_G(sampling_interval_tsc) = XHPROF_G(sample_interval); }