Snapshotting runtime

115 views
Skip to first unread message

Александр Гурьянов

unread,
May 10, 2019, 7:33:05 AM5/10/19
to emscripte...@googlegroups.com
Hi is it possible to make snapshot of runtime, serelize it to file and
then restore runtime on other client. I understand that snapshotting
have a lot of pitfalls. But for now I want to implement simpliest
case. My target is wasm, main loop is executed by requestAnimation
frame.

So stack always same:

request animation frame -> loop function -> create/resotre memory ->
actual programm implementation

The first code in loop function is to restore/save heap, like this:

static void em_main_loop(void) {
if (doHeapOperation()) {
return;
}

// actual programm implementation
}

As heapOperation I use this:

Module.heapOperation = function() {
delete Module.heapOperation;
var buffer = new ArrayBuffer(Module.HEAPU8.byteLength);
window.heapCopy = new Uint8Array(buffer);
window.heapCopy.set(Module.HEAPU8);
console.log("INFO: heapCopy created");

function restore() {
Module.heapOperation = function() {
delete Module.heapOperation;
Module.HEAPU8.set(window.heapCopy);
console.log("INFO: heapCopy loaded");
}
};

setTimeout(restore, 1000);
}

So, I just saving all wasm memory, and then restore it after 1 sec.
And this code works in 50% cases, sometimes program is start as 1
second before, but other time it continue with unpredictable
behaviour.

Because we paused / resume with same memory and same stack I assume
that everything should works. But, in reality it does not. Is
emscripten also save some state in javascript? What I am missing.
Assume that my program did not use FS, GPU, SOUNDS and pure native (no
EM_ASM inside), is this technique should works?

Alon Zakai

unread,
May 11, 2019, 4:45:27 PM5/11/19
to emscripte...@googlegroups.com
That could work in theory - but the JS state is a big problem, as you said. If anything in JS closes over state, or otherwise creates data you can't serialize, it won't work. Emscripten does save some state in JS, some obvious examples are the things you said you are not using (like FS). But if you use the WebGL support for example then you also have a bunch of JS objects (like WebGL buffers) that represent browser state that can't be serialized.

The wasm part is much simpler, but aside from the wasm memory which is easy to serialize, you also need to save the globals (like the stack pointer). Those aren't externally visible or modifiable, so you'd need to add methods to save and load them.



--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAKOm%3DVHBPUGg7%3DbWSTT0b_%2BKy5TAv6LvHk2Kc7Un3udY6%2BGTLQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Liam Wilson

unread,
May 12, 2019, 11:31:39 AM5/12/19
to emscripten-discuss
A couple of years ago was playing around with something similar with Emscripten in asm.js mode. I was able to snapshot/restore program state by saving the contents of the heap to another array, and also saving STACKTOP. I also had to replace _malloc and _free as there seemed to be some internal state in there too.


Thanks
Liam Wilson

Александр Гурьянов

unread,
May 14, 2019, 6:56:21 AM5/14/19
to emscripte...@googlegroups.com
Thanks for feedback. Now I am saving and restoring all wasm mutable
globals. But seems it does not help, I still have unstable behaviour.
I forgot to say that I use emterpeter too, can it produce additional
problem for save/restore wasm state?

вс, 12 мая 2019 г. в 22:31, Liam Wilson <cosinus...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/ca1ab65c-d418-4ba6-89b8-8f2fc08501f1%40googlegroups.com.

Alon Zakai

unread,
May 14, 2019, 11:56:27 AM5/14/19
to emscripte...@googlegroups.com
The EmterpreterAsync JS object does have a few fields, yes. I think they only matter during an async event though.

To debug this kind of thing, the DETERMINISTIC option may be useful. It makes all timing and random numbers deterministic, which for many programs (without user input, and single-threaded) means it is deterministic, so you can re-run the same reloaded program to debug it.

Александр Гурьянов

unread,
May 15, 2019, 4:05:24 AM5/15/19
to emscripte...@googlegroups.com
Thanks a lot. Now everything seems working. Changes that helps me:
1. Do save/restore operation only if EmterpreterAsync.state === 0 (normal state)
2. Reset some variables that depends on current time

вт, 14 мая 2019 г. в 22:56, Alon Zakai <alon...@gmail.com>:
> To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAEX4NpTm0pkGNvRuzH42cNUzEPnpCEkrb7xfk_ArW3uaefPKEQ%40mail.gmail.com.

Grant Nestor

unread,
Jun 24, 2019, 11:55:19 AM6/24/19
to emscripten-discuss
@caiiycuk, can you provide a summary of how you got this working or a link to a working example? I've done some experimentation but no luck so far...
>> > To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
>> > To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/ca1ab65c-d418-4ba6-89b8-8f2fc08501f1%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAKOm%3DVFttict2uVoLWTTdeScUFjeu8FFhFif%3DJSm%3Djvd4%3DFdkA%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Александр Гурьянов

unread,
Jun 27, 2019, 11:41:37 AM6/27/19
to emscripte...@googlegroups.com
Hi, It works for me when I save wasm memory and all global variables.
Then I restore them. Unfortunately I can't show code because of NDA.
Can you explain what did you try?

пн, 24 июн. 2019 г. в 22:55, Grant Nestor <grant...@gmail.com>:
>> >> > To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
>> >> > To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/ca1ab65c-d418-4ba6-89b8-8f2fc08501f1%40googlegroups.com.
>> >> > For more options, visit https://groups.google.com/d/optout.
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
>> >> To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/CAKOm%3DVFttict2uVoLWTTdeScUFjeu8FFhFif%3DJSm%3Djvd4%3DFdkA%40mail.gmail.com.
>> >> For more options, visit https://groups.google.com/d/optout.
>> >
>> > --
>> > You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
>> > To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/b0b4a6cc-c250-4327-8eef-e77eda604163%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages