Skip to content

Commit f3112f7

Browse files
Inherit max metaspace size of client VM for Kotlin compile daemon
#KT-32521 In Progress #KT-32950 Fixed
1 parent 5c99243 commit f3112f7

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/DaemonParams.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,14 @@ fun Iterable<String>.filterExtractProps(vararg groups: OptionsGroup, prefix: Str
187187
data class DaemonJVMOptions(
188188
var maxMemory: String = "",
189189
var maxPermSize: String = "",
190+
var maxMetaspaceSize: String = "",
190191
var reservedCodeCacheSize: String = "",
191192
var jvmParams: MutableCollection<String> = arrayListOf()
192193
) : OptionsGroup {
193-
194194
override val mappers: List<PropMapper<*, *, *>>
195195
get() = listOf(StringPropMapper(this, DaemonJVMOptions::maxMemory, listOf("Xmx"), mergeDelimiter = ""),
196196
StringPropMapper(this, DaemonJVMOptions::maxPermSize, listOf("XX:MaxPermSize"), mergeDelimiter = "="),
197+
StringPropMapper(this, DaemonJVMOptions::maxMetaspaceSize, listOf("XX:MaxMetaspaceSize"), mergeDelimiter = "="),
197198
StringPropMapper(this, DaemonJVMOptions::reservedCodeCacheSize, listOf("XX:ReservedCodeCacheSize"), mergeDelimiter = "="),
198199
restMapper)
199200

@@ -279,11 +280,13 @@ fun configureDaemonJVMOptions(opts: DaemonJVMOptions,
279280
val targetOptions = if (inheritMemoryLimits) opts else DaemonJVMOptions()
280281
val otherArgs = jvmArguments.filterExtractProps(targetOptions.mappers, prefix = "-")
281282

282-
if (inheritMemoryLimits && opts.maxMemory.isBlank()) {
283-
val maxMemBytes = Runtime.getRuntime().maxMemory()
284-
// rounding up
285-
val maxMemMegabytes = maxMemBytes / (1024 * 1024) + if (maxMemBytes % (1024 * 1024) == 0L) 0 else 1
286-
opts.maxMemory = "${maxMemMegabytes}m"
283+
if (inheritMemoryLimits) {
284+
if (opts.maxMemory.isBlank()) {
285+
val maxMemBytes = Runtime.getRuntime().maxMemory()
286+
// rounding up
287+
val maxMemMegabytes = maxMemBytes / (1024 * 1024) + if (maxMemBytes % (1024 * 1024) == 0L) 0 else 1
288+
opts.maxMemory = "${maxMemMegabytes}m"
289+
}
287290
}
288291

289292
if (inheritOtherJvmOptions) {
@@ -365,7 +368,7 @@ private fun String.memToBytes(): Long? =
365368

366369

367370
private val daemonJVMOptionsMemoryProps =
368-
listOf(DaemonJVMOptions::maxMemory, DaemonJVMOptions::maxPermSize, DaemonJVMOptions::reservedCodeCacheSize)
371+
listOf(DaemonJVMOptions::maxMemory, DaemonJVMOptions::maxPermSize, DaemonJVMOptions::maxMetaspaceSize, DaemonJVMOptions::reservedCodeCacheSize)
369372

370373
infix fun DaemonJVMOptions.memorywiseFitsInto(other: DaemonJVMOptions): Boolean =
371374
daemonJVMOptionsMemoryProps

libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/ExecutionStrategyIT.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.jetbrains.kotlin.gradle
22

3+
import org.gradle.util.GradleVersion
34
import org.jetbrains.kotlin.gradle.util.checkedReplace
45
import org.jetbrains.kotlin.gradle.util.getFileByName
56
import org.jetbrains.kotlin.gradle.util.modify
7+
import org.junit.Assert
68
import org.junit.Test
79
import java.io.File
810

@@ -64,11 +66,24 @@ abstract class ExecutionStrategyIT : BaseGradleIT() {
6466
val strategyCLIArg = "-Dkotlin.compiler.execution.strategy=$executionStrategy"
6567
val finishMessage = "Finished executing kotlin compiler using $executionStrategy strategy"
6668

69+
val isGradleAtLeast50 = project.testGradleVersionAtLeast("5.0")
70+
6771
project.build("build", strategyCLIArg) {
6872
assertSuccessful()
6973
assertContains(finishMessage)
7074
checkOutput()
7175
assertNoWarnings()
76+
77+
if (executionStrategy == "daemon" && isGradleAtLeast50) {
78+
val m = "Kotlin compile daemon JVM options: \\[(.*?)\\]".toRegex().find(output)
79+
?: error("Could not find Kotlin compile daemon JVM options in Gradle's output")
80+
val kotlinDaemonJvmArgs = m.groupValues[1].split(",").map { it.trim() }
81+
val maxMetaspaceArg = "-XX:MaxMetaspaceSize=256m"
82+
Assert.assertTrue(
83+
"Kotlin daemon JVM args do not contain '$maxMetaspaceArg': $kotlinDaemonJvmArgs",
84+
maxMetaspaceArg in kotlinDaemonJvmArgs
85+
)
86+
}
7287
}
7388

7489
val classesKt = project.projectDir.getFileByName("classes.kt")

libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/kotlinBuiltins/gradle.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ internal class GradleKotlinCompilerWork @Inject constructor(
179179
}
180180

181181
val (daemon, sessionId) = connection
182+
183+
if (log.isDebugEnabled) {
184+
daemon.getDaemonJVMOptions().takeIf { it.isGood }?.let { jvmOpts ->
185+
log.debug("Kotlin compile daemon JVM options: ${jvmOpts.get().mappers.flatMap { it.toArgs("-") }}")
186+
}
187+
}
182188
val targetPlatform = when (compilerClassName) {
183189
KotlinCompilerClass.JVM -> CompileService.TargetPlatform.JVM
184190
KotlinCompilerClass.JS -> CompileService.TargetPlatform.JS

0 commit comments

Comments
 (0)