Skip to content

Commit 626346a

Browse files
committed
Lookup isContainerized at build time.
1 parent 229280f commit 626346a

File tree

1 file changed

+28
-18
lines changed
  • substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver

1 file changed

+28
-18
lines changed

substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/MemoryUtil.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.BufferedReader;
2828
import java.io.InputStreamReader;
2929
import java.lang.management.ManagementFactory;
30+
import java.lang.reflect.Method;
3031
import java.nio.file.Files;
3132
import java.nio.file.Paths;
3233
import java.util.ArrayList;
@@ -65,6 +66,23 @@ class MemoryUtil {
6566
*/
6667
private static final long MAX_HEAP_BYTES = 32_000_000_000L;
6768

69+
private static final Method IS_CONTAINERIZED_METHOD;
70+
private static final Object IS_CONTAINERIZED_RECEIVER;
71+
72+
static {
73+
IS_CONTAINERIZED_METHOD = ReflectionUtil.lookupMethod(jdk.jfr.internal.JVM.class, "isContainerized");
74+
if (JavaVersionUtil.JAVA_SPEC == 21) { // non-static
75+
var jvmField = ReflectionUtil.lookupField(jdk.jfr.internal.JVM.class, "jvm");
76+
try {
77+
IS_CONTAINERIZED_RECEIVER = jvmField.get(null);
78+
} catch (IllegalAccessException e) {
79+
throw VMError.shouldNotReachHere(e);
80+
}
81+
} else {
82+
IS_CONTAINERIZED_RECEIVER = null; // static
83+
}
84+
}
85+
6886
public static List<String> determineMemoryFlags(NativeImage.HostFlags hostFlags) {
6987
List<String> flags = new ArrayList<>();
7088
if (hostFlags.hasUseParallelGC()) {
@@ -77,9 +95,9 @@ public static List<String> determineMemoryFlags(NativeImage.HostFlags hostFlags)
7795
* -XX:InitialRAMPercentage or -Xms.
7896
*/
7997
if (hostFlags.hasMaxRAMPercentage()) {
80-
flags.addAll(determineReasonableMaxRAMPercentage(value -> "-XX:MaxRAMPercentage=" + value));
98+
flags.addAll(determineMemoryUsageFlags(value -> "-XX:MaxRAMPercentage=" + value));
8199
} else if (hostFlags.hasMaximumHeapSizePercent()) {
82-
flags.addAll(determineReasonableMaxRAMPercentage(value -> "-XX:MaximumHeapSizePercent=" + value.intValue()));
100+
flags.addAll(determineMemoryUsageFlags(value -> "-XX:MaximumHeapSizePercent=" + value.intValue()));
83101
}
84102
if (hostFlags.hasGCTimeRatio()) {
85103
/*
@@ -98,13 +116,12 @@ public static List<String> determineMemoryFlags(NativeImage.HostFlags hostFlags)
98116
}
99117

100118
/**
101-
* Returns a percentage (0.0-100.0) to be used as a value for the -XX:MaxRAMPercentage or
102-
* -XX:MaximumHeapSizePercent flags of the builder process. Dedicated mode uses a fixed
103-
* percentage of total memory and is the default in containers. Shared mode tries to use
104-
* available memory to reduce memory pressure on the host machine. Note that this method uses
105-
* OperatingSystemMXBean, which is container-aware.
119+
* Returns memory usage flags for the build process. Dedicated mode uses a fixed percentage of
120+
* total memory and is the default in containers. Shared mode tries to use available memory to
121+
* reduce memory pressure on the host machine. Note that this method uses OperatingSystemMXBean,
122+
* which is container-aware.
106123
*/
107-
private static List<String> determineReasonableMaxRAMPercentage(Function<Double, String> toMemoryFlag) {
124+
private static List<String> determineMemoryUsageFlags(Function<Double, String> toMemoryFlag) {
108125
var osBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
109126
final double totalMemorySize = osBean.getTotalMemorySize();
110127
final double dedicatedMemorySize = totalMemorySize * DEDICATED_MODE_TOTAL_MEMORY_RATIO;
@@ -151,18 +168,11 @@ private static List<String> determineReasonableMaxRAMPercentage(Function<Double,
151168

152169
private static boolean isContainerized() {
153170
/*
154-
* [GR-55515]: Accessing isContainerized() reflectively for 21 JDK compatibility (non-static
155-
* vs static method). After dropping JDK 21, use it directly.
171+
* [GR-55515]: Accessing isContainerized() reflectively only for 21 JDK compatibility
172+
* (non-static vs static method). After dropping JDK 21, use it directly.
156173
*/
157-
var isContainerized = ReflectionUtil.lookupMethod(jdk.jfr.internal.JVM.class, "isContainerized");
158174
try {
159-
final Object receiver;
160-
if (JavaVersionUtil.JAVA_SPEC == 21) { // non-static
161-
receiver = ReflectionUtil.lookupField(jdk.jfr.internal.JVM.class, "jvm").get(null);
162-
} else {
163-
receiver = null; // static
164-
}
165-
return (boolean) isContainerized.invoke(receiver);
175+
return (boolean) IS_CONTAINERIZED_METHOD.invoke(IS_CONTAINERIZED_RECEIVER);
166176
} catch (ReflectiveOperationException | ClassCastException e) {
167177
throw VMError.shouldNotReachHere(e);
168178
}

0 commit comments

Comments
 (0)