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 org.junit.Test; 26 27 import org.graalvm.compiler.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.OptionValue; 33 import org.graalvm.compiler.options.OptionValue.OverrideScope; 34 import org.graalvm.compiler.phases.Phase; 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 while (true) { 47 sleep(200); 48 if (CompilationAlarm.hasExpired()) { 49 return; 50 } 51 } 52 } 53 54 } 55 56 private static class UnCooperativePhase extends Phase { 57 58 @Override 59 protected void run(StructuredGraph graph) { 60 while (true) { 61 sleep(200); 62 if (CompilationAlarm.hasExpired()) { 63 throw new RetryableBailoutException("Expiring..."); 64 } 65 } 66 } 67 68 } 69 70 private static class ParlyCooperativePhase extends Phase { 71 72 @Override 73 protected void run(StructuredGraph graph) { 74 for (int i = 0; i < 10; i++) { 75 sleep(200); 76 if (CompilationAlarm.hasExpired()) { 77 throw new RuntimeException("Phase must not exit in the timeout path"); 78 } 79 } 80 } 81 } 82 83 private static class CooperativePhaseWithoutAlarm extends Phase { 84 85 @Override 86 protected void run(StructuredGraph graph) { 87 if (CompilationAlarm.hasExpired()) { 88 throw new RuntimeException("Phase must not exit in the timeout path"); 89 } 90 } 91 } 92 93 private static void sleep(long millis) { 94 try { 95 Thread.sleep(millis); 96 } catch (InterruptedException e) { 97 GraalError.shouldNotReachHere(e.getCause()); 98 } 99 } 100 101 @Test(timeout = 60_000) 102 @SuppressWarnings("try") 103 public void test01() { 104 StructuredGraph g = parseEager("snippet", AllowAssumptions.NO); 105 try (OverrideScope o = OptionValue.override(CompilationAlarm.Options.CompilationExpirationPeriod, 1/* sec */); 106 CompilationAlarm c1 = CompilationAlarm.trackCompilationPeriod()) { 107 new CooperativePhase().apply(g); 108 } 109 } 110 111 @Test(expected = RetryableBailoutException.class, timeout = 60_000) 112 @SuppressWarnings("try") 113 public void test02() { 114 StructuredGraph g = parseEager("snippet", AllowAssumptions.NO); 115 try (OverrideScope o = OptionValue.override(CompilationAlarm.Options.CompilationExpirationPeriod, 1/* sec */); 116 CompilationAlarm c1 = CompilationAlarm.trackCompilationPeriod()) { 117 new UnCooperativePhase().apply(g); 118 } 119 } 120 121 @Test(timeout = 60_000) 122 @SuppressWarnings("try") 123 public void test03() { 124 StructuredGraph g = parseEager("snippet", AllowAssumptions.NO); 125 // 0 disables alarm utility 126 try (OverrideScope o = OptionValue.override(CompilationAlarm.Options.CompilationExpirationPeriod, 0); 127 CompilationAlarm c1 = CompilationAlarm.trackCompilationPeriod()) { 128 new ParlyCooperativePhase().apply(g); 129 } 130 } 131 132 @Test(timeout = 60_000) 133 @SuppressWarnings("try") 134 public void test04() { 135 StructuredGraph g = parseEager("snippet", AllowAssumptions.NO); 136 new CooperativePhaseWithoutAlarm().apply(g); 137 } 138 } | 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 } |