Skip to content

Commit

Permalink
refactor(shm) move ngx_wasm_shm to ngx_wa for IPC subsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
casimiro authored and thibaultcha committed Sep 19, 2024
1 parent 13cba8b commit ea2690f
Show file tree
Hide file tree
Showing 19 changed files with 400 additions and 391 deletions.
12 changes: 6 additions & 6 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ NGX_WASMX_DEPS="\
$ngx_addon_dir/src/common/ngx_wasm_subsystem.h \
$ngx_addon_dir/src/common/ngx_wasm_socket_tcp.h \
$ngx_addon_dir/src/common/ngx_wasm_socket_tcp_readers.h \
$ngx_addon_dir/src/common/shm/ngx_wa_shm.h \
$ngx_addon_dir/src/common/shm/ngx_wa_shm_kv.h \
$ngx_addon_dir/src/common/shm/ngx_wa_shm_queue.h \
$ngx_addon_dir/src/common/proxy_wasm/ngx_proxy_wasm.h \
$ngx_addon_dir/src/common/proxy_wasm/ngx_proxy_wasm_maps.h \
$ngx_addon_dir/src/common/proxy_wasm/ngx_proxy_wasm_properties.h \
$ngx_addon_dir/src/common/shm/ngx_wasm_shm.h \
$ngx_addon_dir/src/common/shm/ngx_wasm_shm_kv.h \
$ngx_addon_dir/src/common/shm/ngx_wasm_shm_queue.h \
$ngx_addon_dir/src/common/metrics/ngx_wa_metrics.h \
$ngx_addon_dir/src/common/metrics/ngx_wa_histogram.h"

Expand All @@ -151,14 +151,14 @@ NGX_WASMX_SRCS="\
$ngx_addon_dir/src/common/ngx_wasm_subsystem.c \
$ngx_addon_dir/src/common/ngx_wasm_socket_tcp.c \
$ngx_addon_dir/src/common/ngx_wasm_socket_tcp_readers.c \
$ngx_addon_dir/src/common/shm/ngx_wa_shm.c \
$ngx_addon_dir/src/common/shm/ngx_wa_shm_kv.c \
$ngx_addon_dir/src/common/shm/ngx_wa_shm_queue.c \
$ngx_addon_dir/src/common/proxy_wasm/ngx_proxy_wasm.c \
$ngx_addon_dir/src/common/proxy_wasm/ngx_proxy_wasm_host.c \
$ngx_addon_dir/src/common/proxy_wasm/ngx_proxy_wasm_maps.c \
$ngx_addon_dir/src/common/proxy_wasm/ngx_proxy_wasm_properties.c \
$ngx_addon_dir/src/common/proxy_wasm/ngx_proxy_wasm_util.c \
$ngx_addon_dir/src/common/shm/ngx_wasm_shm.c \
$ngx_addon_dir/src/common/shm/ngx_wasm_shm_kv.c \
$ngx_addon_dir/src/common/shm/ngx_wasm_shm_queue.c \
$ngx_addon_dir/src/common/metrics/ngx_wa_metrics.c \
$ngx_addon_dir/src/common/metrics/ngx_wa_histogram.c"

