src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.common/src/org/graalvm/compiler/core/common/util/CompilationAlarm.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Cdiff src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.common/src/org/graalvm/compiler/core/common/util/CompilationAlarm.java

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.common/src/org/graalvm/compiler/core/common/util/CompilationAlarm.java

Print this page

        

*** 21,113 **** * questions. */ package org.graalvm.compiler.core.common.util; import org.graalvm.compiler.options.Option; import org.graalvm.compiler.options.OptionType; ! import org.graalvm.compiler.options.OptionValue; /** * Utility class that allows the compiler to monitor compilations that take a very long time. */ public final class CompilationAlarm implements AutoCloseable { public static class Options { // @formatter:off @Option(help = "Time limit in seconds before a compilation expires (0 to disable the limit).", type = OptionType.Debug) ! public static final OptionValue<Integer> CompilationExpirationPeriod = new OptionValue<>(300); // @formatter:on } ! private CompilationAlarm() { ! } ! ! private static boolean enabled() { ! return Options.CompilationExpirationPeriod.getValue() > 0; } /** ! * Thread local storage for compilation start timestamps. Everytime a compiler thread calls ! * {@link #trackCompilationPeriod()} it will save the start timestamp of the compilation. */ ! private static final ThreadLocal<Long> compilationStartedTimeStamps = new ThreadLocal<>(); ! private static boolean compilationStarted() { ! if (enabled()) { ! Long start = compilationStartedTimeStamps.get(); ! if (start == null) { ! compilationStartedTimeStamps.set(System.currentTimeMillis()); ! return true; ! } ! } ! return false; ! } ! private static void compilationFinished() { ! if (enabled()) { ! assert compilationStartedTimeStamps.get() != null; ! compilationStartedTimeStamps.set(null); ! } } /** ! * Determines if the current compilation is expired. A compilation expires if it takes longer ! * than {@linkplain CompilationAlarm.Options#CompilationExpirationPeriod}. * * @return {@code true} if the current compilation already takes longer than * {@linkplain CompilationAlarm.Options#CompilationExpirationPeriod}, {@code false} * otherwise */ ! public static boolean hasExpired() { ! if (enabled()) { ! Long start = compilationStartedTimeStamps.get(); ! if (start != null) { ! long time = System.currentTimeMillis(); ! assert time >= start; ! return time - start > Options.CompilationExpirationPeriod.getValue() * 1000; ! } ! } ! return false; } @Override public void close() { ! compilationFinished(); } ! private static final CompilationAlarm INSTANCE = enabled() ? new CompilationAlarm() : null; /** ! * Gets an object that can be used in a try-with-resource statement to set an time limit based ! * alarm for a compilation. * ! * @return a {@link CompilationAlarm} instance if there is no current alarm for the calling ! * thread otherwise {@code null} */ ! public static CompilationAlarm trackCompilationPeriod() { ! if (compilationStarted()) { ! return INSTANCE; } return null; } - } --- 21,108 ---- * questions. */ package org.graalvm.compiler.core.common.util; import org.graalvm.compiler.options.Option; + import org.graalvm.compiler.options.OptionKey; import org.graalvm.compiler.options.OptionType; ! import org.graalvm.compiler.options.OptionValues; /** * Utility class that allows the compiler to monitor compilations that take a very long time. */ public final class CompilationAlarm implements AutoCloseable { public static class Options { // @formatter:off @Option(help = "Time limit in seconds before a compilation expires (0 to disable the limit).", type = OptionType.Debug) ! public static final OptionKey<Integer> CompilationExpirationPeriod = new OptionKey<>(300); // @formatter:on } ! private CompilationAlarm(long expiration) { ! this.expiration = expiration; } /** ! * Thread local storage for the active compilation alarm. */ ! private static final ThreadLocal<CompilationAlarm> currentAlarm = new ThreadLocal<>(); ! private static final CompilationAlarm NEVER_EXPIRES = new CompilationAlarm(0); ! /** ! * Gets the current compilation alarm. If there is no current alarm, a non-null value is ! * returned that will always return {@code false} for {@link #hasExpired()}. ! */ ! public static CompilationAlarm current() { ! CompilationAlarm alarm = currentAlarm.get(); ! return alarm == null ? NEVER_EXPIRES : alarm; } /** ! * Determines if this alarm has expired. A compilation expires if it takes longer than ! * {@linkplain CompilationAlarm.Options#CompilationExpirationPeriod}. * * @return {@code true} if the current compilation already takes longer than * {@linkplain CompilationAlarm.Options#CompilationExpirationPeriod}, {@code false} * otherwise */ ! public boolean hasExpired() { ! return this != NEVER_EXPIRES && System.currentTimeMillis() > expiration; } @Override public void close() { ! if (this != NEVER_EXPIRES) { ! currentAlarm.set(null); ! } } ! /** ! * The time at which this alarm expires. ! */ ! private final long expiration; /** ! * Starts an alarm for setting a time limit on a compilation if there isn't already an active ! * alarm and {@link CompilationAlarm.Options#CompilationExpirationPeriod}{@code > 0}. The ! * returned value can be used in a try-with-resource statement to disable the alarm once the ! * compilation is finished. * ! * @return a {@link CompilationAlarm} if there was no current alarm for the calling thread ! * before this call otherwise {@code null} */ ! public static CompilationAlarm trackCompilationPeriod(OptionValues options) { ! int period = Options.CompilationExpirationPeriod.getValue(options); ! if (period > 0) { ! CompilationAlarm current = currentAlarm.get(); ! if (current == null) { ! long expiration = System.currentTimeMillis() + period * 1000; ! current = new CompilationAlarm(expiration); ! currentAlarm.set(current); ! return current; ! } } return null; } }
src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.common/src/org/graalvm/compiler/core/common/util/CompilationAlarm.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File