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 final String DEDUP = "-XX:+UseStringDeduplication"; // This increases code coverage.
  44 
  45     static class Scenario {
  46         int dumpSize;   // in MB
  47         int runSizes[]; // in MB
  48         Scenario(int ds, int... rs) {
  49             dumpSize = ds;
  50             runSizes = rs;
  51         }
  52     }
  53 
  54     static Scenario[] scenarios = {
  55         //           dump -Xmx ,         run -Xmx
  56         new Scenario(        32,         32, 64, 512, 2048, 4097, 16374, 31000),
  57         new Scenario(       128,         32, 64, 512, 2048, 4097, 16374, 31000, 40000),
  58         new Scenario(      2048,         32, 512, 2600, 4097, 8500, 31000,      40000),
  59         new Scenario(     17000,         32, 512, 2048, 4097, 8500, 31000,      40000),
  60         new Scenario(     31000,         32, 512, 2048, 4097, 8500, 17000,      40000)
  61     };
  62 
  63     public static void main(String[] args) throws Exception {
  64         JarBuilder.getOrCreateHelloJar();
  65         String appJar = TestCommon.getTestJar("hello.jar");
  66         String appClasses[] = TestCommon.list("Hello");
  67 
  68         for (Scenario s : scenarios) {
  69             String dumpXmx = "-Xmx" + s.dumpSize + "m";
  70             OutputAnalyzer output = TestCommon.dump(appJar, appClasses, dumpXmx);
  71 
  72             for (int runSize : s.runSizes) {
  73                 String runXmx = "-Xmx" + runSize + "m";
  74                 CDSTestUtils.Result result = TestCommon.run("-cp", appJar, "-showversion",
  75                         "-Xlog:cds", runXmx, DEDUP, "Hello");
  76                 if (runSize < 32768) {
  77                     result
  78                         .assertNormalExit("Hello World")
  79                         .assertNormalExit(out -> {
  80                             out.shouldNotContain(CDSTestUtils.MSG_RANGE_NOT_WITHIN_HEAP);
  81                             out.shouldNotContain(CDSTestUtils.MSG_RANGE_ALREADT_IN_USE);
  82                         });
  83                 } else {
  84                     result.assertAbnormalExit("Unable to use shared archive: UseCompressedOops and UseCompressedClassPointers must be on for UseSharedSpaces.");
  85                 }
  86             }
  87         }
  88 
  89         // Test various settings of -XX:HeapBaseMinAddress that would trigger 
  90         // "CDS heap data need to be relocated because the desired range ... is outside of the heap"
  91         long default_base = WhiteBox.getWhiteBox().getSizeTVMFlag("HeapBaseMinAddress").longValue();
  92         long M = 1024 * 1024;
  93         long bases[] = new long[] {
  94             /* dump xmx */   /* run xmx */   /* dump base */             /* run base */
  95             128 * M,         128 * M,        default_base,               default_base + 256L * 1024 * 1024,
  96             128 * M,         16376 * M,      0x0000000119200000L,        -1,
  97         };
  98 
  99         for (int i = 0; i < bases.length; i += 4) {
 100             String dump_xmx  = getXmx(bases[i+0]);
 101             String run_xmx   = getXmx(bases[i+1]);
 102             String dump_base = getHeapBaseMinAddress(bases[i+2]);
 103             String run_base  = getHeapBaseMinAddress(bases[i+3]);
 104 
 105             TestCommon.dump(appJar, appClasses, dump_xmx, dump_base);
 106             TestCommon.run("-cp", appJar, "-showversion", "-Xlog:cds", run_xmx, run_base, DEDUP, "Hello")
 107                 .assertNormalExit("Hello World")
 108                 .assertNormalExit(out -> {
 109                         out.shouldNotContain(CDSTestUtils.MSG_RANGE_NOT_WITHIN_HEAP);
 110                         out.shouldNotContain(CDSTestUtils.MSG_RANGE_ALREADT_IN_USE);
 111                     });
 112         }
 113     }
 114 
 115     static String getXmx(long value) {
 116         if (value < 0) {
 117             return "-showversion"; // This is a harmless command line arg
 118         } else {
 119             return "-Xmx" + (value / 1024 / 1024) + "m";
 120         }
 121     }
 122     static String getHeapBaseMinAddress(long value) {
 123         if (value < 0) {
 124             return "-showversion"; // This is a harmless command line arg
 125         } else {
 126             return "-XX:HeapBaseMinAddress=0x" + Long.toHexString(value);
 127         }
 128     }
 129 }