1 /*
2  * Copyright (C) 2020 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.server.power.stats;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.platform.test.ravenwood.RavenwoodRule;
22 
23 import androidx.test.filters.SmallTest;
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import com.android.internal.os.KernelSingleProcessCpuThreadReader;
27 
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.io.IOException;
33 
34 @SmallTest
35 @RunWith(AndroidJUnit4.class)
36 @android.platform.test.annotations.DisabledOnRavenwood(reason = "Kernel dependency")
37 public class SystemServerCpuThreadReaderTest {
38     @Rule
39     public final RavenwoodRule mRavenwood = new RavenwoodRule();
40 
41     @Test
testReadDelta()42     public void testReadDelta() throws IOException {
43         int pid = 42;
44 
45         MockCpuTimeInStateReader mockReader = new MockCpuTimeInStateReader(4);
46         // Units are nanoseconds
47         mockReader.setAggregatedTaskCpuFreqTimes(new String[] {
48                 "0:1000000000 2000000000 3000000000:4000000000",
49                 "1:100000000 200000000 300000000:400000000",
50         });
51 
52         SystemServerCpuThreadReader reader = new SystemServerCpuThreadReader(pid, mockReader);
53         reader.setBinderThreadNativeTids(new int[] {1, 3});
54 
55         // The first invocation of readDelta populates the "last" snapshot
56         SystemServerCpuThreadReader.SystemServiceCpuThreadTimes systemServiceCpuThreadTimes =
57                 reader.readDelta();
58 
59         assertThat(systemServiceCpuThreadTimes.threadCpuTimesUs)
60                 .isEqualTo(new long[] {1100000, 2200000, 3300000, 4400000});
61         assertThat(systemServiceCpuThreadTimes.binderThreadCpuTimesUs)
62                 .isEqualTo(new long[] {100000, 200000, 300000, 400000});
63 
64         mockReader.setAggregatedTaskCpuFreqTimes(new String[] {
65                 "0:1010000000 2020000000 3030000000:4040000000",
66                 "1:101000000 202000000 303000000:404000000",
67         });
68 
69         // The second invocation gets the actual delta
70         systemServiceCpuThreadTimes = reader.readDelta();
71 
72         assertThat(systemServiceCpuThreadTimes.threadCpuTimesUs)
73                 .isEqualTo(new long[] {11000, 22000, 33000, 44000});
74         assertThat(systemServiceCpuThreadTimes.binderThreadCpuTimesUs)
75                 .isEqualTo(new long[] {1000, 2000, 3000, 4000});
76     }
77 
78     public static class MockCpuTimeInStateReader implements
79             KernelSingleProcessCpuThreadReader.CpuTimeInStateReader {
80         private final int mCpuFrequencyCount;
81         private String[] mAggregatedTaskCpuFreqTimes;
82 
MockCpuTimeInStateReader(int frequencyCount)83         MockCpuTimeInStateReader(int frequencyCount) {
84             mCpuFrequencyCount = frequencyCount;
85         }
86 
87         @Override
getCpuFrequencyCount()88         public int getCpuFrequencyCount() {
89             return mCpuFrequencyCount;
90         }
91 
92         @Override
startTrackingProcessCpuTimes(int tgid)93         public boolean startTrackingProcessCpuTimes(int tgid) {
94             return true;
95         }
96 
startAggregatingTaskCpuTimes(int pid, int aggregationKey)97         public boolean startAggregatingTaskCpuTimes(int pid, int aggregationKey) {
98             return true;
99         }
100 
setAggregatedTaskCpuFreqTimes(String[] mAggregatedTaskCpuFreqTimes)101         public void setAggregatedTaskCpuFreqTimes(String[] mAggregatedTaskCpuFreqTimes) {
102             this.mAggregatedTaskCpuFreqTimes = mAggregatedTaskCpuFreqTimes;
103         }
104 
getAggregatedTaskCpuFreqTimes(int pid)105         public String[] getAggregatedTaskCpuFreqTimes(int pid) {
106             return mAggregatedTaskCpuFreqTimes;
107         }
108     }
109 }
110