From 498a3a653ab966d8a3833da5d70338cf71dd18a7 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sat, 5 Dec 2026 03:52:32 -0500 Subject: [PATCH] Wait on resume for a spill that had not answered when the spill wait ended --- .../session/SessionSuspendController.kt | 26 +++++++++++++------ .../session/SessionSuspendControllerTest.kt | 14 ++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt index c04416b703..3515debd72 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt @@ -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() diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt index 876205ead0..1f4980e816 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt @@ -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")