blob: cd156176c7666a4e172878c67b8200f75d67426a [file] [log] [blame]
Kevin DuBois305bef12019-10-09 13:23:27 -07001/*
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#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Ady Abraham5e7371c2020-03-24 14:47:24 -070018#include <android-base/stringprintf.h>
Kevin DuBois305bef12019-10-09 13:23:27 -070019#include <utils/Trace.h>
20#include <vector>
21
22#include "TimeKeeper.h"
Kevin DuBoise4f27a82019-11-12 11:41:41 -080023#include "VSyncDispatchTimerQueue.h"
Kevin DuBois305bef12019-10-09 13:23:27 -070024#include "VSyncTracker.h"
25
26namespace android::scheduler {
Ady Abraham5e7371c2020-03-24 14:47:24 -070027using base::StringAppendF;
Kevin DuBois305bef12019-10-09 13:23:27 -070028
Kevin DuBoise4f27a82019-11-12 11:41:41 -080029VSyncDispatch::~VSyncDispatch() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070030VSyncTracker::~VSyncTracker() = default;
31TimeKeeper::~TimeKeeper() = default;
32
Kevin DuBoise4f27a82019-11-12 11:41:41 -080033VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string const& name,
Kevin DuBois2968afc2020-01-14 09:48:50 -080034 VSyncDispatch::Callback const& cb,
Kevin DuBoisc94ca832019-11-26 12:56:24 -080035 nsecs_t minVsyncDistance)
36 : mName(name),
37 mCallback(cb),
38 mWorkDuration(0),
39 mEarliestVsync(0),
40 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -070041
Kevin DuBoise4f27a82019-11-12 11:41:41 -080042std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070043 return mLastDispatchTime;
44}
45
Kevin DuBoise4f27a82019-11-12 11:41:41 -080046std::string_view VSyncDispatchTimerQueueEntry::name() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070047 return mName;
48}
49
Kevin DuBoise4f27a82019-11-12 11:41:41 -080050std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070051 if (!mArmedInfo) {
52 return {};
53 }
54 return {mArmedInfo->mActualWakeupTime};
55}
56
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080057std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::targetVsync() const {
58 if (!mArmedInfo) {
59 return {};
60 }
61 return {mArmedInfo->mActualVsyncTime};
62}
63
Kevin DuBois2311b1a2019-11-18 16:19:08 -080064ScheduleResult VSyncDispatchTimerQueueEntry::schedule(nsecs_t workDuration, nsecs_t earliestVsync,
65 VSyncTracker& tracker, nsecs_t now) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -080066 auto nextVsyncTime =
Kevin DuBois2311b1a2019-11-18 16:19:08 -080067 tracker.nextAnticipatedVSyncTimeFrom(std::max(earliestVsync, now + workDuration));
Kevin DuBoisc94ca832019-11-26 12:56:24 -080068
69 bool const wouldSkipAVsyncTarget =
70 mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance));
71 if (wouldSkipAVsyncTarget) {
72 return ScheduleResult::Scheduled;
73 }
74
75 bool const alreadyDispatchedForVsync = mLastDispatchTime &&
76 ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
77 (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
78 if (alreadyDispatchedForVsync) {
79 nextVsyncTime =
80 tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance);
Kevin DuBois2311b1a2019-11-18 16:19:08 -080081 }
82
83 auto const nextWakeupTime = nextVsyncTime - workDuration;
Kevin DuBois305bef12019-10-09 13:23:27 -070084 mWorkDuration = workDuration;
85 mEarliestVsync = earliestVsync;
Kevin DuBois2311b1a2019-11-18 16:19:08 -080086 mArmedInfo = {nextWakeupTime, nextVsyncTime};
Kevin DuBoisc94ca832019-11-26 12:56:24 -080087 return ScheduleResult::Scheduled;
Kevin DuBois305bef12019-10-09 13:23:27 -070088}
89
Kevin DuBoise4f27a82019-11-12 11:41:41 -080090void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -070091 if (!mArmedInfo) {
92 return;
93 }
Kevin DuBois305bef12019-10-09 13:23:27 -070094 auto const nextVsyncTime =
95 tracker.nextAnticipatedVSyncTimeFrom(std::max(mEarliestVsync, now + mWorkDuration));
96 mArmedInfo = {nextVsyncTime - mWorkDuration, nextVsyncTime};
97}
98
Kevin DuBoise4f27a82019-11-12 11:41:41 -080099void VSyncDispatchTimerQueueEntry::disarm() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700100 mArmedInfo.reset();
101}
102
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800103nsecs_t VSyncDispatchTimerQueueEntry::executing() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700104 mLastDispatchTime = mArmedInfo->mActualVsyncTime;
105 disarm();
106 return *mLastDispatchTime;
107}
108
Kevin DuBois2968afc2020-01-14 09:48:50 -0800109void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700110 {
111 std::lock_guard<std::mutex> lk(mRunningMutex);
112 mRunning = true;
113 }
114
Kevin DuBois2968afc2020-01-14 09:48:50 -0800115 mCallback(vsyncTimestamp, wakeupTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700116
117 std::lock_guard<std::mutex> lk(mRunningMutex);
118 mRunning = false;
119 mCv.notify_all();
120}
121
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800122void VSyncDispatchTimerQueueEntry::ensureNotRunning() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700123 std::unique_lock<std::mutex> lk(mRunningMutex);
124 mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; });
125}
126
Ady Abraham5e7371c2020-03-24 14:47:24 -0700127void VSyncDispatchTimerQueueEntry::dump(std::string& result) const {
128 std::lock_guard<std::mutex> lk(mRunningMutex);
129 std::string armedInfo;
130 if (mArmedInfo) {
131 StringAppendF(&armedInfo, "[wake up in %.2fms for vsync %.2fms from now]",
132 (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f,
133 (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f);
134 }
135
136 StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(),
137 mRunning ? "(in callback function)" : "", armedInfo.c_str());
138 StringAppendF(&result, "\t\t\tmWorkDuration: %.2fms mEarliestVsync: %.2fms relative to now\n",
139 mWorkDuration / 1e6f, (mEarliestVsync - systemTime()) / 1e6f);
140
141 if (mLastDispatchTime) {
142 StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n",
143 (systemTime() - *mLastDispatchTime) / 1e6f);
144 } else {
145 StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n");
146 }
147}
148
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800149VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800150 VSyncTracker& tracker, nsecs_t timerSlack,
151 nsecs_t minVsyncDistance)
152 : mTimeKeeper(std::move(tk)),
153 mTracker(tracker),
154 mTimerSlack(timerSlack),
155 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700156
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800157VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700158 std::lock_guard<decltype(mMutex)> lk(mMutex);
159 cancelTimer();
160}
161
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800162void VSyncDispatchTimerQueue::cancelTimer() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700163 mIntendedWakeupTime = kInvalidTime;
164 mTimeKeeper->alarmCancel();
165}
166
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800167void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700168 mIntendedWakeupTime = targetTime;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800169 mTimeKeeper->alarmIn(std::bind(&VSyncDispatchTimerQueue::timerCallback, this),
170 targetTime - now);
Ady Abraham75398722020-04-07 14:08:45 -0700171 mLastTimerSchedule = mTimeKeeper->now();
Kevin DuBois305bef12019-10-09 13:23:27 -0700172}
173
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800174void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700175 rearmTimerSkippingUpdateFor(now, mCallbacks.end());
176}
177
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800178void VSyncDispatchTimerQueue::TraceBuffer::note(std::string_view name, nsecs_t alarmIn,
179 nsecs_t vsFor) {
180 if (ATRACE_ENABLED()) {
181 snprintf(str_buffer.data(), str_buffer.size(), "%.4s%s%" PRId64 "%s%" PRId64,
182 name.substr(0, kMaxNamePrint).data(), kTraceNamePrefix, alarmIn,
183 kTraceNameSeparator, vsFor);
184 }
185 ATRACE_NAME(str_buffer.data());
186}
187
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800188void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor(
189 nsecs_t now, CallbackMap::iterator const& skipUpdateIt) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700190 std::optional<nsecs_t> min;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800191 std::optional<nsecs_t> targetVsync;
192 std::optional<std::string_view> nextWakeupName;
Kevin DuBois305bef12019-10-09 13:23:27 -0700193 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
194 auto& callback = it->second;
195 if (!callback->wakeupTime()) {
196 continue;
197 }
198
199 if (it != skipUpdateIt) {
200 callback->update(mTracker, now);
201 }
202 auto const wakeupTime = *callback->wakeupTime();
203 if (!min || (min && *min > wakeupTime)) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800204 nextWakeupName = callback->name();
Kevin DuBois305bef12019-10-09 13:23:27 -0700205 min = wakeupTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800206 targetVsync = callback->targetVsync();
Kevin DuBois305bef12019-10-09 13:23:27 -0700207 }
208 }
209
210 if (min && (min < mIntendedWakeupTime)) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800211 if (targetVsync && nextWakeupName) {
212 mTraceBuffer.note(*nextWakeupName, *min - now, *targetVsync - now);
213 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700214 setTimer(*min, now);
215 } else {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800216 ATRACE_NAME("cancel timer");
Kevin DuBois305bef12019-10-09 13:23:27 -0700217 cancelTimer();
218 }
219}
220
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800221void VSyncDispatchTimerQueue::timerCallback() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700222 struct Invocation {
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800223 std::shared_ptr<VSyncDispatchTimerQueueEntry> callback;
Kevin DuBois2968afc2020-01-14 09:48:50 -0800224 nsecs_t vsyncTimestamp;
225 nsecs_t wakeupTimestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700226 };
227 std::vector<Invocation> invocations;
228 {
229 std::lock_guard<decltype(mMutex)> lk(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700230 mLastTimerCallback = mTimeKeeper->now();
Kevin DuBois305bef12019-10-09 13:23:27 -0700231 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
232 auto& callback = it->second;
233 auto const wakeupTime = callback->wakeupTime();
234 if (!wakeupTime) {
235 continue;
236 }
237
238 if (*wakeupTime < mIntendedWakeupTime + mTimerSlack) {
239 callback->executing();
240 invocations.emplace_back(
Kevin DuBois2968afc2020-01-14 09:48:50 -0800241 Invocation{callback, *callback->lastExecutedVsyncTarget(), *wakeupTime});
Kevin DuBois305bef12019-10-09 13:23:27 -0700242 }
243 }
244
245 mIntendedWakeupTime = kInvalidTime;
246 rearmTimer(mTimeKeeper->now());
247 }
248
249 for (auto const& invocation : invocations) {
Kevin DuBois2968afc2020-01-14 09:48:50 -0800250 invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700251 }
252}
253
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800254VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback(
Kevin DuBois2968afc2020-01-14 09:48:50 -0800255 Callback const& callbackFn, std::string callbackName) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700256 std::lock_guard<decltype(mMutex)> lk(mMutex);
257 return CallbackToken{
258 mCallbacks
259 .emplace(++mCallbackToken,
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800260 std::make_shared<VSyncDispatchTimerQueueEntry>(callbackName,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800261 callbackFn,
262 mMinVsyncDistance))
Kevin DuBois305bef12019-10-09 13:23:27 -0700263 .first->first};
264}
265
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800266void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) {
267 std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr;
Kevin DuBois305bef12019-10-09 13:23:27 -0700268 {
269 std::lock_guard<decltype(mMutex)> lk(mMutex);
270 auto it = mCallbacks.find(token);
271 if (it != mCallbacks.end()) {
272 entry = it->second;
273 mCallbacks.erase(it);
274 }
275 }
276
277 if (entry) {
278 entry->ensureNotRunning();
279 }
280}
281
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800282ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token, nsecs_t workDuration,
283 nsecs_t earliestVsync) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700284 auto result = ScheduleResult::Error;
285 {
286 std::lock_guard<decltype(mMutex)> lk(mMutex);
287
288 auto it = mCallbacks.find(token);
289 if (it == mCallbacks.end()) {
290 return result;
291 }
292 auto& callback = it->second;
Kevin DuBois305bef12019-10-09 13:23:27 -0700293 auto const now = mTimeKeeper->now();
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800294 result = callback->schedule(workDuration, earliestVsync, mTracker, now);
295 if (result == ScheduleResult::CannotSchedule) {
296 return result;
Kevin DuBois305bef12019-10-09 13:23:27 -0700297 }
298
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800299 if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700300 rearmTimerSkippingUpdateFor(now, it);
301 }
302 }
303
304 return result;
305}
306
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800307CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700308 std::lock_guard<decltype(mMutex)> lk(mMutex);
309
310 auto it = mCallbacks.find(token);
311 if (it == mCallbacks.end()) {
312 return CancelResult::Error;
313 }
314 auto& callback = it->second;
315
316 if (callback->wakeupTime()) {
317 callback->disarm();
318 mIntendedWakeupTime = kInvalidTime;
319 rearmTimer(mTimeKeeper->now());
320 return CancelResult::Cancelled;
321 }
322 return CancelResult::TooLate;
323}
324
Ady Abraham5e7371c2020-03-24 14:47:24 -0700325void VSyncDispatchTimerQueue::dump(std::string& result) const {
326 std::lock_guard<decltype(mMutex)> lk(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700327 StringAppendF(&result, "\tTimer:\n");
328 mTimeKeeper->dump(result);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700329 StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f,
330 mMinVsyncDistance / 1e6f);
331 StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n",
Ady Abraham75398722020-04-07 14:08:45 -0700332 (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f);
333 StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n",
334 (mTimeKeeper->now() - mLastTimerCallback) / 1e6f,
335 (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700336 StringAppendF(&result, "\tCallbacks:\n");
337 for (const auto& [token, entry] : mCallbacks) {
338 entry->dump(result);
339 }
340}
341
Kevin DuBois305bef12019-10-09 13:23:27 -0700342VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncDispatch& dispatch,
Kevin DuBois2968afc2020-01-14 09:48:50 -0800343 VSyncDispatch::Callback const& callbackFn,
Kevin DuBois305bef12019-10-09 13:23:27 -0700344 std::string const& callbackName)
345 : mDispatch(dispatch),
346 mToken(dispatch.registerCallback(callbackFn, callbackName)),
347 mValidToken(true) {}
348
349VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other)
350 : mDispatch(other.mDispatch),
351 mToken(std::move(other.mToken)),
352 mValidToken(std::move(other.mValidToken)) {
353 other.mValidToken = false;
354}
355
356VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) {
357 mDispatch = std::move(other.mDispatch);
358 mToken = std::move(other.mToken);
359 mValidToken = std::move(other.mValidToken);
360 other.mValidToken = false;
361 return *this;
362}
363
364VSyncCallbackRegistration::~VSyncCallbackRegistration() {
365 if (mValidToken) mDispatch.get().unregisterCallback(mToken);
366}
367
368ScheduleResult VSyncCallbackRegistration::schedule(nsecs_t workDuration, nsecs_t earliestVsync) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800369 if (!mValidToken) {
370 return ScheduleResult::Error;
371 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700372 return mDispatch.get().schedule(mToken, workDuration, earliestVsync);
373}
374
375CancelResult VSyncCallbackRegistration::cancel() {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800376 if (!mValidToken) {
377 return CancelResult::Error;
378 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700379 return mDispatch.get().cancel(mToken);
380}
381
382} // namespace android::scheduler