changeset 11617:0d10c6717edf

fixed TimerImpl to account for (i.e., deduct) all nested timers, not just the most recent one
author Doug Simon <doug.simon@oracle.com>
date Thu, 12 Sep 2013 23:04:35 +0200
parents dc35a8134f4a
children 7f513590ac06
files graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerImpl.java
diffstat 1 files changed, 25 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerImpl.java	Thu Sep 12 22:58:36 2013 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/TimerImpl.java	Thu Sep 12 23:04:35 2013 +0200
@@ -37,7 +37,11 @@
         }
     };
 
-    private ThreadLocal<Long> valueToSubstract = new ThreadLocal<>();
+    /**
+     * Records the most recent active timer.
+     */
+    private static ThreadLocal<AbstractTimer> currentTimer = new ThreadLocal<>();
+
     private boolean conditional;
 
     public TimerImpl(String name, boolean conditional) {
@@ -62,17 +66,14 @@
             } else {
                 startTime = System.nanoTime();
             }
-            if (valueToSubstract.get() == null) {
-                valueToSubstract.set(0L);
-            }
-            long previousValueToSubstract = valueToSubstract.get();
+
             AbstractTimer result;
             if (threadMXBean.isCurrentThreadCpuTimeSupported()) {
-                result = new CpuTimeTimer(startTime, previousValueToSubstract);
+                result = new CpuTimeTimer(startTime);
             } else {
-                result = new SystemNanosTimer(startTime, previousValueToSubstract);
+                result = new SystemNanosTimer(startTime);
             }
-            valueToSubstract.set(0L);
+            currentTimer.set(result);
             return result;
         } else {
             return VOID_CLOSEABLE;
@@ -86,20 +87,25 @@
 
     private abstract class AbstractTimer implements TimerCloseable {
 
+        private final AbstractTimer parent;
         private final long startTime;
-        private final long previousValueToSubstract;
+        private long nestedTimeToSubtract;
 
-        private AbstractTimer(long startTime, long previousValueToSubstract) {
+        private AbstractTimer(long startTime) {
+            this.parent = currentTimer.get();
             this.startTime = startTime;
-            this.previousValueToSubstract = previousValueToSubstract;
         }
 
         @Override
         public void close() {
-            long timeSpan = currentTime() - startTime;
-            long oldValueToSubstract = valueToSubstract.get();
-            valueToSubstract.set(timeSpan + previousValueToSubstract);
-            TimerImpl.this.addToCurrentValue(timeSpan - oldValueToSubstract);
+            long endTime = currentTime();
+            long timeSpan = endTime - startTime;
+            if (parent != null) {
+                parent.nestedTimeToSubtract += timeSpan;
+            }
+            currentTimer.set(parent);
+            long flatTime = timeSpan - nestedTimeToSubtract;
+            TimerImpl.this.addToCurrentValue(flatTime);
         }
 
         protected abstract long currentTime();
@@ -107,8 +113,8 @@
 
     private final class SystemNanosTimer extends AbstractTimer {
 
-        public SystemNanosTimer(long startTime, long previousValueToSubstract) {
-            super(startTime, previousValueToSubstract);
+        public SystemNanosTimer(long startTime) {
+            super(startTime);
         }
 
         @Override
@@ -119,8 +125,8 @@
 
     private final class CpuTimeTimer extends AbstractTimer {
 
-        public CpuTimeTimer(long startTime, long previousValueToSubstract) {
-            super(startTime, previousValueToSubstract);
+        public CpuTimeTimer(long startTime) {
+            super(startTime);
         }
 
         @Override