Skip to content

Commit 06a5d27

Browse files
committed
[OPENJDK-1510] accept integers for JAVA_*_MEM_RATIO
Ensure that JAVA_INITIAL_MEM_RATIO and JAVA_MAX_MEM_RATIO can accept Integer-format numbers, as they could prior to the move from calculating -Xmx/ms to -XX:Max/InitialRAMPercentage. For JDK8, -XX:Max/InitialRAMPercentage require their parameter to be formatted as a float; i.e., with a trailing decimal. JDK11+ will accept a number without the decimal. To ensure backwards compatibility for users who specified the environment variables in an integer-format, process the supplied value with bash's built-in "printf" and output a float. Since we are now processing the value, rather than passing to the JVM unaltered, we need to decide upon the precision that we allow. Otherwise we risk truncating the user-supplied value without warning. Specify that only the whole-number portion of the value is used, and achieve that with the appropriate printf format specifier. We documented that JAVA_MAX_MEM_RATIO=0 disables calculating a JVM flag. Add a test for this, as well as the other behaviour described above. Signed-off-by: Jonathan Dowland <[email protected]>
1 parent 6b9c560 commit 06a5d27

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

modules/jvm/api/module.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ envs:
1414
description: User specified Java options to be appended to generated options in JAVA_OPTS.
1515
example: "-Dsome.property=foo"
1616
- name: JAVA_MAX_MEM_RATIO
17-
description: Specify the maximum heap memory. Corresponds to the JVM argument `-XX:MaxRAMPercentage`. The default is `50.0` which means 50% of the available memory. You can disable this mechanism by setting the value to `0`.
17+
description:
18+
Specify the maximum heap memory. Corresponds to the JVM argument
19+
`-XX:MaxRAMPercentage`. The default is `50.0` which means 50% of the
20+
available memory. You can disable this mechanism by setting the value to
21+
`0`.
22+
The supplied value can be an integer or float, but only the whole
23+
number part is used.
1824
example: "90.0"
1925
- name: JAVA_INITIAL_MEM_RATIO
2026
description:
2127
Specify the initial heap memory. Corresponds to the JVM argument
2228
`-XX:InitialRAMPercentage`. By default this is not specified.
23-
**This is deprecated and will be removed in a future release. Users should
24-
specify `-XX:InitialRAMPercentage` directly in JAVA_OPTS instead.**
29+
The supplied value can be an integer or float, but only the whole
30+
number part is used.
31+
**JAVA_INITIAL_MEM_RATIO is deprecated and will be removed in a future
32+
release. Users should specify `-XX:InitialRAMPercentage` directly in
33+
JAVA_OPTS instead.**
2534
example: "25.0"
2635
- name: JAVA_MAX_INITIAL_MEM
2736
description:

modules/jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ fi
2121

2222
# Check for memory options and calculate a sane default if not given
2323
max_memory() {
24-
# Check if explicitly disabled
25-
if [ "x$JAVA_MAX_MEM_RATIO" = "x0" ]; then
26-
return
27-
fi
28-
echo "-XX:MaxRAMPercentage=${JAVA_MAX_MEM_RATIO:-50.0}"
24+
case "$JAVA_MAX_MEM_RATIO" in
25+
"0") # explicitly disabled
26+
return
27+
;;
28+
"")
29+
maxmem="50.0"
30+
;;
31+
*)
32+
maxmem="$(printf "%.0f.0" "$JAVA_MAX_MEM_RATIO")"
33+
;;
34+
esac
35+
echo "-XX:MaxRAMPercentage=$maxmem"
2936
}
3037

3138
# Check for memory options and calculate a sane default if not given
@@ -36,8 +43,11 @@ initial_memory() {
3643
else
3744
# there's no point setting this if we are passing -Xms, since -Xms
3845
# overrides this
46+
# JAVA_INITIAL_MEM_RATIO is deprecated and will be removed in a
47+
# future release
3948
if [ -n "$JAVA_INITIAL_MEM_RATIO" ]; then
40-
echo "-XX:InitialRAMPercentage=${JAVA_INITIAL_MEM_RATIO}"
49+
initmem="$(printf "%.0f.0" "$JAVA_INITIAL_MEM_RATIO")"
50+
echo "-XX:InitialRAMPercentage=${initmem}"
4151
fi
4252
fi
4353
}

tests/features/java/memory.feature

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,44 @@ Feature: OPENJDK-559 JVM Memory tests
77
And container log should not contain -Xmx
88

99
@ubi8
10-
Scenario: Check configured JVM max heap configuration
10+
Scenario: Check configured JVM max heap configuration and ensure JAVA_MAX_MEM_RATIO accepts floats but only takes whole number part
1111
Given container is started with env
1212
| variable | value |
13-
| JAVA_MAX_MEM_RATIO | 90.0 |
13+
| JAVA_MAX_MEM_RATIO | 90.4 |
1414
Then container log should contain -XX:MaxRAMPercentage=90.0
1515

16+
@ubi8
17+
Scenario: Ensure JAVA_MAX_MEM_RATIO accepts Integers
18+
Given container is started with env
19+
| variable | value |
20+
| JAVA_MAX_MEM_RATIO | 90 |
21+
Then container log should contain -XX:MaxRAMPercentage=90.0
22+
23+
@ubi8
24+
Scenario: Ensure JAVA_INITIAL_MEM_RATIO accepts Integers
25+
Given container is started with env
26+
| variable | value |
27+
| JAVA_INITIAL_MEM_RATIO | 10 |
28+
Then container log should contain -XX:InitialRAMPercentage=10.0
29+
30+
@ubi8
31+
Scenario: Ensure JAVA_MAX_MEM_RATIO=0 disables parameter
32+
Given container is started with env
33+
| variable | value |
34+
| JAVA_MAX_MEM_RATIO | 0 |
35+
Then container log should not contain -XX:MaxRAMPercentage
36+
1637
@ubi8
1738
Scenario: Check default JVM initial heap configuration is unspecified
1839
Given container is started as uid 1000
1940
Then container log should not contain -XX:InitialRAMPercentage
2041
And container log should not contain -Xms
2142

2243
@ubi8
23-
Scenario: Check configured JVM max heap configuration
44+
Scenario: Check configured JVM max heap configuration and ensure JAVA_INITIAL_MEM_RATIO accepts floats but only takes whole number part
2445
Given container is started with env
2546
| variable | value |
26-
| JAVA_INITIAL_MEM_RATIO | 25.0 |
47+
| JAVA_INITIAL_MEM_RATIO | 25.2 |
2748
Then container log should contain -XX:InitialRAMPercentage=25.0
2849

2950
@ubi8

0 commit comments

Comments
 (0)