From 7ad52166a2192c508de63659b0f8b13592ae17e7 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sat, 5 Dec 2026 03:02:57 -0500 Subject: [PATCH] Keep unanswered spills and unfinished restores tracked until a resume can release them --- .../session/SessionSuspendController.kt | 34 +++++++++++------- .../session/SessionSuspendControllerTest.kt | 35 +++++++++++++++++-- tools/tests/test_zram_spill_preload.py | 20 +++++++---- 3 files changed, 68 insertions(+), 21 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 bf89c3e320..15499706aa 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt @@ -56,6 +56,8 @@ internal class SessionSuspendController( private var frozenSince: Long? = null private var pagedOutTargets = emptyList() private var spilledTargets = emptyList() + // Asked to spill and not yet released by a resume count they saw: acknowledged or not, each may park its process. + private var spillAsked = emptyList() fun freeze(pageOut: Boolean = false, spill: Boolean = false, completion: (Boolean) -> Unit) { cancelled = false @@ -131,7 +133,7 @@ internal class SessionSuspendController( if (resumed) frozen = emptyList() // a spilled process runs again only once its helper has read its memory back completion(resumed && awaitRestored()) - if (resumed) readBack() + if (resumed) readBack(waitForRestore = false) }, { completion(false) }) } @@ -321,6 +323,7 @@ internal class SessionSuspendController( if (targets.isEmpty()) return targets.forEach { ackFile(control, "ack", it).delete(); ackFile(control, "restored", it).delete() } val start = nowMillis() + spillAsked = (spillAsked + targets).distinct() for (record in targets) send(record, SIGURG) val waiting = targets.toMutableList() while (!closing && !cancelled && nowMillis() - start < SPILL_TIMEOUT_MS) { @@ -349,24 +352,30 @@ internal class SessionSuspendController( 0L } - // False when a spilled process's helper may not have seen the new count: it would stay stopped. + // False when a process asked to spill may not have seen the new count: it would stay stopped. The count is + // put in place whole, so a failed write leaves the one before. private fun bumpResume(): Boolean { - val control = zramControl?.takeIf { it.isFile } ?: return spilledTargets.isEmpty() + val control = zramControl?.takeIf { it.isFile } ?: return spillAsked.isEmpty() val counter = resumeFile(control) - return try { + val ok = try { val next = resumeCount(control) + 1 - counter.writeText(next.toString()) + val fresh = File(counter.parentFile, "${counter.name}.new") + fresh.writeText(next.toString()) + java.nio.file.Files.move(fresh.toPath(), counter.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING, + java.nio.file.StandardCopyOption.ATOMIC_MOVE) resumeCount(control) == next } catch (_: IOException) { - spilledTargets.isEmpty() + false } + if (ok) spillAsked = emptyList() + return ok || spillAsked.isEmpty() } - // True when every spilled process has its memory back (or is gone). + // True when every spilled process has its memory back (or is gone). Ones still reading stay tracked, so a + // later resume waits for them again. private fun awaitRestored(): Boolean { - val targets = spilledTargets - spilledTargets = emptyList() val control = zramControl ?: return true + val targets = spilledTargets if (targets.isEmpty()) return true val start = nowMillis() val waiting = targets.toMutableList() @@ -379,7 +388,8 @@ internal class SessionSuspendController( } catch (_: InterruptedException) { Thread.currentThread().interrupt() } - targets.forEach { ackFile(control, "restored", it).delete() } + targets.filterNot { it in waiting }.forEach { ackFile(control, "restored", it).delete() } + spilledTargets = waiting.toList() restored(targets.size, waiting.size, memoryKb(targets).first, nowMillis() - start) return waiting.isEmpty() } @@ -413,8 +423,8 @@ internal class SessionSuspendController( false } - private fun readBack() { - awaitRestored() + private fun readBack(waitForRestore: Boolean = true) { + if (waitForRestore) awaitRestored() val targets = pagedOutTargets.filterNot { isGone(it) } pagedOutTargets = emptyList() val control = zramControl?.takeIf { it.isFile } ?: return 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 2e785a3b3b..8b2f262a36 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt @@ -162,6 +162,35 @@ class SessionSuspendControllerTest { assertEquals(2, fixture.restores.size) } + @Test fun aRestoreThatOutlastsTheWaitIsWaitedForAgainByTheNextResume() { + 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) } + assertEquals(listOf(1 to 1), fixture.restores) + assertTrue(fixture.game.threads.all { it.state == 't' }) + fixture.restoreDelayMs = 0L + var called = false + fixture.controller.resume { + assertTrue(it) + assertTrue(fixture.game.threads.none { thread -> thread.state == 't' }) + called = true + } + assertTrue(called) + assertEquals(listOf(1 to 1, 1 to 0), fixture.restores) + } + + @Test fun aResumeCountThatCannotBeWrittenFailsWhileASpillHasNotAnswered() { + val fixture = fixture(17) + fixture.control.writeText("0") + fixture.spillSilent = true + fixture.controller.freeze(pageOut = true, spill = true) { } + File(fixture.control.parentFile, ".dd-zram.resume.new").mkdirs() + fixture.controller.resume { assertFalse(it) } + assertFalse(File(fixture.control.parentFile, ".dd-zram.resume").isFile) + } + @Test fun aGuestThatCouldNotSpillIsReadBackFromZram() { val fixture = fixture(17) fixture.control.writeText("0") @@ -583,6 +612,8 @@ class SessionSuspendControllerTest { var handlerStalls = false var handlerReplaced = false var spillFallsBack = false + var spillSilent = false + var restoreDelayMs = RESTORE_DELAY_MS val spills = mutableListOf>() val restores = mutableListOf>() val resumeAtContinue = mutableListOf() @@ -651,7 +682,7 @@ class SessionSuspendControllerTest { private fun sleep(millis: Long) { now += millis - if (spilling) { + if (spilling && !spillSilent) { spilling = false val ack = File(control.parentFile, "${control.name}.ack.$GAME") if (spillFallsBack) { @@ -665,7 +696,7 @@ class SessionSuspendControllerTest { } } val count = resume.takeIf { it.isFile }?.readText()?.trim() ?: "0" - if (restoring && continuing && count != spilledBase && now - continuedAt >= RESTORE_DELAY_MS) { + if (restoring && continuing && count != spilledBase && now - continuedAt >= restoreDelayMs) { restoring = false gameResidentKb = 4_000_000L game.threads.forEach { if (it.state != 'Z') it.state = 'S' } diff --git a/tools/tests/test_zram_spill_preload.py b/tools/tests/test_zram_spill_preload.py index 0342ec59ba..a9e8b081bf 100644 --- a/tools/tests/test_zram_spill_preload.py +++ b/tools/tests/test_zram_spill_preload.py @@ -31,6 +31,7 @@ struct shared { volatile long progress; volatile long errors; volatile int stop; + volatile int quiet; // readers leave the blocks alone (a page-out they would undo is being measured) }; static const char *ctl; @@ -58,6 +59,7 @@ static void *work(void *arg) { } unsigned seed = (unsigned)id * 77u + 1u; while (!shared->stop) { + if (shared->quiet) continue; if (!same(blocks[id], size, seed)) __atomic_add_fetch(&shared->errors, 1, __ATOMIC_RELAXED); __atomic_add_fetch(&shared->progress, 1, __ATOMIC_RELAXED); } @@ -130,21 +132,23 @@ int main(int argc, char **argv) { ctl = argv[1]; size = (size_t)atol(argv[2]) << 20; block_urg = argc > 3 && !strcmp(argv[3], "block"); - if (argc > 3 && !strcmp(argv[3], "undumpable")) prctl(PR_SET_DUMPABLE, 0); + int undumpable = argc > 3 && !strcmp(argv[3], "undumpable"); + if (undumpable) prctl(PR_SET_DUMPABLE, 0); shared = mmap(NULL, sizeof(*shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (shared == MAP_FAILED) return 2; char resume[512]; snprintf(resume, sizeof(resume), "%s.resume", ctl); unlink(resume); put(".hold", ""); - for (int i = 0; i < THREADS; i++) { - blocks[i] = i % 2 ? malloc(size) : mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (!blocks[i] || blocks[i] == MAP_FAILED) return 3; - fill(blocks[i], size, (unsigned)i * 77u + 1u); - } - if (mprotect(blocks[2], size, PROT_READ) != 0) return 4; pid_t child = fork(); if (child == 0) { + // the blocks are the child's alone: a page-out skips pages another process still maps + for (int i = 0; i < THREADS; i++) { + blocks[i] = i % 2 ? malloc(size) : mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (!blocks[i] || blocks[i] == MAP_FAILED) _exit(3); + fill(blocks[i], size, (unsigned)i * 77u + 1u); + } + if (mprotect(blocks[2], size, PROT_READ) != 0) _exit(4); pthread_t t[THREADS]; for (long i = 0; i < THREADS; i++) pthread_create(&t[i], NULL, work, (void *)i); for (int i = 0; i < THREADS; i++) pthread_join(t[i], NULL); @@ -152,10 +156,12 @@ int main(int argc, char **argv) { } while (shared->progress < 50) nap(10); long before = rss_kb(child); + if (undumpable) shared->quiet = 1; put(".hold", "4 0"); kill(child, SIGURG); long spilled = wait_for(".ack.", child, 20000); long after = rss_kb(child); + shared->quiet = 0; long frozen_at = shared->progress; nap(500); long frozen_now = shared->progress;