Update wakelock detector

1. Use new API: getSubTimer().getTotalDurationMsLocked(), which returns
the wakelock running time in the background.
2. Add code to detect whether app is currently holding wakelocks, by
using API: getCurrentDurationMsLocked

Bug: 38233034
Test: RunSettingsRoboTests
Change-Id: If69b751acf5741ff8df2c905642c008a0a2b32e6
This commit is contained in:
jackqdyulei
2017-06-15 10:18:00 -07:00
parent ea46967c2e
commit c2baa2947a
2 changed files with 43 additions and 60 deletions

View File

@@ -21,7 +21,6 @@ import android.content.pm.PackageManager;
import android.os.BatteryStats;
import android.os.SystemClock;
import android.support.annotation.VisibleForTesting;
import android.util.ArrayMap;
import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatteryStatsHelper;
@@ -87,23 +86,10 @@ public class WakeLockAnomalyDetector implements AnomalyDetector {
continue;
}
final ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> wakelocks =
uid.getWakelockStats();
long maxPartialWakeLockMs = 0;
final long currentDurationMs = getCurrentDurationMs(uid, rawRealtime);
final long backgroundDurationMs = getBackgroundTotalDurationMs(uid, rawRealtime);
for (int iw = wakelocks.size() - 1; iw >= 0; iw--) {
final BatteryStats.Timer timer = wakelocks.valueAt(iw).getWakeTime(
BatteryStats.WAKE_TYPE_PARTIAL);
if (timer == null) {
continue;
}
maxPartialWakeLockMs = Math.max(maxPartialWakeLockMs,
getTotalDurationMs(timer, rawRealtime));
}
// Report application as anomaly if wakelock time is too long
// TODO(b/38233034): add more attributes to detect wakelock anomaly
if (maxPartialWakeLockMs > mWakeLockThresholdMs) {
if (backgroundDurationMs > mWakeLockThresholdMs && currentDurationMs != 0) {
final String packageName = mBatteryUtils.getPackageName(uid.getUid());
final CharSequence displayName = Utils.getApplicationLabel(mContext,
packageName);
@@ -119,16 +105,22 @@ public class WakeLockAnomalyDetector implements AnomalyDetector {
anomalies.add(anomaly);
}
}
}
return anomalies;
}
@VisibleForTesting
long getTotalDurationMs(BatteryStats.Timer timer, long rawRealtime) {
if (timer == null) {
return 0;
}
return timer.getTotalDurationMsLocked(rawRealtime);
long getCurrentDurationMs(BatteryStats.Uid uid, long elapsedRealtimeMs) {
BatteryStats.Timer timer = uid.getAggregatedPartialWakelockTimer();
return timer != null ? timer.getCurrentDurationMsLocked(elapsedRealtimeMs) : 0;
}
@VisibleForTesting
long getBackgroundTotalDurationMs(BatteryStats.Uid uid, long elapsedRealtimeMs) {
BatteryStats.Timer timer = uid.getAggregatedPartialWakelockTimer();
BatteryStats.Timer subTimer = timer != null ? timer.getSubTimer() : null;
return subTimer != null ? subTimer.getTotalDurationMsLocked(elapsedRealtimeMs) : 0;
}
}