Skip to content

Commit

Permalink
wip(*) merge edits
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Sep 19, 2024
1 parent 04042c6 commit 19c29d4
Show file tree
Hide file tree
Showing 31 changed files with 1,559 additions and 1,282 deletions.
452 changes: 262 additions & 190 deletions lib/resty/wasmx/shm.lua

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/common/debug/ngx_wasm_debug_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ngx_wasm_debug_init(ngx_cycle_t *cycle)
ngx_wa_metrics_define(ngx_wasmx_metrics(cycle),
&metric_name,
100,
&mid) == NGX_ABORT
&mid) == NGX_BUSY
);

/* unknown metric type name */
Expand Down
142 changes: 60 additions & 82 deletions src/common/lua/ngx_wasm_lua_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#include <ngx_wasm_lua_ffi.h>
#include <ngx_wa_shm_kv.h>
#ifdef NGX_WASM_HTTP
#include <ngx_http_lua_common.h>
#endif


ngx_wavm_t *
Expand Down Expand Up @@ -260,24 +262,6 @@ ngx_http_wasm_ffi_set_host_properties_handlers(ngx_http_request_t *r,
#endif


void
ngx_wa_ffi_shm_lock(ngx_wa_shm_t *shm)
{
ngx_wa_assert(shm);

ngx_wa_shm_lock(shm);
}


void
ngx_wa_ffi_shm_unlock(ngx_wa_shm_t *shm)
{
ngx_wa_assert(shm);

ngx_wa_shm_unlock(shm);
}


ngx_int_t
ngx_wa_ffi_shm_setup_zones(ngx_wa_ffi_shm_setup_zones_handler_pt setup_handler)
{
Expand All @@ -304,126 +288,126 @@ ngx_wa_ffi_shm_setup_zones(ngx_wa_ffi_shm_setup_zones_handler_pt setup_handler)


static void
shm_kv_retrieve_keys(ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel,
ngx_uint_t start, ngx_uint_t limit,
ngx_str_t **keys, ngx_uint_t *total)
retrieve_keys(ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel,
ngx_uint_t limit, ngx_uint_t start_idx, ngx_uint_t *cur_idx,
ngx_str_t **keys)
{
ngx_wa_shm_kv_node_t *n = (ngx_wa_shm_kv_node_t *) node;
ngx_wa_shm_kv_node_t *n = (ngx_wa_shm_kv_node_t *) node;

if (limit > 0 && (*total - start) == limit) {
if ((*cur_idx - start_idx) == limit) {
return;
}

if (keys && *total >= start) {
keys[(*total - start)] = &n->key.str;
if (*cur_idx >= start_idx) {
/* append to result */
keys[(*cur_idx - start_idx)] = &n->key.str;
}

(*total)++;
(*cur_idx)++;

if (node->left != sentinel) {
shm_kv_retrieve_keys(node->left, sentinel, start, limit, keys, total);
retrieve_keys(node->left, sentinel, limit, start_idx, cur_idx, keys);
}

if (node->right != sentinel) {
shm_kv_retrieve_keys(node->right, sentinel, start, limit, keys, total);
retrieve_keys(node->right, sentinel, limit, start_idx, cur_idx, keys);
}
}


ngx_int_t
ngx_wa_ffi_shm_iterate_keys(ngx_wa_shm_t *shm,
ngx_uint_t page, ngx_uint_t page_size,
ngx_str_t **keys, ngx_uint_t *total)
ngx_wa_ffi_shm_iterate_keys(ngx_wa_shm_t *shm, ngx_uint_t page_size,
ngx_uint_t *clast_idx, ngx_uint_t *cur_idx, ngx_str_t **keys)
{
ngx_uint_t last_idx = clast_idx ? *clast_idx : 0;
ngx_wa_shm_kv_t *kv;

ngx_wa_assert(shm && shm->type != NGX_WA_SHM_TYPE_QUEUE);
ngx_wa_assert(shm->type != NGX_WA_SHM_TYPE_QUEUE);

if (!ngx_wa_shm_locked(shm)) {
return NGX_ABORT;
}

kv = shm->data;

if (page >= kv->nelts) {
return NGX_DECLINED;
if (last_idx >= kv->nelts) {
/* finished */
return NGX_DONE;
}

if (kv->rbtree.root != kv->rbtree.sentinel) {
shm_kv_retrieve_keys(kv->rbtree.root, kv->rbtree.sentinel,
page, page_size, keys, total);
}
/* cannot be empty here (kv->nelts > 0) */
ngx_wa_assert(kv->rbtree.root != kv->rbtree.sentinel);

*total = *total - page;
retrieve_keys(kv->rbtree.root, kv->rbtree.sentinel, page_size, last_idx,
cur_idx, keys);

*cur_idx -= last_idx;

if (clast_idx) {
*clast_idx += *cur_idx;
}

return NGX_OK;
}


ngx_uint_t
ngx_wa_ffi_shm_kv_nelts(ngx_wa_shm_t *shm)
{
ngx_wa_shm_kv_t *kv;

ngx_wa_assert(shm->type != NGX_WA_SHM_TYPE_QUEUE);

kv = shm->data;

return kv->nelts;
}


ngx_int_t
ngx_wa_ffi_shm_get_kv_value(ngx_wa_shm_t *shm,
ngx_str_t *k, ngx_str_t **v, uint32_t *cas)
ngx_wa_ffi_shm_kv_get_value(ngx_wa_shm_t *shm, ngx_str_t *k, ngx_str_t **v,
uint32_t *cas)
{
unsigned unlock = 0;
ngx_int_t rc;

ngx_wa_assert(shm && k && v && cas);
ngx_wa_assert(shm->type == NGX_WA_SHM_TYPE_KV);

if (!ngx_wa_shm_locked(shm)) {
ngx_wa_shm_lock(shm);
unlock = 1;
}
ngx_wa_shm_lock(shm);

rc = ngx_wa_shm_kv_get_locked(shm, k, NULL, v, cas);

if (unlock) {
ngx_wa_shm_unlock(shm);
}
ngx_wa_shm_unlock(shm);

return rc;
}


ngx_int_t
ngx_wa_ffi_shm_set_kv_value(ngx_wa_shm_t *shm,
ngx_str_t *k, ngx_str_t *v, uint32_t cas,
ngx_int_t *written)
ngx_wa_ffi_shm_kv_set_value(ngx_wa_shm_t *shm, ngx_str_t *k, ngx_str_t *v,
uint32_t cas, ngx_int_t *written)
{
unsigned unlock = 0;
ngx_int_t rc;

ngx_wa_assert(shm && k && v && written);
ngx_wa_assert(shm->type == NGX_WA_SHM_TYPE_KV);

if (!ngx_wa_shm_locked(shm)) {
ngx_wa_shm_lock(shm);
unlock = 1;
}
ngx_wa_shm_lock(shm);

rc = ngx_wa_shm_kv_set_locked(shm, k, v, cas, written);

if (unlock) {
ngx_wa_shm_unlock(shm);
}
ngx_wa_shm_unlock(shm);

return rc;
}


ngx_int_t
ngx_wa_ffi_shm_define_metric(ngx_str_t *name, ngx_wa_metric_type_e type,
uint32_t *metric_id)
ngx_wa_ffi_shm_metric_define(ngx_str_t *name, ngx_wa_metric_type_e type,
uint32_t *metric_id)
{
ngx_int_t rc;
ngx_str_t prefixed_name;
ngx_wa_metrics_t *metrics = ngx_wasmx_metrics((ngx_cycle_t *) ngx_cycle);
u_char buf[NGX_MAX_ERROR_STR];

ngx_wa_assert(metrics && name);

prefixed_name.data = buf;
prefixed_name.len = ngx_sprintf(buf, "lua.%V", name) - buf;

rc = ngx_wa_metrics_define(metrics, &prefixed_name, type, metric_id);
rc = ngx_wa_metrics_define(metrics, name, type, metric_id);
if (rc != NGX_OK) {
return rc;
}
Expand All @@ -433,36 +417,30 @@ ngx_wa_ffi_shm_define_metric(ngx_str_t *name, ngx_wa_metric_type_e type,


ngx_int_t
ngx_wa_ffi_shm_increment_metric(uint32_t metric_id, ngx_uint_t value)
ngx_wa_ffi_shm_metric_increment(uint32_t metric_id, ngx_uint_t value)
{
ngx_wa_metrics_t *metrics = ngx_wasmx_metrics((ngx_cycle_t *) ngx_cycle);

ngx_wa_assert(metrics);

return ngx_wa_metrics_increment(metrics, metric_id, value);
}


ngx_int_t
ngx_wa_ffi_shm_record_metric(uint32_t metric_id, ngx_uint_t value)
ngx_wa_ffi_shm_metric_record(uint32_t metric_id, ngx_uint_t value)
{
ngx_wa_metrics_t *metrics = ngx_wasmx_metrics((ngx_cycle_t *) ngx_cycle);

ngx_wa_assert(metrics);

return ngx_wa_metrics_record(metrics, metric_id, value);
}


ngx_int_t
ngx_wa_ffi_shm_get_metric(uint32_t metric_id, ngx_str_t *name,
u_char *m_buf, size_t mbs, u_char *h_buf, size_t hbs)
ngx_wa_ffi_shm_metric_get(uint32_t metric_id, ngx_str_t *name, u_char *m_buf,
size_t mbs, u_char *h_buf, size_t hbs)
{
ngx_wa_metrics_t *metrics = ngx_wasmx_metrics((ngx_cycle_t *) ngx_cycle);
ngx_wa_metric_t *m;

ngx_wa_assert(metrics && (name || metric_id) && m_buf && h_buf);

m = (ngx_wa_metric_t *) m_buf;
ngx_wa_metrics_histogram_set_buffer(m, h_buf, hbs);

Expand Down
41 changes: 27 additions & 14 deletions src/common/lua/ngx_wasm_lua_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,48 @@ ngx_int_t ngx_http_wasm_ffi_set_host_properties_handlers(ngx_http_request_t *r,

ngx_int_t ngx_wa_ffi_shm_setup_zones(
ngx_wa_ffi_shm_setup_zones_handler_pt handler);
ngx_int_t ngx_wa_ffi_shm_iterate_keys(ngx_wa_shm_t *shm, ngx_uint_t page,
ngx_uint_t page_size, ngx_str_t **keys, ngx_uint_t *total);
void ngx_wa_ffi_shm_lock(ngx_wa_shm_t *shm);
void ngx_wa_ffi_shm_unlock(ngx_wa_shm_t *shm);
ngx_int_t ngx_wa_ffi_shm_iterate_keys(ngx_wa_shm_t *shm, ngx_uint_t page_size,
ngx_uint_t *clast_idx, ngx_uint_t *cur_idx, ngx_str_t **keys);

ngx_int_t ngx_wa_ffi_shm_get_kv_value(ngx_wa_shm_t *shm, ngx_str_t *k,
ngx_uint_t ngx_wa_ffi_shm_kv_nelts(ngx_wa_shm_t *shm);
ngx_int_t ngx_wa_ffi_shm_kv_get_value(ngx_wa_shm_t *shm, ngx_str_t *k,
ngx_str_t **v, uint32_t *cas);
ngx_int_t ngx_wa_ffi_shm_set_kv_value(ngx_wa_shm_t *shm, ngx_str_t *k,
ngx_int_t ngx_wa_ffi_shm_kv_set_value(ngx_wa_shm_t *shm, ngx_str_t *k,
ngx_str_t *v, uint32_t cas, ngx_int_t *written);

ngx_int_t ngx_wa_ffi_shm_define_metric(ngx_str_t *name,
ngx_int_t ngx_wa_ffi_shm_metric_define(ngx_str_t *name,
ngx_wa_metric_type_e type, uint32_t *metric_id);
ngx_int_t ngx_wa_ffi_shm_increment_metric(uint32_t metric_id, ngx_uint_t value);
ngx_int_t ngx_wa_ffi_shm_record_metric(uint32_t metric_id, ngx_uint_t value);
ngx_int_t ngx_wa_ffi_shm_get_metric(uint32_t metric_id, ngx_str_t *name,
ngx_int_t ngx_wa_ffi_shm_metric_increment(uint32_t metric_id, ngx_uint_t value);
ngx_int_t ngx_wa_ffi_shm_metric_record(uint32_t metric_id, ngx_uint_t value);
ngx_int_t ngx_wa_ffi_shm_metric_get(uint32_t metric_id, ngx_str_t *name,
u_char *m_buf, size_t mbs, u_char *h_buf, size_t hbs);


void
ngx_wa_ffi_shm_lock(ngx_wa_shm_t *shm)
{
ngx_wa_shm_lock(shm);
}


void
ngx_wa_ffi_shm_unlock(ngx_wa_shm_t *shm)
{
ngx_wa_shm_unlock(shm);
}


ngx_int_t
ngx_wa_ffi_shm_one_slot_metric_size()
ngx_wa_ffi_shm_metrics_one_slot_size()
{
return NGX_WA_METRICS_ONE_SLOT_METRIC_SIZE;
return NGX_WA_METRICS_ONE_SLOT_SIZE;
}


ngx_int_t
ngx_wa_ffi_shm_max_histogram_size()
ngx_wa_ffi_shm_metrics_histogram_max_size()
{
return NGX_WA_METRICS_MAX_HISTOGRAM_SIZE;
return NGX_WA_METRICS_HISTOGRAM_MAX_SIZE;
}


Expand Down
2 changes: 1 addition & 1 deletion src/common/metrics/ngx_wa_histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ histogram_log(ngx_wa_metrics_t *metrics, ngx_wa_metric_t *m, uint32_t mid)
u_char *p;
ngx_wa_metrics_bin_t *b;
ngx_wa_metrics_histogram_t *h;
u_char h_buf[NGX_WA_METRICS_MAX_HISTOGRAM_SIZE];
u_char h_buf[NGX_WA_METRICS_HISTOGRAM_MAX_SIZE];
u_char s_buf[NGX_MAX_ERROR_STR];

ngx_memzero(h_buf, sizeof(h_buf));
Expand Down
8 changes: 4 additions & 4 deletions src/common/metrics/ngx_wa_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ ngx_wa_metrics_define(ngx_wa_metrics_t *metrics, ngx_str_t *name,
&& type != NGX_WA_METRIC_GAUGE
&& type != NGX_WA_METRIC_HISTOGRAM)
{
return NGX_ABORT;
return NGX_BUSY;
}

if (name->len > metrics->config.max_metric_name_length) {
Expand Down Expand Up @@ -337,9 +337,9 @@ ngx_wa_metrics_define(ngx_wa_metrics_t *metrics, ngx_str_t *name,
ngx_wa_shm_unlock(metrics->shm);

if (rc == NGX_OK) {
ngx_wasm_log_error(NGX_LOG_INFO, metrics->shm->log, 0,
"defined %V \"%V\" with id %uD",
ngx_wa_metric_type_name(type), name, mid);
ngx_log_debug3(NGX_LOG_DEBUG_WASM, metrics->shm->log, 0,
"defined %V \"%V\" with id %uD",
ngx_wa_metric_type_name(type), name, mid);
}

ngx_wa_assert(rc == NGX_OK || rc == NGX_ERROR);
Expand Down
9 changes: 5 additions & 4 deletions src/common/metrics/ngx_wa_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
#define NGX_WA_METRICS_BINS_INIT 5
#define NGX_WA_METRICS_BINS_MAX 18
#define NGX_WA_METRICS_BINS_INCREMENT 4
#define NGX_WA_METRICS_MAX_HISTOGRAM_SIZE sizeof(ngx_wa_metrics_histogram_t)\
+ sizeof(ngx_wa_metrics_bin_t) \
* NGX_WA_METRICS_BINS_MAX
#define NGX_WA_METRICS_ONE_SLOT_METRIC_SIZE sizeof(ngx_wa_metric_t) \
#define NGX_WA_METRICS_ONE_SLOT_SIZE sizeof(ngx_wa_metric_t) \
+ sizeof(ngx_wa_metric_val_t)
#define NGX_WA_METRICS_HISTOGRAM_MAX_SIZE \
sizeof(ngx_wa_metrics_histogram_t) \
+ sizeof(ngx_wa_metrics_bin_t) \
* NGX_WA_METRICS_BINS_MAX


typedef struct ngx_wa_metrics_s ngx_wa_metrics_t;
Expand Down
Loading

0 comments on commit 19c29d4

Please sign in to comment.