1 /*
   2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  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 memory 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  * @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo
  34  * @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  35  * @run driver TestMemoryAwareness
  36  */
  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 
  41 
  42 public class TestMemoryAwareness {
  43     private static final String imageName = Common.imageName("memory");
  44 
  45     public static void main(String[] args) throws Exception {
  46         if (!DockerTestUtils.canTestDocker()) {
  47             return;
  48         }
  49 
  50         Common.prepareWhiteBox();
  51         DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
  52 
  53         try {
  54             testMemoryLimit("100m", "104857600");
  55             testMemoryLimit("500m", "524288000");
  56             testMemoryLimit("1g", "1073741824");
  57             testMemoryLimit("4g", "4294967296");
  58 
  59             testMemorySoftLimit("500m", "524288000");
  60             testMemorySoftLimit("1g", "1073741824");
  61 
  62             // Add extra 10 Mb to allocator limit, to be sure to cause OOM
  63             testOOM("256m", 256 + 10);
  64 
  65         } finally {
  66             DockerTestUtils.removeDockerImage(imageName);
  67         }
  68     }
  69 
  70 
  71     private static void testMemoryLimit(String valueToSet, String expectedTraceValue)
  72             throws Exception {
  73 
  74         Common.logNewTestCase("memory limit: " + valueToSet);
  75 
  76         DockerRunOptions opts = Common.newOpts(imageName)
  77             .addDockerOpts("--memory", valueToSet);
  78 
  79         Common.run(opts)
  80             .shouldMatch("Memory Limit is:.*" + expectedTraceValue);
  81     }
  82 
  83 
  84     private static void testMemorySoftLimit(String valueToSet, String expectedTraceValue)
  85             throws Exception {
  86         Common.logNewTestCase("memory soft limit: " + valueToSet);
  87 
  88         DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo");
  89         Common.addWhiteBoxOpts(opts);
  90         opts.addDockerOpts("--memory-reservation=" + valueToSet);
  91 
  92         Common.run(opts)
  93             .shouldMatch("Memory Soft Limit.*" + expectedTraceValue);
  94     }
  95 
  96 
  97     // provoke OOM inside the container, see how VM reacts
  98     private static void testOOM(String dockerMemLimit, int sizeToAllocInMb) throws Exception {
  99         Common.logNewTestCase("OOM");
 100 
 101         DockerRunOptions opts = Common.newOpts(imageName, "AttemptOOM")
 102             .addDockerOpts("--memory", dockerMemLimit, "--memory-swap", dockerMemLimit);
 103         opts.classParams.add("" + sizeToAllocInMb);
 104 
 105         DockerTestUtils.dockerRunJava(opts)
 106             .shouldHaveExitValue(1)
 107             .shouldContain("Entering AttemptOOM main")
 108             .shouldNotContain("AttemptOOM allocation successful")
 109             .shouldContain("java.lang.OutOfMemoryError");
 110     }
 111 
 112 }