Skip to content

Commit

Permalink
use Metrics.class get the CgroupV1 information
Browse files Browse the repository at this point in the history
  • Loading branch information
sendaoYan committed Apr 12, 2024
1 parent ecb7159 commit bd144b7
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions test/hotspot/jtreg/containers/docker/TestJFREvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
* Also make sure that PIDs are based on value provided by container,
* not by the host system.
* @requires (docker.support & os.maxMemory >= 2g)
* @modules java.base/jdk.internal.platform
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* jdk.jartool/sun.tools.jar
* @build JfrReporter jdk.test.whitebox.WhiteBox PrintContainerInfo
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox
* @build JfrReporter
* @run driver TestJFREvents
*/
import java.util.List;
Expand All @@ -45,6 +45,7 @@
import jdk.test.lib.Asserts;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.Utils;
import jdk.internal.platform.Metrics;


public class TestJFREvents {
Expand All @@ -53,15 +54,23 @@ public class TestJFREvents {
private static final String TEST_ENV_VALUE = "unique_value_abc592903xyz";
private static final int availableCPUs = Runtime.getRuntime().availableProcessors();
private static final int UNKNOWN = -100;
private static boolean isCgroupV1 = false;

public static void main(String[] args) throws Exception {
System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);
if (!DockerTestUtils.canTestDocker()) {
return;
}

// If cgroups is not configured, report success.
Metrics metrics = Metrics.systemMetrics();
if (metrics == null) {
System.out.println("TEST PASSED!!!");
return;
}
isCgroupV1 = "cgroupv1".equals(metrics.getProvider());

DockerTestUtils.buildJdkContainerImage(imageName);
Common.prepareWhiteBox();

try {

Expand Down Expand Up @@ -219,7 +228,7 @@ private static void testMemory(String valueToSet, String expectedValue) throws E

private static void testSwapMemory(String memValueToSet, String swapValueToSet, String expectedTotalValue, String expectedFreeValue) throws Exception {
OutputAnalyzer out;
if (isCgroupV1(memValueToSet)) {
if (isCgroupV1) {
Common.logNewTestCase("Memory: --memory = " + memValueToSet + " --memory-swap = " + swapValueToSet + " --memory-swappiness = 60");
out = DockerTestUtils.dockerRunJava(
commonDockerOpts()
Expand Down Expand Up @@ -301,17 +310,4 @@ private static void testEnvironmentVariables() throws Exception {
.shouldNotContain(TEST_ENV_VARIABLE)
.shouldNotContain(TEST_ENV_VALUE);
}

private static boolean isCgroupV1(String memValueToSet) throws Exception {
DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo")
.addDockerOpts("--memory=" + memValueToSet)
.addDockerOpts("--memory-swap=" + memValueToSet); // no swap
Common.addWhiteBoxOpts(opts);

OutputAnalyzer out = Common.run(opts);
if (out.contains("cgroupv1")) {
return true;
}
return false;
}
}

0 comments on commit bd144b7

Please sign in to comment.