1 /*
2  * Copyright (c) 2018, 2019, 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  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
30  * @modules jdk.jartool/sun.tools.jar
31  * @compile ../test-classes/Hello.java
32  * @build sun.hotspot.WhiteBox
33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
34  * @run main/othervm/timeout=160 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. DifferentHeapSizes
35  */
36 
37 import jdk.test.lib.process.OutputAnalyzer;
38 import sun.hotspot.WhiteBox;
39 import jdk.test.lib.cds.CDSTestUtils;
40 
41 public class DifferentHeapSizes {
42     static final String DEDUP = "-XX:+UseStringDeduplication"; // This increases code coverage.
43 
44     static class Scenario {
45         int dumpSize;   // in MB
46         int runSizes[]; // in MB
Scenario(int ds, int... rs)47         Scenario(int ds, int... rs) {
48             dumpSize = ds;
49             runSizes = rs;
50         }
51     }
52 
53     static Scenario[] scenarios = {
54         //           dump -Xmx ,         run -Xmx
55         new Scenario(        32,         32, 64, 512, 2048, 4097, 16374, 31000),
56         new Scenario(       128,         32, 64, 512, 2048, 4097, 16374, 31000, 40000),
57         new Scenario(      2048,         32, 512, 2600, 4097, 8500, 31000,      40000),
58         new Scenario(     17000,         32, 512, 2048, 4097, 8500, 31000,      40000),
59         new Scenario(     31000,         32, 512, 2048, 4097, 8500, 17000,      40000)
60     };
61 
main(String[] args)62     public static void main(String[] args) throws Exception {
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(CDSTestUtils.MSG_COMPRESSION_MUST_BE_USED);
84                 }
85             }
86         }
87 
88         // Test various settings of -XX:HeapBaseMinAddress that would trigger
89         // "CDS heap data need to be relocated because the desired range ... is outside of the heap"
90         long default_base = WhiteBox.getWhiteBox().getSizeTVMFlag("HeapBaseMinAddress").longValue();
91         long M = 1024 * 1024;
92         long bases[] = new long[] {
93             /* dump xmx */   /* run xmx */   /* dump base */             /* run base */
94             128 * M,         128 * M,        default_base,               default_base + 256L * 1024 * 1024,
95             128 * M,         16376 * M,      0x0000000119200000L,        -1,
96         };
97 
98         for (int i = 0; i < bases.length; i += 4) {
99             String dump_xmx  = getXmx(bases[i+0]);
100             String run_xmx   = getXmx(bases[i+1]);
101             String dump_base = getHeapBaseMinAddress(bases[i+2]);
102             String run_base  = getHeapBaseMinAddress(bases[i+3]);
103 
104             TestCommon.dump(appJar, appClasses, dump_xmx, dump_base);
105             TestCommon.run("-cp", appJar, "-showversion", "-Xlog:cds", run_xmx, run_base, DEDUP, "Hello")
106                 .assertNormalExit("Hello World")
107                 .assertNormalExit(out -> {
108                         out.shouldNotContain(CDSTestUtils.MSG_RANGE_NOT_WITHIN_HEAP);
109                         out.shouldNotContain(CDSTestUtils.MSG_RANGE_ALREADT_IN_USE);
110                     });
111         }
112     }
113 
getXmx(long value)114     static String getXmx(long value) {
115         if (value < 0) {
116             return "-showversion"; // This is a harmless command line arg
117         } else {
118             return "-Xmx" + (value / 1024 / 1024) + "m";
119         }
120     }
getHeapBaseMinAddress(long value)121     static String getHeapBaseMinAddress(long value) {
122         if (value < 0) {
123             return "-showversion"; // This is a harmless command line arg
124         } else {
125             return "-XX:HeapBaseMinAddress=0x" + Long.toHexString(value);
126         }
127     }
128 }
129