Merge "ThermalManagerService: fix different system clock comparison"

This commit is contained in:
TreeHugger Robot
2021-10-18 14:34:39 +00:00
committed by Android (Google) Code Review
2 changed files with 36 additions and 3 deletions

View File

@@ -1088,6 +1088,10 @@ public class ThermalManagerService extends SystemService {
@GuardedBy("mSamples")
private long mLastForecastCallTimeMillis = 0;
private static final int INACTIVITY_THRESHOLD_MILLIS = 10000;
@VisibleForTesting
long mInactivityThresholdMillis = INACTIVITY_THRESHOLD_MILLIS;
void updateSevereThresholds() {
synchronized (mSamples) {
List<TemperatureThreshold> thresholds =
@@ -1107,13 +1111,12 @@ public class ThermalManagerService extends SystemService {
}
}
private static final int INACTIVITY_THRESHOLD_MILLIS = 10000;
private static final int RING_BUFFER_SIZE = 30;
private void updateTemperature() {
synchronized (mSamples) {
if (SystemClock.elapsedRealtime() - mLastForecastCallTimeMillis
< INACTIVITY_THRESHOLD_MILLIS) {
< mInactivityThresholdMillis) {
// Trigger this again after a second as long as forecast has been called more
// recently than the inactivity timeout
mHandler.postDelayed(this::updateTemperature, 1000);
@@ -1199,7 +1202,7 @@ public class ThermalManagerService extends SystemService {
float getForecast(int forecastSeconds) {
synchronized (mSamples) {
mLastForecastCallTimeMillis = System.currentTimeMillis();
mLastForecastCallTimeMillis = SystemClock.elapsedRealtime();
if (mSamples.isEmpty()) {
updateTemperature();
}

View File

@@ -17,6 +17,7 @@
package com.android.server.power;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -457,4 +458,33 @@ public class ThermalManagerServiceTest {
watcher.mSevereThresholds.erase();
assertTrue(Float.isNaN(watcher.getForecast(0)));
}
@Test
public void testTemperatureWatcherGetForecastUpdate() throws Exception {
ThermalManagerService.TemperatureWatcher watcher = mService.mTemperatureWatcher;
// Reduce the inactivity threshold to speed up testing
watcher.mInactivityThresholdMillis = 2000;
// Make sure mSamples is empty before updateTemperature
assertTrue(isWatcherSamplesEmpty(watcher));
// Call getForecast once to trigger updateTemperature
watcher.getForecast(0);
// After 1 second, the samples should be updated
Thread.sleep(1000);
assertFalse(isWatcherSamplesEmpty(watcher));
// After mInactivityThresholdMillis, the samples should be cleared
Thread.sleep(watcher.mInactivityThresholdMillis);
assertTrue(isWatcherSamplesEmpty(watcher));
}
// Helper function to hold mSamples lock, avoid GuardedBy lint errors
private boolean isWatcherSamplesEmpty(ThermalManagerService.TemperatureWatcher watcher) {
synchronized (watcher.mSamples) {
return watcher.mSamples.isEmpty();
}
}
}