Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasmtime) expose cache_config directive #536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 41 additions & 1 deletion src/wasm/wrt/ngx_wrt_wasmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ typedef void (*wasmtime_config_set_int_pt)(wasm_config_t *config,
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 +133,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 +167,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_MAX_ERROR_STR + 1, log);
if (errmsg == NULL) {
goto error;
}

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

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
124 changes: 124 additions & 0 deletions t/01-wasm/directives/011-wasmtime_cache_config_directive.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# vim:set ft= ts=4 sts=4 sw=4 et fdm=marker:

use strict;
use lib '.';
use t::TestWasmX;

our $nginxV = $t::TestWasmX::nginxV;
our $osname = $t::TestWasmX::osname;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also looks unused


add_cleanup_handler(sub {
my @jit_dumps = glob("$::pwd/jit-*.dump");

foreach my $file (@jit_dumps) {
#warn "\n", $file, "\n";
unlink($file);
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this in this file?


plan_tests(4);
run_tests();

__DATA__

=== TEST 1: wasmtime cache_config - missing file
--- skip_eval: 4: $::nginxV !~ m/wasmtime/
--- main_config
wasm {
wasmtime {
cache_config missing_file;
}
}
--- error_log eval
qr/\[emerg\] .*? failed configuring wasmtime cache; failed to read config file: missing_file/
--- no_error_log
[error]
[crit]
--- must_die



=== TEST 2: wasmtime cache_config - bad file
--- skip_eval: 4: $::nginxV !~ m/wasmtime/
--- user_files
>>> wasmtime_config.toml
invalid contents
--- main_config
wasm {
wasmtime {
cache_config $TEST_NGINX_HTML_DIR/wasmtime_config.toml;
}
}
--- error_log eval
qr@\[emerg\] .*? failed configuring wasmtime cache; failed to parse config file: .*/wasmtime_config.toml@
--- no_error_log
[error]
[crit]
--- must_die



=== TEST 3: wasmtime cache_config - good file, cache disabled
--- skip_eval: 4: $::nginxV !~ m/wasmtime/
--- user_files
>>> wasmtime_config.toml
[cache]
enabled = false
--- main_config eval
qq{
wasm {
module hostcalls $t::TestWasmX::crates/hostcalls.wasm;

wasmtime {
cache_config $ENV{TEST_NGINX_HTML_DIR}/wasmtime_config.toml;
}
}
}
--- config
location /t {
proxy_wasm hostcalls 'test=/t/set_request_header \
value=Hello:wasm';
proxy_wasm hostcalls 'test=/t/echo/headers';
}
--- response_body
Host: localhost
Connection: close
Hello: wasm
--- error_log eval
qr@setting wasmtime cache config file: ".*/wasmtime_config.toml"@
--- no_error_log
[error]



=== TEST 4: wasmtime cache_config - good file, cache enabled
--- skip_eval: 4: $::nginxV !~ m/wasmtime/
--- user_files
>>> wasmtime_config.toml
[cache]
enabled = true
directory = "/tmp/ngx_wasm_module/cache/wasmtime"
--- main_config eval
qq{
wasm {
module hostcalls $t::TestWasmX::crates/hostcalls.wasm;

wasmtime {
cache_config $ENV{TEST_NGINX_HTML_DIR}/wasmtime_config.toml;
}
}
}
--- config
location /t {
proxy_wasm hostcalls 'test=/t/set_request_header \
value=Hello:wasm';
proxy_wasm hostcalls 'test=/t/echo/headers';
}
--- response_body
Host: localhost
Connection: close
Hello: wasm
--- error_log eval
qr@setting wasmtime cache config file: ".*/wasmtime_config.toml"@
--- no_error_log
[error]