Skip to content

Commit 6389c69

Browse files
zsistlalavaroubduranleau-nr
authored
refactor: only call cufa callback when needed (#769)
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]>
1 parent 54ad95d commit 6389c69

File tree

7 files changed

+36
-1
lines changed

7 files changed

+36
-1
lines changed

agent/fw_drupal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,16 @@ NR_PHP_WRAPPER(nr_drupal_wrap_module_invoke_all) {
608608
NRPRG(drupal_module_invoke_all_hook)
609609
= nr_strndup(Z_STRVAL_P(hook), Z_STRLEN_P(hook));
610610
NRPRG(drupal_module_invoke_all_hook_len) = Z_STRLEN_P(hook);
611+
NRPRG(check_cufa) = true;
611612

612613
NR_PHP_WRAPPER_CALL;
613614

614615
nr_free(NRPRG(drupal_module_invoke_all_hook));
615616
NRPRG(drupal_module_invoke_all_hook) = prev_hook;
616617
NRPRG(drupal_module_invoke_all_hook_len) = prev_hook_len;
618+
if (NULL == NRPRG(drupal_module_invoke_all_hook)) {
619+
NRPRG(check_cufa) = false;
620+
}
617621

618622
leave:
619623
nr_php_arg_release(&hook);

agent/fw_drupal8.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ NR_PHP_WRAPPER(nr_drupal94_invoke_all_with) {
418418
NRPRG(drupal_module_invoke_all_hook)
419419
= nr_strndup(Z_STRVAL_P(hook), Z_STRLEN_P(hook));
420420
NRPRG(drupal_module_invoke_all_hook_len) = Z_STRLEN_P(hook);
421-
421+
NRPRG(check_cufa) = true;
422422
callback = nr_php_arg_get(2, NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
423423
/* This instrumentation will fail if callback has already been wrapped
424424
* with a special instrumentation callback in a different context.
@@ -432,6 +432,9 @@ NR_PHP_WRAPPER(nr_drupal94_invoke_all_with) {
432432
nr_free(NRPRG(drupal_module_invoke_all_hook));
433433
NRPRG(drupal_module_invoke_all_hook) = prev_hook;
434434
NRPRG(drupal_module_invoke_all_hook_len) = prev_hook_len;
435+
if (NULL == NRPRG(drupal_module_invoke_all_hook)) {
436+
NRPRG(check_cufa) = false;
437+
}
435438

436439
leave:
437440
nr_php_arg_release(&hook);

agent/fw_wordpress.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,16 @@ NR_PHP_WRAPPER(nr_wordpress_exec_handle_tag) {
494494
*/
495495
char* old_tag = NRPRG(wordpress_tag);
496496

497+
NRPRG(check_cufa) = true;
498+
497499
NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag);
498500
NR_PHP_WRAPPER_CALL;
499501
NRPRG(wordpress_tag) = old_tag;
502+
if (NULL == NRPRG(wordpress_tag)) {
503+
NRPRG(check_cufa) = false;
504+
}
500505
} else {
506+
NRPRG(check_cufa) = false;
501507
NR_PHP_WRAPPER_CALL;
502508
}
503509

@@ -575,10 +581,17 @@ NR_PHP_WRAPPER(nr_wordpress_apply_filters) {
575581
*/
576582
char* old_tag = NRPRG(wordpress_tag);
577583

584+
NRPRG(check_cufa) = true;
585+
578586
NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag);
587+
579588
NR_PHP_WRAPPER_CALL;
580589
NRPRG(wordpress_tag) = old_tag;
590+
if (NULL == NRPRG(wordpress_tag)) {
591+
NRPRG(check_cufa) = false;
592+
}
581593
} else {
594+
NRPRG(check_cufa) = false;
582595
NR_PHP_WRAPPER_CALL;
583596
}
584597

agent/php_newrelic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ int symfony1_in_dispatch; /* Whether we are currently within a
378378
int symfony1_in_error404; /* Whether we are currently within a
379379
sfError404Exception::printStackTrace() frame */
380380

381+
bool check_cufa; /* Whether we need to check cufa because we are
382+
instrumenting hooks, or whether we can skip cufa */
383+
381384
char* wordpress_tag; /* The current WordPress tag */
382385
nr_regex_t* wordpress_plugin_regex; /* Regex for plugin filenames */
383386
nr_regex_t* wordpress_theme_regex; /* Regex for theme filenames */

agent/php_rinit.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ PHP_RINIT_FUNCTION(newrelic) {
8989
nr_php_extension_instrument_rescan(NRPRG(extensions) TSRMLS_CC);
9090
}
9191

92+
NRPRG(check_cufa) = false;
93+
9294
NRPRG(mysql_last_conn) = NULL;
9395
NRPRG(pgsql_last_conn) = NULL;
9496
NRPRG(datastore_connections) = nr_hashmap_create(

agent/php_vm.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ static int nr_php_handle_cufa_fcall(zend_execute_data* execute_data) {
108108
goto call_previous_and_return;
109109
}
110110

111+
/*
112+
* If we don't have instrumented hooks that require this, skip to the
113+
* end.
114+
*/
115+
if (false == NRPRG(check_cufa)) {
116+
goto call_previous_and_return;
117+
}
118+
111119
/*
112120
* Since we're in the middle of a function call, the Zend Engine is actually
113121
* only partway through constructing the new function frame. As a result, it

agent/tests/test_internal_instrument.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static void test_cufa_direct(TSRMLS_D) {
4949

5050
tlib_php_request_start();
5151

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

7576
tlib_php_request_start();
7677

78+
NRPRG(check_cufa) = true;
7779
define_cufa_function_f(TSRMLS_C);
7880
tlib_php_request_eval(
7981
"function g() { $cufa = 'call_user_func_array'; return $cufa('f', "

0 commit comments

Comments
 (0)