Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | 94e6fe6 | 2018-08-06 16:43:47 +0100 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
Marcin Oczeretko | 772e8f2 | 2018-11-21 13:00:32 +0000 | [diff] [blame] | 20 | import static org.junit.Assert.assertTrue; |
Olivier Gaillard | 94e6fe6 | 2018-08-06 16:43:47 +0100 | [diff] [blame] | 21 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 22 | import android.os.Binder; |
Olivier Gaillard | 28109b5 | 2018-12-14 15:14:14 +0000 | [diff] [blame] | 23 | import android.os.SystemClock; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 24 | import android.platform.test.annotations.Presubmit; |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 25 | import android.util.ArrayMap; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 26 | import android.util.SparseArray; |
| 27 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 28 | import androidx.test.filters.SmallTest; |
| 29 | import androidx.test.runner.AndroidJUnit4; |
| 30 | |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 31 | import com.android.internal.os.BinderInternal.CallSession; |
| 32 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 33 | import org.junit.Assert; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 37 | import java.io.PrintWriter; |
| 38 | import java.io.StringWriter; |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 39 | import java.util.ArrayList; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 40 | import java.util.Arrays; |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 41 | import java.util.HashMap; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 42 | import java.util.List; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 43 | import java.util.Random; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 44 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 45 | @SmallTest |
| 46 | @RunWith(AndroidJUnit4.class) |
| 47 | @Presubmit |
| 48 | public class BinderCallsStatsTest { |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 49 | private static final int WORKSOURCE_UID = 1; |
| 50 | private static final int CALLING_UID = 2; |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 51 | private static final int REQUEST_SIZE = 2; |
| 52 | private static final int REPLY_SIZE = 3; |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 53 | private final CachedDeviceState mDeviceState = new CachedDeviceState(false, true); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 54 | |
| 55 | @Test |
| 56 | public void testDetailedOff() { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 57 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 58 | bcs.setDetailedTracking(false); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 59 | bcs.setSamplingInterval(5); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 60 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 61 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 62 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 63 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 64 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 65 | |
| 66 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 67 | assertEquals(1, uidEntries.size()); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 68 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 69 | Assert.assertNotNull(uidEntry); |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 70 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 71 | assertEquals(1, uidEntry.callCount); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 72 | assertEquals(1, uidEntry.recordedCallCount); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 73 | assertEquals(10, uidEntry.cpuTimeMicros); |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 74 | assertEquals(binder.getClass(), callStatsList.get(0).binderClass); |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 75 | assertEquals(1, callStatsList.get(0).transactionCode); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 76 | |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 77 | // CPU usage is sampled, should not be tracked here. |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 78 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 79 | bcs.time += 20; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 80 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 81 | assertEquals(2, uidEntry.callCount); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 82 | assertEquals(1, uidEntry.recordedCallCount); |
| 83 | assertEquals(10, uidEntry.cpuTimeMicros); |
| 84 | assertEquals(1, callStatsList.size()); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 85 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 86 | callSession = bcs.callStarted(binder, 2, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 87 | bcs.time += 50; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 88 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 89 | uidEntry = bcs.getUidEntries().get(WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 90 | assertEquals(3, uidEntry.callCount); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 91 | assertEquals(1, uidEntry.recordedCallCount); |
| 92 | // Still sampled even for another API. |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 93 | callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
Olivier Gaillard | 34ad8af | 2018-08-06 15:55:03 +0100 | [diff] [blame] | 94 | assertEquals(1, callStatsList.size()); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testDetailedOn() { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 99 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 100 | bcs.setDetailedTracking(true); |
| 101 | |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 102 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 103 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 104 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 105 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 106 | |
| 107 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 108 | assertEquals(1, uidEntries.size()); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 109 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 110 | Assert.assertNotNull(uidEntry); |
| 111 | assertEquals(1, uidEntry.callCount); |
| 112 | assertEquals(10, uidEntry.cpuTimeMicros); |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 113 | assertEquals(1, new ArrayList(uidEntry.getCallStatsList()).size()); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 114 | |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 115 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 116 | assertEquals(1, callStatsList.get(0).callCount); |
| 117 | assertEquals(10, callStatsList.get(0).cpuTimeMicros); |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 118 | assertEquals(binder.getClass(), callStatsList.get(0).binderClass); |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 119 | assertEquals(1, callStatsList.get(0).transactionCode); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 120 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 121 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 122 | bcs.time += 20; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 123 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 124 | |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 125 | uidEntry = bcs.getUidEntries().get(WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 126 | assertEquals(2, uidEntry.callCount); |
| 127 | assertEquals(30, uidEntry.cpuTimeMicros); |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 128 | callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 129 | assertEquals(1, callStatsList.size()); |
| 130 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 131 | callSession = bcs.callStarted(binder, 2, WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 132 | bcs.time += 50; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 133 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 134 | uidEntry = bcs.getUidEntries().get(WORKSOURCE_UID); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 135 | assertEquals(3, uidEntry.callCount); |
| 136 | |
| 137 | // This is the first transaction of a new type, so the real CPU time will be measured |
| 138 | assertEquals(80, uidEntry.cpuTimeMicros); |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 139 | callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 140 | assertEquals(2, callStatsList.size()); |
| 141 | } |
| 142 | |
| 143 | @Test |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 144 | public void testEnableInBetweenCall() { |
| 145 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 146 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 147 | bcs.callEnded(null, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 148 | |
| 149 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 150 | assertEquals(0, uidEntries.size()); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testInBetweenCallWhenExceptionThrown() { |
| 155 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 156 | Binder binder = new Binder(); |
| 157 | bcs.callThrewException(null, new IllegalStateException()); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 158 | bcs.callEnded(null, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 159 | |
| 160 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 161 | assertEquals(0, uidEntries.size()); |
| 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testSampling() { |
| 166 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 167 | bcs.setDetailedTracking(false); |
| 168 | bcs.setSamplingInterval(2); |
| 169 | |
| 170 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 171 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 172 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 173 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 174 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 175 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 176 | bcs.time += 1000; // shoud be ignored. |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 177 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 178 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 179 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 180 | bcs.time += 50; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 181 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 182 | |
| 183 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 184 | assertEquals(1, uidEntries.size()); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 185 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 186 | Assert.assertNotNull(uidEntry); |
| 187 | assertEquals(3, uidEntry.callCount); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 188 | assertEquals(60 /* 10 + 50 */, uidEntry.cpuTimeMicros); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 189 | |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 190 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 191 | assertEquals(1, callStatsList.size()); |
| 192 | BinderCallsStats.CallStat callStats = callStatsList.get(0); |
| 193 | assertEquals(3, callStats.callCount); |
| 194 | assertEquals(2, callStats.recordedCallCount); |
| 195 | assertEquals(60, callStats.cpuTimeMicros); |
| 196 | assertEquals(50, callStats.maxCpuTimeMicros); |
| 197 | assertEquals(0, callStats.maxRequestSizeBytes); |
| 198 | assertEquals(0, callStats.maxReplySizeBytes); |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void testSamplingWithDifferentApis() { |
| 203 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 204 | bcs.setDetailedTracking(false); |
| 205 | bcs.setSamplingInterval(2); |
| 206 | |
| 207 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 208 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 209 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 210 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 211 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 212 | callSession = bcs.callStarted(binder, 2 /* another method */, WORKSOURCE_UID); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 213 | bcs.time += 1000; // shoud be ignored. |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 214 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 215 | |
| 216 | |
| 217 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 218 | assertEquals(1, uidEntries.size()); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 219 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 220 | assertEquals(2, uidEntry.callCount); |
| 221 | assertEquals(1, uidEntry.recordedCallCount); |
| 222 | assertEquals(10, uidEntry.cpuTimeMicros); |
| 223 | |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 224 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
Olivier Gaillard | 34ad8af | 2018-08-06 15:55:03 +0100 | [diff] [blame] | 225 | assertEquals(1, callStatsList.size()); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 226 | |
| 227 | BinderCallsStats.CallStat callStats = callStatsList.get(0); |
| 228 | assertEquals(1, callStats.callCount); |
| 229 | assertEquals(1, callStats.recordedCallCount); |
| 230 | assertEquals(10, callStats.cpuTimeMicros); |
| 231 | assertEquals(10, callStats.maxCpuTimeMicros); |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Olivier Gaillard | 1f93a77 | 2018-07-30 14:09:22 +0100 | [diff] [blame] | 234 | private static class BinderWithGetTransactionName extends Binder { |
| 235 | public static String getDefaultTransactionName(int code) { |
| 236 | return "resolved"; |
| 237 | } |
| 238 | } |
| 239 | |
Olivier Gaillard | f31dfb9 | 2018-07-31 12:59:20 +0100 | [diff] [blame] | 240 | private static class AnotherBinderWithGetTransactionName extends Binder { |
| 241 | public static String getDefaultTransactionName(int code) { |
| 242 | return "foo" + code; |
| 243 | } |
| 244 | } |
| 245 | |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 246 | @Test |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 247 | public void testTransactionCodeResolved() { |
| 248 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 249 | bcs.setDetailedTracking(true); |
Olivier Gaillard | 1f93a77 | 2018-07-30 14:09:22 +0100 | [diff] [blame] | 250 | Binder binder = new BinderWithGetTransactionName(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 251 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 252 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 253 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 254 | |
Olivier Gaillard | 1f93a77 | 2018-07-30 14:09:22 +0100 | [diff] [blame] | 255 | List<BinderCallsStats.ExportedCallStat> callStatsList = |
| 256 | bcs.getExportedCallStats(); |
Olivier Gaillard | 103aae2 | 2018-06-07 18:20:10 +0100 | [diff] [blame] | 257 | assertEquals("resolved", callStatsList.get(0).methodName); |
| 258 | } |
| 259 | |
| 260 | @Test |
Olivier Gaillard | f31dfb9 | 2018-07-31 12:59:20 +0100 | [diff] [blame] | 261 | public void testMultipleTransactionCodeResolved() { |
| 262 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 263 | bcs.setDetailedTracking(true); |
| 264 | |
| 265 | Binder binder = new AnotherBinderWithGetTransactionName(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 266 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | f31dfb9 | 2018-07-31 12:59:20 +0100 | [diff] [blame] | 267 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 268 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | f31dfb9 | 2018-07-31 12:59:20 +0100 | [diff] [blame] | 269 | |
| 270 | Binder binder2 = new BinderWithGetTransactionName(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 271 | callSession = bcs.callStarted(binder2, 1, WORKSOURCE_UID); |
Olivier Gaillard | f31dfb9 | 2018-07-31 12:59:20 +0100 | [diff] [blame] | 272 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 273 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | f31dfb9 | 2018-07-31 12:59:20 +0100 | [diff] [blame] | 274 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 275 | callSession = bcs.callStarted(binder, 2, WORKSOURCE_UID); |
Olivier Gaillard | f31dfb9 | 2018-07-31 12:59:20 +0100 | [diff] [blame] | 276 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 277 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | f31dfb9 | 2018-07-31 12:59:20 +0100 | [diff] [blame] | 278 | |
| 279 | List<BinderCallsStats.ExportedCallStat> callStatsList = |
| 280 | bcs.getExportedCallStats(); |
| 281 | assertEquals("foo1", callStatsList.get(0).methodName); |
| 282 | assertEquals(AnotherBinderWithGetTransactionName.class.getName(), |
| 283 | callStatsList.get(0).className); |
| 284 | assertEquals("foo2", callStatsList.get(1).methodName); |
| 285 | assertEquals(AnotherBinderWithGetTransactionName.class.getName(), |
| 286 | callStatsList.get(1).className); |
| 287 | assertEquals("resolved", callStatsList.get(2).methodName); |
| 288 | assertEquals(BinderWithGetTransactionName.class.getName(), |
| 289 | callStatsList.get(2).className); |
| 290 | } |
| 291 | |
| 292 | @Test |
Olivier Gaillard | 1f93a77 | 2018-07-30 14:09:22 +0100 | [diff] [blame] | 293 | public void testResolvingCodeDoesNotThrowWhenMethodNotPresent() { |
| 294 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 295 | bcs.setDetailedTracking(true); |
| 296 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 297 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 1f93a77 | 2018-07-30 14:09:22 +0100 | [diff] [blame] | 298 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 299 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 1f93a77 | 2018-07-30 14:09:22 +0100 | [diff] [blame] | 300 | |
| 301 | List<BinderCallsStats.ExportedCallStat> callStatsList = |
| 302 | bcs.getExportedCallStats(); |
| 303 | assertEquals("1", callStatsList.get(0).methodName); |
| 304 | } |
| 305 | |
| 306 | @Test |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 307 | public void testParcelSize() { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 308 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 309 | bcs.setDetailedTracking(true); |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 310 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 311 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 312 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 313 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 314 | |
| 315 | List<BinderCallsStats.CallStat> callStatsList = |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 316 | new ArrayList(bcs.getUidEntries().get(WORKSOURCE_UID).getCallStatsList()); |
Olivier Gaillard | 58b56e3 | 2018-06-01 16:18:43 +0100 | [diff] [blame] | 317 | |
| 318 | assertEquals(REQUEST_SIZE, callStatsList.get(0).maxRequestSizeBytes); |
| 319 | assertEquals(REPLY_SIZE, callStatsList.get(0).maxReplySizeBytes); |
| 320 | } |
| 321 | |
| 322 | @Test |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 323 | public void testMaxCpu() { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 324 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 325 | bcs.setDetailedTracking(true); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 326 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 327 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 328 | bcs.time += 50; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 329 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 330 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 331 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 332 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 333 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 334 | |
| 335 | List<BinderCallsStats.CallStat> callStatsList = |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 336 | new ArrayList(bcs.getUidEntries().get(WORKSOURCE_UID).getCallStatsList()); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 337 | |
| 338 | assertEquals(50, callStatsList.get(0).maxCpuTimeMicros); |
| 339 | } |
| 340 | |
| 341 | @Test |
| 342 | public void testMaxLatency() { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 343 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 344 | bcs.setDetailedTracking(true); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 345 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 346 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 347 | bcs.elapsedTime += 5; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 348 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 349 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 350 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 351 | bcs.elapsedTime += 1; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 352 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 353 | |
| 354 | List<BinderCallsStats.CallStat> callStatsList = |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 355 | new ArrayList(bcs.getUidEntries().get(WORKSOURCE_UID).getCallStatsList()); |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 356 | |
| 357 | assertEquals(5, callStatsList.get(0).maxLatencyMicros); |
| 358 | } |
| 359 | |
| 360 | @Test |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 361 | public void testGetHighestValues() { |
| 362 | List<Integer> list = Arrays.asList(1, 2, 3, 4); |
| 363 | List<Integer> highestValues = BinderCallsStats |
| 364 | .getHighestValues(list, value -> value, 0.8); |
| 365 | assertEquals(Arrays.asList(4, 3, 2), highestValues); |
| 366 | } |
| 367 | |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 368 | @Test |
| 369 | public void testExceptionCount() { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 370 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 371 | bcs.setDetailedTracking(true); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 372 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 373 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 374 | bcs.callThrewException(callSession, new IllegalStateException()); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 375 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 376 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 377 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 378 | bcs.callThrewException(callSession, new IllegalStateException()); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 379 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 380 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 381 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 382 | bcs.callThrewException(callSession, new RuntimeException()); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 383 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 384 | |
| 385 | ArrayMap<String, Integer> expected = new ArrayMap<>(); |
| 386 | expected.put("java.lang.IllegalStateException", 2); |
| 387 | expected.put("java.lang.RuntimeException", 1); |
| 388 | assertEquals(expected, bcs.getExceptionCounts()); |
| 389 | } |
| 390 | |
| 391 | @Test |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 392 | public void testNoDataCollectedBeforeInitialDeviceStateSet() { |
Marcin Oczeretko | 6a2e524 | 2018-11-28 11:08:50 +0000 | [diff] [blame] | 393 | TestBinderCallsStats bcs = new TestBinderCallsStats(null); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 394 | bcs.setDetailedTracking(true); |
| 395 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 396 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 397 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 398 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 399 | |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 400 | bcs.setDeviceState(mDeviceState.getReadonlyClient()); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 401 | |
| 402 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 403 | assertEquals(0, uidEntries.size()); |
| 404 | } |
| 405 | |
| 406 | @Test |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 407 | public void testNoDataCollectedOnCharger() { |
| 408 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 409 | bcs.setDetailedTracking(true); |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 410 | mDeviceState.setCharging(true); |
| 411 | |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 412 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 413 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 414 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 415 | |
| 416 | assertEquals(0, bcs.getUidEntries().size()); |
| 417 | } |
| 418 | |
| 419 | @Test |
| 420 | public void testScreenOff() { |
| 421 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 422 | bcs.setDetailedTracking(true); |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 423 | mDeviceState.setScreenInteractive(false); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 424 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 425 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 426 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 427 | |
| 428 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 429 | assertEquals(1, uidEntries.size()); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 430 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 431 | Assert.assertNotNull(uidEntry); |
| 432 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
| 433 | assertEquals(false, callStatsList.get(0).screenInteractive); |
| 434 | } |
| 435 | |
| 436 | @Test |
| 437 | public void testScreenOn() { |
| 438 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 439 | bcs.setDetailedTracking(true); |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 440 | mDeviceState.setScreenInteractive(true); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 441 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 442 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 443 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 444 | |
| 445 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 446 | assertEquals(1, uidEntries.size()); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 447 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 448 | Assert.assertNotNull(uidEntry); |
| 449 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
| 450 | assertEquals(true, callStatsList.get(0).screenInteractive); |
| 451 | } |
| 452 | |
| 453 | @Test |
| 454 | public void testOnCharger() { |
| 455 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 456 | bcs.setDetailedTracking(true); |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 457 | mDeviceState.setCharging(true); |
| 458 | |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 459 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 460 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 461 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 462 | |
| 463 | assertEquals(0, bcs.getExportedCallStats().size()); |
| 464 | } |
| 465 | |
| 466 | @Test |
| 467 | public void testOnBattery() { |
| 468 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 469 | bcs.setDetailedTracking(true); |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 470 | mDeviceState.setCharging(false); |
| 471 | |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 472 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 473 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 474 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 475 | |
| 476 | assertEquals(1, bcs.getExportedCallStats().size()); |
| 477 | } |
| 478 | |
| 479 | @Test |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 480 | public void testDumpDoesNotThrowException() { |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 481 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 482 | bcs.setDetailedTracking(true); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 483 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 484 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 485 | bcs.callThrewException(callSession, new IllegalStateException()); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 486 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 487 | |
| 488 | PrintWriter pw = new PrintWriter(new StringWriter()); |
Marcin Oczeretko | c4c45a8 | 2018-12-06 15:09:49 +0000 | [diff] [blame] | 489 | bcs.dump(pw, new AppIdToPackageMap(new HashMap<>()), true); |
Olivier Gaillard | f82d2e73 | 2018-06-07 11:45:35 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 492 | @Test |
| 493 | public void testGetExportedStatsWhenDetailedTrackingDisabled() { |
| 494 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 495 | bcs.setDetailedTracking(false); |
| 496 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 497 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 498 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 499 | |
| 500 | assertEquals(0, bcs.getExportedCallStats().size()); |
| 501 | } |
| 502 | |
| 503 | @Test |
| 504 | public void testGetExportedStatsWhenDetailedTrackingEnabled() { |
| 505 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 506 | bcs.setDetailedTracking(true); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 507 | |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 508 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 509 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 510 | bcs.time += 10; |
| 511 | bcs.elapsedTime += 20; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 512 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 513 | |
| 514 | assertEquals(1, bcs.getExportedCallStats().size()); |
| 515 | BinderCallsStats.ExportedCallStat stat = bcs.getExportedCallStats().get(0); |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 516 | assertEquals(WORKSOURCE_UID, stat.workSourceUid); |
| 517 | assertEquals(CALLING_UID, stat.callingUid); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 518 | assertEquals("android.os.Binder", stat.className); |
| 519 | assertEquals("1", stat.methodName); |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 520 | assertEquals(true, stat.screenInteractive); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 521 | assertEquals(10, stat.cpuTimeMicros); |
| 522 | assertEquals(10, stat.maxCpuTimeMicros); |
| 523 | assertEquals(20, stat.latencyMicros); |
| 524 | assertEquals(20, stat.maxLatencyMicros); |
| 525 | assertEquals(1, stat.callCount); |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 526 | assertEquals(1, stat.recordedCallCount); |
Olivier Gaillard | 00bfb1b | 2018-07-10 11:25:09 +0100 | [diff] [blame] | 527 | assertEquals(REQUEST_SIZE, stat.maxRequestSizeBytes); |
| 528 | assertEquals(REPLY_SIZE, stat.maxReplySizeBytes); |
| 529 | assertEquals(0, stat.exceptionCount); |
| 530 | } |
| 531 | |
Olivier Gaillard | 7a2a98b | 2018-07-26 13:29:16 +0100 | [diff] [blame] | 532 | @Test |
| 533 | public void testGetExportedStatsWithoutCalls() { |
| 534 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 535 | Binder binder = new Binder(); |
| 536 | assertEquals(0, bcs.getExportedCallStats().size()); |
| 537 | } |
| 538 | |
| 539 | @Test |
| 540 | public void testGetExportedExceptionsWithoutCalls() { |
| 541 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 542 | Binder binder = new Binder(); |
| 543 | assertEquals(0, bcs.getExceptionCounts().size()); |
| 544 | } |
| 545 | |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 546 | @Test |
| 547 | public void testOverflow_sameEntry() { |
| 548 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 549 | bcs.setDetailedTracking(true); |
| 550 | bcs.setSamplingInterval(1); |
| 551 | bcs.setMaxBinderCallStats(2); |
| 552 | |
| 553 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 554 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 555 | bcs.time += 10; |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 556 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 557 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 558 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 559 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 560 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 561 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 562 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 563 | |
| 564 | BinderCallsStats.UidEntry uidEntry = bcs.getUidEntries().get(WORKSOURCE_UID); |
| 565 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
| 566 | assertEquals(1, callStatsList.size()); |
| 567 | BinderCallsStats.CallStat callStats = callStatsList.get(0); |
| 568 | assertEquals(3, callStats.callCount); |
| 569 | } |
| 570 | |
| 571 | @Test |
| 572 | public void testOverflow_overflowEntry() { |
| 573 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 574 | bcs.setDetailedTracking(true); |
| 575 | bcs.setSamplingInterval(1); |
| 576 | bcs.setMaxBinderCallStats(1); |
| 577 | |
| 578 | Binder binder = new Binder(); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 579 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 580 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 581 | |
Olivier Gaillard | 76c231d | 2018-12-05 12:52:08 +0000 | [diff] [blame] | 582 | callSession = bcs.callStarted(binder, 2, WORKSOURCE_UID); |
| 583 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 584 | |
Olivier Gaillard | a3686dd | 2018-12-07 11:28:07 +0000 | [diff] [blame] | 585 | // Should use the same overflow entry. |
| 586 | callSession = bcs.callStarted(binder, 3, WORKSOURCE_UID); |
| 587 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
| 588 | |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 589 | List<BinderCallsStats.ExportedCallStat> callStatsList = bcs.getExportedCallStats(); |
| 590 | assertEquals(2, callStatsList.size()); |
| 591 | BinderCallsStats.ExportedCallStat callStats = callStatsList.get(0); |
| 592 | assertEquals(1, callStats.callCount); |
| 593 | assertEquals("1", callStats.methodName); |
| 594 | assertEquals("android.os.Binder", callStats.className); |
| 595 | assertEquals(CALLING_UID, callStats.callingUid); |
| 596 | |
| 597 | callStats = callStatsList.get(1); |
Olivier Gaillard | a3686dd | 2018-12-07 11:28:07 +0000 | [diff] [blame] | 598 | assertEquals(2, callStats.callCount); |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 599 | assertEquals("-1", callStats.methodName); |
| 600 | assertEquals("com.android.internal.os.BinderCallsStats$OverflowBinder", |
| 601 | callStats.className); |
Olivier Gaillard | a3686dd | 2018-12-07 11:28:07 +0000 | [diff] [blame] | 602 | assertEquals(false , callStats.screenInteractive); |
| 603 | assertEquals(-1 , callStats.callingUid); |
| 604 | } |
| 605 | |
| 606 | @Test |
| 607 | public void testOverflow_oneOverflowEntryPerUid() { |
| 608 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 609 | bcs.setDetailedTracking(true); |
| 610 | bcs.setSamplingInterval(1); |
| 611 | bcs.setMaxBinderCallStats(1); |
| 612 | |
| 613 | Binder binder = new Binder(); |
| 614 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 615 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
| 616 | |
| 617 | callSession = bcs.callStarted(binder, 2, WORKSOURCE_UID + 1); |
| 618 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID + 1); |
| 619 | |
| 620 | // Different uids have different overflow entries. |
| 621 | callSession = bcs.callStarted(binder, 2, WORKSOURCE_UID + 2); |
| 622 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID + 2); |
| 623 | |
| 624 | List<BinderCallsStats.ExportedCallStat> callStatsList = bcs.getExportedCallStats(); |
| 625 | assertEquals(3, callStatsList.size()); |
| 626 | |
| 627 | BinderCallsStats.ExportedCallStat callStats = callStatsList.get(1); |
| 628 | assertEquals(WORKSOURCE_UID + 1, callStats.workSourceUid); |
| 629 | assertEquals(1, callStats.callCount); |
| 630 | assertEquals("com.android.internal.os.BinderCallsStats$OverflowBinder", |
| 631 | callStats.className); |
| 632 | |
| 633 | callStats = callStatsList.get(2); |
| 634 | assertEquals(WORKSOURCE_UID + 2, callStats.workSourceUid); |
| 635 | assertEquals(1, callStats.callCount); |
| 636 | assertEquals("com.android.internal.os.BinderCallsStats$OverflowBinder", |
| 637 | callStats.className); |
Olivier Gaillard | 7949061 | 2018-11-30 16:22:23 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Marcin Oczeretko | 772e8f2 | 2018-11-21 13:00:32 +0000 | [diff] [blame] | 640 | @Test |
| 641 | public void testAddsDebugEntries() { |
Olivier Gaillard | 28109b5 | 2018-12-14 15:14:14 +0000 | [diff] [blame] | 642 | long startTime = SystemClock.elapsedRealtime(); |
Marcin Oczeretko | 772e8f2 | 2018-11-21 13:00:32 +0000 | [diff] [blame] | 643 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 644 | bcs.setAddDebugEntries(true); |
Olivier Gaillard | 5d339dd | 2019-03-08 11:25:05 +0000 | [diff] [blame] | 645 | bcs.setSamplingInterval(10); |
Marcin Oczeretko | 772e8f2 | 2018-11-21 13:00:32 +0000 | [diff] [blame] | 646 | ArrayList<BinderCallsStats.ExportedCallStat> callStats = bcs.getExportedCallStats(); |
Olivier Gaillard | 5d339dd | 2019-03-08 11:25:05 +0000 | [diff] [blame] | 647 | assertEquals(4, callStats.size()); |
Marcin Oczeretko | 772e8f2 | 2018-11-21 13:00:32 +0000 | [diff] [blame] | 648 | BinderCallsStats.ExportedCallStat debugEntry1 = callStats.get(0); |
| 649 | assertEquals("", debugEntry1.className); |
| 650 | assertEquals("__DEBUG_start_time_millis", debugEntry1.methodName); |
Marcin Oczeretko | 8d86174 | 2018-12-06 11:13:29 +0000 | [diff] [blame] | 651 | assertTrue(startTime <= debugEntry1.latencyMicros); |
Marcin Oczeretko | 772e8f2 | 2018-11-21 13:00:32 +0000 | [diff] [blame] | 652 | BinderCallsStats.ExportedCallStat debugEntry2 = callStats.get(1); |
| 653 | assertEquals("", debugEntry2.className); |
| 654 | assertEquals("__DEBUG_end_time_millis", debugEntry2.methodName); |
Marcin Oczeretko | 8d86174 | 2018-12-06 11:13:29 +0000 | [diff] [blame] | 655 | assertTrue(debugEntry1.latencyMicros <= debugEntry2.latencyMicros); |
Marcin Oczeretko | 6a2e524 | 2018-11-28 11:08:50 +0000 | [diff] [blame] | 656 | BinderCallsStats.ExportedCallStat debugEntry3 = callStats.get(2); |
| 657 | assertEquals("", debugEntry3.className); |
| 658 | assertEquals("__DEBUG_battery_time_millis", debugEntry3.methodName); |
Marcin Oczeretko | 8d86174 | 2018-12-06 11:13:29 +0000 | [diff] [blame] | 659 | assertTrue(debugEntry3.latencyMicros >= 0); |
Olivier Gaillard | 5d339dd | 2019-03-08 11:25:05 +0000 | [diff] [blame] | 660 | BinderCallsStats.ExportedCallStat debugEntry4 = callStats.get(3); |
| 661 | assertEquals("__DEBUG_sampling_interval", debugEntry4.methodName); |
| 662 | assertEquals(10, debugEntry4.latencyMicros); |
Marcin Oczeretko | 772e8f2 | 2018-11-21 13:00:32 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 665 | @Test |
| 666 | public void testTrackScreenInteractiveDisabled() { |
| 667 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 668 | bcs.setTrackScreenInteractive(false); |
| 669 | Binder binder = new Binder(); |
| 670 | |
| 671 | mDeviceState.setScreenInteractive(false); |
| 672 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 673 | bcs.time += 10; |
| 674 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
| 675 | |
| 676 | mDeviceState.setScreenInteractive(true); |
| 677 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 678 | bcs.time += 1000; // shoud be ignored. |
| 679 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
| 680 | |
| 681 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 682 | assertEquals(1, uidEntries.size()); |
| 683 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
| 684 | Assert.assertNotNull(uidEntry); |
| 685 | assertEquals(2, uidEntry.callCount); |
| 686 | |
| 687 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
| 688 | assertEquals(1, callStatsList.size()); |
| 689 | BinderCallsStats.CallStat callStats = callStatsList.get(0); |
| 690 | assertEquals(false, callStats.screenInteractive); |
| 691 | assertEquals(2, callStats.callCount); |
| 692 | assertEquals(2, callStats.recordedCallCount); |
| 693 | } |
| 694 | |
| 695 | @Test |
| 696 | public void testTrackCallingUidDisabled() { |
| 697 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 698 | bcs.setTrackDirectCallerUid(false); |
| 699 | Binder binder = new Binder(); |
| 700 | |
| 701 | bcs.setCallingUid(1); |
| 702 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 703 | bcs.time += 10; |
| 704 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
| 705 | |
| 706 | bcs.setCallingUid(2); |
| 707 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 708 | bcs.time += 1000; // shoud be ignored. |
| 709 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
| 710 | |
| 711 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 712 | assertEquals(1, uidEntries.size()); |
| 713 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
| 714 | Assert.assertNotNull(uidEntry); |
| 715 | assertEquals(2, uidEntry.callCount); |
| 716 | |
| 717 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
| 718 | assertEquals(1, callStatsList.size()); |
| 719 | BinderCallsStats.CallStat callStats = callStatsList.get(0); |
| 720 | assertEquals(-1, callStats.callingUid); |
| 721 | assertEquals(2, callStats.callCount); |
| 722 | assertEquals(2, callStats.recordedCallCount); |
| 723 | } |
| 724 | |
Olivier Gaillard | 1c56b4d | 2019-03-07 16:18:39 +0000 | [diff] [blame] | 725 | @Test |
| 726 | public void testTrackScreenInteractiveDisabled_sampling() { |
| 727 | TestBinderCallsStats bcs = new TestBinderCallsStats(); |
| 728 | bcs.setSamplingInterval(2); |
| 729 | bcs.setTrackScreenInteractive(false); |
| 730 | Binder binder = new Binder(); |
| 731 | |
| 732 | mDeviceState.setScreenInteractive(false); |
| 733 | CallSession callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 734 | bcs.time += 10; |
| 735 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
| 736 | |
| 737 | mDeviceState.setScreenInteractive(true); |
| 738 | callSession = bcs.callStarted(binder, 1, WORKSOURCE_UID); |
| 739 | bcs.time += 1000; // shoud be ignored. |
| 740 | bcs.callEnded(callSession, REQUEST_SIZE, REPLY_SIZE, WORKSOURCE_UID); |
| 741 | |
| 742 | SparseArray<BinderCallsStats.UidEntry> uidEntries = bcs.getUidEntries(); |
| 743 | assertEquals(1, uidEntries.size()); |
| 744 | BinderCallsStats.UidEntry uidEntry = uidEntries.get(WORKSOURCE_UID); |
| 745 | Assert.assertNotNull(uidEntry); |
| 746 | assertEquals(2, uidEntry.callCount); |
| 747 | |
| 748 | List<BinderCallsStats.CallStat> callStatsList = new ArrayList(uidEntry.getCallStatsList()); |
| 749 | assertEquals(1, callStatsList.size()); |
| 750 | BinderCallsStats.CallStat callStats = callStatsList.get(0); |
| 751 | assertEquals(false, callStats.screenInteractive); |
| 752 | assertEquals(2, callStats.callCount); |
| 753 | assertEquals(1, callStats.recordedCallCount); |
| 754 | } |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 755 | |
Marcin Oczeretko | c80c81a | 2018-08-30 20:15:52 +0100 | [diff] [blame] | 756 | class TestBinderCallsStats extends BinderCallsStats { |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 757 | public int callingUid = CALLING_UID; |
Olivier Gaillard | 88de48f | 2018-10-18 10:21:21 +0100 | [diff] [blame] | 758 | public long time = 1234; |
| 759 | public long elapsedTime = 0; |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 760 | |
Olivier Gaillard | 1d7f615 | 2018-07-03 13:57:58 +0100 | [diff] [blame] | 761 | TestBinderCallsStats() { |
Marcin Oczeretko | 6a2e524 | 2018-11-28 11:08:50 +0000 | [diff] [blame] | 762 | this(mDeviceState); |
| 763 | } |
| 764 | |
| 765 | TestBinderCallsStats(CachedDeviceState deviceState) { |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 766 | // Make random generator not random. |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 767 | super(new Injector() { |
| 768 | public Random getRandomGenerator() { |
| 769 | return new Random() { |
| 770 | int mCallCount = 0; |
Olivier Gaillard | 2c13c6f | 2018-07-16 16:55:58 +0100 | [diff] [blame] | 771 | |
Olivier Gaillard | 86714d1 | 2018-08-01 15:05:36 +0100 | [diff] [blame] | 772 | public int nextInt() { |
| 773 | return mCallCount++; |
| 774 | } |
| 775 | }; |
Olivier Gaillard | 289ba40 | 2018-07-24 18:50:13 +0100 | [diff] [blame] | 776 | } |
| 777 | }); |
Olivier Gaillard | 34ad8af | 2018-08-06 15:55:03 +0100 | [diff] [blame] | 778 | setSamplingInterval(1); |
Marcin Oczeretko | 772e8f2 | 2018-11-21 13:00:32 +0000 | [diff] [blame] | 779 | setAddDebugEntries(false); |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 780 | setTrackScreenInteractive(true); |
| 781 | setTrackDirectCallerUid(true); |
Marcin Oczeretko | 6a2e524 | 2018-11-28 11:08:50 +0000 | [diff] [blame] | 782 | if (deviceState != null) { |
| 783 | setDeviceState(deviceState.getReadonlyClient()); |
| 784 | } |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | @Override |
| 788 | protected long getThreadTimeMicro() { |
| 789 | return time; |
| 790 | } |
| 791 | |
| 792 | @Override |
Olivier Gaillard | 11965ed | 2018-06-04 14:14:04 +0100 | [diff] [blame] | 793 | protected long getElapsedRealtimeMicro() { |
| 794 | return elapsedTime; |
| 795 | } |
| 796 | |
| 797 | @Override |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 798 | protected int getCallingUid() { |
| 799 | return callingUid; |
| 800 | } |
Olivier Gaillard | 36b80ca | 2019-02-11 11:41:39 +0000 | [diff] [blame] | 801 | |
| 802 | protected void setCallingUid(int uid) { |
| 803 | callingUid = uid; |
| 804 | } |
Fyodor Kupolov | cf0fe2d | 2018-05-22 18:50:04 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | } |