Compare commits

...
Author SHA1 Message Date
The412Banner d811d87b7a fix(session): stop clock and memory sampling while the session is suspended
A suspended session froze the guest, but the app kept sampling clocks every
2 s and memory every 10 s. On ROMs that refuse some of the clock files, every
clock tick also raised five SELinux denials, which filled most of app.log.
Both loggers now skip their samples while the session is suspended and pick
up again after resume.
2026-11-29 13:42:33 +00:00
4 changed files with 86 additions and 20 deletions

No files matched your search

@@ -8,13 +8,17 @@ import android.os.Build
import android.os.PowerManager
import android.util.Log
import com.droiddeck.launcher.core.SessionPart
import com.droiddeck.launcher.session.SessionState
import java.io.File
import java.io.FileWriter
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
class ClockLogComponent(private val logFile: File) : SessionPart() {
class ClockLogComponent(
private val logFile: File,
private val paused: () -> Boolean = { SessionState.suspended },
) : SessionPart() {
@Volatile private var running = false
private var stopped = false
private var thread: Thread? = null
@@ -55,26 +59,28 @@ class ClockLogComponent(private val logFile: File) : SessionPart() {
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()))
for (p in policies) {
val cur = firstNumber(File(p, "scaling_cur_freq").path)
val max = firstNumber(File(p, "scaling_max_freq").path)
val hw = firstNumber(File(p, "cpuinfo_max_freq").path)
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)})")
}
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) {
val h = power.getThermalHeadroom(10)
if (!h.isNaN()) line.append(String.format(Locale.US, " headroom %.2f", h))
if (!paused()) {
val line = StringBuilder(time.format(Date()))
for (p in policies) {
val cur = firstNumber(File(p, "scaling_cur_freq").path)
val max = firstNumber(File(p, "scaling_max_freq").path)
val hw = firstNumber(File(p, "cpuinfo_max_freq").path)
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)})")
}
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) {
val h = power.getThermalHeadroom(10)
if (!h.isNaN()) line.append(String.format(Locale.US, " headroom %.2f", h))
}
}
batteryTenths()?.let { line.append(String.format(Locale.US, " | battery %.1f C", it / 10.0)) }
w.write(line.append('\n').toString())
w.flush()
}
batteryTenths()?.let { line.append(String.format(Locale.US, " | battery %.1f C", it / 10.0)) }
w.write(line.append('\n').toString())
w.flush()
try { Thread.sleep(PERIOD_MS) } catch (e: InterruptedException) { break }
}
}
@@ -104,7 +104,9 @@ object MemoryLog {
if (thread != null) return
thread = Thread({
while (!Thread.currentThread().isInterrupted) {
runCatching { MemoryReadout.device()?.let { android.util.Log.i(TAG, line(it, MemoryReadout.split())) } }
if (!SessionState.suspended) {
runCatching { MemoryReadout.device()?.let { android.util.Log.i(TAG, line(it, MemoryReadout.split())) } }
}
try { Thread.sleep(PERIOD_MS) } catch (e: InterruptedException) { break }
}
}, "memory-log").apply { isDaemon = true; start() }
@@ -4,6 +4,7 @@ import android.content.Intent
import android.os.BatteryManager
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Rule
@@ -13,6 +14,7 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.util.concurrent.atomic.AtomicBoolean
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
@@ -36,6 +38,23 @@ class ClockLogComponentTest {
assertTrue(log.readText(), log.readText().contains("| battery 42.3 C"))
}
@Test fun aSuspendedSessionTakesNoSamplesUntilItResumes() {
val context = RuntimeEnvironment.getApplication()
context.sendStickyBroadcast(Intent(Intent.ACTION_BATTERY_CHANGED).putExtra(BatteryManager.EXTRA_TEMPERATURE, 412))
val log = tmp.newFile("suspended-clocks.log")
val suspended = AtomicBoolean(true)
clocks = ClockLogComponent(log) { suspended.get() }.also { it.attach(context); it.start() }
val headerDeadline = System.currentTimeMillis() + 5_000
while (!log.readText().contains("gpu floor:") && System.currentTimeMillis() < headerDeadline) Thread.sleep(20)
assertTrue(log.readText(), log.readText().contains("gpu floor:"))
Thread.sleep(500)
assertFalse(log.readText(), log.readText().contains("| battery"))
suspended.set(false)
val resumedDeadline = System.currentTimeMillis() + 5_000
while (!log.readText().contains("| battery 41.2 C") && System.currentTimeMillis() < resumedDeadline) Thread.sleep(20)
assertTrue(log.readText(), log.readText().contains("| battery 41.2 C"))
}
@Test fun stoppedComponentCannotStartFromAnEarlierSnapshot() {
val context = RuntimeEnvironment.getApplication()
val log = tmp.newFile("stopped-clocks.log")
@@ -0,0 +1,39 @@
package com.droiddeck.launcher.session
import org.junit.After
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowLog
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class MemoryLogTest {
@Before fun clear() {
MemoryLog.stop()
ShadowLog.clear()
}
@After fun reset() {
MemoryLog.stop()
SessionState.suspended = false
}
@Test fun aRunningSessionLogsMemory() {
SessionState.suspended = false
MemoryLog.start()
val deadline = System.currentTimeMillis() + 5_000
while (ShadowLog.getLogsForTag("Memory").isEmpty() && System.currentTimeMillis() < deadline) Thread.sleep(20)
assertTrue(ShadowLog.getLogsForTag("Memory").isNotEmpty())
}
@Test fun aSuspendedSessionLogsNoMemory() {
SessionState.suspended = true
MemoryLog.start()
Thread.sleep(1_500)
assertTrue(ShadowLog.getLogsForTag("Memory").map { it.msg }.toString(), ShadowLog.getLogsForTag("Memory").isEmpty())
}
}