Skip to content

Commit

Permalink
criu_dump() seems to work
Browse files Browse the repository at this point in the history
Signed-off-by: Elazar Gershuni <[email protected]>
  • Loading branch information
elazarg committed Sep 19, 2024
1 parent 01e6274 commit 53cff8d
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions scripts/selfcheckpoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For access(), getpid(), sleep(), etc.
#include <fcntl.h> // For open()
#include <sys/types.h>
#include <criu/criu.h>

#define CHECKPOINT_ITER 5
#define TOTAL_ITER 10

int fd = -1;

void set_criu() {
if (criu_init_opts() < 0) {
perror("CRIU init failed");
exit(EXIT_FAILURE);
}

fd = open("./criu_images", O_DIRECTORY);
if (fd < 0) {
perror("Failed to open criu_images directory");
exit(EXIT_FAILURE);
}
criu_set_images_dir_fd(fd); // Use current directory for images

criu_set_log_file("criu.log");
criu_set_log_level(4); // Log level 4 for detailed logging
criu_set_pid(getpid()); // Set PID of the process to checkpoint
criu_set_leave_running(1); // Keep the process running after dump
criu_set_service_address("/tmp/criu_service.socket");
}

int main() {
set_criu();

if (access("./criu_images", F_OK) == 0) {
printf("Restoring from checkpoint...\n");
criu_restore();
perror("CRIU restore failed");
exit(EXIT_FAILURE);
}
for (int i = 1; i <= TOTAL_ITER; i++) {
printf("Iteration: %d\n", i);
sleep(1); // Simulate long-running work

// Create a checkpoint after a certain number of iterations
if (i == CHECKPOINT_ITER) {
printf("Creating checkpoint...\n");

// Perform the checkpoint
if (criu_dump() < 0) {
perror("CRIU checkpoint failed");
exit(EXIT_FAILURE);
}

printf("Checkpoint created successfully!\n");
printf("Exiting after creating a checkpoint, run again to restore from it.\n");
exit(EXIT_SUCCESS);
}
}

printf("Loop finished.\n");
close(fd);
return 0;
}

0 comments on commit 53cff8d

Please sign in to comment.