From 990bc153755f3c1601296ab6a10bef69bd270a23 Mon Sep 17 00:00:00 2001 From: Ken Wills Date: Wed, 4 Sep 2019 09:47:31 -0500 Subject: [PATCH] CLOUD-2842 - Xms and Xms are different when limits are higher than 5Gi using JAVA_MAX_MEM_RATIO and JAVA_INITIAL_MEM_RATIO --- .../jboss/container/java/jvm/container-limits | 2 + .../container/java/jvm/java-default-options | 9 +- .../bash/tests/bats/java-default-options.bats | 139 ++++++++++++++++++ 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 jboss/container/java/jvm/bash/tests/bats/java-default-options.bats diff --git a/jboss/container/java/jvm/bash/artifacts/opt/jboss/container/java/jvm/container-limits b/jboss/container/java/jvm/bash/artifacts/opt/jboss/container/java/jvm/container-limits index e5c0d6af..69c14f3b 100644 --- a/jboss/container/java/jvm/bash/artifacts/opt/jboss/container/java/jvm/container-limits +++ b/jboss/container/java/jvm/bash/artifacts/opt/jboss/container/java/jvm/container-limits @@ -47,6 +47,8 @@ container_memory() { local max_mem_unbounded="$(max_unbounded)" if [ -r "${mem_file}" ]; then local max_mem="$(cat ${mem_file})" + # note that this may return a value that is the maximum 64-bit signed integer, rounded to the nearest page (by dropping the last bits). + # When this occurs, the comparision with MemTotal in /proc/meminfo will result it no value being returned. if [ ${max_mem} -lt ${max_mem_unbounded} ]; then echo "${max_mem}" fi diff --git a/jboss/container/java/jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options b/jboss/container/java/jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options index 2333108d..1e8eb63c 100644 --- a/jboss/container/java/jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options +++ b/jboss/container/java/jvm/bash/artifacts/opt/jboss/container/java/jvm/java-default-options @@ -66,7 +66,7 @@ max_memory() { return fi - # Check for the 'real memory size' and caluclate mx from a ratio + # Check for the 'real memory size' and calculate mx from a ratio # given (default is 50%) if [ "x$CONTAINER_MAX_MEMORY" != x ]; then local max_mem="${CONTAINER_MAX_MEMORY}" @@ -98,13 +98,18 @@ initial_memory() { local max_ratio=${JAVA_MAX_MEM_RATIO:-${__DEFAULT_JAVA_MAX_MEM_RATIO}} local initial_ratio=${JAVA_INITIAL_MEM_RATIO:-${__DEFAULT_JAVA_INITIAL_MEM_RATIO}} local ms=$(echo "${max_mem} ${max_ratio} ${initial_ratio} 1048576" | awk '{printf "%d\n" , ($1*(($2*$3)/10000))/$4 + 0.5}') - local max_initial_memory=${JAVA_MAX_INITIAL_MEM:-${__DEFAULT_JAVA_MAX_INITIAL_MEM}} + + # since CONTAINER_MAX_MEMORY is available (set by cgroups /sys/fs/cgroup/memory/memory.limit_in_bytes) then use that as the default) + local cmm=$(echo "${CONTAINER_MAX_MEMORY} 1048576" | awk '{printf "%d\n" , ($1/$2) + 0.5}') + local max_initial_memory=${JAVA_MAX_INITIAL_MEM:-${cmm}} + if [ "${ms}" -lt "${max_initial_memory}" ] ; then echo "-Xms${ms}m" else echo "-Xms${max_initial_memory}m" fi fi + # otherwise no default value is returned } # Switch on diagnostics except when switched off diff --git a/jboss/container/java/jvm/bash/tests/bats/java-default-options.bats b/jboss/container/java/jvm/bash/tests/bats/java-default-options.bats new file mode 100644 index 00000000..436af615 --- /dev/null +++ b/jboss/container/java/jvm/bash/tests/bats/java-default-options.bats @@ -0,0 +1,139 @@ +export JBOSS_CONTAINER_JAVA_JVM_MODULE=${BATS_TEST_DIRNAME}/../../../bash/artifacts/opt/jboss/container/java/jvm/ +load $BATS_TEST_DIRNAME/../../../bash/artifacts/opt/jboss/container/java/jvm/java-default-options + +@test "Test default initial memory value" { + expected="" # expect nothing + run initial_memory + echo "Result: ${output}" + echo "Expected: ${expected}" + [ "${expected}" = "${output}" ] +} + +@test "Test default initial memory values with valid CONTAINER_MAX_MEMORY" { + expected="-Xms64m" + CONTAINER_MAX_MEMORY=536870912 + run initial_memory + echo "Result: ${output}" + echo "Expected: ${expected}" + [ "${expected}" = "${output}" ] +} + +@test "Test 6g initial memory values" { + expected="-Xms768m" + CONTAINER_MAX_MEMORY=6442450944 + JAVA_MAX_INITIAL_MEM=6144 + run initial_memory + echo "Result: ${output}" + echo "Expected: ${expected}" + [ "${expected}" = "${output}" ] +} + +@test "Test 6g initial memory values no CMM" { + expected="" # nothing, no CMM available + JAVA_MAX_INITIAL_MEM=6144 + run initial_memory + echo "Result: ${output}" + echo "Expected: ${expected}" + [ "${expected}" = "${output}" ] +} + +@test "Test default max memory values" { + expected="" # expect nothing + run max_memory + echo "Result: ${output}" + echo "Expected: ${expected}" + [ "${expected}" = "${output}" ] +} + +@test "Test default max memory values with valid CONTAINER_MAX_MEMORY" { + expected="-Xmx256m" + CONTAINER_MAX_MEMORY=536870912 + run max_memory + echo "Result: ${output}" + echo "Expected: ${expected}" + [ "${expected}" = "${output}" ] +} + +@test "Test default max memory values with CONTAINER_MAX_MEMORY=512mb" { + expected="-Xmx256m" + CONTAINER_MAX_MEMORY=536870912 + run max_memory + echo "Result: ${output}" + echo "Expected: ${expected}" + [ "${expected}" = "${output}" ] +} + +@test "Test 6g max memory values with CONTAINER_MAX_MEMORY set to 6g" { + expected="-Xmx3072m" + CONTAINER_MAX_MEMORY=6442450944 + run max_memory + echo "Result: ${output}" + echo "Expected: ${expected}" + [ "${expected}" = "${output}" ] +} + +@test "Test default 0.9 max 1.0 initial " { + min_expected="" # should be empty + max_expected="" + JAVA_MAX_MEM_RATIO=90 + JAVA_INITIAL_MEM_RATIO=100 + run initial_memory + min=${output} + echo "Min Result: ${output}" + echo "Min Expected: ${min_expected}" + [ "${min_expected}" = "${min}" ] + run max_memory + max=${output} + echo "Max Result: ${output}" + echo "Max Expected: ${max_expected}" +} + +@test "Test 6g 0.9 max 1.0 initial " { + min_expected="-Xms5530m" + max_expected="-Xmx5530m" + CONTAINER_MAX_MEMORY=6442450944 + JAVA_MAX_MEM_RATIO=90 + JAVA_INITIAL_MEM_RATIO=100 + run initial_memory + min=${output} + echo "Min Result: ${output}" + echo "Min Expected: ${min_expected}" + [ "${min_expected}" = "${min}" ] + run max_memory + max=${output} + echo "Max Result: ${output}" + echo "Max Expected: ${max_expected}" + [ "${max_expected}" = "${max}" ] +} + +@test "Test default 4g 0.9 max 1.0 max only " { + min_expected="" # should be unset, no CMM available + max_expected="-Xmx3686m" + JAVA_MAX_MEM_RATIO=90 + JAVA_INITIAL_MEM_RATIO=100 + run initial_memory + min=${output} + echo "Min Result: ${output}" + echo "Min Expected: ${min_expected}" + [ "${min_expected}" = "${min}" ] + run max_memory + max=${output} + echo "Max Result: ${output}" + echo "Max Expected: ${max_expected}" +} + +@test "Test default 4g 1.0 max 1.0 max only " { + min_expected="" # CMM not available, expect nothing + max_expected="-Xmx4096m" + JAVA_MAX_MEM_RATIO=100 + JAVA_INITIAL_MEM_RATIO=100 + run initial_memory + min=${output} + echo "Min Result: ${output}" + echo "Min Expected: ${min_expected}" + [ "${min_expected}" = "${min}" ] + run max_memory + max=${output} + echo "Max Result: ${output}" + echo "Max Expected: ${max_expected}" +} \ No newline at end of file