fix: read the paged-out session back from zram after a resume (#1839)

This commit is contained in:
l4nr3 authored and GitHub committed 2026-11-01 00:24:04 -04:00
1 parent 901c453c16
commit 3fc80a03d9
3 files changed
+207 -1

No files matched your search

@@ -729,6 +729,20 @@ class SessionService : Service() {
))
}
},
readBackAllowed = { SessionPrefs.ramCapMb(this) == 0 },
pagedIn = { processes, swapKb, swapBeforeKb, availableKb, millis ->
if (processes == 0) {
Log.i(TAG, "left ${swapBeforeKb / 1024} MB in zram after resume (RAM cap, or ${availableKb / 1024} MB available is too little)")
} else {
Log.i(TAG, "read back $processes processes in $millis ms: zram ${swapBeforeKb / 1024} -> ${swapKb / 1024} MB")
}
mainHandler.post {
SessionEvents.record("session.paged_in", mapOf(
"processes" to processes, "swapMb" to swapKb / 1024, "swapBeforeMb" to swapBeforeKb / 1024,
"availableMb" to availableKb / 1024, "ms" to millis,
))
}
},
)
updateDownloadMonitoring()
mainHandler.post { updateSuspendPolicy() }
@@ -26,6 +26,11 @@ internal class SessionSuspendController(
private val nowNanos: () -> Long = System::nanoTime,
private val pagedOut: (processes: Int, unacknowledged: Int, residentKb: Long, swapKb: Long, swapBeforeKb: Long, millis: Long) -> Unit =
{ _, _, _, _, _, _ -> },
// False with a RAM cap set: the user asked for the session to use less memory, so nothing is read back.
private val readBackAllowed: () -> Boolean = { true },
// processes is 0 when the read-back was skipped: a RAM cap, or MemAvailable could not take it.
private val pagedIn: (processes: Int, swapKb: Long, swapBeforeKb: Long, availableKb: Long, millis: Long) -> Unit =
{ _, _, _, _, _ -> },
) {
private enum class Role { SESSION_ROOT, SESSION_CHILD, HELPER_ROOT, HELPER_CHILD }
@@ -49,6 +54,7 @@ internal class SessionSuspendController(
@Volatile private var cancelled = false
private var frozen = emptyList<ProcessRecord>()
private var frozenSince: Long? = null
private var pagedOutTargets = emptyList<ProcessRecord>()
fun freeze(pageOut: Boolean = false, completion: (Boolean) -> Unit) {
cancelled = false
@@ -107,6 +113,8 @@ internal class SessionSuspendController(
if (interrupted) Thread.currentThread().interrupt()
}
completion(complete)
// A cancelled or failed freeze leaves the game running with whatever the page-out already moved.
if (!complete && frozen.isEmpty() && !interrupted) readBack()
}, { completion(false) })
}
@@ -123,6 +131,7 @@ internal class SessionSuspendController(
val resumed = restoreSafely(records)
if (resumed) frozen = emptyList()
completion(resumed)
if (resumed) readBack()
}, { completion(false) })
}
@@ -289,6 +298,7 @@ internal class SessionSuspendController(
}
}
if (targets.isEmpty()) return
pagedOutTargets = (pagedOutTargets + targets).distinct()
val acks = targets.associateWith { File(control.parentFile, "${control.name}.ack.${it.pid}") }
acks.values.forEach { it.delete() }
val start = nowMillis()
@@ -309,6 +319,69 @@ internal class SessionSuspendController(
pagedOut(targets.size, unacknowledged, after.first, after.second, before.second, nowMillis() - start)
}
// Left in zram, the paged-out memory comes back one major fault at a time while the game runs; in a heavy
// game that is hundreds of faults per frame and stalls of a quarter second and more, long after the resume.
// libblsession's handler reads back only the swapped pages (mode 0, as the session script does for the client).
// The handler runs on whichever thread takes the signal, so that thread pauses for the read-back,
// once and right after the resume instead of mid-game.
// It signals even with nothing in zram yet: after a cancelled freeze a page-out handler may not have started,
// and it reads the mode only when it starts, so it reads back instead of paging out.
private fun readBack() {
val targets = pagedOutTargets.filterNot { isGone(it) }
pagedOutTargets = emptyList()
val control = zramControl?.takeIf { it.isFile } ?: return
if (targets.isEmpty() || closing) return
val before = memoryKb(targets).second
val (total, available) = meminfoKb()
if (!readBackAllowed() || !canTake(before, total, available)) {
pagedIn(0, before, before, available, 0)
return
}
try {
control.writeText("0")
} catch (_: IOException) {
return
}
val start = nowMillis()
for (record in targets) send(record, SIGURG)
var swap = before
var unchanged = 0
try {
while (!closing && nowMillis() - start < READ_BACK_TIMEOUT_MS) {
sleep(READ_BACK_POLL_MS)
val current = memoryKb(targets).second
unchanged = if (current == swap) unchanged + 1 else 0
swap = current
if (swap == 0L || unchanged >= READ_BACK_STABLE_POLLS) break
}
} catch (_: InterruptedException) {
Thread.currentThread().interrupt()
}
pagedIn(targets.size, swap, before, available, nowMillis() - start)
}
// Reading back moves the swap into resident memory, out of MemAvailable. On a device short on memory that
// could push Android's low-memory killer onto DroidDeck itself, so the memory comes back only when
// MemAvailable is at least twice the swap and keeps 1 GB or a tenth of MemTotal, whichever is more, after it.
// Otherwise it stays in zram and faults back in as the game touches it, as before.
private fun canTake(swapKb: Long, totalKb: Long, availableKb: Long): Boolean =
availableKb >= swapKb * READ_BACK_AVAILABLE_FACTOR &&
availableKb - swapKb >= maxOf(READ_BACK_HEADROOM_KB, totalKb / READ_BACK_TOTAL_SHARE)
// MemTotal and MemAvailable, 0 when unreadable (which skips the read-back).
private fun meminfoKb(): Pair<Long, Long> = try {
val values = HashMap<String, Long>()
File(procRoot, "meminfo").forEachLine { line ->
val name = line.substringBefore(':')
if (name == "MemTotal" || name == "MemAvailable") {
line.substringAfter(':').trim().substringBefore(' ').toLongOrNull()?.let { values[name] = it }
}
}
(values["MemTotal"] ?: 0L) to (values["MemAvailable"] ?: 0L)
} catch (_: IOException) {
0L to 0L
}
private fun isGone(record: ProcessRecord): Boolean =
try { readProcess(record.pid)?.started != record.started } catch (_: Exception) { false }
@@ -410,6 +483,12 @@ internal class SessionSuspendController(
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 READ_BACK_POLL_MS = 100L
const val READ_BACK_STABLE_POLLS = 5
const val READ_BACK_TIMEOUT_MS = 5_000L
const val READ_BACK_HEADROOM_KB = 1_048_576L
const val READ_BACK_AVAILABLE_FACTOR = 2L
const val READ_BACK_TOTAL_SHARE = 10L
const val RESTORE_CHECKS = 80
const val FREEZE_TIMEOUT_MS = 8_000L
const val FREEZE_MAX_PASSES = 256