feat(hud): GPU load and clock without root where Android blocks kgsl (#1969)

On some Adreno devices (the Retroid Pocket Duo) SELinux hides kgsl's sysfs
nodes from apps, so the HUD showed no GPU % and no GPU clock. Devices whose
nodes are readable keep using them; nothing changes there or on GPUs
without kgsl.

Where the nodes are blocked:

- GPU %: a native reader maps kgsl's memstore read-only (any kgsl client
  may) and samples the ringbuffer start/end timestamps once a millisecond
  while a HUD is shown. That covers all GPU work, Android's own
  composition included.
- Backup for GPU %: an implicit Vulkan layer in the guest
  (tools/gpuload) and the compositor time their own GPU work with
  timestamp queries. It runs until the memstore stamps are seen moving,
  and for the whole session if they never move.
- GPU clock: a small GPU job of our own reads the always-on and RBBM
  cycle counters (libgpuclock).

The HUD diagnostics export lists which source each GPU value came from.
tools/gpuload/build.sh takes the Vulkan headers from build_local.sh's NDK
first and keeps NDK paths with spaces intact.
This commit is contained in:
The412Banner authored and GitHub committed 2026-11-01 20:22:51 +00:00
1 parent 6cd844ea51
commit 8cb5c2168e
30 files changed
+1891 -10

No files matched your search

@@ -0,0 +1,114 @@
package com.droiddeck.launcher.session
import java.io.File
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class GpuLayerLoadTest {
@get:Rule val tmp = TemporaryFolder()
private var now = 0L
private fun load(dir: File) = GpuStatsComponent.LayerLoad(dir, clock = { now })
private fun write(dir: File, pid: Int, busyNs: Long) = File(dir, "$pid").writeText("%20d %-32s\n".format(busyNs, "game"))
@Test fun busyTimeOfAllProcessesOverWallTime() {
val dir = tmp.newFolder("gpu-load")
val load = load(dir)
write(dir, 100, 1_000_000_000L)
write(dir, 200, 5_000_000_000L)
now = 10_000_000_000L
assertNull(load.percent())
write(dir, 100, 1_300_000_000L)
write(dir, 200, 5_100_000_000L)
now += 1_000_000_000L
assertEquals(40L, load.percent())
}
@Test fun aNewProcessStartsFromItsOwnTotalAndAGoneOneStopsCounting() {
val dir = tmp.newFolder("gpu-load")
val load = load(dir)
write(dir, 100, 2_000_000_000L)
now = 1L
load.percent()
File(dir, "100").delete()
write(dir, 300, 9_000_000_000L)
now += 500_000_000L
assertEquals(0L, load.percent())
write(dir, 300, 9_250_000_000L)
now += 500_000_000L
assertEquals(50L, load.percent())
}
@Test fun capsAtFullAndIgnoresOtherFiles() {
val dir = tmp.newFolder("gpu-load")
val load = load(dir)
File(dir, "on").createNewFile()
write(dir, 1, 0L)
write(dir, 2, 0L)
now = 1L
load.percent()
write(dir, 1, 900_000_000L)
write(dir, 2, 900_000_000L)
now += 1_000_000_000L
assertEquals(100L, load.percent())
}
@Test fun theOnFlagFollowsTheHudAndHidingDropsTheBaseline() {
val dir = tmp.newFolder("gpu-load")
val load = load(dir)
load.wanted(true)
assertTrue(File(dir, "on").exists())
write(dir, 1, 100L)
now = 1L
load.percent()
load.wanted(false)
assertFalse(File(dir, "on").exists())
load.wanted(true)
now += 1_000L
assertNull(load.percent())
}
@Test fun theCompositorsOwnGpuTimeAddsToTheGames() {
val dir = tmp.newFolder("gpu-load")
var compositor: Long? = 7_000_000_000L
val load = GpuStatsComponent.LayerLoad(dir, clock = { now }, extra = { compositor })
write(dir, 100, 1_000_000_000L)
now = 1L
assertNull(load.percent())
write(dir, 100, 1_600_000_000L)
compositor = 7_150_000_000L
now += 1_000_000_000L
assertEquals(75L, load.percent())
compositor = null
write(dir, 100, 1_900_000_000L)
now += 1_000_000_000L
assertEquals(30L, load.percent())
}
@Test fun onlyTheCompositorCountsWhenNoGameReportsYet() {
val dir = File(tmp.root, "never-created")
var compositor = 0L
val load = GpuStatsComponent.LayerLoad(dir, clock = { now }, extra = { compositor })
now = 1L
load.percent()
compositor = 200_000_000L
now += 1_000_000_000L
assertEquals(20L, load.percent())
}
@Test fun compositorTimingFollowsTheHudOnlyOnChanges() {
val dir = tmp.newFolder("gpu-load")
val calls = mutableListOf<Boolean>()
val load = GpuStatsComponent.LayerLoad(dir, clock = { now }, onWanted = { calls += it })
load.wanted(true)
load.wanted(true)
load.wanted(false)
load.wanted(true)
load.clear()
assertEquals(listOf(true, false, true, false), calls)
}
}
@@ -0,0 +1,74 @@
package com.droiddeck.launcher.session
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
class GpuMemstoreLoadTest {
private var busy = 0L
private var samples = 0L
private var moves = 0L
private var available = true
private val load = GpuStatsComponent.MemstoreLoad { if (available) longArrayOf(busy, samples, moves) else null }
private fun tick(newSamples: Long, newBusy: Long, newMoves: Long = 1) {
samples += newSamples
busy += newBusy
moves += newMoves
}
@Test fun notTrustedUntilNewWorkIsSeen() {
assertNull(load.percent())
tick(500, 400, newMoves = 0)
assertNull(load.percent())
assertFalse(load.working)
tick(500, 300)
assertEquals(70L, load.percent())
assertTrue(load.working)
}
@Test fun givesUpWhenTheTimestampsNeverMove() {
load.percent()
repeat(10) {
tick(500, 250, newMoves = 0)
assertNull(load.percent())
}
assertTrue(load.failed)
tick(500, 250)
assertNull(load.percent())
assertFalse(load.working)
}
@Test fun averagesOverTheLastTwoReads() {
load.percent()
tick(500, 500)
assertEquals(100L, load.percent())
tick(500, 0)
assertEquals(50L, load.percent())
tick(500, 0, newMoves = 0)
assertEquals(0L, load.percent())
}
@Test fun pausingStartsANewWindow() {
load.percent()
tick(500, 500)
load.percent()
load.pause()
tick(0, 0, newMoves = 0)
assertNull(load.percent())
tick(500, 100)
assertEquals(20L, load.percent())
}
@Test fun noReadingWithoutNewSamplesOrCounts() {
load.percent()
tick(500, 100)
load.percent()
assertNull(load.percent())
available = false
tick(500, 100)
assertNull(load.percent())
}
}