diff --git a/src/wasm/ngx_wasm_core_module.c b/src/wasm/ngx_wasm_core_module.c index d68c00e27..cd7f8d80b 100644 --- a/src/wasm/ngx_wasm_core_module.c +++ b/src/wasm/ngx_wasm_core_module.c @@ -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, diff --git a/src/wasm/wrt/ngx_wrt.h b/src/wasm/wrt/ngx_wrt.h index 1700f1f6c..6add6fe0b 100644 --- a/src/wasm/wrt/ngx_wrt.h +++ b/src/wasm/wrt/ngx_wrt.h @@ -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; diff --git a/src/wasm/wrt/ngx_wrt_wasmtime.c b/src/wasm/wrt/ngx_wrt_wasmtime.c index f42ee05fd..2ed0f1c2d 100644 --- a/src/wasm/wrt/ngx_wrt_wasmtime.c +++ b/src/wasm/wrt/ngx_wrt_wasmtime.c @@ -8,8 +8,9 @@ #include -#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, @@ -17,6 +18,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) @@ -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; @@ -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)) diff --git a/t/01-wasm/directives/008-flag_directive_wasmtime.t b/t/01-wasm/directives/008-flag_directive_wasmtime.t index 6e69076b6..9fb5c3250 100644 --- a/t/01-wasm/directives/008-flag_directive_wasmtime.t +++ b/t/01-wasm/directives/008-flag_directive_wasmtime.t @@ -87,7 +87,111 @@ qr/setting flag: "debug_info=off"/ -=== TEST 5: flag directive - wasmtime - consume_fuel - on +=== TEST 5: flag directive - wasmtime - cache_config_load - 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 6: flag directive - wasmtime - cache_config_load - 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 7: flag directive - wasmtime - cache_config_load - 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 8: flag directive - wasmtime - cache_config_load - 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] + + + +=== TEST 9: flag directive - wasmtime - consume_fuel - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -103,7 +207,7 @@ qr/setting flag: "consume_fuel=on"/ -=== TEST 6: flag directive - wasmtime - consume_fuel - off +=== TEST 10: flag directive - wasmtime - consume_fuel - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -119,7 +223,7 @@ qr/setting flag: "consume_fuel=off"/ -=== TEST 7: flag directive - wasmtime - epoch_interruption - on +=== TEST 11: flag directive - wasmtime - epoch_interruption - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -135,7 +239,7 @@ qr/setting flag: "epoch_interruption=on"/ -=== TEST 8: flag directive - wasmtime - epoch_interruption - off +=== TEST 12: flag directive - wasmtime - epoch_interruption - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -151,7 +255,7 @@ qr/setting flag: "epoch_interruption=off"/ -=== TEST 9: flag directive - wasmtime - max_wasm_stack +=== TEST 13: flag directive - wasmtime - max_wasm_stack --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -167,7 +271,7 @@ qr/setting flag: "max_wasm_stack=2m"/ -=== TEST 10: flag directive - wasmtime - wasm_threads - on +=== TEST 14: flag directive - wasmtime - wasm_threads - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -183,7 +287,7 @@ qr/setting flag: "wasm_threads=on"/ -=== TEST 11: flag directive - wasmtime - wasm_threads - off +=== TEST 15: flag directive - wasmtime - wasm_threads - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -199,7 +303,7 @@ qr/setting flag: "wasm_threads=off"/ -=== TEST 12: flag directive - wasmtime - wasm_reference_types - on +=== TEST 16: flag directive - wasmtime - wasm_reference_types - on --- SKIP: Unsupported (hard-coded) --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config @@ -216,7 +320,7 @@ qr/setting flag: "wasm_reference_types=on"/ -=== TEST 13: flag directive - wasmtime - wasm_reference_types - off +=== TEST 17: flag directive - wasmtime - wasm_reference_types - off --- SKIP: Unsupported (hard-coded) --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config @@ -233,7 +337,7 @@ qr/setting flag: "wasm_reference_types=off"/ -=== TEST 14: flag directive - wasmtime - wasm_simd - on +=== TEST 18: flag directive - wasmtime - wasm_simd - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -249,7 +353,7 @@ qr/setting flag: "wasm_simd=on"/ -=== TEST 15: flag directive - wasmtime - wasm_simd - off +=== TEST 19: flag directive - wasmtime - wasm_simd - off Disable both flags to avoid: "cannot disable the simd proposal but enable the relaxed simd proposal" Shown twice in logs for master+single procs @@ -273,7 +377,7 @@ setting flag: "wasm_relaxed_simd=off" -=== TEST 16: flag directive - wasmtime - wasm_bulk_memory - on +=== TEST 20: flag directive - wasmtime - wasm_bulk_memory - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -289,7 +393,7 @@ qr/setting flag: "wasm_bulk_memory=on"/ -=== TEST 17: flag directive - wasmtime - wasm_bulk_memory - off +=== TEST 21: flag directive - wasmtime - wasm_bulk_memory - off Disable all three flags as 'bulk_memory' requires 'threads' and 'reference_types' Shown twice in logs for master+single procs --- skip_eval: 4: $::nginxV !~ m/wasmtime/ @@ -315,7 +419,7 @@ setting flag: "wasm_reference_types=off" -=== TEST 18: flag directive - wasmtime - wasm_multi_value - on +=== TEST 22: flag directive - wasmtime - wasm_multi_value - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -331,7 +435,7 @@ qr/setting flag: "wasm_multi_value=on"/ -=== TEST 19: flag directive - wasmtime - wasm_multi_value - off +=== TEST 23: flag directive - wasmtime - wasm_multi_value - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -347,7 +451,7 @@ qr/setting flag: "wasm_multi_value=off"/ -=== TEST 20: flag directive - wasmtime - wasm_multi_memory - on +=== TEST 24: flag directive - wasmtime - wasm_multi_memory - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -363,7 +467,7 @@ qr/setting flag: "wasm_multi_memory=on"/ -=== TEST 21: flag directive - wasmtime - wasm_multi_memory - off +=== TEST 25: flag directive - wasmtime - wasm_multi_memory - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -379,7 +483,7 @@ qr/setting flag: "wasm_multi_memory=off"/ -=== TEST 22: flag directive - wasmtime - wasm_memory64 - on +=== TEST 26: flag directive - wasmtime - wasm_memory64 - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -395,7 +499,7 @@ qr/setting flag: "wasm_memory64=on"/ -=== TEST 23: flag directive - wasmtime - wasm_memory64 - off +=== TEST 27: flag directive - wasmtime - wasm_memory64 - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -411,7 +515,7 @@ qr/setting flag: "wasm_memory64=off"/ -=== TEST 24: flag directive - wasmtime - strategy - auto +=== TEST 28: flag directive - wasmtime - strategy - auto --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -427,7 +531,7 @@ qr/setting flag: "strategy=auto"/ -=== TEST 25: flag directive - wasmtime - strategy - cranelift +=== TEST 29: flag directive - wasmtime - strategy - cranelift --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -443,7 +547,7 @@ qr/setting flag: "strategy=cranelift"/ -=== TEST 26: flag directive - wasmtime - parallel_compilation - on +=== TEST 30: flag directive - wasmtime - parallel_compilation - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -459,7 +563,7 @@ qr/setting flag: "parallel_compilation=on"/ -=== TEST 27: flag directive - wasmtime - parallel_compilation - off +=== TEST 31: flag directive - wasmtime - parallel_compilation - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -475,7 +579,7 @@ qr/setting flag: "parallel_compilation=off"/ -=== TEST 28: flag directive - wasmtime - cranelift_debug_verifier - on +=== TEST 32: flag directive - wasmtime - cranelift_debug_verifier - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -491,7 +595,7 @@ qr/setting flag: "cranelift_debug_verifier=on"/ -=== TEST 29: flag directive - wasmtime - cranelift_debug_verifier - off +=== TEST 33: flag directive - wasmtime - cranelift_debug_verifier - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -507,7 +611,7 @@ qr/setting flag: "cranelift_debug_verifier=off"/ -=== TEST 30: flag directive - wasmtime - cranelift_nan_canonicalization - on +=== TEST 34: flag directive - wasmtime - cranelift_nan_canonicalization - on --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -523,7 +627,7 @@ qr/setting flag: "cranelift_nan_canonicalization=on"/ -=== TEST 31: flag directive - wasmtime - cranelift_nan_canonicalization - off +=== TEST 35: flag directive - wasmtime - cranelift_nan_canonicalization - off --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -539,7 +643,7 @@ qr/setting flag: "cranelift_nan_canonicalization=off"/ -=== TEST 32: flag directive - wasmtime - cranelift_opt_level - none +=== TEST 36: flag directive - wasmtime - cranelift_opt_level - none --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -555,7 +659,7 @@ qr/setting flag: "cranelift_opt_level=none"/ -=== TEST 33: flag directive - wasmtime - cranelift_opt_level - speed +=== TEST 37: flag directive - wasmtime - cranelift_opt_level - speed --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -571,7 +675,7 @@ qr/setting flag: "cranelift_opt_level=speed"/ -=== TEST 34: flag directive - wasmtime - cranelift_opt_level - speed_and_size +=== TEST 38: flag directive - wasmtime - cranelift_opt_level - speed_and_size --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -587,7 +691,7 @@ qr/setting flag: "cranelift_opt_level=speed_and_size"/ -=== TEST 35: flag directive - wasmtime - profiler - none +=== TEST 39: flag directive - wasmtime - profiler - none --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -603,7 +707,7 @@ qr/setting flag: "profiler=none"/ -=== TEST 36: flag directive - wasmtime - profiler - jitdump +=== TEST 40: flag directive - wasmtime - profiler - jitdump Linux-only: jitdump support --- skip_eval: 4: ($::nginxV !~ m/wasmtime/ || $::osname !~ /linux/) --- main_config @@ -620,7 +724,7 @@ qr/setting flag: "profiler=jitdump"/ -=== TEST 37: flag directive - wasmtime - profiler - vtune +=== TEST 41: flag directive - wasmtime - profiler - vtune --- SKIP: vtune disabled at compile time in release builds --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config @@ -637,7 +741,7 @@ qr/setting flag: "profiler=vtune"/ -=== TEST 38: flag directive - wasmtime - profiler - perfmap +=== TEST 42: flag directive - wasmtime - profiler - perfmap Linux-only: perfmap support --- skip_eval: 4: ($::nginxV !~ m/wasmtime/ || $::osname !~ /linux/) --- main_config @@ -654,7 +758,7 @@ qr/setting flag: "profiler=perfmap"/ -=== TEST 39: flag directive - wasmtime - static_memory_maximum_size +=== TEST 43: flag directive - wasmtime - static_memory_maximum_size --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -670,7 +774,7 @@ qr/setting flag: "static_memory_maximum_size=10m"/ -=== TEST 40: flag directive - wasmtime - static_memory_guard_size +=== TEST 44: flag directive - wasmtime - static_memory_guard_size --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm { @@ -686,7 +790,7 @@ qr/setting flag: "static_memory_guard_size=4000m"/ -=== TEST 41: flag directive - wasmtime - dynamic_memory_guard_size +=== TEST 45: flag directive - wasmtime - dynamic_memory_guard_size --- skip_eval: 4: $::nginxV !~ m/wasmtime/ --- main_config wasm {