-
Notifications
You must be signed in to change notification settings - Fork 1
/
CheckpointRestore.java
132 lines (107 loc) · 3.51 KB
/
CheckpointRestore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class CheckpointRestore {
public static CheckpointRestore CRContext;
private static List<Hook> CheckpointHooks;
private static List<Hook> RestoreHooks;
public static void CleanupTheWorld() {
System.gc();
System.gc();
}
private native void CheckTheWorldNative();
private native void SaveTheWorldNative(String dir);
private native void RestoreTheWorldNative(String dir);
public static native void MigrateTheWorld();
public static native void SaveTheWorldIncremental();
public static void CheckTheWorld() {
CRContext.CheckTheWorldNative();
}
public static void SaveTheWorld(String dir) {
for (Hook h : CheckpointHooks) {
h.run();
}
WriteRestoreHooks(dir);
CRContext.SaveTheWorldNative(dir);
}
public static void WriteRestoreHooks(String dir) {
try {
File outputDir = new File(dir);
outputDir.mkdir();
File file = new File(dir, "/JavaRestoreHooks.txt");
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream o = new ObjectOutputStream(f);
for (Hook h : RestoreHooks) {
o.writeObject(h);
}
o.close();
f.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error initializing stream: " + e.getMessage());
}
}
public static void ReadRestoreHooks(String dir) {
try {
FileInputStream f = new FileInputStream(new File(dir + "/JavaRestoreHooks.txt"));
ObjectInputStream o = new ObjectInputStream(f);
while (true) {
Hook h = (Hook) o.readObject();
RestoreHooks.add(h);
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (EOFException e) {
// This always happens.
// It's ugly, exceptions should be exceptional.
// Is there a better way?
} catch (IOException e) {
System.out.println("Error initializing stream:" + e.getMessage());
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void RestoreTheWorld(String dir) {
CRContext.RestoreTheWorldNative(dir);
ReadRestoreHooks(dir);
for (Hook h : RestoreHooks) {
h.run();
}
}
public static void RegisterCheckpointHook(Hook h) {
CRContext.CheckpointHooks.add(h);
}
public static void RegisterRestoreHook(Hook h) {
CRContext.RestoreHooks.add(h);
}
public static void DebugPrint(String s) {
if (false) {
System.out.println(s);
}
}
static {
DebugPrint("Library path = " + System.getProperty("java.library.path"));
DebugPrint("About to load Checkpoint Restore library " + System.mapLibraryName("CheckpointRestore"));
DebugPrint("About to load criu library " + System.mapLibraryName("criu"));
DebugPrint("Before call to load CheckpointRestore");
System.loadLibrary("CheckpointRestore");
DebugPrint("After call to load CheckpointRestore");
System.loadLibrary("criu");
DebugPrint("After call to load criu");
CheckpointHooks = new ArrayList<Hook>();
RestoreHooks = new ArrayList<Hook>();
CRContext = new CheckpointRestore();
}
}