blob: 2da266bd331e8e4c159f4d4af92a01069a4f663a [file] [log] [blame]
Kevin DuBoiscc27b502019-11-13 09:40:07 -08001/*
2 * Copyright 2019 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
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080017#include <thread>
Kevin DuBoiscc27b502019-11-13 09:40:07 -080018
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080021
22#include <scheduler/Timer.h>
23
24#include "Scheduler/VSyncDispatchTimerQueue.h"
25#include "Scheduler/VSyncTracker.h"
Kevin DuBoiscc27b502019-11-13 09:40:07 -080026
27using namespace testing;
28using namespace std::literals;
29
30namespace android::scheduler {
31
32template <typename Rep, typename Per>
33constexpr nsecs_t toNs(std::chrono::duration<Rep, Per> const& tp) {
34 return std::chrono::duration_cast<std::chrono::nanoseconds>(tp).count();
35}
36
37class FixedRateIdealStubTracker : public VSyncTracker {
38public:
39 FixedRateIdealStubTracker() : mPeriod{toNs(3ms)} {}
40
Kevin DuBois02d5ed92020-01-27 11:05:46 -080041 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080042
43 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final {
44 auto const floor = timePoint % mPeriod;
45 if (floor == 0) {
46 return timePoint;
47 }
48 return timePoint - floor + mPeriod;
49 }
50
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080051 nsecs_t currentPeriod() const final { return mPeriod; }
52
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080053 void setPeriod(nsecs_t) final {}
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080054 void resetModel() final {}
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070055 bool needsMoreSamples() const final { return false; }
Ady Abraham5cc2e262021-03-25 13:09:17 -070056 bool isVSyncInPhase(nsecs_t, Fps) const final { return false; }
Ady Abraham5e7371c2020-03-24 14:47:24 -070057 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080058
Kevin DuBoiscc27b502019-11-13 09:40:07 -080059private:
60 nsecs_t const mPeriod;
61};
62
63class VRRStubTracker : public VSyncTracker {
64public:
65 VRRStubTracker(nsecs_t period) : mPeriod{period} {}
66
Kevin DuBois02d5ed92020-01-27 11:05:46 -080067 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080068
69 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final {
Ady Abraham8cb21882020-08-26 18:22:05 -070070 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080071 auto const normalized_to_base = time_point - mBase;
72 auto const floor = (normalized_to_base) % mPeriod;
73 if (floor == 0) {
74 return time_point;
75 }
76 return normalized_to_base - floor + mPeriod + mBase;
77 }
78
79 void set_interval(nsecs_t interval, nsecs_t last_known) {
Ady Abraham8cb21882020-08-26 18:22:05 -070080 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080081 mPeriod = interval;
82 mBase = last_known;
83 }
84
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080085 nsecs_t currentPeriod() const final {
Ady Abraham8cb21882020-08-26 18:22:05 -070086 std::lock_guard lock(mMutex);
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080087 return mPeriod;
88 }
89
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080090 void setPeriod(nsecs_t) final {}
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080091 void resetModel() final {}
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070092 bool needsMoreSamples() const final { return false; }
Ady Abraham5cc2e262021-03-25 13:09:17 -070093 bool isVSyncInPhase(nsecs_t, Fps) const final { return false; }
Ady Abraham5e7371c2020-03-24 14:47:24 -070094 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080095
Kevin DuBoiscc27b502019-11-13 09:40:07 -080096private:
97 std::mutex mutable mMutex;
98 nsecs_t mPeriod;
99 nsecs_t mBase = 0;
100};
101
102struct VSyncDispatchRealtimeTest : testing::Test {
103 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800104 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800105 static size_t constexpr mIterations = 20;
106};
107
108class RepeatingCallbackReceiver {
109public:
Ady Abraham9c53ee72020-07-22 21:16:18 -0700110 RepeatingCallbackReceiver(VSyncDispatch& dispatch, nsecs_t workload, nsecs_t readyDuration)
111 : mWorkload(workload),
112 mReadyDuration(readyDuration),
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800113 mCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700114 dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800115
116 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
117 mCallbackTimes.reserve(iterations);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700118 mCallback.schedule(
119 {.workDuration = mWorkload,
120 .readyDuration = mReadyDuration,
121 .earliestVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800122
123 for (auto i = 0u; i < iterations - 1; i++) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700124 std::unique_lock lock(mMutex);
125 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800126 mCalled = false;
127 auto last = mLastTarget;
Ady Abraham8cb21882020-08-26 18:22:05 -0700128 lock.unlock();
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800129
130 onEachFrame(last);
131
Ady Abraham9c53ee72020-07-22 21:16:18 -0700132 mCallback.schedule({.workDuration = mWorkload,
133 .readyDuration = mReadyDuration,
134 .earliestVsync = last + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800135 }
136
137 // wait for the last callback.
Ady Abraham8cb21882020-08-26 18:22:05 -0700138 std::unique_lock lock(mMutex);
139 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800140 }
141
142 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
143 fn(mCallbackTimes);
144 }
145
146private:
147 void callback_called(nsecs_t time) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700148 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800149 mCallbackTimes.push_back(time);
150 mCalled = true;
151 mLastTarget = time;
152 mCv.notify_all();
153 }
154
155 nsecs_t const mWorkload;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700156 nsecs_t const mReadyDuration;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800157 VSyncCallbackRegistration mCallback;
158
159 std::mutex mMutex;
160 std::condition_variable mCv;
161 bool mCalled = false;
162 nsecs_t mLastTarget = 0;
163 std::vector<nsecs_t> mCallbackTimes;
164};
165
166TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
167 FixedRateIdealStubTracker tracker;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800168 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
169 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800170
171 static size_t constexpr num_clients = 3;
172 std::array<RepeatingCallbackReceiver, num_clients>
Ady Abraham9c53ee72020-07-22 21:16:18 -0700173 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)),
174 RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)),
175 RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))};
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800176
177 auto const on_each_frame = [](nsecs_t) {};
178 std::array<std::thread, num_clients> threads{
179 std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }),
180 std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }),
181 std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }),
182 };
183
184 for (auto it = threads.rbegin(); it != threads.rend(); it++) {
185 it->join();
186 }
187
188 for (auto const& cbs : cb_receiver) {
189 cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
190 }
191}
192
193// starts at 333hz, slides down to 43hz
194TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
195 auto next_vsync_interval = toNs(3ms);
196 VRRStubTracker tracker(next_vsync_interval);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800197 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
198 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800199
Ady Abraham9c53ee72020-07-22 21:16:18 -0700200 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800201
202 auto const on_each_frame = [&](nsecs_t last_known) {
203 tracker.set_interval(next_vsync_interval += toNs(1ms), last_known);
204 };
205
206 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
207 eventThread.join();
208
209 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
210}
211
212// starts at 333hz, jumps to 200hz at frame 10
213TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
214 VRRStubTracker tracker(toNs(3ms));
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800215 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
216 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800217
Ady Abraham9c53ee72020-07-22 21:16:18 -0700218 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800219
220 auto jump_frame_counter = 0u;
221 auto constexpr jump_frame_at = 10u;
222 auto const on_each_frame = [&](nsecs_t last_known) {
223 if (jump_frame_counter++ == jump_frame_at) {
224 tracker.set_interval(toNs(5ms), last_known);
225 }
226 };
227 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
228 eventThread.join();
229
230 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
231}
232} // namespace android::scheduler