< prev index next >

test/hotspot/jtreg/containers/docker/TestCPUAwareness.java

Print this page
@  rev 56647 : 8230305: Cgroups v2: Container awareness
|  Summary: Implement Cgroups v2 container awareness in hotspot
~  Reviewed-by: bobv


  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @summary Test JVM's CPU resource awareness when running inside docker container
  28  * @requires docker.support
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *          jdk.jartool/sun.tools.jar
  33  * @run driver TestCPUAwareness
  34  */
  35 import java.util.List;

  36 import jdk.test.lib.containers.docker.Common;
  37 import jdk.test.lib.containers.docker.DockerRunOptions;
  38 import jdk.test.lib.containers.docker.DockerTestUtils;
  39 import jdk.test.lib.containers.cgroup.CPUSetsReader;

  40 
  41 public class TestCPUAwareness {
  42     private static final String imageName = Common.imageName("cpu");
  43     private static final int availableCPUs = Runtime.getRuntime().availableProcessors();
  44 
  45     public static void main(String[] args) throws Exception {
  46         if (!DockerTestUtils.canTestDocker()) {
  47             return;
  48         }
  49 
  50         System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);
  51         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  52 
  53         try {
  54             // cpuset, period, shares, expected Active Processor Count
  55             testComboWithCpuSets();
  56 
  57             // cpu shares - it should be safe to use CPU shares exceeding available CPUs
  58             testCpuShares(256, 1);
  59             testCpuShares(2048, 2);


 185             .addDockerOpts("--cpuset-cpus", "" + cpuset)
 186             .addDockerOpts("--cpu-period=" + period)
 187             .addDockerOpts("--cpu-quota=" + quota)
 188             .addDockerOpts("--cpu-shares=" + shares);
 189 
 190         if (!usePreferContainerQuotaForCPUCount) opts.addJavaOpts("-XX:-PreferContainerQuotaForCPUCount");
 191 
 192         Common.run(opts)
 193             .shouldMatch("active_processor_count.*" + expectedAPC);
 194     }
 195 
 196 
 197     private static void testCpuShares(int shares, int expectedAPC) throws Exception {
 198         Common.logNewTestCase("test cpu shares, shares = " + shares);
 199         System.out.println("expectedAPC = " + expectedAPC);
 200 
 201         expectedAPC = adjustExpectedAPCForAvailableCPUs(expectedAPC);
 202 
 203         DockerRunOptions opts = Common.newOpts(imageName)
 204             .addDockerOpts("--cpu-shares=" + shares);
 205         Common.run(opts)
 206             .shouldMatch("CPU Shares is.*" + shares)
 207             .shouldMatch("active_processor_count.*" + expectedAPC);


















 208     }


 209 }


  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @summary Test JVM's CPU resource awareness when running inside docker container
  28  * @requires docker.support
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *          jdk.jartool/sun.tools.jar
  33  * @run driver TestCPUAwareness
  34  */
  35 import java.util.List;
  36 import java.util.Optional;
  37 import jdk.test.lib.containers.docker.Common;
  38 import jdk.test.lib.containers.docker.DockerRunOptions;
  39 import jdk.test.lib.containers.docker.DockerTestUtils;
  40 import jdk.test.lib.containers.cgroup.CPUSetsReader;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 
  43 public class TestCPUAwareness {
  44     private static final String imageName = Common.imageName("cpu");
  45     private static final int availableCPUs = Runtime.getRuntime().availableProcessors();
  46 
  47     public static void main(String[] args) throws Exception {
  48         if (!DockerTestUtils.canTestDocker()) {
  49             return;
  50         }
  51 
  52         System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);
  53         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  54 
  55         try {
  56             // cpuset, period, shares, expected Active Processor Count
  57             testComboWithCpuSets();
  58 
  59             // cpu shares - it should be safe to use CPU shares exceeding available CPUs
  60             testCpuShares(256, 1);
  61             testCpuShares(2048, 2);


 187             .addDockerOpts("--cpuset-cpus", "" + cpuset)
 188             .addDockerOpts("--cpu-period=" + period)
 189             .addDockerOpts("--cpu-quota=" + quota)
 190             .addDockerOpts("--cpu-shares=" + shares);
 191 
 192         if (!usePreferContainerQuotaForCPUCount) opts.addJavaOpts("-XX:-PreferContainerQuotaForCPUCount");
 193 
 194         Common.run(opts)
 195             .shouldMatch("active_processor_count.*" + expectedAPC);
 196     }
 197 
 198 
 199     private static void testCpuShares(int shares, int expectedAPC) throws Exception {
 200         Common.logNewTestCase("test cpu shares, shares = " + shares);
 201         System.out.println("expectedAPC = " + expectedAPC);
 202 
 203         expectedAPC = adjustExpectedAPCForAvailableCPUs(expectedAPC);
 204 
 205         DockerRunOptions opts = Common.newOpts(imageName)
 206             .addDockerOpts("--cpu-shares=" + shares);
 207         OutputAnalyzer out = Common.run(opts);
 208         String cgroupVer = getDetectedCgroupVersion(out);
 209         if (cgroupVer != null ) {
 210             if ("cgroupv1".equals(cgroupVer)) {
 211                 out.shouldMatch("CPU Shares is.*" + shares);
 212             } else if ("cgroupsv2".equals(cgroupVer)) {
 213                 out.shouldMatch("Scaled CPU Shares value is:.*");
 214             }
 215         }
 216         out.shouldMatch("active_processor_count.*" + expectedAPC);
 217     }
 218 
 219     private static String getDetectedCgroupVersion(OutputAnalyzer out) throws Exception {
 220         Optional<String> cgroupVersString = out.asLines()
 221                                                 .stream()
 222                                                 .filter( l -> l.startsWith("Detected CGroups version is:") )
 223                                                 .findFirst();
 224         if (cgroupVersString.isPresent()) { // only non-product builds have this
 225             return cgroupVersString.get().split(": ")[1].trim();
 226         } else {
 227             return null;
 228         }
 229     }
 230 
 231 }
< prev index next >