Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,14 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
fun testDaemonOptionsParsing() {
val backupOptions = System.getProperty(CompilerSystemProperties.COMPILE_DAEMON_OPTIONS_PROPERTY.property)
try {
System.setProperty(CompilerSystemProperties.COMPILE_DAEMON_OPTIONS_PROPERTY.property, "runFilesPath=abcd,autoshutdownIdleSeconds=1111")
System.setProperty(
CompilerSystemProperties.COMPILE_DAEMON_OPTIONS_PROPERTY.property,
"runFilesPath=abcd,autoshutdownIdleSeconds=1111,autoshutdownMemoryThreshold=333"
)
val opts = configureDaemonOptions(DaemonOptions(shutdownDelayMilliseconds = 1))
assertEquals("abcd", opts.runFilesPath)
assertEquals(1111, opts.autoshutdownIdleSeconds)
assertEquals(333L, opts.autoshutdownMemoryThreshold)
}
finally {
restoreSystemProperty(CompilerSystemProperties.COMPILE_DAEMON_OPTIONS_PROPERTY.property, backupOptions)
Expand Down Expand Up @@ -375,6 +379,36 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
}
}

fun testDaemonAutoshutdownOnMemoryThreshold() {
withFlagFile(getTestName(true), ".alive") { flagFile ->
val daemonOptions = DaemonOptions(
autoshutdownMemoryThreshold = 1,
autoshutdownIdleSeconds = COMPILE_DAEMON_TIMEOUT_INFINITE_S,
autoshutdownUnusedSeconds = COMPILE_DAEMON_TIMEOUT_INFINITE_S,
shutdownDelayMilliseconds = 1,
runFilesPath = File(testTempDir, getTestName(true)).absolutePath
)

withLogFile("kotlin-daemon-test") { logFile ->
val daemonJVMOptions = makeTestDaemonJvmOptions(logFile)

val daemon = KotlinCompilerClient.connectToCompileService(compilerId, flagFile, daemonJVMOptions, daemonOptions, DaemonReportingTargets(out = System.err), autostart = true)
assertNotNull("failed to connect daemon", daemon)
daemon?.registerClient(flagFile.absolutePath)

// wait up to 6s (more than 2 * DAEMON_PERIODIC_CHECK_INTERVAL_MS)
for (attempts in 1..30) {
if (logFile.isLogContainsSequence("Memory threshold exceeded")) break
Thread.sleep(200)
}
Thread.sleep(200)

logFile.assertLogContainsSequence("Memory threshold exceeded",
"Shutdown started")
}
}
}

fun testDaemonGracefulShutdown() {
withFlagFile(getTestName(true), ".alive") { flagFile ->
val daemonOptions = DaemonOptions(autoshutdownIdleSeconds = 1, shutdownDelayMilliseconds = 1, forceShutdownTimeoutMilliseconds = 60000, runFilesPath = File(testTempDir, getTestName(true)).absolutePath)
Expand Down Expand Up @@ -987,4 +1021,3 @@ internal fun URL.toFile() =
if (protocol != "file") null
else File(file)
}

Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,11 @@ class CompileServiceImpl(

ifAliveUnit(minAliveness = Aliveness.Alive) {
when {
daemonOptions.autoshutdownMemoryThreshold != COMPILE_DAEMON_MEMORY_THRESHOLD_INFINITE &&
usedMemory(withGC = false) > daemonOptions.autoshutdownMemoryThreshold -> {
log.info("Memory threshold exceeded: threshold ${daemonOptions.autoshutdownMemoryThreshold}b")
gracefulShutdown(false)
}
daemonOptions.autoshutdownUnusedSeconds != COMPILE_DAEMON_TIMEOUT_INFINITE_S && compilationsCounter.get() == 0 && nowSeconds() - lastUsedSeconds > daemonOptions.autoshutdownUnusedSeconds -> {
log.info("Unused timeout exceeded ${daemonOptions.autoshutdownUnusedSeconds}s")
gracefulShutdown(false)
Expand Down Expand Up @@ -1329,4 +1334,4 @@ internal class RemoteLookupTracker(private val servicesFacade: CompilerServicesF
attachment = null
)
}
}
}