From 94c2d811c7f30814859c91ae287d35b20a6cbd56 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Tue, 1 Dec 2026 20:38:40 +0000 Subject: [PATCH] fix(hud): GPU load on GPUs without kgsl, such as Mali --- .../droiddeck/launcher/hud/HudMetrics.java | 9 ++- .../launcher/session/GpuStatsComponent.kt | 39 +++++++++---- .../launcher/session/SessionService.kt | 3 +- .../launcher/session/GpuStatsBindsTest.kt | 2 +- .../GpuStatsComponentWithoutKgslTest.kt | 57 +++++++++++++++++++ 5 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/session/GpuStatsComponentWithoutKgslTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/hud/HudMetrics.java b/app/src/main/java/com/droiddeck/launcher/hud/HudMetrics.java index 2fab8cdc14..bad035426f 100644 --- a/app/src/main/java/com/droiddeck/launcher/hud/HudMetrics.java +++ b/app/src/main/java/com/droiddeck/launcher/hud/HudMetrics.java @@ -108,7 +108,11 @@ public class HudMetrics { } private List discoverGpuUsagePaths() { - if (gpuUsagePathsCache != null) return gpuUsagePathsCache; + if (gpuUsagePathsCache == null) gpuUsagePathsCache = gpuUsagePaths(); + return gpuUsagePathsCache; + } + + public static List gpuUsagePaths() { LinkedHashSet candidates = new LinkedHashSet<>(); for (String p : GPU_USAGE_STATIC_PATHS) { @@ -157,8 +161,7 @@ public class HudMetrics { } } - gpuUsagePathsCache = new ArrayList<>(candidates); - return gpuUsagePathsCache; + return new ArrayList<>(candidates); } private static final String[] GPU_USAGE_STATIC_PATHS = { diff --git a/app/src/main/java/com/droiddeck/launcher/session/GpuStatsComponent.kt b/app/src/main/java/com/droiddeck/launcher/session/GpuStatsComponent.kt index b0c0b45b2f..10b868a329 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/GpuStatsComponent.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/GpuStatsComponent.kt @@ -3,11 +3,18 @@ package com.droiddeck.launcher.session import android.os.SystemClock import android.util.Log import com.droiddeck.launcher.core.SessionPart +import com.droiddeck.launcher.hud.HudMetrics import com.droiddeck.launcher.runtime.LinuxRuntime import java.io.File import java.io.RandomAccessFile -class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() { +class GpuStatsComponent( + val dir: File, + private val root: File, + private val metrics: HudMetrics? = null, + private val kgsl: Boolean = File(KGSL).exists(), + private val loadNodes: () -> List = HudMetrics::gpuUsagePaths, +) : SessionPart() { @Volatile private var running = false private var thread: Thread? = null private val feeds = ArrayList() @@ -23,6 +30,7 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() { ) fun binds(): List { + if (!kgsl) return emptyList() val list = mutableListOf(dir.path + ":" + KGSL) if (File(dir, "perf_now").exists()) list.add(File(dir, "perf_now").path + ":" + MSM_LOAD) if (File(dir, "cur_freq").exists()) list.add(File(dir, "cur_freq").path + ":" + MSM_CLOCK) @@ -33,10 +41,9 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() { sources = "" KgslBusy.status = "not tried" KgslClock.status = "not tried" - if (!File(KGSL).exists()) return false val unreadable = MANGOHUD_FILES.any { File(KGSL, it).let { f -> f.exists() && !f.canRead() } } // Where SELinux hides the kgsl nodes completely (exists() is false too), the layer and cycle-counter feeds stand in. - if (!unreadable && !File(GPUBUSY).canRead() && !loadBlocked()) return false + if (kgsl && !unreadable && !File(GPUBUSY).canRead() && !loadBlocked()) return false return try { dir.mkdirs() dir.listFiles()?.forEach { java.nio.file.Files.deleteIfExists(it.toPath()) } @@ -44,19 +51,20 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() { PASSTHROUGH_FILES.forEach { name -> runCatching { File(dir, name).writeText(File(KGSL, name).readText()) } } - (loadFeed() ?: blockedLoadFeed())?.let { load -> + ((if (kgsl) loadFeed() else nodeLoadFeed()) ?: blockedLoadFeed())?.let { load -> feeds.add(load) feeds.add(Feed(File(dir, "perf_now"), load.source, load.read, "%-6s\n", percent = true)) } - (pick("clock_mhz", clockSources(), "%5d\n") { mhz(it) } ?: cycleClockFeed())?.let { clock -> + (pick("clock_mhz", clockSources(), "%5d\n") { mhz(it) } ?: (if (kgsl) cycleClockFeed() else null))?.let { clock -> feeds.add(clock) feeds.add(Feed(File(dir, "cur_freq"), clock.source, { clock.read()?.times(1_000_000) }, "%-10d\n")) } - pick("temp", tempSources(), "%6d\n") { milliCelsius(it) }?.let { feeds.add(it) } + if (kgsl) pick("temp", tempSources(), "%6d\n") { milliCelsius(it) }?.let { feeds.add(it) } + if (!kgsl && feeds.isEmpty()) return false refresh() createMissing(feeds.map { it.file }) describeSources() - Log.i(TAG, "hud: kgsl stats from the session's own: $sources") + Log.i(TAG, "hud: GPU stats from the session's own: $sources") true } catch (e: Exception) { Log.w(TAG, "hud: could not stand in for kgsl stats: $e") @@ -146,10 +154,10 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() { // Vulkan layer and the compositor time their own GPU work until the memstore timestamps are seen moving, and // for the whole session where they never move. private fun blockedLoadFeed(): Feed? { - if (!loadBlocked()) return null + if (!noLoadNode(kgsl, loadNodes)) return null val load = LayerLoad(guestLoadDir(root), extra = ::compositorBusyNs, onWanted = ::compositorTiming) .also { it.clear(); layerLoad = it } - val sampled = KgslBusy.open()?.let { reader -> + val sampled = if (!kgsl) null else KgslBusy.open()?.let { reader -> memstore = reader MemstoreLoad(reader::counts).also { memstoreLoad = it } } @@ -289,6 +297,12 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() { } } + private fun nodeLoadFeed(): Feed? { + val metrics = metrics ?: return null + val node = loadNodes().firstOrNull() ?: return null + return Feed(File(dir, "gpu_busy_percentage"), node, { metrics.gpuUsagePercent?.toLong() }, "%3d %%\n") + } + private fun loadFeed(): Feed? { latchedBusyFeed()?.let { return it } return pick("gpu_busy_percentage", LOAD_SOURCES, "%3d %%\n") { it.coerceIn(0, 100) } @@ -362,9 +376,14 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() { /** True when this app can see kgsl but none of its GPU load nodes (SELinux-blocked, e.g. the Retroid Pocket Duo). */ fun loadBlocked(): Boolean = File(KGSL).exists() && !File(GPUBUSY).canRead() && LOAD_SOURCES.none { File(it).canRead() } + private fun noLoadNode(): Boolean = noLoadNode(File(KGSL).exists(), HudMetrics::gpuUsagePaths) + + internal fun noLoadNode(kgsl: Boolean, loadNodes: () -> List): Boolean = + if (kgsl) loadBlocked() else loadNodes().isEmpty() + /** Turns on the in-game layer (tools/gpuload) where the load is blocked; empty elsewhere. */ fun guestEnv(root: File): List { - if (!loadBlocked()) return emptyList() + if (!noLoadNode()) return emptyList() guestLoadDir(root).mkdirs() return listOf("DROIDDECK_GPU_LOAD=1", "DROIDDECK_GPU_LOAD_DIR=$GUEST_LOAD_DIR") } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 56c4f27f57..6a26d39947 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -4,6 +4,7 @@ import com.droiddeck.launcher.core.AddonPack import com.droiddeck.launcher.gpu.LinuxVulkanDriver import com.droiddeck.launcher.gpu.LinuxVulkanDriverManager import com.droiddeck.launcher.gpu.SystemVulkanDriver +import com.droiddeck.launcher.hud.HudMetrics import android.app.Notification import android.app.NotificationChannel @@ -1293,7 +1294,7 @@ class SessionService : Service() { gpuMem.attach(this) components.add(gpuMem) } - val gpuStats = GpuStatsComponent(File(filesDir, "session/sys/kgsl-3d0"), LinuxRuntime.rootDir(this)) + val gpuStats = GpuStatsComponent(File(filesDir, "session/sys/kgsl-3d0"), LinuxRuntime.rootDir(this), HudMetrics(this)) if (gpuStats.prepare()) { gpuStats.attach(this) components.add(gpuStats) diff --git a/app/src/test/java/com/droiddeck/launcher/session/GpuStatsBindsTest.kt b/app/src/test/java/com/droiddeck/launcher/session/GpuStatsBindsTest.kt index 4343683f35..17872a7af0 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/GpuStatsBindsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/GpuStatsBindsTest.kt @@ -13,7 +13,7 @@ class GpuStatsBindsTest { @Test fun feedsWithNoValueYetAreStillBound() { val dir = tmp.newFolder("kgsl-3d0") GpuStatsComponent.createMissing(listOf(File(dir, "perf_now"), File(dir, "cur_freq"))) - val binds = GpuStatsComponent(dir, tmp.root).binds() + val binds = GpuStatsComponent(dir, tmp.root, kgsl = true).binds() assertTrue(binds.contains(File(dir, "perf_now").path + ":" + GpuStatsComponent.MSM_LOAD)) assertTrue(binds.contains(File(dir, "cur_freq").path + ":" + GpuStatsComponent.MSM_CLOCK)) } diff --git a/app/src/test/java/com/droiddeck/launcher/session/GpuStatsComponentWithoutKgslTest.kt b/app/src/test/java/com/droiddeck/launcher/session/GpuStatsComponentWithoutKgslTest.kt new file mode 100644 index 0000000000..3128e54b1a --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/GpuStatsComponentWithoutKgslTest.kt @@ -0,0 +1,57 @@ +package com.droiddeck.launcher.session + +import com.droiddeck.launcher.hud.HudMetrics +import java.io.File +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class GpuStatsComponentWithoutKgslTest { + @get:Rule val tmp = TemporaryFolder() + private val parts = ArrayList() + + private fun component(nodes: List, metrics: HudMetrics? = null) = + GpuStatsComponent(tmp.newFolder("kgsl-3d0"), tmp.root, metrics, kgsl = false, loadNodes = { nodes }).also { parts += it } + + @After fun reset() { + parts.forEach { it.stop() } + KgslBusy.status = "not tried" + KgslClock.status = "not tried" + } + + @Test fun noReadableLoadNodeWithoutKgslTurnsTheTimingFeedOn() { + assertTrue(GpuStatsComponent.noLoadNode(false) { emptyList() }) + assertFalse(GpuStatsComponent.noLoadNode(false) { listOf(MALI_NODE) }) + } + + @Test fun kgslDevicesIgnoreOtherGpusNodes() { + val decided = GpuStatsComponent.noLoadNode(true) { throw AssertionError("kgsl devices read other GPUs' nodes") } + assertEquals(GpuStatsComponent.loadBlocked(), decided) + } + + @Test fun withoutAnyLoadNodeTheHudLoadComesFromVulkanTiming() { + val gpu = component(emptyList()) + assertTrue(gpu.prepare()) + assertTrue(GpuStatsComponent.sources, GpuStatsComponent.sources.startsWith("gpu_busy_percentage Vulkan timing")) + assertTrue(File(gpu.dir, "perf_now").isFile) + assertEquals(emptyList(), gpu.binds()) + assertEquals("", GpuStatsComponent.readerStatus()) + } + + @Test fun aReadableLoadNodeFeedsTheHudInsteadOfTheLayer() { + val gpu = component(listOf(MALI_NODE), HudMetrics(null)) + assertTrue(gpu.prepare()) + assertTrue(GpuStatsComponent.sources, GpuStatsComponent.sources.startsWith("gpu_busy_percentage $MALI_NODE")) + assertTrue(File(gpu.dir, "perf_now").isFile) + assertFalse(File(GpuStatsComponent.guestLoadDir(tmp.root), "on").exists()) + assertEquals("", GpuStatsComponent.readerStatus()) + } + + private companion object { + const val MALI_NODE = "/sys/class/misc/mali0/device/utilisation" + } +}