Skip to content

Commit

Permalink
refactor: only call cufa callback when needed (#769)
Browse files Browse the repository at this point in the history
Improve agent's performance by only calling `call_user_function_array`
callback when trying to instrument Drupal or WordPress hooks. Imported
from #729.

---------

Co-authored-by: Michal Nowacki <[email protected]>
Co-authored-by: bduranleau-nr <[email protected]>
  • Loading branch information
3 people authored Dec 4, 2023
1 parent 54ad95d commit 6389c69
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions agent/fw_drupal.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,16 @@ NR_PHP_WRAPPER(nr_drupal_wrap_module_invoke_all) {
NRPRG(drupal_module_invoke_all_hook)
= nr_strndup(Z_STRVAL_P(hook), Z_STRLEN_P(hook));
NRPRG(drupal_module_invoke_all_hook_len) = Z_STRLEN_P(hook);
NRPRG(check_cufa) = true;

NR_PHP_WRAPPER_CALL;

nr_free(NRPRG(drupal_module_invoke_all_hook));
NRPRG(drupal_module_invoke_all_hook) = prev_hook;
NRPRG(drupal_module_invoke_all_hook_len) = prev_hook_len;
if (NULL == NRPRG(drupal_module_invoke_all_hook)) {
NRPRG(check_cufa) = false;
}

leave:
nr_php_arg_release(&hook);
Expand Down
5 changes: 4 additions & 1 deletion agent/fw_drupal8.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ NR_PHP_WRAPPER(nr_drupal94_invoke_all_with) {
NRPRG(drupal_module_invoke_all_hook)
= nr_strndup(Z_STRVAL_P(hook), Z_STRLEN_P(hook));
NRPRG(drupal_module_invoke_all_hook_len) = Z_STRLEN_P(hook);

NRPRG(check_cufa) = true;
callback = nr_php_arg_get(2, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
/* This instrumentation will fail if callback has already been wrapped
* with a special instrumentation callback in a different context.
Expand All @@ -432,6 +432,9 @@ NR_PHP_WRAPPER(nr_drupal94_invoke_all_with) {
nr_free(NRPRG(drupal_module_invoke_all_hook));
NRPRG(drupal_module_invoke_all_hook) = prev_hook;
NRPRG(drupal_module_invoke_all_hook_len) = prev_hook_len;
if (NULL == NRPRG(drupal_module_invoke_all_hook)) {
NRPRG(check_cufa) = false;
}

leave:
nr_php_arg_release(&hook);
Expand Down
13 changes: 13 additions & 0 deletions agent/fw_wordpress.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,16 @@ NR_PHP_WRAPPER(nr_wordpress_exec_handle_tag) {
*/
char* old_tag = NRPRG(wordpress_tag);

NRPRG(check_cufa) = true;

NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag);
NR_PHP_WRAPPER_CALL;
NRPRG(wordpress_tag) = old_tag;
if (NULL == NRPRG(wordpress_tag)) {
NRPRG(check_cufa) = false;
}
} else {
NRPRG(check_cufa) = false;
NR_PHP_WRAPPER_CALL;
}

Expand Down Expand Up @@ -575,10 +581,17 @@ NR_PHP_WRAPPER(nr_wordpress_apply_filters) {
*/
char* old_tag = NRPRG(wordpress_tag);

NRPRG(check_cufa) = true;

NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag);

NR_PHP_WRAPPER_CALL;
NRPRG(wordpress_tag) = old_tag;
if (NULL == NRPRG(wordpress_tag)) {
NRPRG(check_cufa) = false;
}
} else {
NRPRG(check_cufa) = false;
NR_PHP_WRAPPER_CALL;
}

Expand Down
3 changes: 3 additions & 0 deletions agent/php_newrelic.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ int symfony1_in_dispatch; /* Whether we are currently within a
int symfony1_in_error404; /* Whether we are currently within a
sfError404Exception::printStackTrace() frame */

bool check_cufa; /* Whether we need to check cufa because we are
instrumenting hooks, or whether we can skip cufa */

char* wordpress_tag; /* The current WordPress tag */
nr_regex_t* wordpress_plugin_regex; /* Regex for plugin filenames */
nr_regex_t* wordpress_theme_regex; /* Regex for theme filenames */
Expand Down
2 changes: 2 additions & 0 deletions agent/php_rinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ PHP_RINIT_FUNCTION(newrelic) {
nr_php_extension_instrument_rescan(NRPRG(extensions) TSRMLS_CC);
}

NRPRG(check_cufa) = false;

NRPRG(mysql_last_conn) = NULL;
NRPRG(pgsql_last_conn) = NULL;
NRPRG(datastore_connections) = nr_hashmap_create(
Expand Down
8 changes: 8 additions & 0 deletions agent/php_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ static int nr_php_handle_cufa_fcall(zend_execute_data* execute_data) {
goto call_previous_and_return;
}

/*
* If we don't have instrumented hooks that require this, skip to the
* end.
*/
if (false == NRPRG(check_cufa)) {
goto call_previous_and_return;
}

/*
* Since we're in the middle of a function call, the Zend Engine is actually
* only partway through constructing the new function frame. As a result, it
Expand Down
2 changes: 2 additions & 0 deletions agent/tests/test_internal_instrument.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static void test_cufa_direct(TSRMLS_D) {

tlib_php_request_start();

NRPRG(check_cufa) = true;
define_cufa_function_f(TSRMLS_C);
tlib_php_request_eval(
"function g() { return call_user_func_array('f', array()); }" TSRMLS_CC);
Expand All @@ -74,6 +75,7 @@ static void test_cufa_indirect(TSRMLS_D) {

tlib_php_request_start();

NRPRG(check_cufa) = true;
define_cufa_function_f(TSRMLS_C);
tlib_php_request_eval(
"function g() { $cufa = 'call_user_func_array'; return $cufa('f', "
Expand Down

0 comments on commit 6389c69

Please sign in to comment.