blob: 3408fed3e02d8880e950a73e34e2540b0a694ac7 [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 Abraham5e7371c2020-03-24 14:47:24 -070055 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080056
Kevin DuBoiscc27b502019-11-13 09:40:07 -080057private:
58 nsecs_t const mPeriod;
59};
60
61class VRRStubTracker : public VSyncTracker {
62public:
63 VRRStubTracker(nsecs_t period) : mPeriod{period} {}
64
Kevin DuBois02d5ed92020-01-27 11:05:46 -080065 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080066
67 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final {
68 std::lock_guard<decltype(mMutex)> lk(mMutex);
69 auto const normalized_to_base = time_point - mBase;
70 auto const floor = (normalized_to_base) % mPeriod;
71 if (floor == 0) {
72 return time_point;
73 }
74 return normalized_to_base - floor + mPeriod + mBase;
75 }
76
77 void set_interval(nsecs_t interval, nsecs_t last_known) {
78 std::lock_guard<decltype(mMutex)> lk(mMutex);
79 mPeriod = interval;
80 mBase = last_known;
81 }
82
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080083 nsecs_t currentPeriod() const final {
84 std::lock_guard<decltype(mMutex)> lk(mMutex);
85 return mPeriod;
86 }
87
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080088 void setPeriod(nsecs_t) final {}
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080089 void resetModel() final {}
Kevin DuBoisb818bfa2020-07-10 14:29:36 -070090 bool needsMoreSamples() const final { return false; }
Ady Abraham5e7371c2020-03-24 14:47:24 -070091 void dump(std::string&) const final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080092
Kevin DuBoiscc27b502019-11-13 09:40:07 -080093private:
94 std::mutex mutable mMutex;
95 nsecs_t mPeriod;
96 nsecs_t mBase = 0;
97};
98
99struct VSyncDispatchRealtimeTest : testing::Test {
100 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800101 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800102 static size_t constexpr mIterations = 20;
103};
104
105class RepeatingCallbackReceiver {
106public:
Ady Abraham9c53ee72020-07-22 21:16:18 -0700107 RepeatingCallbackReceiver(VSyncDispatch& dispatch, nsecs_t workload, nsecs_t readyDuration)
108 : mWorkload(workload),
109 mReadyDuration(readyDuration),
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800110 mCallback(
Ady Abraham9c53ee72020-07-22 21:16:18 -0700111 dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800112
113 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
114 mCallbackTimes.reserve(iterations);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700115 mCallback.schedule(
116 {.workDuration = mWorkload,
117 .readyDuration = mReadyDuration,
118 .earliestVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800119
120 for (auto i = 0u; i < iterations - 1; i++) {
121 std::unique_lock<decltype(mMutex)> lk(mMutex);
122 mCv.wait(lk, [&] { return mCalled; });
123 mCalled = false;
124 auto last = mLastTarget;
125 lk.unlock();
126
127 onEachFrame(last);
128
Ady Abraham9c53ee72020-07-22 21:16:18 -0700129 mCallback.schedule({.workDuration = mWorkload,
130 .readyDuration = mReadyDuration,
131 .earliestVsync = last + mWorkload + mReadyDuration});
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800132 }
133
134 // wait for the last callback.
135 std::unique_lock<decltype(mMutex)> lk(mMutex);
136 mCv.wait(lk, [&] { return mCalled; });
137 }
138
139 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
140 fn(mCallbackTimes);
141 }
142
143private:
144 void callback_called(nsecs_t time) {
145 std::lock_guard<decltype(mMutex)> lk(mMutex);
146 mCallbackTimes.push_back(time);
147 mCalled = true;
148 mLastTarget = time;
149 mCv.notify_all();
150 }
151
152 nsecs_t const mWorkload;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700153 nsecs_t const mReadyDuration;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800154 VSyncCallbackRegistration mCallback;
155
156 std::mutex mMutex;
157 std::condition_variable mCv;
158 bool mCalled = false;
159 nsecs_t mLastTarget = 0;
160 std::vector<nsecs_t> mCallbackTimes;
161};
162
163TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
164 FixedRateIdealStubTracker tracker;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800165 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
166 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800167
168 static size_t constexpr num_clients = 3;
169 std::array<RepeatingCallbackReceiver, num_clients>
Ady Abraham9c53ee72020-07-22 21:16:18 -0700170 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)),
171 RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)),
172 RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))};
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800173
174 auto const on_each_frame = [](nsecs_t) {};
175 std::array<std::thread, num_clients> threads{
176 std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }),
177 std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }),
178 std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }),
179 };
180
181 for (auto it = threads.rbegin(); it != threads.rend(); it++) {
182 it->join();
183 }
184
185 for (auto const& cbs : cb_receiver) {
186 cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
187 }
188}
189
190// starts at 333hz, slides down to 43hz
191TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
192 auto next_vsync_interval = toNs(3ms);
193 VRRStubTracker tracker(next_vsync_interval);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800194 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
195 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800196
Ady Abraham9c53ee72020-07-22 21:16:18 -0700197 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800198
199 auto const on_each_frame = [&](nsecs_t last_known) {
200 tracker.set_interval(next_vsync_interval += toNs(1ms), last_known);
201 };
202
203 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
204 eventThread.join();
205
206 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
207}
208
209// starts at 333hz, jumps to 200hz at frame 10
210TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
211 VRRStubTracker tracker(toNs(3ms));
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800212 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
213 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800214
Ady Abraham9c53ee72020-07-22 21:16:18 -0700215 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms));
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800216
217 auto jump_frame_counter = 0u;
218 auto constexpr jump_frame_at = 10u;
219 auto const on_each_frame = [&](nsecs_t last_known) {
220 if (jump_frame_counter++ == jump_frame_at) {
221 tracker.set_interval(toNs(5ms), last_known);
222 }
223 };
224 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
225 eventThread.join();
226
227 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
228}
229} // namespace android::scheduler