mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
fix(hud): GPU clock and load without root on Adreno 8xx and hardware-scheduled kernels (#1981)
On phones where Android blocks the GPU's sysfs files for apps, the HUD
reads the GPU without root (#1969). On Adreno 8xx that showed no GPU
clock and only the partial load from Vulkan timing (OnePlus 13, Adreno
830, reported by iamTooShaar).
- GPU clock: the clock job used the Adreno 6xx/7xx registers. The
register layout is now picked from the KGSL chip id: on 8xx
CP_ALWAYS_ON_COUNTER is 0x8e7 and the RBBM cycle countable is 1.
Unknown later generations read nothing.
- GPU load: hardware-scheduled kgsl kernels (every 8xx, and some 7xx
such as the AYANEO Pocket FIT's) never write the memstore ringbuffer
timestamps. When those stay 0, the reader samples the contexts' own
memstore entries (every context that has submitted work, up to 64,
contexts with new or running work first).
- HUD diagnostics report: a "GPU readers without root" line with the
reason each reader works or fails, "none" instead of "null", and the
per-core CPU % source named honestly.
Tested: iamTooShaar's OnePlus 13 shows GPU clock and load with the PR
test build (06be2c5). Pocket FIT standalone build of the final readers
against the kernel's sysfs numbers: idle 1-2 % vs 3-4 %, GPU job loop
25-29 % vs 28-30 %, clock 1050 MHz vs 1050 MHz. The review fixes after
the test build were not tested on the OnePlus 13.
This commit is contained in:
1 parent
313b6a59d2
commit
55da0afd06
13 files changed
+299
-29
No files matched your search
@@ -193,11 +193,15 @@ class DroidDeckHudHost(context: Context) {
|
||||
metrics.snapshot()
|
||||
Thread.sleep(500)
|
||||
val mango = metrics.snapshot()
|
||||
fun shown(value: Any?, unit: String) = if (value == null) "none" else "$value$unit"
|
||||
val readers = com.droiddeck.launcher.session.GpuStatsComponent.readerStatus()
|
||||
file.writeText(metrics.also { it.setMangoReadings(null) }.buildDiagnosticsReport(context) + "\n== As the HUD shows them (MangoHud's sources) ==\n" +
|
||||
"CPU ${mango.cpuPercent}% ${mango.cpuTempC}°C, GPU ${mango.gpuPercent}% ${mango.gpuClockMhz}MHz ${mango.gpuTempC}°C, " +
|
||||
"CPU ${shown(mango.cpuPercent, "%")} ${shown(mango.cpuTempC, "°C")}, GPU ${shown(mango.gpuPercent, "%")} " +
|
||||
"${shown(mango.gpuClockMhz, "MHz")} ${shown(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" +
|
||||
(if (readers.isNotEmpty()) "GPU readers without root: $readers\n" else "") +
|
||||
"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")
|
||||
|
||||
@@ -870,6 +870,9 @@ public class HudMetrics {
|
||||
|
||||
private long[] lastCoreTotal = null;
|
||||
private long[] lastCoreIdle = null;
|
||||
private static final int PER_CORE_STAT = 1, PER_CORE_ESTIMATE = 2;
|
||||
/** Sources of the last per-core reading: PER_CORE_STAT and/or PER_CORE_ESTIMATE (clock speed); 0 when no core was read. */
|
||||
private volatile int perCoreSources = 0;
|
||||
|
||||
public int[] readPerCorePercent() {
|
||||
int cores = Runtime.getRuntime().availableProcessors();
|
||||
@@ -909,13 +912,20 @@ public class HudMetrics {
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
int sources = 0;
|
||||
for (int i = 0; i < out.length; i++) {
|
||||
if (out[i] >= 0) continue;
|
||||
if (out[i] >= 0) {
|
||||
sources |= PER_CORE_STAT;
|
||||
continue;
|
||||
}
|
||||
Long cur = readLongFromLine("/sys/devices/system/cpu/cpu" + i + "/cpufreq/scaling_cur_freq");
|
||||
Long max = readLongFromLine("/sys/devices/system/cpu/cpu" + i + "/cpufreq/cpuinfo_max_freq");
|
||||
if (cur != null && max != null && max > 0L)
|
||||
if (cur != null && max != null && max > 0L) {
|
||||
out[i] = clampPercent((int) ((Math.max(0L, Math.min(cur, max)) * 100L) / max));
|
||||
sources |= PER_CORE_ESTIMATE;
|
||||
}
|
||||
}
|
||||
perCoreSources = sources;
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -1008,10 +1018,13 @@ public class HudMetrics {
|
||||
int v = s.perCorePercent[i];
|
||||
cores.append('c').append(i).append('=').append(v < 0 ? "?" : (v + "%"));
|
||||
}
|
||||
if (s.perCorePercent.length > 0)
|
||||
metricHit(sb, "Per-core %", cores.toString(), "/proc/stat (per-cpu lines)");
|
||||
int coreSources = perCoreSources;
|
||||
if (s.perCorePercent.length > 0 && coreSources != 0)
|
||||
metricHit(sb, "Per-core %", cores.toString(), coreSources == PER_CORE_STAT ? "/proc/stat (per-cpu lines)"
|
||||
: coreSources == PER_CORE_ESTIMATE ? "scaling_cur_freq / cpuinfo_max_freq (clock estimate)"
|
||||
: "/proc/stat (per-cpu lines); scaling_cur_freq / cpuinfo_max_freq (clock estimate) for the other cores");
|
||||
else
|
||||
metricMiss(sb, "Per-core %", "/proc/stat");
|
||||
metricMiss(sb, "Per-core %", "/proc/stat", "/sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq");
|
||||
|
||||
StringBuilder coreClk = new StringBuilder();
|
||||
for (int i = 0; i < s.perCoreClockMhz.length; i++) {
|
||||
|
||||
@@ -31,6 +31,8 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() {
|
||||
|
||||
fun prepare(): Boolean {
|
||||
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.
|
||||
@@ -96,6 +98,7 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() {
|
||||
if (load.failed) {
|
||||
memstore?.close()
|
||||
memstore = null
|
||||
KgslBusy.status += ", but the stamps never moved"
|
||||
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)")
|
||||
@@ -114,7 +117,9 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() {
|
||||
}
|
||||
|
||||
private fun memstoreSource(): String = when {
|
||||
memstoreLoad?.working == true -> "kgsl memstore ringbuffer timestamps (all GPU work)"
|
||||
memstoreLoad?.working == true ->
|
||||
if (memstore?.mode == KgslBusy.MODE_CONTEXTS) "kgsl memstore per-context timestamps (all GPU work)"
|
||||
else "kgsl memstore ringbuffer timestamps (all GPU work)"
|
||||
memstoreLoad?.failed == true -> LAYER_SOURCE
|
||||
else -> "kgsl memstore once its timestamps move; until then $LAYER_SOURCE"
|
||||
}
|
||||
@@ -339,6 +344,12 @@ class GpuStatsComponent(val dir: File, private val root: File) : SessionPart() {
|
||||
|
||||
/** Where the running session's GPU feeds come from, for the HUD diagnostics export. */
|
||||
@Volatile var sources = ""
|
||||
|
||||
/** Why the no-root GPU readers work or not, for the HUD diagnostics export; empty where they were not needed. */
|
||||
fun readerStatus(): String =
|
||||
if (KgslBusy.status == "not tried" && KgslClock.status == "not tried") ""
|
||||
else "kgsl memstore ${KgslBusy.status}; GPU cycle counter ${KgslClock.status}"
|
||||
|
||||
private const val STALE_MS = 30_000L
|
||||
|
||||
fun guestLoadDir(root: File) = File(root, GUEST_LOAD_DIR.removePrefix("/"))
|
||||
|
||||
@@ -10,6 +10,7 @@ object KgslBusy {
|
||||
private const val TAG = "SessionService"
|
||||
|
||||
@JvmStatic private external fun nativeOpen(error: IntArray): Long
|
||||
@JvmStatic private external fun nativeMode(handle: Long): Int
|
||||
@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)
|
||||
@@ -24,7 +25,10 @@ object KgslBusy {
|
||||
if (handle != 0L) nativeRun(handle, on)
|
||||
}
|
||||
|
||||
/** Running totals: busy samples, samples, samples in which a ringbuffer started new work. */
|
||||
/** Which memstore entries are watched: [MODE_RINGBUFFER] or [MODE_CONTEXTS] (hardware-scheduled kernels). */
|
||||
val mode: Int = if (handle != 0L) runCatching { nativeMode(handle) }.getOrDefault(0) else 0
|
||||
|
||||
/** Running totals: busy samples, samples, samples in which a ringbuffer or context started new work. */
|
||||
@Synchronized fun counts(): LongArray? {
|
||||
if (handle == 0L) return null
|
||||
val out = LongArray(3)
|
||||
@@ -37,14 +41,27 @@ object KgslBusy {
|
||||
}
|
||||
}
|
||||
|
||||
const val MODE_RINGBUFFER = 1
|
||||
const val MODE_CONTEXTS = 2
|
||||
|
||||
/** What the last [open] found, for the HUD diagnostics export. */
|
||||
@Volatile var status = "not tried"
|
||||
|
||||
fun open(): Reader? {
|
||||
if (!loaded) return null
|
||||
if (!loaded) {
|
||||
status = "gpuclock library did not load"
|
||||
return null
|
||||
}
|
||||
val error = IntArray(1)
|
||||
val handle = runCatching { nativeOpen(error) }.getOrDefault(0L)
|
||||
if (handle == 0L) {
|
||||
status = "open failed: ${KgslErrors.describe(error[0])}"
|
||||
Log.i(TAG, "hud: no GPU load from the kgsl memstore (open error ${error[0]})")
|
||||
return null
|
||||
}
|
||||
return Reader(handle)
|
||||
val reader = Reader(handle)
|
||||
status = if (reader.mode == MODE_CONTEXTS) "per-context stamps (hardware-scheduled kernel)" else "ringbuffer stamps"
|
||||
Log.i(TAG, "hud: kgsl memstore reader uses $status")
|
||||
return reader
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,10 @@ object KgslClock {
|
||||
|
||||
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 fun mhz(): Int? = sample().takeIf { it > 0 }
|
||||
|
||||
/** MHz, or a negative native code: -errno from kgsl or [RANGE_CHECK_FAILED]. */
|
||||
@Synchronized fun sample(): Int = if (handle == 0L) CLOSED else nativeSample(handle)
|
||||
|
||||
@Synchronized override fun close() {
|
||||
if (handle != 0L) nativeClose(handle)
|
||||
@@ -28,22 +31,41 @@ object KgslClock {
|
||||
}
|
||||
}
|
||||
|
||||
/** A sampler that already produced one good reading, or null (the reason goes to the log). */
|
||||
/** -EBADF, what the native side also returns for a closed handle. */
|
||||
private const val CLOSED = -9
|
||||
|
||||
/** The native sample code for a reading that failed its range checks (kgsl_clock.c sampler_mhz). */
|
||||
const val RANGE_CHECK_FAILED = -1000
|
||||
|
||||
/** What the last [open] found, for the HUD diagnostics export. */
|
||||
@Volatile var status = "not tried"
|
||||
|
||||
/** A sampler that already produced one good reading, or null (the reason goes to the log and [status]). */
|
||||
fun open(): Sampler? {
|
||||
if (!loaded) return null
|
||||
if (!loaded) {
|
||||
status = "gpuclock library did not load"
|
||||
return null
|
||||
}
|
||||
val error = IntArray(1)
|
||||
val handle = runCatching { nativeOpen(error) }.getOrDefault(0L)
|
||||
if (handle == 0L) {
|
||||
status = "open failed: ${KgslErrors.describe(error[0])}"
|
||||
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() }
|
||||
var code = 0
|
||||
val first = (0 until 3).firstNotNullOfOrNull {
|
||||
code = sampler.sample()
|
||||
code.takeIf { it > 0 }
|
||||
}
|
||||
if (first == null) {
|
||||
Log.i(TAG, "hud: GPU cycle counter gave no usable reading")
|
||||
status = "no usable reading: ${KgslErrors.describe(code)}"
|
||||
Log.i(TAG, "hud: GPU cycle counter gave no usable reading ($code)")
|
||||
sampler.close()
|
||||
return null
|
||||
}
|
||||
status = "working ($first MHz at start)"
|
||||
Log.i(TAG, "hud: GPU clock from the cycle counter ($first MHz now)")
|
||||
return sampler
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
/** Readable text for the negative errno codes the gpuclock library returns. */
|
||||
internal object KgslErrors {
|
||||
fun describe(code: Int): String {
|
||||
if (code == 0) return "no error code"
|
||||
if (code == KgslClock.RANGE_CHECK_FAILED) return "sample out of range"
|
||||
val name = runCatching { android.system.OsConstants.errnoName(-code) }.getOrNull()
|
||||
return if (name != null) "$name ($code)" else "error $code"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user