Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -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 | |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 19 | import static org.mockito.Mockito.verify; |
| 20 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 21 | import static org.mockito.Mockito.when; |
| 22 | |
| 23 | import android.support.test.filters.SmallTest; |
| 24 | import android.support.test.runner.AndroidJUnit4; |
| 25 | |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 29 | import org.mockito.Mock; |
| 30 | import org.mockito.Mockito; |
| 31 | import org.mockito.MockitoAnnotations; |
| 32 | |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 33 | import java.nio.ByteBuffer; |
| 34 | import java.nio.ByteOrder; |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 35 | import java.util.Random; |
| 36 | |
| 37 | /** |
| 38 | * Test class for {@link KernelUidCpuActiveTimeReader}. |
| 39 | * |
| 40 | * To run it: |
| 41 | * bit FrameworksCoreTests:com.android.internal.os.KernelUidCpuActiveTimeReaderTest |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 42 | */ |
| 43 | @SmallTest |
| 44 | @RunWith(AndroidJUnit4.class) |
| 45 | public class KernelUidCpuActiveTimeReaderTest { |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 46 | @Mock |
| 47 | private KernelCpuProcReader mProcReader; |
| 48 | @Mock |
| 49 | private KernelUidCpuActiveTimeReader.Callback mCallback; |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 50 | private KernelUidCpuActiveTimeReader mReader; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | MockitoAnnotations.initMocks(this); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 55 | mReader = new KernelUidCpuActiveTimeReader(mProcReader); |
| 56 | mReader.setThrottleInterval(0); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testReadDelta() throws Exception { |
| 61 | final int cores = 8; |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 62 | final int[] uids = {1, 22, 333, 4444, 5555}; |
| 63 | |
| 64 | final long[][] times = increaseTime(new long[uids.length][cores]); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 65 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times)); |
| 66 | mReader.readDelta(mCallback); |
| 67 | for (int i = 0; i < uids.length; i++) { |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 68 | verify(mCallback).onUidCpuActiveTime(uids[i], getTotal(times[i])); |
| 69 | } |
| 70 | verifyNoMoreInteractions(mCallback); |
| 71 | |
| 72 | // Verify that a second call will only return deltas. |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 73 | Mockito.reset(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 74 | final long[][] times1 = increaseTime(times); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 75 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times1)); |
| 76 | mReader.readDelta(mCallback); |
| 77 | for (int i = 0; i < uids.length; i++) { |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 78 | verify(mCallback).onUidCpuActiveTime(uids[i], getTotal(subtract(times1[i], times[i]))); |
| 79 | } |
| 80 | verifyNoMoreInteractions(mCallback); |
| 81 | |
| 82 | // Verify that there won't be a callback if the proc file values didn't change. |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 83 | Mockito.reset(mCallback); |
| 84 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times1)); |
| 85 | mReader.readDelta(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 86 | verifyNoMoreInteractions(mCallback); |
| 87 | |
| 88 | // Verify that calling with a null callback doesn't result in any crashes |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 89 | Mockito.reset(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 90 | final long[][] times2 = increaseTime(times1); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 91 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times2)); |
| 92 | mReader.readDelta(null); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 93 | |
| 94 | // Verify that the readDelta call will only return deltas when |
| 95 | // the previous call had null callback. |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 96 | Mockito.reset(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 97 | final long[][] times3 = increaseTime(times2); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 98 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times3)); |
| 99 | mReader.readDelta(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 100 | for (int i = 0; i < uids.length; ++i) { |
| 101 | verify(mCallback).onUidCpuActiveTime(uids[i], getTotal(subtract(times3[i], times2[i]))); |
| 102 | } |
| 103 | verifyNoMoreInteractions(mCallback); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testReadDelta_malformedData() throws Exception { |
| 108 | final int cores = 8; |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 109 | final int[] uids = {1, 22, 333, 4444, 5555}; |
| 110 | final long[][] times = increaseTime(new long[uids.length][cores]); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 111 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times)); |
| 112 | mReader.readDelta(mCallback); |
| 113 | for (int i = 0; i < uids.length; i++) { |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 114 | verify(mCallback).onUidCpuActiveTime(uids[i], getTotal(times[i])); |
| 115 | } |
| 116 | verifyNoMoreInteractions(mCallback); |
| 117 | |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 118 | // Verify that there is no callback if subsequent call is in wrong format. |
| 119 | Mockito.reset(mCallback); |
| 120 | final long[][] times1 = increaseTime(times); |
| 121 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times1).putInt(0, 5)); |
| 122 | mReader.readDelta(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 123 | verifyNoMoreInteractions(mCallback); |
| 124 | |
| 125 | // Verify that the internal state was not modified if the given core count does not match |
| 126 | // the following # of entries. |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 127 | Mockito.reset(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 128 | final long[][] times2 = increaseTime(times); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 129 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times2)); |
| 130 | mReader.readDelta(mCallback); |
| 131 | for (int i = 0; i < uids.length; i++) { |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 132 | verify(mCallback).onUidCpuActiveTime(uids[i], getTotal(subtract(times2[i], times[i]))); |
| 133 | } |
| 134 | verifyNoMoreInteractions(mCallback); |
| 135 | |
| 136 | // Verify that there is no callback if any value in the proc file is -ve. |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 137 | Mockito.reset(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 138 | final long[][] times3 = increaseTime(times2); |
| 139 | times3[uids.length - 1][cores - 1] *= -1; |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 140 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times3)); |
| 141 | mReader.readDelta(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 142 | for (int i = 0; i < uids.length - 1; ++i) { |
| 143 | verify(mCallback).onUidCpuActiveTime(uids[i], getTotal(subtract(times3[i], times2[i]))); |
| 144 | } |
| 145 | verifyNoMoreInteractions(mCallback); |
| 146 | |
| 147 | // Verify that the internal state was not modified when the proc file had -ve value. |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 148 | Mockito.reset(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 149 | for (int i = 0; i < cores; i++) { |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 150 | times3[uids.length - 1][i] = times2[uids.length - 1][i] + uids[uids.length - 1] * 2520; |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 151 | } |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 152 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times3)); |
| 153 | mReader.readDelta(mCallback); |
| 154 | verify(mCallback).onUidCpuActiveTime(uids[uids.length - 1], |
| 155 | getTotal(subtract(times3[uids.length - 1], times2[uids.length - 1]))); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 156 | verifyNoMoreInteractions(mCallback); |
| 157 | |
| 158 | // Verify that there is no callback if the values in the proc file are decreased. |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 159 | Mockito.reset(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 160 | final long[][] times4 = increaseTime(times3); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 161 | System.arraycopy(times3[uids.length - 1], 0, times4[uids.length - 1], 0, cores); |
| 162 | times4[uids.length - 1][cores - 1] -= 100; |
| 163 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times4)); |
| 164 | mReader.readDelta(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 165 | for (int i = 0; i < uids.length - 1; ++i) { |
| 166 | verify(mCallback).onUidCpuActiveTime(uids[i], getTotal(subtract(times4[i], times3[i]))); |
| 167 | } |
| 168 | verifyNoMoreInteractions(mCallback); |
| 169 | |
| 170 | // Verify that the internal state was not modified when the proc file had decreased values. |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 171 | Mockito.reset(mCallback); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 172 | for (int i = 0; i < cores; i++) { |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 173 | times4[uids.length - 1][i] = times3[uids.length - 1][i] + uids[uids.length - 1] * 2520; |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 174 | } |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 175 | when(mProcReader.readBytes()).thenReturn(getUidTimesBytes(uids, times4)); |
| 176 | mReader.readDelta(mCallback); |
| 177 | verify(mCallback).onUidCpuActiveTime(uids[uids.length - 1], |
| 178 | getTotal(subtract(times4[uids.length - 1], times3[uids.length - 1]))); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 179 | verifyNoMoreInteractions(mCallback); |
| 180 | } |
| 181 | |
| 182 | private long[] subtract(long[] a1, long[] a2) { |
| 183 | long[] val = new long[a1.length]; |
| 184 | for (int i = 0; i < val.length; ++i) { |
| 185 | val[i] = a1[i] - a2[i]; |
| 186 | } |
| 187 | return val; |
| 188 | } |
| 189 | |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 190 | /** |
| 191 | * Unit of original and return value is 10ms. What's special about 2520? 2520 is LCM of 1, 2, 3, |
| 192 | * ..., 10. So that when wedivide shared cpu time by concurrent thread count, we always get a |
| 193 | * nice integer, avoiding rounding errors. |
| 194 | */ |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 195 | private long[][] increaseTime(long[][] original) { |
| 196 | long[][] newTime = new long[original.length][original[0].length]; |
| 197 | Random rand = new Random(); |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 198 | for (int i = 0; i < original.length; i++) { |
| 199 | for (int j = 0; j < original[0].length; j++) { |
| 200 | newTime[i][j] = original[i][j] + rand.nextInt(1000) * 2520 + 2520; |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | return newTime; |
| 204 | } |
| 205 | |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 206 | // Unit of times is 10ms |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 207 | private long getTotal(long[] times) { |
| 208 | long sum = 0; |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 209 | for (int i = 0; i < times.length; i++) { |
| 210 | sum += times[i] * 10 / (i + 1); |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 211 | } |
| 212 | return sum; |
| 213 | } |
Mike Ma | 2ab0144 | 2018-02-13 14:22:47 -0800 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Format uids and times (in 10ms) into the following format: |
| 217 | * [n, uid0, time0a, time0b, ..., time0n, |
| 218 | * uid1, time1a, time1b, ..., time1n, |
| 219 | * uid2, time2a, time2b, ..., time2n, etc.] |
| 220 | * where n is the total number of cpus (num_possible_cpus) |
| 221 | */ |
| 222 | private ByteBuffer getUidTimesBytes(int[] uids, long[][] times) { |
| 223 | int size = (1 + uids.length * (times[0].length + 1)) * 4; |
| 224 | ByteBuffer buf = ByteBuffer.allocate(size); |
| 225 | buf.order(ByteOrder.nativeOrder()); |
| 226 | buf.putInt(times[0].length); |
| 227 | for (int i = 0; i < uids.length; i++) { |
| 228 | buf.putInt(uids[i]); |
| 229 | for (int j = 0; j < times[i].length; j++) { |
| 230 | buf.putInt((int) times[i][j]); |
| 231 | } |
| 232 | } |
| 233 | buf.flip(); |
| 234 | return buf.order(ByteOrder.nativeOrder()); |
| 235 | } |
Mike Ma | 3d422c3 | 2017-10-25 11:08:57 -0700 | [diff] [blame] | 236 | } |