Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package com.android.internal.os; |
| 18 | |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 20 | import android.os.Binder; |
| 21 | import android.os.SystemClock; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 22 | import android.os.UserHandle; |
Fyodor Kupolov | 8aa5124 | 2018-04-23 12:44:51 -0700 | [diff] [blame] | 23 | import android.text.format.DateFormat; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 24 | import android.util.ArrayMap; |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | import android.util.Pair; |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 27 | import android.util.Slog; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 28 | import android.util.SparseArray; |
| 29 | |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 30 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 31 | import com.android.internal.annotations.VisibleForTesting; |
| 32 | import com.android.internal.util.Preconditions; |
| 33 | |
| 34 | import java.io.PrintWriter; |
| 35 | import java.util.ArrayList; |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 36 | import java.util.Collections; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 37 | import java.util.Comparator; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 38 | import java.util.List; |
| 39 | import java.util.Map; |
| 40 | import java.util.Queue; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 41 | import java.util.Random; |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 42 | import java.util.Set; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 43 | import java.util.concurrent.ConcurrentLinkedQueue; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 44 | import java.util.function.ToDoubleFunction; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Collects statistics about CPU time spent per binder call across multiple dimensions, e.g. |
| 48 | * per thread, uid or call description. |
| 49 | */ |
| 50 | public class BinderCallsStats { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 51 | public static final boolean ENABLED_DEFAULT = true; |
| 52 | public static final boolean DETAILED_TRACKING_DEFAULT = true; |
| 53 | public static final int PERIODIC_SAMPLING_INTERVAL_DEFAULT = 10; |
| 54 | |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 55 | private static final String TAG = "BinderCallsStats"; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 56 | private static final int CALL_SESSIONS_POOL_SIZE = 100; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 57 | private static final int PERIODIC_SAMPLING_INTERVAL = 10; |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 58 | private static final int MAX_EXCEPTION_COUNT_SIZE = 50; |
| 59 | private static final String EXCEPTION_COUNT_OVERFLOW_NAME = "overflow"; |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 60 | private static final CallSession NOT_ENABLED = new CallSession(); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 61 | private static final BinderCallsStats sInstance = new BinderCallsStats(new Random()); |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 62 | |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 63 | private volatile boolean mEnabled = ENABLED_DEFAULT; |
| 64 | private volatile boolean mDetailedTracking = DETAILED_TRACKING_DEFAULT; |
| 65 | private volatile int mPeriodicSamplingInterval = PERIODIC_SAMPLING_INTERVAL_DEFAULT; |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 66 | @GuardedBy("mLock") |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 67 | private final SparseArray<UidEntry> mUidEntries = new SparseArray<>(); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 68 | @GuardedBy("mLock") |
| 69 | private final ArrayMap<String, Integer> mExceptionCounts = new ArrayMap<>(); |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 70 | private final Queue<CallSession> mCallSessionsPool = new ConcurrentLinkedQueue<>(); |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 71 | private final Object mLock = new Object(); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 72 | private final Random mRandom; |
Fyodor Kupolov | 8aa5124 | 2018-04-23 12:44:51 -0700 | [diff] [blame] | 73 | private long mStartTime = System.currentTimeMillis(); |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 74 | |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 75 | @VisibleForTesting // Use getInstance() instead. |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 76 | public BinderCallsStats(Random random) { |
| 77 | this.mRandom = random; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | public CallSession callStarted(Binder binder, int code) { |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 81 | return callStarted(binder.getClass().getName(), code, binder.getTransactionName(code)); |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 84 | private CallSession callStarted(String className, int code, @Nullable String methodName) { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 85 | if (!mEnabled) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 86 | return NOT_ENABLED; |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 89 | CallSession s = mCallSessionsPool.poll(); |
| 90 | if (s == null) { |
| 91 | s = new CallSession(); |
| 92 | } |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 93 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 94 | s.callStat.className = className; |
| 95 | s.callStat.msg = code; |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 96 | s.callStat.methodName = methodName; |
Olivier Gaillard | 9429bf5 | 2018-05-15 23:25:03 +0100 | [diff] [blame] | 97 | s.exceptionThrown = false; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 98 | s.cpuTimeStarted = -1; |
| 99 | s.timeStarted = -1; |
| 100 | |
| 101 | synchronized (mLock) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 102 | if (!mDetailedTracking && !shouldTrackCall()) { |
| 103 | return s; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 104 | } |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 105 | |
| 106 | s.cpuTimeStarted = getThreadTimeMicro(); |
| 107 | s.timeStarted = getElapsedRealtimeMicro(); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 108 | } |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 109 | return s; |
| 110 | } |
| 111 | |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 112 | public void callEnded(CallSession s, int parcelRequestSize, int parcelReplySize) { |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 113 | Preconditions.checkNotNull(s); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 114 | if (s == NOT_ENABLED) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 115 | return; |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | processCallEnded(s, parcelRequestSize, parcelReplySize); |
| 119 | |
| 120 | if (mCallSessionsPool.size() < CALL_SESSIONS_POOL_SIZE) { |
| 121 | mCallSessionsPool.add(s); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private void processCallEnded(CallSession s, int parcelRequestSize, int parcelReplySize) { |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 126 | synchronized (mLock) { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 127 | if (!mEnabled) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 128 | return; |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 131 | final int callingUid = getCallingUid(); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 132 | UidEntry uidEntry = mUidEntries.get(callingUid); |
| 133 | if (uidEntry == null) { |
| 134 | uidEntry = new UidEntry(callingUid); |
| 135 | mUidEntries.put(callingUid, uidEntry); |
| 136 | } |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 137 | uidEntry.callCount++; |
| 138 | CallStat callStat = uidEntry.getOrCreate(s.callStat); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 139 | callStat.callCount++; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 140 | |
| 141 | // Non-negative time signals we need to record data for this call. |
| 142 | final boolean recordCall = s.cpuTimeStarted >= 0; |
| 143 | if (recordCall) { |
| 144 | final long duration = getThreadTimeMicro() - s.cpuTimeStarted; |
| 145 | final long latencyDuration = getElapsedRealtimeMicro() - s.timeStarted; |
| 146 | uidEntry.cpuTimeMicros += duration; |
| 147 | uidEntry.recordedCallCount++; |
| 148 | |
| 149 | callStat.recordedCallCount++; |
| 150 | callStat.methodName = s.callStat.methodName; |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 151 | callStat.cpuTimeMicros += duration; |
| 152 | callStat.maxCpuTimeMicros = Math.max(callStat.maxCpuTimeMicros, duration); |
| 153 | callStat.latencyMicros += latencyDuration; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 154 | callStat.maxLatencyMicros = |
| 155 | Math.max(callStat.maxLatencyMicros, latencyDuration); |
| 156 | if (mDetailedTracking) { |
| 157 | callStat.exceptionCount += s.exceptionThrown ? 1 : 0; |
| 158 | callStat.maxRequestSizeBytes = |
| 159 | Math.max(callStat.maxRequestSizeBytes, parcelRequestSize); |
| 160 | callStat.maxReplySizeBytes = |
| 161 | Math.max(callStat.maxReplySizeBytes, parcelReplySize); |
| 162 | } |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 163 | } |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 164 | } |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Olivier Gaillard | 9429bf5 | 2018-05-15 23:25:03 +0100 | [diff] [blame] | 167 | /** |
| 168 | * Called if an exception is thrown while executing the binder transaction. |
| 169 | * |
| 170 | * <li>BinderCallsStats#callEnded will be called afterwards. |
| 171 | * <li>Do not throw an exception in this method, it will swallow the original exception thrown |
| 172 | * by the binder transaction. |
| 173 | */ |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 174 | public void callThrewException(CallSession s, Exception exception) { |
Olivier Gaillard | 9429bf5 | 2018-05-15 23:25:03 +0100 | [diff] [blame] | 175 | Preconditions.checkNotNull(s); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 176 | if (!mEnabled) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 177 | return; |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 178 | } |
Olivier Gaillard | 9429bf5 | 2018-05-15 23:25:03 +0100 | [diff] [blame] | 179 | s.exceptionThrown = true; |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 180 | try { |
| 181 | String className = exception.getClass().getName(); |
| 182 | synchronized (mLock) { |
| 183 | if (mExceptionCounts.size() >= MAX_EXCEPTION_COUNT_SIZE) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 184 | className = EXCEPTION_COUNT_OVERFLOW_NAME; |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 185 | } |
| 186 | Integer count = mExceptionCounts.get(className); |
| 187 | mExceptionCounts.put(className, count == null ? 1 : count + 1); |
| 188 | } |
| 189 | } catch (RuntimeException e) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 190 | // Do not propagate the exception. We do not want to swallow original exception. |
| 191 | Log.wtf(TAG, "Unexpected exception while updating mExceptionCounts", e); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 192 | } |
Olivier Gaillard | 9429bf5 | 2018-05-15 23:25:03 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 195 | public ArrayList<ExportedCallStat> getExportedCallStats() { |
| 196 | // We do not collect all the data if detailed tracking is off. |
| 197 | if (!mDetailedTracking) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 198 | return new ArrayList<ExportedCallStat>(); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | ArrayList<ExportedCallStat> resultCallStats = new ArrayList<>(); |
| 202 | synchronized (mLock) { |
| 203 | int uidEntriesSize = mUidEntries.size(); |
| 204 | for (int entryIdx = 0; entryIdx < uidEntriesSize; entryIdx++){ |
| 205 | UidEntry entry = mUidEntries.valueAt(entryIdx); |
| 206 | for (CallStat stat : entry.getCallStatsList()) { |
| 207 | ExportedCallStat exported = new ExportedCallStat(); |
| 208 | exported.uid = entry.uid; |
| 209 | exported.className = stat.className; |
| 210 | exported.methodName = stat.methodName == null |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 211 | ? String.valueOf(stat.msg) : stat.methodName; |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 212 | exported.cpuTimeMicros = stat.cpuTimeMicros; |
| 213 | exported.maxCpuTimeMicros = stat.maxCpuTimeMicros; |
| 214 | exported.latencyMicros = stat.latencyMicros; |
| 215 | exported.maxLatencyMicros = stat.maxLatencyMicros; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 216 | exported.recordedCallCount = stat.recordedCallCount; |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 217 | exported.callCount = stat.callCount; |
| 218 | exported.maxRequestSizeBytes = stat.maxRequestSizeBytes; |
| 219 | exported.maxReplySizeBytes = stat.maxReplySizeBytes; |
| 220 | exported.exceptionCount = stat.exceptionCount; |
| 221 | resultCallStats.add(exported); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return resultCallStats; |
| 227 | } |
| 228 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 229 | public void dump(PrintWriter pw, Map<Integer,String> appIdToPkgNameMap, boolean verbose) { |
| 230 | synchronized (mLock) { |
| 231 | dumpLocked(pw, appIdToPkgNameMap, verbose); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | private void dumpLocked(PrintWriter pw, Map<Integer,String> appIdToPkgNameMap, boolean verbose) { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 236 | if (!mEnabled) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 237 | pw.println("Binder calls stats disabled."); |
| 238 | return; |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 241 | long totalCallsCount = 0; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 242 | long totalRecordedCallsCount = 0; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 243 | long totalCpuTime = 0; |
Fyodor Kupolov | 8aa5124 | 2018-04-23 12:44:51 -0700 | [diff] [blame] | 244 | pw.print("Start time: "); |
| 245 | pw.println(DateFormat.format("yyyy-MM-dd HH:mm:ss", mStartTime)); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 246 | pw.println("Sampling interval period: " + mPeriodicSamplingInterval); |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 247 | List<UidEntry> entries = new ArrayList<>(); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 248 | |
| 249 | int uidEntriesSize = mUidEntries.size(); |
| 250 | for (int i = 0; i < uidEntriesSize; i++) { |
| 251 | UidEntry e = mUidEntries.valueAt(i); |
| 252 | entries.add(e); |
| 253 | totalCpuTime += e.cpuTimeMicros; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 254 | totalRecordedCallsCount += e.recordedCallCount; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 255 | totalCallsCount += e.callCount; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 256 | } |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 257 | |
| 258 | entries.sort(Comparator.<UidEntry>comparingDouble(value -> value.cpuTimeMicros).reversed()); |
| 259 | String datasetSizeDesc = verbose ? "" : "(top 90% by cpu time) "; |
| 260 | StringBuilder sb = new StringBuilder(); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 261 | List<UidEntry> topEntries = verbose ? entries |
| 262 | : getHighestValues(entries, value -> value.cpuTimeMicros, 0.9); |
| 263 | pw.println("Per-UID raw data " + datasetSizeDesc |
| 264 | + "(package/uid, call_desc, cpu_time_micros, max_cpu_time_micros, " |
| 265 | + "latency_time_micros, max_latency_time_micros, exception_count, " |
| 266 | + "max_request_size_bytes, max_reply_size_bytes, recorded_call_count, " |
| 267 | + "call_count):"); |
| 268 | for (UidEntry uidEntry : topEntries) { |
| 269 | for (CallStat e : uidEntry.getCallStatsList()) { |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 270 | sb.setLength(0); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 271 | sb.append(" ") |
| 272 | .append(uidToString(uidEntry.uid, appIdToPkgNameMap)) |
| 273 | .append(',').append(e) |
| 274 | .append(',').append(e.cpuTimeMicros) |
| 275 | .append(',').append(e.maxCpuTimeMicros) |
| 276 | .append(',').append(e.latencyMicros) |
| 277 | .append(',').append(e.maxLatencyMicros) |
| 278 | .append(',').append(mDetailedTracking ? e.exceptionCount : '_') |
| 279 | .append(',').append(mDetailedTracking ? e.maxRequestSizeBytes : '_') |
| 280 | .append(',').append(mDetailedTracking ? e.maxReplySizeBytes : '_') |
| 281 | .append(',').append(e.recordedCallCount) |
| 282 | .append(',').append(e.callCount); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 283 | pw.println(sb); |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 284 | } |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 285 | } |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 286 | pw.println(); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 287 | pw.println("Per-UID Summary " + datasetSizeDesc |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 288 | + "(cpu_time, % of total cpu_time, recorded_call_count, call_count, package/uid):"); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 289 | List<UidEntry> summaryEntries = verbose ? entries |
| 290 | : getHighestValues(entries, value -> value.cpuTimeMicros, 0.9); |
| 291 | for (UidEntry entry : summaryEntries) { |
| 292 | String uidStr = uidToString(entry.uid, appIdToPkgNameMap); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 293 | pw.println(String.format(" %10d %3.0f%% %8d %8d %s", |
| 294 | entry.cpuTimeMicros, 100d * entry.cpuTimeMicros / totalCpuTime, |
| 295 | entry.recordedCallCount, entry.callCount, uidStr)); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 296 | } |
| 297 | pw.println(); |
| 298 | pw.println(String.format(" Summary: total_cpu_time=%d, " |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 299 | + "calls_count=%d, avg_call_cpu_time=%.0f", |
| 300 | totalCpuTime, totalCallsCount, (double)totalCpuTime / totalRecordedCallsCount)); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 301 | pw.println(); |
| 302 | |
| 303 | pw.println("Exceptions thrown (exception_count, class_name):"); |
| 304 | List<Pair<String, Integer>> exceptionEntries = new ArrayList<>(); |
| 305 | // We cannot use new ArrayList(Collection) constructor because MapCollections does not |
| 306 | // implement toArray method. |
| 307 | mExceptionCounts.entrySet().iterator().forEachRemaining( |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 308 | (e) -> exceptionEntries.add(Pair.create(e.getKey(), e.getValue()))); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 309 | exceptionEntries.sort((e1, e2) -> Integer.compare(e2.second, e1.second)); |
| 310 | for (Pair<String, Integer> entry : exceptionEntries) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 311 | pw.println(String.format(" %6d %s", entry.second, entry.first)); |
| 312 | } |
| 313 | |
| 314 | if (!mDetailedTracking && mPeriodicSamplingInterval != 1) { |
| 315 | pw.println(""); |
| 316 | pw.println("/!\\ Displayed data is sampled. See sampling interval at the top."); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 317 | } |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 320 | private static String uidToString(int uid, Map<Integer, String> pkgNameMap) { |
| 321 | int appId = UserHandle.getAppId(uid); |
| 322 | String pkgName = pkgNameMap == null ? null : pkgNameMap.get(appId); |
| 323 | String uidStr = UserHandle.formatUid(uid); |
| 324 | return pkgName == null ? uidStr : pkgName + '/' + uidStr; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 327 | protected long getThreadTimeMicro() { |
| 328 | return SystemClock.currentThreadTimeMicro(); |
| 329 | } |
| 330 | |
| 331 | protected int getCallingUid() { |
| 332 | return Binder.getCallingUid(); |
| 333 | } |
| 334 | |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 335 | protected long getElapsedRealtimeMicro() { |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 336 | return SystemClock.elapsedRealtimeNanos() / 1000; |
Olivier Gaillard | 121988e | 2018-05-15 20:49:47 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 339 | private boolean shouldTrackCall() { |
| 340 | return mRandom.nextInt() % mPeriodicSamplingInterval == 0; |
| 341 | } |
| 342 | |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 343 | public static BinderCallsStats getInstance() { |
| 344 | return sInstance; |
| 345 | } |
| 346 | |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 347 | public void setDetailedTracking(boolean enabled) { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 348 | synchronized (mLock) { |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 349 | if (enabled != mDetailedTracking) { |
| 350 | mDetailedTracking = enabled; |
| 351 | reset(); |
| 352 | } |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | public void setEnabled(boolean enabled) { |
| 357 | synchronized (mLock) { |
| 358 | if (enabled != mEnabled) { |
| 359 | mEnabled = enabled; |
| 360 | reset(); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | public void setSamplingInterval(int samplingInterval) { |
| 366 | synchronized (mLock) { |
| 367 | if (samplingInterval != mPeriodicSamplingInterval) { |
| 368 | mPeriodicSamplingInterval = samplingInterval; |
| 369 | reset(); |
| 370 | } |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 371 | } |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 374 | public void reset() { |
| 375 | synchronized (mLock) { |
| 376 | mUidEntries.clear(); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 377 | mExceptionCounts.clear(); |
Fyodor Kupolov | 8aa5124 | 2018-04-23 12:44:51 -0700 | [diff] [blame] | 378 | mStartTime = System.currentTimeMillis(); |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 379 | } |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 382 | /** |
| 383 | * Aggregated data by uid/class/method to be sent through WestWorld. |
| 384 | */ |
| 385 | public static class ExportedCallStat { |
| 386 | public int uid; |
| 387 | public String className; |
| 388 | public String methodName; |
| 389 | public long cpuTimeMicros; |
| 390 | public long maxCpuTimeMicros; |
| 391 | public long latencyMicros; |
| 392 | public long maxLatencyMicros; |
| 393 | public long callCount; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 394 | public long recordedCallCount; |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 395 | public long maxRequestSizeBytes; |
| 396 | public long maxReplySizeBytes; |
| 397 | public long exceptionCount; |
| 398 | } |
| 399 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 400 | @VisibleForTesting |
| 401 | public static class CallStat { |
| 402 | public String className; |
| 403 | public int msg; |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 404 | // Method name might be null when we cannot resolve the transaction code. For instance, if |
| 405 | // the binder was not generated by AIDL. |
| 406 | public @Nullable String methodName; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 407 | // Number of calls for which we collected data for. We do not record data for all the calls |
| 408 | // when sampling is on. |
| 409 | public long recordedCallCount; |
| 410 | // Real number of total calls. |
| 411 | public long callCount; |
| 412 | // Total CPU of all for all the recorded calls. |
| 413 | // Approximate total CPU usage can be computed by |
| 414 | // cpuTimeMicros * callCount / recordedCallCount |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 415 | public long cpuTimeMicros; |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 416 | public long maxCpuTimeMicros; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 417 | // Total latency of all for all the recorded calls. |
| 418 | // Approximate average latency can be computed by |
| 419 | // latencyMicros * callCount / recordedCallCount |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 420 | public long latencyMicros; |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 421 | public long maxLatencyMicros; |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 422 | // The following fields are only computed if mDetailedTracking is set. |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 423 | public long maxRequestSizeBytes; |
| 424 | public long maxReplySizeBytes; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 425 | public long exceptionCount; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 426 | |
| 427 | CallStat() { |
| 428 | } |
| 429 | |
| 430 | CallStat(String className, int msg) { |
| 431 | this.className = className; |
| 432 | this.msg = msg; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public boolean equals(Object o) { |
| 437 | if (this == o) { |
| 438 | return true; |
| 439 | } |
| 440 | |
| 441 | CallStat callStat = (CallStat) o; |
Fyodor Kupolov | 3f3af61 | 2018-04-18 17:26:43 -0700 | [diff] [blame] | 442 | return msg == callStat.msg && (className.equals(callStat.className)); |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public int hashCode() { |
| 447 | int result = className.hashCode(); |
| 448 | result = 31 * result + msg; |
| 449 | return result; |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public String toString() { |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 454 | return className + "#" + (methodName == null ? msg : methodName); |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| 458 | public static class CallSession { |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 459 | long cpuTimeStarted; |
| 460 | long timeStarted; |
Olivier Gaillard | 9429bf5 | 2018-05-15 23:25:03 +0100 | [diff] [blame] | 461 | boolean exceptionThrown; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 462 | final CallStat callStat = new CallStat(); |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 465 | @VisibleForTesting |
| 466 | public static class UidEntry { |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 467 | int uid; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 468 | // Number of calls for which we collected data for. We do not record data for all the calls |
| 469 | // when sampling is on. |
| 470 | public long recordedCallCount; |
| 471 | // Real number of total calls. |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 472 | public long callCount; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 473 | // Total CPU of all for all the recorded calls. |
| 474 | // Approximate total CPU usage can be computed by |
| 475 | // cpuTimeMicros * callCount / recordedCallCount |
| 476 | public long cpuTimeMicros; |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 477 | |
| 478 | UidEntry(int uid) { |
| 479 | this.uid = uid; |
| 480 | } |
| 481 | |
| 482 | // Aggregate time spent per each call name: call_desc -> cpu_time_micros |
| 483 | Map<CallStat, CallStat> mCallStats = new ArrayMap<>(); |
| 484 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 485 | CallStat getOrCreate(CallStat callStat) { |
| 486 | CallStat mapCallStat = mCallStats.get(callStat); |
| 487 | // Only create CallStat if it's a new entry, otherwise update existing instance |
| 488 | if (mapCallStat == null) { |
| 489 | mapCallStat = new CallStat(callStat.className, callStat.msg); |
| 490 | mCallStats.put(mapCallStat, mapCallStat); |
| 491 | } |
| 492 | return mapCallStat; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Returns list of calls sorted by CPU time |
| 497 | */ |
| 498 | public List<CallStat> getCallStatsList() { |
| 499 | List<CallStat> callStats = new ArrayList<>(mCallStats.keySet()); |
| 500 | callStats.sort((o1, o2) -> { |
| 501 | if (o1.cpuTimeMicros < o2.cpuTimeMicros) { |
| 502 | return 1; |
| 503 | } else if (o1.cpuTimeMicros > o2.cpuTimeMicros) { |
| 504 | return -1; |
| 505 | } |
| 506 | return 0; |
| 507 | }); |
| 508 | return callStats; |
| 509 | } |
| 510 | |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 511 | @Override |
| 512 | public String toString() { |
| 513 | return "UidEntry{" + |
Olivier Gaillard | 121988e | 2018-05-15 20:49:47 +0100 | [diff] [blame] | 514 | "cpuTimeMicros=" + cpuTimeMicros + |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 515 | ", callCount=" + callCount + |
| 516 | ", mCallStats=" + mCallStats + |
| 517 | '}'; |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public boolean equals(Object o) { |
| 522 | if (this == o) { |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | UidEntry uidEntry = (UidEntry) o; |
| 527 | return uid == uidEntry.uid; |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public int hashCode() { |
| 532 | return uid; |
| 533 | } |
| 534 | } |
| 535 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 536 | @VisibleForTesting |
| 537 | public SparseArray<UidEntry> getUidEntries() { |
| 538 | return mUidEntries; |
| 539 | } |
| 540 | |
| 541 | @VisibleForTesting |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 542 | public ArrayMap<String, Integer> getExceptionCounts() { |
| 543 | return mExceptionCounts; |
| 544 | } |
| 545 | |
| 546 | @VisibleForTesting |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 547 | public static <T> List<T> getHighestValues(List<T> list, ToDoubleFunction<T> toDouble, |
| 548 | double percentile) { |
| 549 | List<T> sortedList = new ArrayList<>(list); |
| 550 | sortedList.sort(Comparator.comparingDouble(toDouble).reversed()); |
| 551 | double total = 0; |
| 552 | for (T item : list) { |
| 553 | total += toDouble.applyAsDouble(item); |
| 554 | } |
| 555 | List<T> result = new ArrayList<>(); |
| 556 | double runningSum = 0; |
| 557 | for (T item : sortedList) { |
| 558 | if (runningSum > percentile * total) { |
| 559 | break; |
| 560 | } |
| 561 | result.add(item); |
| 562 | runningSum += toDouble.applyAsDouble(item); |
| 563 | } |
| 564 | return result; |
| 565 | } |
| 566 | |
Fyodor Kupolov | ca34851 | 2018-01-10 18:05:53 -0800 | [diff] [blame] | 567 | } |