Wait on resume for a spill that had not answered when the spill wait ended

This commit is contained in:
Kurt Himebauch committed 2026-12-05 03:52:32 -05:00
1 parent 644f8e655f
commit 498a3a653a
2 files changed
+32 -8

No files matched your search

@@ -321,7 +321,7 @@ internal class SessionSuspendController(
addSnapshot(records, root, helpers)
// A restore that finished after the last resume stopped waiting is counted before its marker goes; one
// still reading is not asked to spill again.
spilledTargets = spilledTargets.filterNot { readAck(ackFile(control, "restored", it)) != null || isGone(it) }
spilledTargets = spilledTargets.filterNot { settled(control, it) }
val targets = preloaded(records.values).filterNot { it in spilledTargets }
if (targets.isEmpty()) return
targets.forEach { ackFile(control, "ack", it).delete(); ackFile(control, "restored", it).delete() }
@@ -338,18 +338,25 @@ internal class SessionSuspendController(
var total = 0L
val acks = targets.associateWith { readAck(ackFile(control, "ack", it)) }
val written = targets.filter { record ->
val kb = acks[record] ?: 0L
val kb = acks[record]?.coerceAtLeast(0L) ?: 0L
total += kb
kb > 0L
}
// a process that answered 0 paged out to zram instead and has no spill to release
// A process that answered 0 paged out to zram instead and has no spill to release. One that has not
// answered may still be writing: it is waited for like a spilled one, its acknowledgement kept.
val unanswered = targets.filter { (acks[it] ?: -1L) < 0L && !isGone(it) }
spillAsked = spillAsked.filterNot { acks[it] == 0L }
spilledTargets = (spilledTargets + written).distinct()
pagedOutTargets = (pagedOutTargets + targets.filterNot { it in written }).distinct()
targets.forEach { ackFile(control, "ack", it).delete() }
spilledTargets = (spilledTargets + written + unanswered).distinct()
pagedOutTargets = (pagedOutTargets + targets.filter { acks[it] == 0L }).distinct()
targets.filterNot { it in unanswered }.forEach { ackFile(control, "ack", it).delete() }
spilled(written.size, total, memoryKb(targets).first, nowMillis() - start)
}
// Its memory is back, it paged out to zram after all (an answer of 0 that came late), or it is gone.
private fun settled(control: File, record: ProcessRecord) =
readAck(ackFile(control, "restored", record)) != null || readAck(ackFile(control, "ack", record)) == 0L ||
isGone(record)
private fun resumeFile(control: File) = File(control.parentFile, "${control.name}.resume")
private fun resumeCount(control: File): Long = try {
@@ -387,14 +394,17 @@ internal class SessionSuspendController(
val waiting = targets.toMutableList()
try {
while (!closing && nowMillis() - start < RESTORE_TIMEOUT_MS) {
waiting.removeAll { readAck(ackFile(control, "restored", it)) != null || isGone(it) }
waiting.removeAll { settled(control, it) }
if (waiting.isEmpty()) break
sleep(PAGE_OUT_POLL_MS)
}
} catch (_: InterruptedException) {
Thread.currentThread().interrupt()
}
targets.filterNot { it in waiting }.forEach { ackFile(control, "restored", it).delete() }
targets.filterNot { it in waiting }.forEach {
ackFile(control, "restored", it).delete()
ackFile(control, "ack", it).delete()
}
spilledTargets = waiting.toList()
restored(targets.size, waiting.size, memoryKb(targets).first, nowMillis() - start)
return waiting.isEmpty()
@@ -191,6 +191,20 @@ class SessionSuspendControllerTest {
assertFalse(File(fixture.control.parentFile, ".dd-zram.resume").isFile)
}
@Test fun aSpillThatAnswersAfterTheWaitIsStillRestoredOnResume() {
val fixture = fixture(17)
fixture.control.writeText("0")
fixture.spillSilent = true
fixture.controller.freeze(pageOut = true, spill = true) { }
fixture.spillSilent = false
fixture.advance(1_000L)
assertTrue(File(fixture.control.parentFile, ".dd-zram.ack.$GAME").isFile)
fixture.controller.resume { assertTrue(it) }
assertEquals(listOf(1 to 0), fixture.restores)
assertTrue(fixture.pagedIn.isEmpty())
assertTrue(fixture.game.threads.none { it.state == 't' || it.state == 'T' })
}
@Test fun aGuestThatFellBackToZramResumesWhenTheCountCannotBeWritten() {
val fixture = fixture(17)
fixture.control.writeText("0")