Expand Down
8 changes: 4 additions & 4 deletions src/common/metrics/ngx_wa_histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ histogram_grow(ngx_wa_metrics_t *metrics, ngx_wa_metrics_histogram_t *h,
+ sizeof(ngx_wa_metrics_bin_t) * h->n_bins;
size = old_size + sizeof(ngx_wa_metrics_bin_t) * n;

if (metrics->shm->eviction == NGX_WASM_SHM_EVICTION_NONE) {
ngx_wasm_shm_lock(metrics->shm);
if (metrics->shm->eviction == NGX_WA_SHM_EVICTION_NONE) {
ngx_wa_shm_lock(metrics->shm);
}

new_h = ngx_slab_calloc_locked(metrics->shm->shpool, size);
Expand All @@ -72,8 +72,8 @@ histogram_grow(ngx_wa_metrics_t *metrics, ngx_wa_metrics_histogram_t *h,

error:

if (metrics->shm->eviction == NGX_WASM_SHM_EVICTION_NONE) {
ngx_wasm_shm_unlock(metrics->shm);
if (metrics->shm->eviction == NGX_WA_SHM_EVICTION_NONE) {
ngx_wa_shm_unlock(metrics->shm);
}

return rc;
Expand Down
66 changes: 33 additions & 33 deletions src/common/metrics/ngx_wa_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ realloc_histogram(ngx_wa_metrics_t *metrics, ngx_wa_metric_t *old_m,
ngx_str_t *val;
ngx_wa_metric_t *m;

rc = ngx_wasm_shm_kv_get_locked(metrics->shm, NULL, &mid, &val, &cas);
rc = ngx_wa_shm_kv_get_locked(metrics->shm, NULL, &mid, &val, &cas);
if (rc != NGX_OK) {
return rc;
}
Expand All @@ -91,11 +91,11 @@ static ngx_int_t
realloc_metrics(ngx_wa_metrics_t *metrics, ngx_rbtree_node_t *node,
ngx_rbtree_node_t *sentinel)
{
uint32_t mid;
ngx_int_t rc;
ngx_uint_t val;
ngx_wasm_shm_kv_node_t *n = (ngx_wasm_shm_kv_node_t *) node;
ngx_wa_metric_t *m = (ngx_wa_metric_t *) n->value.data;
uint32_t mid;
ngx_int_t rc;
ngx_uint_t val;
ngx_wa_shm_kv_node_t *n = (ngx_wa_shm_kv_node_t *) node;
ngx_wa_metric_t *m = (ngx_wa_metric_t *) n->value.data;

if (node == sentinel) {
return NGX_OK;
Expand Down Expand Up @@ -168,16 +168,16 @@ ngx_wa_metrics_alloc(ngx_cycle_t *cycle)
metrics->config.slab_size = NGX_CONF_UNSET_SIZE;
metrics->config.max_metric_name_length = NGX_CONF_UNSET_SIZE;

metrics->shm = ngx_pcalloc(cycle->pool, sizeof(ngx_wasm_shm_t));
metrics->shm = ngx_pcalloc(cycle->pool, sizeof(ngx_wa_shm_t));
if (metrics->shm == NULL) {
ngx_pfree(cycle->pool, metrics);
return NULL;
}

metrics->shm->log = &cycle->new_log;
metrics->shm->name = shm_name;
metrics->shm->type = NGX_WASM_SHM_TYPE_METRICS;
metrics->shm->eviction = NGX_WASM_SHM_EVICTION_NONE;
metrics->shm->type = NGX_WA_SHM_TYPE_METRICS;
metrics->shm->eviction = NGX_WA_SHM_EVICTION_NONE;

return metrics;
}
Expand All @@ -186,12 +186,12 @@ ngx_wa_metrics_alloc(ngx_cycle_t *cycle)
char *
ngx_wa_metrics_init_conf(ngx_conf_t *cf)
{
ngx_cycle_t *cycle = cf->cycle;
ngx_wa_conf_t *wacf = ngx_wa_cycle_get_conf(cycle);
ngx_wa_metrics_t *metrics = wacf->metrics;
ngx_wa_metrics_t *old_metrics = metrics->old_metrics;
ngx_wasm_core_conf_t *wcf = ngx_wasm_core_cycle_get_conf(cycle);
ngx_core_conf_t *ccf;
ngx_cycle_t *cycle = cf->cycle;
ngx_array_t *shms = ngx_wasmx_shms(cycle);
ngx_wa_conf_t *wacf = ngx_wa_cycle_get_conf(cycle);
ngx_wa_metrics_t *metrics = wacf->metrics;
ngx_wa_metrics_t *old_metrics = metrics->old_metrics;
ngx_core_conf_t *ccf;

ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);

Expand All @@ -207,7 +207,7 @@ ngx_wa_metrics_init_conf(ngx_conf_t *cf)
/* TODO: if eviction is enabled, metrics->workers must be set to 1 */
metrics->workers = ccf->worker_processes;

metrics->mapping = ngx_array_push(&wcf->shms);
metrics->mapping = ngx_array_push(shms);
if (metrics->mapping == NULL) {
return NULL;
}
Expand All @@ -220,7 +220,7 @@ ngx_wa_metrics_init_conf(ngx_conf_t *cf)
return NGX_CONF_ERROR;
}

metrics->mapping->zone->init = ngx_wasm_shm_init_zone;
metrics->mapping->zone->init = ngx_wa_shm_init_zone;
metrics->mapping->zone->data = metrics->shm;
metrics->mapping->zone->noreuse = 0;

Expand All @@ -238,17 +238,17 @@ ngx_wa_metrics_init_conf(ngx_conf_t *cf)
ngx_int_t
ngx_wa_metrics_shm_init(ngx_cycle_t *cycle)
{
ngx_int_t rc;
ngx_wasm_shm_kv_t *old_shm_kv;
ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(cycle);
ngx_int_t rc;
ngx_wa_shm_kv_t *old_shm_kv;
ngx_wa_metrics_t *metrics = ngx_wasmx_metrics(cycle);

if (metrics->old_metrics && !metrics->mapping->zone->noreuse) {
/* reuse old kv store */
metrics->shm->data = metrics->old_metrics->shm->data;
return NGX_OK;
}

rc = ngx_wasm_shm_kv_init(metrics->shm);
rc = ngx_wa_shm_kv_init(metrics->shm);
if (rc != NGX_OK) {
return rc;
}
Expand All @@ -259,7 +259,7 @@ ngx_wa_metrics_shm_init(ngx_cycle_t *cycle)
metrics->mapping->zone->noreuse = 0;

/* realloc old kv store */
old_shm_kv = ngx_wasm_shm_get_kv(metrics->old_metrics->shm);
old_shm_kv = ngx_wa_shm_get_kv(metrics->old_metrics->shm);

return realloc_metrics(metrics, old_shm_kv->rbtree.root,
old_shm_kv->rbtree.sentinel);
Expand Down Expand Up @@ -299,9 +299,9 @@ ngx_wa_metrics_define(ngx_wa_metrics_t *metrics, ngx_str_t *name,

mid = ngx_crc32_long(name->data, name->len);

ngx_wasm_shm_lock(metrics->shm);
ngx_wa_shm_lock(metrics->shm);

rc = ngx_wasm_shm_kv_get_locked(metrics->shm, NULL, &mid, &p, &cas);
rc = ngx_wa_shm_kv_get_locked(metrics->shm, NULL, &mid, &p, &cas);
if (rc == NGX_OK) {
ngx_log_debug1(NGX_LOG_DEBUG_WASM, metrics->shm->log, 0,
"wasm returning existing metric id \"%uD\"", mid);
Expand All @@ -324,7 +324,7 @@ ngx_wa_metrics_define(ngx_wa_metrics_t *metrics, ngx_str_t *name,
val.len = size;
val.data = buf;

rc = ngx_wasm_shm_kv_set_locked(metrics->shm, name, &val, 0, &written);
rc = ngx_wa_shm_kv_set_locked(metrics->shm, name, &val, 0, &written);
if (rc != NGX_OK) {
goto error;
}
Expand All @@ -335,7 +335,7 @@ ngx_wa_metrics_define(ngx_wa_metrics_t *metrics, ngx_str_t *name,

error:

ngx_wasm_shm_unlock(metrics->shm);
ngx_wa_shm_unlock(metrics->shm);

if (rc == NGX_OK) {
ngx_wasm_log_error(NGX_LOG_INFO, metrics->shm->log, 0,
Expand Down Expand Up @@ -368,11 +368,11 @@ ngx_wa_metrics_increment(ngx_wa_metrics_t *metrics, uint32_t mid, ngx_int_t n)
#if 0
if (metrics->shm->eviction != NGX_WASM_EVICTION_NONE) {
slot = 0;
ngx_wasm_shm_lock(metrics->shm);
ngx_wa_shm_lock(metrics->shm);
}
#endif

rc = ngx_wasm_shm_kv_get_locked(metrics->shm, NULL, &mid, &val, &cas);
rc = ngx_wa_shm_kv_get_locked(metrics->shm, NULL, &mid, &val, &cas);
if (rc != NGX_OK) {
goto error;
}
Expand All @@ -397,7 +397,7 @@ ngx_wa_metrics_increment(ngx_wa_metrics_t *metrics, uint32_t mid, ngx_int_t n)

#if 0
if (metrics->shm->eviction != NGX_WASM_EVICTION_NONE) {
ngx_wasm_shm_unlock(metrics->shm);
ngx_wa_shm_unlock(metrics->shm);
}
#endif

Expand Down Expand Up @@ -428,11 +428,11 @@ ngx_wa_metrics_record(ngx_wa_metrics_t *metrics, uint32_t mid, ngx_int_t n)
#if 0
if (metrics->shm->eviction != NGX_WASM_EVICTION_NONE) {
slot = 0;
ngx_wasm_shm_lock(metrics->shm);
ngx_wa_shm_lock(metrics->shm);
}
#endif

rc = ngx_wasm_shm_kv_get_locked(metrics->shm, NULL, &mid, &val, &cas);
rc = ngx_wa_shm_kv_get_locked(metrics->shm, NULL, &mid, &val, &cas);
if (rc != NGX_OK) {
goto error;
}
Expand Down Expand Up @@ -461,7 +461,7 @@ ngx_wa_metrics_record(ngx_wa_metrics_t *metrics, uint32_t mid, ngx_int_t n)

#if 0
if (metrics->shm->eviction != NGX_WASM_EVICTION_NONE) {
ngx_wasm_shm_unlock(metrics->shm);
ngx_wa_shm_unlock(metrics->shm);
}
#endif

Expand All @@ -485,7 +485,7 @@ ngx_wa_metrics_get(ngx_wa_metrics_t *metrics, uint32_t mid, ngx_uint_t *out)
ngx_str_t *n;
ngx_wa_metric_t *m;

rc = ngx_wasm_shm_kv_get_locked(metrics->shm, NULL, &mid, &n, &cas);
rc = ngx_wa_shm_kv_get_locked(metrics->shm, NULL, &mid, &n, &cas);
if (rc != NGX_OK) {
goto done;
}
Expand Down
6 changes: 3 additions & 3 deletions src/common/metrics/ngx_wa_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define _NGX_WA_METRICS_H_INCLUDED_


#include <ngx_wasm_shm_kv.h>
#include <ngx_wa_shm_kv.h>


typedef struct ngx_wa_metrics_s ngx_wa_metrics_t;
Expand Down Expand Up @@ -54,10 +54,10 @@ typedef struct {

struct ngx_wa_metrics_s {
ngx_uint_t workers;
ngx_wasm_shm_t *shm;
ngx_wa_shm_t *shm;
ngx_wa_metrics_t *old_metrics;
ngx_wa_metrics_conf_t config;
ngx_wasm_shm_mapping_t *mapping;
ngx_wa_shm_mapping_t *mapping;
};


Expand Down
Loading

0 comments on commit ea2690f

Please sign in to comment.