From 9ce9039f4238828ab66742efd49d979d27f511c9 Mon Sep 17 00:00:00 2001 From: Caio Ramos Casimiro Date: Fri, 17 May 2024 14:15:46 +0100 Subject: [PATCH] refactor(metrics) move global ptr to ngx_wa_conf_t --- src/common/debug/ngx_wasm_debug_module.c | 4 +-- src/common/metrics/ngx_wa_metrics.c | 2 +- src/common/metrics/ngx_wa_metrics.h | 2 +- src/common/proxy_wasm/ngx_proxy_wasm_host.c | 12 ++++++--- src/ngx_wasmx.c | 28 +++++++++++++-------- src/ngx_wasmx.h | 8 +++--- src/wasm/ngx_wasm_directives.c | 4 +-- 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/common/debug/ngx_wasm_debug_module.c b/src/common/debug/ngx_wasm_debug_module.c index 11adb3a66..3aee09ecd 100644 --- a/src/common/debug/ngx_wasm_debug_module.c +++ b/src/common/debug/ngx_wasm_debug_module.c @@ -53,7 +53,7 @@ ngx_wasm_debug_init(ngx_cycle_t *cycle) /* invalid metric name length */ ngx_wa_assert( - ngx_wa_metrics_add(ngx_wasmx_metrics(), + ngx_wa_metrics_add(ngx_wasmx_metrics(cycle), &metric_name, NGX_WA_METRIC_COUNTER, &mid) == NGX_ERROR @@ -61,7 +61,7 @@ ngx_wasm_debug_init(ngx_cycle_t *cycle) /* invalid metric type */ ngx_wa_assert( - ngx_wa_metrics_add(ngx_wasmx_metrics(), + ngx_wa_metrics_add(ngx_wasmx_metrics(cycle), &metric_name, 100, &mid) == NGX_ERROR diff --git a/src/common/metrics/ngx_wa_metrics.c b/src/common/metrics/ngx_wa_metrics.c index e57aba13e..80a715a79 100644 --- a/src/common/metrics/ngx_wa_metrics.c +++ b/src/common/metrics/ngx_wa_metrics.c @@ -170,7 +170,7 @@ ngx_wa_metrics_create(ngx_cycle_t *cycle) return NULL; } - metrics->old_metrics = ngx_wasmx_metrics(); + metrics->old_metrics = ngx_wasmx_metrics(cycle->old_cycle); metrics->config.slab_size = NGX_CONF_UNSET_SIZE; metrics->config.max_metric_name_length = NGX_CONF_UNSET_SIZE; diff --git a/src/common/metrics/ngx_wa_metrics.h b/src/common/metrics/ngx_wa_metrics.h index a884b0e6d..34b0a19fd 100644 --- a/src/common/metrics/ngx_wa_metrics.h +++ b/src/common/metrics/ngx_wa_metrics.h @@ -62,7 +62,7 @@ struct ngx_wa_metrics_s { }; -ngx_wa_metrics_t *ngx_wasmx_metrics(); +ngx_wa_metrics_t *ngx_wasmx_metrics(ngx_cycle_t *cycle); ngx_wa_metrics_t *ngx_wa_metrics_create(ngx_cycle_t *cycle); // alloc ngx_int_t ngx_wa_metrics_init_conf(ngx_wa_metrics_t *metrics, ngx_conf_t *cf); diff --git a/src/common/proxy_wasm/ngx_proxy_wasm_host.c b/src/common/proxy_wasm/ngx_proxy_wasm_host.c index dbe1cadb7..2c073e24d 100644 --- a/src/common/proxy_wasm/ngx_proxy_wasm_host.c +++ b/src/common/proxy_wasm/ngx_proxy_wasm_host.c @@ -1585,7 +1585,8 @@ ngx_proxy_wasm_hfuncs_define_metric(ngx_wavm_instance_t *instance, size_t max_len; uint32_t *id; ngx_str_t name, prefixed_name, *filter_name; - ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(); + ngx_cycle_t *cycle = (ngx_cycle_t *) ngx_cycle; + ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(cycle); ngx_wa_metric_type_e type; ngx_proxy_wasm_exec_t *pwexec; ngx_proxy_wasm_metric_type_e pw_type; @@ -1644,7 +1645,8 @@ ngx_proxy_wasm_hfuncs_increment_metric(ngx_wavm_instance_t *instance, { uint32_t metric_id; ngx_int_t rc, offset; - ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(); + ngx_cycle_t *cycle = (ngx_cycle_t *) ngx_cycle; + ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(cycle); ngx_proxy_wasm_exec_t *pwexec = ngx_proxy_wasm_instance2pwexec(instance); metric_id = args[0].of.i32; @@ -1667,7 +1669,8 @@ ngx_proxy_wasm_hfuncs_record_metric(ngx_wavm_instance_t *instance, { uint32_t metric_id; ngx_int_t rc, offset; - ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(); + ngx_cycle_t *cycle = (ngx_cycle_t *) ngx_cycle; + ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(cycle); ngx_proxy_wasm_exec_t *pwexec = ngx_proxy_wasm_instance2pwexec(instance); metric_id = args[0].of.i32; @@ -1692,7 +1695,8 @@ ngx_proxy_wasm_hfuncs_get_metric(ngx_wavm_instance_t *instance, uint32_t metric_id; ngx_int_t rc; ngx_uint_t *ret_value; - ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(); + ngx_cycle_t *cycle = (ngx_cycle_t *) ngx_cycle; + ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(cycle); ngx_proxy_wasm_exec_t *pwexec = ngx_proxy_wasm_instance2pwexec(instance); metric_id = args[0].of.i32; diff --git a/src/ngx_wasmx.c b/src/ngx_wasmx.c index df48ae6c1..09ec82fd3 100644 --- a/src/ngx_wasmx.c +++ b/src/ngx_wasmx.c @@ -23,9 +23,6 @@ ngx_uint_t ngx_wasm_max_module = 0; ngx_uint_t ngx_ipc_max_module = 0; -static ngx_wa_metrics_t *ngx_wa_metrics = NULL; - - static ngx_command_t ngx_wasmx_cmds[] = { { ngx_string("wasm"), @@ -71,10 +68,21 @@ ngx_module_t ngx_wasmx_module = { }; -ngx_wa_metrics_t * -ngx_wasmx_metrics() +ngx_inline ngx_wa_metrics_t * +ngx_wasmx_metrics(ngx_cycle_t *cycle) { - return ngx_wa_metrics; + ngx_wa_conf_t *wacf; + + if (!cycle->conf_ctx) { + return NULL; + } + + wacf = ngx_wa_cycle_get_conf(cycle); + if (wacf == NULL) { + return NULL; + } + + return wacf->metrics; } @@ -126,8 +134,8 @@ ngx_wasmx_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf, } #endif - ngx_wa_metrics = ngx_wa_metrics_create(cf->cycle); - if (ngx_wa_metrics == NULL) { + wacf->metrics = ngx_wa_metrics_create(cf->cycle); + if (wacf->metrics == NULL) { return NGX_CONF_ERROR; } @@ -232,7 +240,7 @@ ngx_wasmx_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf, } } - if (ngx_wa_metrics_init_conf(ngx_wa_metrics, cf) != NGX_OK) { + if (ngx_wa_metrics_init_conf(wacf->metrics, cf) != NGX_OK) { return NGX_CONF_ERROR; } @@ -268,7 +276,7 @@ ngx_wasmx_init(ngx_cycle_t *cycle) return NGX_OK; } - rc = ngx_wa_metrics_init(ngx_wa_metrics, cycle); + rc = ngx_wa_metrics_init(wacf->metrics, cycle); if (rc != NGX_OK) { return rc; } diff --git a/src/ngx_wasmx.h b/src/ngx_wasmx.h index e61001fd2..2ebd324ca 100644 --- a/src/ngx_wasmx.h +++ b/src/ngx_wasmx.h @@ -3,6 +3,7 @@ #include +#include #if (NGX_DEBUG) @@ -27,11 +28,12 @@ typedef ngx_int_t (*ngx_wa_init_pt)(ngx_cycle_t *cycle); typedef struct { - ngx_uint_t initialized_types; - void **wasm_confs; + ngx_uint_t initialized_types; + void **wasm_confs; #ifdef NGX_WA_IPC - void **ipc_confs; + void **ipc_confs; #endif + ngx_wa_metrics_t *metrics; } ngx_wa_conf_t; diff --git a/src/wasm/ngx_wasm_directives.c b/src/wasm/ngx_wasm_directives.c index 6a915a38d..0ff42751f 100644 --- a/src/wasm/ngx_wasm_directives.c +++ b/src/wasm/ngx_wasm_directives.c @@ -334,7 +334,7 @@ ngx_wasm_core_metrics_slab_size_directive(ngx_conf_t *cf, ngx_command_t *cmd, { ssize_t size; ngx_str_t *value; - ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(); + ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(cf->cycle); if (cf->cmd_type != NGX_METRICS_CONF) { return NGX_CONF_ERROR; @@ -362,7 +362,7 @@ ngx_wasm_core_metrics_max_metric_name_length_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_str_t *value; - ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(); + ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(cf->cycle); if (cf->cmd_type != NGX_METRICS_CONF) { return NGX_CONF_ERROR;