Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Unified Diff: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/utilities/general/TimeCounter.java

Issue 225183020: Correct or pause previous TimeCounters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/MainEngine.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
}
}
« no previous file with comments | « no previous file | editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/MainEngine.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698