Skip to content

Commit 063e7e5

Browse files
committed
Support restore with -XX:CRaCRestoreFrom=PATH
Signed-off-by: Jason Feng <[email protected]>
1 parent 5875c62 commit 063e7e5

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

closed/src/java.base/share/native/libjli/criuhelpers.c

+28-34
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#include "criuhelpers.h"
2626

2727
#if defined(J9VM_OPT_CRAC_SUPPORT)
28+
29+
#include "java.h"
30+
#include <ctype.h>
31+
#include <sys/wait.h>
32+
2833
/**
2934
* Get the value of a command line option in the array of command line arguments.
3035
* @param[in] optionName The name of the command line option to search for
@@ -37,7 +42,7 @@ static const char *
3742
getCommandLineOptionValue(const char *optionName, int argc, char **argv, int *error)
3843
{
3944
const char *value = NULL;
40-
int i = argc - 1;
45+
int i = 0;
4146
size_t optionNameLength = strlen(optionName);
4247
*error = -1;
4348
for (i = argc - 1; i >= 0; i--) {
@@ -85,6 +90,7 @@ getCheckpointDirectory(int argc, char **argv, int *error)
8590

8691
/**
8792
* Get the log level specified in the command line arguments.
93+
* Valid log levels are from 0 to 4, inclusive; the default is 2.
8894
* @param[in] argc The number of command line arguments
8995
* @param[in] argv The array of command line arguments
9096
* @param[in/out] error A pointer to an integer for the error code
@@ -94,8 +100,7 @@ static int
94100
getLogLevel(int argc, char **argv, int *error)
95101
{
96102
int logLevelValue = 2; /* default */
97-
const char *logLevelPropertyValue = NULL;
98-
logLevelPropertyValue = getCommandLineOptionValue("-Dopenj9.internal.criu.logLevel", argc, argv, error);
103+
const char *logLevelPropertyValue = getCommandLineOptionValue("-Dopenj9.internal.criu.logLevel", argc, argv, error);
99104
if (0 == *error) {
100105
const char *c = logLevelPropertyValue;
101106
if (NULL == c) {
@@ -116,7 +121,9 @@ getLogLevel(int argc, char **argv, int *error)
116121
goto done;
117122
}
118123
setLogLevelOptionValueNotValidError:
119-
JLI_ReportErrorMessage("The value of the command line option is not valid, option=-Dopenj9.internal.criu.logLevel, value=%s.", logLevelPropertyValue);
124+
JLI_ReportErrorMessage(
125+
"The option '-Dopenj9.internal.criu.logLevel=%s' is not valid.",
126+
logLevelPropertyValue);
120127
*error = -2;
121128
done:
122129
return logLevelValue;
@@ -138,7 +145,9 @@ isUnprivilegedModeOn(int argc, char **argv, int *error)
138145
if (NULL == unprivilegedModePropertyValue) {
139146
isUnprivilegedModeOn = JNI_TRUE;
140147
} else {
141-
JLI_ReportErrorMessage("The value of the command line option is not expected, option=-Dopenj9.internal.criu.unprivilegedMode, value=%s.", unprivilegedModePropertyValue);
148+
JLI_ReportErrorMessage(
149+
"The option '-Dopenj9.internal.criu.unprivilegedMode=%s' does not accept a value.",
150+
unprivilegedModePropertyValue);
142151
*error = -2;
143152
}
144153
}
@@ -161,7 +170,7 @@ getLogFile(int argc, char **argv, int *error)
161170
if (NULL != logFilePropertyValue) {
162171
logFile = logFilePropertyValue;
163172
} else {
164-
JLI_ReportErrorMessage("The value of the command line option -Dopenj9.internal.criu.logFile was not found.");
173+
JLI_ReportErrorMessage("The option -Dopenj9.internal.criu.logFile requires a value.");
165174
*error = -2;
166175
}
167176
}
@@ -179,7 +188,6 @@ getLogFile(int argc, char **argv, int *error)
179188
static int
180189
restoreFromCheckpoint(const char *checkpointDirectory, int logLevel, jboolean unprivilegedModeOn, const char *logFile)
181190
{
182-
int restoreStatus = 0;
183191
int length = -1;
184192
char logLevelOption[4] = { 0 };
185193
char *logFileOption = NULL;
@@ -189,48 +197,34 @@ restoreFromCheckpoint(const char *checkpointDirectory, int logLevel, jboolean un
189197
argv[argc++] = "restore";
190198
argv[argc++] = "-D";
191199
argv[argc++] = checkpointDirectory;
192-
length = snprintf(NULL, 0, "-v%d", logLevel);
193-
if (length < 0) {
194-
JLI_ReportErrorMessage("Failed to calculate the length of option '-v%d'.", logLevel);
195-
restoreStatus = -1;
196-
goto done;
197-
}
198-
if (snprintf(logLevelOption, sizeof(logLevelOption), "-v%d", logLevel) < 0) {
199-
JLI_ReportErrorMessage("Failed to format the log level option '-v%d'.", logLevel);
200-
restoreStatus = -1;
201-
goto done;
202-
}
200+
/* The log level is a single digit. */
201+
snprintf(logLevelOption, sizeof(logLevelOption), "-v%d", logLevel);
203202
argv[argc++] = logLevelOption;
204203
argv[argc++] = "--shell-job";
205204
if (unprivilegedModeOn) {
206205
argv[argc++] = "--unprivileged";
207206
}
208207
if (NULL != logFile) {
209208
length = strlen(logFile) + sizeof("--log-file=%s") - 1;
210-
logFileOption = (char *)JLI_MemAlloc(length - 1);
209+
logFileOption = (char *)JLI_MemAlloc(length + 1);
211210
if (NULL == logFileOption) {
212211
JLI_ReportErrorMessage("Failed to allocate memory for option '--log-file=%s'.", logFile);
213-
restoreStatus = -1;
214-
goto done;
212+
goto fail;
215213
}
216-
if (snprintf(logFileOption, length - 1, "--log-file=%s", logFile) < 0) {
214+
if (snprintf(logFileOption, length + 1, "--log-file=%s", logFile) < 0) {
217215
JLI_ReportErrorMessage("Failed to format option '--log-file=%s'.", logFile);
218-
restoreStatus = -1;
219-
goto done;
216+
goto fail;
220217
}
221218
argv[argc++] = logFileOption;
222219
}
223220
argv[argc] = NULL;
224221
execvp(argv[0], (char * const *)argv);
225222
/* If execvp returns, there was an error. */
226-
restoreStatus = -1;
227-
freeLogFileOption:
223+
fail:
228224
if (NULL != logFileOption) {
229225
JLI_MemFree((void *)logFileOption);
230-
logFileOption = NULL;
231226
}
232-
done:
233-
return restoreStatus;
227+
return -1;
234228
}
235229

236230
/**
@@ -252,12 +246,12 @@ handleCRaCRestore(int argc, char **argv)
252246
int childProcessPidStatus = 0;
253247
int childProcessPidExitStatus = 0;
254248
checkpointDirectory = getCheckpointDirectory(argc, argv, &error);
255-
/* We ignore restore if the restore option -XX:CRaCRestoreFrom is not specified. */
256249
if (-1 == error) {
250+
/* Option -XX:CRaCRestoreFrom not specified. */
257251
return;
258252
}
259253
if (-2 == error) {
260-
JLI_ReportErrorMessage("Failed to get the CRIU checkpoint directory, error=%d.", error);
254+
JLI_ReportErrorMessage("Failed to get the CRIU checkpoint directory.");
261255
parentProcessExitStatus = EXIT_FAILURE;
262256
goto doneParentProcess;
263257
}
@@ -269,19 +263,19 @@ handleCRaCRestore(int argc, char **argv)
269263
if (0 == childProcessPid) {
270264
logLevel = getLogLevel(argc, argv, &error);
271265
if (-2 == error) {
272-
JLI_ReportErrorMessage("Failed to get the CRIU log level, error=%d.", error);
266+
JLI_ReportErrorMessage("Failed to get the CRIU log level.");
273267
childProcessExitStatus = EXIT_FAILURE;
274268
goto doneChildProcess;
275269
}
276270
unprivilegedModeOn = isUnprivilegedModeOn(argc, argv, &error);
277271
if (-2 == error) {
278-
JLI_ReportErrorMessage("Failed to get the CRIU unprivileged mode, error=%d.", error);
272+
JLI_ReportErrorMessage("Failed to get the CRIU unprivileged mode.");
279273
childProcessExitStatus = EXIT_FAILURE;
280274
goto doneChildProcess;
281275
}
282276
logFile = getLogFile(argc, argv, &error);
283277
if (-2 == error) {
284-
JLI_ReportErrorMessage("Failed to get the CRIU log file, error=%d.", error);
278+
JLI_ReportErrorMessage("Failed to get the CRIU log file.");
285279
childProcessExitStatus = EXIT_FAILURE;
286280
goto doneChildProcess;
287281
}

closed/src/java.base/share/native/libjli/criuhelpers.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@
2525
#ifndef CRIUHELPERS_H
2626
#define CRIUHELPERS_H
2727

28-
#include "java.h"
2928
#include "j9cfg.h"
3029

3130
#if defined(J9VM_OPT_CRAC_SUPPORT)
32-
#include <ctype.h>
33-
#include <sys/wait.h>
34-
void
35-
handleCRaCRestore(int argc, char **argv);
31+
void handleCRaCRestore(int argc, char **argv);
3632
#endif /* defined(J9VM_OPT_CRAC_SUPPORT) */
3733

3834
#endif /* CRIUHELPERS_H */

src/java.base/share/native/libjli/java.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,12 @@ JavaMain(void* _args)
567567
jfieldID noArgMainField;
568568
jboolean noArgMain;
569569

570-
RegisterThread();
571-
572570
#if defined(J9VM_OPT_CRAC_SUPPORT)
573571
handleCRaCRestore(argc, argv);
574572
#endif /* defined(J9VM_OPT_CRAC_SUPPORT) */
575573

574+
RegisterThread();
575+
576576
/* Initialize the virtual machine */
577577
start = CurrentTimeMicros();
578578
if (!InitializeJVM(&vm, &env, &ifn)) {

0 commit comments

Comments
 (0)