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 /* @test TestAllocateOldGenAtError.java
  25  * @key gc
  26  * @summary Test to check correct handling of non-existent directory passed to AllocateOldGenAt option
  27  * @requires vm.gc=="null" & os.family != "aix"
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  */
  31 
  32 import java.io.File;
  33 import jdk.test.lib.JDKToolFinder;
  34 import jdk.test.lib.process.ProcessTools;
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import java.util.ArrayList;
  37 import java.util.Collections;
  38 import java.util.UUID;
  39 
  40 public class TestAllocateOldGenAtError {
  41   private static ArrayList<String> commonOpts;
  42 
  43   public static void main(String args[]) throws Exception {
  44     commonOpts = new ArrayList();
  45 
  46     String testVmOptsStr = System.getProperty("test.java.opts");
  47     if (!testVmOptsStr.isEmpty()) {
  48       String[] testVmOpts = testVmOptsStr.split(" ");
  49       Collections.addAll(commonOpts, testVmOpts);
  50     }
  51     String test_dir = System.getProperty("test.dir", ".");
  52 
  53     File f = null;
  54     do {
  55       f = new File(test_dir, UUID.randomUUID().toString());
  56     } while(f.exists());
  57 
  58     Collections.addAll(commonOpts, new String[] {"-XX:+UnlockExperimentalVMOptions",
  59                                                  "-XX:AllocateOldGenAt=" + f.getName(),
  60                                                  "-Xlog:gc+heap=info",
  61                                                  "-Xmx32m",
  62                                                  "-Xms32m",
  63                                                  "-version"});
  64 
  65     testG1();
  66     testParallelOld();
  67   }
  68 
  69   private static void testG1() throws Exception {
  70     System.out.println("Testing G1 GC");
  71 
  72     OutputAnalyzer output = runTest("-XX:+UseG1GC");
  73 
  74     output.shouldContain("Could not initialize G1 heap");
  75     output.shouldContain("Error occurred during initialization of VM");
  76     output.shouldNotHaveExitValue(0);
  77 
  78   }
  79 
  80   private static void testParallelOld() throws Exception {
  81     System.out.println("Testing ParallelOld GC with UseAdaptiveGCBoundary disabled");
  82     OutputAnalyzer output = runTest("-XX:+UseParallelOldGC -XX:-UseAdaptiveGCBoundary");
  83     output.shouldContain("Error occurred during initialization of VM");
  84     output.shouldNotHaveExitValue(0);
  85 
  86     System.out.println("Testing ParallelOld GC with UseAdaptiveGCBoundary enabled");
  87     output = runTest("-XX:+UseParallelOldGC -XX:+UseAdaptiveGCBoundary");
  88     output.shouldContain("Error occurred during initialization of VM");
  89     output.shouldNotHaveExitValue(0);
  90   }
  91 
  92   private static OutputAnalyzer runTest(String... extraFlags) throws Exception {
  93     ArrayList<String> testOpts = new ArrayList();
  94     Collections.addAll(testOpts, commonOpts.toArray(new String[commonOpts.size()]));
  95     Collections.addAll(testOpts, extraFlags);
  96 
  97     System.out.print("Testing:\n" + JDKToolFinder.getJDKTool("java"));
  98     for (int i = 0; i < testOpts.size(); i += 1) {
  99       System.out.print(" " + testOpts.get(i));
 100     }
 101     System.out.println();
 102 
 103     ProcessBuilder pb =
 104       ProcessTools.createJavaProcessBuilder(testOpts.toArray(new String[testOpts.size()]));
 105     OutputAnalyzer output = new OutputAnalyzer(pb.start());
 106     return output;
 107   }
 108 }