1818
1919import java .io .PrintWriter ;
2020import java .util .ArrayList ;
21+ import java .util .Collections ;
22+ import java .util .Comparator ;
2123import java .util .Formatter ;
2224import java .util .List ;
2325import java .util .Map ;
@@ -1127,8 +1129,10 @@ private static final String printWakeLock(StringBuilder sb, Timer timer,
11271129 if (totalTimeMillis != 0 ) {
11281130 sb .append (linePrefix );
11291131 formatTimeMs (sb , totalTimeMillis );
1130- if (name != null ) sb .append (name );
1131- sb .append (' ' );
1132+ if (name != null ) {
1133+ sb .append (name );
1134+ sb .append (' ' );
1135+ }
11321136 sb .append ('(' );
11331137 sb .append (count );
11341138 sb .append (" times)" );
@@ -1440,8 +1444,21 @@ public final void dumpCheckinLocked(PrintWriter pw, int which, int reqUid) {
14401444 }
14411445 }
14421446
1447+ static final class TimerEntry {
1448+ final String mName ;
1449+ final int mId ;
1450+ final BatteryStats .Timer mTimer ;
1451+ final long mTime ;
1452+ TimerEntry (String name , int id , BatteryStats .Timer timer , long time ) {
1453+ mName = name ;
1454+ mId = id ;
1455+ mTimer = timer ;
1456+ mTime = time ;
1457+ }
1458+ }
1459+
14431460 @ SuppressWarnings ("unused" )
1444- public final void dumpLocked (PrintWriter pw , String prefix , int which , int reqUid ) {
1461+ public final void dumpLocked (PrintWriter pw , String prefix , final int which , int reqUid ) {
14451462 final long rawUptime = SystemClock .uptimeMillis () * 1000 ;
14461463 final long rawRealtime = SystemClock .elapsedRealtime () * 1000 ;
14471464 final long batteryUptime = getBatteryUptime (rawUptime );
@@ -1516,19 +1533,43 @@ public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUi
15161533 long txTotal = 0 ;
15171534 long fullWakeLockTimeTotalMicros = 0 ;
15181535 long partialWakeLockTimeTotalMicros = 0 ;
1519-
1536+
1537+ final Comparator <TimerEntry > timerComparator = new Comparator <TimerEntry >() {
1538+ @ Override
1539+ public int compare (TimerEntry lhs , TimerEntry rhs ) {
1540+ long lhsTime = lhs .mTime ;
1541+ long rhsTime = rhs .mTime ;
1542+ if (lhsTime < rhsTime ) {
1543+ return 1 ;
1544+ }
1545+ if (lhsTime > rhsTime ) {
1546+ return -1 ;
1547+ }
1548+ return 0 ;
1549+ }
1550+ };
1551+
15201552 if (reqUid < 0 ) {
15211553 Map <String , ? extends BatteryStats .Timer > kernelWakelocks = getKernelWakelockStats ();
15221554 if (kernelWakelocks .size () > 0 ) {
1555+ final ArrayList <TimerEntry > timers = new ArrayList <TimerEntry >();
15231556 for (Map .Entry <String , ? extends BatteryStats .Timer > ent : kernelWakelocks .entrySet ()) {
1524-
1557+ BatteryStats .Timer timer = ent .getValue ();
1558+ long totalTimeMillis = computeWakeLock (timer , batteryRealtime , which );
1559+ if (totalTimeMillis > 0 ) {
1560+ timers .add (new TimerEntry (ent .getKey (), 0 , timer , totalTimeMillis ));
1561+ }
1562+ }
1563+ Collections .sort (timers , timerComparator );
1564+ for (int i =0 ; i <timers .size (); i ++) {
1565+ TimerEntry timer = timers .get (i );
15251566 String linePrefix = ": " ;
15261567 sb .setLength (0 );
15271568 sb .append (prefix );
15281569 sb .append (" Kernel Wake lock " );
1529- sb .append (ent . getKey () );
1530- linePrefix = printWakeLock (sb , ent . getValue () , batteryRealtime , null , which ,
1531- linePrefix );
1570+ sb .append (timer . mName );
1571+ linePrefix = printWakeLock (sb , timer . mTimer , batteryRealtime , null ,
1572+ which , linePrefix );
15321573 if (!linePrefix .equals (": " )) {
15331574 sb .append (" realtime" );
15341575 // Only print out wake locks that were held
@@ -1537,7 +1578,9 @@ public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUi
15371578 }
15381579 }
15391580 }
1540-
1581+
1582+ final ArrayList <TimerEntry > timers = new ArrayList <TimerEntry >();
1583+
15411584 for (int iu = 0 ; iu < NU ; iu ++) {
15421585 Uid u = uidStats .valueAt (iu );
15431586 rxTotal += u .getTcpBytesReceived (which );
@@ -1557,8 +1600,18 @@ public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUi
15571600
15581601 Timer partialWakeTimer = wl .getWakeTime (WAKE_TYPE_PARTIAL );
15591602 if (partialWakeTimer != null ) {
1560- partialWakeLockTimeTotalMicros + = partialWakeTimer .getTotalTimeLocked (
1603+ long totalTimeMicros = partialWakeTimer .getTotalTimeLocked (
15611604 batteryRealtime , which );
1605+ if (totalTimeMicros > 0 ) {
1606+ if (reqUid < 0 ) {
1607+ // Only show the ordered list of all wake
1608+ // locks if the caller is not asking for data
1609+ // about a specific uid.
1610+ timers .add (new TimerEntry (ent .getKey (), u .getUid (),
1611+ partialWakeTimer , totalTimeMicros ));
1612+ }
1613+ partialWakeLockTimeTotalMicros += totalTimeMicros ;
1614+ }
15621615 }
15631616 }
15641617 }
@@ -1571,7 +1624,7 @@ public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUi
15711624 sb .append (prefix );
15721625 sb .append (" Total full wakelock time: " ); formatTimeMs (sb ,
15731626 (fullWakeLockTimeTotalMicros + 500 ) / 1000 );
1574- sb .append (", Total partial waklock time: " ); formatTimeMs (sb ,
1627+ sb .append (", Total partial wakelock time: " ); formatTimeMs (sb ,
15751628 (partialWakeLockTimeTotalMicros + 500 ) / 1000 );
15761629 pw .println (sb .toString ());
15771630
@@ -1676,9 +1729,26 @@ public final void dumpLocked(PrintWriter pw, String prefix, int which, int reqUi
16761729 pw .println (getDischargeAmountScreenOnSinceCharge ());
16771730 pw .print (prefix ); pw .print (" Amount discharged while screen off: " );
16781731 pw .println (getDischargeAmountScreenOffSinceCharge ());
1679- pw .println (" " );
1732+ pw .println ();
1733+ }
1734+
1735+ if (timers .size () > 0 ) {
1736+ Collections .sort (timers , timerComparator );
1737+ pw .print (prefix ); pw .println (" All partial wake locks:" );
1738+ for (int i =0 ; i <timers .size (); i ++) {
1739+ TimerEntry timer = timers .get (i );
1740+ sb .setLength (0 );
1741+ sb .append (" Wake lock #" );
1742+ sb .append (timer .mId );
1743+ sb .append (" " );
1744+ sb .append (timer .mName );
1745+ printWakeLock (sb , timer .mTimer , batteryRealtime , null , which , ": " );
1746+ sb .append (" realtime" );
1747+ pw .println (sb .toString ());
1748+ }
1749+ timers .clear ();
1750+ pw .println ();
16801751 }
1681-
16821752
16831753 for (int iu =0 ; iu <NU ; iu ++) {
16841754 final int uid = uidStats .keyAt (iu );
0 commit comments