Skip to content

Commit

Permalink
refactor(metrics) move global ptr to ngx_wa_conf_t
Browse files Browse the repository at this point in the history
  • Loading branch information
casimiro committed May 17, 2024
1 parent e8cf21c commit 9ce9039
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/common/debug/ngx_wasm_debug_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ 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
);

/* 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
Expand Down
2 changes: 1 addition & 1 deletion src/common/metrics/ngx_wa_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/common/metrics/ngx_wa_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions src/common/proxy_wasm/ngx_proxy_wasm_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
28 changes: 18 additions & 10 deletions src/ngx_wasmx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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;
}


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

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

Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 5 additions & 3 deletions src/ngx_wasmx.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


#include <ngx_core.h>
#include <ngx_wa_metrics.h>


#if (NGX_DEBUG)
Expand All @@ -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;


Expand Down
4 changes: 2 additions & 2 deletions src/wasm/ngx_wasm_directives.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9ce9039

Please sign in to comment.