src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/CooperativePhaseTest.java
changeset 48404 177e1783d886
parent 48380 597f69e5f1e3
parent 48403 6d4e1efac80a
child 48405 5f1c30b80554
equal deleted inserted replaced
48380:597f69e5f1e3 48404:177e1783d886
     1 /*
       
     2  * Copyright (c) 2016, 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 package org.graalvm.compiler.core.test;
       
    24 
       
    25 import static org.graalvm.compiler.core.common.util.CompilationAlarm.Options.CompilationExpirationPeriod;
       
    26 
       
    27 import org.graalvm.compiler.core.common.RetryableBailoutException;
       
    28 import org.graalvm.compiler.core.common.util.CompilationAlarm;
       
    29 import org.graalvm.compiler.debug.GraalError;
       
    30 import org.graalvm.compiler.nodes.StructuredGraph;
       
    31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
       
    32 import org.graalvm.compiler.options.OptionValues;
       
    33 import org.graalvm.compiler.phases.Phase;
       
    34 import org.junit.Test;
       
    35 
       
    36 public class CooperativePhaseTest extends GraalCompilerTest {
       
    37 
       
    38     public static void snippet() {
       
    39         // dummy snippet
       
    40     }
       
    41 
       
    42     private static class CooperativePhase extends Phase {
       
    43 
       
    44         @Override
       
    45         protected void run(StructuredGraph graph) {
       
    46             CompilationAlarm compilationAlarm = CompilationAlarm.current();
       
    47             while (true) {
       
    48                 sleep(200);
       
    49                 if (compilationAlarm.hasExpired()) {
       
    50                     return;
       
    51                 }
       
    52             }
       
    53         }
       
    54 
       
    55     }
       
    56 
       
    57     private static class UnCooperativePhase extends Phase {
       
    58 
       
    59         @Override
       
    60         protected void run(StructuredGraph graph) {
       
    61             CompilationAlarm compilationAlarm = CompilationAlarm.current();
       
    62             while (true) {
       
    63                 sleep(200);
       
    64                 if (compilationAlarm.hasExpired()) {
       
    65                     throw new RetryableBailoutException("Expiring...");
       
    66                 }
       
    67             }
       
    68         }
       
    69 
       
    70     }
       
    71 
       
    72     private static class PartiallyCooperativePhase extends Phase {
       
    73 
       
    74         @Override
       
    75         protected void run(StructuredGraph graph) {
       
    76             CompilationAlarm compilationAlarm = CompilationAlarm.current();
       
    77             for (int i = 0; i < 10; i++) {
       
    78                 sleep(200);
       
    79                 if (compilationAlarm.hasExpired()) {
       
    80                     throw new RuntimeException("Phase must not exit in the timeout path");
       
    81                 }
       
    82             }
       
    83         }
       
    84     }
       
    85 
       
    86     private static class CooperativePhaseWithoutAlarm extends Phase {
       
    87 
       
    88         @Override
       
    89         protected void run(StructuredGraph graph) {
       
    90             CompilationAlarm compilationAlarm = CompilationAlarm.current();
       
    91             if (compilationAlarm.hasExpired()) {
       
    92                 throw new RuntimeException("Phase must not exit in the timeout path");
       
    93             }
       
    94         }
       
    95     }
       
    96 
       
    97     private static void sleep(long millis) {
       
    98         try {
       
    99             Thread.sleep(millis);
       
   100         } catch (InterruptedException e) {
       
   101             GraalError.shouldNotReachHere(e.getCause());
       
   102         }
       
   103     }
       
   104 
       
   105     @Test(timeout = 60_000)
       
   106     @SuppressWarnings("try")
       
   107     public void test01() {
       
   108         initializeForTimeout();
       
   109         OptionValues initialOptions = getInitialOptions();
       
   110         OptionValues options = new OptionValues(initialOptions, CompilationExpirationPeriod, 1/* sec */);
       
   111         try (CompilationAlarm c1 = CompilationAlarm.trackCompilationPeriod(options)) {
       
   112             StructuredGraph g = parseEager("snippet", AllowAssumptions.NO, options);
       
   113             new CooperativePhase().apply(g);
       
   114         }
       
   115     }
       
   116 
       
   117     @Test(expected = RetryableBailoutException.class, timeout = 60_000)
       
   118     @SuppressWarnings("try")
       
   119     public void test02() {
       
   120         initializeForTimeout();
       
   121         OptionValues initialOptions = getInitialOptions();
       
   122         OptionValues options = new OptionValues(initialOptions, CompilationExpirationPeriod, 1/* sec */);
       
   123         try (CompilationAlarm c1 = CompilationAlarm.trackCompilationPeriod(options)) {
       
   124             StructuredGraph g = parseEager("snippet", AllowAssumptions.NO, options);
       
   125             new UnCooperativePhase().apply(g);
       
   126         }
       
   127     }
       
   128 
       
   129     @Test(timeout = 60_000)
       
   130     @SuppressWarnings("try")
       
   131     public void test03() {
       
   132         initializeForTimeout();
       
   133         // 0 disables alarm utility
       
   134         OptionValues initialOptions = getInitialOptions();
       
   135         OptionValues options = new OptionValues(initialOptions, CompilationExpirationPeriod, 0);
       
   136         try (CompilationAlarm c1 = CompilationAlarm.trackCompilationPeriod(options)) {
       
   137             StructuredGraph g = parseEager("snippet", AllowAssumptions.NO, options);
       
   138             new PartiallyCooperativePhase().apply(g);
       
   139         }
       
   140     }
       
   141 
       
   142     @Test(timeout = 60_000)
       
   143     @SuppressWarnings("try")
       
   144     public void test04() {
       
   145         initializeForTimeout();
       
   146         StructuredGraph g = parseEager("snippet", AllowAssumptions.NO);
       
   147         new CooperativePhaseWithoutAlarm().apply(g);
       
   148     }
       
   149 }