blob: 26a8ec0b07e05ab64c55f08afbb785a6a3c4726a [file] [log] [blame]
Kevin DuBoise4f27a82019-11-12 11:41:41 -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#pragma once
18
19#include <android-base/thread_annotations.h>
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080020#include <array>
Kevin DuBoise4f27a82019-11-12 11:41:41 -080021#include <functional>
22#include <memory>
23#include <mutex>
24#include <string>
25#include <string_view>
26#include <unordered_map>
27
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080028#include "SchedulerUtils.h"
Kevin DuBoise4f27a82019-11-12 11:41:41 -080029#include "VSyncDispatch.h"
30
31namespace android::scheduler {
32
33// VSyncDispatchTimerQueueEntry is a helper class representing internal state for each entry in
34// VSyncDispatchTimerQueue hoisted to public for unit testing.
35class VSyncDispatchTimerQueueEntry {
36public:
37 // This is the state of the entry. There are 3 states, armed, running, disarmed.
38 // Valid transition: disarmed -> armed ( when scheduled )
39 // Valid transition: armed -> running -> disarmed ( when timer is called)
40 // Valid transition: armed -> disarmed ( when cancelled )
Kevin DuBois2968afc2020-01-14 09:48:50 -080041 VSyncDispatchTimerQueueEntry(std::string const& name, VSyncDispatch::Callback const& fn,
Kevin DuBoisc94ca832019-11-26 12:56:24 -080042 nsecs_t minVsyncDistance);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080043 std::string_view name() const;
44
45 // Start: functions that are not threadsafe.
46 // Return the last vsync time this callback was invoked.
47 std::optional<nsecs_t> lastExecutedVsyncTarget() const;
48
49 // This moves the state from disarmed->armed and will calculate the wakeupTime.
Kevin DuBois2311b1a2019-11-18 16:19:08 -080050 ScheduleResult schedule(nsecs_t workDuration, nsecs_t earliestVsync, VSyncTracker& tracker,
51 nsecs_t now);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080052 // This will update armed entries with the latest vsync information. Entry remains armed.
53 void update(VSyncTracker& tracker, nsecs_t now);
54
55 // This will return empty if not armed, or the next calculated wakeup time if armed.
56 // It will not update the wakeupTime.
57 std::optional<nsecs_t> wakeupTime() const;
58
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080059 std::optional<nsecs_t> targetVsync() const;
60
Kevin DuBoise4f27a82019-11-12 11:41:41 -080061 // This moves state from armed->disarmed.
62 void disarm();
63
64 // This moves the state from armed->running.
65 // Store the timestamp that this was intended for as the last called timestamp.
66 nsecs_t executing();
67 // End: functions that are not threadsafe.
68
Kevin DuBois2968afc2020-01-14 09:48:50 -080069 // Invoke the callback with the two given timestamps, moving the state from running->disarmed.
70 void callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080071 // Block calling thread while the callback is executing.
72 void ensureNotRunning();
73
Ady Abraham5e7371c2020-03-24 14:47:24 -070074 void dump(std::string& result) const;
75
Kevin DuBoise4f27a82019-11-12 11:41:41 -080076private:
Kevin DuBoise4f27a82019-11-12 11:41:41 -080077 std::string const mName;
Kevin DuBois2968afc2020-01-14 09:48:50 -080078 VSyncDispatch::Callback const mCallback;
Kevin DuBoise4f27a82019-11-12 11:41:41 -080079
80 nsecs_t mWorkDuration;
81 nsecs_t mEarliestVsync;
Kevin DuBoisc94ca832019-11-26 12:56:24 -080082 nsecs_t const mMinVsyncDistance;
Kevin DuBoise4f27a82019-11-12 11:41:41 -080083
84 struct ArmingInfo {
85 nsecs_t mActualWakeupTime;
86 nsecs_t mActualVsyncTime;
87 };
88 std::optional<ArmingInfo> mArmedInfo;
89 std::optional<nsecs_t> mLastDispatchTime;
90
Ady Abraham5e7371c2020-03-24 14:47:24 -070091 mutable std::mutex mRunningMutex;
Kevin DuBoise4f27a82019-11-12 11:41:41 -080092 std::condition_variable mCv;
93 bool mRunning GUARDED_BY(mRunningMutex) = false;
94};
95
96/*
97 * VSyncDispatchTimerQueue is a class that will dispatch callbacks as per VSyncDispatch interface
98 * using a single timer queue.
99 */
100class VSyncDispatchTimerQueue : public VSyncDispatch {
101public:
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800102 // Constructs a VSyncDispatchTimerQueue.
103 // \param[in] tk A timekeeper.
104 // \param[in] tracker A tracker.
105 // \param[in] timerSlack The threshold at which different similarly timed callbacks
106 // should be grouped into one wakeup.
107 // \param[in] minVsyncDistance The minimum distance between two vsync estimates before the
108 // vsyncs are considered the same vsync event.
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800109 explicit VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk, VSyncTracker& tracker,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800110 nsecs_t timerSlack, nsecs_t minVsyncDistance);
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800111 ~VSyncDispatchTimerQueue();
112
Kevin DuBois2968afc2020-01-14 09:48:50 -0800113 CallbackToken registerCallback(Callback const& callbackFn, std::string callbackName) final;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800114 void unregisterCallback(CallbackToken token) final;
115 ScheduleResult schedule(CallbackToken token, nsecs_t workDuration, nsecs_t earliestVsync) final;
116 CancelResult cancel(CallbackToken token) final;
Ady Abraham5e7371c2020-03-24 14:47:24 -0700117 void dump(std::string& result) const final;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800118
119private:
120 VSyncDispatchTimerQueue(VSyncDispatchTimerQueue const&) = delete;
121 VSyncDispatchTimerQueue& operator=(VSyncDispatchTimerQueue const&) = delete;
122
Ady Abraham2139f732019-11-13 18:56:40 -0800123 using CallbackMap =
124 std::unordered_map<CallbackToken, std::shared_ptr<VSyncDispatchTimerQueueEntry>>;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800125
126 void timerCallback();
127 void setTimer(nsecs_t, nsecs_t) REQUIRES(mMutex);
128 void rearmTimer(nsecs_t now) REQUIRES(mMutex);
129 void rearmTimerSkippingUpdateFor(nsecs_t now, CallbackMap::iterator const& skipUpdate)
130 REQUIRES(mMutex);
131 void cancelTimer() REQUIRES(mMutex);
132
133 static constexpr nsecs_t kInvalidTime = std::numeric_limits<int64_t>::max();
134 std::unique_ptr<TimeKeeper> const mTimeKeeper;
135 VSyncTracker& mTracker;
136 nsecs_t const mTimerSlack;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800137 nsecs_t const mMinVsyncDistance;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800138
139 std::mutex mutable mMutex;
140 size_t mCallbackToken GUARDED_BY(mMutex) = 0;
141
142 CallbackMap mCallbacks GUARDED_BY(mMutex);
143 nsecs_t mIntendedWakeupTime GUARDED_BY(mMutex) = kInvalidTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800144
145 struct TraceBuffer {
146 static constexpr char const kTraceNamePrefix[] = "-alarm in:";
147 static constexpr char const kTraceNameSeparator[] = " for vs:";
148 static constexpr size_t kMaxNamePrint = 4;
149 static constexpr size_t kNumTsPrinted = 2;
150 static constexpr size_t maxlen = kMaxNamePrint + arrayLen(kTraceNamePrefix) +
151 arrayLen(kTraceNameSeparator) - 1 + (kNumTsPrinted * max64print);
152 std::array<char, maxlen> str_buffer;
153 void note(std::string_view name, nsecs_t in, nsecs_t vs);
154 } mTraceBuffer GUARDED_BY(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700155
156 // For debugging purposes
157 nsecs_t mLastTimerCallback GUARDED_BY(mMutex) = kInvalidTime;
158 nsecs_t mLastTimerSchedule GUARDED_BY(mMutex) = kInvalidTime;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800159};
160
161} // namespace android::scheduler