From 1b1493973109f05ee8af3d43e8f48aae7a6ec80a Mon Sep 17 00:00:00 2001 From: xXJsonDeruloXx Date: Thu, 22 Oct 2026 10:01:49 -0400 Subject: [PATCH] fix: suspend page-out waits for each handler's ack and can be cancelled --- .../launcher/session/SessionService.kt | 27 +++++++-- .../session/SessionSuspendController.kt | 40 +++++++++---- .../session/SessionSuspendControllerTest.kt | 58 +++++++++++++++++-- tools/linuxfs/preload/zram.c | 21 +++++++ tools/tests/test_zram_preload.py | 8 ++- 5 files changed, 130 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 839cb28a5f..ea34327a67 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -91,6 +91,8 @@ class SessionService : Service() { private var suspendOperationPending = false private var pipTask = false private var suspendAttemptFailed = false + private var freezePending = false + private var freezeCancelled = false private var pendingSteamGameId: String? = null private var pendingSteamGameGeneration = -1 // Bumped to cancel a running game-exit watch (a new launch, or the session ending). @@ -287,6 +289,7 @@ class SessionService : Service() { steamSleepToken = null completeNativeSleep() suspendOperationPending = false + freezePending = false suspendAttemptFailed = false suspendController = null SessionState.suspended = false @@ -582,12 +585,12 @@ class SessionService : Service() { if (!pulse.setSinkSuspended(false)) Log.w(TAG, "could not resume audio sink") }, zramControl = File(filesDir, "${LinuxRuntime.DIR}/tmp/.bl-zram"), - pagedOut = { processes, residentKb, swapKb, swapBeforeKb, millis -> - Log.i(TAG, "paged out $processes processes in $millis ms: resident ${residentKb / 1024} MB, " + - "zram ${swapBeforeKb / 1024} -> ${swapKb / 1024} MB") + pagedOut = { processes, unacknowledged, residentKb, swapKb, swapBeforeKb, millis -> + Log.i(TAG, "paged out $processes processes ($unacknowledged unacknowledged) in $millis ms: " + + "resident ${residentKb / 1024} MB, zram ${swapBeforeKb / 1024} -> ${swapKb / 1024} MB") mainHandler.post { SessionEvents.record("session.paged_out", mapOf( - "processes" to processes, "residentMb" to residentKb / 1024, + "processes" to processes, "unacknowledged" to unacknowledged, "residentMb" to residentKb / 1024, "swapMb" to swapKb / 1024, "swapBeforeMb" to swapBeforeKb / 1024, "ms" to millis, )) } @@ -1386,6 +1389,10 @@ class SessionService : Service() { SessionPrefs.SUSPEND_MANUAL -> manualPauseRequested && prepared && !downloadsBlockingSuspend else -> false } + if (freezePending && !shouldSuspend && !freezeCancelled) { + freezeCancelled = true + controller.cancelFreeze() + } if (suspendOperationPending || suspendAttemptFailed || shouldSuspend == SessionState.suspended) return val gen = sessionGen suspendOperationPending = true @@ -1396,12 +1403,15 @@ class SessionService : Service() { "nativeSleep" to (nativeSleepToken != null), )) if (shouldSuspend) { + freezePending = true + freezeCancelled = false controller.freeze( pageOut = steamSleepToken != null || nativeSleepToken != null || !activityVisible || !screenOn, ) { success -> mainHandler.post { if (gen != sessionGen || !SessionState.running) return@post suspendOperationPending = false + freezePending = false if (success) { SessionState.suspended = true SessionEvents.transition(SessionPhase.SUSPENDED, "session.suspended") @@ -1410,8 +1420,12 @@ class SessionService : Service() { } else { completeNativeSleep() completeSteamSleep() - suspendAttemptFailed = true - Log.w(TAG, "could not confirm that the session stopped") + if (freezeCancelled) { + SessionEvents.record("session.suspend_cancelled") + } else { + suspendAttemptFailed = true + Log.w(TAG, "could not confirm that the session stopped") + } } updateSuspendPolicy() } @@ -1582,6 +1596,7 @@ class SessionService : Service() { GameThreadHints.stop() SessionEvents.transition(SessionPhase.STOPPING, "session.stopping", mapOf("status" to status)) suspendOperationPending = false + freezePending = false launchWatcher?.stopWatching() launchWatcher = null syncWatcher?.stopWatching() 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 268719f55d..5d0e32f990 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionSuspendController.kt @@ -19,7 +19,8 @@ internal class SessionSuspendController( private val nowMillis: () -> Long = { System.nanoTime() / 1_000_000 }, private val executor: ExecutorService = Executors.newSingleThreadExecutor(), private val zramControl: File? = null, - private val pagedOut: (processes: Int, residentKb: Long, swapKb: Long, swapBeforeKb: Long, millis: Long) -> Unit = { _, _, _, _, _ -> }, + private val pagedOut: (processes: Int, unacknowledged: Int, residentKb: Long, swapKb: Long, swapBeforeKb: Long, millis: Long) -> Unit = + { _, _, _, _, _, _ -> }, ) { private enum class Role { SESSION_ROOT, SESSION_CHILD, HELPER_ROOT, HELPER_CHILD } @@ -40,9 +41,11 @@ internal class SessionSuspendController( private data class TaskIdentity(val process: ProcessRecord, val tid: Int, val started: Long) @Volatile private var closing = false + @Volatile private var cancelled = false private var frozen = emptyList() fun freeze(pageOut: Boolean = false, completion: (Boolean) -> Unit) { + cancelled = false submit({ val records = frozen.associateByTo(LinkedHashMap()) { it.pid to it.started } var complete = false @@ -65,14 +68,14 @@ internal class SessionSuspendController( val deadline = nowMillis() + FREEZE_TIMEOUT_MS var previous: Map? = null for (pass in 0 until FREEZE_MAX_PASSES) { - if (closing || nowMillis() >= deadline) break + if (closing || cancelled || nowMillis() >= deadline) break try { if (readProcess(root.pid)?.started != root.started) break addSnapshot(records, root.pid, helpers) if (readProcess(root.pid)?.started != root.started) break stop(records.values.toList()) sleep(if (pass == 0) 70L else 35L) - if (closing || nowMillis() >= deadline) break + if (closing || cancelled || nowMillis() >= deadline) break val current = stoppedSnapshot(records.values.toList(), root) if (current != null && current == previous) { complete = true @@ -100,6 +103,11 @@ internal class SessionSuspendController( }, { completion(false) }) } + // A freeze still queued or paging out stops before its next SIGSTOP pass and rolls back. + fun cancelFreeze() { + cancelled = true + } + fun resume(completion: (Boolean) -> Unit) { submit({ val records = frozen @@ -255,21 +263,29 @@ internal class SessionSuspendController( } } if (targets.isEmpty()) return + val acks = targets.associateWith { File(control.parentFile, "${control.name}.ack.${it.pid}") } + acks.values.forEach { it.delete() } val start = nowMillis() val before = memoryKb(targets) for (record in targets) send(record, SIGURG) - var previous = before.second - var steady = 0 - while (steady < PAGE_OUT_STEADY_POLLS && nowMillis() - start < PAGE_OUT_TIMEOUT_MS) { + // A program can replace libblsession's SIGURG handler (PyInstaller's bootloader, the Go runtime); + // such a process never creates its ack and is not waited for once the start window has passed. + val waiting = targets.toMutableList() + while (!closing && !cancelled && nowMillis() - start < PAGE_OUT_TIMEOUT_MS) { + val started = nowMillis() - start >= PAGE_OUT_START_MS + waiting.removeAll { val ack = acks.getValue(it); ack.length() > 0 || (started && !ack.exists()) || isGone(it) } + if (waiting.isEmpty()) break sleep(PAGE_OUT_POLL_MS) - val swap = memoryKb(targets).second - steady = if (swap == previous) steady + 1 else 0 - previous = swap } + val unacknowledged = targets.count { acks.getValue(it).length() == 0L && !isGone(it) } + acks.values.forEach { it.delete() } val after = memoryKb(targets) - pagedOut(targets.size, after.first, after.second, before.second, nowMillis() - start) + pagedOut(targets.size, unacknowledged, after.first, after.second, before.second, nowMillis() - start) } + private fun isGone(record: ProcessRecord): Boolean = + try { readProcess(record.pid)?.started != record.started } catch (_: Exception) { false } + private fun memoryKb(records: List): Pair { var resident = 0L var swap = 0L @@ -365,8 +381,8 @@ internal class SessionSuspendController( const val SIGSTOP = 19 const val SIGCONT = 18 const val SIGURG = 23 - const val PAGE_OUT_POLL_MS = 250L - const val PAGE_OUT_STEADY_POLLS = 3 + const val PAGE_OUT_POLL_MS = 100L + const val PAGE_OUT_START_MS = 2_000L const val PAGE_OUT_TIMEOUT_MS = 12_000L const val RESTORE_CHECKS = 80 const val FREEZE_TIMEOUT_MS = 8_000L 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 b8eafcc1bc..02b38143ee 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionSuspendControllerTest.kt @@ -35,7 +35,43 @@ class SessionSuspendControllerTest { assertTrue(fixture.signals.indexOfFirst { it.signal == URG } < fixture.signals.indexOfFirst { it.signal == STOP }) assertEquals("1", fixture.control.readText()) assertEquals(listOf(Triple(1, 1_000_000L, 3_000_000L)), fixture.pagedOut) + assertEquals(0, fixture.unacknowledged) assertTrue(fixture.game.threads.all { it.state == 't' }) + assertFalse(File(fixture.control.parentFile, ".bl-zram.ack.$GAME").exists()) + } + + @Test fun unchangedSwapIsNotTakenForAFinishedPageOut() { + val fixture = fixture(17) + fixture.control.writeText("0") + fixture.handlerStalls = true + var stoppedEarly = false + fixture.afterSleep = { + if (fixture.now == 3000L) fixture.handlerStalls = false + if (fixture.handlerStalls && fixture.signals.any { it.signal == STOP }) stoppedEarly = true + } + fixture.controller.freeze(pageOut = true) { assertTrue(it) } + assertFalse(stoppedEarly) + assertEquals(0, fixture.unacknowledged) + } + + @Test fun replacedHandlerIsNotWaitedForPastTheStartWindow() { + val fixture = fixture(17) + fixture.control.writeText("0") + fixture.handlerReplaced = true + fixture.controller.freeze(pageOut = true) { assertTrue(it) } + assertEquals(1, fixture.unacknowledged) + assertTrue(fixture.now in 2000L..3000L) + } + + @Test fun cancelledFreezeRollsBackWithoutStopping() { + val fixture = fixture(17) + fixture.control.writeText("0") + fixture.handlerStalls = true + fixture.afterSleep = { if (fixture.now >= 500) fixture.controller.cancelFreeze() } + fixture.controller.freeze(pageOut = true) { assertFalse(it) } + assertFalse(fixture.signals.any { it.signal == STOP }) + assertTrue(fixture.game.threads.all { it.state == 'S' }) + assertEquals(1, fixture.audioResumes) } @Test fun noPageOutWithoutTheRequestOrTheControlFile() { @@ -305,12 +341,18 @@ class SessionSuspendControllerTest { private var gameResidentKb = 4_000_000L private var gameSwapKb = 0L private var pagingOut = false + var unacknowledged = -1 + var handlerStalls = false + var handlerReplaced = false private var continuing = false val controller = SessionSuspendController( { rootPid }, { helpers }, { audioSuspends++ }, { audioResumes++ }, proc, ::signal, ::threadSignal, ::sleep, { now }, executor, zramControl = control, - pagedOut = { processes, resident, swap, _, _ -> pagedOut += Triple(processes, resident, swap) }, + pagedOut = { processes, unacknowledged, resident, swap, _, _ -> + pagedOut += Triple(processes, resident, swap) + this.unacknowledged = unacknowledged + }, ) init { sync() } @@ -344,9 +386,17 @@ class SessionSuspendControllerTest { private fun sleep(millis: Long) { now += millis - if (pagingOut && gameResidentKb > 1_000_000L) { - gameResidentKb -= 1_000_000L - gameSwapKb += 1_000_000L + if (pagingOut && !handlerReplaced) { + File(control.parentFile, "${control.name}.ack.$GAME").apply { if (!exists()) writeText("") } + } + if (pagingOut && !handlerStalls && !handlerReplaced) { + if (gameResidentKb > 1_000_000L) { + gameResidentKb -= 1_000_000L + gameSwapKb += 1_000_000L + } else { + File(control.parentFile, "${control.name}.ack.$GAME").writeText("1") + pagingOut = false + } } if (root.threads.first().state != 'T' && !continuing) game.threads.forEach { if (it.state != 'Z') { it.state = 'S'; it.cpu++ } diff --git a/tools/linuxfs/preload/zram.c b/tools/linuxfs/preload/zram.c index f5e09cf48b..8f31d19eb1 100644 --- a/tools/linuxfs/preload/zram.c +++ b/tools/linuxfs/preload/zram.c @@ -126,6 +126,25 @@ static void zr_walk(int compress) { zr_sys(SYS_close, maps); } +// Before it stops a session for suspend, the app waits for .ack. to appear (the handler +// runs here, not one a program installed over it) and then to hold a byte (the page-out finished). +static void zr_ack(int done) { + static const char suffix[] = ".ack."; + char path[sizeof(ZR_CTL) + sizeof(suffix) + 20]; + char digits[20]; + size_t n = 0, d = 0; + for (const char *p = ZR_CTL; *p; p++) path[n++] = *p; + for (const char *p = suffix; *p; p++) path[n++] = *p; + long pid = zr_sys(SYS_getpid); + do digits[d++] = (char)('0' + pid % 10); while ((pid /= 10) > 0); + while (d > 0) path[n++] = digits[--d]; + path[n] = 0; + int fd = (int)zr_sys(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); + if (fd < 0) return; + if (done) zr_sys(SYS_write, fd, "1", 1UL); + zr_sys(SYS_close, fd); +} + static void zr_handler(int sig, siginfo_t *info, void *context) { (void)sig; (void)context; @@ -136,7 +155,9 @@ static void zr_handler(int sig, siginfo_t *info, void *context) { && !__atomic_exchange_n(&zr_busy, 1, __ATOMIC_SEQ_CST)) { __atomic_store_n(&zr_pending, 0, __ATOMIC_SEQ_CST); int mode = zr_mode(); + if (mode == 1) zr_ack(0); if (mode >= 0) zr_walk(mode); + if (mode == 1) zr_ack(1); __atomic_store_n(&zr_busy, 0, __ATOMIC_SEQ_CST); } errno = saved; diff --git a/tools/tests/test_zram_preload.py b/tools/tests/test_zram_preload.py index a9a437e3ec..88d5c73020 100644 --- a/tools/tests/test_zram_preload.py +++ b/tools/tests/test_zram_preload.py @@ -129,6 +129,8 @@ class ZramPreloadTest(unittest.TestCase): def setUp(self): self.ctl.write_text("0") + for ack in self.dir.glob("ctl.ack.*"): + ack.unlink() def run_program(self, program=None, mb=48, cycles=1, *extra): environment = dict(os.environ, LD_PRELOAD=str(self.lib)) @@ -140,9 +142,10 @@ class ZramPreloadTest(unittest.TestCase): out.setdefault(key, []).append(int(value)) return out - def test_only_the_client_takes_the_signal(self): + def test_every_process_takes_the_signal(self): self.assertEqual([1], self.run_program()["handler"]) - self.assertEqual([0], self.run_program(self.game)["handler"]) + self.assertEqual([1], self.run_program(self.game)["handler"]) + self.assertEqual(2, len(list(self.dir.glob("ctl.ack.*")))) def test_nothing_is_installed_without_the_control_file(self): self.ctl.unlink() @@ -157,6 +160,7 @@ class ZramPreloadTest(unittest.TestCase): self.assertGreater(out["out"][0], 32 * 1024) self.assertLess(out["back"][0], out["out"][0] // 4) self.assertEqual([1], out["intact"]) + self.assertEqual(["1"], [ack.read_text() for ack in self.dir.glob("ctl.ack.*")]) @unittest.skipUnless(swap_total_kb() > 256 * 1024, "needs swap") def test_reads_back_what_the_kernel_swapped_elsewhere(self):