diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 3249d0b3c7..618452d028 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -1471,7 +1471,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde SessionPrefs.setGpuClockPin(this@SessionActivity, on) gpuClockPin = on Thread { - GpuClockPin.sync(applicationContext) + synchronized(GpuClockPin) { GpuClockPin.sync(applicationContext)?.let { Log.i(TAG, it) } } runOnUiThread { gpuClockPin = gpuClockHeld() } }.start() }, diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/ClockLogComponent.kt b/app/src/main/java/com/droiddeck/launcher/gpu/ClockLogComponent.kt index 6f6827ad0a..9368bddcfe 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/ClockLogComponent.kt +++ b/app/src/main/java/com/droiddeck/launcher/gpu/ClockLogComponent.kt @@ -37,22 +37,22 @@ class ClockLogComponent(private val logFile: File) : SessionPart() { ?.sortedBy { it.name.removePrefix("policy").toIntOrNull() ?: 0 } ?: emptyList() val gpuCandidates = gpuSources() val gpu = gpuCandidates.firstOrNull { !it.startsWith("/proc/") && firstNumber(it) != null } + val stepSources = GpuLimits.stepSources() + val maxSources = GpuLimits.maxSources() + val minSources = GpuLimits.minSources() + val steps = GpuLimits.first(stepSources) + var ceiling = ceilingSource(maxSources) val power = app().getSystemService(Context.POWER_SERVICE) as? PowerManager val time = SimpleDateFormat("HH:mm:ss", Locale.US) try { FileWriter(logFile, true).use { w -> w.write("== clocks: ${Build.MANUFACTURER} ${Build.MODEL} (${Build.HARDWARE}), every ${PERIOD_MS / 1000}s\n") w.write("cpu policies: ${policies.joinToString { p -> p.name + " " + (File(p, "related_cpus").readTextOrNull()?.trim() ?: "?") }}\n") - for (source in gpuCandidates) { - val f = File(source) - val state = when { - !f.exists() -> continue - firstNumber(source) != null -> "readable" - else -> "refused" - } - w.write("gpu source $source: $state\n") - } + probeLines("gpu source", gpuCandidates).forEach { w.write(it + "\n") } w.write("gpu clock from: ${gpu ?: "nothing readable"}\n") + (probeLines("gpu limit", stepSources, ::firstStep) + probeLines("gpu limit", maxSources, ::limit) + + probeLines("gpu limit", minSources, ::firstStep)).distinct().forEach { w.write(it + "\n") } + limitLines(steps, ceiling, GpuLimits.first(minSources)).forEach { w.write(it + "\n") } w.flush() while (running && logFile.length() < MAX_BYTES) { val line = StringBuilder(time.format(Date())) @@ -63,7 +63,8 @@ class ClockLogComponent(private val logFile: File) : SessionPart() { line.append(" ${p.name.removePrefix("policy").let { "p$it" }} ${mhz(cur)}/${mhz(max)}") if (hw != null && max != null && max < hw) line.append("(of ${mhz(hw)})") } - gpu?.let { line.append(" | gpu ${mhz(firstNumber(it))} MHz") } + if (ceiling == null) ceiling = ceilingSource(maxSources)?.also { w.write("gpu ceiling from: $it\n") } + line.append(gpuField(gpu, ceiling, steps?.mhz?.last())) if (power != null && Build.VERSION.SDK_INT >= 29) { line.append(" | thermal ${power.currentThermalStatus}") if (Build.VERSION.SDK_INT >= 30) { @@ -109,16 +110,41 @@ class ClockLogComponent(private val logFile: File) : SessionPart() { return list.distinct() } + internal fun probeLines(label: String, sources: List, read: (String) -> Long? = ::firstNumber): List = sources.mapNotNull { source -> + when { + !File(source).exists() -> null + read(source) != null -> "$label $source: readable" + else -> "$label $source: refused" + } + } + + internal fun limitLines(steps: GpuLimits.Found?, ceiling: String?, floor: GpuLimits.Found?): List = listOf( + "gpu steps: " + (steps?.let { "${it.mhz.joinToString(" ")} MHz from ${it.path}" } ?: "nothing readable"), + "gpu ceiling from: " + (ceiling ?: "nothing readable"), + "gpu floor: " + (floor?.let { "${it.mhz.first()} MHz from ${it.path}" } ?: "nothing readable"), + ) + + internal fun gpuField(clock: String?, ceiling: String?, top: Long?): String { + if (clock == null && ceiling == null) return "" + val field = StringBuilder(" | gpu ").append(mhz(clock?.let(::firstNumber))) + ceiling?.let(::limit)?.let { max -> + field.append('/').append(mhz(max)) + if (top != null && GpuLimits.mhz(max) < top) field.append("(of $top)") + } + return field.append(" MHz").toString() + } + private fun File.readTextOrNull(): String? = runCatching { readText() }.getOrNull() private fun firstNumber(path: String): Long? = runCatching { Regex("\\d+").find(File(path).readText())?.value?.toLong() }.getOrNull() - private fun mhz(raw: Long?): String = when { - raw == null -> "?" - raw > 10_000_000 -> (raw / 1_000_000).toString() - raw > 10_000 -> (raw / 1_000).toString() - else -> raw.toString() - } + internal fun limit(path: String): Long? = firstNumber(path)?.takeIf { it > 0 } + + internal fun firstStep(path: String): Long? = GpuLimits.read(path).firstOrNull() + + internal fun ceilingSource(sources: List): String? = sources.firstOrNull { limit(it) != null } + + private fun mhz(raw: Long?): String = raw?.let { GpuLimits.mhz(it).toString() } ?: "?" } } diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/DeviceInfo.kt b/app/src/main/java/com/droiddeck/launcher/gpu/DeviceInfo.kt index bf44d7eec8..45a396b652 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/DeviceInfo.kt +++ b/app/src/main/java/com/droiddeck/launcher/gpu/DeviceInfo.kt @@ -87,20 +87,10 @@ object DeviceInfo { private fun ghz(khz: Long) = String.format(Locale.US, "%.2f GHz", khz / 1e6) private fun gpuRange(): String? { - fun mhz(hz: Long) = "${hz / 1_000_000} MHz" - val kgsl = read("/sys/class/kgsl/kgsl-3d0/gpu_available_frequencies") - ?.split(Regex("\\s+"))?.mapNotNull { it.toLongOrNull() }?.filter { it > 0 }.orEmpty() - if (kgsl.isNotEmpty()) return "${mhz(kgsl.min())}–${mhz(kgsl.max())} (${kgsl.size} steps)" - read("/sys/class/kgsl/kgsl-3d0/max_gpuclk")?.toLongOrNull()?.let { return "up to ${mhz(it)}" } - val node = File("/sys/class/devfreq").listFiles()?.firstOrNull { f -> - val n = f.name.lowercase() - (n.contains("gpu") || n.contains("mali") || n.contains("kgsl-3d")) && listOf("bus", "bw", "memlat").none { n.contains(it) } - } ?: return null - val steps = read("${node.path}/available_frequencies")?.split(Regex("\\s+"))?.mapNotNull { it.toLongOrNull() }?.filter { it > 0 }.orEmpty() - if (steps.isNotEmpty()) return "${mhz(steps.min())}–${mhz(steps.max())} (${steps.size} steps)" - val min = read("${node.path}/min_freq")?.toLongOrNull() - val max = read("${node.path}/max_freq")?.toLongOrNull() ?: return null - return (if (min != null) "${mhz(min)}–" else "up to ") + mhz(max) + GpuLimits.first(GpuLimits.stepSources())?.mhz?.let { return "${it.first()} MHz–${it.last()} MHz (${it.size} steps)" } + val max = GpuLimits.first(GpuLimits.maxSources())?.mhz?.last() ?: return null + val min = GpuLimits.first(GpuLimits.minSources())?.mhz?.first() + return (if (min != null) "$min MHz–" else "up to ") + "$max MHz" } private fun memory(context: Context) = Section("Memory", buildList> { diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/GpuLimits.kt b/app/src/main/java/com/droiddeck/launcher/gpu/GpuLimits.kt new file mode 100644 index 0000000000..67eaa23741 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/gpu/GpuLimits.kt @@ -0,0 +1,41 @@ +package com.droiddeck.launcher.gpu + +import java.io.File + +object GpuLimits { + class Found(val path: String, val mhz: List) + + private const val KGSL = "/sys/class/kgsl/kgsl-3d0" + private const val QCOM_GPU = "/sys/kernel/gpu" + + fun stepSources(): List = listOf( + "$KGSL/gpu_available_frequencies", "$KGSL/freq_table_mhz", "$KGSL/devfreq/available_frequencies", "$QCOM_GPU/gpu_freq_table", + ) + devfreq("available_frequencies") + + fun maxSources(): List = listOf( + "$KGSL/max_gpuclk", "$KGSL/max_clock_mhz", "$KGSL/devfreq/max_freq", "$QCOM_GPU/gpu_max_clock", + ) + devfreq("max_freq") + + fun minSources(): List = listOf( + "$KGSL/min_clock_mhz", "$KGSL/devfreq/min_freq", "$QCOM_GPU/gpu_min_clock", + ) + devfreq("min_freq") + + fun first(sources: List): Found? = + sources.firstNotNullOfOrNull { path -> read(path).takeIf { it.isNotEmpty() }?.let { Found(path, it) } } + + fun read(path: String): List = runCatching { + Regex("\\d+").findAll(File(path).readText()).map { mhz(it.value.toLong()) }.filter { it > 0 }.distinct().sorted().toList() + }.getOrDefault(emptyList()) + + fun mhz(raw: Long): Long = when { + raw > 10_000_000 -> raw / 1_000_000 + raw > 10_000 -> raw / 1_000 + else -> raw + } + + private fun devfreq(file: String): List = + File("/sys/class/devfreq").listFiles()?.filter { f -> + val n = f.name.lowercase() + (n.contains("gpu") || n.contains("mali") || n.contains("kgsl-3d")) && listOf("bus", "bw", "memlat").none { n.contains(it) } + }?.sortedBy { it.name }?.map { "${it.path}/$file" }.orEmpty() +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/GpuClockPin.kt b/app/src/main/java/com/droiddeck/launcher/session/GpuClockPin.kt index e136df2604..e02b3e7bfa 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/GpuClockPin.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/GpuClockPin.kt @@ -46,9 +46,10 @@ object GpuClockPin { } @Synchronized - fun sync(context: Context) { - if (!live) return - if (SessionPrefs.gpuClockPin(context)) hold(context) else if (pinned) clear(context) + fun sync(context: Context): String? { + if (!live) return null + if (!SessionPrefs.gpuClockPin(context) && pinned) clear(context) + return hold(context) } @Synchronized diff --git a/app/src/test/java/com/droiddeck/launcher/gpu/ClockLogComponentTest.kt b/app/src/test/java/com/droiddeck/launcher/gpu/ClockLogComponentTest.kt index 1dc1d6b6af..fed4553bf3 100644 --- a/app/src/test/java/com/droiddeck/launcher/gpu/ClockLogComponentTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/gpu/ClockLogComponentTest.kt @@ -3,6 +3,8 @@ package com.droiddeck.launcher.gpu import android.content.Intent import android.os.BatteryManager import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test @@ -40,4 +42,68 @@ class ClockLogComponentTest { clocks = ClockLogComponent(log).also { it.attach(context); it.stop(); it.start() } assertTrue(log.readText(), log.length() == 0L) } + @Test fun gpuStepsAndCeilingAreParsedAndPrinted() { + val table = tmp.newFile("gpu_available_frequencies").apply { writeText("443000000 350000000 285000000 160000000\n") } + val ceiling = tmp.newFile("max_gpuclk").apply { writeText("285000000\n") } + val floor = tmp.newFile("min_clock_mhz").apply { writeText("160\n") } + val clock = tmp.newFile("gpu_clock").apply { writeText("230\n") } + val steps = GpuLimits.first(listOf(tmp.root.path + "/missing", table.path))!! + assertEquals(listOf(160L, 285L, 350L, 443L), steps.mhz) + assertEquals( + listOf( + "gpu steps: 160 285 350 443 MHz from ${table.path}", + "gpu ceiling from: ${ceiling.path}", + "gpu floor: 160 MHz from ${floor.path}", + ), + ClockLogComponent.limitLines(steps, ceiling.path, GpuLimits.first(listOf(floor.path))), + ) + assertEquals(" | gpu 230/285(of 443) MHz", ClockLogComponent.gpuField(clock.path, ceiling.path, steps.mhz.last())) + ceiling.writeText("443000000\n") + assertEquals(" | gpu 230/443 MHz", ClockLogComponent.gpuField(clock.path, ceiling.path, steps.mhz.last())) + } + + @Test fun aZeroCeilingIsNotALimit() { + val clock = tmp.newFile("gpu_clock").apply { writeText("0\n") } + val zero = tmp.newFile("max_gpuclk").apply { writeText("0\n") } + assertEquals(" | gpu 0 MHz", ClockLogComponent.gpuField(clock.path, zero.path, 443)) + assertEquals(listOf("gpu limit ${zero.path}: refused"), ClockLogComponent.probeLines("gpu limit", listOf(zero.path), ClockLogComponent::limit)) + assertEquals(listOf("gpu source ${clock.path}: readable"), ClockLogComponent.probeLines("gpu source", listOf(clock.path))) + assertNull(ClockLogComponent.ceilingSource(listOf(zero.path))) + zero.writeText("285000000\n") + assertEquals(zero.path, ClockLogComponent.ceilingSource(listOf(zero.path))) + assertEquals(" | gpu 0/285(of 443) MHz", ClockLogComponent.gpuField(clock.path, zero.path, 443)) + } + + @Test fun aStepTableWithALeadingZeroIsReadable() { + val steps = tmp.newFile("freq_table_mhz").apply { writeText("0 160000000 285000000\n") } + assertEquals(listOf(160L, 285L), GpuLimits.first(listOf(steps.path))?.mhz) + assertEquals(listOf("gpu limit ${steps.path}: readable"), ClockLogComponent.probeLines("gpu limit", listOf(steps.path), ClockLogComponent::firstStep)) + } + + @Test fun unreadableOrMissingLimitsLeaveTheSampleUnchanged() { + val clock = tmp.newFile("clock_mhz").apply { writeText("230\n") } + val refused = tmp.newFolder("max_clock_mhz") + val missing = tmp.root.path + "/gpu_max_clock" + assertEquals(" | gpu 230 MHz", ClockLogComponent.gpuField(clock.path, null, null)) + assertEquals(" | gpu 230 MHz", ClockLogComponent.gpuField(clock.path, missing, 443)) + assertEquals(" | gpu 230 MHz", ClockLogComponent.gpuField(clock.path, refused.path, 443)) + assertEquals("", ClockLogComponent.gpuField(null, null, null)) + assertNull(GpuLimits.first(listOf(missing, refused.path))) + assertEquals(listOf("gpu limit ${refused.path}: refused"), ClockLogComponent.probeLines("gpu limit", listOf(missing, refused.path))) + assertEquals( + listOf("gpu steps: nothing readable", "gpu ceiling from: nothing readable", "gpu floor: nothing readable"), + ClockLogComponent.limitLines(null, null, null), + ) + } + + @Test fun aCeilingChangeMidSessionShowsInTheNextSample() { + val clock = tmp.newFile("cur_freq").apply { writeText("443000000\n") } + val ceiling = tmp.newFile("max_freq").apply { writeText("443000000\n") } + val first = ClockLogComponent.gpuField(clock.path, ceiling.path, 443) + ceiling.writeText("285000000\n") + clock.writeText("285000000\n") + val second = ClockLogComponent.gpuField(clock.path, ceiling.path, 443) + assertEquals(" | gpu 443/443 MHz", first) + assertEquals(" | gpu 285/285(of 443) MHz", second) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/session/GpuClockPinTest.kt b/app/src/test/java/com/droiddeck/launcher/session/GpuClockPinTest.kt new file mode 100644 index 0000000000..3311df0501 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/GpuClockPinTest.kt @@ -0,0 +1,34 @@ +package com.droiddeck.launcher.session + +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE) +class GpuClockPinTest { + private val context = RuntimeEnvironment.getApplication() + + @After fun stop() { GpuClockPin.stop(context) } + + @Test fun aToggleOutsideASessionSaysNothing() { + SessionPrefs.setGpuClockPin(context, true) + assertNull(GpuClockPin.sync(context)) + } + + @Test fun aToggleMidSessionReportsTheHoldWithTheStartWording() { + SessionPrefs.setGpuClockPin(context, false) + val started = GpuClockPin.start(context) + assertEquals("gpu clock: governed", started) + SessionPrefs.setGpuClockPin(context, true) + val held = GpuClockPin.sync(context) + assertEquals("gpu clock: no /dev/kgsl-3d0, left governed", held) + SessionPrefs.setGpuClockPin(context, false) + assertEquals(started, GpuClockPin.sync(context)) + } +}