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