test/hotspot/jtreg/runtime/appcds/GraalWithLimitedMetaspace.java
branchJDK-8200758-branch
changeset 57588 dac8f245de8e
parent 57587 16c4975e9e09
parent 57586 f459f98aa30d
child 57591 6805e0ef7453
equal deleted inserted replaced
57587:16c4975e9e09 57588:dac8f245de8e
     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 dumping with limited metaspace with loading of JVMCI related classes.
       
    28  *          VM should not crash but CDS dump will abort upon failure in allocating metaspace.
       
    29  * @requires vm.cds & vm.graal.enabled & vm.compMode == "Xmixed"
       
    30  * @library /test/lib
       
    31  * @modules java.base/jdk.internal.misc
       
    32  *          java.management
       
    33  *          jdk.jartool/sun.tools.jar
       
    34  * @build UseAppCDS_Test
       
    35  * @run driver ClassFileInstaller -jar test.jar UseAppCDS_Test
       
    36  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
       
    37  *      -XX:+TieredCompilation -XX:+UseJVMCICompiler -Djvmci.Compiler=graal
       
    38  *      GraalWithLimitedMetaspace
       
    39  */
       
    40 
       
    41 import jdk.test.lib.cds.CDSTestUtils;
       
    42 import jdk.test.lib.process.OutputAnalyzer;
       
    43 import jdk.test.lib.process.ProcessTools;
       
    44 
       
    45 import java.util.ArrayList;
       
    46 import java.util.List;
       
    47 import java.io.*;
       
    48 
       
    49 public class GraalWithLimitedMetaspace {
       
    50 
       
    51     // Class UseAppCDS_Test is loaded by the App loader
       
    52 
       
    53     static final String TEST_OUT = "UseAppCDS_Test.main--executed";
       
    54 
       
    55     private static final String TESTJAR = "./test.jar";
       
    56     private static final String TESTNAME = "UseAppCDS_Test";
       
    57     private static final String TESTCLASS = TESTNAME + ".class";
       
    58 
       
    59     private static final String CLASSLIST_FILE = "./GraalWithLimitedMetaspace.classlist";
       
    60     private static final String ARCHIVE_FILE = "./GraalWithLimitedMetaspace.jsa";
       
    61     private static final String BOOTCLASS = "java.lang.Class";
       
    62 
       
    63     public static void main(String[] args) throws Exception {
       
    64 
       
    65         // dump loaded classes into a classlist file
       
    66         dumpLoadedClasses(new String[] { BOOTCLASS, TESTNAME });
       
    67 
       
    68 
       
    69         // create an archive using the classlist
       
    70         dumpArchive();
       
    71 
       
    72     }
       
    73 
       
    74     public static List<String> toClassNames(String filename) throws IOException {
       
    75         ArrayList<String> classes = new ArrayList<>();
       
    76         try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)))) {
       
    77             for (; ; ) {
       
    78                 String line = br.readLine();
       
    79                 if (line == null) {
       
    80                     break;
       
    81                 }
       
    82                 classes.add(line.replaceAll("/", "."));
       
    83             }
       
    84         }
       
    85         return classes;
       
    86     }
       
    87 
       
    88     static void dumpLoadedClasses(String[] expectedClasses) throws Exception {
       
    89         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
       
    90             "-XX:DumpLoadedClassList=" + CLASSLIST_FILE,
       
    91             // trigger JVMCI runtime init so that JVMCI classes will be
       
    92             // included in the classlist
       
    93             "-XX:+UnlockExperimentalVMOptions",
       
    94             "-XX:+EnableJVMCI",
       
    95             "-XX:+EagerJVMCI",
       
    96             "-cp",
       
    97             TESTJAR,
       
    98             TESTNAME,
       
    99             TEST_OUT);
       
   100 
       
   101         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-loaded-classes")
       
   102             .shouldHaveExitValue(0)
       
   103             .shouldContain(TEST_OUT);
       
   104 
       
   105         List<String> dumpedClasses = toClassNames(CLASSLIST_FILE);
       
   106 
       
   107         for (String clazz : expectedClasses) {
       
   108             if (!dumpedClasses.contains(clazz)) {
       
   109                 throw new RuntimeException(clazz + " missing in " +
       
   110                                            CLASSLIST_FILE);
       
   111             }
       
   112         }
       
   113     }
       
   114 
       
   115     static void dumpArchive() throws Exception {
       
   116         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true,
       
   117             "-cp",
       
   118             TESTJAR,
       
   119             "-XX:SharedClassListFile=" + CLASSLIST_FILE,
       
   120             "-XX:SharedArchiveFile=" + ARCHIVE_FILE,
       
   121             "-Xlog:cds",
       
   122             "-Xshare:dump",
       
   123             "-XX:MetaspaceSize=12M",
       
   124             "-XX:MaxMetaspaceSize=12M");
       
   125 
       
   126         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dump-archive");
       
   127         int exitValue = output.getExitValue();
       
   128         if (exitValue == 1) {
       
   129             output.shouldContain("Failed allocating metaspace object type");
       
   130         } else if (exitValue == 0) {
       
   131             output.shouldContain("Loading classes to share");
       
   132         } else {
       
   133             throw new RuntimeException("Unexpected exit value " + exitValue);
       
   134         }
       
   135     }
       
   136 }