From 48f587bb414b5554424f5f47fc24494c8bd99469 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sat, 5 Dec 2026 04:15:28 -0500 Subject: [PATCH] Wait only on spills whose handler started, and read back a late zram fallback --- .../session/SessionSuspendController.kt | 29 +++++++++++++------ .../session/SessionSuspendControllerTest.kt | 27 ++++++++++++++++- 2 files changed, 46 insertions(+), 10 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 3515debd72..d4b3942020 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,9 @@ 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 { settled(control, it) } + val (done, still) = spilledTargets.partition { settled(control, it) } + release(control, done) + spilledTargets = still 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() } @@ -342,10 +344,11 @@ internal class SessionSuspendController( total += kb kb > 0L } - // 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 } + // A process that answered 0 paged out to zram instead and has no spill to release. One whose handler + // started (its acknowledgement is there, still empty) may still be writing: it is waited for like a + // spilled one, its acknowledgement kept. One with no acknowledgement at all has no handler and was never held. + val unanswered = targets.filter { (acks[it] ?: -1L) < 0L && ackFile(control, "ack", it).exists() && !isGone(it) } + spillAsked = spillAsked.filterNot { it in targets && (acks[it] == 0L || !ackFile(control, "ack", it).exists()) } spilledTargets = (spilledTargets + written + unanswered).distinct() pagedOutTargets = (pagedOutTargets + targets.filter { acks[it] == 0L }).distinct() targets.filterNot { it in unanswered }.forEach { ackFile(control, "ack", it).delete() } @@ -357,6 +360,17 @@ internal class SessionSuspendController( readAck(ackFile(control, "restored", record)) != null || readAck(ackFile(control, "ack", record)) == 0L || isGone(record) + // Settled targets stop being tracked; one that fell back to zram late is read back like any paged-out one. + private fun release(control: File, settledTargets: List) { + val lateZram = settledTargets.filter { readAck(ackFile(control, "ack", it)) == 0L && !isGone(it) } + pagedOutTargets = (pagedOutTargets + lateZram).distinct() + spillAsked = spillAsked.filterNot { it in settledTargets } + settledTargets.forEach { + ackFile(control, "restored", it).delete() + ackFile(control, "ack", it).delete() + } + } + private fun resumeFile(control: File) = File(control.parentFile, "${control.name}.resume") private fun resumeCount(control: File): Long = try { @@ -401,10 +415,7 @@ internal class SessionSuspendController( } catch (_: InterruptedException) { Thread.currentThread().interrupt() } - targets.filterNot { it in waiting }.forEach { - ackFile(control, "restored", it).delete() - ackFile(control, "ack", it).delete() - } + release(control, targets.filterNot { it in waiting }) 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 1f4980e816..33b125bcbe 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt @@ -205,6 +205,28 @@ class SessionSuspendControllerTest { assertTrue(fixture.game.threads.none { it.state == 't' || it.state == 'T' }) } + @Test fun aGuestWithoutTheSpillHandlerIsNotWaitedForOnResume() { + val fixture = fixture(17) + fixture.control.writeText("0") + fixture.handlerReplaced = true + fixture.controller.freeze(pageOut = true, spill = true) { } + File(fixture.control.parentFile, ".dd-zram.resume.new").mkdirs() + fixture.controller.resume { assertTrue(it) } + assertTrue(fixture.restores.isEmpty()) + } + + @Test fun aLateFallbackToZramIsReadBack() { + val fixture = fixture(17) + fixture.control.writeText("0") + fixture.spillSilent = true + fixture.controller.freeze(pageOut = true, spill = true) { } + fixture.spillSilent = false + fixture.spillFallsBack = true + fixture.advance(1_000L) + fixture.controller.resume { assertTrue(it) } + assertEquals(1, fixture.pagedIn.size) + } + @Test fun aGuestThatFellBackToZramResumesWhenTheCountCannotBeWritten() { val fixture = fixture(17) fixture.control.writeText("0") @@ -692,7 +714,10 @@ class SessionSuspendControllerTest { }?.state = 't' } else if (signal == URG) { if (request?.startsWith("4") == true) { - spilling = true + if (!handlerReplaced) { + spilling = true + File(control.parentFile, "${control.name}.ack.$GAME").writeText("") + } spilledBase = request.substringAfter(' ', "0").trim() } else if (request == "0") { pagingOut = false; pagingIn = true } else pagingOut = true