mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
Merge current main into PR #1042
This commit is contained in:
commit
e1e2017590
47 files changed
+359
-188
No files matched your search
@@ -14,20 +14,6 @@ class ZramSupportTest {
|
||||
assertFalse(ZramSupport.kernelAtLeast("", 5, 4))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun underTwelveGbFromAdvertisedOrTotalMemory() {
|
||||
val gb = 1_000_000_000L
|
||||
val gib = 1L shl 30
|
||||
assertTrue(ZramSupport.belowTwelveGb(8 * gb, 0))
|
||||
assertTrue(ZramSupport.belowTwelveGb(8 * gib, 0))
|
||||
assertFalse(ZramSupport.belowTwelveGb(12 * gb, 0))
|
||||
assertFalse(ZramSupport.belowTwelveGb(12 * gib, 0))
|
||||
assertTrue(ZramSupport.belowTwelveGb(0, 7_400_000L * 1024))
|
||||
assertTrue(ZramSupport.belowTwelveGb(0, 9_300_000L * 1024))
|
||||
assertFalse(ZramSupport.belowTwelveGb(0, 11_100_000L * 1024))
|
||||
assertFalse(ZramSupport.belowTwelveGb(0, 15_600_380L * 1024))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun swapTotalComesFromMeminfo() {
|
||||
assertEquals(12582908L, ZramSupport.swapTotalKb(listOf("MemTotal: 15600380 kB", "SwapTotal: 12582908 kB")))
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import android.os.Looper
|
||||
import java.util.concurrent.AbstractExecutorService
|
||||
import java.util.concurrent.TimeUnit
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33])
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class SessionSleepRequestTest {
|
||||
private lateinit var service: SessionService
|
||||
private val work = ArrayDeque<Runnable>()
|
||||
private val executor = object : AbstractExecutorService() {
|
||||
override fun execute(command: Runnable) { work.addLast(command) }
|
||||
override fun shutdown() {}
|
||||
override fun shutdownNow(): MutableList<Runnable> = mutableListOf()
|
||||
override fun isShutdown() = false
|
||||
override fun isTerminated() = false
|
||||
override fun awaitTermination(timeout: Long, unit: TimeUnit) = false
|
||||
}
|
||||
|
||||
@Before fun setUp() {
|
||||
service = Robolectric.buildService(SessionService::class.java).get()
|
||||
SessionState.running = true
|
||||
SessionState.suspended = false
|
||||
SessionState.mode = SessionService.MODE_RUN
|
||||
ReflectionHelpers.setField(service, "suspendPolicy", SessionPrefs.SUSPEND_NEVER)
|
||||
ReflectionHelpers.setField(service, "suspendController", SessionSuspendController(
|
||||
sessionRoot = { -1 }, helperRoots = { emptyList() },
|
||||
suspendAudio = {}, resumeAudio = {}, executor = executor,
|
||||
))
|
||||
}
|
||||
|
||||
@After fun tearDown() {
|
||||
SessionState.running = false
|
||||
SessionState.suspended = false
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
work.clear()
|
||||
}
|
||||
|
||||
@Test fun cancelledFreezePreservesAndRetriesANewerSteamRequest() {
|
||||
ReflectionHelpers.setField(service, "steamSleepToken", "old-request")
|
||||
ReflectionHelpers.callInstanceMethod<Void>(service, "updateSuspendPolicy")
|
||||
assertEquals(1, work.size)
|
||||
ReflectionHelpers.setField(service, "freezeCancelled", true)
|
||||
ReflectionHelpers.setField(service, "steamSleepToken", "new-request")
|
||||
work.removeFirst().run()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals("new-request", ReflectionHelpers.getField<String>(service, "steamSleepToken"))
|
||||
assertEquals(1, work.size)
|
||||
assertFalse(ReflectionHelpers.getField(service, "suspendAttemptFailed"))
|
||||
}
|
||||
|
||||
@Test fun cancelledFreezeCompletesItsOwnRequestWithoutRetryingIt() {
|
||||
ReflectionHelpers.setField(service, "steamSleepToken", "old-request")
|
||||
ReflectionHelpers.callInstanceMethod<Void>(service, "updateSuspendPolicy")
|
||||
ReflectionHelpers.setField(service, "freezeCancelled", true)
|
||||
work.removeFirst().run()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(null, ReflectionHelpers.getField<String?>(service, "steamSleepToken"))
|
||||
assertEquals(0, work.size)
|
||||
assertFalse(ReflectionHelpers.getField(service, "suspendAttemptFailed"))
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,66 @@ class SessionSuspendControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun pageOutRunsInPreloadedGuestsBeforeTheyStop() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
val urgent = fixture.signals.filter { it.signal == URG }
|
||||
assertEquals(listOf(GAME), urgent.map { it.pid })
|
||||
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() {
|
||||
val visible = fixture(17)
|
||||
visible.control.writeText("0")
|
||||
visible.controller.freeze { assertTrue(it) }
|
||||
assertFalse(visible.signals.any { it.signal == URG })
|
||||
assertEquals("0", visible.control.readText())
|
||||
val disabled = fixture(17)
|
||||
disabled.controller.freeze(pageOut = true) { assertTrue(it) }
|
||||
assertFalse(disabled.signals.any { it.signal == URG })
|
||||
assertTrue(disabled.pagedOut.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun cpuMustBeStableAcrossTwoCompleteSnapshots() {
|
||||
val fixture = fixture(17)
|
||||
var changes = 0
|
||||
@@ -253,13 +313,13 @@ class SessionSuspendControllerTest {
|
||||
}
|
||||
|
||||
private fun fixture(count: Int, executor: QueueExecutor = InlineExecutor()) =
|
||||
Fixture(temporary.newFolder(), count, executor)
|
||||
Fixture(temporary.newFolder(), temporary.newFolder(), count, executor)
|
||||
|
||||
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 Fixture(val proc: File, count: Int, executor: QueueExecutor) {
|
||||
private class Fixture(val proc: File, tmp: File, count: Int, executor: QueueExecutor) {
|
||||
val root = Group(ROOT, 9000, 0, mutableListOf(Task(ROOT, 1000, 'S')))
|
||||
val game = Group(GAME, ROOT, ROOT, MutableList(count) { Task(GAME + it, 2000L + it, 'S') })
|
||||
val groups = mutableListOf(root, game)
|
||||
@@ -276,10 +336,23 @@ class SessionSuspendControllerTest {
|
||||
var emptyGameTasks = false
|
||||
var rootGone = false
|
||||
var afterSleep: (() -> Unit)? = null
|
||||
val control = File(tmp, ".bl-zram")
|
||||
val pagedOut = mutableListOf<Triple<Int, Long, Long>>()
|
||||
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, unacknowledged, resident, swap, _, _ ->
|
||||
pagedOut += Triple(processes, resident, swap)
|
||||
this.unacknowledged = unacknowledged
|
||||
},
|
||||
)
|
||||
|
||||
init { sync() }
|
||||
@@ -293,6 +366,8 @@ class SessionSuspendControllerTest {
|
||||
else group.threads.firstOrNull {
|
||||
it.state !in listOf('T', 't', 'Z', 'X') && !(neverStop && it == game.threads.last())
|
||||
}?.state = 't'
|
||||
} else if (signal == URG) {
|
||||
pagingOut = true
|
||||
} else if (signal == CONT) {
|
||||
continuing = true
|
||||
if (!(pid == GAME && blockGameContinue)) group.threads.forEach { if (it.state != 'Z') it.state = 'S' }
|
||||
@@ -311,6 +386,18 @@ 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
|
||||
}
|
||||
}
|
||||
if (root.threads.first().state != 'T' && !continuing) game.threads.forEach {
|
||||
if (it.state != 'Z') { it.state = 'S'; it.cpu++ }
|
||||
}
|
||||
@@ -326,7 +413,11 @@ class SessionSuspendControllerTest {
|
||||
val stat = File(dir, "stat")
|
||||
if (group == game && missingGameStat) stat.delete()
|
||||
else stat.writeText(stat(group, group.threads.first()))
|
||||
File(dir, "status").writeText("TracerPid: ${group.tracer}\n")
|
||||
File(dir, "status").writeText(
|
||||
"TracerPid: ${group.tracer}\n" +
|
||||
if (group == game) "VmRSS:\t$gameResidentKb kB\nVmSwap:\t$gameSwapKb kB\n" else "",
|
||||
)
|
||||
if (group == game) 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()
|
||||
@@ -375,5 +466,6 @@ class SessionSuspendControllerTest {
|
||||
const val GAME = 200
|
||||
const val STOP = 19
|
||||
const val CONT = 18
|
||||
const val URG = 23
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user