From 644f8e655f98cf9ace72651bdeef1341021de212 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sat, 5 Dec 2026 03:33:08 -0500 Subject: [PATCH] Release zram fallbacks on resume and count late restores before another spill --- .../session/SessionSuspendController.kt | 10 +++++-- .../session/SessionSuspendControllerTest.kt | 26 +++++++++++++++++++ tools/tests/test_zram_spill_preload.py | 21 ++++++++++++--- 3 files changed, 52 insertions(+), 5 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 15499706aa..c04416b703 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt @@ -319,7 +319,10 @@ internal class SessionSuspendController( // the resume counter changes; its acknowledgement is the kB it wrote, 0 when it paged out to zram instead. private fun spill(control: File, records: MutableMap, ProcessRecord>, root: Int, helpers: List) { addSnapshot(records, root, helpers) - val targets = preloaded(records.values) + // 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) } + 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() } val start = nowMillis() @@ -333,11 +336,14 @@ internal class SessionSuspendController( sleep(PAGE_OUT_POLL_MS) } var total = 0L + val acks = targets.associateWith { readAck(ackFile(control, "ack", it)) } val written = targets.filter { record -> - val kb = readAck(ackFile(control, "ack", record)) ?: 0L + val kb = acks[record] ?: 0L total += kb kb > 0L } + // a process that answered 0 paged out to zram instead and has no spill to release + 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() } 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 8b2f262a36..876205ead0 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,30 @@ class SessionSuspendControllerTest { assertFalse(File(fixture.control.parentFile, ".dd-zram.resume").isFile) } + @Test fun aGuestThatFellBackToZramResumesWhenTheCountCannotBeWritten() { + val fixture = fixture(17) + fixture.control.writeText("0") + fixture.spillFallsBack = true + fixture.controller.freeze(pageOut = true, spill = true) { assertTrue(it) } + File(fixture.control.parentFile, ".dd-zram.resume.new").mkdirs() + fixture.controller.resume { assertTrue(it) } + assertTrue(fixture.game.threads.none { it.state == 't' || it.state == 'T' }) + } + + @Test fun aRestoreThatFinishedLateDoesNotHoldBackTheNextResume() { + val fixture = fixture(17) + fixture.control.writeText("0") + fixture.controller.freeze(pageOut = true, spill = true) { assertTrue(it) } + fixture.restoreDelayMs = 60_000L + fixture.controller.resume { assertFalse(it) } + fixture.restoreDelayMs = 0L + fixture.advance(1_000L) + assertTrue(File(fixture.control.parentFile, ".dd-zram.restored.$GAME").isFile) + fixture.spillFallsBack = true + fixture.controller.freeze(pageOut = true, spill = true) { assertTrue(it) } + fixture.controller.resume { assertTrue(it) } + } + @Test fun aGuestThatCouldNotSpillIsReadBackFromZram() { val fixture = fixture(17) fixture.control.writeText("0") @@ -680,6 +704,8 @@ class SessionSuspendControllerTest { sync() } + fun advance(millis: Long) = sleep(millis) + private fun sleep(millis: Long) { now += millis if (spilling && !spillSilent) { diff --git a/tools/tests/test_zram_spill_preload.py b/tools/tests/test_zram_spill_preload.py index a9e8b081bf..cdab52ced1 100644 --- a/tools/tests/test_zram_spill_preload.py +++ b/tools/tests/test_zram_spill_preload.py @@ -32,6 +32,7 @@ struct shared { volatile long errors; volatile int stop; volatile int quiet; // readers leave the blocks alone (a page-out they would undo is being measured) + volatile int quieted; // readers that have stopped reading }; static const char *ctl; @@ -58,8 +59,19 @@ static void *work(void *arg) { pthread_sigmask(SIG_BLOCK, &set, NULL); } unsigned seed = (unsigned)id * 77u + 1u; + int idle = 0; while (!shared->stop) { - if (shared->quiet) continue; + if (shared->quiet) { + if (!idle) { + idle = 1; + __atomic_add_fetch(&shared->quieted, 1, __ATOMIC_RELEASE); + } + continue; + } + if (idle) { + idle = 0; + __atomic_sub_fetch(&shared->quieted, 1, __ATOMIC_RELEASE); + } if (!same(blocks[id], size, seed)) __atomic_add_fetch(&shared->errors, 1, __ATOMIC_RELAXED); __atomic_add_fetch(&shared->progress, 1, __ATOMIC_RELAXED); } @@ -156,7 +168,10 @@ int main(int argc, char **argv) { } while (shared->progress < 50) nap(10); long before = rss_kb(child); - if (undumpable) shared->quiet = 1; + if (undumpable) { + shared->quiet = 1; + for (int waited = 0; __atomic_load_n(&shared->quieted, __ATOMIC_ACQUIRE) < THREADS && waited < 5000; waited += 1) nap(1); + } put(".hold", "4 0"); kill(child, SIGURG); long spilled = wait_for(".ack.", child, 20000); @@ -167,7 +182,7 @@ int main(int argc, char **argv) { long frozen_now = shared->progress; put(".resume", "1"); long restored = spilled > 0 ? wait_for(".restored.", child, 20000) : 0; - nap(300); + for (int waited = 0; shared->progress == frozen_now && waited < 10000; waited += 10) nap(10); long resumed = shared->progress; long back = rss_kb(child); shared->stop = 1;