Debian Codegraph @

apct-tests/perftests/core/src/android/os/LongArrayMultiStateCounterPerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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 android.os;
18
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21
22 import androidx.test.filters.LargeTest;
23 import androidx.test.runner.AndroidJUnit4;
24
25 import com.android.internal.os.LongArrayMultiStateCounter;
26
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30
31 @RunWith(AndroidJUnit4.class)
32 @LargeTest
33 public class LongArrayMultiStateCounterPerfTest {
34
35 @Rule
36 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
37
38 /**
39 * A complete line-for-line reimplementation of
40 * {@link com.android.internal.os.LongArrayMultiStateCounter}, only in Java instead of
41 * native.
42 */
43 private static class TestLongArrayMultiStateCounter {
44 private final int mStateCount;
45 private final int mArrayLength;
46 private int mCurrentState;
47 private long mLastStateChangeTimestampMs = -1;
48 private long mLastUpdateTimestampMs = -1;
49
50 private static class State {
51 private long mTimeInStateSinceUpdate;
52 private long[] mCounter;
53 }
54
55 private final State[] mStates;
56 private final long[] mLastTimeInFreq;
57 private final long[] mDelta;
58
59 TestLongArrayMultiStateCounter(int stateCount, int arrayLength) {
60 mStateCount = stateCount;
61 mArrayLength = arrayLength;
62 mStates = new State[stateCount];
63 for (int i = 0; i < mStateCount; i++) {
64 mStates[i] = new State();
65 mStates[i].mCounter = new long[mArrayLength];
66 }
67 mLastTimeInFreq = new long[mArrayLength];
68 mDelta = new long[mArrayLength];
69 }
70
71 public void setState(int state, long timestampMs) {
72 if (mLastStateChangeTimestampMs > 0) {
73 if (timestampMs >= mLastStateChangeTimestampMs) {
74 mStates[mCurrentState].mTimeInStateSinceUpdate +=
75 timestampMs - mLastStateChangeTimestampMs;
76 } else {
77 for (int i = 0; i < mStateCount; i++) {
78 mStates[i].mTimeInStateSinceUpdate = 0;
79 }
80 }
81 }
82 mCurrentState = state;
83 mLastStateChangeTimestampMs = timestampMs;
84 }
85
86 public void updateValue(long[] timeInFreq, long timestampMs) {
87 setState(mCurrentState, timestampMs);
88
89 if (mLastUpdateTimestampMs >= 0) {
90 if (timestampMs > mLastUpdateTimestampMs) {
91 if (delta(mLastTimeInFreq, timeInFreq, mDelta)) {
92 long timeSinceUpdate = timestampMs - mLastUpdateTimestampMs;
93 for (int i = 0; i < mStateCount; i++) {
94 long timeInState = mStates[i].mTimeInStateSinceUpdate;
95 if (timeInState > 0) {
96 add(mStates[i].mCounter, mDelta, timeInState, timeSinceUpdate);
97 mStates[i].mTimeInStateSinceUpdate = 0;
98 }
99 }
100 } else {
101 throw new RuntimeException();
102 }
103 } else if (timestampMs < mLastUpdateTimestampMs) {
104 throw new RuntimeException();
105 }
106 }
107 System.arraycopy(timeInFreq, 0, mLastTimeInFreq, 0, mArrayLength);
108 mLastUpdateTimestampMs = timestampMs;
109 }
110
111 private boolean delta(long[] timeInFreq1, long[] timeInFreq2, long[] delta) {
112 if (delta.length != mArrayLength) {
113 throw new RuntimeException();
114 }
115
116 boolean is_delta_valid = true;
117 for (int i = 0; i < mStateCount; i++) {
118 if (timeInFreq2[i] >= timeInFreq1[i]) {
119 delta[i] = timeInFreq2[i] - timeInFreq1[i];
120 } else {
121 delta[i] = 0;
122 is_delta_valid = false;
123 }
124 }
125
126 return is_delta_valid;
127 }
128
129 private void add(long[] counter, long[] delta, long numerator, long denominator) {
130 if (numerator != denominator) {
131 for (int i = 0; i < mArrayLength; i++) {
132 counter[i] += delta[i] * numerator / denominator;
133 }
134 } else {
135 for (int i = 0; i < mArrayLength; i++) {
136 counter[i] += delta[i];
137 }
138 }
139 }
140 }
141
142 @Test
143 public void javaImplementation() {
144 TestLongArrayMultiStateCounter counter =
145 new TestLongArrayMultiStateCounter(2, 4);
146 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
147 long time = 1000;
148 long[] timeInFreq = {100, 200, 300, 400};
149 while (state.keepRunning()) {
150 counter.setState(1, time);
151 counter.setState(0, time + 1000);
152 counter.updateValue(timeInFreq, time + 2000);
153 time += 10000;
154 }
155 }
156
157 @Test
158 public void nativeImplementation() {
159 LongArrayMultiStateCounter counter = new LongArrayMultiStateCounter(2, 4);
160 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
161 long time = 1000;
162 LongArrayMultiStateCounter.LongArrayContainer timeInFreq =
163 new LongArrayMultiStateCounter.LongArrayContainer(4);
164 timeInFreq.setValues(new long[]{100, 200, 300, 400});
165 while (state.keepRunning()) {
166 counter.setState(1, time);
167 counter.setState(0, time + 1000);
168 counter.updateValues(timeInFreq, time + 2000);
169 time += 10000;
170 }
171 }
172 }