performing cleanup (and memory usage reporting) only if kotlin compiler was actually called, should fix e.g. KT-10127

This commit is contained in:
Ilya Chernikov
2016-02-18 13:02:31 +01:00
parent d0efd4083f
commit 8065e490e8
3 changed files with 31 additions and 16 deletions
@@ -42,6 +42,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
public var kotlinOptions: T = createBlankArgs()
public var kotlinDestinationDir: File? = destinationDir
var compilerCalled: Boolean = false
private val loggerInstance = Logging.getLogger(this.javaClass)
override fun getLogger() = loggerInstance
@@ -56,6 +57,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
return
}
compilerCalled = true
populateCommonArgs(args, sources)
populateTargetSpecificArgs(args)
callCompiler(args)
@@ -19,7 +19,9 @@ package org.jetbrains.kotlin.gradle.plugin
import org.apache.commons.lang.SystemUtils
import org.gradle.BuildAdapter
import org.gradle.BuildResult
import org.gradle.api.Project
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.compile.AbstractCompile
import java.util.concurrent.ScheduledExecutorService
import kotlin.properties.Delegates
@@ -34,7 +36,7 @@ private fun comparableVersionStr(version: String) =
?.let { if (it.all { (it?.value?.length ?: 0).let { it > 0 && it < 4 }}) it else null }
?.joinToString(".", transform = { it!!.value.padStart(3, '0') })
class CleanUpBuildListener(pluginClassLoader: ClassLoader) : BuildAdapter() {
class CleanUpBuildListener(pluginClassLoader: ClassLoader, private val project: Project) : BuildAdapter() {
companion object {
const val FORCE_SYSTEM_GC_MESSAGE = "Forcing System.gc()"
}
@@ -53,23 +55,35 @@ class CleanUpBuildListener(pluginClassLoader: ClassLoader) : BuildAdapter() {
}
override fun buildFinished(result: BuildResult?) {
log.kotlinDebug("Build finished listener")
val gradle = result?.gradle
if (gradle != null) {
cleanup(gradle.gradleVersion)
// checking thread leaks only then cleaning up
threadTracker?.checkThreadLeak(gradle)
val kotlinCompilerCalled = project.tasks.filter { it.name.contains("kotlin", ignoreCase = true) }
.any { task -> task.hasProperty("compilerCalled") && task.property("compilerCalled") as? Boolean ?: false }
if (kotlinCompilerCalled) {
log.kotlinDebug("Cleanup after kotlin")
cleanup(gradle.gradleVersion)
// checking thread leaks only then cleaning up
threadTracker?.checkThreadLeak(gradle)
}
else {
log.kotlinDebug("Skipping kotlin cleanup since compiler wasn't called")
}
threadTracker = null
gradle.removeListener(this)
}
startMemory?.let { startMemoryCopy ->
getUsedMemoryKb()?.let { endMemory ->
// the value reported here is not necessarily a leak, since it is calculated before collecting the plugin classes
// but on subsequent runs in the daemon it should be rather small, then the classes are actually reused by the daemon (see above)
log.kotlinDebug("[PERF] Used memory after build: $endMemory kb (difference since build start: ${"%+d".format(endMemory - startMemoryCopy)} kb)")
if (kotlinCompilerCalled) {
startMemory?.let { startMemoryCopy ->
getUsedMemoryKb()?.let { endMemory ->
// the value reported here is not necessarily a leak, since it is calculated before collecting the plugin classes
// but on subsequent runs in the daemon it should be rather small, then the classes are actually reused by the daemon (see above)
log.kotlinDebug("[PERF] Used memory after build: $endMemory kb (difference since build start: ${"%+d".format(endMemory - startMemoryCopy)} kb)")
}
}
}
}
}
@@ -14,10 +14,6 @@ abstract class KotlinBasePluginWrapper: Plugin<Project> {
val log = Logging.getLogger(this.javaClass)
override fun apply(project: Project) {
val cleanUpBuildListener = CleanUpBuildListener(this.javaClass.classLoader)
cleanUpBuildListener.buildStarted()
project.gradle.addBuildListener(cleanUpBuildListener)
val sourceBuildScript = findSourceBuildScript(project)
if (sourceBuildScript == null) {
log.error("Failed to determine source cofiguration of kotlin plugin. Can not download core. Please verify that this or any parent project " +
@@ -31,6 +27,9 @@ abstract class KotlinBasePluginWrapper: Plugin<Project> {
val plugin = getPlugin(this.javaClass.classLoader, sourceBuildScript)
plugin.apply(project)
val cleanUpBuildListener = CleanUpBuildListener(this.javaClass.classLoader, project)
cleanUpBuildListener.buildStarted()
project.gradle.addBuildListener(cleanUpBuildListener)
}
protected abstract fun getPlugin(pluginClassLoader: ClassLoader, scriptHandler: ScriptHandler): Plugin<Project>