Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.internal.os; |
| 17 | |
| 18 | import android.os.Process; |
Mike Ma | ea357e5 | 2018-02-23 11:07:53 -0800 | [diff] [blame] | 19 | import android.os.StrictMode; |
Amith Yamasani | 3d5d9ae | 2017-07-05 13:34:46 -0700 | [diff] [blame] | 20 | import android.os.SystemClock; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 21 | import android.util.Slog; |
| 22 | |
Adam Lesinski | d84ad30 | 2016-05-17 18:31:02 -0700 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
| 24 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 25 | import java.io.FileInputStream; |
| 26 | import java.util.Iterator; |
| 27 | |
| 28 | /** |
| 29 | * Reads and parses wakelock stats from the kernel (/proc/wakelocks). |
| 30 | */ |
| 31 | public class KernelWakelockReader { |
| 32 | private static final String TAG = "KernelWakelockReader"; |
| 33 | private static int sKernelWakelockUpdateVersion = 0; |
| 34 | private static final String sWakelockFile = "/proc/wakelocks"; |
| 35 | private static final String sWakeupSourceFile = "/d/wakeup_sources"; |
| 36 | |
| 37 | private static final int[] PROC_WAKELOCKS_FORMAT = new int[] { |
| 38 | Process.PROC_TAB_TERM|Process.PROC_OUT_STRING| // 0: name |
| 39 | Process.PROC_QUOTES, |
| 40 | Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 1: count |
| 41 | Process.PROC_TAB_TERM, |
| 42 | Process.PROC_TAB_TERM, |
| 43 | Process.PROC_TAB_TERM, |
| 44 | Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 5: totalTime |
| 45 | }; |
| 46 | |
| 47 | private static final int[] WAKEUP_SOURCES_FORMAT = new int[] { |
| 48 | Process.PROC_TAB_TERM|Process.PROC_OUT_STRING, // 0: name |
| 49 | Process.PROC_TAB_TERM|Process.PROC_COMBINE| |
| 50 | Process.PROC_OUT_LONG, // 1: count |
| 51 | Process.PROC_TAB_TERM|Process.PROC_COMBINE, |
| 52 | Process.PROC_TAB_TERM|Process.PROC_COMBINE, |
| 53 | Process.PROC_TAB_TERM|Process.PROC_COMBINE, |
| 54 | Process.PROC_TAB_TERM|Process.PROC_COMBINE, |
| 55 | Process.PROC_TAB_TERM|Process.PROC_COMBINE |
| 56 | |Process.PROC_OUT_LONG, // 6: totalTime |
| 57 | }; |
| 58 | |
| 59 | private final String[] mProcWakelocksName = new String[3]; |
| 60 | private final long[] mProcWakelocksData = new long[3]; |
| 61 | |
| 62 | /** |
| 63 | * Reads kernel wakelock stats and updates the staleStats with the new information. |
| 64 | * @param staleStats Existing object to update. |
| 65 | * @return the updated data. |
| 66 | */ |
| 67 | public final KernelWakelockStats readKernelWakelockStats(KernelWakelockStats staleStats) { |
| 68 | byte[] buffer = new byte[32*1024]; |
| 69 | int len; |
| 70 | boolean wakeup_sources; |
Amith Yamasani | 3d5d9ae | 2017-07-05 13:34:46 -0700 | [diff] [blame] | 71 | final long startTime = SystemClock.uptimeMillis(); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 72 | |
Mike Ma | ea357e5 | 2018-02-23 11:07:53 -0800 | [diff] [blame] | 73 | final int oldMask = StrictMode.allowThreadDiskReadsMask(); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 74 | try { |
| 75 | FileInputStream is; |
| 76 | try { |
Adam Lesinski | 6856964 | 2015-07-21 12:08:56 -0700 | [diff] [blame] | 77 | is = new FileInputStream(sWakelockFile); |
| 78 | wakeup_sources = false; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 79 | } catch (java.io.FileNotFoundException e) { |
| 80 | try { |
Adam Lesinski | 6856964 | 2015-07-21 12:08:56 -0700 | [diff] [blame] | 81 | is = new FileInputStream(sWakeupSourceFile); |
| 82 | wakeup_sources = true; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 83 | } catch (java.io.FileNotFoundException e2) { |
Adam Lesinski | fbabe7d | 2015-08-03 14:37:38 -0700 | [diff] [blame] | 84 | Slog.wtf(TAG, "neither " + sWakelockFile + " nor " + |
| 85 | sWakeupSourceFile + " exists"); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 86 | return null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | len = is.read(buffer); |
| 91 | is.close(); |
| 92 | } catch (java.io.IOException e) { |
Adam Lesinski | fbabe7d | 2015-08-03 14:37:38 -0700 | [diff] [blame] | 93 | Slog.wtf(TAG, "failed to read kernel wakelocks", e); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 94 | return null; |
Mike Ma | ea357e5 | 2018-02-23 11:07:53 -0800 | [diff] [blame] | 95 | } finally { |
| 96 | StrictMode.setThreadPolicyMask(oldMask); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Amith Yamasani | 3d5d9ae | 2017-07-05 13:34:46 -0700 | [diff] [blame] | 99 | final long readTime = SystemClock.uptimeMillis() - startTime; |
| 100 | if (readTime > 100) { |
| 101 | Slog.w(TAG, "Reading wakelock stats took " + readTime + "ms"); |
| 102 | } |
| 103 | |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 104 | if (len > 0) { |
| 105 | if (len >= buffer.length) { |
| 106 | Slog.wtf(TAG, "Kernel wake locks exceeded buffer size " + buffer.length); |
| 107 | } |
| 108 | int i; |
| 109 | for (i=0; i<len; i++) { |
| 110 | if (buffer[i] == '\0') { |
| 111 | len = i; |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return parseProcWakelocks(buffer, len, wakeup_sources, staleStats); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Reads the wakelocks and updates the staleStats with the new information. |
| 121 | */ |
Adam Lesinski | d84ad30 | 2016-05-17 18:31:02 -0700 | [diff] [blame] | 122 | @VisibleForTesting |
| 123 | public KernelWakelockStats parseProcWakelocks(byte[] wlBuffer, int len, boolean wakeup_sources, |
| 124 | final KernelWakelockStats staleStats) { |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 125 | String name; |
| 126 | int count; |
| 127 | long totalTime; |
| 128 | int startIndex; |
| 129 | int endIndex; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 130 | |
| 131 | // Advance past the first line. |
| 132 | int i; |
| 133 | for (i = 0; i < len && wlBuffer[i] != '\n' && wlBuffer[i] != '\0'; i++); |
| 134 | startIndex = endIndex = i + 1; |
| 135 | |
| 136 | synchronized(this) { |
| 137 | sKernelWakelockUpdateVersion++; |
| 138 | while (endIndex < len) { |
| 139 | for (endIndex=startIndex; |
| 140 | endIndex < len && wlBuffer[endIndex] != '\n' && wlBuffer[endIndex] != '\0'; |
| 141 | endIndex++); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 142 | // Don't go over the end of the buffer, Process.parseProcLine might |
| 143 | // write to wlBuffer[endIndex] |
Adam Lesinski | d84ad30 | 2016-05-17 18:31:02 -0700 | [diff] [blame] | 144 | if (endIndex > (len - 1) ) { |
| 145 | break; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | String[] nameStringArray = mProcWakelocksName; |
| 149 | long[] wlData = mProcWakelocksData; |
| 150 | // Stomp out any bad characters since this is from a circular buffer |
| 151 | // A corruption is seen sometimes that results in the vm crashing |
| 152 | // This should prevent crashes and the line will probably fail to parse |
| 153 | for (int j = startIndex; j < endIndex; j++) { |
| 154 | if ((wlBuffer[j] & 0x80) != 0) wlBuffer[j] = (byte) '?'; |
| 155 | } |
| 156 | boolean parsed = Process.parseProcLine(wlBuffer, startIndex, endIndex, |
| 157 | wakeup_sources ? WAKEUP_SOURCES_FORMAT : |
| 158 | PROC_WAKELOCKS_FORMAT, |
| 159 | nameStringArray, wlData, null); |
| 160 | |
| 161 | name = nameStringArray[0]; |
| 162 | count = (int) wlData[1]; |
| 163 | |
| 164 | if (wakeup_sources) { |
| 165 | // convert milliseconds to microseconds |
| 166 | totalTime = wlData[2] * 1000; |
| 167 | } else { |
| 168 | // convert nanoseconds to microseconds with rounding. |
| 169 | totalTime = (wlData[2] + 500) / 1000; |
| 170 | } |
| 171 | |
| 172 | if (parsed && name.length() > 0) { |
| 173 | if (!staleStats.containsKey(name)) { |
| 174 | staleStats.put(name, new KernelWakelockStats.Entry(count, totalTime, |
| 175 | sKernelWakelockUpdateVersion)); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 176 | } else { |
| 177 | KernelWakelockStats.Entry kwlStats = staleStats.get(name); |
| 178 | if (kwlStats.mVersion == sKernelWakelockUpdateVersion) { |
| 179 | kwlStats.mCount += count; |
| 180 | kwlStats.mTotalTime += totalTime; |
| 181 | } else { |
| 182 | kwlStats.mCount = count; |
| 183 | kwlStats.mTotalTime = totalTime; |
| 184 | kwlStats.mVersion = sKernelWakelockUpdateVersion; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
Adam Lesinski | fbabe7d | 2015-08-03 14:37:38 -0700 | [diff] [blame] | 187 | } else if (!parsed) { |
| 188 | try { |
| 189 | Slog.wtf(TAG, "Failed to parse proc line: " + |
| 190 | new String(wlBuffer, startIndex, endIndex - startIndex)); |
| 191 | } catch (Exception e) { |
| 192 | Slog.wtf(TAG, "Failed to parse proc line!"); |
| 193 | } |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 194 | } |
Adam Lesinski | d84ad30 | 2016-05-17 18:31:02 -0700 | [diff] [blame] | 195 | startIndex = endIndex + 1; |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Adam Lesinski | d84ad30 | 2016-05-17 18:31:02 -0700 | [diff] [blame] | 198 | // Don't report old data. |
| 199 | Iterator<KernelWakelockStats.Entry> itr = staleStats.values().iterator(); |
| 200 | while (itr.hasNext()) { |
| 201 | if (itr.next().mVersion != sKernelWakelockUpdateVersion) { |
| 202 | itr.remove(); |
Adam Lesinski | 4b6bd8d | 2015-03-19 14:35:45 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | staleStats.kernelWakelockVersion = sKernelWakelockUpdateVersion; |
| 207 | return staleStats; |
| 208 | } |
| 209 | } |
| 210 | } |