Compare commits

...
Author SHA1 Message Date
The412Banner 94c2d811c7 fix(hud): GPU load on GPUs without kgsl, such as Mali 2026-12-01 20:38:40 +00:00
5 changed files with 95 additions and 15 deletions

No files matched your search

@@ -108,7 +108,11 @@ public class HudMetrics {
} }
private List<String> discoverGpuUsagePaths() { private List<String> discoverGpuUsagePaths() {
if (gpuUsagePathsCache != null) return gpuUsagePathsCache; if (gpuUsagePathsCache == null) gpuUsagePathsCache = gpuUsagePaths();
return gpuUsagePathsCache;
}
public static List<String> gpuUsagePaths() {
LinkedHashSet<String> candidates = new LinkedHashSet<>(); LinkedHashSet<String> candidates = new LinkedHashSet<>();
for (String p : GPU_USAGE_STATIC_PATHS) { for (String p : GPU_USAGE_STATIC_PATHS) {
@@ -157,8 +161,7 @@ public class HudMetrics {
} }
} }
gpuUsagePathsCache = new ArrayList<>(candidates); return new ArrayList<>(candidates);
return gpuUsagePathsCache;
} }
private static final String[] GPU_USAGE_STATIC_PATHS = { private static final String[] GPU_USAGE_STATIC_PATHS = {
@@ -3,11 +3,18 @@ package com.droiddeck.launcher.session
import android.os.SystemClock import android.os.SystemClock
import android.util.Log import android.util.Log
import com.droiddeck.launcher.core.SessionPart import com.droiddeck.launcher.core.SessionPart
import com.droiddeck.launcher.hud.HudMetrics
import com.droiddeck.launcher.runtime.LinuxRuntime import com.droiddeck.launcher.runtime.LinuxRuntime
import java.io.File import java.io.File
import java.io.RandomAccessFile 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<String> = HudMetrics::gpuUsagePaths,
) : SessionPart() {
@Volatile private var running = false @Volatile private var running = false
private var thread: Thread? = null private var thread: Thread? = null
private val feeds = ArrayList<Feed>() private val feeds = ArrayList<Feed>()
@@ -23,6 +30,7 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() {
) )
fun binds(): List<String> { fun binds(): List<String> {
if (!kgsl) return emptyList()
val list = mutableListOf(dir.path + ":" + KGSL) val list = mutableListOf(dir.path + ":" + KGSL)
if (File(dir, "perf_now").exists()) list.add(File(dir, "perf_now").path + ":" + MSM_LOAD) 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) 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 = "" sources = ""
KgslBusy.status = "not tried" KgslBusy.status = "not tried"
KgslClock.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() } } 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. // 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 { return try {
dir.mkdirs() dir.mkdirs()
dir.listFiles()?.forEach { java.nio.file.Files.deleteIfExists(it.toPath()) } 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 -> PASSTHROUGH_FILES.forEach { name ->
runCatching { File(dir, name).writeText(File(KGSL, name).readText()) } 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(load)
feeds.add(Feed(File(dir, "perf_now"), load.source, load.read, "%-6s\n", percent = true)) 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(clock)
feeds.add(Feed(File(dir, "cur_freq"), clock.source, { clock.read()?.times(1_000_000) }, "%-10d\n")) 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() refresh()
createMissing(feeds.map { it.file }) createMissing(feeds.map { it.file })
describeSources() 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 true
} catch (e: Exception) { } catch (e: Exception) {
Log.w(TAG, "hud: could not stand in for kgsl stats: $e") 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 // 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. // for the whole session where they never move.
private fun blockedLoadFeed(): Feed? { private fun blockedLoadFeed(): Feed? {
if (!loadBlocked()) return null if (!noLoadNode(kgsl, loadNodes)) return null
val load = LayerLoad(guestLoadDir(root), extra = ::compositorBusyNs, onWanted = ::compositorTiming) val load = LayerLoad(guestLoadDir(root), extra = ::compositorBusyNs, onWanted = ::compositorTiming)
.also { it.clear(); layerLoad = it } .also { it.clear(); layerLoad = it }
val sampled = KgslBusy.open()?.let { reader -> val sampled = if (!kgsl) null else KgslBusy.open()?.let { reader ->
memstore = reader memstore = reader
MemstoreLoad(reader::counts).also { memstoreLoad = it } 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? { private fun loadFeed(): Feed? {
latchedBusyFeed()?.let { return it } latchedBusyFeed()?.let { return it }
return pick("gpu_busy_percentage", LOAD_SOURCES, "%3d %%\n") { it.coerceIn(0, 100) } 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). */ /** 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() } 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<String>): Boolean =
if (kgsl) loadBlocked() else loadNodes().isEmpty()
/** Turns on the in-game layer (tools/gpuload) where the load is blocked; empty elsewhere. */ /** Turns on the in-game layer (tools/gpuload) where the load is blocked; empty elsewhere. */
fun guestEnv(root: File): List<String> { fun guestEnv(root: File): List<String> {
if (!loadBlocked()) return emptyList() if (!noLoadNode()) return emptyList()
guestLoadDir(root).mkdirs() guestLoadDir(root).mkdirs()
return listOf("DROIDDECK_GPU_LOAD=1", "DROIDDECK_GPU_LOAD_DIR=$GUEST_LOAD_DIR") return listOf("DROIDDECK_GPU_LOAD=1", "DROIDDECK_GPU_LOAD_DIR=$GUEST_LOAD_DIR")
} }
@@ -4,6 +4,7 @@ import com.droiddeck.launcher.core.AddonPack
import com.droiddeck.launcher.gpu.LinuxVulkanDriver import com.droiddeck.launcher.gpu.LinuxVulkanDriver
import com.droiddeck.launcher.gpu.LinuxVulkanDriverManager import com.droiddeck.launcher.gpu.LinuxVulkanDriverManager
import com.droiddeck.launcher.gpu.SystemVulkanDriver import com.droiddeck.launcher.gpu.SystemVulkanDriver
import com.droiddeck.launcher.hud.HudMetrics
import android.app.Notification import android.app.Notification
import android.app.NotificationChannel import android.app.NotificationChannel
@@ -1293,7 +1294,7 @@ class SessionService : Service() {
gpuMem.attach(this) gpuMem.attach(this)
components.add(gpuMem) 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()) { if (gpuStats.prepare()) {
gpuStats.attach(this) gpuStats.attach(this)
components.add(gpuStats) components.add(gpuStats)
@@ -13,7 +13,7 @@ class GpuStatsBindsTest {
@Test fun feedsWithNoValueYetAreStillBound() { @Test fun feedsWithNoValueYetAreStillBound() {
val dir = tmp.newFolder("kgsl-3d0") val dir = tmp.newFolder("kgsl-3d0")
GpuStatsComponent.createMissing(listOf(File(dir, "perf_now"), File(dir, "cur_freq"))) 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, "perf_now").path + ":" + GpuStatsComponent.MSM_LOAD))
assertTrue(binds.contains(File(dir, "cur_freq").path + ":" + GpuStatsComponent.MSM_CLOCK)) assertTrue(binds.contains(File(dir, "cur_freq").path + ":" + GpuStatsComponent.MSM_CLOCK))
} }
@@ -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<GpuStatsComponent>()
private fun component(nodes: List<String>, 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<String>(), 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"
}
}