From 2f68cc4c97fdf11b63c8515208addcc53a967e71 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Fri, 26 Jun 2015 18:02:19 +0300 Subject: [PATCH] Created reset method. --- .../kotlin/util/PerformanceCounter.kt | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/util/PerformanceCounter.kt b/compiler/frontend/src/org/jetbrains/kotlin/util/PerformanceCounter.kt index e6caf3cac18..a0aa4e08b75 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/util/PerformanceCounter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/util/PerformanceCounter.kt @@ -19,6 +19,8 @@ package org.jetbrains.kotlin.util import java.lang.management.ManagementFactory import java.util.* import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong /** * This counter is thread-safe for initialization and usage. @@ -43,6 +45,14 @@ public abstract class PerformanceCounter protected constructor(val name: String) enabled = enable } + public fun resetAllCounters() { + synchronized(allCounters) { + allCounters.forEach { + it.reset() + } + } + } + public jvmOverloads fun create(name: String, reenterable: Boolean = false): PerformanceCounter { return if (reenterable) ReenterableCounter(name) @@ -65,7 +75,7 @@ public abstract class PerformanceCounter protected constructor(val name: String) protected val excludedFrom: MutableList = ArrayList() private var count: Int = 0 - protected var totalTimeNanos: Long = 0 + private var totalTimeNanos: Long = 0 init { synchronized(allCounters) { @@ -90,6 +100,15 @@ public abstract class PerformanceCounter protected constructor(val name: String) } } + public fun reset() { + count = 0 + totalTimeNanos = 0 + } + + protected final fun incrementTime(delta: Long) { + totalTimeNanos += delta + } + protected abstract fun countTime(block: () -> T): T public fun report(consumer: (String) -> Unit) { @@ -110,7 +129,7 @@ private class SimpleCounter(name: String): PerformanceCounter(name) { return block() } finally { - totalTimeNanos += PerformanceCounter.currentTime() - startTime + incrementTime(PerformanceCounter.currentTime() - startTime) } } } @@ -134,7 +153,7 @@ private class ReenterableCounter(name: String): PerformanceCounter(name) { } finally { if (needTime) { - totalTimeNanos += PerformanceCounter.currentTime() - startTime + incrementTime(PerformanceCounter.currentTime() - startTime) leaveCounter(this) } } @@ -163,21 +182,21 @@ private class CounterWithExclude(name: String, vararg excludedCounters: Performa get() = getCallStack(this) override fun countTime(block: () -> T): T { - totalTimeNanos += callStack.push(true) + incrementTime(callStack.push(true)) try { return block() } finally { - totalTimeNanos += callStack.pop(true) + incrementTime(callStack.pop(true)) } } fun enterExcludedMethod() { - totalTimeNanos += callStack.push(false) + incrementTime(callStack.push(false)) } fun exitExcludedMethod() { - totalTimeNanos += callStack.pop(false) + incrementTime(callStack.pop(false)) } private class CallStackWithTime {