Saving and loading "state"

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
Cyrille de Brébisson
Posts: 3
Joined: Wed May 30, 2018 7:46 am

Saving and loading "state"

Post by Cyrille de Brébisson » Fri Jun 01, 2018 11:50 am

Hello,

Is there a way to load/save the current python machine state?

I am in a system where I have an interractive evaluation loop (repl)...
But I might need to interrupt and restart said evaluation (basically save state to stream, load state from strem).
How would I do that?

Thanks,
Cyrille

jickster
Posts: 227
Joined: Thu Sep 07, 2017 8:57 pm

Re: Saving and loading "state"

Post by jickster » Fri Jun 01, 2018 4:32 pm

Cyrille de Brébisson wrote:
Fri Jun 01, 2018 11:50 am
Hello,

Is there a way to load/save the current python machine state?

I am in a system where I have an interractive evaluation loop (repl)...
But I might need to interrupt and restart said evaluation (basically save state to stream, load state from strem).
How would I do that?

Thanks,
Cyrille
The state is composed of two things: the heap and

Code: Select all

mp_state_ctx_t mp_state_ctx;
You'd have to save/restore those two items but it's actually more complicated because the state contains pointers to the heap.
In the case that the location of the uPy heap is not the same location (or same size) as when it was saved, you have to modify some internal pointers from mp_state_ctx_t mp_state_ctx;:

Code: Select all

MP_STATE_MEM(gc_alloc_table_start), MP_STATE_MEM(gc_finaliser_table_start), MP_STATE_MEM(gc_pool_start) = (byte*)end, MP_STATE_MEM(gc_pool_end) = end


Very messy.

There should definitely be an API that abstracts all this messiness but if you want to try, see gc_init()

I created an enhancement request https://github.com/micropython/micropython/issues/3833


Simple case - size of heap doesn't change
If you're ok keeping the heap size the same after you restore, it becomes much simpler:

Save (to flash) steps
1. save the heap to flash
2. save mp_state_ctx_t mp_state_ctx to flash

Restore steps
1. allocate heap in RAM
2. restore mp_state_ctx_t mp_state_ctx from flash
3. call gc_init(void *start, void *end) with pointer ranges for the heap from (1)
mp_state_ctx is updated with correct addresses for new heap-in-RAM
and heap-in-RAM is reset
4. copy heap in flash to heap in RAM
Restores the heap as it was before

jickster
Posts: 227
Joined: Thu Sep 07, 2017 8:57 pm

Re: Saving and loading "state"

Post by jickster » Fri Jun 01, 2018 8:59 pm

Cyrille de Brébisson wrote:
Fri Jun 01, 2018 11:50 am
Hello,

Is there a way to load/save the current python machine state?

I am in a system where I have an interractive evaluation loop (repl)...
But I might need to interrupt and restart said evaluation (basically save state to stream, load state from strem).
How would I do that?

Thanks,
Cyrille
Looks like it's only possible in very narrow circumstance.
Restoring the heap to a different location is basically the same problem as supporting a precise GC. So basically I think it would only work if being restored to same location and size.

The other big issue is the state of the hardware and all of the peripherals.

And you'd probably need to save the stack as well, although that may not be required if saving the state is only allowed from certain locations.

The heap may also contain pointers back into flash, so you wouldn't be able to save the state, update some firmware and restore the state.
https://github.com/micropython/micropython/issues/3833

Post Reply