1 /*
   2  * Copyright (c) 2017, 2018, 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 Similar to GCDuringDumping.java, this test adds the -XX:SharedArchiveConfigFile
  28  *          option for testing the interaction with GC and shared strings.
  29  * @library /test/lib /test/hotspot/jtreg/runtime/cds /test/hotspot/jtreg/runtime/cds/test-classes
  30  * @requires vm.cds.archived.java.heap
  31  * @modules java.base/jdk.internal.misc
  32  *          jdk.jartool/sun.tools.jar
  33  *          java.management
  34  * @build sun.hotspot.WhiteBox GCDuringDumpTransformer GCSharedStringsDuringDumpWb
  35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  36  * @run main/othervm/timeout=480 GCSharedStringsDuringDump
  37  */
  38 
  39 import java.io.File;
  40 import java.io.FileOutputStream;
  41 import java.io.OutputStreamWriter;
  42 import java.io.PrintWriter;
  43 import jdk.test.lib.cds.CDSOptions;
  44 import jdk.test.lib.process.OutputAnalyzer;
  45 import jdk.test.lib.process.ProcessTools;
  46 import sun.hotspot.WhiteBox;
  47 
  48 public class GCSharedStringsDuringDump {
  49     public static String appClasses[] = {
  50         "GCSharedStringsDuringDumpWb",
  51     };
  52     public static String agentClasses[] = {
  53         "GCDuringDumpTransformer",
  54     };
  55 
  56     public static void main(String[] args) throws Throwable {
  57         String agentJar =
  58             ClassFileInstaller.writeJar("GCDuringDumpTransformer.jar",
  59                                         ClassFileInstaller.Manifest.fromSourceFile("GCDuringDumpTransformer.mf"),
  60                                         agentClasses);
  61 
  62         String appJar =
  63             ClassFileInstaller.writeJar("GCSharedStringsDuringDumpApp.jar", appClasses);
  64 
  65         String gcLog = Boolean.getBoolean("test.cds.verbose.gc") ?
  66             "-Xlog:gc*=info,gc+region=trace,gc+alloc+region=debug" : "-showversion";
  67 
  68         String sharedArchiveCfgFile =
  69             System.getProperty("user.dir") + File.separator + "GCSharedStringDuringDump_gen.txt";
  70         try (FileOutputStream fos = new FileOutputStream(sharedArchiveCfgFile)) {
  71             PrintWriter out = new PrintWriter(new OutputStreamWriter(fos));
  72             out.println("VERSION: 1.0");
  73             out.println("@SECTION: String");
  74             out.println("31: shared_test_string_unique_14325");
  75             for (int i=0; i<100000; i++) {
  76                 String s = "generated_string " + i;
  77                 out.println(s.length() + ": " + s);
  78             }
  79             out.close();
  80         }
  81 
  82         JarBuilder.build(true, "WhiteBox", "sun/hotspot/WhiteBox");
  83         String whiteBoxJar = TestCommon.getTestJar("WhiteBox.jar");
  84         String bootClassPath = "-Xbootclasspath/a:" + whiteBoxJar;
  85 
  86         for (int i=0; i<2; i++) {
  87             // i = 0 -- run without agent = no extra GCs
  88             // i = 1 -- run with agent = cause extra GCs
  89 
  90             String extraArg = (i == 0) ? "-showversion" : "-javaagent:" + agentJar;
  91             String extraOption = (i == 0) ? "-showversion" : "-XX:+AllowArchivingWithJavaAgent";
  92             OutputAnalyzer output = TestCommon.dump(
  93                                 appJar, TestCommon.list("GCSharedStringsDuringDumpWb"),
  94                                 bootClassPath, extraArg, "-Xmx32m", gcLog,
  95                                 "-XX:SharedArchiveConfigFile=" + sharedArchiveCfgFile,
  96                                 "-XX:+UnlockDiagnosticVMOptions", extraOption);
  97 
  98             if (output.getStdout().contains("Too many string space regions") ||
  99                 output.getStderr().contains("Unable to write archive heap memory regions") ||
 100                 output.getStdout().contains("Try increasing NewSize") ||
 101                 !output.getStdout().contains("oa0 space:") ||
 102                 output.getExitValue() != 0) {
 103                 // Try again with larger heap and NewSize, this should increase the
 104                 // G1 heap region size to 2M
 105                 TestCommon.testDump(
 106                     appJar, TestCommon.list("GCSharedStringsDuringDumpWb"),
 107                     bootClassPath, extraArg, "-Xmx8g", "-XX:NewSize=8m", gcLog,
 108                     "-XX:SharedArchiveConfigFile=" + sharedArchiveCfgFile,
 109                     "-XX:+UnlockDiagnosticVMOptions", extraOption);
 110             }
 111 
 112             TestCommon.run(
 113                 "-cp", appJar,
 114                 bootClassPath,
 115                 extraArg,
 116                 "-Xlog:cds=info,class+path=info",
 117                 "-Xmx32m",
 118                 "-XX:+PrintSharedSpaces",
 119                 "-XX:+UnlockDiagnosticVMOptions",
 120                 extraOption,
 121                 "-XX:+WhiteBoxAPI",
 122                 "-XX:SharedReadOnlySize=30m",
 123                 gcLog,
 124                 "GCSharedStringsDuringDumpWb")
 125               .assertNormalExit();
 126         }
 127     }
 128 }
 129