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