blob: caac61da29953f6e2efacd65baa426bb1fa19fba [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 DuBoisee2ad9f2019-11-21 11:10:57 -080054
Kevin DuBoiscc27b502019-11-13 09:40:07 -080055private:
56 nsecs_t const mPeriod;
57};
58
59class VRRStubTracker : public VSyncTracker {
60public:
61 VRRStubTracker(nsecs_t period) : mPeriod{period} {}
62
Kevin DuBois02d5ed92020-01-27 11:05:46 -080063 bool addVsyncTimestamp(nsecs_t) final { return true; }
Kevin DuBoiscc27b502019-11-13 09:40:07 -080064
65 nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final {
66 std::lock_guard<decltype(mMutex)> lk(mMutex);
67 auto const normalized_to_base = time_point - mBase;
68 auto const floor = (normalized_to_base) % mPeriod;
69 if (floor == 0) {
70 return time_point;
71 }
72 return normalized_to_base - floor + mPeriod + mBase;
73 }
74
75 void set_interval(nsecs_t interval, nsecs_t last_known) {
76 std::lock_guard<decltype(mMutex)> lk(mMutex);
77 mPeriod = interval;
78 mBase = last_known;
79 }
80
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080081 nsecs_t currentPeriod() const final {
82 std::lock_guard<decltype(mMutex)> lk(mMutex);
83 return mPeriod;
84 }
85
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080086 void setPeriod(nsecs_t) final {}
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080087 void resetModel() final {}
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080088
Kevin DuBoiscc27b502019-11-13 09:40:07 -080089private:
90 std::mutex mutable mMutex;
91 nsecs_t mPeriod;
92 nsecs_t mBase = 0;
93};
94
95struct VSyncDispatchRealtimeTest : testing::Test {
96 static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us);
Kevin DuBoisc94ca832019-11-26 12:56:24 -080097 static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us);
Kevin DuBoiscc27b502019-11-13 09:40:07 -080098 static size_t constexpr mIterations = 20;
99};
100
101class RepeatingCallbackReceiver {
102public:
103 RepeatingCallbackReceiver(VSyncDispatch& dispatch, nsecs_t wl)
104 : mWorkload(wl),
105 mCallback(
Kevin DuBois2968afc2020-01-14 09:48:50 -0800106 dispatch, [&](auto time, auto) { callback_called(time); }, "repeat0") {}
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800107
108 void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) {
109 mCallbackTimes.reserve(iterations);
110 mCallback.schedule(mWorkload, systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload);
111
112 for (auto i = 0u; i < iterations - 1; i++) {
113 std::unique_lock<decltype(mMutex)> lk(mMutex);
114 mCv.wait(lk, [&] { return mCalled; });
115 mCalled = false;
116 auto last = mLastTarget;
117 lk.unlock();
118
119 onEachFrame(last);
120
121 mCallback.schedule(mWorkload, last + mWorkload);
122 }
123
124 // wait for the last callback.
125 std::unique_lock<decltype(mMutex)> lk(mMutex);
126 mCv.wait(lk, [&] { return mCalled; });
127 }
128
129 void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const {
130 fn(mCallbackTimes);
131 }
132
133private:
134 void callback_called(nsecs_t time) {
135 std::lock_guard<decltype(mMutex)> lk(mMutex);
136 mCallbackTimes.push_back(time);
137 mCalled = true;
138 mLastTarget = time;
139 mCv.notify_all();
140 }
141
142 nsecs_t const mWorkload;
143 VSyncCallbackRegistration mCallback;
144
145 std::mutex mMutex;
146 std::condition_variable mCv;
147 bool mCalled = false;
148 nsecs_t mLastTarget = 0;
149 std::vector<nsecs_t> mCallbackTimes;
150};
151
152TEST_F(VSyncDispatchRealtimeTest, triple_alarm) {
153 FixedRateIdealStubTracker tracker;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800154 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
155 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800156
157 static size_t constexpr num_clients = 3;
158 std::array<RepeatingCallbackReceiver, num_clients>
159 cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us)),
160 RepeatingCallbackReceiver(dispatch, toNs(0h)),
161 RepeatingCallbackReceiver(dispatch, toNs(1ms))};
162
163 auto const on_each_frame = [](nsecs_t) {};
164 std::array<std::thread, num_clients> threads{
165 std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }),
166 std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }),
167 std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }),
168 };
169
170 for (auto it = threads.rbegin(); it != threads.rend(); it++) {
171 it->join();
172 }
173
174 for (auto const& cbs : cb_receiver) {
175 cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
176 }
177}
178
179// starts at 333hz, slides down to 43hz
180TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) {
181 auto next_vsync_interval = toNs(3ms);
182 VRRStubTracker tracker(next_vsync_interval);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800183 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
184 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800185
186 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms));
187
188 auto const on_each_frame = [&](nsecs_t last_known) {
189 tracker.set_interval(next_vsync_interval += toNs(1ms), last_known);
190 };
191
192 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
193 eventThread.join();
194
195 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
196}
197
198// starts at 333hz, jumps to 200hz at frame 10
199TEST_F(VSyncDispatchRealtimeTest, fixed_jump) {
200 VRRStubTracker tracker(toNs(3ms));
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800201 VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold,
202 mVsyncMoveThreshold);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800203
204 RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms));
205
206 auto jump_frame_counter = 0u;
207 auto constexpr jump_frame_at = 10u;
208 auto const on_each_frame = [&](nsecs_t last_known) {
209 if (jump_frame_counter++ == jump_frame_at) {
210 tracker.set_interval(toNs(5ms), last_known);
211 }
212 };
213 std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); });
214 eventThread.join();
215
216 cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); });
217}
218} // namespace android::scheduler