mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
fix: read the paged-out session back from zram after a resume (#1839)
This commit is contained in:
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
|
||||
|
||||
@@ -88,6 +88,105 @@ class SessionSuspendControllerTest {
|
||||
assertTrue(disabled.pagedOut.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun resumeReadsThePagedOutMemoryBack() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
val resumed = fixture.signals.drop(before)
|
||||
assertEquals(listOf(GAME), resumed.filter { it.signal == URG }.map { it.pid })
|
||||
assertTrue(resumed.indexOfLast { it.signal == CONT } < resumed.indexOfFirst { it.signal == URG })
|
||||
assertEquals("0", fixture.control.readText())
|
||||
assertEquals(listOf(Triple(1, 0L, 3_000_000L)), fixture.pagedIn)
|
||||
assertTrue(fixture.game.threads.all { it.state == 'S' })
|
||||
val again = fixture.signals.size
|
||||
fixture.controller.freeze { assertTrue(it) }
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertFalse(fixture.signals.drop(again).any { it.signal == URG })
|
||||
assertEquals(1, fixture.pagedIn.size)
|
||||
}
|
||||
|
||||
@Test fun noReadBackAfterAFreezeWithoutPageOut() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.controller.freeze { assertTrue(it) }
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertFalse(fixture.signals.any { it.signal == URG })
|
||||
assertTrue(fixture.pagedIn.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun readBackIsSkippedWhenAvailableMemoryCannotTakeIt() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.availableKb = 3_500_000L
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertFalse(fixture.signals.drop(before).any { it.signal == URG })
|
||||
assertEquals("1", fixture.control.readText())
|
||||
assertEquals(listOf(Triple(0, 3_000_000L, 3_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun readBackNeedsTwiceTheSwapAvailable() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.availableKb = 5_500_000L
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertFalse(fixture.signals.drop(before).any { it.signal == URG })
|
||||
assertEquals(listOf(Triple(0, 3_000_000L, 3_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun readBackKeepsATenthOfMemTotalAvailable() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.totalKb = 40_000_000L
|
||||
fixture.availableKb = 6_500_000L
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertFalse(fixture.signals.drop(before).any { it.signal == URG })
|
||||
assertEquals(listOf(Triple(0, 3_000_000L, 3_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun noReadBackWithARamCap() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.readBackAllowed = false
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertFalse(fixture.signals.drop(before).any { it.signal == URG })
|
||||
assertEquals("1", fixture.control.readText())
|
||||
assertEquals(listOf(Triple(0, 3_000_000L, 3_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun cancelledFreezeReadsBackWhatThePageOutAlreadyMoved() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.afterSleep = { if (fixture.now >= 200 && fixture.signals.none { it.signal == STOP }) fixture.controller.cancelFreeze() }
|
||||
fixture.controller.freeze(pageOut = true) { assertFalse(it) }
|
||||
assertFalse(fixture.signals.any { it.signal == STOP })
|
||||
assertEquals(2, fixture.signals.count { it.signal == URG })
|
||||
assertEquals("0", fixture.control.readText())
|
||||
assertEquals(1, fixture.pagedIn.single().first)
|
||||
assertEquals(0L, fixture.pagedIn.single().second)
|
||||
assertTrue(fixture.pagedIn.single().third > 0L)
|
||||
}
|
||||
|
||||
@Test fun cancelledFreezeLeavesAPendingPageOutHandlerReadingBack() {
|
||||
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) }
|
||||
assertEquals(2, fixture.signals.count { it.signal == URG })
|
||||
assertEquals("0", fixture.control.readText())
|
||||
assertEquals(Triple(1, 0L, 0L), fixture.pagedIn.single())
|
||||
}
|
||||
|
||||
@Test fun cpuMustBeStableAcrossTwoCompleteSnapshots() {
|
||||
val fixture = fixture(17)
|
||||
var changes = 0
|
||||
@@ -380,9 +479,14 @@ class SessionSuspendControllerTest {
|
||||
val clock = File(tmp, ".bl-clock")
|
||||
var clockAtContinue: Long? = null
|
||||
val pagedOut = mutableListOf<Triple<Int, Long, Long>>()
|
||||
val pagedIn = mutableListOf<Triple<Int, Long, Long>>()
|
||||
var availableKb = 8_000_000L
|
||||
var totalKb = 16_000_000L
|
||||
var readBackAllowed = true
|
||||
private var gameResidentKb = 4_000_000L
|
||||
private var gameSwapKb = 0L
|
||||
private var pagingOut = false
|
||||
private var pagingIn = false
|
||||
var unacknowledged = -1
|
||||
var handlerStalls = false
|
||||
var handlerReplaced = false
|
||||
@@ -397,6 +501,8 @@ class SessionSuspendControllerTest {
|
||||
pagedOut += Triple(processes, resident, swap)
|
||||
this.unacknowledged = unacknowledged
|
||||
},
|
||||
readBackAllowed = { readBackAllowed },
|
||||
pagedIn = { processes, swap, swapBefore, _, _ -> pagedIn += Triple(processes, swap, swapBefore) },
|
||||
)
|
||||
|
||||
init { sync() }
|
||||
@@ -411,7 +517,7 @@ class SessionSuspendControllerTest {
|
||||
it.state !in listOf('T', 't', 'Z', 'X') && !(neverStop && it == game.threads.last())
|
||||
}?.state = 't'
|
||||
} else if (signal == URG) {
|
||||
pagingOut = true
|
||||
if (control.readText() == "0") { pagingOut = false; pagingIn = true } else pagingOut = true
|
||||
} else if (signal == CONT) {
|
||||
if (clockAtContinue == null && clock.isFile) {
|
||||
clockAtContinue = ByteBuffer.wrap(clock.readBytes()).order(ByteOrder.LITTLE_ENDIAN).long
|
||||
@@ -445,6 +551,12 @@ class SessionSuspendControllerTest {
|
||||
pagingOut = false
|
||||
}
|
||||
}
|
||||
if (pagingIn) {
|
||||
val moved = minOf(gameSwapKb, 1_000_000L)
|
||||
gameSwapKb -= moved
|
||||
gameResidentKb += moved
|
||||
if (gameSwapKb == 0L) pagingIn = false
|
||||
}
|
||||
if (root.threads.first().state != 'T' && !continuing) game.threads.forEach {
|
||||
if (it.state != 'Z') { it.state = 'S'; it.cpu++ }
|
||||
}
|
||||
@@ -453,6 +565,7 @@ class SessionSuspendControllerTest {
|
||||
}
|
||||
|
||||
fun sync() {
|
||||
File(proc, "meminfo").writeText("MemTotal:\t$totalKb kB\nMemFree:\t1000 kB\nMemAvailable:\t$availableKb kB\n")
|
||||
for (group in groups) {
|
||||
val dir = File(proc, group.pid.toString())
|
||||
if (group == root && rootGone) { dir.deleteRecursively(); continue }
|
||||
|
||||
Reference in new issue
Block a user