1 /*
   2  * Copyright (c) 2014, 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 CommandLineFlagCombo
  27  * @requires vm.cds.archived.java.heap
  28  * @comment This test explicitly chooses the type of GC to be used by sub-processes. It may conflict with the GC type set
  29  * via the -vmoptions command line option of JTREG. vm.gc==null will help the test case to discard the explicitly passed
  30  * vm options.
  31  * @requires (vm.gc=="null")
  32  * @summary Test command line flag combinations that
  33  *          could likely affect the behaviour of AppCDS
  34  * @library /test/lib
  35  * @modules java.base/jdk.internal.misc
  36  *          java.management
  37  *          jdk.jartool/sun.tools.jar
  38  * @build sun.hotspot.WhiteBox
  39  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  40  * @compile test-classes/Hello.java
  41  * @run main/othervm/timeout=240 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. CommandLineFlagCombo
  42  */
  43 
  44 import jdk.test.lib.BuildHelper;
  45 import jdk.test.lib.Platform;
  46 import jdk.test.lib.process.OutputAnalyzer;
  47 
  48 import sun.hotspot.code.Compiler;
  49 
  50 public class CommandLineFlagCombo {
  51 
  52     // shared base address test table
  53     private static final String[] testTable = {
  54         "-XX:+UseG1GC", "-XX:+UseSerialGC", "-XX:+UseParallelGC", "-XX:+UseConcMarkSweepGC",
  55         "-XX:+FlightRecorder",
  56         "-XX:+UseLargePages", // may only take effect on machines with large-pages
  57         "-XX:+UseCompressedClassPointers",
  58         "-XX:+UseCompressedOops",
  59         "-XX:ObjectAlignmentInBytes=16",
  60         "-XX:ObjectAlignmentInBytes=32",
  61         "-XX:ObjectAlignmentInBytes=64"
  62     };
  63 
  64     public static void main(String[] args) throws Exception {
  65         String appJar = JarBuilder.getOrCreateHelloJar();
  66         String classList[] = {"Hello"};
  67 
  68         for (String testEntry : testTable) {
  69             System.out.println("CommandLineFlagCombo = " + testEntry);
  70 
  71             if (skipTestCase(testEntry))
  72                 continue;
  73 
  74             OutputAnalyzer dumpOutput = TestCommon.dump(appJar, classList, testEntry);
  75             TestCommon.checkDump(dumpOutput, "Loading classes to share");
  76 
  77             OutputAnalyzer execOutput = TestCommon.exec(appJar, testEntry, "Hello");
  78             TestCommon.checkExec(execOutput, "Hello World");
  79         }
  80 
  81         for (int i=0; i<2; i++) {
  82             String g1Flag, serialFlag;
  83 
  84             // Interned strings are supported only with G1GC. However, we should not crash if:
  85             // 0: archive has shared strings, but run time doesn't support shared strings
  86             // 1: archive has no shared strings, but run time supports shared strings
  87 
  88             String dump_g1Flag     = "-XX:" + (i == 0 ? "+" : "-") + "UseG1GC";
  89             String run_g1Flag      = "-XX:" + (i != 0 ? "+" : "-") + "UseG1GC";
  90             String dump_serialFlag = "-XX:" + (i != 0 ? "+" : "-") + "UseSerialGC";
  91             String run_serialFlag  = "-XX:" + (i == 0 ? "+" : "-") + "UseSerialGC";
  92 
  93             OutputAnalyzer dumpOutput = TestCommon.dump(
  94                appJar, classList, dump_g1Flag, dump_serialFlag);
  95 
  96             TestCommon.checkDump(dumpOutput, "Loading classes to share");
  97 
  98             OutputAnalyzer execOutput = TestCommon.exec(appJar, run_g1Flag, run_serialFlag, "Hello");
  99             TestCommon.checkExec(execOutput, "Hello World");
 100         }
 101     }
 102 
 103     private static boolean skipTestCase(String testEntry) throws Exception {
 104         if (Platform.is32bit())
 105         {
 106             if (testEntry.equals("-XX:+UseCompressedOops") ||
 107                 testEntry.equals("-XX:+UseCompressedClassPointers") ||
 108                 testEntry.contains("ObjectAlignmentInBytes") )
 109             {
 110                 System.out.println("Test case not applicable on 32-bit platforms");
 111                 return true;
 112             }
 113         }
 114 
 115         if (Compiler.isGraalEnabled() && testEntry.equals("-XX:+UseConcMarkSweepGC"))
 116         {
 117             System.out.println("Graal does not support CMS");
 118             return true;
 119         }
 120 
 121         return false;
 122     }
 123 }