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/appcds /test/hotspot/jtreg/runtime/appcds/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 = "-Xlog:gc*=info,gc+region=trace,gc+alloc+region=debug";
  66 
  67         String sharedArchiveCfgFile =
  68             System.getProperty("user.dir") + File.separator + "GCSharedStringDuringDump_gen.txt";
  69         try (FileOutputStream fos = new FileOutputStream(sharedArchiveCfgFile)) {
  70             PrintWriter out = new PrintWriter(new OutputStreamWriter(fos));
  71             out.println("VERSION: 1.0");
  72             out.println("@SECTION: String");
  73             out.println("31: shared_test_string_unique_14325");
  74             for (int i=0; i<100000; i++) {
  75                 String s = "generated_string " + i;
  76                 out.println(s.length() + ": " + s);
  77             }
  78             out.close();
  79         }
  80 
  81         JarBuilder.build(true, "WhiteBox", "sun/hotspot/WhiteBox");
  82         String whiteBoxJar = TestCommon.getTestJar("WhiteBox.jar");
  83         String bootClassPath = "-Xbootclasspath/a:" + whiteBoxJar;
  84 
  85         for (int i=0; i<2; i++) {
  86             // i = 0 -- run without agent = no extra GCs
  87             // i = 1 -- run with agent = cause extra GCs
  88 
  89             String extraArg = (i == 0) ? "-showversion" : "-javaagent:" + agentJar;
  90 
  91             OutputAnalyzer output = TestCommon.dump(
  92                                 appJar, TestCommon.list("GCSharedStringsDuringDumpWb"),
  93                                 bootClassPath, extraArg, "-Xmx32m", gcLog,
  94                                 "-XX:SharedArchiveConfigFile=" + sharedArchiveCfgFile);
  95 
  96             if (output.getStdout().contains("Too many string space regions") ||
  97                 output.getStderr().contains("Unable to write archive heap memory regions") ||
  98                 output.getStdout().contains("Try increasing NewSize") ||
  99                 !output.getStdout().contains("oa0 space:") ||
 100                 output.getExitValue() != 0) {
 101                 // Try again with larger heap and NewSize, this should increase the
 102                 // G1 heap region size to 2M
 103                 TestCommon.testDump(
 104                     appJar, TestCommon.list("GCSharedStringsDuringDumpWb"),
 105                     bootClassPath, extraArg, "-Xmx8g", "-XX:NewSize=8m", gcLog,
 106                     "-XX:SharedArchiveConfigFile=" + sharedArchiveCfgFile);
 107             }
 108 
 109             TestCommon.run(
 110                 "-cp", appJar,
 111                 bootClassPath,
 112                 "-Xmx32m",
 113                 "-XX:+PrintSharedSpaces",
 114                 "-XX:+UnlockDiagnosticVMOptions",
 115                 "-XX:+WhiteBoxAPI",
 116                 "-XX:SharedReadOnlySize=30m",
 117                 gcLog,
 118                 "GCSharedStringsDuringDumpWb")
 119               .assertNormalExit();
 120         }
 121     }
 122 }
 123