# HG changeset patch # User Doug Simon # Date 1378820428 -7200 # Node ID 6b6c6e5d0fc27820ad13c35ecce8638308bf6a1a # Parent e1309fc4d17f990e89325f24aa534db209b9cc17 added support for DebugMetric and DebugTimer objects to be unconditionally enabled (GRAAL-195) diff -r e1309fc4d17f -r 6b6c6e5d0fc2 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java Tue Sep 10 14:32:55 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java Tue Sep 10 15:40:28 2013 +0200 @@ -330,7 +330,7 @@ public static DebugMetric metric(String name) { if (ENABLED) { - return new MetricImpl(name); + return new MetricImpl(name, true); } else { return VOID_METRIC; } @@ -405,11 +405,19 @@ public void add(long value) { } + + public void setConditional(boolean flag) { + throw new InternalError("Cannot make void metric conditional"); + } + + public boolean isConditional() { + return false; + } }; public static DebugTimer timer(String name) { if (ENABLED) { - return new TimerImpl(name); + return new TimerImpl(name, true); } else { return VOID_TIMER; } @@ -420,5 +428,13 @@ public TimerCloseable start() { return TimerImpl.VOID_CLOSEABLE; } + + public void setConditional(boolean flag) { + throw new InternalError("Cannot make void timer conditional"); + } + + public boolean isConditional() { + return false; + } }; } diff -r e1309fc4d17f -r 6b6c6e5d0fc2 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugMetric.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugMetric.java Tue Sep 10 14:32:55 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugMetric.java Tue Sep 10 15:40:28 2013 +0200 @@ -22,9 +22,32 @@ */ package com.oracle.graal.debug; +/** + * A counter for some value of interest. + */ public interface DebugMetric { + /** + * Adds 1 to this counter if metering is {@link Debug#isMeterEnabled() enabled} or this is an + * {@linkplain #isConditional() unconditional} metric. + */ void increment(); + /** + * Adds {@code value} to this counter if metering is {@link Debug#isMeterEnabled() enabled} or + * this is an {@linkplain #isConditional() unconditional} metric. + */ void add(long value); + + /** + * Sets a flag determining if this counter is only enabled if metering is + * {@link Debug#isMeterEnabled() enabled}. + */ + void setConditional(boolean flag); + + /** + * Determines if this counter is only enabled if metering is {@link Debug#isMeterEnabled() + * enabled}. + */ + boolean isConditional(); } diff -r e1309fc4d17f -r 6b6c6e5d0fc2 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugTimer.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugTimer.java Tue Sep 10 14:32:55 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/DebugTimer.java Tue Sep 10 15:40:28 2013 +0200 @@ -24,7 +24,36 @@ import com.oracle.graal.debug.internal.*; +/** + * A timer for some activity of interest. A timer should be deployed using the try-with-resources + * pattern: + * + *
+ * try (TimerCloseable a = timer.start()) {
+ *     // the code to time
+ * }
+ * 
+ */ public interface DebugTimer { + /** + * Starts this timer if timing is {@linkplain Debug#isTimeEnabled() enabled} or this is an + * {@linkplain #isConditional() unconditional} timer. + * + * @return an object that must be closed once the activity has completed to add the elapsed time + * since this call to the total for this timer + */ TimerCloseable start(); + + /** + * Sets a flag determining if this timer is only enabled if metering is + * {@link Debug#isMeterEnabled() enabled}. + */ + void setConditional(boolean flag); + + /** + * Determines if this timer is only enabled if metering is {@link Debug#isMeterEnabled() + * enabled}. + */ + boolean isConditional(); } diff -r e1309fc4d17f -r 6b6c6e5d0fc2 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/MetricImpl.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/MetricImpl.java Tue Sep 10 14:32:55 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/MetricImpl.java Tue Sep 10 15:40:28 2013 +0200 @@ -26,8 +26,11 @@ public final class MetricImpl extends DebugValue implements DebugMetric { - public MetricImpl(String name) { + private boolean conditional; + + public MetricImpl(String name, boolean conditional) { super(name); + this.conditional = conditional; } public void increment() { @@ -35,7 +38,7 @@ } public void add(long value) { - if (Debug.isMeterEnabled()) { + if (!conditional || Debug.isMeterEnabled()) { super.addToCurrentValue(value); } } @@ -44,4 +47,12 @@ public String toString(long value) { return Long.toString(value); } + + public void setConditional(boolean flag) { + conditional = flag; + } + + public boolean isConditional() { + return conditional; + } } diff -r e1309fc4d17f -r 6b6c6e5d0fc2 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerCloseable.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerCloseable.java Tue Sep 10 14:32:55 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerCloseable.java Tue Sep 10 15:40:28 2013 +0200 @@ -22,6 +22,12 @@ */ package com.oracle.graal.debug.internal; +import com.oracle.graal.debug.*; + +/** + * An object returned by {@link DebugTimer#start()} that when closed, stops the associated timer and + * adds the elapsed time since {@code start()} to the total for the timer. + */ public interface TimerCloseable extends AutoCloseable { void close(); diff -r e1309fc4d17f -r 6b6c6e5d0fc2 graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerImpl.java --- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerImpl.java Tue Sep 10 14:32:55 2013 +0200 +++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerImpl.java Tue Sep 10 15:40:28 2013 +0200 @@ -38,9 +38,19 @@ }; private ThreadLocal valueToSubstract = new ThreadLocal<>(); + private boolean conditional; - public TimerImpl(String name) { + public TimerImpl(String name, boolean conditional) { super(name); + this.conditional = conditional; + } + + public void setConditional(boolean flag) { + conditional = flag; + } + + public boolean isConditional() { + return conditional; } @Override