Skip to content

Commit c24ab86

Browse files
author
Dianne Hackborn
committed
Work on issue #5465917: Wakelock *overflow* held for very long times
- Fix ordering problem in sync manager that would cause its wake locks to slightly appear in the system process when they should be fully accounted against the app. - Allow the system process to have more wake lock names in its battery stats. - In the bug report output, print totals of the wake locks for each process, to make it easier to parse what is being printed for things like the system process with a huge number of individual wake locks. Change-Id: I3cf39330f22f3c51c11e65e4124150d73a7da2dd
1 parent 535f6ea commit c24ab86

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

core/java/android/content/SyncManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,8 @@ protected void close() {
990990
mBound = false;
991991
mContext.unbindService(this);
992992
}
993-
mSyncWakeLock.setWorkSource(null);
994993
mSyncWakeLock.release();
994+
mSyncWakeLock.setWorkSource(null);
995995
}
996996

997997
@Override

core/java/android/os/BatteryStats.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,16 @@ private final String formatBytesLocked(long bytes) {
10951095
}
10961096
}
10971097

1098+
private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1099+
if (timer != null) {
1100+
// Convert from microseconds to milliseconds with rounding
1101+
long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1102+
long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1103+
return totalTimeMillis;
1104+
}
1105+
return 0;
1106+
}
1107+
10981108
/**
10991109
*
11001110
* @param sb a StringBuilder object.
@@ -1109,9 +1119,7 @@ private static final String printWakeLock(StringBuilder sb, Timer timer,
11091119
long batteryRealtime, String name, int which, String linePrefix) {
11101120

11111121
if (timer != null) {
1112-
// Convert from microseconds to milliseconds with rounding
1113-
long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1114-
long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1122+
long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
11151123

11161124
int count = timer.getCountLocked(which);
11171125
if (totalTimeMillis != 0) {
@@ -1735,6 +1743,8 @@ public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUi
17351743

17361744
Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
17371745
if (wakelocks.size() > 0) {
1746+
long totalFull = 0, totalPartial = 0, totalWindow = 0;
1747+
int count = 0;
17381748
for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
17391749
: wakelocks.entrySet()) {
17401750
Uid.Wakelock wl = ent.getValue();
@@ -1754,6 +1764,44 @@ public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUi
17541764
// Only print out wake locks that were held
17551765
pw.println(sb.toString());
17561766
uidActivity = true;
1767+
count++;
1768+
}
1769+
totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
1770+
batteryRealtime, which);
1771+
totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
1772+
batteryRealtime, which);
1773+
totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
1774+
batteryRealtime, which);
1775+
}
1776+
if (count > 1) {
1777+
if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
1778+
sb.setLength(0);
1779+
sb.append(prefix);
1780+
sb.append(" TOTAL wake: ");
1781+
boolean needComma = false;
1782+
if (totalFull != 0) {
1783+
needComma = true;
1784+
formatTimeMs(sb, totalFull);
1785+
sb.append("full");
1786+
}
1787+
if (totalPartial != 0) {
1788+
if (needComma) {
1789+
sb.append(", ");
1790+
}
1791+
needComma = true;
1792+
formatTimeMs(sb, totalPartial);
1793+
sb.append("partial");
1794+
}
1795+
if (totalWindow != 0) {
1796+
if (needComma) {
1797+
sb.append(", ");
1798+
}
1799+
needComma = true;
1800+
formatTimeMs(sb, totalWindow);
1801+
sb.append("window");
1802+
}
1803+
sb.append(" realtime");
1804+
pw.println(sb.toString());
17571805
}
17581806
}
17591807
}

core/java/com/android/internal/os/BatteryStatsImpl.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public final class BatteryStatsImpl extends BatteryStats {
9898
// in to one common name.
9999
private static final int MAX_WAKELOCKS_PER_UID = 30;
100100

101+
// The system process gets more. It is special. Oh so special.
102+
// With, you know, special needs. Like this.
103+
private static final int MAX_WAKELOCKS_PER_UID_IN_SYSTEM = 50;
104+
101105
private static final String BATCHED_WAKELOCK_NAME = "*overflow*";
102106

103107
private static int sNumSpeedSteps;
@@ -2895,12 +2899,10 @@ void readFromParcelLocked(ArrayList<Unpluggable> unpluggables, Parcel in) {
28952899
String wakelockName = in.readString();
28962900
Uid.Wakelock wakelock = new Wakelock();
28972901
wakelock.readFromParcelLocked(unpluggables, in);
2898-
if (mWakelockStats.size() < MAX_WAKELOCKS_PER_UID) {
2899-
// We will just drop some random set of wakelocks if
2900-
// the previous run of the system was an older version
2901-
// that didn't impose a limit.
2902-
mWakelockStats.put(wakelockName, wakelock);
2903-
}
2902+
// We will just drop some random set of wakelocks if
2903+
// the previous run of the system was an older version
2904+
// that didn't impose a limit.
2905+
mWakelockStats.put(wakelockName, wakelock);
29042906
}
29052907

29062908
int numSensors = in.readInt();
@@ -3904,7 +3906,9 @@ public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
39043906
public StopwatchTimer getWakeTimerLocked(String name, int type) {
39053907
Wakelock wl = mWakelockStats.get(name);
39063908
if (wl == null) {
3907-
if (mWakelockStats.size() > MAX_WAKELOCKS_PER_UID) {
3909+
final int N = mWakelockStats.size();
3910+
if (N > MAX_WAKELOCKS_PER_UID && (mUid != Process.SYSTEM_UID
3911+
|| N > MAX_WAKELOCKS_PER_UID_IN_SYSTEM)) {
39083912
name = BATCHED_WAKELOCK_NAME;
39093913
wl = mWakelockStats.get(name);
39103914
}

0 commit comments

Comments
 (0)