Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/wp-performance-improvements' i…
Browse files Browse the repository at this point in the history
…nto cufa_check
  • Loading branch information
lavarou committed Dec 4, 2023
2 parents 9a43ace + 54ad95d commit 7451347
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
43 changes: 33 additions & 10 deletions agent/fw_wordpress.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define NR_WORDPRESS_HOOK_PREFIX "Framework/WordPress/Hook/"
#define NR_WORDPRESS_PLUGIN_PREFIX "Framework/WordPress/Plugin/"

static nr_regex_t* wordpress_hook_regex;

static size_t zval_len_without_trailing_slash(const zval* zstr) {
nr_string_len_t len = Z_STRLEN_P(zstr);
const char* str = Z_STRVAL_P(zstr);
Expand Down Expand Up @@ -410,6 +412,10 @@ static void nr_wordpress_call_user_func_array(zend_function* func,
nr_php_wrap_callable(func, nr_wordpress_wrap_hook TSRMLS_CC);
}

static void free_tag(void* tag) {
nr_free(tag);
}

/*
* Some plugins generate transient tag names. We can detect these by checking
* the substrings returned from our regex rule. If the tag is transient, we
Expand All @@ -418,8 +424,7 @@ static void nr_wordpress_call_user_func_array(zend_function* func,
* Example: (old) add_option__transient_timeout_twccr_382402301f44c883bc0137_cat
* (new) add_option__transient_timeout_twccr_*_cat
*/
static char* nr_wordpress_clean_tag(const zval* tag TSRMLS_DC) {
char* orig_tag = NULL;
static char* nr_wordpress_clean_tag(const zval* tag) {
char* clean_tag = NULL;
nr_regex_t* regex = NULL;
nr_regex_substrings_t* ss = NULL;
Expand All @@ -428,13 +433,21 @@ static char* nr_wordpress_clean_tag(const zval* tag TSRMLS_DC) {
return NULL;
}

regex = NRPRG(wordpress_hook_regex);
regex = wordpress_hook_regex;
if (NULL == regex) {
return NULL;
}

orig_tag = nr_strndup(Z_STRVAL_P(tag), Z_STRLEN_P(tag));
ss = nr_regex_match_capture(regex, orig_tag, nr_strlen(orig_tag));
if (NULL == NRPRG(wordpress_clean_tag_cache)) {
NRPRG(wordpress_clean_tag_cache) = nr_hashmap_create(free_tag);
}

if (nr_hashmap_get_into(NRPRG(wordpress_clean_tag_cache), Z_STRVAL_P(tag),
Z_STRLEN_P(tag), (void**)&clean_tag)) {
return clean_tag;
}

ss = nr_regex_match_capture(regex, Z_STRVAL_P(tag), Z_STRLEN_P(tag));
clean_tag = nr_regex_substrings_get(ss, 5);

/*
Expand All @@ -455,7 +468,9 @@ static char* nr_wordpress_clean_tag(const zval* tag TSRMLS_DC) {
}

nr_regex_substrings_destroy(&ss);
nr_free(orig_tag);

nr_hashmap_set(NRPRG(wordpress_clean_tag_cache), Z_STRVAL_P(tag),
Z_STRLEN_P(tag), clean_tag);

return clean_tag;
}
Expand All @@ -481,9 +496,8 @@ NR_PHP_WRAPPER(nr_wordpress_exec_handle_tag) {

NRPRG(check_cufa) = true;

NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag TSRMLS_CC);
NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag);
NR_PHP_WRAPPER_CALL;
nr_free(NRPRG(wordpress_tag));
NRPRG(wordpress_tag) = old_tag;
if (NULL == NRPRG(wordpress_tag)) {
NRPRG(check_cufa) = false;
Expand Down Expand Up @@ -569,10 +583,9 @@ NR_PHP_WRAPPER(nr_wordpress_apply_filters) {

NRPRG(check_cufa) = true;

NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag TSRMLS_CC);
NRPRG(wordpress_tag) = nr_wordpress_clean_tag(tag);

NR_PHP_WRAPPER_CALL;
nr_free(NRPRG(wordpress_tag));
NRPRG(wordpress_tag) = old_tag;
if (NULL == NRPRG(wordpress_tag)) {
NRPRG(check_cufa) = false;
Expand Down Expand Up @@ -607,3 +620,13 @@ void nr_wordpress_enable(TSRMLS_D) {
nr_php_add_call_user_func_array_pre_callback(
nr_wordpress_call_user_func_array TSRMLS_CC);
}

void nr_wordpress_minit(void) {
wordpress_hook_regex = nr_regex_create(
"(^([a-z_-]+[_-])([0-9a-f_.]+[0-9][0-9a-f.]+)(_{0,1}.*)$|(.*))",
NR_REGEX_CASELESS, 0);
}

void nr_wordpress_mshutdown(void) {
nr_regex_destroy(&wordpress_hook_regex);
}
3 changes: 3 additions & 0 deletions agent/fw_wordpress.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ char* nr_php_wordpress_core_match_regex(const char* filename TSRMLS_DC);
*/
char* nr_php_wordpress_plugin_match_regex(const char* filename TSRMLS_DC);

extern void nr_wordpress_minit(void);
extern void nr_wordpress_mshutdown(void);

#endif /* FW_WORDPRESS_HDR */
5 changes: 2 additions & 3 deletions agent/php_minit.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "php_vm.h"
#include "php_wrapper.h"
#include "fw_laravel.h"
#include "fw_wordpress.h"
#include "lib_guzzle4.h"
#include "lib_guzzle6.h"
#include "nr_agent.h"
Expand All @@ -35,9 +36,6 @@
#include "util_syscalls.h"
#include "util_threads.h"

#include "fw_laravel.h"
#include "lib_guzzle4.h"

static void php_newrelic_init_globals(zend_newrelic_globals* nrg) {
if (nrunlikely(NULL == nrg)) {
return;
Expand Down Expand Up @@ -709,6 +707,7 @@ PHP_MINIT_FUNCTION(newrelic) {
nr_guzzle4_minit(TSRMLS_C);
nr_guzzle6_minit(TSRMLS_C);
nr_laravel_minit(TSRMLS_C);
nr_wordpress_minit();
nr_php_set_opcode_handlers();

nrl_debug(NRL_INIT, "MINIT processing done");
Expand Down
3 changes: 3 additions & 0 deletions agent/php_mshutdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "php_vm.h"
#include "nr_agent.h"
#include "util_logging.h"
#include "fw_wordpress.h"

#ifdef TAGS
void zm_shutdown_newrelic(void); /* ctags landing pad only */
Expand All @@ -39,6 +40,8 @@ PHP_MSHUTDOWN_FUNCTION(newrelic) {
*/
nrl_debug(NRL_INIT, "MSHUTDOWN processing started");

nr_wordpress_mshutdown();

/* restore header handler */
sapi_module.header_handler = NR_PHP_PROCESS_GLOBALS(orig_header_handler);
NR_PHP_PROCESS_GLOBALS(orig_header_handler) = NULL;
Expand Down
2 changes: 1 addition & 1 deletion agent/php_newrelic.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,12 @@ 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_hook_regex; /* Regex to sanitize hook names */
nr_regex_t* wordpress_plugin_regex; /* Regex for plugin filenames */
nr_regex_t* wordpress_theme_regex; /* Regex for theme filenames */
nr_regex_t* wordpress_core_regex; /* Regex for plugin filenames */
nr_hashmap_t* wordpress_file_metadata; /* Metadata for plugin and theme names
given a filename */
nr_hashmap_t* wordpress_clean_tag_cache; /* Cached clean tags */

char* doctrine_dql; /* The current Doctrine DQL. Only non-NULL while a Doctrine
object is on the stack. */
Expand Down
8 changes: 0 additions & 8 deletions agent/php_rinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ PHP_RINIT_FUNCTION(newrelic) {
nr_php_extension_instrument_rescan(NRPRG(extensions) TSRMLS_CC);
}

/*
* Compile regex for WordPress: includes logic for
* hook sanitization regex.
*/
NRPRG(wordpress_hook_regex) = nr_regex_create(
"(^([a-z_-]+[_-])([0-9a-f_.]+[0-9][0-9a-f.]+)(_{0,1}.*)$|(.*))",
NR_REGEX_CASELESS, 0);

NRPRG(check_cufa) = false;

NRPRG(mysql_last_conn) = NULL;
Expand Down
2 changes: 1 addition & 1 deletion agent/php_rshutdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ int nr_php_post_deactivate(void) {
nr_php_exception_filters_destroy(&NRPRG(exception_filters));
nr_regex_destroy(&NRPRG(wordpress_plugin_regex));
nr_regex_destroy(&NRPRG(wordpress_core_regex));
nr_regex_destroy(&NRPRG(wordpress_hook_regex));
nr_regex_destroy(&NRPRG(wordpress_theme_regex));
nr_hashmap_destroy(&NRPRG(wordpress_file_metadata));
nr_hashmap_destroy(&NRPRG(wordpress_clean_tag_cache));

nr_free(NRPRG(mysql_last_conn));
nr_free(NRPRG(pgsql_last_conn));
Expand Down

0 comments on commit 7451347

Please sign in to comment.