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