mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
fix(session): budget the post-resume read-back instead of skipping it
This commit is contained in:
1 parent
1ab090405e
commit
6fbca0b6d1
4 files changed
+123
-39
No files matched your search
@@ -885,10 +885,10 @@ class SessionService : Service() {
|
||||
))
|
||||
}
|
||||
},
|
||||
readBackAllowed = { SessionPrefs.ramCapMb(this) == 0 },
|
||||
ramCapKb = { SessionPrefs.ramCapMb(this) * 1024L },
|
||||
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)")
|
||||
Log.i(TAG, "left ${swapBeforeKb / 1024} MB in zram after resume (RAM cap reached, 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")
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ 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 =
|
||||
{ _, _, _, _, _, _ -> },
|
||||
private val readBackAllowed: () -> Boolean = { true },
|
||||
private val ramCapKb: () -> Long = { 0L },
|
||||
private val pagedIn: (processes: Int, swapKb: Long, swapBeforeKb: Long, availableKb: Long, millis: Long) -> Unit =
|
||||
{ _, _, _, _, _ -> },
|
||||
) {
|
||||
@@ -316,9 +316,11 @@ internal class SessionSuspendController(
|
||||
pagedOutTargets = emptyList()
|
||||
val control = zramControl?.takeIf { it.isFile } ?: return
|
||||
if (targets.isEmpty() || closing) return
|
||||
val before = memoryKb(targets).second
|
||||
val usage = targets.associateWith { memoryKb(listOf(it)) }
|
||||
val before = usage.values.sumOf { it.second }
|
||||
val (total, available) = meminfoKb()
|
||||
if (!readBackAllowed() || !canTake(before, total, available)) {
|
||||
val chosen = readBackTargets(usage, total, available)
|
||||
if (chosen.isEmpty()) {
|
||||
pagedIn(0, before, before, available, 0)
|
||||
return
|
||||
}
|
||||
@@ -328,13 +330,13 @@ internal class SessionSuspendController(
|
||||
return
|
||||
}
|
||||
val start = nowMillis()
|
||||
for (record in targets) send(record, SIGURG)
|
||||
var swap = before
|
||||
for (record in chosen) send(record, SIGURG)
|
||||
var swap = memoryKb(chosen).second
|
||||
var unchanged = 0
|
||||
try {
|
||||
while (!closing && nowMillis() - start < READ_BACK_TIMEOUT_MS) {
|
||||
sleep(READ_BACK_POLL_MS)
|
||||
val current = memoryKb(targets).second
|
||||
val current = memoryKb(chosen).second
|
||||
unchanged = if (current == swap) unchanged + 1 else 0
|
||||
swap = current
|
||||
if (swap == 0L || unchanged >= READ_BACK_STABLE_POLLS) break
|
||||
@@ -342,12 +344,24 @@ internal class SessionSuspendController(
|
||||
} catch (_: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
}
|
||||
pagedIn(targets.size, swap, before, available, nowMillis() - start)
|
||||
pagedIn(chosen.size, memoryKb(targets).second, before, available, nowMillis() - start)
|
||||
}
|
||||
|
||||
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)
|
||||
private fun readBackTargets(usage: Map<ProcessRecord, Pair<Long, Long>>, totalKb: Long, availableKb: Long): List<ProcessRecord> {
|
||||
val cap = ramCapKb()
|
||||
var budget = minOf(
|
||||
availableKb / READ_BACK_AVAILABLE_FACTOR,
|
||||
availableKb - maxOf(READ_BACK_HEADROOM_KB, totalKb / READ_BACK_TOTAL_SHARE),
|
||||
)
|
||||
if (cap > 0L) budget = minOf(budget, cap - usage.values.sumOf { it.first })
|
||||
val chosen = ArrayList<ProcessRecord>()
|
||||
for ((record, memory) in usage.entries.sortedByDescending { it.value.second }) {
|
||||
if (memory.second > budget) continue
|
||||
chosen += record
|
||||
budget -= memory.second
|
||||
}
|
||||
return chosen
|
||||
}
|
||||
|
||||
private fun meminfoKb(): Pair<Long, Long> = try {
|
||||
val values = HashMap<String, Long>()
|
||||
|
||||
@@ -151,10 +151,10 @@ class SessionSuspendControllerTest {
|
||||
assertEquals(listOf(Triple(0, 3_000_000L, 3_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun noReadBackWithARamCap() {
|
||||
@Test fun noReadBackPastTheRamCap() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.readBackAllowed = false
|
||||
fixture.ramCapKb = 3_500_000L
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
@@ -163,6 +163,56 @@ class SessionSuspendControllerTest {
|
||||
assertEquals(listOf(Triple(0, 3_000_000L, 3_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun readBackRunsUnderARamCapThatHoldsTheSession() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.ramCapKb = 4_000_000L
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertEquals(listOf(GAME), fixture.signals.drop(before).filter { it.signal == URG }.map { it.pid })
|
||||
assertEquals("0", fixture.control.readText())
|
||||
assertEquals(listOf(Triple(1, 0L, 3_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun readBackTakesTheLargestProcessesThatFitTheAvailableMemory() {
|
||||
val fixture = fixture(17)
|
||||
fixture.addService(2_000_000L)
|
||||
fixture.control.writeText("0")
|
||||
fixture.availableKb = 7_000_000L
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
assertEquals(2, fixture.pagedOut.single().first)
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertEquals(listOf(GAME), fixture.signals.drop(before).filter { it.signal == URG }.map { it.pid })
|
||||
assertEquals(listOf(Triple(1, 1_000_000L, 4_000_000L)), fixture.pagedIn)
|
||||
assertEquals(0L, fixture.swapKb(GAME))
|
||||
assertEquals(1_000_000L, fixture.swapKb(SERVICE))
|
||||
}
|
||||
|
||||
@Test fun ramCapLimitsReadBackToTheProcessesItHolds() {
|
||||
val fixture = fixture(17)
|
||||
fixture.addService(2_000_000L)
|
||||
fixture.control.writeText("0")
|
||||
fixture.ramCapKb = 5_500_000L
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertEquals(listOf(GAME), fixture.signals.drop(before).filter { it.signal == URG }.map { it.pid })
|
||||
assertEquals(listOf(Triple(1, 1_000_000L, 4_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun everyPagedOutProcessIsReadBackWhenMemoryAllows() {
|
||||
val fixture = fixture(17)
|
||||
fixture.addService(2_000_000L)
|
||||
fixture.control.writeText("0")
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val before = fixture.signals.size
|
||||
fixture.controller.resume { assertTrue(it) }
|
||||
assertEquals(setOf(GAME, SERVICE), fixture.signals.drop(before).filter { it.signal == URG }.map { it.pid }.toSet())
|
||||
assertEquals(listOf(Triple(2, 0L, 4_000_000L)), fixture.pagedIn)
|
||||
}
|
||||
|
||||
@Test fun cancelledFreezeReadsBackWhatThePageOutAlreadyMoved() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
@@ -460,6 +510,7 @@ class SessionSuspendControllerTest {
|
||||
private data class Task(val tid: Int, var started: Long, var state: Char, var cpu: Long = 0)
|
||||
private data class Group(val pid: Int, var parent: Int, var tracer: Int, val threads: MutableList<Task>)
|
||||
private data class SentSignal(val pid: Int, val signal: Int)
|
||||
private class Paging(var resident: Long, var swap: Long = 0L, var out: Boolean = false, var into: Boolean = false)
|
||||
|
||||
private class Fixture(val proc: File, tmp: File, count: Int, executor: QueueExecutor) {
|
||||
val root = Group(ROOT, 9000, 0, mutableListOf(Task(ROOT, 1000, 'S')))
|
||||
@@ -486,11 +537,8 @@ class SessionSuspendControllerTest {
|
||||
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 ramCapKb = 0L
|
||||
private val paging = linkedMapOf(GAME to Paging(4_000_000L))
|
||||
var unacknowledged = -1
|
||||
var handlerStalls = false
|
||||
var handlerReplaced = false
|
||||
@@ -506,12 +554,20 @@ class SessionSuspendControllerTest {
|
||||
pagedOut += Triple(processes, resident, swap)
|
||||
this.unacknowledged = unacknowledged
|
||||
},
|
||||
readBackAllowed = { readBackAllowed },
|
||||
ramCapKb = { ramCapKb },
|
||||
pagedIn = { processes, swap, swapBefore, _, _ -> pagedIn += Triple(processes, swap, swapBefore) },
|
||||
)
|
||||
|
||||
init { sync() }
|
||||
|
||||
fun addService(residentKb: Long) {
|
||||
groups += Group(SERVICE, ROOT, 0, mutableListOf(Task(SERVICE, 3000, 'S')))
|
||||
paging[SERVICE] = Paging(residentKb)
|
||||
sync()
|
||||
}
|
||||
|
||||
fun swapKb(pid: Int) = paging.getValue(pid).swap
|
||||
|
||||
private fun signal(pid: Int, signal: Int) {
|
||||
signals += SentSignal(pid, signal)
|
||||
val group = groups.first { it.pid == pid }
|
||||
@@ -522,7 +578,8 @@ class SessionSuspendControllerTest {
|
||||
it.state !in listOf('T', 't', 'Z', 'X') && !(neverStop && it == game.threads.last())
|
||||
}?.state = 't'
|
||||
} else if (signal == URG) {
|
||||
if (control.readText() == "0") { pagingOut = false; pagingIn = true } else pagingOut = true
|
||||
val memory = paging.getValue(pid)
|
||||
if (control.readText() == "0") { memory.out = false; memory.into = true } else memory.out = true
|
||||
} else if (signal == CONT) {
|
||||
if (clockAtContinue == null && clock.isFile) {
|
||||
clockAtContinue = ByteBuffer.wrap(clock.readBytes()).order(ByteOrder.LITTLE_ENDIAN).long
|
||||
@@ -544,23 +601,25 @@ class SessionSuspendControllerTest {
|
||||
|
||||
private fun sleep(millis: Long) {
|
||||
now += millis
|
||||
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
|
||||
for ((pid, memory) in paging) {
|
||||
if (memory.out && !handlerReplaced) {
|
||||
File(control.parentFile, "${control.name}.ack.$pid").apply { if (!exists()) writeText("") }
|
||||
}
|
||||
if (memory.out && !handlerStalls && !handlerReplaced) {
|
||||
if (memory.resident > 1_000_000L) {
|
||||
memory.resident -= 1_000_000L
|
||||
memory.swap += 1_000_000L
|
||||
} else {
|
||||
File(control.parentFile, "${control.name}.ack.$pid").writeText("1")
|
||||
memory.out = false
|
||||
}
|
||||
}
|
||||
if (memory.into) {
|
||||
val moved = minOf(memory.swap, 1_000_000L)
|
||||
memory.swap -= moved
|
||||
memory.resident += moved
|
||||
if (memory.swap == 0L) memory.into = 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++ }
|
||||
@@ -578,11 +637,12 @@ class SessionSuspendControllerTest {
|
||||
val stat = File(dir, "stat")
|
||||
if (group == game && missingGameStat) stat.delete()
|
||||
else stat.writeText(stat(group, group.threads.first()))
|
||||
val memory = paging[group.pid]
|
||||
File(dir, "status").writeText(
|
||||
"TracerPid: ${group.tracer}\n" +
|
||||
if (group == game) "VmRSS:\t$gameResidentKb kB\nVmSwap:\t$gameSwapKb kB\n" else "",
|
||||
if (memory != null) "VmRSS:\t${memory.resident} kB\nVmSwap:\t${memory.swap} kB\n" else "",
|
||||
)
|
||||
if (group == game) File(dir, "maps").writeText("7f00000000-7f00001000 r-xp 00000000 fe:05 42 /usr/local/lib/libblsession.so\n")
|
||||
if (memory != null) File(dir, "maps").writeText("7f00000000-7f00001000 r-xp 00000000 fe:05 42 /usr/local/lib/libblsession.so\n")
|
||||
val taskDir = File(dir, "task")
|
||||
if (group == game && missingGameTasks) { taskDir.deleteRecursively(); continue }
|
||||
taskDir.mkdirs()
|
||||
@@ -629,6 +689,7 @@ class SessionSuspendControllerTest {
|
||||
private companion object {
|
||||
const val ROOT = 100
|
||||
const val GAME = 200
|
||||
const val SERVICE = 300
|
||||
const val STOP = 19
|
||||
const val CONT = 18
|
||||
const val URG = 23
|
||||
|
||||
@@ -109,6 +109,15 @@ Without root an app cannot write its memory cgroup's `memory.reclaim` (owned by
|
||||
and KGSL's per-process GPU reclaim `state` is root-only. Page-out therefore runs inside each
|
||||
process through the preload, and GPU memory itself cannot be compressed from userspace.
|
||||
|
||||
## Read-back after a suspend
|
||||
|
||||
A hidden or screen-off suspend pages the session out before it stops it. After the resume, the
|
||||
processes that were paged out are read back largest first, as long as their swap fits the
|
||||
budget: half of `MemAvailable`, while leaving the larger of 1 GiB and a tenth of RAM available.
|
||||
With a RAM cap, the budget also ends at the cap minus the session's resident memory. Processes
|
||||
that do not fit keep their memory in zram and fault it back on demand. Before this, read-back was
|
||||
all or nothing, and any RAM cap turned it off even when the session was far below the cap.
|
||||
|
||||
## CRAM and the bounded pager experiment
|
||||
|
||||
[Meta's CRAM proposal](https://lpc.events/event/20/contributions/2424/) uses memory compression
|
||||
|
||||
Reference in new issue
Block a user