Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [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 | |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.assertArrayEquals; |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertFalse; |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 22 | import static org.junit.Assert.assertNotNull; |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertTrue; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.verifyZeroInteractions; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.support.test.filters.SmallTest; |
| 29 | import android.support.test.runner.AndroidJUnit4; |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 30 | import android.util.SparseArray; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 31 | |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | import org.mockito.Mock; |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 36 | import org.mockito.Mockito; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 37 | import org.mockito.MockitoAnnotations; |
| 38 | |
| 39 | import java.io.BufferedReader; |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 40 | import java.nio.ByteBuffer; |
| 41 | import java.nio.ByteOrder; |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 42 | import java.util.Arrays; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Test class for {@link KernelUidCpuFreqTimeReader}. |
| 46 | * |
| 47 | * To run the tests, use |
| 48 | * |
| 49 | * runtest -c com.android.internal.os.KernelUidCpuFreqTimeReaderTest frameworks-core |
| 50 | * |
| 51 | * or the following steps: |
| 52 | * |
| 53 | * Build: m FrameworksCoreTests |
| 54 | * Install: adb install -r \ |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 55 | * ${ANDROID_PRODUCT_OUT}/data/app/FrameworksCoreTests/FrameworksCoreTests.apk |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 56 | * Run: adb shell am instrument -e class com.android.internal.os.KernelUidCpuFreqTimeReaderTest -w \ |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 57 | * com.android.frameworks.coretests/android.support.test.runner.AndroidJUnitRunner |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 58 | * |
| 59 | * or |
| 60 | * |
| 61 | * bit FrameworksCoreTests:com.android.internal.os.KernelUidCpuFreqTimeReaderTest |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 62 | */ |
| 63 | @SmallTest |
| 64 | @RunWith(AndroidJUnit4.class) |
| 65 | public class KernelUidCpuFreqTimeReaderTest { |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 66 | @Mock |
| 67 | private BufferedReader mBufferedReader; |
| 68 | @Mock |
| 69 | private KernelUidCpuFreqTimeReader.Callback mCallback; |
| 70 | @Mock |
| 71 | private PowerProfile mPowerProfile; |
| 72 | @Mock |
| 73 | private KernelCpuProcReader mProcReader; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 74 | |
| 75 | private KernelUidCpuFreqTimeReader mKernelUidCpuFreqTimeReader; |
| 76 | |
| 77 | @Before |
| 78 | public void setUp() { |
| 79 | MockitoAnnotations.initMocks(this); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 80 | mKernelUidCpuFreqTimeReader = new KernelUidCpuFreqTimeReader(mProcReader); |
| 81 | mKernelUidCpuFreqTimeReader.setThrottleInterval(0); |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 85 | public void testReadFreqs_perClusterTimesNotAvailable() throws Exception { |
| 86 | final long[][] freqs = { |
| 87 | {1, 12, 123, 1234}, |
| 88 | {1, 12, 123, 23, 123, 1234, 12345, 123456}, |
| 89 | {1, 12, 123, 23, 123, 1234, 12345, 123456, 12, 123, 12345}, |
| 90 | {1, 12, 123, 23, 2345, 234567} |
| 91 | }; |
| 92 | final int[] numClusters = {2, 2, 3, 1}; |
| 93 | final int[][] numFreqs = {{3, 6}, {4, 5}, {3, 5, 4}, {3}}; |
| 94 | for (int i = 0; i < freqs.length; ++i) { |
| 95 | setCpuClusterFreqs(numClusters[i], numFreqs[i]); |
| 96 | when(mBufferedReader.readLine()).thenReturn(getFreqsLine(freqs[i])); |
| 97 | long[] actualFreqs = mKernelUidCpuFreqTimeReader.readFreqs( |
| 98 | mBufferedReader, mPowerProfile); |
| 99 | assertArrayEquals(freqs[i], actualFreqs); |
| 100 | verifyZeroInteractions(mCallback); |
| 101 | final String errMsg = String.format("Freqs=%s, nClusters=%d, nFreqs=%s", |
| 102 | Arrays.toString(freqs[i]), numClusters[i], Arrays.toString(numFreqs[i])); |
| 103 | assertFalse(errMsg, mKernelUidCpuFreqTimeReader.perClusterTimesAvailable()); |
| 104 | |
| 105 | // Verify that a second call won't read the proc file again |
| 106 | Mockito.reset(mBufferedReader); |
| 107 | actualFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mPowerProfile); |
| 108 | assertArrayEquals(freqs[i], actualFreqs); |
| 109 | assertFalse(errMsg, mKernelUidCpuFreqTimeReader.perClusterTimesAvailable()); |
| 110 | |
| 111 | // Prepare for next iteration |
| 112 | Mockito.reset(mBufferedReader, mPowerProfile); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testReadFreqs_perClusterTimesAvailable() throws Exception { |
| 118 | final long[][] freqs = { |
| 119 | {1, 12, 123, 1234}, |
| 120 | {1, 12, 123, 23, 123, 1234, 12345, 123456}, |
| 121 | {1, 12, 123, 23, 123, 1234, 12345, 123456, 12, 123, 12345, 1234567} |
| 122 | }; |
| 123 | final int[] numClusters = {1, 2, 3}; |
| 124 | final int[][] numFreqs = {{4}, {3, 5}, {3, 5, 4}}; |
| 125 | for (int i = 0; i < freqs.length; ++i) { |
| 126 | setCpuClusterFreqs(numClusters[i], numFreqs[i]); |
| 127 | when(mBufferedReader.readLine()).thenReturn(getFreqsLine(freqs[i])); |
| 128 | long[] actualFreqs = mKernelUidCpuFreqTimeReader.readFreqs( |
| 129 | mBufferedReader, mPowerProfile); |
| 130 | assertArrayEquals(freqs[i], actualFreqs); |
| 131 | verifyZeroInteractions(mCallback); |
| 132 | final String errMsg = String.format("Freqs=%s, nClusters=%d, nFreqs=%s", |
| 133 | Arrays.toString(freqs[i]), numClusters[i], Arrays.toString(numFreqs[i])); |
| 134 | assertTrue(errMsg, mKernelUidCpuFreqTimeReader.perClusterTimesAvailable()); |
| 135 | |
| 136 | // Verify that a second call won't read the proc file again |
| 137 | Mockito.reset(mBufferedReader); |
| 138 | actualFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mPowerProfile); |
| 139 | assertArrayEquals(freqs[i], actualFreqs); |
| 140 | assertTrue(errMsg, mKernelUidCpuFreqTimeReader.perClusterTimesAvailable()); |
| 141 | |
| 142 | // Prepare for next iteration |
| 143 | Mockito.reset(mBufferedReader, mPowerProfile); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Test |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 148 | public void testReadDelta() throws Exception { |
| 149 | final long[] freqs = {1, 12, 123, 1234, 12345, 123456}; |
| 150 | final int[] uids = {1, 22, 333, 4444, 5555}; |
| 151 | final long[][] times = new long[uids.length][freqs.length]; |
| 152 | for (int i = 0; i < uids.length; ++i) { |
| 153 | for (int j = 0; j < freqs.length; ++j) { |
| 154 | times[i][j] = uids[i] * freqs[j] * 10; |
| 155 | } |
| 156 | } |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 157 | when(mBufferedReader.readLine()).thenReturn(getFreqsLine(freqs)); |
| 158 | long[] actualFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mBufferedReader, mPowerProfile); |
| 159 | |
| 160 | assertArrayEquals(freqs, actualFreqs); |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 161 | when(mBufferedReader.readLine()) |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 162 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, times)); |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 163 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 164 | for (int i = 0; i < uids.length; ++i) { |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 165 | Mockito.verify(mCallback).onUidCpuFreqTime(uids[i], times[i]); |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 166 | } |
| 167 | verifyNoMoreInteractions(mCallback); |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 168 | |
| 169 | // Verify that a second call will only return deltas. |
| 170 | Mockito.reset(mCallback, mBufferedReader); |
| 171 | final long[][] newTimes1 = new long[uids.length][freqs.length]; |
| 172 | for (int i = 0; i < uids.length; ++i) { |
| 173 | for (int j = 0; j < freqs.length; ++j) { |
| 174 | newTimes1[i][j] = (times[i][j] + uids[i] + freqs[j]) * 10; |
| 175 | } |
| 176 | } |
| 177 | when(mBufferedReader.readLine()) |
| 178 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, newTimes1)); |
| 179 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 180 | for (int i = 0; i < uids.length; ++i) { |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 181 | Mockito.verify(mCallback).onUidCpuFreqTime(uids[i], subtract(newTimes1[i], times[i])); |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 182 | } |
| 183 | verifyNoMoreInteractions(mCallback); |
| 184 | |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 185 | // Verify that there won't be a callback if the proc file values didn't change. |
| 186 | Mockito.reset(mCallback, mBufferedReader); |
| 187 | when(mBufferedReader.readLine()) |
| 188 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, newTimes1)); |
| 189 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 190 | verifyNoMoreInteractions(mCallback); |
| 191 | |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 192 | // Verify that calling with a null callback doesn't result in any crashes |
| 193 | Mockito.reset(mCallback, mBufferedReader); |
| 194 | final long[][] newTimes2 = new long[uids.length][freqs.length]; |
| 195 | for (int i = 0; i < uids.length; ++i) { |
| 196 | for (int j = 0; j < freqs.length; ++j) { |
| 197 | newTimes2[i][j] = (newTimes1[i][j] + uids[i] * freqs[j]) * 10; |
| 198 | } |
| 199 | } |
| 200 | when(mBufferedReader.readLine()) |
| 201 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, newTimes2)); |
| 202 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, null); |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 203 | |
| 204 | // Verify that the readDelta call will only return deltas when |
| 205 | // the previous call had null callback. |
| 206 | Mockito.reset(mCallback, mBufferedReader); |
| 207 | final long[][] newTimes3 = new long[uids.length][freqs.length]; |
| 208 | for (int i = 0; i < uids.length; ++i) { |
| 209 | for (int j = 0; j < freqs.length; ++j) { |
| 210 | newTimes3[i][j] = (newTimes2[i][j] * (uids[i] + freqs[j])) * 10; |
| 211 | } |
| 212 | } |
| 213 | when(mBufferedReader.readLine()) |
| 214 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, newTimes3)); |
| 215 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 216 | for (int i = 0; i < uids.length; ++i) { |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 217 | Mockito.verify(mCallback).onUidCpuFreqTime(uids[i], |
| 218 | subtract(newTimes3[i], newTimes2[i])); |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 219 | } |
| 220 | verifyNoMoreInteractions(mCallback); |
| 221 | } |
| 222 | |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 223 | @Test |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 224 | public void testReadDelta_Binary() throws Exception { |
| 225 | VerifiableCallback cb = new VerifiableCallback(); |
| 226 | final long[] freqs = {110, 123, 145, 167, 289, 997}; |
| 227 | final int[] uids = {1, 22, 333, 444, 555}; |
| 228 | final long[][] times = new long[uids.length][freqs.length]; |
| 229 | for (int i = 0; i < uids.length; ++i) { |
| 230 | for (int j = 0; j < freqs.length; ++j) { |
| 231 | times[i][j] = uids[i] * freqs[j] * 10; |
| 232 | } |
| 233 | } |
| 234 | when(mBufferedReader.readLine()).thenReturn(getFreqsLine(freqs)); |
| 235 | long[] actualFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mBufferedReader, mPowerProfile); |
| 236 | |
| 237 | assertArrayEquals(freqs, actualFreqs); |
| 238 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times)); |
| 239 | mKernelUidCpuFreqTimeReader.readDeltaBinary(cb); |
| 240 | for (int i = 0; i < uids.length; ++i) { |
| 241 | cb.verify(uids[i], times[i]); |
| 242 | } |
| 243 | cb.verifyNoMoreInteractions(); |
| 244 | |
| 245 | // Verify that a second call will only return deltas. |
| 246 | cb.clear(); |
| 247 | Mockito.reset(mProcReader); |
| 248 | final long[][] newTimes1 = new long[uids.length][freqs.length]; |
| 249 | for (int i = 0; i < uids.length; ++i) { |
| 250 | for (int j = 0; j < freqs.length; ++j) { |
| 251 | newTimes1[i][j] = times[i][j] + (uids[i] + freqs[j]) * 50; |
| 252 | } |
| 253 | } |
| 254 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, newTimes1)); |
| 255 | mKernelUidCpuFreqTimeReader.readDeltaBinary(cb); |
| 256 | for (int i = 0; i < uids.length; ++i) { |
| 257 | cb.verify(uids[i], subtract(newTimes1[i], times[i])); |
| 258 | } |
| 259 | cb.verifyNoMoreInteractions(); |
| 260 | |
| 261 | // Verify that there won't be a callback if the proc file values didn't change. |
| 262 | cb.clear(); |
| 263 | Mockito.reset(mProcReader); |
| 264 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, newTimes1)); |
| 265 | mKernelUidCpuFreqTimeReader.readDeltaBinary(cb); |
| 266 | cb.verifyNoMoreInteractions(); |
| 267 | |
| 268 | // Verify that calling with a null callback doesn't result in any crashes |
| 269 | cb.clear(); |
| 270 | Mockito.reset(mProcReader); |
| 271 | final long[][] newTimes2 = new long[uids.length][freqs.length]; |
| 272 | for (int i = 0; i < uids.length; ++i) { |
| 273 | for (int j = 0; j < freqs.length; ++j) { |
| 274 | newTimes2[i][j] = newTimes1[i][j] + (uids[i] * freqs[j]) * 30; |
| 275 | } |
| 276 | } |
| 277 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, newTimes2)); |
| 278 | mKernelUidCpuFreqTimeReader.readDeltaBinary(null); |
| 279 | cb.verifyNoMoreInteractions(); |
| 280 | |
| 281 | // Verify that the readDelta call will only return deltas when |
| 282 | // the previous call had null callback. |
| 283 | cb.clear(); |
| 284 | Mockito.reset(mProcReader); |
| 285 | final long[][] newTimes3 = new long[uids.length][freqs.length]; |
| 286 | for (int i = 0; i < uids.length; ++i) { |
| 287 | for (int j = 0; j < freqs.length; ++j) { |
| 288 | newTimes3[i][j] = newTimes2[i][j] + (uids[i] + freqs[j]) * 40; |
| 289 | } |
| 290 | } |
| 291 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, newTimes3)); |
| 292 | mKernelUidCpuFreqTimeReader.readDeltaBinary(cb); |
| 293 | for (int i = 0; i < uids.length; ++i) { |
| 294 | cb.verify(uids[i], subtract(newTimes3[i], newTimes2[i])); |
| 295 | } |
| 296 | cb.verifyNoMoreInteractions(); |
| 297 | } |
| 298 | |
| 299 | @Test |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 300 | public void testReadDelta_malformedData() throws Exception { |
| 301 | final long[] freqs = {1, 12, 123, 1234, 12345, 123456}; |
| 302 | final int[] uids = {1, 22, 333, 4444, 5555}; |
| 303 | final long[][] times = new long[uids.length][freqs.length]; |
| 304 | for (int i = 0; i < uids.length; ++i) { |
| 305 | for (int j = 0; j < freqs.length; ++j) { |
| 306 | times[i][j] = uids[i] * freqs[j] * 10; |
| 307 | } |
| 308 | } |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 309 | when(mBufferedReader.readLine()).thenReturn(getFreqsLine(freqs)); |
| 310 | long[] actualFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mBufferedReader, mPowerProfile); |
| 311 | |
| 312 | assertArrayEquals(freqs, actualFreqs); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 313 | when(mBufferedReader.readLine()) |
| 314 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, times)); |
| 315 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 316 | for (int i = 0; i < uids.length; ++i) { |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 317 | Mockito.verify(mCallback).onUidCpuFreqTime(uids[i], times[i]); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 318 | } |
| 319 | verifyNoMoreInteractions(mCallback); |
| 320 | |
| 321 | // Verify that there is no callback if any value in the proc file is -ve. |
| 322 | Mockito.reset(mCallback, mBufferedReader); |
| 323 | final long[][] newTimes1 = new long[uids.length][freqs.length]; |
| 324 | for (int i = 0; i < uids.length; ++i) { |
| 325 | for (int j = 0; j < freqs.length; ++j) { |
| 326 | newTimes1[i][j] = (times[i][j] + uids[i] + freqs[j]) * 10; |
| 327 | } |
| 328 | } |
| 329 | newTimes1[uids.length - 1][freqs.length - 1] *= -1; |
| 330 | when(mBufferedReader.readLine()) |
| 331 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, newTimes1)); |
| 332 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 333 | for (int i = 0; i < uids.length; ++i) { |
| 334 | if (i == uids.length - 1) { |
| 335 | continue; |
| 336 | } |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 337 | Mockito.verify(mCallback).onUidCpuFreqTime(uids[i], subtract(newTimes1[i], times[i])); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 338 | } |
| 339 | verifyNoMoreInteractions(mCallback); |
| 340 | |
| 341 | // Verify that the internal state was not modified when the proc file had -ve value. |
| 342 | Mockito.reset(mCallback, mBufferedReader); |
| 343 | for (int i = 0; i < freqs.length; ++i) { |
| 344 | newTimes1[uids.length - 1][i] = times[uids.length - 1][i]; |
| 345 | } |
| 346 | when(mBufferedReader.readLine()) |
| 347 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, newTimes1)); |
| 348 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 349 | verifyNoMoreInteractions(mCallback); |
| 350 | |
| 351 | // Verify that there is no callback if the values in the proc file are decreased. |
| 352 | Mockito.reset(mCallback, mBufferedReader); |
| 353 | final long[][] newTimes2 = new long[uids.length][freqs.length]; |
| 354 | for (int i = 0; i < uids.length; ++i) { |
| 355 | for (int j = 0; j < freqs.length; ++j) { |
| 356 | newTimes2[i][j] = (newTimes1[i][j] + uids[i] * freqs[j]) * 10; |
| 357 | } |
| 358 | } |
| 359 | newTimes2[uids.length - 1][freqs.length - 1] = |
| 360 | newTimes1[uids.length - 1][freqs.length - 1] - 222; |
| 361 | when(mBufferedReader.readLine()) |
| 362 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, newTimes2)); |
| 363 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 364 | for (int i = 0; i < uids.length; ++i) { |
| 365 | if (i == uids.length - 1) { |
| 366 | continue; |
| 367 | } |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 368 | Mockito.verify(mCallback).onUidCpuFreqTime(uids[i], |
| 369 | subtract(newTimes2[i], newTimes1[i])); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 370 | } |
| 371 | verifyNoMoreInteractions(mCallback); |
| 372 | |
| 373 | // Verify that the internal state was not modified when the proc file had decreasing values. |
| 374 | Mockito.reset(mCallback, mBufferedReader); |
| 375 | for (int i = 0; i < freqs.length; ++i) { |
| 376 | newTimes2[uids.length - 1][i] = newTimes1[uids.length - 1][i]; |
| 377 | } |
| 378 | when(mBufferedReader.readLine()) |
| 379 | .thenReturn(getFreqsLine(freqs), getUidTimesLines(uids, newTimes2)); |
| 380 | mKernelUidCpuFreqTimeReader.readDelta(mBufferedReader, mCallback); |
Sudheer Shanka | 0085752 | 2017-07-06 18:28:14 -0700 | [diff] [blame] | 381 | verifyNoMoreInteractions(mCallback); |
| 382 | } |
| 383 | |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 384 | private long[] subtract(long[] a1, long[] a2) { |
| 385 | long[] val = new long[a1.length]; |
| 386 | for (int i = 0; i < val.length; ++i) { |
| 387 | val[i] = a1[i] - a2[i]; |
| 388 | } |
| 389 | return val; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private String getFreqsLine(long[] freqs) { |
| 393 | final StringBuilder sb = new StringBuilder(); |
| 394 | sb.append("uid:"); |
| 395 | for (int i = 0; i < freqs.length; ++i) { |
| 396 | sb.append(" " + freqs[i]); |
| 397 | } |
| 398 | return sb.toString(); |
| 399 | } |
| 400 | |
| 401 | private String[] getUidTimesLines(int[] uids, long[][] times) { |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 402 | final String[] lines = new String[uids.length + 1]; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 403 | final StringBuilder sb = new StringBuilder(); |
| 404 | for (int i = 0; i < uids.length; ++i) { |
| 405 | sb.setLength(0); |
| 406 | sb.append(uids[i] + ":"); |
| 407 | for (int j = 0; j < times[i].length; ++j) { |
| 408 | sb.append(" " + times[i][j] / 10); |
| 409 | } |
| 410 | lines[i] = sb.toString(); |
| 411 | } |
Sudheer Shanka | 59f5c00 | 2017-05-15 10:57:15 -0700 | [diff] [blame] | 412 | lines[uids.length] = null; |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 413 | return lines; |
| 414 | } |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 415 | |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 416 | private ByteBuffer getUidTimesBytes(int[] uids, long[][] times) { |
| 417 | int size = (1 + uids.length + uids.length * times[0].length) * 4; |
| 418 | ByteBuffer buf = ByteBuffer.allocate(size); |
| 419 | buf.order(ByteOrder.nativeOrder()); |
| 420 | buf.putInt(times[0].length); |
| 421 | for (int i = 0; i < uids.length; i++) { |
| 422 | buf.putInt(uids[i]); |
| 423 | for (int j = 0; j < times[i].length; j++) { |
| 424 | buf.putInt((int) (times[i][j] / 10)); |
| 425 | } |
| 426 | } |
| 427 | buf.flip(); |
| 428 | return buf.asReadOnlyBuffer().order(ByteOrder.nativeOrder()); |
| 429 | } |
| 430 | |
Sudheer Shanka | b8ad594 | 2017-08-08 12:16:09 -0700 | [diff] [blame] | 431 | private void setCpuClusterFreqs(int numClusters, int... clusterFreqs) { |
| 432 | assertEquals(numClusters, clusterFreqs.length); |
| 433 | when(mPowerProfile.getNumCpuClusters()).thenReturn(numClusters); |
| 434 | for (int i = 0; i < numClusters; ++i) { |
| 435 | when(mPowerProfile.getNumSpeedStepsInCpuCluster(i)).thenReturn(clusterFreqs[i]); |
| 436 | } |
| 437 | } |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 438 | |
| 439 | private class VerifiableCallback implements KernelUidCpuFreqTimeReader.Callback { |
| 440 | |
| 441 | SparseArray<long[]> mData = new SparseArray<>(); |
| 442 | int count = 0; |
| 443 | |
| 444 | public void verify(int uid, long[] cpuFreqTimeMs) { |
| 445 | long[] array = mData.get(uid); |
| 446 | assertNotNull(array); |
| 447 | assertArrayEquals(cpuFreqTimeMs, array); |
| 448 | count++; |
| 449 | } |
| 450 | |
| 451 | public void clear() { |
| 452 | mData.clear(); |
| 453 | count = 0; |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public void onUidCpuFreqTime(int uid, long[] cpuFreqTimeMs) { |
| 458 | long[] array = new long[cpuFreqTimeMs.length]; |
| 459 | System.arraycopy(cpuFreqTimeMs, 0, array, 0, array.length); |
| 460 | mData.put(uid, array); |
| 461 | } |
| 462 | |
| 463 | public void verifyNoMoreInteractions() { |
| 464 | assertEquals(mData.size(), count); |
| 465 | } |
| 466 | } |
Sudheer Shanka | 9b735c5 | 2017-05-09 18:26:18 -0700 | [diff] [blame] | 467 | } |