From 298a27951fc1fa760c8f59c2483df991faccec07 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Thu, 25 Jun 2015 16:05:18 +0300 Subject: [PATCH] Use System.nanoTime() instead threadMxBean.getCurrentThreadUserTime() --- .../compiler/KotlinToJVMBytecodeCompiler.java | 8 +++---- .../kotlin/util/PerformanceCounter.kt | 23 +++++++++---------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java index e0908706940..b75a88baaaf 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.java @@ -314,7 +314,7 @@ public class KotlinToJVMBytecodeCompiler { MessageCollector collector = environment.getConfiguration().get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY); assert collector != null; - long analysisStart = PerformanceCounter.Companion.currentThreadCpuTime(); + long analysisStart = PerformanceCounter.Companion.currentTime(); AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(collector); analyzerWithCompilerReport.analyzeAndReport( environment.getSourceFiles(), new Function0() { @@ -334,7 +334,7 @@ public class KotlinToJVMBytecodeCompiler { } } ); - long analysisNanos = PerformanceCounter.Companion.currentThreadCpuTime() - analysisStart; + long analysisNanos = PerformanceCounter.Companion.currentTime() - analysisStart; String message = "ANALYZE: " + environment.getSourceFiles().size() + " files (" + environment.getSourceLinesOfCode() + " lines) " + (targetDescription != null ? targetDescription : "") + @@ -395,11 +395,11 @@ public class KotlinToJVMBytecodeCompiler { ); ProgressIndicatorAndCompilationCanceledStatus.checkCanceled(); - long generationStart = PerformanceCounter.Companion.currentThreadCpuTime(); + long generationStart = PerformanceCounter.Companion.currentTime(); KotlinCodegenFacade.compileCorrectFiles(generationState, CompilationErrorHandler.THROW_EXCEPTION); - long generationNanos = PerformanceCounter.Companion.currentThreadCpuTime() - generationStart; + long generationNanos = PerformanceCounter.Companion.currentTime() - generationStart; String desc = moduleId != null ? "module " + moduleId + " " : ""; String message = "GENERATE: " + sourceFiles.size() + " files (" + environment.countLinesOfCode(sourceFiles) + " lines) " + desc + "in " + TimeUnit.NANOSECONDS.toMillis(generationNanos) + " ms"; diff --git a/compiler/frontend/src/org/jetbrains/kotlin/util/PerformanceCounter.kt b/compiler/frontend/src/org/jetbrains/kotlin/util/PerformanceCounter.kt index 96ebd30ca8d..e6caf3cac18 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/util/PerformanceCounter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/util/PerformanceCounter.kt @@ -20,18 +20,17 @@ import java.lang.management.ManagementFactory import java.util.* import java.util.concurrent.TimeUnit +/** + * This counter is thread-safe for initialization and usage. + * But it may calculate time and number of runs not precisely. + */ public abstract class PerformanceCounter protected constructor(val name: String) { companion object { - private val threadMxBean = ManagementFactory.getThreadMXBean() private val allCounters = arrayListOf() private var enabled = false - init { - threadMxBean.setThreadCpuTimeEnabled(true) - } - - public fun currentThreadCpuTime(): Long = threadMxBean.getCurrentThreadUserTime() + public fun currentTime(): Long = System.nanoTime() public fun report(consumer: (String) -> Unit) { val countersCopy = synchronized(allCounters) { @@ -106,12 +105,12 @@ public abstract class PerformanceCounter protected constructor(val name: String) private class SimpleCounter(name: String): PerformanceCounter(name) { override fun countTime(block: () -> T): T { - val startTime = PerformanceCounter.currentThreadCpuTime() + val startTime = PerformanceCounter.currentTime() try { return block() } finally { - totalTimeNanos += PerformanceCounter.currentThreadCpuTime() - startTime + totalTimeNanos += PerformanceCounter.currentTime() - startTime } } } @@ -128,14 +127,14 @@ private class ReenterableCounter(name: String): PerformanceCounter(name) { } override fun countTime(block: () -> T): T { - val startTime = PerformanceCounter.currentThreadCpuTime() + val startTime = PerformanceCounter.currentTime() val needTime = enterCounter(this) try { return block() } finally { if (needTime) { - totalTimeNanos += PerformanceCounter.currentThreadCpuTime() - startTime + totalTimeNanos += PerformanceCounter.currentTime() - startTime leaveCounter(this) } } @@ -188,10 +187,10 @@ private class CounterWithExclude(name: String, vararg excludedCounters: Performa fun Stack.peekOrFalse() = if (isEmpty()) false else peek() private fun intervalUsefulTime(callStackUpdate: Stack.() -> Unit): Long { - val delta = if (callStack.peekOrFalse()) PerformanceCounter.currentThreadCpuTime() - intervalStartTime else 0 + val delta = if (callStack.peekOrFalse()) PerformanceCounter.currentTime() - intervalStartTime else 0 callStack.callStackUpdate() - intervalStartTime = PerformanceCounter.currentThreadCpuTime() + intervalStartTime = PerformanceCounter.currentTime() return delta }