diff options
| author | Kalesh Singh <kaleshsingh@google.com> | 2019-07-18 14:09:44 -0700 |
|---|---|---|
| committer | Kalesh Singh <kaleshsingh@google.com> | 2019-07-23 09:40:31 -0700 |
| commit | f166d19ebe1e590c837c2fdce21652c550fd5f55 (patch) | |
| tree | 96e635815a1bc97558c7efdfadb1f25dd0b85c54 /core/java | |
| parent | f4b0318d6f9ff9078fc5fda5be7046105bca3fb1 (diff) | |
Get wakelock stats from SystemSuspend
Read wakelock stats from SystemSuspend if possible else fallback to
/d/wakeup_source, /proc/wakelocks.
Bug: 128923994
Test: KernelWakelockReaderTest
Change-Id: I859092d53d7697a4940f9369480e203181f0c370
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/com/android/internal/os/KernelWakelockReader.java | 135 |
1 files changed, 73 insertions, 62 deletions
diff --git a/core/java/com/android/internal/os/KernelWakelockReader.java b/core/java/com/android/internal/os/KernelWakelockReader.java index df8c6d021466..ee0903a30e6d 100644 --- a/core/java/com/android/internal/os/KernelWakelockReader.java +++ b/core/java/com/android/internal/os/KernelWakelockReader.java @@ -27,6 +27,7 @@ import android.util.Slog; import com.android.internal.annotations.VisibleForTesting; +import java.io.File; import java.io.FileInputStream; import java.util.Iterator; @@ -38,6 +39,7 @@ public class KernelWakelockReader { private static int sKernelWakelockUpdateVersion = 0; private static final String sWakelockFile = "/proc/wakelocks"; private static final String sWakeupSourceFile = "/d/wakeup_sources"; + private static final String sSysClassWakeupDir = "/sys/class/wakeup"; private static final int[] PROC_WAKELOCKS_FORMAT = new int[] { Process.PROC_TAB_TERM|Process.PROC_OUT_STRING| // 0: name @@ -71,99 +73,108 @@ public class KernelWakelockReader { * @return the updated data. */ public final KernelWakelockStats readKernelWakelockStats(KernelWakelockStats staleStats) { - byte[] buffer = new byte[32*1024]; - int len = 0; - boolean wakeup_sources; - final long startTime = SystemClock.uptimeMillis(); + boolean useSystemSuspend = (new File(sSysClassWakeupDir)).exists(); - final int oldMask = StrictMode.allowThreadDiskReadsMask(); - try { - FileInputStream is; - try { - is = new FileInputStream(sWakelockFile); - wakeup_sources = false; - } catch (java.io.FileNotFoundException e) { + if (useSystemSuspend) { + WakeLockInfo[] wlStats = null; + if (mSuspendControlService == null) { try { - is = new FileInputStream(sWakeupSourceFile); - wakeup_sources = true; - } catch (java.io.FileNotFoundException e2) { - Slog.wtf(TAG, "neither " + sWakelockFile + " nor " + - sWakeupSourceFile + " exists"); + mSuspendControlService = ISuspendControlService.Stub.asInterface( + ServiceManager.getServiceOrThrow("suspend_control")); + } catch (ServiceNotFoundException e) { + Slog.wtf(TAG, "Required service suspend_control not available", e); return null; } } - int cnt; - while ((cnt = is.read(buffer, len, buffer.length - len)) > 0) { - len += cnt; + try { + wlStats = mSuspendControlService.getWakeLockStats(); + updateVersion(staleStats); + updateWakelockStats(wlStats, staleStats); + } catch (RemoteException e) { + Slog.wtf(TAG, "Failed to obtain wakelock stats from ISuspendControlService", e); + return null; } - is.close(); - } catch (java.io.IOException e) { - Slog.wtf(TAG, "failed to read kernel wakelocks", e); - return null; - } finally { - StrictMode.setThreadPolicyMask(oldMask); - } + return removeOldStats(staleStats); - final long readTime = SystemClock.uptimeMillis() - startTime; - if (readTime > 100) { - Slog.w(TAG, "Reading wakelock stats took " + readTime + "ms"); - } + } else { + byte[] buffer = new byte[32*1024]; + int len = 0; + boolean wakeup_sources; + final long startTime = SystemClock.uptimeMillis(); - if (len > 0) { - if (len >= buffer.length) { - Slog.wtf(TAG, "Kernel wake locks exceeded buffer size " + buffer.length); - } - int i; - for (i=0; i<len; i++) { - if (buffer[i] == '\0') { - len = i; - break; + final int oldMask = StrictMode.allowThreadDiskReadsMask(); + try { + FileInputStream is; + try { + is = new FileInputStream(sWakelockFile); + wakeup_sources = false; + } catch (java.io.FileNotFoundException e) { + try { + is = new FileInputStream(sWakeupSourceFile); + wakeup_sources = true; + } catch (java.io.FileNotFoundException e2) { + Slog.wtf(TAG, "neither " + sWakelockFile + " nor " + + sWakeupSourceFile + " exists"); + return null; + } } - } - } - updateVersion(staleStats); + int cnt; + while ((cnt = is.read(buffer, len, buffer.length - len)) > 0) { + len += cnt; + } - parseProcWakelocks(buffer, len, wakeup_sources, staleStats); + is.close(); + } catch (java.io.IOException e) { + Slog.wtf(TAG, "failed to read kernel wakelocks", e); + return null; + } finally { + StrictMode.setThreadPolicyMask(oldMask); + } - if (mSuspendControlService == null) { - try { - mSuspendControlService = ISuspendControlService.Stub.asInterface( - ServiceManager.getServiceOrThrow("suspend_control")); - } catch (ServiceNotFoundException e) { - Slog.wtf(TAG, "Required service suspend_control not available", e); + final long readTime = SystemClock.uptimeMillis() - startTime; + if (readTime > 100) { + Slog.w(TAG, "Reading wakelock stats took " + readTime + "ms"); } - } - try { - WakeLockInfo[] wlStats = mSuspendControlService.getWakeLockStats(); - getNativeWakelockStats(wlStats, staleStats); - } catch (RemoteException e) { - Slog.wtf(TAG, "Failed to obtain wakelock stats from ISuspendControlService", e); - } + if (len > 0) { + if (len >= buffer.length) { + Slog.wtf(TAG, "Kernel wake locks exceeded buffer size " + buffer.length); + } + int i; + for (i=0; i<len; i++) { + if (buffer[i] == '\0') { + len = i; + break; + } + } + } - return removeOldStats(staleStats); + updateVersion(staleStats); + parseProcWakelocks(buffer, len, wakeup_sources, staleStats); + return removeOldStats(staleStats); + } } /** - * Reads native wakelock stats from SystemSuspend and updates staleStats with the new - * information. + * Updates statleStats with stats from SystemSuspend. * @param staleStats Existing object to update. * @return the updated stats. */ @VisibleForTesting - public KernelWakelockStats getNativeWakelockStats(WakeLockInfo[] wlStats, + public KernelWakelockStats updateWakelockStats(WakeLockInfo[] wlStats, final KernelWakelockStats staleStats) { for (WakeLockInfo info : wlStats) { if (!staleStats.containsKey(info.name)) { staleStats.put(info.name, new KernelWakelockStats.Entry((int) info.activeCount, - info.totalTime, sKernelWakelockUpdateVersion)); + info.totalTime * 1000 /* ms to us */, sKernelWakelockUpdateVersion)); } else { KernelWakelockStats.Entry kwlStats = staleStats.get(info.name); kwlStats.mCount = (int) info.activeCount; - kwlStats.mTotalTime = info.totalTime; + // Convert milliseconds to microseconds + kwlStats.mTotalTime = info.totalTime * 1000; kwlStats.mVersion = sKernelWakelockUpdateVersion; } } |
