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

WASM instances support resource limit #607

Closed
6 tasks
zhenjunMa opened this issue May 25, 2022 · 11 comments
Closed
6 tasks

WASM instances support resource limit #607

zhenjunMa opened this issue May 25, 2022 · 11 comments
Assignees
Labels
area/community stale wasm WebAssembly technology

Comments

@zhenjunMa
Copy link
Contributor

zhenjunMa commented May 25, 2022

1. Background

In a FaaS scenario, Layotto supports loading and running multiple wasm-based functions at the same time. WebAssembly ensures that multiple functions run in the same process without affecting each other. However, currently Layotto does not support configuring the maximum used resources for each function, so we hope to add this capability.

2. Why is this needed?

If you can set available resources (such as heap, stack, CPU, etc.) for the function, then on the one hand, you can better pre-allocate resources, and on the other hand, you can lay a solid foundation for the function platform's ability to pay on demand.

3. What can we try?

Regarding how to limit the resources that can be used by a wasm instance, the possible directions are as follows:

A. At compile time

image

View the source code of the wasm file and you will find a declaration about memory. For details, please refer to the WebAssembly spec.

The value here represents the number of pages that can be used, 1 page = 64K

For Rust, you can use the following command to set the available memory size at compile time:

cargo rustc --target wasm32-wasi -- -Clink-arg=--initial-memory=1245184 -Clink-arg=--max-memory=1310720

B. At run time

The running of wasm instances depends on the runtime. Some runtimes support setting available resources for loading and running wasm instances. As for wasmtime:

// Define the WASI functions globally on the `Config`.
let mut config = Config::new();

// cpu
config.consume_fuel(true);

// memory
config.dynamic_memory_guard_size(65536);
config.static_memory_maximum_size(65536);
config.guard_before_linear_memory(true);
config.static_memory_guard_size(65536);

// memory
config.max_wasm_stack(1);

Result:
image
image

4. Goals

This is a direction that requires long-term construction, and some features of it may not be supported at this stage or not supported perfectly. Therefore, our goal is to do it little by little, and then develop step by step.

A few sub-items I can think of for now:

  • Limit the CPU quota a function can use
  • Limit the amount of memory (heap, stack) that a function can use
  • Permissions to restrict functions, such as accessible disk files

5. Other references

  1. https://users.rust-lang.org/t/how-to-set-max-memory-limit-in-wasm/64725
  2. How to set resource limit in wasmtime-go? bytecodealliance/wasmtime-go#101

中文

一、背景

在 FaaS 场景中, Layotto 支持同时加载并运行多个以 wasm 为载体的函数。WebAssembly 可以保证多个函数虽然运行在同一个进程中,但互相之间不受影响。不过目前 Layotto 还不支持为每个函数配置最大使用的资源,因此我们希望建设这块能力。

二、意义&价值

如果能为函数设置可使用的资源(如堆、栈、CPU等),那么一方面可以更好的预分配资源,另一方面可以为函数平台按需付费等能力打好基础。

三、可尝试的方向

关于如何对 wasm 实例可使用的资源进行限制,可能尝试的方向如下:

A、编译期

image

查看 wasm 文件的源码会发现有关于 memory 的声明,详细信息可以参考 WebAssembly spec.

这里的值表示可使用的页数,1页 = 64K

以 Rust 为例,可以使用如下命令在编译期设置可使用的内存大小:

cargo rustc --target wasm32-wasi -- -Clink-arg=--initial-memory=1245184 -Clink-arg=--max-memory=1310720

B、运行期

wasm 实例的运行依赖运行时,一些运行时支持为加载运行的 wasm 实例设置可使用资源,以 wasmtime 为例:

// Define the WASI functions globally on the `Config`.
let mut config = Config::new();

// cpu
config.consume_fuel(true);

// memory
config.dynamic_memory_guard_size(65536);
config.static_memory_maximum_size(65536);
config.guard_before_linear_memory(true);
config.static_memory_guard_size(65536);

// memory
config.max_wasm_stack(1);

运行结果:
image
image

四、目标

这是一个需要长期建设的方向,现阶段不一定都能支持,比如可能只支持某些能力,或者支持的不够完善,因此我们的目标是一点点做,当前能做到什么程度就做到什么程度,后续再逐步迭代。

暂时能想到的几个子项:

  • 限制函数可使用的CPU配额
  • 限制函数可使用的内存(堆、栈)大小
  • 限制函数的权限,如可访问的磁盘文件

五、其他参考资料

  1. https://users.rust-lang.org/t/how-to-set-max-memory-limit-in-wasm/64725
  2. How to set resource limit in wasmtime-go? bytecodealliance/wasmtime-go#101
@zhenjunMa zhenjunMa added the help wanted Extra attention is needed label May 25, 2022
@rayowang
Copy link
Member

/assign

@seeflood
Copy link
Member

Cool !

@zhenjunMa zhenjunMa added the wasm WebAssembly technology label May 26, 2022
@seeflood
Copy link
Member

进展:
依赖的c文件比较多,待调研

@seeflood
Copy link
Member

seeflood commented Jul 2, 2022

action:

stack 和memory再说

@seeflood
Copy link
Member

seeflood commented Jul 9, 2022

进展:测试中

@seeflood
Copy link
Member

action:

@Mossaka
Copy link

Mossaka commented Jul 28, 2022

我们的wasm shim里面集成了一个global的wasmtime,这样或许可以解决你们的concurrency run multiple wasm module的问题。不过问题是runwasi是用rust写的,可能不太好做language interop

@Xunzhuo
Copy link
Member

Xunzhuo commented Mar 6, 2023

/good-first-issue cancel
/help-wanted cancel

@github-actions github-actions bot removed the help wanted Extra attention is needed label Mar 6, 2023
@github-actions
Copy link

github-actions bot commented Apr 6, 2023

This issue has been automatically marked as stale because it has not had recent activity in the last 30 days. It will be closed in the next 7 days unless it is tagged (pinned, good first issue or help wanted) or other activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Apr 6, 2023
@github-actions
Copy link

This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as pinned, good first issue or help wanted. Thank you for your contributions.

@rayowang rayowang reopened this Jun 17, 2023
@github-actions
Copy link

This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as pinned, good first issue or help wanted. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/community stale wasm WebAssembly technology
Projects
None yet
Development

No branches or pull requests

5 participants