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