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