mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
feat: page the session out to zram before suspending it, and zram is always on where supported
This commit is contained in:
1 parent
aaa2e40def
commit
240b7fcce6
43 files changed
+122
-168
No files matched your search
@@ -7,7 +7,6 @@ import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.core.CpuAllocationDefaults
|
||||
import com.droiddeck.launcher.core.GameEnvironment
|
||||
import com.droiddeck.launcher.core.TextureFiltering
|
||||
import com.droiddeck.launcher.core.ZramSupport
|
||||
import com.droiddeck.launcher.gpu.Cardboard
|
||||
import com.droiddeck.launcher.gpu.ScreenEffects
|
||||
import org.json.JSONObject
|
||||
@@ -1034,15 +1033,6 @@ object SessionPrefs {
|
||||
}.apply()
|
||||
}
|
||||
|
||||
fun zramCompression(context: Context): Boolean {
|
||||
val prefs = prefs(context)
|
||||
return if (prefs.contains("zramCompression")) prefs.getBoolean("zramCompression", false) else ZramSupport.recommended(context)
|
||||
}
|
||||
|
||||
fun setZramCompression(context: Context, on: Boolean) {
|
||||
prefs(context).edit().putBoolean("zramCompression", on).apply()
|
||||
}
|
||||
|
||||
|
||||
fun gameStorage(context: Context): String = prefs(context).getString("gameStorage", "") ?: ""
|
||||
|
||||
|
||||
@@ -581,6 +581,17 @@ class SessionService : Service() {
|
||||
resumeAudio = {
|
||||
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")
|
||||
mainHandler.post {
|
||||
SessionEvents.record("session.paged_out", mapOf(
|
||||
"processes" to processes, "residentMb" to residentKb / 1024,
|
||||
"swapMb" to swapKb / 1024, "swapBeforeMb" to swapBeforeKb / 1024, "ms" to millis,
|
||||
))
|
||||
}
|
||||
},
|
||||
)
|
||||
mainHandler.post { updateSuspendPolicy() }
|
||||
updateDownloadMonitoring()
|
||||
@@ -661,8 +672,7 @@ class SessionService : Service() {
|
||||
guest.add("BL_SYNC_FALLBACK=" + (if (SessionPrefs.syncFallback(this)) "1" else "0"))
|
||||
guest.add("BL_FSYNC_FIRST=" + (if (SessionPrefs.fsyncFirst(this)) "1" else "0"))
|
||||
guest.add("BL_GAMESCOPE_REALTIME=" + (if (SessionPrefs.gamescopeRealtime(this)) "1" else "0"))
|
||||
if (SessionState.mode == MODE_STEAM && SessionPrefs.zramCompression(this) &&
|
||||
com.droiddeck.launcher.core.ZramSupport.supported()) guest.add("BL_ZRAM=1")
|
||||
if (com.droiddeck.launcher.core.ZramSupport.supported()) guest.add("BL_ZRAM=1")
|
||||
guest.add("BANNER_AUDIO_DIRECT_DECAY=0")
|
||||
val extra = extraEnv() + com.droiddeck.launcher.agent.AgentEnv.beginSession(this)
|
||||
if (extra.none { it.startsWith("TU_DEBUG=") }) guest.add("$GAME_TU_DEBUG=" + (tuOverride ?: if (tuDriver) "sysmem" else ""))
|
||||
@@ -1386,7 +1396,9 @@ class SessionService : Service() {
|
||||
"nativeSleep" to (nativeSleepToken != null),
|
||||
))
|
||||
if (shouldSuspend) {
|
||||
controller.freeze { success ->
|
||||
controller.freeze(
|
||||
pageOut = steamSleepToken != null || nativeSleepToken != null || !activityVisible || !screenOn,
|
||||
) { success ->
|
||||
mainHandler.post {
|
||||
if (gen != sessionGen || !SessionState.running) return@post
|
||||
suspendOperationPending = false
|
||||
|
||||
@@ -18,6 +18,8 @@ internal class SessionSuspendController(
|
||||
private val sleep: (Long) -> Unit = Thread::sleep,
|
||||
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 enum class Role { SESSION_ROOT, SESSION_CHILD, HELPER_ROOT, HELPER_CHILD }
|
||||
|
||||
@@ -40,7 +42,7 @@ internal class SessionSuspendController(
|
||||
@Volatile private var closing = false
|
||||
private var frozen = emptyList<ProcessRecord>()
|
||||
|
||||
fun freeze(completion: (Boolean) -> Unit) {
|
||||
fun freeze(pageOut: Boolean = false, completion: (Boolean) -> Unit) {
|
||||
submit({
|
||||
val records = frozen.associateByTo(LinkedHashMap()) { it.pid to it.started }
|
||||
var complete = false
|
||||
@@ -59,6 +61,7 @@ internal class SessionSuspendController(
|
||||
records[it.pid to it.started] = ProcessRecord(it.pid, it.started, Role.HELPER_ROOT)
|
||||
}
|
||||
}
|
||||
if (pageOut) pageOut(records, root.pid, helpers)
|
||||
val deadline = nowMillis() + FREEZE_TIMEOUT_MS
|
||||
var previous: Map<TaskIdentity, Long>? = null
|
||||
for (pass in 0 until FREEZE_MAX_PASSES) {
|
||||
@@ -233,6 +236,57 @@ internal class SessionSuspendController(
|
||||
return tasks.takeIf { stopped && it.isNotEmpty() }
|
||||
}
|
||||
|
||||
// A stopped process cannot run libblsession's SIGURG handler, so the page-out has to finish
|
||||
// before the stop. Android's low-memory killer otherwise finds the frozen game resident in RAM
|
||||
// with most of zram still free.
|
||||
private fun pageOut(records: MutableMap<Pair<Int, Long>, ProcessRecord>, root: Int, helpers: List<Int>) {
|
||||
val control = zramControl?.takeIf { it.isFile } ?: return
|
||||
try {
|
||||
addSnapshot(records, root, helpers)
|
||||
control.writeText("1")
|
||||
} catch (_: IOException) {
|
||||
return
|
||||
}
|
||||
val targets = records.values.filter { record ->
|
||||
try {
|
||||
File(procRoot, "${record.pid}/maps").useLines { lines -> lines.any { it.endsWith("/libblsession.so") } }
|
||||
} catch (_: IOException) {
|
||||
false
|
||||
}
|
||||
}
|
||||
if (targets.isEmpty()) return
|
||||
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) {
|
||||
sleep(PAGE_OUT_POLL_MS)
|
||||
val swap = memoryKb(targets).second
|
||||
steady = if (swap == previous) steady + 1 else 0
|
||||
previous = swap
|
||||
}
|
||||
val after = memoryKb(targets)
|
||||
pagedOut(targets.size, after.first, after.second, before.second, nowMillis() - start)
|
||||
}
|
||||
|
||||
private fun memoryKb(records: List<ProcessRecord>): Pair<Long, Long> {
|
||||
var resident = 0L
|
||||
var swap = 0L
|
||||
for (record in records) {
|
||||
try {
|
||||
File(procRoot, "${record.pid}/status").useLines { lines ->
|
||||
for (line in lines) {
|
||||
val kb = line.substringAfter(':').trim().substringBefore(' ').toLongOrNull() ?: continue
|
||||
if (line.startsWith("VmRSS:")) resident += kb else if (line.startsWith("VmSwap:")) swap += kb
|
||||
}
|
||||
}
|
||||
} catch (_: IOException) {
|
||||
}
|
||||
}
|
||||
return resident to swap
|
||||
}
|
||||
|
||||
// PRoot traces every guest. A stop sent while it runs becomes a group stop that it restarts
|
||||
// thread by thread; once it is stopped, each thread that takes a SIGSTOP stays parked in its
|
||||
// signal-delivery stop. So the roots stop first and every guest thread gets its own SIGSTOP.
|
||||
@@ -310,6 +364,10 @@ internal class SessionSuspendController(
|
||||
private companion object {
|
||||
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_TIMEOUT_MS = 12_000L
|
||||
const val RESTORE_CHECKS = 80
|
||||
const val FREEZE_TIMEOUT_MS = 8_000L
|
||||
const val FREEZE_MAX_PASSES = 256
|
||||
|
||||
Reference in new issue
Block a user