Skip to content

Commit

Permalink
feat(wasmtime) expose cache_config directive
Browse files Browse the repository at this point in the history
This adds support for `wasmtime { cache_config <config.toml>; }`,
for enabling disk caching of ahead-of-time compilation of .wasm files.

This should alleviate noticeable CPU spikes when starting up workers using
Wasmtime and Wasm modules larger than a few megabytes.
  • Loading branch information
hishamhm committed Apr 24, 2024
1 parent bb139e1 commit 9ef2ec5
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 40 deletions.
8 changes: 8 additions & 0 deletions src/wasm/ngx_wasm_core_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ static ngx_command_t ngx_wasm_core_commands[] = {
0,
NULL },

{ ngx_string("cache_config"),
NGX_WASMTIME_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_WA_WASM_CONF_OFFSET,
offsetof(ngx_wasm_core_conf_t, vm_conf)
+ offsetof(ngx_wavm_conf_t, cache_config),
NULL },

{ ngx_string("compiler"),
NGX_WASM_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
Expand Down
1 change: 1 addition & 0 deletions src/wasm/wrt/ngx_wrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct ngx_wavm_instance_s ngx_wavm_instance_t;
typedef struct {
const ngx_str_t *vm_name;
const ngx_str_t *runtime_name;
ngx_str_t cache_config;
ngx_str_t compiler;
ngx_flag_t backtraces;
ngx_array_t flags;
Expand Down
47 changes: 44 additions & 3 deletions src/wasm/wrt/ngx_wrt_wasmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
#include <ngx_wasi.h>


#define NGX_WRT_WASMTIME_STACK_NARGS 10
#define NGX_WRT_WASMTIME_STACK_NRETS 10
#define NGX_WRT_WASMTIME_STACK_NARGS 10
#define NGX_WRT_WASMTIME_STACK_NRETS 10
#define NGX_WRT_WASMTIME_CONFIG_ERR_LEN 255


typedef void (*wasmtime_config_set_int_pt)(wasm_config_t *config,
uint64_t value);
typedef void (*wasmtime_config_set_bool_pt)(wasm_config_t *config, bool value);


static u_char * ngx_wasmtime_log_handler(ngx_wrt_res_t *res, u_char *buf,
size_t len);


static ngx_int_t
size_flag_handler(wasm_config_t *config, ngx_str_t *name, ngx_str_t *value,
ngx_log_t *log, void *wrt_setter)
Expand Down Expand Up @@ -129,7 +134,10 @@ profiler_flag_handler(wasm_config_t *config, ngx_str_t *name, ngx_str_t *value,
static wasm_config_t *
ngx_wasmtime_init_conf(ngx_wavm_conf_t *conf, ngx_log_t *log)
{
wasm_config_t *config;
wasm_config_t *config;
char *pathname;
u_char *errmsg;
wasmtime_error_t *err;
#if 0
wasm_name_t msg;
wasmtime_error_t *err = NULL;
Expand Down Expand Up @@ -160,6 +168,39 @@ ngx_wasmtime_init_conf(ngx_wavm_conf_t *conf, ngx_log_t *log)
wasmtime_config_static_memory_maximum_size_set(config, 0);
#endif

if (conf->cache_config.len) {
ngx_wavm_log_error(NGX_LOG_INFO, log, NULL,
"setting wasmtime cache config file: \"%V\"",
&conf->cache_config);

pathname = ngx_calloc(conf->cache_config.len + 1, log);
if (pathname == NULL) {
goto error;
}

ngx_memcpy(pathname, conf->cache_config.data, conf->cache_config.len);

err = wasmtime_config_cache_config_load(config, pathname);

ngx_free(pathname);

if (err) {
errmsg = ngx_calloc(NGX_WRT_WASMTIME_CONFIG_ERR_LEN + 1, log);
if (errmsg == NULL) {
goto error;
}

ngx_wasmtime_log_handler((ngx_wrt_res_t *) err, errmsg,
NGX_WRT_WASMTIME_CONFIG_ERR_LEN);

ngx_log_error(NGX_LOG_EMERG, log, 0,
"failed configuring wasmtime cache; %s",
errmsg);

goto error;
}
}

if (conf->compiler.len) {
if (ngx_str_eq(conf->compiler.data, conf->compiler.len,
"auto", -1))
Expand Down
Loading

0 comments on commit 9ef2ec5

Please sign in to comment.