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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "SchedulerTimer" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 20 | |
| 21 | #include <chrono> |
| 22 | #include <cstdint> |
| 23 | |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 24 | #include <sys/epoll.h> |
| 25 | #include <sys/timerfd.h> |
| 26 | #include <sys/unistd.h> |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 27 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame^] | 28 | #include <ftl/concat.h> |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 29 | #include <ftl/enum.h> |
| 30 | #include <log/log.h> |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 31 | #include <utils/Trace.h> |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 32 | |
| 33 | #include "Timer.h" |
| 34 | |
| 35 | namespace android::scheduler { |
| 36 | |
| 37 | static constexpr size_t kReadPipe = 0; |
| 38 | static constexpr size_t kWritePipe = 1; |
| 39 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 40 | Timer::Timer() { |
| 41 | reset(); |
| 42 | mDispatchThread = std::thread([this]() { threadMain(); }); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | Timer::~Timer() { |
| 46 | endDispatch(); |
| 47 | mDispatchThread.join(); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 48 | cleanup(); |
| 49 | } |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 50 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 51 | void Timer::reset() { |
| 52 | cleanup(); |
| 53 | mTimerFd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); |
| 54 | mEpollFd = epoll_create1(EPOLL_CLOEXEC); |
| 55 | if (pipe2(mPipes.data(), O_CLOEXEC | O_NONBLOCK)) { |
| 56 | ALOGE("could not create TimerDispatch mPipes"); |
| 57 | return; |
| 58 | }; |
| 59 | setDebugState(DebugState::Reset); |
| 60 | } |
| 61 | |
| 62 | void Timer::cleanup() { |
| 63 | if (mTimerFd != -1) { |
| 64 | close(mTimerFd); |
| 65 | mTimerFd = -1; |
| 66 | } |
| 67 | |
| 68 | if (mEpollFd != -1) { |
| 69 | close(mEpollFd); |
| 70 | mEpollFd = -1; |
| 71 | } |
| 72 | |
| 73 | if (mPipes[kReadPipe] != -1) { |
| 74 | close(mPipes[kReadPipe]); |
| 75 | mPipes[kReadPipe] = -1; |
| 76 | } |
| 77 | |
| 78 | if (mPipes[kWritePipe] != -1) { |
| 79 | close(mPipes[kWritePipe]); |
| 80 | mPipes[kWritePipe] = -1; |
| 81 | } |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void Timer::endDispatch() { |
| 85 | static constexpr unsigned char end = 'e'; |
| 86 | write(mPipes[kWritePipe], &end, sizeof(end)); |
| 87 | } |
| 88 | |
| 89 | nsecs_t Timer::now() const { |
| 90 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 91 | } |
| 92 | |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 93 | void Timer::alarmAt(std::function<void()> const& cb, nsecs_t time) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 94 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 95 | using namespace std::literals; |
| 96 | static constexpr int ns_per_s = |
| 97 | std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count(); |
| 98 | |
| 99 | mCallback = cb; |
| 100 | |
| 101 | struct itimerspec old_timer; |
| 102 | struct itimerspec new_timer { |
| 103 | .it_interval = {.tv_sec = 0, .tv_nsec = 0}, |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 104 | .it_value = {.tv_sec = static_cast<long>(time / ns_per_s), |
| 105 | .tv_nsec = static_cast<long>(time % ns_per_s)}, |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 108 | if (timerfd_settime(mTimerFd, TFD_TIMER_ABSTIME, &new_timer, &old_timer)) { |
Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 109 | ALOGW("Failed to set timerfd %s (%i)", strerror(errno), errno); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | void Timer::alarmCancel() { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 114 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 115 | |
| 116 | struct itimerspec old_timer; |
| 117 | struct itimerspec new_timer { |
| 118 | .it_interval = {.tv_sec = 0, .tv_nsec = 0}, |
| 119 | .it_value = { |
| 120 | .tv_sec = 0, |
| 121 | .tv_nsec = 0, |
| 122 | }, |
| 123 | }; |
| 124 | |
| 125 | if (timerfd_settime(mTimerFd, 0, &new_timer, &old_timer)) { |
| 126 | ALOGW("Failed to disarm timerfd"); |
| 127 | } |
| 128 | } |
| 129 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 130 | void Timer::threadMain() { |
| 131 | while (dispatch()) { |
| 132 | reset(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | bool Timer::dispatch() { |
| 137 | setDebugState(DebugState::Running); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 138 | struct sched_param param = {0}; |
| 139 | param.sched_priority = 2; |
| 140 | if (pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m) != 0) { |
| 141 | ALOGW("Failed to set SCHED_FIFO on dispatch thread"); |
| 142 | } |
| 143 | |
| 144 | if (pthread_setname_np(pthread_self(), "TimerDispatch")) { |
| 145 | ALOGW("Failed to set thread name on dispatch thread"); |
| 146 | } |
| 147 | |
| 148 | enum DispatchType : uint32_t { TIMER, TERMINATE, MAX_DISPATCH_TYPE }; |
| 149 | epoll_event timerEvent; |
| 150 | timerEvent.events = EPOLLIN; |
| 151 | timerEvent.data.u32 = DispatchType::TIMER; |
| 152 | if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mTimerFd, &timerEvent) == -1) { |
| 153 | ALOGE("Error adding timer fd to epoll dispatch loop"); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 154 | return true; |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | epoll_event terminateEvent; |
| 158 | terminateEvent.events = EPOLLIN; |
| 159 | terminateEvent.data.u32 = DispatchType::TERMINATE; |
| 160 | if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mPipes[kReadPipe], &terminateEvent) == -1) { |
| 161 | ALOGE("Error adding control fd to dispatch loop"); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 162 | return true; |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | uint64_t iteration = 0; |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 166 | |
| 167 | while (true) { |
| 168 | setDebugState(DebugState::Waiting); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 169 | epoll_event events[DispatchType::MAX_DISPATCH_TYPE]; |
| 170 | int nfds = epoll_wait(mEpollFd, events, DispatchType::MAX_DISPATCH_TYPE, -1); |
| 171 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 172 | setDebugState(DebugState::Running); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 173 | if (ATRACE_ENABLED()) { |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame^] | 174 | ftl::Concat trace("TimerIteration #", iteration++); |
| 175 | ATRACE_NAME(trace.c_str()); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | if (nfds == -1) { |
| 179 | if (errno != EINTR) { |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 180 | ALOGE("Error waiting on epoll: %s", strerror(errno)); |
| 181 | return true; |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | for (auto i = 0; i < nfds; i++) { |
| 186 | if (events[i].data.u32 == DispatchType::TIMER) { |
| 187 | static uint64_t mIgnored = 0; |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 188 | setDebugState(DebugState::Reading); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 189 | read(mTimerFd, &mIgnored, sizeof(mIgnored)); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 190 | setDebugState(DebugState::Running); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 191 | std::function<void()> cb; |
| 192 | { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 193 | std::lock_guard lock(mMutex); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 194 | cb = mCallback; |
| 195 | } |
| 196 | if (cb) { |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 197 | setDebugState(DebugState::InCallback); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 198 | cb(); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 199 | setDebugState(DebugState::Running); |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | if (events[i].data.u32 == DispatchType::TERMINATE) { |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 203 | ALOGE("Terminated"); |
| 204 | setDebugState(DebugState::Running); |
| 205 | return false; |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 211 | void Timer::setDebugState(DebugState state) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 212 | std::lock_guard lock(mMutex); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 213 | mDebugState = state; |
| 214 | } |
| 215 | |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 216 | void Timer::dump(std::string& result) const { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 217 | std::lock_guard lock(mMutex); |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame^] | 218 | result.append("\t\tDebugState: "); |
| 219 | result.append(ftl::enum_string(mDebugState)); |
| 220 | result.push_back('\n'); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Kevin DuBois | cc27b50 | 2019-11-13 09:40:07 -0800 | [diff] [blame] | 223 | } // namespace android::scheduler |