1 /*
  2  * Copyright (c) 2022, 2024, 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  * @test Loading CDS archived heap objects into ParallelGC
 26  * @bug 8274788
 27  * @requires vm.cds
 28  * @requires vm.gc.Parallel
 29  * @requires vm.gc.G1
 30  *
 31  * @comment don't run this test if any -XX::+Use???GC options are specified, since they will
 32  *          interfere with the test.
 33  * @requires vm.gc == null
 34  *
 35  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 36  * @compile test-classes/Hello.java
 37  * @run driver TestParallelGCWithCDS
 38  */
 39 
 40 // Below is exactly the same as above, except:
 41 // - requires vm.bits == "64"
 42 // - extra argument "false"
 43 
 44  /*
 45  * @test Loading CDS archived heap objects into ParallelGC
 46  * @bug 8274788 8341371
 47  * @requires vm.cds
 48  * @requires vm.gc.Parallel
 49  * @requires vm.gc.G1
 50  * @requires vm.bits == "64"
 51  *
 52  * @comment don't run this test if any -XX::+Use???GC options are specified, since they will
 53  *          interfere with the test.
 54  * @requires vm.gc == null
 55  *
 56  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 57  * @compile test-classes/Hello.java
 58  * @run driver TestParallelGCWithCDS false
 59  */
 60 import jdk.test.lib.Platform;
 61 import jdk.test.lib.process.OutputAnalyzer;
 62 
 63 public class TestParallelGCWithCDS {
 64     public final static String HELLO = "Hello World";
 65     static String helloJar;
 66     static boolean useCompressedOops = true;
 67 
 68     public static void main(String... args) throws Exception {
 69         helloJar = JarBuilder.build("hello", "Hello");
 70 
 71         if (args.length > 0 && args[0].equals("false")) {
 72             useCompressedOops = false;
 73         }
 74 
 75         // Check if we can use ParallelGC during dump time, or run time, or both.
 76         test(false, true);
 77         test(true,  false);
 78         test(true,  true);
 79 
 80         // With G1 we usually have 2 heap regions. To increase test coverage, we can have 3 heap regions
 81         // by using "-Xmx256m -XX:ObjectAlignmentInBytes=64"
 82         if (Platform.is64bit()) test(false, true, true);
 83     }
 84 
 85     final static String G1 = "-XX:+UseG1GC";
 86     final static String Parallel = "-XX:+UseParallelGC";
 87 
 88     static void test(boolean dumpWithParallel, boolean execWithParallel) throws Exception {
 89         test(dumpWithParallel, execWithParallel, false);
 90     }
 91 
 92     static void test(boolean dumpWithParallel, boolean execWithParallel, boolean useSmallRegions) throws Exception {
 93         String dumpGC = dumpWithParallel ? Parallel : G1;
 94         String execGC = execWithParallel ? Parallel : G1;
 95         String small1 = useSmallRegions ? "-Xmx256m" : "-showversion";
 96         String small2 = useSmallRegions ? "-XX:ObjectAlignmentInBytes=64" : "-showversion";
 97         String errMsg = "Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops";
 98         String coops = useCompressedOops ? "-XX:+UseCompressedOops" : "-XX:-UseCompressedOops";
 99         OutputAnalyzer out;
100 
101         System.out.println("0. Dump with " + dumpGC);
102         out = TestCommon.dump(helloJar,
103                               new String[] {"Hello"},
104                               dumpGC,
105                               small1,
106                               small2,
107                               coops,
108                               "-Xlog:cds");
109         out.shouldContain("Dumping shared data to file:");
110         out.shouldHaveExitValue(0);
111 
112         System.out.println("1. Exec with " + execGC);
113         out = TestCommon.exec(helloJar,
114                               execGC,
115                               small1,
116                               small2,
117                               coops,
118                               "-Xlog:cds",
119                               "Hello");
120         out.shouldContain(HELLO);
121         out.shouldNotContain(errMsg);
122         out.shouldHaveExitValue(0);
123 
124         int n = 2;
125         if (!dumpWithParallel && execWithParallel) {
126             // We dumped with G1, so we have an archived heap. At exec time, try to load them into
127             // a small ParallelGC heap that may be too small.
128             String[] sizes = {
129                 "4m",   // usually this will success load the archived heap
130                 "2m",   // usually this will fail to load the archived heap, but app can launch
131                         // or fail with "GC triggered before VM initialization completed"
132                 "1m"    // usually this will cause VM launch to fail with "Too small maximum heap"
133             };
134             for (String sz : sizes) {
135                 String xmx = "-Xmx" + sz;
136                 System.out.println("=======\n" + n + ". Exec with " + execGC + " " + xmx);
137                 out = TestCommon.exec(helloJar,
138                                       execGC,
139                                       small1,
140                                       small2,
141                                       xmx,
142                                       coops,
143                                       "-Xlog:cds",
144                                       "Hello");
145                 if (out.getExitValue() == 0) {
146                     out.shouldContain(HELLO);
147                     out.shouldNotContain(errMsg);
148                 } else {
149                     String pattern = "((Too small maximum heap)" +
150                                      "|(GC triggered before VM initialization completed)" +
151                                      "|(CDS archive has aot-linked classes but the archived heap objects cannot be loaded)" +
152                                      "|(Initial heap size set to a larger value than the maximum heap size)" +
153                                      "|(java.lang.OutOfMemoryError)" +
154                                      "|(Error: A JNI error has occurred, please check your installation and try again))";
155                     out.shouldMatch(pattern);
156                     out.shouldNotHaveFatalError();
157                 }
158                 n++;
159             }
160         }
161     }
162 }