checkpoint-restore / criu

Checkpoint/Restore tool
criu.org
Other
2.76k stars 560 forks source link

How do I detect if the current process has restored from a checkpoint? #2354

Closed liuaifu closed 4 months ago

liuaifu commented 4 months ago

I use criu to dump and restore my app, and everything works. But my program needs to perform some specific actions when it is restored. How can I detect the event that is restored? Thank you.

#include <iostream>
#include <thread>
#include <chrono>

bool is_restored_from_checkpoint() {
    // ......
    return false;
}

int main(){
    int n = 0;
    while(n < 3600) {
        if(is_restored_from_checkpoint()) {
            std::cout << "restored from checkpoint." << std::endl;
            // do something
        }

        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::cout << n++ << std::endl;
    }
}
rst0git commented 4 months ago

@liuaifu you can use an action-script to create a file that indicates the current process has been restored. Then is_restored_from_checkpoint() could return true if this file exists and false otherwise.

liuaifu commented 4 months ago

Got it. Thank you.

Snorch commented 4 months ago

There is actually one more option I recently found, it's with external file descriptors "debug" mode https://criu.org/Inheriting_FDs_on_restore#Debugging_aid

Upd: sorry, no --inherit-fd 'debug[0]:test' works a bit differently, it writes debug string into file descriptor of criu process.