Use System.nanoTime() instead threadMxBean.getCurrentThreadUserTime()
This commit is contained in:
+4
-4
@@ -314,7 +314,7 @@ public class KotlinToJVMBytecodeCompiler {
|
|||||||
MessageCollector collector = environment.getConfiguration().get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
|
MessageCollector collector = environment.getConfiguration().get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
|
||||||
assert collector != null;
|
assert collector != null;
|
||||||
|
|
||||||
long analysisStart = PerformanceCounter.Companion.currentThreadCpuTime();
|
long analysisStart = PerformanceCounter.Companion.currentTime();
|
||||||
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(collector);
|
AnalyzerWithCompilerReport analyzerWithCompilerReport = new AnalyzerWithCompilerReport(collector);
|
||||||
analyzerWithCompilerReport.analyzeAndReport(
|
analyzerWithCompilerReport.analyzeAndReport(
|
||||||
environment.getSourceFiles(), new Function0<AnalysisResult>() {
|
environment.getSourceFiles(), new Function0<AnalysisResult>() {
|
||||||
@@ -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 (" +
|
String message = "ANALYZE: " + environment.getSourceFiles().size() + " files (" +
|
||||||
environment.getSourceLinesOfCode() + " lines) " +
|
environment.getSourceLinesOfCode() + " lines) " +
|
||||||
(targetDescription != null ? targetDescription : "") +
|
(targetDescription != null ? targetDescription : "") +
|
||||||
@@ -395,11 +395,11 @@ public class KotlinToJVMBytecodeCompiler {
|
|||||||
);
|
);
|
||||||
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
|
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
|
||||||
|
|
||||||
long generationStart = PerformanceCounter.Companion.currentThreadCpuTime();
|
long generationStart = PerformanceCounter.Companion.currentTime();
|
||||||
|
|
||||||
KotlinCodegenFacade.compileCorrectFiles(generationState, CompilationErrorHandler.THROW_EXCEPTION);
|
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 desc = moduleId != null ? "module " + moduleId + " " : "";
|
||||||
String message = "GENERATE: " + sourceFiles.size() + " files (" +
|
String message = "GENERATE: " + sourceFiles.size() + " files (" +
|
||||||
environment.countLinesOfCode(sourceFiles) + " lines) " + desc + "in " + TimeUnit.NANOSECONDS.toMillis(generationNanos) + " ms";
|
environment.countLinesOfCode(sourceFiles) + " lines) " + desc + "in " + TimeUnit.NANOSECONDS.toMillis(generationNanos) + " ms";
|
||||||
|
|||||||
@@ -20,18 +20,17 @@ import java.lang.management.ManagementFactory
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.TimeUnit
|
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) {
|
public abstract class PerformanceCounter protected constructor(val name: String) {
|
||||||
companion object {
|
companion object {
|
||||||
private val threadMxBean = ManagementFactory.getThreadMXBean()
|
|
||||||
private val allCounters = arrayListOf<PerformanceCounter>()
|
private val allCounters = arrayListOf<PerformanceCounter>()
|
||||||
|
|
||||||
private var enabled = false
|
private var enabled = false
|
||||||
|
|
||||||
init {
|
public fun currentTime(): Long = System.nanoTime()
|
||||||
threadMxBean.setThreadCpuTimeEnabled(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun currentThreadCpuTime(): Long = threadMxBean.getCurrentThreadUserTime()
|
|
||||||
|
|
||||||
public fun report(consumer: (String) -> Unit) {
|
public fun report(consumer: (String) -> Unit) {
|
||||||
val countersCopy = synchronized(allCounters) {
|
val countersCopy = synchronized(allCounters) {
|
||||||
@@ -106,12 +105,12 @@ public abstract class PerformanceCounter protected constructor(val name: String)
|
|||||||
|
|
||||||
private class SimpleCounter(name: String): PerformanceCounter(name) {
|
private class SimpleCounter(name: String): PerformanceCounter(name) {
|
||||||
override fun <T> countTime(block: () -> T): T {
|
override fun <T> countTime(block: () -> T): T {
|
||||||
val startTime = PerformanceCounter.currentThreadCpuTime()
|
val startTime = PerformanceCounter.currentTime()
|
||||||
try {
|
try {
|
||||||
return block()
|
return block()
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
totalTimeNanos += PerformanceCounter.currentThreadCpuTime() - startTime
|
totalTimeNanos += PerformanceCounter.currentTime() - startTime
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,14 +127,14 @@ private class ReenterableCounter(name: String): PerformanceCounter(name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun <T> countTime(block: () -> T): T {
|
override fun <T> countTime(block: () -> T): T {
|
||||||
val startTime = PerformanceCounter.currentThreadCpuTime()
|
val startTime = PerformanceCounter.currentTime()
|
||||||
val needTime = enterCounter(this)
|
val needTime = enterCounter(this)
|
||||||
try {
|
try {
|
||||||
return block()
|
return block()
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (needTime) {
|
if (needTime) {
|
||||||
totalTimeNanos += PerformanceCounter.currentThreadCpuTime() - startTime
|
totalTimeNanos += PerformanceCounter.currentTime() - startTime
|
||||||
leaveCounter(this)
|
leaveCounter(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,10 +187,10 @@ private class CounterWithExclude(name: String, vararg excludedCounters: Performa
|
|||||||
fun Stack<Boolean>.peekOrFalse() = if (isEmpty()) false else peek()
|
fun Stack<Boolean>.peekOrFalse() = if (isEmpty()) false else peek()
|
||||||
|
|
||||||
private fun intervalUsefulTime(callStackUpdate: Stack<Boolean>.() -> Unit): Long {
|
private fun intervalUsefulTime(callStackUpdate: Stack<Boolean>.() -> Unit): Long {
|
||||||
val delta = if (callStack.peekOrFalse()) PerformanceCounter.currentThreadCpuTime() - intervalStartTime else 0
|
val delta = if (callStack.peekOrFalse()) PerformanceCounter.currentTime() - intervalStartTime else 0
|
||||||
callStack.callStackUpdate()
|
callStack.callStackUpdate()
|
||||||
|
|
||||||
intervalStartTime = PerformanceCounter.currentThreadCpuTime()
|
intervalStartTime = PerformanceCounter.currentTime()
|
||||||
return delta
|
return delta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user