Compare commits

..
5 changed files with 92 additions and 37 deletions

No files matched your search

@@ -242,7 +242,7 @@ public final class LinuxRuntime {
// A kernel that refuses to list /sys/class/thermal (Pixel 10, Android 17) made mangoapp abort
// at start ("filesystem_error ... Permission denied: '/sys/class/thermal/'") until its
// wrapper gave up: it lists the directory for CPU temperatures. Empty there instead.
if (new File("/sys/class/thermal").list() == null) bind(cmd, new File(root, "etc/droiddeck/empty").getPath() + ":/sys/class/thermal");
if (new File("/sys/class/thermal").list() == null) bindStandIn(cmd, root, "/sys/class/thermal");
bind(cmd, context.getFilesDir().getPath());
bind(cmd, context.getCacheDir().getPath());
bind(cmd, liveDir(context).getPath());
@@ -302,7 +302,7 @@ public final class LinuxRuntime {
}
}
if (unreadablePciDevice()) {
bind(cmd, new File(root, "etc/droiddeck/empty").getPath() + ":/sys/bus/pci/devices");
bindStandIn(cmd, root, "/sys/bus/pci/devices");
}
if (com.droiddeck.launcher.gpu.SystemVulkanDriver.isPowerVr()) {
bindPowerVrGpuNode(context, cmd);
@@ -337,6 +337,16 @@ public final class LinuxRuntime {
android.util.Log.i("LinuxRuntime", "PowerVR: exposing real " + name + " through a listable directory");
}
static File standIn(File root, String guestPath) {
File dir = new File(root, "etc/droiddeck/stand-in" + guestPath);
dir.mkdirs();
return dir;
}
private static void bindStandIn(List<String> cmd, File root, String guestPath) {
bind(cmd, standIn(root, guestPath).getPath() + ":" + guestPath);
}
private static boolean unreadablePciDevice() {
File[] devices = new File("/sys/bus/pci/devices").listFiles();
if (devices == null) {
@@ -59,8 +59,19 @@ class GpuMemComponent(private val file: File, private val root: File) : SessionP
private fun sessionPids(): List<String> =
File("/proc").list()?.filter { it.isNotEmpty() && it.all(Char::isDigit) }.orEmpty()
private fun kgslMappedKb(pid: String): Long? =
runCatching { File("/proc/$pid/maps").useLines(MemoryReadout::gpuMappedKb) }.getOrNull()
private fun kgslMappedKb(pid: String): Long? = runCatching {
var bytes = 0L
File("/proc/$pid/maps").forEachLine { line ->
if (line.endsWith("kgsl-3d0") || line.contains("/dev/dri/renderD")) {
val dash = line.indexOf('-')
val space = line.indexOf(' ')
if (dash > 0 && space > dash) {
bytes += line.substring(dash + 1, space).toLong(16) - line.substring(0, dash).toLong(16)
}
}
}
bytes / 1024
}.getOrNull()
private fun write(bytes: Long) {
try {
@@ -73,27 +73,9 @@ internal object MemoryReadout {
}
children[pid]?.let(queue::addAll)
}
return Split(game, droiddeck, gpuKb() ?: mappedGpuKb(seen))
return Split(game, droiddeck, gpuKb())
}
fun gpuMappedKb(maps: Sequence<String>): Long {
var bytes = 0L
maps.forEach { line ->
if (line.endsWith("kgsl-3d0") || line.contains("/dev/dri/renderD")) {
val dash = line.indexOf('-')
val space = line.indexOf(' ')
if (dash > 0 && space > dash) {
bytes += line.substring(dash + 1, space).toLong(16) - line.substring(0, dash).toLong(16)
}
}
}
return bytes / 1024
}
private fun mappedGpuKb(pids: Collection<Int>): Long? =
pids.sumOf { pid -> runCatching { File("/proc/$pid/maps").useLines(::gpuMappedKb) }.getOrDefault(0L) }
.takeIf { it > 0 }
private fun parentOf(pid: Int): Int? {
val stat = runCatching { File("/proc/$pid/stat").readText() }.getOrNull() ?: return null
val close = stat.lastIndexOf(')')
@@ -0,0 +1,66 @@
package com.droiddeck.launcher.runtime
import android.content.ContextWrapper
import java.io.File
import java.nio.file.Files
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
@RunWith(RobolectricTestRunner::class)
class LinuxRuntimeStandInTest {
private val root = Files.createTempDirectory("guest-stand-ins").toFile()
private val context = object : ContextWrapper(RuntimeEnvironment.getApplication()) {
override fun getFilesDir() = File(root, "files").apply { mkdirs() }
override fun getCacheDir() = File(root, "cache").apply { mkdirs() }
override fun getNoBackupFilesDir() = File(root, "no_backup").apply { mkdirs() }
}
private val guestRoot get() = LinuxRuntime.rootDir(context)
@After fun tearDown() {
root.deleteRecursively()
}
@Test fun aBindUnderTheThermalStandInLeavesThePciStandInEmpty() {
val thermal = LinuxRuntime.standIn(guestRoot, "/sys/class/thermal")
val pci = LinuxRuntime.standIn(guestRoot, "/sys/bus/pci/devices")
File(thermal, "thermal_zone28").mkdirs()
File(thermal, "thermal_zone26").mkdirs()
assertTrue(pci.isDirectory)
assertEquals(emptyList<String>(), pci.list()!!.toList())
assertTrue(File(thermal, "thermal_zone28").isDirectory)
}
@Test fun standInsAreSeparateFromEachOtherAndFromTheSharedEmptyDirectory() {
val empty = File(guestRoot, "etc/droiddeck/empty").canonicalFile
val thermal = LinuxRuntime.standIn(guestRoot, "/sys/class/thermal").canonicalFile
val pci = LinuxRuntime.standIn(guestRoot, "/sys/bus/pci/devices").canonicalFile
assertNotEquals(thermal, pci)
for (dir in listOf(thermal, pci)) {
assertNotEquals(empty, dir)
assertFalse(dir.startsWith(empty))
}
assertFalse(pci.startsWith(thermal))
assertFalse(thermal.startsWith(pci))
assertEquals(thermal, LinuxRuntime.standIn(guestRoot, "/sys/class/thermal").canonicalFile)
}
@Test fun selinuxKeepsTheEmptyDirectoryAndNoSourceDirectoryIsSharedByTwoTargets() {
val specs = LinuxRuntime.binds(context, null, null, null, null)
val empty = File(guestRoot, "etc/droiddeck/empty").path
assertTrue(specs.contains("$empty:/sys/fs/selinux"))
val guestDirs = specs.map { it.split(":", limit = 2) }
.filter { it.size == 2 && it[0].startsWith(File(guestRoot, "etc/droiddeck").path) && File(it[0]).isDirectory }
.groupBy({ it[0] }, { it[1] })
for ((source, targets) in guestDirs) assertEquals(source, 1, targets.size)
}
}
@@ -54,20 +54,6 @@ class MemoryReadoutTest {
assertEquals(0L, MemoryReadout.androidKb(device, MemoryReadout.Split(gameKb = 6_000, droiddeckKb = 2_000, gpuKb = 1_000)))
}
@Test fun gpuMemoryIsWhatTheGpuNodesMapIntoAProcess() {
val maps = """
7a00000000-7a00200000 rw-s 100000000 00:0c 512 /dev/dri/renderD128
7a00200000-7a00600000 rw-s 100200000 00:0c 512 /dev/dri/renderD128
7b00000000-7b00100000 rw-s 00000000 00:0d 300 /dev/kgsl-3d0
7c00000000-7c40000000 rw-p 00000000 00:00 0 [anon:dalvik-main space]
7d00000000-7d00400000 rw-s 00000000 00:0e 77 /dmabuf:
7e00000000-7e00100000 r--p 00000000 fd:00 1234 /data/app/lib/arm64/libdroiddeck.so
""".trimIndent()
assertEquals(2048L + 4096L + 1024L, MemoryReadout.gpuMappedKb(maps.lineSequence()))
assertEquals(0L, MemoryReadout.gpuMappedKb(maps.lineSequence().drop(3)))
assertEquals(0L, MemoryReadout.gpuMappedKb(emptySequence()))
}
@Test fun theLogLineSaysEveryPartInMegabytes() {
val device = MemoryReadout.Device(totalKb = 11_379_920, availableKb = 1_048_576, swapTotalKb = 16_777_212, swapFreeKb = 8_388_606)
val split = MemoryReadout.Split(gameKb = 2_097_152, droiddeckKb = 524_288, gpuKb = 3_145_728)