Chromium Code Reviews| Index: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/utilities/general/TimeCounter.java |
| diff --git a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/utilities/general/TimeCounter.java b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/utilities/general/TimeCounter.java |
| index b630b8943353d2a533cb008b7253a9e694f48e82..9bd28bdee3ab30c0bcb3eb73d7b5eb9e8e5e6551 100644 |
| --- a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/utilities/general/TimeCounter.java |
| +++ b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/utilities/general/TimeCounter.java |
| @@ -14,6 +14,10 @@ |
| package com.google.dart.engine.utilities.general; |
| +import com.google.common.collect.Lists; |
| + |
| +import java.util.LinkedList; |
| + |
| /** |
| * Helper for measuring how much time is spent doing some operation. Each call to |
| * {@link #recordElapsedNanos(long)} or each pair of calls to {@link #start()} and |
| @@ -31,17 +35,31 @@ public class TimeCounter { |
| * time to the counter. |
| */ |
| public void stop() { |
| - synchronized (TimeCounter.this) { |
| - recordElapsedNanos(System.nanoTime() - startTime); |
| - } |
| + recordElapsedNanos(System.nanoTime() - startTime); |
| } |
| } |
| + private static final ThreadLocal<LinkedList<TimeCounter>> stacks = new ThreadLocal<LinkedList<TimeCounter>>(); |
| public static final int NANOS_PER_MILLI = 1000 * 1000; |
| + /** |
| + * Returns the stack of {@link TimeCounter} started on the current {@link Thread} and not stopped |
| + * yet. |
| + */ |
| + private static LinkedList<TimeCounter> getCountersStack() { |
| + LinkedList<TimeCounter> stack = stacks.get(); |
| + if (stack == null) { |
| + stack = Lists.newLinkedList(); |
| + stacks.set(stack); |
| + } |
| + return stack; |
| + } |
| + |
| private long totalTime = 0L; |
| + private long correctionTime = 0L; |
| private long maxInterval = 0L; |
| private long minInterval = Long.MAX_VALUE; |
| + |
| private int intervalCount = 0; |
| /** |
| @@ -98,8 +116,16 @@ public class TimeCounter { |
| * |
| * @param delta the number of nanoseconds |
| */ |
| - public void recordElapsedNanos(long delta) { |
| - totalTime += delta; |
| + public synchronized void recordElapsedNanos(long delta) { |
| + // apply correction to the other counters on the thread stack |
| + LinkedList<TimeCounter> stack = getCountersStack(); |
| + stack.removeFirst(); // should be 'this' |
|
Brian Wilkerson
2014/04/08 04:29:51
We could add a check, and if the assumption is eve
scheglov
2014/04/08 05:08:45
Done.
|
| + for (TimeCounter timeCounter : stack) { |
| + timeCounter.correctionTime += delta; |
| + } |
| + // update statistics |
| + totalTime += delta - correctionTime; |
| + correctionTime = 0; |
| intervalCount++; |
| minInterval = Math.min(minInterval, delta); |
| maxInterval = Math.max(maxInterval, delta); |
| @@ -110,7 +136,8 @@ public class TimeCounter { |
| * |
| * @return the {@link TimeCounterHandle} that should be used to stop counting. |
| */ |
| - public TimeCounterHandle start() { |
| + public synchronized TimeCounterHandle start() { |
| + getCountersStack().addFirst(this); |
| return new TimeCounterHandle(); |
| } |
| } |