blob: 47c2deef51d403580408cef243b17789a01752d9 [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 Abrahamace3d052022-11-17 16:25:05 -080057 void setDivisor(unsigned) final {}
Ady Abraham5e7371c2020-03-24 14:47:24 -070058 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080059
Kevin DuBoiscc27b502019-11-13 09:40:07 -080060private:
61 nsecs_t const mPeriod;
62};
63
64class VRRStubTracker : public VSyncTracker {
65public:
66 VRRStubTracker(nsecs_t period) : mPeriod{period} {}
67
Kevin DuBois02d5ed92020-01-27 11:05:46 -080068 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080069
70 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final {
Ady Abraham8cb21882020-08-26 18:22:05 -070071 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080072 auto const normalized_to_base = time_point - mBase;
73 auto const floor = (normalized_to_base) % mPeriod;
74 if (floor == 0) {
75 return time_point;
76 }
77 return normalized_to_base - floor + mPeriod + mBase;
78 }
79
80 void set_interval(nsecs_t interval, nsecs_t last_known) {
Ady Abraham8cb21882020-08-26 18:22:05 -070081 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080082 mPeriod = interval;
83 mBase = last_known;
84 }
85
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080086 nsecs_t currentPeriod() const final {
Ady Abraham8cb21882020-08-26 18:22:05 -070087 std::lock_guard lock(mMutex);
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080088 return mPeriod;
89 }
90
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080091 void setPeriod(nsecs_t) final {}
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080092 void resetModel() final {}
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070093 bool needsMoreSamples() const final { return false; }
Ady Abraham5cc2e262021-03-25 13:09:17 -070094 bool isVSyncInPhase(nsecs_t, Fps) const final { return false; }
Ady Abrahamace3d052022-11-17 16:25:05 -080095 void setDivisor(unsigned) final {}
Ady Abraham5e7371c2020-03-24 14:47:24 -070096 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080097
Kevin DuBoiscc27b502019-11-13 09:40:07 -080098private:
99 std::mutex mutable mMutex;
100 nsecs_t mPeriod;
101 nsecs_t mBase = 0;
102};
103
104struct VSyncDispatchRealtimeTest : testing::Test {
105 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800106 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800107 static size_t constexpr mIterations = 20;
108};
109
110class RepeatingCallbackReceiver {
111public:
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500112 RepeatingCallbackReceiver(VSyncDispatch& dispatch, nsecs_t workload, nsecs_t readyDuration)
Ady Abraham9c53ee72020-07-22 21:16:18 -0700113 : mWorkload(workload),
114 mReadyDuration(readyDuration),
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800115 mCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700116 dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800117
118 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
119 mCallbackTimes.reserve(iterations);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700120 mCallback.schedule(
121 {.workDuration = mWorkload,
122 .readyDuration = mReadyDuration,
123 .earliestVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800124
125 for (auto i = 0u; i < iterations - 1; i++) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700126 std::unique_lock lock(mMutex);
127 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800128 mCalled = false;
129 auto last = mLastTarget;
Ady Abraham8cb21882020-08-26 18:22:05 -0700130 lock.unlock();
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800131
132 onEachFrame(last);
133
Ady Abraham9c53ee72020-07-22 21:16:18 -0700134 mCallback.schedule({.workDuration = mWorkload,
135 .readyDuration = mReadyDuration,
136 .earliestVsync = last + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800137 }
138
139 // wait for the last callback.
Ady Abraham8cb21882020-08-26 18:22:05 -0700140 std::unique_lock lock(mMutex);
141 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800142 }
143
144 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
145 fn(mCallbackTimes);
146 }
147
148private:
149 void callback_called(nsecs_t time) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700150 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800151 mCallbackTimes.push_back(time);
152 mCalled = true;
153 mLastTarget = time;
154 mCv.notify_all();
155 }
156
157 nsecs_t const mWorkload;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700158 nsecs_t const mReadyDuration;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800159 VSyncCallbackRegistration mCallback;
160
161 std::mutex mMutex;
162 std::condition_variable mCv;
163 bool mCalled = false;
164 nsecs_t mLastTarget = 0;
165 std::vector<nsecs_t> mCallbackTimes;
166};
167
168TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500169 FixedRateIdealStubTracker tracker;
170 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
171 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800172
173 static size_t constexpr num_clients = 3;
174 std::array<RepeatingCallbackReceiver, num_clients>
Ady Abraham9c53ee72020-07-22 21:16:18 -0700175 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)),
176 RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)),
177 RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))};
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800178
179 auto const on_each_frame = [](nsecs_t) {};
180 std::array<std::thread, num_clients> threads{
181 std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }),
182 std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }),
183 std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }),
184 };
185
186 for (auto it = threads.rbegin(); it != threads.rend(); it++) {
187 it->join();
188 }
189
190 for (auto const& cbs : cb_receiver) {
191 cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
192 }
193}
194
195// starts at 333hz, slides down to 43hz
196TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
197 auto next_vsync_interval = toNs(3ms);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500198 VRRStubTracker tracker(next_vsync_interval);
199 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
200 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800201
Ady Abraham9c53ee72020-07-22 21:16:18 -0700202 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800203
204 auto const on_each_frame = [&](nsecs_t last_known) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500205 tracker.set_interval(next_vsync_interval += toNs(1ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800206 };
207
208 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
209 eventThread.join();
210
211 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
212}
213
214// starts at 333hz, jumps to 200hz at frame 10
215TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500216 VRRStubTracker tracker(toNs(3ms));
217 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
218 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800219
Ady Abraham9c53ee72020-07-22 21:16:18 -0700220 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800221
222 auto jump_frame_counter = 0u;
223 auto constexpr jump_frame_at = 10u;
224 auto const on_each_frame = [&](nsecs_t last_known) {
225 if (jump_frame_counter++ == jump_frame_at) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500226 tracker.set_interval(toNs(5ms), last_known);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800227 }
228 };
229 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
230 eventThread.join();
231
232 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
233}
234} // namespace android::scheduler