Skip to content

Commit

Permalink
Parse -Xmso from OPENJ9_JAVA_OPTIONS for main thread
Browse files Browse the repository at this point in the history
Issue eclipse-openj9/openj9#16105

Backport of ibmruntimes/openj9-openjdk-jdk#644

Signed-off-by: Peter Shipton <Peter_Shipton@ca.ibm.com>
  • Loading branch information
pshipton committed Aug 22, 2023
1 parent c0d4f33 commit 6e51ddf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/java.base/share/native/libjli/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
Expand Down Expand Up @@ -78,6 +84,7 @@ static jboolean expectingNoDashArg = JNI_FALSE;
static size_t argsCount = 1;
static jboolean stopExpansion = JNI_FALSE;
static jboolean relaunch = JNI_FALSE;
static jboolean parsingOpenJ9Args = JNI_FALSE;

/*
* Prototypes for internal functions.
Expand Down Expand Up @@ -422,6 +429,10 @@ JLI_PreprocessArg(const char *arg, jboolean expandSourceOpt) {
return expandArg(arg);
}

if (parsingOpenJ9Args && (JLI_StrCCmp(arg, "-Xoptionsfile=") == 0)) {
return expandArgFile(arg + 14);
}

if (arg[0] != '@') {
checkArg(arg);
return NULL;
Expand Down Expand Up @@ -484,6 +495,42 @@ JLI_AddArgsFromEnvVar(JLI_List args, const char *var_name) {
return expand(args, env, var_name);
}

JNIEXPORT jboolean JNICALL
JLI_ParseOpenJ9ArgsFromEnvVar(JLI_List args, const char *var_name) {
const char *env = getenv(var_name);
jboolean result = JNI_FALSE;

/* Save the state. */
int savedFirstAppArgIndex = firstAppArgIndex;
jboolean savedExpectingNoDashArg = expectingNoDashArg;
size_t savedArgsCount = argsCount;
jboolean savedStopExpansion = stopExpansion;
jboolean savedRelaunch = relaunch;

if (NULL == env) {
return JNI_FALSE;
}

parsingOpenJ9Args = JNI_TRUE;
firstAppArgIndex = NOT_FOUND;
expectingNoDashArg = JNI_FALSE;
argsCount = 1;
stopExpansion = JNI_FALSE;
relaunch = JNI_FALSE;

result = expand(args, env, var_name);

/* Restore the state. */
parsingOpenJ9Args = JNI_FALSE;
firstAppArgIndex = savedFirstAppArgIndex;
expectingNoDashArg = savedExpectingNoDashArg;
argsCount = savedArgsCount;
stopExpansion = savedStopExpansion;
relaunch = savedRelaunch;

return result;
}

/*
* Expand a string into a list of args.
* If the string is the result of looking up an environment variable,
Expand All @@ -497,9 +544,13 @@ static jboolean expand(JLI_List args, const char *str, const char *var_name) {
char *p, *arg;
char quote;
JLI_List argsInFile;
char *argMem = NULL;

// This is retained until the process terminates as it is saved as the args
p = JLI_MemAlloc(JLI_StrLen(str) + 1);
if (parsingOpenJ9Args) {
argMem = p;
}
while (*str != '\0') {
while (*str != '\0' && isspace(*str)) {
str++;
Expand Down Expand Up @@ -541,6 +592,10 @@ static jboolean expand(JLI_List args, const char *str, const char *var_name) {
}
exit(1);
}
if (parsingOpenJ9Args) {
/* Dup the string so it can easily be freed later. */
arg = JLI_StringDup(arg);
}
JLI_List_add(args, arg);
} else {
size_t cnt, idx;
Expand Down Expand Up @@ -580,6 +635,9 @@ static jboolean expand(JLI_List args, const char *str, const char *var_name) {
assert (*str == '\0' || isspace(*str));
}

if (parsingOpenJ9Args) {
JLI_MemFree(argMem);
}
return JNI_TRUE;
}

Expand Down
29 changes: 28 additions & 1 deletion src/java.base/share/native/libjli/java.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
* (c) Copyright IBM Corp. 2022, 2023 All Rights Reserved
* ===========================================================================
*/

Expand Down Expand Up @@ -139,6 +139,8 @@ static jboolean ValidateModules(JNIEnv* env);

static void SetPaths(int argc, char **argv);

static int parse_size(const char *s, jlong *result);

static void DumpState();

enum OptionKind {
Expand Down Expand Up @@ -325,6 +327,31 @@ JLI_Launch(int argc, char ** argv, /* main argc, argv */
}
}

{
/* Process -Xmso in the OPENJ9_JAVA_OPTIONS environment variable to
* set the main thread stack size. May be overridden by a later command
* line option.
*/
JLI_List openj9Args = JLI_List_new(8); /* 8 is arbitrary */
if (JLI_ParseOpenJ9ArgsFromEnvVar(openj9Args, "OPENJ9_JAVA_OPTIONS")) {
size_t i = openj9Args->size;
while (i > 0) {
i -= 1;
if (JLI_StrCCmp(openj9Args->elements[i], "-Xmso") == 0) {
jlong tmp = 0;
if (parse_size(openj9Args->elements[i] + 5, &tmp)) {
threadStackSize = tmp;
if (threadStackSize > 0 && threadStackSize < (jlong)STACK_SIZE_MINIMUM) {
threadStackSize = STACK_SIZE_MINIMUM;
}
}
break;
}
}
JLI_List_free(openj9Args);
}
}

/* Parse command line options; if the return value of
* ParseArguments is false, the program should exit.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/java.base/share/native/libjli/jli_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
* questions.
*/

/*
* ===========================================================================
* (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved
* ===========================================================================
*/

#ifndef _JLI_UTIL_H
#define _JLI_UTIL_H

Expand Down Expand Up @@ -158,4 +164,7 @@ JLI_PreprocessArg(const char *arg, jboolean expandSourceOpt);
JNIEXPORT jboolean JNICALL
JLI_AddArgsFromEnvVar(JLI_List args, const char *var_name);

JNIEXPORT jboolean JNICALL
JLI_ParseOpenJ9ArgsFromEnvVar(JLI_List args, const char *var_name);

#endif /* _JLI_UTIL_H */

0 comments on commit 6e51ddf

Please sign in to comment.