Restoring With New Arguments
In most cases, you do not need to provide arguments to the JVM when restoring because the restored execution will already be based on the arguments specified before checkpoint. For example:
# Start a JVM to be checkpointed:
java -XX:CRaCCheckpointTo=crac-image MyMainClass arg1 arg2
# Restore from a checkpoint; the old arguments are "baked in"
# so we don't need to specify them again:
java -XX:CRaCRestoreFrom=crac-image
However, CRaC allows you to provide a new main class and a new set of arguments for it, in case you wish to alter the execution after restore:
# Restore from a checkpoint with new arguments
java -XX:CRaCRestoreFrom=crac-image MyNewMainClass newArg1 newArg2
When you restore the application with the new main class and new arguments, after all CRaC resources have been successfully processed, the thread which initiated the checkpoint finds the static void main(String[]) method in MyNewMainClass and invokes it with the provided set of arguments (which can be empty) on top of its existing call stack. After this new main method completes, the thread will proceed with executing the code placed after the checkpoint call.
|
Note
|
Since you can’t extend the class path on restore, the new main class is required to be available on the class path defined before the checkpoint is created. It is also not possible to use the -jar app.jar syntax on restore to launch the main class from a JAR file. These limitations may be lifted in the future.
|
|
Note
|
When you create a checkpoint via jcmd, the single thread that executes all diagnostic commands will be the one executing the new main. As a result, new commands will be blocked from being executed until the new main completes. Thus, when using new arguments, it is recommended to either initiate the checkpoint on an application thread or start a new thread from the new main.
|
Example: CRaC-ing the Java Compiler
The Zulu CRaC distributions contain a demo of a CRaC-ed javac based on the restore arguments feature in <java-dir>/demo/crac/JavaCompilerCRaC. There, you can find an executable JAR and the source code to understand how this works.
This is how the CRaC-ed javac can be used:
-
Implement some Java test classes to be compiled, suppose they are
MyClass1.javaandMyClass2.java. -
Warm-up the CRaC-ed
javacby compilingMyClass1.javaand checkpoint it:java -XX:CRaCCheckpointTo=javac-image \ -jar <java-dir>/demo/crac/JavaCompilerCRaC/JavaCompilerCRaC.jar \ MyClass1.javaAt this point, the default main class of
JavaCompilerCRaC.jaris invoked withMyClass1.javaas an argument. The default main class invokes the standardjavacto compileMyClass1.javaand then initiates a checkpoint. -
Restore and use the warmed-up CRaC-ed
javacto compileMyClass2.java:java -XX:CRaCRestoreFrom=javac-image \ Compile MyClass2.java-
At this point, we specify a class named
Compileand contained inJavaCompilerCRaC.jaras the new main class andMyClass2.javaas an argument for its main method. -
Compileinvokes the standardjavac, which was warmed up before checkpoint, to compileMyClass2.javaand completes. -
The execution then returns to the default main class of
JavaCompilerCRaC.jarwhich also completes and thus the restored JVM exits.
-