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 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 17 | #include <thread> |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 18 | |
| 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 21 | |
| 22 | #include <scheduler/Timer.h> |
| 23 | |
| 24 | #include "Scheduler/VSyncDispatchTimerQueue.h" |
| 25 | #include "Scheduler/VSyncTracker.h" |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 26 | |
| 27 | using namespace testing; |
| 28 | using namespace std::literals; |
| 29 | |
| 30 | namespace android::scheduler { |
| 31 | |
| 32 | template <typename Rep, typename Per> |
| 33 | constexpr nsecs_t toNs(std::chrono::duration<Rep, Per> const& tp) { |
| 34 | return std::chrono::duration_cast<std::chrono::nanoseconds>(tp).count(); |
| 35 | } |
| 36 | |
| 37 | class FixedRateIdealStubTracker : public VSyncTracker { |
| 38 | public: |
| 39 | FixedRateIdealStubTracker() : mPeriod{toNs(3ms)} {} |
| 40 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 41 | bool addVsyncTimestamp(nsecs_t) final { return true; } |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 42 | |
| 43 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final { |
| 44 | auto const floor = timePoint % mPeriod; |
| 45 | if (floor == 0) { |
| 46 | return timePoint; |
| 47 | } |
| 48 | return timePoint - floor + mPeriod; |
| 49 | } |
| 50 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 51 | nsecs_t currentPeriod() const final { return mPeriod; } |
| 52 | |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 53 | void setPeriod(nsecs_t) final {} |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 54 | void resetModel() final {} |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 55 | bool needsMoreSamples() const final { return false; } |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 56 | bool isVSyncInPhase(nsecs_t, Fps) const final { return false; } |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 57 | void setDivisor(unsigned) final {} |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 58 | void dump(std::string&) const final {} |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 59 | |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 60 | private: |
| 61 | nsecs_t const mPeriod; |
| 62 | }; |
| 63 | |
| 64 | class VRRStubTracker : public VSyncTracker { |
| 65 | public: |
| 66 | VRRStubTracker(nsecs_t period) : mPeriod{period} {} |
| 67 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 68 | bool addVsyncTimestamp(nsecs_t) final { return true; } |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 69 | |
| 70 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t time_point) const final { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 71 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 72 | auto const normalized_to_base = time_point - mBase; |
| 73 | auto const floor = (normalized_to_base) % mPeriod; |
| 74 | if (floor == 0) { |
| 75 | return time_point; |
| 76 | } |
| 77 | return normalized_to_base - floor + mPeriod + mBase; |
| 78 | } |
| 79 | |
| 80 | void set_interval(nsecs_t interval, nsecs_t last_known) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 81 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 82 | mPeriod = interval; |
| 83 | mBase = last_known; |
| 84 | } |
| 85 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 86 | nsecs_t currentPeriod() const final { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 87 | std::lock_guard lock(mMutex); |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 88 | return mPeriod; |
| 89 | } |
| 90 | |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 91 | void setPeriod(nsecs_t) final {} |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 92 | void resetModel() final {} |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 93 | bool needsMoreSamples() const final { return false; } |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 94 | bool isVSyncInPhase(nsecs_t, Fps) const final { return false; } |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 95 | void setDivisor(unsigned) final {} |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 96 | void dump(std::string&) const final {} |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 97 | |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 98 | private: |
| 99 | std::mutex mutable mMutex; |
| 100 | nsecs_t mPeriod; |
| 101 | nsecs_t mBase = 0; |
| 102 | }; |
| 103 | |
| 104 | struct VSyncDispatchRealtimeTest : testing::Test { |
| 105 | static nsecs_t constexpr mDispatchGroupThreshold = toNs(100us); |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 106 | static nsecs_t constexpr mVsyncMoveThreshold = toNs(500us); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 107 | static size_t constexpr mIterations = 20; |
| 108 | }; |
| 109 | |
| 110 | class RepeatingCallbackReceiver { |
| 111 | public: |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame^] | 112 | RepeatingCallbackReceiver(VSyncDispatch& dispatch, nsecs_t workload, nsecs_t readyDuration) |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 113 | : mWorkload(workload), |
| 114 | mReadyDuration(readyDuration), |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 115 | mCallback( |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 116 | dispatch, [&](auto time, auto, auto) { callback_called(time); }, "repeat0") {} |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 117 | |
| 118 | void repeatedly_schedule(size_t iterations, std::function<void(nsecs_t)> const& onEachFrame) { |
| 119 | mCallbackTimes.reserve(iterations); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 120 | mCallback.schedule( |
| 121 | {.workDuration = mWorkload, |
| 122 | .readyDuration = mReadyDuration, |
| 123 | .earliestVsync = systemTime(SYSTEM_TIME_MONOTONIC) + mWorkload + mReadyDuration}); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 124 | |
| 125 | for (auto i = 0u; i < iterations - 1; i++) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 126 | std::unique_lock lock(mMutex); |
| 127 | mCv.wait(lock, [&] { return mCalled; }); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 128 | mCalled = false; |
| 129 | auto last = mLastTarget; |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 130 | lock.unlock(); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 131 | |
| 132 | onEachFrame(last); |
| 133 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 134 | mCallback.schedule({.workDuration = mWorkload, |
| 135 | .readyDuration = mReadyDuration, |
| 136 | .earliestVsync = last + mWorkload + mReadyDuration}); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // wait for the last callback. |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 140 | std::unique_lock lock(mMutex); |
| 141 | mCv.wait(lock, [&] { return mCalled; }); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void with_callback_times(std::function<void(std::vector<nsecs_t> const&)> const& fn) const { |
| 145 | fn(mCallbackTimes); |
| 146 | } |
| 147 | |
| 148 | private: |
| 149 | void callback_called(nsecs_t time) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 150 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 151 | mCallbackTimes.push_back(time); |
| 152 | mCalled = true; |
| 153 | mLastTarget = time; |
| 154 | mCv.notify_all(); |
| 155 | } |
| 156 | |
| 157 | nsecs_t const mWorkload; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 158 | nsecs_t const mReadyDuration; |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 159 | VSyncCallbackRegistration mCallback; |
| 160 | |
| 161 | std::mutex mMutex; |
| 162 | std::condition_variable mCv; |
| 163 | bool mCalled = false; |
| 164 | nsecs_t mLastTarget = 0; |
| 165 | std::vector<nsecs_t> mCallbackTimes; |
| 166 | }; |
| 167 | |
| 168 | TEST_F(VSyncDispatchRealtimeTest, triple_alarm) { |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame^] | 169 | FixedRateIdealStubTracker tracker; |
| 170 | VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold, |
| 171 | mVsyncMoveThreshold); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 172 | |
| 173 | static size_t constexpr num_clients = 3; |
| 174 | std::array<RepeatingCallbackReceiver, num_clients> |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 175 | cb_receiver{RepeatingCallbackReceiver(dispatch, toNs(1500us), toNs(2500us)), |
| 176 | RepeatingCallbackReceiver(dispatch, toNs(0h), toNs(0h)), |
| 177 | RepeatingCallbackReceiver(dispatch, toNs(1ms), toNs(3ms))}; |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 178 | |
| 179 | auto const on_each_frame = [](nsecs_t) {}; |
| 180 | std::array<std::thread, num_clients> threads{ |
| 181 | std::thread([&] { cb_receiver[0].repeatedly_schedule(mIterations, on_each_frame); }), |
| 182 | std::thread([&] { cb_receiver[1].repeatedly_schedule(mIterations, on_each_frame); }), |
| 183 | std::thread([&] { cb_receiver[2].repeatedly_schedule(mIterations, on_each_frame); }), |
| 184 | }; |
| 185 | |
| 186 | for (auto it = threads.rbegin(); it != threads.rend(); it++) { |
| 187 | it->join(); |
| 188 | } |
| 189 | |
| 190 | for (auto const& cbs : cb_receiver) { |
| 191 | cbs.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); }); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // starts at 333hz, slides down to 43hz |
| 196 | TEST_F(VSyncDispatchRealtimeTest, vascillating_vrr) { |
| 197 | auto next_vsync_interval = toNs(3ms); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame^] | 198 | VRRStubTracker tracker(next_vsync_interval); |
| 199 | VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold, |
| 200 | mVsyncMoveThreshold); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 201 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 202 | RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms)); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 203 | |
| 204 | auto const on_each_frame = [&](nsecs_t last_known) { |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame^] | 205 | tracker.set_interval(next_vsync_interval += toNs(1ms), last_known); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); }); |
| 209 | eventThread.join(); |
| 210 | |
| 211 | cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); }); |
| 212 | } |
| 213 | |
| 214 | // starts at 333hz, jumps to 200hz at frame 10 |
| 215 | TEST_F(VSyncDispatchRealtimeTest, fixed_jump) { |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame^] | 216 | VRRStubTracker tracker(toNs(3ms)); |
| 217 | VSyncDispatchTimerQueue dispatch(std::make_unique<Timer>(), tracker, mDispatchGroupThreshold, |
| 218 | mVsyncMoveThreshold); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 219 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 220 | RepeatingCallbackReceiver cb_receiver(dispatch, toNs(1ms), toNs(5ms)); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 221 | |
| 222 | auto jump_frame_counter = 0u; |
| 223 | auto constexpr jump_frame_at = 10u; |
| 224 | auto const on_each_frame = [&](nsecs_t last_known) { |
| 225 | if (jump_frame_counter++ == jump_frame_at) { |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame^] | 226 | tracker.set_interval(toNs(5ms), last_known); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | std::thread eventThread([&] { cb_receiver.repeatedly_schedule(mIterations, on_each_frame); }); |
| 230 | eventThread.join(); |
| 231 | |
| 232 | cb_receiver.with_callback_times([](auto times) { EXPECT_THAT(times.size(), Eq(mIterations)); }); |
| 233 | } |
| 234 | } // namespace android::scheduler |