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