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