2929 * @requires !vm.asan
3030 * @library /test/lib
3131 * @modules java.base/jdk.internal.misc
32+ * java.base/jdk.internal.platform
3233 * java.management
3334 * jdk.jartool/sun.tools.jar
3435 * @build CheckContainerized jdk.test.whitebox.WhiteBox PrintContainerInfo
3536 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox
3637 * @run driver TestMisc
3738 */
39+ import jdk .internal .platform .Metrics ;
3840import jdk .test .lib .containers .docker .Common ;
3941import jdk .test .lib .containers .docker .DockerTestUtils ;
4042import jdk .test .lib .containers .docker .DockerRunOptions ;
4143import jdk .test .lib .process .OutputAnalyzer ;
4244import jdk .test .lib .process .ProcessTools ;
45+ import jtreg .SkippedException ;
4346
4447
4548public class TestMisc {
49+ private static final Metrics metrics = Metrics .systemMetrics ();
4650 private static final String imageName = Common .imageName ("misc" );
4751
4852 public static void main (String [] args ) throws Exception {
@@ -58,6 +62,7 @@ public static void main(String[] args) throws Exception {
5862 testIsContainerized ();
5963 testPrintContainerInfo ();
6064 testPrintContainerInfoActiveProcessorCount ();
65+ testPrintContainerInfoCPUShares ();
6166 } finally {
6267 DockerTestUtils .removeDockerImage (imageName );
6368 }
@@ -94,8 +99,53 @@ private static void testPrintContainerInfo() throws Exception {
9499 checkContainerInfo (Common .run (opts ));
95100 }
96101
102+ // Test the mapping function on cgroups v2. Should also pass on cgroups v1 as it's
103+ // a direct mapping there.
104+ private static void testPrintContainerInfoCPUShares () throws Exception {
105+ // Test won't work on cgv1 rootless podman since resource limits don't
106+ // work there.
107+ if ("cgroupv1" .equals (metrics .getProvider ()) &&
108+ DockerTestUtils .isPodman () &&
109+ DockerTestUtils .isRootless ()) {
110+ throw new SkippedException ("Resource limits required for testPrintContainerInfoCPUShares(). " +
111+ "This is cgv1 with podman in rootless mode. Test skipped." );
112+ }
113+ // Anything less than 1024 should return the back-mapped cpu-shares value without
114+ // rounding to next multiple of 1024 (on cg v2). Only ensure that we get
115+ // 'cpu_shares: <back-mapped-value>' over 'cpu_shares: no shares'.
116+ printContainerInfo (512 , 1024 , false );
117+ // Don't use 1024 exactly so as to avoid mapping to the unlimited/uset case.
118+ // Use a value > 100 post-mapping so as to hit the non-default branch: 1052 => 103
119+ printContainerInfo (1052 , 1024 , true );
120+ // need at least 2 CPU cores for this test to work
121+ if (Runtime .getRuntime ().availableProcessors () >= 2 ) {
122+ printContainerInfo (2048 , 2048 , true );
123+ }
124+ }
125+
126+ private static void printContainerInfo (int cpuShares , int expected , boolean numberMatch ) throws Exception {
127+ Common .logNewTestCase ("Test print_container_info() - cpu shares - given: " + cpuShares + ", expected: " + expected );
128+
129+ DockerRunOptions opts = Common .newOpts (imageName , "PrintContainerInfo" );
130+ Common .addWhiteBoxOpts (opts );
131+ opts .addDockerOpts ("--cpu-shares" , Integer .valueOf (cpuShares ).toString ());
132+
133+ OutputAnalyzer out = Common .run (opts );
134+ String str = out .getOutput ();
135+ boolean isCgroupV2 = str .contains ("cgroupv2" );
136+ // cg v1 maps cpu shares values verbatim. Only cg v2 uses the
137+ // mapping function.
138+ if (numberMatch ) {
139+ int valueExpected = isCgroupV2 ? expected : cpuShares ;
140+ out .shouldContain ("cpu_shares: " + valueExpected );
141+ } else {
142+ // must not print "no shares"
143+ out .shouldNotContain ("cpu_shares: no shares" );
144+ }
145+ }
146+
97147 private static void testPrintContainerInfoActiveProcessorCount () throws Exception {
98- Common .logNewTestCase ("Test print_container_info()" );
148+ Common .logNewTestCase ("Test print_container_info() - ActiveProcessorCount " );
99149
100150 DockerRunOptions opts = Common .newOpts (imageName , "PrintContainerInfo" ).addJavaOpts ("-XX:ActiveProcessorCount=2" );
101151 Common .addWhiteBoxOpts (opts );
0 commit comments