Skip to content

Commit efa1786

Browse files
committed
8292206: TestCgroupMetrics.java fails as getMemoryUsage() is lower than expected
Backport-of: 6ccee839580fd9dc4cd4941b44dbbe3105202561
1 parent d4467a7 commit efa1786

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

test/jdk/jdk/internal/platform/cgroup/TestCgroupMetrics.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,16 +36,7 @@
3636
public class TestCgroupMetrics {
3737

3838
public static void main(String[] args) throws Exception {
39-
// If cgroups is not configured, report success.
40-
Metrics metrics = Metrics.systemMetrics();
41-
if (metrics == null) {
42-
System.out.println("TEST PASSED!!!");
43-
return;
44-
}
45-
46-
MetricsTester metricsTester = new MetricsTester();
47-
metricsTester.testAll(metrics);
48-
System.out.println("TEST PASSED!!!");
39+
MetricsTester.main(args);
4940
}
5041

5142
}

test/jdk/jdk/internal/platform/docker/TestSystemMetrics.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -55,6 +55,7 @@ public static void main(String[] args) throws Exception {
5555
opts.addDockerOpts("--memory=256m");
5656
opts.addJavaOpts("-cp", "/test-classes/");
5757
opts.addJavaOpts("--add-exports", "java.base/jdk.internal.platform=ALL-UNNAMED");
58+
opts.addClassOptions("-incontainer");
5859
DockerTestUtils.dockerRunJava(opts).shouldHaveExitValue(0).shouldContain("TEST PASSED!!!");
5960
} finally {
6061
DockerTestUtils.removeDockerImage(imageName);

test/lib/jdk/test/lib/containers/cgroup/MetricsTester.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Red Hat Inc.
2+
* Copyright (c) 2020, 2022, Red Hat Inc.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -52,14 +52,22 @@ private static CgroupMetricsTester createInstance(Metrics m) {
5252
}
5353
}
5454

55-
public void testAll(Metrics m) throws Exception {
55+
private void testAll(Metrics m, boolean inContainer) throws Exception {
5656
CgroupMetricsTester tester = createInstance(m);
5757
tester.testCpuAccounting();
5858
tester.testCpuConsumption();
5959
tester.testCpuSchedulingMetrics();
6060
tester.testCpuSets();
61-
tester.testMemorySubsystem();
62-
tester.testMemoryUsage();
61+
if (!inContainer) {
62+
// If not running in a container, these test cases query the memory usage.
63+
// of all processes in the entire system (or those belonging to the current
64+
// Linux user). They cannot produce predictable results due to interference
65+
// from unrelated processes.
66+
System.out.println("testMemorySubsystem and testMemoryUsage skipped");
67+
} else {
68+
tester.testMemorySubsystem();
69+
tester.testMemoryUsage();
70+
}
6371
tester.testMisc();
6472
}
6573

@@ -71,8 +79,14 @@ public static void main(String[] args) throws Exception {
7179
return;
7280
}
7381

82+
boolean inContainer = false;
83+
if (args.length > 0 && "-incontainer".equals(args[0])) {
84+
inContainer = true;
85+
}
86+
System.out.println("inContainer = " + inContainer);
87+
7488
MetricsTester metricsTester = new MetricsTester();
75-
metricsTester.testAll(m);
89+
metricsTester.testAll(m, inContainer);
7690
System.out.println("TEST PASSED!!!");
7791
}
7892
}

0 commit comments

Comments
 (0)