Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
make sample interval and sample depth configurable options
Browse files Browse the repository at this point in the history
see humanmade#2
and phacility#80

Signed-off-by: Slach <[email protected]>
  • Loading branch information
Slach committed Apr 29, 2018
1 parent 05871ae commit d9ee744
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 4 additions & 2 deletions extension/php_xhprof.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
22 changes: 18 additions & 4 deletions extension/xhprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}


Expand Down

0 comments on commit d9ee744

Please sign in to comment.