diff options
author | Kai Shi <kaishi@google.com> | 2020-02-21 23:24:48 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-02-21 23:24:48 +0000 |
commit | 7f06922f0540a679df06baf9cc70008fd1b1fe48 (patch) | |
tree | bfda89a02011db174b3ccbd0883fbcb7011889fc | |
parent | e2cf2e5c7809366692256a44609a021aa7925573 (diff) | |
parent | 4e2eea441c312d3128ffcbc1464d209c37c749c6 (diff) |
Merge "Bug fix WifiHealthMonitor daily detection timer"
-rw-r--r-- | service/java/com/android/server/wifi/WifiHealthMonitor.java | 48 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiHealthMonitorTest.java | 68 |
2 files changed, 93 insertions, 23 deletions
diff --git a/service/java/com/android/server/wifi/WifiHealthMonitor.java b/service/java/com/android/server/wifi/WifiHealthMonitor.java index 402032d2c..4228e0d6a 100644 --- a/service/java/com/android/server/wifi/WifiHealthMonitor.java +++ b/service/java/com/android/server/wifi/WifiHealthMonitor.java @@ -79,11 +79,11 @@ public class WifiHealthMonitor { // The time that device waits after device boot before triggering post-boot detection. // This needs be long enough so that memory read can complete before post-boot detection. private static final int POST_BOOT_DETECTION_WAIT_TIME_MS = 25_000; - // 0 - ELAPSED_REAL_TIME, 1 - RTC - private static final int DAILY_DETECTION_ALARM_TYPE = 1; // The time interval between two daily detections - private static final int DAILY_DETECTION_INTERVAL_MS = 24 * 3600_000; - private static final int DAILY_DETECTION_HOUR = 23; + private static final long DAILY_DETECTION_INTERVAL_MS = 24 * 3600_000; + public static final int DAILY_DETECTION_HOUR = 23; + private static final int DAILY_DETECTION_MIN = 00; + private static final long MIN_WAIT_TIME_BEFORE_FIRST_DETECTION_MS = 100_000; // Max interval between pre-boot scan and post-boot scan to qualify post-boot scan detection private static final long MAX_INTERVAL_BETWEEN_TWO_SCAN_MS = 60_000; // The minimum number of BSSIDs that should be found during a normal scan to trigger detection @@ -179,7 +179,7 @@ public class WifiHealthMonitor { Log.e(TAG, "Reinstalling MemoryStore"); } requestReadForPostBootDetection(); - setDailyDetectionAlarm(); + setFirstHealthDetectionAlarm(); setPostBootDetectionAlarm(); } @@ -259,22 +259,25 @@ public class WifiHealthMonitor { + ", Above2g scanResult count: " + scanStats.getNumBssidLastScanAbove2g()); } - private void setDailyDetectionAlarm() { - if (DAILY_DETECTION_ALARM_TYPE == 0) { - mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, - mClock.getElapsedSinceBootMillis() + DAILY_DETECTION_INTERVAL_MS, - DAILY_DETECTION_TIMER_TAG, - mDailyDetectionListener, mHandler); - } else { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(System.currentTimeMillis()); - calendar.set(Calendar.HOUR_OF_DAY, DAILY_DETECTION_HOUR); - calendar.set(Calendar.MINUTE, 0); - mAlarmManager.set(AlarmManager.RTC_WAKEUP, - calendar.getTimeInMillis(), - DAILY_DETECTION_TIMER_TAG, - mDailyDetectionListener, mHandler); + private void setFirstHealthDetectionAlarm() { + long currTimeMs = mClock.getWallClockMillis(); + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(currTimeMs); + calendar.set(Calendar.HOUR_OF_DAY, DAILY_DETECTION_HOUR); + calendar.set(Calendar.MINUTE, DAILY_DETECTION_MIN); + long targetTimeMs = calendar.getTimeInMillis(); + long waitTimeMs = targetTimeMs - currTimeMs; + if (waitTimeMs < MIN_WAIT_TIME_BEFORE_FIRST_DETECTION_MS) { + waitTimeMs += DAILY_DETECTION_INTERVAL_MS; } + scheduleDailyDetectionAlarm(waitTimeMs); + } + + private void scheduleDailyDetectionAlarm(long waitTimeMs) { + mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, + mClock.getElapsedSinceBootMillis() + waitTimeMs, + DAILY_DETECTION_TIMER_TAG, + mDailyDetectionListener, mHandler); } private void setPostBootDetectionAlarm() { @@ -295,7 +298,7 @@ public class WifiHealthMonitor { mHasNewDataForWifiMetrics = true; int connectionDurationSec = 0; // Set the alarm for the next day - setDailyDetectionAlarm(); + scheduleDailyDetectionAlarm(DAILY_DETECTION_INTERVAL_MS); List<WifiConfiguration> configuredNetworks = mWifiConfigManager.getConfiguredNetworks(); for (WifiConfiguration network : configuredNetworks) { if (isInValidConfiguredNetwork(network)) { @@ -303,7 +306,6 @@ public class WifiHealthMonitor { } logd(network.SSID); PerNetwork perNetwork = mWifiScoreCard.lookupNetwork(network.SSID); - logd("before daily update: " + perNetwork.toString()); int detectionFlag = perNetwork.dailyDetection(mFailureStatsDecrease, mFailureStatsIncrease, mFailureStatsHigh); @@ -316,6 +318,8 @@ public class WifiHealthMonitor { connectionDurationSec += perNetwork.getRecentStats().getCount( WifiScoreCard.CNT_CONNECTION_DURATION_SEC); + + logd("before daily update: " + perNetwork.toString()); // Update historical stats with dailyStats and clear dailyStats perNetwork.updateAfterDailyDetection(); logd("after daily update: " + perNetwork.toString()); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiHealthMonitorTest.java b/tests/wifitests/src/com/android/server/wifi/WifiHealthMonitorTest.java index 33b22d2ef..253180d12 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiHealthMonitorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiHealthMonitorTest.java @@ -54,6 +54,7 @@ import com.android.server.wifi.proto.WifiScoreCardProto.SystemInfoStats; import com.android.server.wifi.proto.WifiStatsLog; import com.android.server.wifi.proto.nano.WifiMetricsProto.HealthMonitorMetrics; + import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -63,6 +64,7 @@ import org.mockito.quality.Strictness; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Calendar; import java.util.List; /** @@ -74,6 +76,7 @@ public class WifiHealthMonitorTest extends WifiBaseTest { static final WifiSsid TEST_SSID_1 = WifiSsid.createFromAsciiEncoded("Joe's Place"); static final WifiSsid TEST_SSID_2 = WifiSsid.createFromAsciiEncoded("Poe's Place"); static final MacAddress TEST_BSSID_1 = MacAddress.fromString("aa:bb:cc:dd:ee:ff"); + private static final long CURRENT_ELAPSED_TIME_MS = 1000; private WifiScoreCard mWifiScoreCard; private WifiHealthMonitor mWifiHealthMonitor; @@ -394,7 +397,70 @@ public class WifiHealthMonitorTest extends WifiBaseTest { } /** - * Check stats after two daily detections. + * Check alarm timing of a multi-day run. + */ + @Test + public void testTimerMultiDayRun() throws Exception { + long currentWallClockTimeMs = 23 * 3600_000; + long currentElapsedTimeMs = CURRENT_ELAPSED_TIME_MS; + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(currentWallClockTimeMs); + int expectedWaitHours = WifiHealthMonitor.DAILY_DETECTION_HOUR + - calendar.get(Calendar.HOUR_OF_DAY); + if (expectedWaitHours <= 0) expectedWaitHours += 24; + + // day 1 + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentElapsedTimeMs); + when(mClock.getWallClockMillis()).thenReturn(currentWallClockTimeMs); + mWifiHealthMonitor.installMemoryStoreSetUpDetectionAlarm(mMemoryStore); + long waitTimeMs = mAlarmManager + .getTriggerTimeMillis(WifiHealthMonitor.DAILY_DETECTION_TIMER_TAG) + - currentElapsedTimeMs; + assertEquals(expectedWaitHours * 3600_000, waitTimeMs); + currentElapsedTimeMs += 24 * 3600_000 + 1; + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentElapsedTimeMs); + mAlarmManager.dispatch(WifiHealthMonitor.DAILY_DETECTION_TIMER_TAG); + mLooper.dispatchAll(); + waitTimeMs = mAlarmManager + .getTriggerTimeMillis(WifiHealthMonitor.DAILY_DETECTION_TIMER_TAG) + - currentElapsedTimeMs; + assertEquals(24 * 3600_000, waitTimeMs); + // day 2 + currentElapsedTimeMs += 24 * 3600_000 - 1; + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentElapsedTimeMs); + mAlarmManager.dispatch(WifiHealthMonitor.DAILY_DETECTION_TIMER_TAG); + mLooper.dispatchAll(); + waitTimeMs = mAlarmManager + .getTriggerTimeMillis(WifiHealthMonitor.DAILY_DETECTION_TIMER_TAG) + - currentElapsedTimeMs; + assertEquals(24 * 3600_000, waitTimeMs); + } + + /** + * Check the alarm timing with a different wall clock time + */ + @Test + public void testTimerWith() throws Exception { + long currentWallClockTimeMs = 7 * 3600_000; + long currentElapsedTimeMs = CURRENT_ELAPSED_TIME_MS; + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(currentWallClockTimeMs); + int expectedWaitHours = WifiHealthMonitor.DAILY_DETECTION_HOUR + - calendar.get(Calendar.HOUR_OF_DAY); + if (expectedWaitHours <= 0) expectedWaitHours += 24; + + // day 1 + when(mClock.getElapsedSinceBootMillis()).thenReturn(currentElapsedTimeMs); + when(mClock.getWallClockMillis()).thenReturn(currentWallClockTimeMs); + mWifiHealthMonitor.installMemoryStoreSetUpDetectionAlarm(mMemoryStore); + long waitTimeMs = mAlarmManager + .getTriggerTimeMillis(WifiHealthMonitor.DAILY_DETECTION_TIMER_TAG) + - currentElapsedTimeMs; + assertEquals(expectedWaitHours * 3600_000, waitTimeMs); + } + + /** + * Check stats with two daily detections. */ @Test public void testTwoDailyDetections() throws Exception { |