blob: 42b19933b414829cf80f60ad95642870463ade73 [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
17#include "Scheduler/TimeKeeper.h"
18#include "Scheduler/Timer.h"
19#include "Scheduler/VSyncDispatchTimerQueue.h"
20#include "Scheduler/VSyncTracker.h"
21
22#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24#include <thread>
25
26using namespace testing;
27using namespace std::literals;
28
29namespace android::scheduler {
30
31template <typename Rep, typename Per>
32constexpr nsecs_t toNs(std::chrono::duration<Rep, Per> const& tp) {
33 return std::chrono::duration_cast<std::chrono::nanoseconds>(tp).count();
34}
35
36class FixedRateIdealStubTracker : public VSyncTracker {
37public:
38 FixedRateIdealStubTracker() : mPeriod{toNs(3ms)} {}
39
Kevin DuBois02d5ed92020-01-27 11:05:46 -080040 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080041
42 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final {
43 auto const floor = timePoint % mPeriod;
44 if (floor == 0) {
45 return timePoint;
46 }
47 return timePoint - floor + mPeriod;
48 }
49
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080050 nsecs_t currentPeriod() const final { return mPeriod; }
51
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080052 void setPeriod(nsecs_t) final {}
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080053 void resetModel() final {}
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070054 bool needsMoreSamples() const final { return false; }
Ady Abraham5cc2e262021-03-25 13:09:17 -070055 bool isVSyncInPhase(nsecs_t, Fps) const final { return false; }
Ady Abraham5e7371c2020-03-24 14:47:24 -070056 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080057
Kevin DuBoiscc27b502019-11-13 09:40:07 -080058private:
59 nsecs_t const mPeriod;
60};
61
62class VRRStubTracker : public VSyncTracker {
63public:
64 VRRStubTracker(nsecs_t period) : mPeriod{period} {}
65
Kevin DuBois02d5ed92020-01-27 11:05:46 -080066 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080067
68 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final {
Ady Abraham8cb21882020-08-26 18:22:05 -070069 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080070 auto const normalized_to_base = time_point - mBase;
71 auto const floor = (normalized_to_base) % mPeriod;
72 if (floor == 0) {
73 return time_point;
74 }
75 return normalized_to_base - floor + mPeriod + mBase;
76 }
77
78 void set_interval(nsecs_t interval, nsecs_t last_known) {
Ady Abraham8cb21882020-08-26 18:22:05 -070079 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080080 mPeriod = interval;
81 mBase = last_known;
82 }
83
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080084 nsecs_t currentPeriod() const final {
Ady Abraham8cb21882020-08-26 18:22:05 -070085 std::lock_guard lock(mMutex);
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080086 return mPeriod;
87 }
88
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080089 void setPeriod(nsecs_t) final {}
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080090 void resetModel() final {}
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070091 bool needsMoreSamples() const final { return false; }
Ady Abraham5cc2e262021-03-25 13:09:17 -070092 bool isVSyncInPhase(nsecs_t, Fps) const final { return false; }
Ady Abraham5e7371c2020-03-24 14:47:24 -070093 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080094
Kevin DuBoiscc27b502019-11-13 09:40:07 -080095private:
96 std::mutex mutable mMutex;
97 nsecs_t mPeriod;
98 nsecs_t mBase = 0;
99};
100
101struct VSyncDispatchRealtimeTest : testing::Test {
102 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800103 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800104 static size_t constexpr mIterations = 20;
105};
106
107class RepeatingCallbackReceiver {
108public:
Ady Abraham9c53ee72020-07-22 21:16:18 -0700109 RepeatingCallbackReceiver(VSyncDispatch& dispatch, nsecs_t workload, nsecs_t readyDuration)
110 : mWorkload(workload),
111 mReadyDuration(readyDuration),
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800112 mCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700113 dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800114
115 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
116 mCallbackTimes.reserve(iterations);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700117 mCallback.schedule(
118 {.workDuration = mWorkload,
119 .readyDuration = mReadyDuration,
120 .earliestVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800121
122 for (auto i = 0u; i < iterations - 1; i++) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700123 std::unique_lock lock(mMutex);
124 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800125 mCalled = false;
126 auto last = mLastTarget;
Ady Abraham8cb21882020-08-26 18:22:05 -0700127 lock.unlock();
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800128
129 onEachFrame(last);
130
Ady Abraham9c53ee72020-07-22 21:16:18 -0700131 mCallback.schedule({.workDuration = mWorkload,
132 .readyDuration = mReadyDuration,
133 .earliestVsync = last + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800134 }
135
136 // wait for the last callback.
Ady Abraham8cb21882020-08-26 18:22:05 -0700137 std::unique_lock lock(mMutex);
138 mCv.wait(lock, [&] { return mCalled; });
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800139 }
140
141 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
142 fn(mCallbackTimes);
143 }
144
145private:
146 void callback_called(nsecs_t time) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700147 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800148 mCallbackTimes.push_back(time);
149 mCalled = true;
150 mLastTarget = time;
151 mCv.notify_all();
152 }
153
154 nsecs_t const mWorkload;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700155 nsecs_t const mReadyDuration;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800156 VSyncCallbackRegistration mCallback;
157
158 std::mutex mMutex;
159 std::condition_variable mCv;
160 bool mCalled = false;
161 nsecs_t mLastTarget = 0;
162 std::vector<nsecs_t> mCallbackTimes;
163};
164
165TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
166 FixedRateIdealStubTracker tracker;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800167 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
168 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800169
170 static size_t constexpr num_clients = 3;
171 std::array<RepeatingCallbackReceiver, num_clients>
Ady Abraham9c53ee72020-07-22 21:16:18 -0700172 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)),
173 RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)),
174 RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))};
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800175
176 auto const on_each_frame = [](nsecs_t) {};
177 std::array<std::thread, num_clients> threads{
178 std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }),
179 std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }),
180 std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }),
181 };
182
183 for (auto it = threads.rbegin(); it != threads.rend(); it++) {
184 it->join();
185 }
186
187 for (auto const& cbs : cb_receiver) {
188 cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
189 }
190}
191
192// starts at 333hz, slides down to 43hz
193TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
194 auto next_vsync_interval = toNs(3ms);
195 VRRStubTracker tracker(next_vsync_interval);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800196 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
197 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800198
Ady Abraham9c53ee72020-07-22 21:16:18 -0700199 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800200
201 auto const on_each_frame = [&](nsecs_t last_known) {
202 tracker.set_interval(next_vsync_interval += toNs(1ms), last_known);
203 };
204
205 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
206 eventThread.join();
207
208 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
209}
210
211// starts at 333hz, jumps to 200hz at frame 10
212TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
213 VRRStubTracker tracker(toNs(3ms));
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800214 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
215 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800216
Ady Abraham9c53ee72020-07-22 21:16:18 -0700217 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800218
219 auto jump_frame_counter = 0u;
220 auto constexpr jump_frame_at = 10u;
221 auto const on_each_frame = [&](nsecs_t last_known) {
222 if (jump_frame_counter++ == jump_frame_at) {
223 tracker.set_interval(toNs(5ms), last_known);
224 }
225 };
226 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
227 eventThread.join();
228
229 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
230}
231} // namespace android::scheduler