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 import jdk.test.lib.Platform;
 41 import jdk.test.lib.process.OutputAnalyzer;
 42 
 43 public class TestParallelGCWithCDS {
 44     public final static String HELLO = "Hello World";
 45     static String helloJar;
 46 
 47     public static void main(String... args) throws Exception {
 48         helloJar = JarBuilder.build("hello", "Hello");
 49 
 50         // Check if we can use ParallelGC during dump time, or run time, or both.
 51         test(false, true);
 52         test(true,  false);
 53         test(true,  true);
 54 
 55         // With G1 we usually have 2 heap regions. To increase test coverage, we can have 3 heap regions
 56         // by using "-Xmx256m -XX:ObjectAlignmentInBytes=64"
 57         if (Platform.is64bit()) test(false, true, true);
 58     }
 59 
 60     final static String G1 = "-XX:+UseG1GC";
 61     final static String Parallel = "-XX:+UseParallelGC";
 62 
 63     static void test(boolean dumpWithParallel, boolean execWithParallel) throws Exception {
 64         test(dumpWithParallel, execWithParallel, false);
 65     }
 66 
 67     static void test(boolean dumpWithParallel, boolean execWithParallel, boolean useSmallRegions) throws Exception {
 68         String dumpGC = dumpWithParallel ? Parallel : G1;
 69         String execGC = execWithParallel ? Parallel : G1;
 70         String small1 = useSmallRegions ? "-Xmx256m" : "-showversion";
 71         String small2 = useSmallRegions ? "-XX:ObjectAlignmentInBytes=64" : "-showversion";
 72         OutputAnalyzer out;
 73 
 74         System.out.println("0. Dump with " + dumpGC);
 75         out = TestCommon.dump(helloJar,
 76                               new String[] {"Hello"},
 77                               dumpGC,
 78                               small1,
 79                               small2,
 80                               "-Xlog:cds");
 81         out.shouldContain("Dumping shared data to file:");
 82         out.shouldHaveExitValue(0);
 83 
 84         System.out.println("1. Exec with " + execGC);
 85         out = TestCommon.exec(helloJar,
 86                               execGC,
 87                               small1,
 88                               small2,
 89                               "-Xlog:cds",
 90                               "Hello");
 91         out.shouldContain(HELLO);
 92         out.shouldHaveExitValue(0);
 93 
 94         int n = 2;
 95         if (!dumpWithParallel && execWithParallel) {
 96             // We dumped with G1, so we have an archived heap. At exec time, try to load them into
 97             // a small ParallelGC heap that may be too small.
 98             String[] sizes = {
 99                 "4m",   // usually this will success load the archived heap
100                 "2m",   // usually this will fail to load the archived heap, but app can launch
101                         // or fail with "GC triggered before VM initialization completed"
102                 "1m"    // usually this will cause VM launch to fail with "Too small maximum heap"
103             };
104             for (String sz : sizes) {
105                 String xmx = "-Xmx" + sz;
106                 System.out.println("=======\n" + n + ". Exec with " + execGC + " " + xmx);
107                 out = TestCommon.exec(helloJar,
108                                       execGC,
109                                       small1,
110                                       small2,
111                                       xmx,
112                                       "-Xlog:cds",
113                                       "Hello");
114                 if (out.getExitValue() == 0) {
115                     out.shouldContain(HELLO);
116                 } else {
117                     String pattern = "((Too small maximum heap)" +
118                                      "|(GC triggered before VM initialization completed)" +
119                                      "|(Initial heap size set to a larger value than the maximum heap size)" +
120                                      "|(CDS archive has aot-linked classes but the archived heap objects cannot be loaded)" +
121                                      "|(java.lang.OutOfMemoryError)" +
122                                      "|(Error: A JNI error has occurred, please check your installation and try again))";
123                     out.shouldMatch(pattern);
124                     out.shouldNotHaveFatalError();
125                 }
126                 n++;
127             }
128         }
129     }
130 }