1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. 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  * @test TestDieDefault
  26  * @summary Epsilon GC should die on heap exhaustion
  27  * @library /test/lib
  28  * @run main TestDieDefault
  29  */
  30 
  31 import jdk.test.lib.process.OutputAnalyzer;
  32 import jdk.test.lib.process.ProcessTools;
  33 
  34 public class TestDieDefault extends AbstractEpsilonTest {
  35 
  36   public static void passWith(String... args) throws Exception {
  37     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
  38     OutputAnalyzer out = new OutputAnalyzer(pb.start());
  39     out.shouldNotContain("OutOfMemoryError");
  40     out.shouldHaveExitValue(0);
  41   }
  42 
  43   public static void failWith(String... args) throws Exception {
  44     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
  45     OutputAnalyzer out = new OutputAnalyzer(pb.start());
  46     out.shouldContain("OutOfMemoryError");
  47     if (out.getExitValue() == 0) {
  48       throw new IllegalStateException("Should have failed with non-zero exit code");
  49     }
  50   }
  51 
  52   public static void main(String[] args) throws Exception {
  53     if (!isEpsilonEnabled()) return;
  54 
  55     passWith("-Xmx128m",
  56              "-XX:+UnlockExperimentalVMOptions",
  57              "-XX:+UseEpsilonGC",
  58              "-Dcount=1",
  59              TestDieDefault.Workload.class.getName());
  60 
  61     failWith("-Xmx128m",
  62              "-XX:+UnlockExperimentalVMOptions",
  63              "-XX:+UseEpsilonGC",
  64              TestDieDefault.Workload.class.getName());
  65 
  66     failWith("-Xmx128m",
  67              "-Xint",
  68              "-XX:+UnlockExperimentalVMOptions",
  69              "-XX:+UseEpsilonGC",
  70              TestDieDefault.Workload.class.getName());
  71 
  72     failWith("-Xmx128m",
  73              "-Xbatch",
  74              "-Xcomp",
  75              "-XX:+UnlockExperimentalVMOptions",
  76              "-XX:+UseEpsilonGC",
  77              TestDieDefault.Workload.class.getName());
  78 
  79     failWith("-Xmx128m",
  80              "-Xbatch",
  81              "-Xcomp",
  82              "-XX:TieredStopAtLevel=1",
  83              "-XX:+UnlockExperimentalVMOptions",
  84              "-XX:+UseEpsilonGC",
  85              TestDieDefault.Workload.class.getName());
  86 
  87     failWith("-Xmx128m",
  88              "-Xbatch",
  89              "-Xcomp",
  90              "-XX:TieredStopAtLevel=4",
  91              "-XX:+UnlockExperimentalVMOptions",
  92              "-XX:+UseEpsilonGC",
  93              TestDieDefault.Workload.class.getName());
  94   }
  95 
  96   public static class Workload {
  97     static int COUNT = Integer.getInteger("count", 1_000_000_000); // ~24 GB allocation
  98 
  99     static volatile Object sink;
 100 
 101     public static void main(String... args) {
 102       for (int c = 0; c < COUNT; c++) {
 103         sink = new Object();
 104       }
 105     }
 106   }
 107 
 108 }