1 /*
   2  * Copyright (c) 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 import jdk.test.lib.*;
  25 import jdk.test.lib.process.*;
  26 
  27 /*
  28  * @test TestAbortOnVMOperationTimeout
  29  * @bug 8181143
  30  * @summary Check abort on VM timeout is working
  31  * @library /test/lib
  32  * @modules java.base/jdk.internal.misc
  33  *          java.management
  34  */
  35 
  36 public class TestAbortOnVMOperationTimeout {
  37 
  38     public static void main(String[] args) throws Exception {
  39         if (args.length > 0) {
  40             Object[] arr = new Object[40_000_000];
  41             for (int i = 0; i < arr.length; i++) {
  42                arr[i] = new Object();
  43             }
  44             return;
  45         }
  46 
  47         // These should definitely pass: more than a minute is enough for Serial to act.
  48         // The values are deliberately non-round to trip off periodic task granularity.
  49         for (int delay : new int[]{63423, 12388131}) {
  50              testWith(delay, true);
  51         }
  52 
  53         // These should fail: Serial is not very fast. Traversing 40M objects in 5 ms
  54         // means less than 0.125 ns per object, which is not doable.
  55         for (int delay : new int[]{0, 1, 5}) {
  56              testWith(delay, false);
  57         }
  58     }
  59 
  60     public static void testWith(int delay, boolean shouldPass) throws Exception {
  61         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  62                 "-XX:+UnlockDiagnosticVMOptions",
  63                 "-XX:+AbortVMOnVMOperationTimeout",
  64                 "-XX:AbortVMOnVMOperationTimeoutDelay=" + delay,
  65                 "-Xmx1g",
  66                 "-XX:+UseSerialGC",
  67                 "TestAbortOnVMOperationTimeout",
  68                 "foo"
  69         );
  70 
  71         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  72         if (shouldPass) {
  73             output.shouldHaveExitValue(0);
  74         } else {
  75             output.shouldMatch("VM operation took too long");
  76             output.shouldNotHaveExitValue(0);
  77         }
  78     }
  79 }
  80