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

@@ -213,6 +213,7 @@ class DroidDeckHudHost(context: Context) {
"CPU ${mango.cpuPercent}% ${mango.cpuTempC}°C, GPU ${mango.gpuPercent}% ${mango.gpuClockMhz}MHz ${mango.gpuTempC}°C, " +
"RAM ${mango.ramUsedText()}/${mango.ramTotalText()}, VRAM ${mango.vramText()}, battery ${mango.battery.percent}% " +
"${mango.battery.watts}W charging=${mango.battery.charging}\n" +
"GPU feeds: ${com.droiddeck.launcher.session.GpuStatsComponent.sources.ifEmpty { "none (no session, or kgsl absent)" }}\n" +
"CPU temperature zones: ${LinuxRuntime.cpuTempZones().joinToString { it.name }}\n" +
"Frames: ${if (feed.engine.isNotEmpty() || feed.pid > 0) "gamescope (pid ${feed.pid}, ${feed.engine})" else "compositor"}, " +
"fps ${feed.stats.fps}\n")
@@ -7,10 +7,16 @@ import com.droiddeck.launcher.runtime.LinuxRuntime
import java.io.File
import java.io.RandomAccessFile
class GpuStatsComponent(val dir: File) : SessionPart() {
class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() {
@Volatile private var running = false
private var thread: Thread? = null
private val feeds = ArrayList<Feed>()
private var layerLoad: LayerLoad? = null
private var memstore: KgslBusy.Reader? = null
private var memstoreLoad: MemstoreLoad? = null
private var memstoreState: String? = null
private var cycleClock: KgslClock.Sampler? = null
@Volatile private var shown = false
private class Feed(
val file: File, val source: String, val read: () -> Long?, val format: String, val percent: Boolean = false,
@@ -24,9 +30,11 @@ class GpuStatsComponent(val dir: File) : SessionPart() {
}
fun prepare(): Boolean {
sources = ""
if (!File(KGSL).exists()) return false
val unreadable = MANGOHUD_FILES.any { File(KGSL, it).let { f -> f.exists() && !f.canRead() } }
if (!unreadable && !File(GPUBUSY).canRead()) return false
// 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
return try {
dir.mkdirs()
dir.listFiles()?.forEach { java.nio.file.Files.deleteIfExists(it.toPath()) }
@@ -34,19 +42,18 @@ class GpuStatsComponent(val dir: File) : SessionPart() {
PASSTHROUGH_FILES.forEach { name ->
runCatching { File(dir, name).writeText(File(KGSL, name).readText()) }
}
loadFeed()?.let { load ->
(loadFeed() ?: 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) }?.let { clock ->
(pick("clock_mhz", clockSources(), "%5d\n") { mhz(it) } ?: cycleClockFeed())?.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) }
refresh()
Log.i(TAG, "hud: kgsl stats from the session's own: " + MANGOHUD_FILES.joinToString { name ->
"$name " + (feeds.firstOrNull { it.file.name == name }?.source ?: "none")
})
describeSources()
Log.i(TAG, "hud: kgsl stats from the session's own: $sources")
true
} catch (e: Exception) {
Log.w(TAG, "hud: could not stand in for kgsl stats: $e")
@@ -68,17 +75,212 @@ class GpuStatsComponent(val dir: File) : SessionPart() {
// kgsl's gpubusy can read 0 0 straight after a read, so feeds that share a reader share one read.
private fun refresh() {
if (layerLoad != null || cycleClock != null || memstore != null) {
shown = hudShown()
memstore?.run(shown)
if (!shown) memstoreLoad?.pause()
layerLoad?.wanted(shown && memstoreLoad?.working != true)
}
val values = HashMap<() -> Long?, Long?>()
for (feed in feeds) {
if (feed.read !in values) values[feed.read] = feed.read()
values[feed.read]?.let { write(feed, it) }
}
memstoreLoad?.let { load ->
val state = when {
load.working -> "working"
load.failed -> "failed"
else -> null
}
if (state != memstoreState) {
memstoreState = state
if (load.failed) {
memstore?.close()
memstore = null
Log.i(TAG, "hud: kgsl memstore timestamps never moved; GPU load from Vulkan timing")
} else if (load.working) {
Log.i(TAG, "hud: GPU load from the kgsl memstore (all GPU work)")
}
describeSources()
}
}
}
private fun describeSources() {
sources = MANGOHUD_FILES.joinToString { name ->
val source = if (name == "gpu_busy_percentage" && memstoreLoad != null) memstoreSource()
else feeds.firstOrNull { it.file.name == name }?.source
"$name " + (source ?: "none")
}
}
private fun memstoreSource(): String = when {
memstoreLoad?.working == true -> "kgsl memstore ringbuffer timestamps (all GPU work)"
memstoreLoad?.failed == true -> LAYER_SOURCE
else -> "kgsl memstore once its timestamps move; until then $LAYER_SOURCE"
}
override fun stop() {
running = false
thread?.interrupt()
thread = null
memstore?.close()
memstore = null
memstoreLoad = null
memstoreState = null
layerLoad?.clear()
cycleClock?.close()
cycleClock = null
sources = ""
}
private fun hudShown(): Boolean = OverlayShown.check(root) || com.droiddeck.launcher.hud.DroidDeckHudHost.showing
// Android hides the load (kgsl sysfs blocked): the kgsl memstore gives the whole GPU's busy share. The in-game
// 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
val load = LayerLoad(guestLoadDir(root), extra = ::compositorBusyNs, onWanted = ::compositorTiming)
.also { it.clear(); layerLoad = it }
val sampled = KgslBusy.open()?.let { reader ->
memstore = reader
MemstoreLoad(reader::counts).also { memstoreLoad = it }
}
val read = read@{
if (!shown) return@read null
val whole = sampled?.percent()
if (sampled?.working == true) whole else load.percent()
}
return Feed(File(dir, "gpu_busy_percentage"), LAYER_SOURCE, read, "%3d %%\n")
}
// The clock nodes are blocked too: read the GPU's cycle counter from a small GPU job of our own.
private fun cycleClockFeed(): Feed? {
val sampler = KgslClock.open() ?: return null
cycleClock = sampler
// clock_mhz and cur_freq both read this; one GPU job per refresh serves both.
var at = 0L
var last: Long? = null
val read = {
val now = SystemClock.elapsedRealtime()
if (now - at > PERIOD_MS / 2) { at = now; last = if (shown) sampler.mhz()?.toLong() else null }
last
}
return Feed(File(dir, "clock_mhz"), "GPU cycle counter (own GPU job)", read, "%5d\n")
}
private fun compositorBusyNs(): Long? =
runCatching { com.droiddeck.launcher.wayland.WaylandCompositor.nativeGpuBusyNs() }.getOrNull()?.takeIf { it >= 0 }
private fun compositorTiming(on: Boolean) {
runCatching { com.droiddeck.launcher.wayland.WaylandCompositor.nativeSetGpuTiming(on) }
}
/**
* Busy time the layer (tools/gpuload) writes per process, plus [extra] (the compositor's own GPU time), as a share
* of wall time between two reads.
*/
internal class LayerLoad(
private val dir: File,
private val clock: () -> Long = System::nanoTime,
private val extra: () -> Long? = { null },
private val onWanted: (Boolean) -> Unit = {},
) {
private val last = HashMap<String, Long>()
private var lastAt = 0L
private var on = false
fun wanted(on: Boolean) {
val flag = File(dir, "on")
if (on && !flag.exists()) runCatching { dir.mkdirs(); flag.createNewFile() }
if (!on && flag.exists()) { flag.delete(); last.clear(); lastAt = 0L }
if (on != this.on) onWanted(on)
this.on = on
}
fun percent(): Long? {
val now = clock()
val files = dir.listFiles { f -> f.name.isNotEmpty() && f.name.all(Char::isDigit) }.orEmpty()
var busy = 0L
val seen = HashSet<String>()
for (file in files) {
val total = runCatching { file.readText().trim().substringBefore(' ').toLong() }.getOrNull() ?: continue
seen += file.name
last[file.name]?.let { if (total >= it) busy += total - it }
last[file.name] = total
if (System.currentTimeMillis() - file.lastModified() > STALE_MS) file.delete()
}
extra()?.let { total ->
seen += EXTRA
last[EXTRA]?.let { if (total >= it) busy += total - it }
last[EXTRA] = total
}
last.keys.retainAll(seen)
val wall = now - lastAt
val first = lastAt == 0L
lastAt = now
if (first || wall <= 0) return null
return (busy * 100 / wall).coerceIn(0, 100)
}
fun clear() {
dir.listFiles()?.forEach { it.delete() }
last.clear()
lastAt = 0L
if (on) onWanted(false)
on = false
}
private companion object {
const val EXTRA = "compositor"
}
}
/**
* Busy share over the last [WINDOW] reads from [KgslBusy] running totals (busy samples, samples, samples with
* new ringbuffer work). Not [working] until new work has been seen; [failed] once [GIVE_UP_SAMPLES] samples
* passed without any.
*/
internal class MemstoreLoad(private val counts: () -> LongArray?) {
var working = false
private set
var failed = false
private set
private val window = ArrayDeque<LongArray>()
private var waited = 0L
fun percent(): Long? {
if (failed) return null
val now = counts() ?: return null
val previous = window.lastOrNull()
window.addLast(now)
while (window.size > WINDOW + 1) window.removeFirst()
if (previous == null) return null
val fresh = now[1] - previous[1]
if (fresh <= 0) return null
if (!working) {
if (now[2] > previous[2]) {
working = true
} else {
waited += fresh
if (waited >= GIVE_UP_SAMPLES) failed = true
return null
}
}
val base = window.first()
return ((now[0] - base[0]) * 100 / (now[1] - base[1])).coerceIn(0, 100)
}
/** The totals stop while sampling is off; the next reading starts a new window. */
fun pause() {
window.clear()
}
companion object {
const val GIVE_UP_SAMPLES = 5_000L
const val WINDOW = 2
}
}
private fun loadFeed(): Feed? {
@@ -134,6 +336,24 @@ class GpuStatsComponent(val dir: File) : SessionPart() {
private const val LOAD_HOLD_MS = 5_000L
private val MANGOHUD_FILES = listOf("gpu_busy_percentage", "clock_mhz", "temp")
private val PASSTHROUGH_FILES = listOf("gpu_model", "gpu_chipid")
const val GUEST_LOAD_DIR = "/run/droiddeck-hud/gpu-load"
private const val LAYER_SOURCE = "Vulkan timing: games ($GUEST_LOAD_DIR) + DroidDeck compositor"
/** Where the running session's GPU feeds come from, for the HUD diagnostics export. */
@Volatile var sources = ""
private const val STALE_MS = 30_000L
fun guestLoadDir(root: File) = File(root, GUEST_LOAD_DIR.removePrefix("/"))
/** 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() }
/** Turns on the in-game layer (tools/gpuload) where the load is blocked; empty elsewhere. */
fun guestEnv(root: File): List<String> {
if (!loadBlocked()) return emptyList()
guestLoadDir(root).mkdirs()
return listOf("DROIDDECK_GPU_LOAD=1", "DROIDDECK_GPU_LOAD_DIR=$GUEST_LOAD_DIR")
}
private const val QCOM_GPU = "/sys/kernel/gpu"
private val LOAD_SOURCES = listOf(
"$KGSL/gpu_busy_percentage", "$KGSL/devfreq/gpu_load", "$KGSL/gpuload", "$QCOM_GPU/gpu_busy",
@@ -0,0 +1,50 @@
package com.droiddeck.launcher.session
import android.util.Log
/**
* Whole-GPU busy share for Adreno devices whose kgsl load nodes are SELinux-blocked for apps
* (app/src/main/cpp/gpuclock/kgsl_busy.c). It covers every process's GPU work, Android's own composition included.
*/
object KgslBusy {
private const val TAG = "SessionService"
@JvmStatic private external fun nativeOpen(error: IntArray): Long
@JvmStatic private external fun nativeRun(handle: Long, on: Boolean)
@JvmStatic private external fun nativeRead(handle: Long, out: LongArray): Boolean
@JvmStatic private external fun nativeClose(handle: Long)
private val loaded: Boolean by lazy {
runCatching { System.loadLibrary("gpuclock") }.onFailure { Log.w(TAG, "hud: gpuclock library: $it") }.isSuccess
}
class Reader internal constructor(private var handle: Long) : AutoCloseable {
/** Samples once a millisecond while on. */
@Synchronized fun run(on: Boolean) {
if (handle != 0L) nativeRun(handle, on)
}
/** Running totals: busy samples, samples, samples in which a ringbuffer started new work. */
@Synchronized fun counts(): LongArray? {
if (handle == 0L) return null
val out = LongArray(3)
return if (nativeRead(handle, out)) out else null
}
@Synchronized override fun close() {
if (handle != 0L) nativeClose(handle)
handle = 0L
}
}
fun open(): Reader? {
if (!loaded) return null
val error = IntArray(1)
val handle = runCatching { nativeOpen(error) }.getOrDefault(0L)
if (handle == 0L) {
Log.i(TAG, "hud: no GPU load from the kgsl memstore (open error ${error[0]})")
return null
}
return Reader(handle)
}
}
@@ -0,0 +1,50 @@
package com.droiddeck.launcher.session
import android.util.Log
/**
* GPU clock from the GPU's own cycle counter, for Adreno devices whose kgsl clock nodes are SELinux-blocked for
* apps (app/src/main/cpp/gpuclock/kgsl_clock.c). Each [mhz] call runs one tiny GPU job, so callers sample only
* while a HUD is shown.
*/
object KgslClock {
private const val TAG = "SessionService"
@JvmStatic private external fun nativeOpen(error: IntArray): Long
@JvmStatic private external fun nativeSample(handle: Long): Int
@JvmStatic private external fun nativeClose(handle: Long)
private val loaded: Boolean by lazy {
runCatching { System.loadLibrary("gpuclock") }.onFailure { Log.w(TAG, "hud: gpuclock library: $it") }.isSuccess
}
class Sampler internal constructor(private var handle: Long) : AutoCloseable {
/** MHz, or null when this sample was refused or failed its checks. */
@Synchronized fun mhz(): Int? = if (handle == 0L) null else nativeSample(handle).takeIf { it > 0 }
@Synchronized override fun close() {
if (handle != 0L) nativeClose(handle)
handle = 0L
}
}
/** A sampler that already produced one good reading, or null (the reason goes to the log). */
fun open(): Sampler? {
if (!loaded) return null
val error = IntArray(1)
val handle = runCatching { nativeOpen(error) }.getOrDefault(0L)
if (handle == 0L) {
Log.i(TAG, "hud: no GPU clock from the cycle counter (open error ${error[0]})")
return null
}
val sampler = Sampler(handle)
val first = (0 until 3).firstNotNullOfOrNull { sampler.mhz() }
if (first == null) {
Log.i(TAG, "hud: GPU cycle counter gave no usable reading")
sampler.close()
return null
}
Log.i(TAG, "hud: GPU clock from the cycle counter ($first MHz now)")
return sampler
}
}
@@ -160,6 +160,11 @@ object SessionFiles {
) + handModels).map { "usr/local/lib/droiddeck-vr/$it" to "usr/local/lib/droiddeck-vr/$it" } +
listOf("usr/local/lib/droiddeck-vr/layer/VK_LAYER_DROIDDECK_vr_implicit.json" to
"usr/share/vulkan/implicit_layer.d/droiddeck_vr.json")
// HUD GPU load where Android hides it (GpuStatsComponent.guestEnv turns the layer on).
val gpuLoad = listOf(
"usr/local/lib/droiddeck-gpuload/libVkLayer_droiddeck_gpuload.so" to "usr/local/lib/droiddeck-gpuload/libVkLayer_droiddeck_gpuload.so",
"usr/local/lib/droiddeck-gpuload/VK_LAYER_DROIDDECK_gpu_load.json" to "usr/share/vulkan/implicit_layer.d/droiddeck_gpu_load.json",
)
val fexPreloads = listOf("x86_64", "i386").flatMap { arch ->
listOf("libblsession.so", "libfakeinput.so", "libfaultreport.so").map { "$arch/$it" to "usr/local/lib/droiddeck-fex/$arch/$it" }
} + listOf("libvulkan-thunk.so", "libwebkitinput.so").map { "x86_64/$it" to "usr/local/lib/droiddeck-fex/x86_64/$it" }
@@ -171,7 +176,7 @@ object SessionFiles {
"usr/local/lib/droiddeck-pipewire.tzst" to "usr/local/lib/droiddeck-pipewire.tzst",
"usr/local/share/licenses/uruntime/LICENSE" to "usr/local/share/licenses/uruntime/LICENSE",
"usr/local/lib/droiddeck-dirt/ws2_32.dll" to "usr/local/lib/droiddeck-dirt/ws2_32.dll",
) + mangoapp + msitools.map { it to it } + vr + fexPreloads).filter { (asset, _) ->
) + mangoapp + msitools.map { it to it } + vr + gpuLoad + fexPreloads).filter { (asset, _) ->
val dir = asset.substringBeforeLast('/')
runCatching { context.assets.list("linuxfs/$dir")?.contains(asset.substringAfterLast('/')) == true }.getOrDefault(false)
}
@@ -808,6 +808,7 @@ class SessionService : Service() {
if (SessionPrefs.glThread(this)) guest.add("mesa_glthread=true")
if (SessionPrefs.noGlError(this)) guest.add("MESA_NO_ERROR=1")
guest.add("MESA_DISK_CACHE_DATABASE=1")
guest.addAll(GpuStatsComponent.guestEnv(LinuxRuntime.rootDir(this)))
guest.add("GLIBC_TUNABLES=glibc.pthread.rseq=0:glibc.malloc.top_pad=16777216")
if (SessionState.mode == MODE_STEAM) guest.add("BL_MANGOAPP=" + (if (SessionPrefs.mangoapp(this)) "1" else "0"))
SessionState.steamClientArch = if (steamHere) SessionPrefs.steamClientArch(this) else null
@@ -994,7 +995,7 @@ class SessionService : Service() {
gpuMem.attach(this)
components.add(gpuMem)
}
val gpuStats = GpuStatsComponent(File(filesDir, "session/sys/kgsl-3d0"))
val gpuStats = GpuStatsComponent(File(filesDir, "session/sys/kgsl-3d0"), LinuxRuntime.rootDir(this))
if (gpuStats.prepare()) {
gpuStats.attach(this)
components.add(gpuStats)
@@ -148,6 +148,10 @@ public final class WaylandCompositor {
public static native void nativeStart(String xdgRuntimeDir);
/** Nanoseconds of the compositor's own GPU work while timing is on, or -1 when it is not timed. */
public static native long nativeGpuBusyNs();
public static native void nativeSetGpuTiming(boolean on);
public static native void nativeStartWithSurface(Surface surface, String xdgRuntimeDir,
String driverPath, String libraryName,
String nativeLibDir);