-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Feature
To be able to interrupt and save the state of executing WASM code and then resume it later, possibly on a different host machine.
Benefit
I am writing an add on for the Open Computers mod in Minecraft. The mod adds tiny computers that can be programmed by the player. One of their features is that if you quit the game, when you resume later, the state of the machine is restored just as they left it.
The default Lua interpreter uses a custom implementation of Lua that can save and restore its state like this.
There's a small hand full of emulators that just save their memory and registers and restore them later.
I don't see a clear way to do this with wasmtime.
I do see potential in this being useful for a tool similar to Jupyter. You could pause a computationally intensive task and resume it later, or a cloud could serialize a job, and send it to another machine to resume in the case of a scale up/down.
Implementation
I can see that modules can be serialized already. What really needs to be saved is the state of memory and execution.
Just dumping the memory into a byte array will be enough to save the memory. Saving the state is what looks hard to me.
Dumping the registers to be restored later could work for a non-portable solution but something that can be restored on another machine of possibly a different architecture would be preferred.
Unfortunately I don't know enough about wasmtime's implementation to give a fantastic recommendation here.
Alternatives
This is actually my fallback plan if the feature is rejected.
If you can just serialize the memory of the WASM environment (actually this may already be possible, I just haven't tried yet) you could push the job of tracking state off onto a Rust module's async state machine. You would need a function that is called regularly to resume this state machine and the module would need to frequently interrupt itself so that this function can return to the host.
The advantage here is that you don't need to worry about any kind of register. Saving the state is handled by the async state machine and you can just call that one function to run the state machine to resume the guest application.
The disadvantage is that this is really only practical if the web assembly is written in Rust, and the person writing this Rust code must correctly write an async application.