| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
|  | 26 | using namespace testing; | 
|  | 27 | using namespace std::literals; | 
|  | 28 |  | 
|  | 29 | namespace android::scheduler { | 
|  | 30 |  | 
|  | 31 | template <typename Rep, typename Per> | 
|  | 32 | constexpr nsecs_t toNs(std::chrono::duration<Rep, Per> const& tp) { | 
|  | 33 | return std::chrono::duration_cast<std::chrono::nanoseconds>(tp).count(); | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | class FixedRateIdealStubTracker : public VSyncTracker { | 
|  | 37 | public: | 
|  | 38 | FixedRateIdealStubTracker() : mPeriod{toNs(3ms)} {} | 
|  | 39 |  | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 40 | bool addVsyncTimestamp(nsecs_t) final { return true; } | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 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 DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 50 | nsecs_t currentPeriod() const final { return mPeriod; } | 
|  | 51 |  | 
| Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 52 | void setPeriod(nsecs_t) final {} | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 53 | void resetModel() final {} | 
| Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 54 | bool needsMoreSamples() const final { return false; } | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 55 | bool isVSyncInPhase(nsecs_t, int) const final { return false; } | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 56 | void dump(std::string&) const final {} | 
| Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 57 |  | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 58 | private: | 
|  | 59 | nsecs_t const mPeriod; | 
|  | 60 | }; | 
|  | 61 |  | 
|  | 62 | class VRRStubTracker : public VSyncTracker { | 
|  | 63 | public: | 
|  | 64 | VRRStubTracker(nsecs_t period) : mPeriod{period} {} | 
|  | 65 |  | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 66 | bool addVsyncTimestamp(nsecs_t) final { return true; } | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 67 |  | 
|  | 68 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final { | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 69 | std::lock_guard lock(mMutex); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 70 | 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 Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 79 | std::lock_guard lock(mMutex); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 80 | mPeriod = interval; | 
|  | 81 | mBase = last_known; | 
|  | 82 | } | 
|  | 83 |  | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 84 | nsecs_t currentPeriod() const final { | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 85 | std::lock_guard lock(mMutex); | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 86 | return mPeriod; | 
|  | 87 | } | 
|  | 88 |  | 
| Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 89 | void setPeriod(nsecs_t) final {} | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 90 | void resetModel() final {} | 
| Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 91 | bool needsMoreSamples() const final { return false; } | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 92 | bool isVSyncInPhase(nsecs_t, int) const final { return false; } | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 93 | void dump(std::string&) const final {} | 
| Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 94 |  | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 95 | private: | 
|  | 96 | std::mutex mutable mMutex; | 
|  | 97 | nsecs_t mPeriod; | 
|  | 98 | nsecs_t mBase = 0; | 
|  | 99 | }; | 
|  | 100 |  | 
|  | 101 | struct VSyncDispatchRealtimeTest : testing::Test { | 
|  | 102 | static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us); | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 103 | static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 104 | static size_t constexpr mIterations = 20; | 
|  | 105 | }; | 
|  | 106 |  | 
|  | 107 | class RepeatingCallbackReceiver { | 
|  | 108 | public: | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 109 | RepeatingCallbackReceiver(VSyncDispatch& dispatch, nsecs_t workload, nsecs_t readyDuration) | 
|  | 110 | : mWorkload(workload), | 
|  | 111 | mReadyDuration(readyDuration), | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 112 | mCallback( | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 113 | dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {} | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 114 |  | 
|  | 115 | void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) { | 
|  | 116 | mCallbackTimes.reserve(iterations); | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 117 | mCallback.schedule( | 
|  | 118 | {.workDuration = mWorkload, | 
|  | 119 | .readyDuration = mReadyDuration, | 
|  | 120 | .earliestVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration}); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 121 |  | 
|  | 122 | for (auto i = 0u; i < iterations - 1; i++) { | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 123 | std::unique_lock lock(mMutex); | 
|  | 124 | mCv.wait(lock, [&] { return mCalled; }); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 125 | mCalled = false; | 
|  | 126 | auto last = mLastTarget; | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 127 | lock.unlock(); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 128 |  | 
|  | 129 | onEachFrame(last); | 
|  | 130 |  | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 131 | mCallback.schedule({.workDuration = mWorkload, | 
|  | 132 | .readyDuration = mReadyDuration, | 
|  | 133 | .earliestVsync = last + mWorkload + mReadyDuration}); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
|  | 136 | // wait for the last callback. | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 137 | std::unique_lock lock(mMutex); | 
|  | 138 | mCv.wait(lock, [&] { return mCalled; }); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
|  | 141 | void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const { | 
|  | 142 | fn(mCallbackTimes); | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | private: | 
|  | 146 | void callback_called(nsecs_t time) { | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 147 | std::lock_guard lock(mMutex); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 148 | mCallbackTimes.push_back(time); | 
|  | 149 | mCalled = true; | 
|  | 150 | mLastTarget = time; | 
|  | 151 | mCv.notify_all(); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | nsecs_t const mWorkload; | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 155 | nsecs_t const mReadyDuration; | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 156 | 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 |  | 
|  | 165 | TEST_F(VSyncDispatchRealtimeTest, triple_alarm) { | 
|  | 166 | FixedRateIdealStubTracker tracker; | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 167 | VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold, | 
|  | 168 | mVsyncMoveThreshold); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 169 |  | 
|  | 170 | static size_t constexpr num_clients = 3; | 
|  | 171 | std::array<RepeatingCallbackReceiver, num_clients> | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 172 | cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)), | 
|  | 173 | RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)), | 
|  | 174 | RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))}; | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 175 |  | 
|  | 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 | 
|  | 193 | TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) { | 
|  | 194 | auto next_vsync_interval = toNs(3ms); | 
|  | 195 | VRRStubTracker tracker(next_vsync_interval); | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 196 | VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold, | 
|  | 197 | mVsyncMoveThreshold); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 198 |  | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 199 | RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms)); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 200 |  | 
|  | 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 | 
|  | 212 | TEST_F(VSyncDispatchRealtimeTest, fixed_jump) { | 
|  | 213 | VRRStubTracker tracker(toNs(3ms)); | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 214 | VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold, | 
|  | 215 | mVsyncMoveThreshold); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 216 |  | 
| Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 217 | RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms)); | 
| Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 218 |  | 
|  | 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 |