1 /*
   2  * Copyright (c) 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 automatic relocation of archive heap regions dur to heap size changes.
  28  * @requires vm.cds.archived.java.heap
  29  * @requires (vm.gc=="null")
  30  * @library /test/lib /test/hotspot/jtreg/runtime/appcds
  31  * @modules jdk.jartool/sun.tools.jar
  32  * @compile ../test-classes/Hello.java
  33  * @build sun.hotspot.WhiteBox
  34  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  35  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. DifferentHeapSizes
  36  */
  37 
  38 import jdk.test.lib.process.OutputAnalyzer;
  39 import sun.hotspot.WhiteBox;
  40 import jdk.test.lib.cds.CDSTestUtils;
  41 
  42 public class DifferentHeapSizes {
  43     static class Scenario {
  44         int dumpSize;   // in MB
  45         int runSizes[]; // in MB
  46         Scenario(int ds, int... rs) {
  47             dumpSize = ds;
  48             runSizes = rs;
  49         }
  50     }
  51 
  52     static Scenario[] scenarios = {
  53         //           dump -Xmx ,         run -Xmx
  54         new Scenario(        32,         32, 64, 512, 2048, 4097, 16374, 31000),
  55         new Scenario(       128,         32, 64, 512, 2048, 4097, 16374, 31000, 40000),
  56         new Scenario(      2048,         32, 512, 2600, 4097, 8500, 31000,      40000),
  57         new Scenario(     17000,         32, 512, 2048, 4097, 8500, 31000,      40000),
  58         new Scenario(     31000,         32, 512, 2048, 4097, 8500, 17000,      40000)
  59     };
  60 
  61     public static void main(String[] args) throws Exception {
  62         String dedup = "-XX:+UseStringDeduplication"; // This increases code coverage.
  63         JarBuilder.getOrCreateHelloJar();
  64         String appJar = TestCommon.getTestJar("hello.jar");
  65         String appClasses[] = TestCommon.list("Hello");
  66 
  67         for (Scenario s : scenarios) {
  68             String dumpXmx = "-Xmx" + s.dumpSize + "m";
  69             OutputAnalyzer output = TestCommon.dump(appJar, appClasses, dumpXmx);
  70 
  71             for (int runSize : s.runSizes) {
  72                 String runXmx = "-Xmx" + runSize + "m";
  73                 CDSTestUtils.Result result = TestCommon.run("-cp", appJar, "-showversion",
  74                         "-Xlog:cds", runXmx, dedup, "Hello");
  75                 if (runSize < 32768) {
  76                     result
  77                         .assertNormalExit("Hello World")
  78                         .assertNormalExit(out -> {
  79                             out.shouldNotContain(CDSTestUtils.MSG_RANGE_NOT_WITHIN_HEAP);
  80                             out.shouldNotContain(CDSTestUtils.MSG_RANGE_ALREADT_IN_USE);
  81                         });
  82                 } else {
  83                     result.assertAbnormalExit("Unable to use shared archive: UseCompressedOops and UseCompressedClassPointers must be on for UseSharedSpaces.");
  84                 }
  85             }
  86         }
  87         String flag = "HeapBaseMinAddress";
  88         String xxflag = "-XX:" + flag + "=";
  89         String mx = "-Xmx128m";
  90         long base = WhiteBox.getWhiteBox().getSizeTVMFlag(flag).longValue();
  91 
  92         TestCommon.dump(appJar, appClasses, mx, xxflag + base);
  93         TestCommon.run("-cp", appJar, "-showversion", "-Xlog:cds", mx, xxflag + (base + 256 * 1024 * 1024), dedup, "Hello")
  94             .assertNormalExit("Hello World")
  95             .assertNormalExit(out -> {
  96                     out.shouldNotContain(CDSTestUtils.MSG_RANGE_NOT_WITHIN_HEAP);
  97                     out.shouldNotContain(CDSTestUtils.MSG_RANGE_ALREADT_IN_USE);
  98                 });
  99     }
 100 }