Skip to content

Commit 37b2cbf

Browse files
authored
Merge pull request #21319 from JasonFengJ9/criusingleton-0.51
(0.51) CRIUSupport supports only one singleton instance
2 parents 0b1a3eb + c6c4409 commit 37b2cbf

File tree

10 files changed

+169
-141
lines changed

10 files changed

+169
-141
lines changed

jcl/src/java.base/share/classes/jdk/crac/Core.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public static void checkpointRestore() throws CheckpointException, RestoreExcept
7373
class CRIUSupportContext<R extends Resource> extends Context<R> {
7474
// InternalCRIUSupport.getCRaCCheckpointToDir() is not null if
7575
// InternalCRIUSupport.isCRaCSupportEnabled() returns true before creating CRIUSupportContext<>().
76-
private final InternalCRIUSupport internalCRIUSupport = new InternalCRIUSupport(
77-
Paths.get(InternalCRIUSupport.getCRaCCheckpointToDir()))
76+
private final InternalCRIUSupport internalCRIUSupport = InternalCRIUSupport.getInternalCRIUSupport()
77+
.setImageDir(Paths.get(InternalCRIUSupport.getCRaCCheckpointToDir()))
7878
.setLeaveRunning(false)
7979
.setShellJob(true)
8080
.setTCPEstablished(true)

jcl/src/java.base/share/classes/openj9/internal/criu/InternalCRIUSupport.java

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,55 @@ public final class InternalCRIUSupport {
8686
private static native String getCRaCCheckpointToDirImpl();
8787
/*[ENDIF] CRAC_SUPPORT */
8888

89+
/**
90+
* A singleton {@code InternalCRIUSupport} instance.
91+
*
92+
* The default CRIU dump options are:
93+
* <p>
94+
* {@code imageDir} = CWD, current Java process working directory.
95+
* <p>
96+
* {@code leaveRunning} = false
97+
* <p>
98+
* {@code shellJob} = false
99+
* <p>
100+
* {@code extUnixSupport} = false
101+
* <p>
102+
* {@code logLevel} = 2
103+
* <p>
104+
* {@code logFile} = criu.log
105+
* <p>
106+
* {@code fileLocks} = false
107+
* <p>
108+
* {@code ghostFileLimit} = 1 MB
109+
* <p>
110+
* {@code workDir} = imageDir, the directory where the images are to be created.
111+
*/
112+
private static final InternalCRIUSupport singletonInternalCRIUSupport = new InternalCRIUSupport();
113+
114+
// no public construtors
115+
private InternalCRIUSupport() {
116+
/*[IF JAVA_SPEC_VERSION < 24]*/
117+
SecurityManager manager = System.getSecurityManager();
118+
if (manager != null) {
119+
manager.checkPermission(CRIU_DUMP_PERMISSION);
120+
}
121+
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
122+
// use current working directory
123+
setImageDir(java.nio.file.FileSystems.getDefault().getPath("").toAbsolutePath());
124+
}
125+
126+
/**
127+
* Returns the singleton InternalCRIUSupport object.
128+
*
129+
* Most methods of class {@code InternalCRIUSupport} are instance methods and
130+
* must be invoked via this object.
131+
*
132+
* @return the singleton {@code InternalCRIUSupport} object
133+
*/
134+
public static InternalCRIUSupport getInternalCRIUSupport() {
135+
return singletonInternalCRIUSupport;
136+
}
137+
89138
/**
90139
* Retrieve the elapsed time between Checkpoint and Restore.
91140
* Only support one Checkpoint.
@@ -299,47 +348,6 @@ private static void init() {
299348
}
300349
}
301350

302-
/**
303-
* Constructs a new {@code InternalCRIUSupport}.
304-
*
305-
* The default CRIU dump options are:
306-
* <p>
307-
* {@code imageDir} = imageDir, the directory where the images are to be
308-
* created.
309-
* <p>
310-
* {@code leaveRunning} = false
311-
* <p>
312-
* {@code shellJob} = false
313-
* <p>
314-
* {@code extUnixSupport} = false
315-
* <p>
316-
* {@code logLevel} = 2
317-
* <p>
318-
* {@code logFile} = criu.log
319-
* <p>
320-
* {@code fileLocks} = false
321-
* <p>
322-
* {@code workDir} = imageDir, the directory where the images are to be created.
323-
*
324-
* @param imageDir the directory that will hold the dump files as a
325-
* java.nio.file.Path
326-
* @throws NullPointerException if imageDir is null
327-
/*[IF JAVA_SPEC_VERSION < 24]
328-
* @throws SecurityException if no permission to access imageDir or no
329-
* CRIU_DUMP_PERMISSION
330-
/*[ENDIF] JAVA_SPEC_VERSION < 24
331-
* @throws IllegalArgumentException if imageDir is not a valid directory
332-
*/
333-
public InternalCRIUSupport(Path imageDir) {
334-
/*[IF JAVA_SPEC_VERSION < 24]*/
335-
SecurityManager manager = System.getSecurityManager();
336-
if (manager != null) {
337-
manager.checkPermission(CRIU_DUMP_PERMISSION);
338-
}
339-
/*[ENDIF] JAVA_SPEC_VERSION < 24 */
340-
setImageDir(imageDir);
341-
}
342-
343351
/**
344352
* Queries if the criu library has been loaded.
345353
*

jcl/src/openj9.criu/share/classes/org/eclipse/openj9/criu/CRIUSupport.java

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,12 @@ public static enum HookMode {
5454
CONCURRENT_MODE
5555
}
5656

57-
private InternalCRIUSupport internalCRIUSupport;
58-
5957
/**
60-
* Constructs a new {@code CRIUSupport}.
58+
* A singleton {@code CRIUSupport} instance.
6159
*
6260
* The default CRIU dump options are:
6361
* <p>
64-
* {@code imageDir} = imageDir, the directory where the images are to be
65-
* created.
62+
* {@code imageDir} = CWD, current Java process working directory.
6663
* <p>
6764
* {@code leaveRunning} = false
6865
* <p>
@@ -79,6 +76,30 @@ public static enum HookMode {
7976
* {@code ghostFileLimit} = 1 MB
8077
* <p>
8178
* {@code workDir} = imageDir, the directory where the images are to be created.
79+
*/
80+
private static final CRIUSupport singletonCRIUSupport = new CRIUSupport();
81+
82+
private static final InternalCRIUSupport singletonInternalCRIUSupport = InternalCRIUSupport
83+
.getInternalCRIUSupport();
84+
85+
// no public construtors
86+
private CRIUSupport() {
87+
}
88+
89+
/**
90+
* Returns the singleton CRIUSupport object.
91+
*
92+
* Most methods of class {@code CRIUSupport} are instance methods and must be
93+
* invoked via this object.
94+
*
95+
* @return the singleton {@code CRIUSupport} object
96+
*/
97+
public static CRIUSupport getCRIUSupport() {
98+
return singletonCRIUSupport;
99+
}
100+
101+
/**
102+
* Constructs a new {@code CRIUSupport}.
82103
*
83104
* @param imageDir the directory that will hold the dump files as a
84105
* java.nio.file.Path
@@ -89,8 +110,9 @@ public static enum HookMode {
89110
/*[ENDIF] JAVA_SPEC_VERSION < 24
90111
* @throws IllegalArgumentException if imageDir is not a valid directory
91112
*/
113+
@Deprecated(forRemoval=true)
92114
public CRIUSupport(Path imageDir) {
93-
internalCRIUSupport = new InternalCRIUSupport(imageDir);
115+
System.err.println("WARNING: CRIUSupport(imageDir) constructor is deprecated, please use CRIUSupport.getCRIUSupport() and setImageDir(imageDir)"); //$NON-NLS-1$
94116
}
95117

96118
/**
@@ -146,7 +168,7 @@ public static String getErrorMessage() {
146168
* @throws IllegalArgumentException if imageDir is not a valid directory
147169
*/
148170
public CRIUSupport setImageDir(Path imageDir) {
149-
internalCRIUSupport = internalCRIUSupport.setImageDir(imageDir);
171+
singletonInternalCRIUSupport.setImageDir(imageDir);
150172
return this;
151173
}
152174

@@ -159,7 +181,7 @@ public CRIUSupport setImageDir(Path imageDir) {
159181
* @return this
160182
*/
161183
public CRIUSupport setLeaveRunning(boolean leaveRunning) {
162-
internalCRIUSupport = internalCRIUSupport.setLeaveRunning(leaveRunning);
184+
singletonInternalCRIUSupport.setLeaveRunning(leaveRunning);
163185
return this;
164186
}
165187

@@ -172,7 +194,7 @@ public CRIUSupport setLeaveRunning(boolean leaveRunning) {
172194
* @return this
173195
*/
174196
public CRIUSupport setShellJob(boolean shellJob) {
175-
internalCRIUSupport = internalCRIUSupport.setShellJob(shellJob);
197+
singletonInternalCRIUSupport.setShellJob(shellJob);
176198
return this;
177199
}
178200

@@ -185,7 +207,7 @@ public CRIUSupport setShellJob(boolean shellJob) {
185207
* @return this
186208
*/
187209
public CRIUSupport setExtUnixSupport(boolean extUnixSupport) {
188-
internalCRIUSupport = internalCRIUSupport.setExtUnixSupport(extUnixSupport);
210+
singletonInternalCRIUSupport.setExtUnixSupport(extUnixSupport);
189211
return this;
190212
}
191213

@@ -205,7 +227,7 @@ public CRIUSupport setExtUnixSupport(boolean extUnixSupport) {
205227
* @throws IllegalArgumentException if logLevel is not valid
206228
*/
207229
public CRIUSupport setLogLevel(int logLevel) {
208-
internalCRIUSupport = internalCRIUSupport.setLogLevel(logLevel);
230+
singletonInternalCRIUSupport.setLogLevel(logLevel);
209231
return this;
210232
}
211233

@@ -220,7 +242,7 @@ public CRIUSupport setLogLevel(int logLevel) {
220242
* @throws IllegalArgumentException if logFile is null or a path
221243
*/
222244
public CRIUSupport setLogFile(String logFile) {
223-
internalCRIUSupport = internalCRIUSupport.setLogFile(logFile);
245+
singletonInternalCRIUSupport.setLogFile(logFile);
224246
return this;
225247
}
226248

@@ -233,7 +255,7 @@ public CRIUSupport setLogFile(String logFile) {
233255
* @return this
234256
*/
235257
public CRIUSupport setFileLocks(boolean fileLocks) {
236-
internalCRIUSupport = internalCRIUSupport.setFileLocks(fileLocks);
258+
singletonInternalCRIUSupport.setFileLocks(fileLocks);
237259
return this;
238260
}
239261

@@ -246,7 +268,7 @@ public CRIUSupport setFileLocks(boolean fileLocks) {
246268
* @return this
247269
*/
248270
public CRIUSupport setTCPEstablished(boolean tcpEstablished) {
249-
internalCRIUSupport = internalCRIUSupport.setTCPEstablished(tcpEstablished);
271+
singletonInternalCRIUSupport.setTCPEstablished(tcpEstablished);
250272
return this;
251273
}
252274

@@ -259,7 +281,7 @@ public CRIUSupport setTCPEstablished(boolean tcpEstablished) {
259281
* @return this
260282
*/
261283
public CRIUSupport setAutoDedup(boolean autoDedup) {
262-
internalCRIUSupport = internalCRIUSupport.setAutoDedup(autoDedup);
284+
singletonInternalCRIUSupport.setAutoDedup(autoDedup);
263285
return this;
264286
}
265287

@@ -272,7 +294,7 @@ public CRIUSupport setAutoDedup(boolean autoDedup) {
272294
* @return this
273295
*/
274296
public CRIUSupport setTrackMemory(boolean trackMemory) {
275-
internalCRIUSupport = internalCRIUSupport.setTrackMemory(trackMemory);
297+
singletonInternalCRIUSupport.setTrackMemory(trackMemory);
276298
return this;
277299
}
278300

@@ -290,7 +312,7 @@ public CRIUSupport setTrackMemory(boolean trackMemory) {
290312
* @throws IllegalArgumentException if workDir is not a valid directory
291313
*/
292314
public CRIUSupport setWorkDir(Path workDir) {
293-
internalCRIUSupport = internalCRIUSupport.setWorkDir(workDir);
315+
singletonInternalCRIUSupport.setWorkDir(workDir);
294316
return this;
295317
}
296318

@@ -303,7 +325,7 @@ public CRIUSupport setWorkDir(Path workDir) {
303325
* @return this
304326
*/
305327
public CRIUSupport setUnprivileged(boolean unprivileged) {
306-
internalCRIUSupport = internalCRIUSupport.setUnprivileged(unprivileged);
328+
singletonInternalCRIUSupport.setUnprivileged(unprivileged);
307329
return this;
308330
}
309331

@@ -318,7 +340,7 @@ public CRIUSupport setUnprivileged(boolean unprivileged) {
318340
* @throws UnsupportedOperationException if file limit is greater than 2^32 - 1 or negative.
319341
*/
320342
public CRIUSupport setGhostFileLimit(long limit) {
321-
internalCRIUSupport = internalCRIUSupport.setGhostFileLimit(limit);
343+
singletonInternalCRIUSupport.setGhostFileLimit(limit);
322344
return this;
323345
}
324346

@@ -331,7 +353,7 @@ public CRIUSupport setGhostFileLimit(long limit) {
331353
* @return this
332354
*/
333355
public CRIUSupport setTCPClose(boolean tcpClose) {
334-
internalCRIUSupport = internalCRIUSupport.setTCPClose(tcpClose);
356+
singletonInternalCRIUSupport.setTCPClose(tcpClose);
335357
return this;
336358
}
337359

@@ -344,7 +366,7 @@ public CRIUSupport setTCPClose(boolean tcpClose) {
344366
* @return this
345367
*/
346368
public CRIUSupport setTCPSkipInFlight(boolean tcpSkipInFlight) {
347-
internalCRIUSupport = internalCRIUSupport.setTCPSkipInFlight(tcpSkipInFlight);
369+
singletonInternalCRIUSupport.setTCPSkipInFlight(tcpSkipInFlight);
348370
return this;
349371
}
350372

@@ -365,7 +387,7 @@ public CRIUSupport setTCPSkipInFlight(boolean tcpSkipInFlight) {
365387
* @return this
366388
*/
367389
public CRIUSupport registerRestoreEnvFile(Path envFile) {
368-
internalCRIUSupport = internalCRIUSupport.registerRestoreEnvFile(envFile);
390+
singletonInternalCRIUSupport.registerRestoreEnvFile(envFile);
369391
return this;
370392
}
371393

@@ -381,7 +403,7 @@ public CRIUSupport registerRestoreEnvFile(Path envFile) {
381403
* @return this
382404
*/
383405
public CRIUSupport registerRestoreOptionsFile(Path optionsFile) {
384-
internalCRIUSupport = internalCRIUSupport.registerRestoreOptionsFile(optionsFile);
406+
singletonInternalCRIUSupport.registerRestoreOptionsFile(optionsFile);
385407
return this;
386408
}
387409

@@ -402,7 +424,7 @@ public CRIUSupport registerRestoreOptionsFile(Path optionsFile) {
402424
*/
403425
public CRIUSupport registerPostRestoreHook(Runnable hook) {
404426
try {
405-
internalCRIUSupport = internalCRIUSupport.registerPostRestoreHook(hook);
427+
singletonInternalCRIUSupport.registerPostRestoreHook(hook);
406428
} catch (openj9.internal.criu.JVMCheckpointException jce) {
407429
throw new JVMCheckpointException(jce.getMessage(), 0, jce);
408430
} catch (openj9.internal.criu.JVMRestoreException jre) {
@@ -455,7 +477,7 @@ public CRIUSupport registerPostRestoreHook(Runnable hook, HookMode mode, int pri
455477
internalMode = InternalCRIUSupport.HookMode.CONCURRENT_MODE;
456478
}
457479
try {
458-
internalCRIUSupport = internalCRIUSupport.registerPostRestoreHook(hook, internalMode, priority);
480+
singletonInternalCRIUSupport.registerPostRestoreHook(hook, internalMode, priority);
459481
} catch (openj9.internal.criu.JVMCheckpointException jce) {
460482
throw new JVMCheckpointException(jce.getMessage(), 0, jce);
461483
} catch (openj9.internal.criu.JVMRestoreException jre) {
@@ -482,7 +504,7 @@ public CRIUSupport registerPostRestoreHook(Runnable hook, HookMode mode, int pri
482504
*/
483505
public CRIUSupport registerPreCheckpointHook(Runnable hook) {
484506
try {
485-
internalCRIUSupport = internalCRIUSupport.registerPreCheckpointHook(hook);
507+
singletonInternalCRIUSupport.registerPreCheckpointHook(hook);
486508
} catch (openj9.internal.criu.JVMCheckpointException jce) {
487509
throw new JVMCheckpointException(jce.getMessage(), 0, jce);
488510
} catch (openj9.internal.criu.JVMRestoreException jre) {
@@ -535,7 +557,7 @@ public CRIUSupport registerPreCheckpointHook(Runnable hook, HookMode mode, int p
535557
internalMode = InternalCRIUSupport.HookMode.CONCURRENT_MODE;
536558
}
537559
try {
538-
internalCRIUSupport = internalCRIUSupport.registerPreCheckpointHook(hook, internalMode, priority);
560+
singletonInternalCRIUSupport.registerPreCheckpointHook(hook, internalMode, priority);
539561
} catch (openj9.internal.criu.JVMCheckpointException jce) {
540562
throw new JVMCheckpointException(jce.getMessage(), 0, jce);
541563
} catch (openj9.internal.criu.JVMRestoreException jre) {
@@ -559,7 +581,7 @@ public CRIUSupport registerPreCheckpointHook(Runnable hook, HookMode mode, int p
559581
public synchronized void checkpointJVM() {
560582
if (isCRIUSupportEnabled()) {
561583
try {
562-
internalCRIUSupport.checkpointJVM();
584+
singletonInternalCRIUSupport.checkpointJVM();
563585
} catch (openj9.internal.criu.JVMCheckpointException jce) {
564586
throw new JVMCheckpointException(jce.getMessage(), 0, jce);
565587
} catch (openj9.internal.criu.JVMRestoreException jre) {

test/functional/cmdLineTests/criu/src/org/openj9/criu/CRIUTestUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static void checkPointJVM(CRIUSupport criu, Path path, boolean deleteDir)
8686
createCheckpointDirectory(path);
8787
try {
8888
if (criu == null) {
89-
criu = new CRIUSupport(path);
89+
criu = CRIUSupport.getCRIUSupport().setImageDir(path);
9090
}
9191
showThreadCurrentTime("Performing CRIUSupport.checkpointJVM()");
9292
criu.setLogLevel(4).setLeaveRunning(false).setShellJob(true).setFileLocks(true).checkpointJVM();
@@ -108,7 +108,7 @@ public static CRIUSupport prepareCheckPointJVM(Path path) {
108108
if (CRIUSupport.isCRIUSupportEnabled()) {
109109
deleteCheckpointDirectory(path);
110110
createCheckpointDirectory(path);
111-
return (new CRIUSupport(path)).setLeaveRunning(false).setShellJob(true).setFileLocks(true);
111+
return CRIUSupport.getCRIUSupport().setImageDir(path).setLeaveRunning(false).setShellJob(true).setFileLocks(true);
112112
} else {
113113
throw new RuntimeException("CRIU is not enabled");
114114
}

0 commit comments

Comments
 (0)