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

Instruction metering #3927

Open
AlixANNERAUD opened this issue Nov 23, 2024 · 2 comments · May be fixed by #4122
Open

Instruction metering #3927

AlixANNERAUD opened this issue Nov 23, 2024 · 2 comments · May be fixed by #4122

Comments

@AlixANNERAUD
Copy link

AlixANNERAUD commented Nov 23, 2024

Feature

Similar to the Wasmer runtime, it would be beneficial to implement instruction metering, allowing control to be handed back to the caller after executing a specified number of instructions.

Benefit

This would enable on platform with a cooperative scheduler to manage how WASM code is executed, preventing system overload.

Implementation

Introduce a function like wasm_func_call_with_metering that returns an after a given number of executed instruction, a boolean indicating whether the function reached its end.

Alternatives

None identified.

@lum1n0us
Copy link
Collaborator

Link similar things together. #2163

@AlixANNERAUD
Copy link
Author

AlixANNERAUD commented Feb 20, 2025

Passing the number of instructions to be executed seems to be the right approach. However, after inspecting the call stack of wasm_runtime_call_wasm, we need to propagate this "instruction count" down to the following functions:

  • For the interpreter:
    1. wasm_call_function
    2. interp_call_wasm (a.k.a. wasm_interp_call_wasm / call_wasm_with_hw_bound_check)
    3. wasm_interp_call_func_bytecode

In this final function, we can use the instruction count to exit the interpreter loop after executing N instructions and return (by propagating an additional variable to the initial call). However, I’m not exactly sure where the loop condition is checked within this goto mess.

  • For AOT:
    I'm not entirely sure, but I believe this concerns the compilation process, where a "return" statement must be added every N instructions ? (maybe far less du to branching).

@AlixANNERAUD AlixANNERAUD linked a pull request Mar 2, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants