Skip to content

Instantly share code, notes, and snippets.

@kota1921
Created August 19, 2021 12:48
Show Gist options
  • Save kota1921/969b5cb10ca2419f6c2e4630fffcf6a3 to your computer and use it in GitHub Desktop.
Save kota1921/969b5cb10ca2419f6c2e4630fffcf6a3 to your computer and use it in GitHub Desktop.
private fun measureWorkNs(): Long {
// Access a non-trivial amount of data to try and 'reset' any cache state.
// Have observed this to give more consistent performance when clocks are unlocked.
copySomeData()
val state = BenchmarkState()
state.performThrottleChecks = false
val input = FloatArray(16) { System.nanoTime().toFloat() }
val output = FloatArray(16)
while (state.keepRunningInline()) {
// Benchmark a simple thermal
Matrix.translateM(output, 0, input, 0, 1F, 2F, 3F)
}
return state.stats.min
}
/**
* Called to calculate throttling baseline, will be ignored after first call.
*/
fun computeThrottleBaseline() {
if (initNs == 0L) {
initNs = measureWorkNs()
}
}
/**
* Makes a guess as to whether the device is currently thermal throttled based on performance
* of single-threaded CPU work.
*/
fun isDeviceThermalThrottled(): Boolean {
if (initNs == 0L) {
// not initialized, so assume not throttled.
return false
}
val workNs = measureWorkNs()
return workNs > initNs * 1.10
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment