blob: 68f9321c7954bc370746fa74caf6c65e30ae77d4 [file] [log] [blame]
Kevin DuBoiscc27b502019-11-13 09:40:07 -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#undef LOG_TAG
18#define LOG_TAG "SchedulerTimer"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070020
21#include <chrono>
22#include <cstdint>
23
Kevin DuBoiscc27b502019-11-13 09:40:07 -080024#include <sys/epoll.h>
25#include <sys/timerfd.h>
26#include <sys/unistd.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070027
28#include <android-base/stringprintf.h>
29#include <ftl/enum.h>
30#include <log/log.h>
Kevin DuBoiscc27b502019-11-13 09:40:07 -080031#include <utils/Trace.h>
Kevin DuBoiscc27b502019-11-13 09:40:07 -080032
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080033#include "SchedulerUtils.h"
Kevin DuBoiscc27b502019-11-13 09:40:07 -080034#include "Timer.h"
35
36namespace android::scheduler {
Ady Abraham75398722020-04-07 14:08:45 -070037using base::StringAppendF;
Kevin DuBoiscc27b502019-11-13 09:40:07 -080038
39static constexpr size_t kReadPipe = 0;
40static constexpr size_t kWritePipe = 1;
41
Ady Abraham75398722020-04-07 14:08:45 -070042Timer::Timer() {
43 reset();
44 mDispatchThread = std::thread([this]() { threadMain(); });
Kevin DuBoiscc27b502019-11-13 09:40:07 -080045}
46
47Timer::~Timer() {
48 endDispatch();
49 mDispatchThread.join();
Ady Abraham75398722020-04-07 14:08:45 -070050 cleanup();
51}
Kevin DuBoiscc27b502019-11-13 09:40:07 -080052
Ady Abraham75398722020-04-07 14:08:45 -070053void Timer::reset() {
Ady Abraham3a3815a2021-12-23 14:32:07 -080054 std::function<void()> cb;
55 {
56 std::lock_guard lock(mMutex);
57 if (mExpectingCallback && mCallback) {
58 cb = mCallback;
59 }
60
61 cleanup();
62 mTimerFd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
63 mEpollFd = epoll_create1(EPOLL_CLOEXEC);
64 if (pipe2(mPipes.data(), O_CLOEXEC | O_NONBLOCK)) {
65 ALOGE("could not create TimerDispatch mPipes");
66 }
67 }
68 if (cb) {
69 setDebugState(DebugState::InCallback);
70 cb();
71 setDebugState(DebugState::Running);
72 }
Ady Abraham75398722020-04-07 14:08:45 -070073 setDebugState(DebugState::Reset);
74}
75
76void Timer::cleanup() {
77 if (mTimerFd != -1) {
78 close(mTimerFd);
79 mTimerFd = -1;
80 }
81
82 if (mEpollFd != -1) {
83 close(mEpollFd);
84 mEpollFd = -1;
85 }
86
87 if (mPipes[kReadPipe] != -1) {
88 close(mPipes[kReadPipe]);
89 mPipes[kReadPipe] = -1;
90 }
91
92 if (mPipes[kWritePipe] != -1) {
93 close(mPipes[kWritePipe]);
94 mPipes[kWritePipe] = -1;
95 }
Ady Abraham3a3815a2021-12-23 14:32:07 -080096 mExpectingCallback = false;
97 mCallback = {};
Kevin DuBoiscc27b502019-11-13 09:40:07 -080098}
99
100void Timer::endDispatch() {
101 static constexpr unsigned char end = 'e';
102 write(mPipes[kWritePipe], &end, sizeof(end));
103}
104
105nsecs_t Timer::now() const {
106 return systemTime(SYSTEM_TIME_MONOTONIC);
107}
108
Ady Abrahamb491c902020-08-15 15:47:56 -0700109void Timer::alarmAt(std::function<void()> const& cb, nsecs_t time) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700110 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800111 using namespace std::literals;
112 static constexpr int ns_per_s =
113 std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count();
114
115 mCallback = cb;
Ady Abraham3a3815a2021-12-23 14:32:07 -0800116 mExpectingCallback = true;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800117
118 struct itimerspec old_timer;
119 struct itimerspec new_timer {
120 .it_interval = {.tv_sec = 0, .tv_nsec = 0},
Ady Abrahamb491c902020-08-15 15:47:56 -0700121 .it_value = {.tv_sec = static_cast<long>(time / ns_per_s),
122 .tv_nsec = static_cast<long>(time % ns_per_s)},
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800123 };
124
Ady Abrahamb491c902020-08-15 15:47:56 -0700125 if (timerfd_settime(mTimerFd, TFD_TIMER_ABSTIME, &new_timer, &old_timer)) {
Kevin DuBois127a2d92019-12-04 13:52:52 -0800126 ALOGW("Failed to set timerfd %s (%i)", strerror(errno), errno);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800127 }
128}
129
130void Timer::alarmCancel() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700131 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800132
133 struct itimerspec old_timer;
134 struct itimerspec new_timer {
135 .it_interval = {.tv_sec = 0, .tv_nsec = 0},
136 .it_value = {
137 .tv_sec = 0,
138 .tv_nsec = 0,
139 },
140 };
141
142 if (timerfd_settime(mTimerFd, 0, &new_timer, &old_timer)) {
143 ALOGW("Failed to disarm timerfd");
144 }
145}
146
Ady Abraham75398722020-04-07 14:08:45 -0700147void Timer::threadMain() {
148 while (dispatch()) {
149 reset();
150 }
151}
152
153bool Timer::dispatch() {
154 setDebugState(DebugState::Running);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800155 struct sched_param param = {0};
156 param.sched_priority = 2;
157 if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) != 0) {
158 ALOGW("Failed to set SCHED_FIFO on dispatch thread");
159 }
160
161 if (pthread_setname_np(pthread_self(), "TimerDispatch")) {
162 ALOGW("Failed to set thread name on dispatch thread");
163 }
164
165 enum DispatchType : uint32_t { TIMER, TERMINATE, MAX_DISPATCH_TYPE };
166 epoll_event timerEvent;
167 timerEvent.events = EPOLLIN;
168 timerEvent.data.u32 = DispatchType::TIMER;
169 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mTimerFd, &timerEvent) == -1) {
170 ALOGE("Error adding timer fd to epoll dispatch loop");
Ady Abraham75398722020-04-07 14:08:45 -0700171 return true;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800172 }
173
174 epoll_event terminateEvent;
175 terminateEvent.events = EPOLLIN;
176 terminateEvent.data.u32 = DispatchType::TERMINATE;
177 if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mPipes[kReadPipe], &terminateEvent) == -1) {
178 ALOGE("Error adding control fd to dispatch loop");
Ady Abraham75398722020-04-07 14:08:45 -0700179 return true;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800180 }
181
182 uint64_t iteration = 0;
183 char const traceNamePrefix[] = "TimerIteration #";
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800184 static constexpr size_t maxlen = arrayLen(traceNamePrefix) + max64print;
185 std::array<char, maxlen> str_buffer;
Ady Abraham75398722020-04-07 14:08:45 -0700186
187 while (true) {
188 setDebugState(DebugState::Waiting);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800189 epoll_event events[DispatchType::MAX_DISPATCH_TYPE];
190 int nfds = epoll_wait(mEpollFd, events, DispatchType::MAX_DISPATCH_TYPE, -1);
191
Ady Abraham75398722020-04-07 14:08:45 -0700192 setDebugState(DebugState::Running);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800193 if (ATRACE_ENABLED()) {
194 snprintf(str_buffer.data(), str_buffer.size(), "%s%" PRIu64, traceNamePrefix,
195 iteration++);
196 ATRACE_NAME(str_buffer.data());
197 }
198
199 if (nfds == -1) {
200 if (errno != EINTR) {
Ady Abraham75398722020-04-07 14:08:45 -0700201 ALOGE("Error waiting on epoll: %s", strerror(errno));
202 return true;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800203 }
204 }
205
206 for (auto i = 0; i < nfds; i++) {
207 if (events[i].data.u32 == DispatchType::TIMER) {
208 static uint64_t mIgnored = 0;
Ady Abraham75398722020-04-07 14:08:45 -0700209 setDebugState(DebugState::Reading);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800210 read(mTimerFd, &mIgnored, sizeof(mIgnored));
Ady Abraham75398722020-04-07 14:08:45 -0700211 setDebugState(DebugState::Running);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800212 std::function<void()> cb;
213 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700214 std::lock_guard lock(mMutex);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800215 cb = mCallback;
Ady Abraham3a3815a2021-12-23 14:32:07 -0800216 mExpectingCallback = false;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800217 }
218 if (cb) {
Ady Abraham75398722020-04-07 14:08:45 -0700219 setDebugState(DebugState::InCallback);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800220 cb();
Ady Abraham75398722020-04-07 14:08:45 -0700221 setDebugState(DebugState::Running);
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800222 }
223 }
224 if (events[i].data.u32 == DispatchType::TERMINATE) {
Ady Abraham75398722020-04-07 14:08:45 -0700225 ALOGE("Terminated");
226 setDebugState(DebugState::Running);
227 return false;
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800228 }
229 }
230 }
231}
232
Ady Abraham75398722020-04-07 14:08:45 -0700233void Timer::setDebugState(DebugState state) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700234 std::lock_guard lock(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700235 mDebugState = state;
236}
237
Ady Abraham75398722020-04-07 14:08:45 -0700238void Timer::dump(std::string& result) const {
Ady Abraham8cb21882020-08-26 18:22:05 -0700239 std::lock_guard lock(mMutex);
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700240 StringAppendF(&result, "\t\tDebugState: %s\n", ftl::enum_string(mDebugState).c_str());
Ady Abraham75398722020-04-07 14:08:45 -0700241}
242
Kevin DuBoiscc27b502019-11-13 09:40:07 -0800243} // namespace android::scheduler