Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include "base/android_task_runner.h" |
| 18 | |
| 19 | #include <sys/eventfd.h> |
| 20 | #include <sys/timerfd.h> |
| 21 | |
| 22 | namespace perfetto { |
| 23 | namespace base { |
| 24 | |
| 25 | AndroidTaskRunner::AndroidTaskRunner() |
| 26 | : looper_(ALooper_prepare(0 /* require callbacks */)), |
| 27 | immediate_event_(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)), |
| 28 | delayed_timer_( |
| 29 | timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC)) { |
| 30 | ALooper_acquire(looper_); |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 31 | PERFETTO_CHECK(immediate_event_); |
| 32 | PERFETTO_CHECK(delayed_timer_); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 33 | AddFileDescriptorWatch(immediate_event_.get(), |
| 34 | std::bind(&AndroidTaskRunner::RunImmediateTask, this)); |
| 35 | AddFileDescriptorWatch(delayed_timer_.get(), |
| 36 | std::bind(&AndroidTaskRunner::RunDelayedTask, this)); |
| 37 | } |
| 38 | |
| 39 | AndroidTaskRunner::~AndroidTaskRunner() { |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 40 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 41 | std::lock_guard<std::mutex> lock(lock_); |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 42 | for (const auto& watch : watch_tasks_) { |
| 43 | // ALooper doesn't guarantee that each watch doesn't run one last time if |
| 44 | // the file descriptor was already signalled. To guard against this point |
| 45 | // the watch to a no-op callback. |
| 46 | ALooper_addFd( |
| 47 | looper_, watch.first, ALOOPER_POLL_CALLBACK, |
| 48 | ALOOPER_EVENT_INPUT | ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP, |
| 49 | [](int, int, void*) -> int { return 0; }, nullptr); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 50 | ALooper_removeFd(looper_, watch.first); |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 51 | } |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 52 | ALooper_release(looper_); |
| 53 | |
| 54 | struct itimerspec time = {}; |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 55 | timerfd_settime(delayed_timer_.get(), TFD_TIMER_ABSTIME, &time, nullptr); |
| 56 | } |
| 57 | |
| 58 | void AndroidTaskRunner::Run() { |
| 59 | quit_ = false; |
| 60 | while (true) { |
| 61 | { |
| 62 | std::lock_guard<std::mutex> lock(lock_); |
| 63 | if (quit_) |
| 64 | break; |
| 65 | } |
| 66 | ALooper_pollOnce(-1 /* timeout */, nullptr, nullptr, nullptr); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void AndroidTaskRunner::Quit() { |
| 71 | std::lock_guard<std::mutex> lock(lock_); |
| 72 | quit_ = true; |
| 73 | ALooper_wake(looper_); |
| 74 | } |
| 75 | |
| 76 | bool AndroidTaskRunner::IsIdleForTesting() { |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 77 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 78 | std::lock_guard<std::mutex> lock(lock_); |
| 79 | return immediate_tasks_.empty(); |
| 80 | } |
| 81 | |
| 82 | AndroidTaskRunner::TimePoint AndroidTaskRunner::GetTime() const { |
| 83 | static_assert(sizeof(TimePoint) == sizeof(struct timespec), |
| 84 | "TimePoint layout must match struct timespec"); |
| 85 | TimePoint now; |
| 86 | if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) |
| 87 | PERFETTO_DPLOG("clock_gettime"); |
| 88 | return now; |
| 89 | } |
| 90 | |
| 91 | void AndroidTaskRunner::RunImmediateTask() { |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 92 | uint64_t unused = 0; |
| 93 | if (read(immediate_event_.get(), &unused, sizeof(unused)) != sizeof(unused) && |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 94 | errno != EAGAIN) { |
| 95 | PERFETTO_DPLOG("read"); |
| 96 | } |
| 97 | |
| 98 | // TODO(skyostil): Add a separate work queue in case in case locking overhead |
| 99 | // becomes an issue. |
| 100 | bool has_next; |
| 101 | std::function<void()> immediate_task; |
| 102 | { |
| 103 | std::lock_guard<std::mutex> lock(lock_); |
| 104 | if (immediate_tasks_.empty()) |
| 105 | return; |
| 106 | immediate_task = std::move(immediate_tasks_.front()); |
| 107 | immediate_tasks_.pop_front(); |
| 108 | has_next = !immediate_tasks_.empty(); |
| 109 | } |
| 110 | // Do another pass through the event loop even if we have immediate tasks to |
| 111 | // run for fairness. |
| 112 | if (has_next) |
| 113 | ScheduleImmediateWakeUp(); |
| 114 | immediate_task(); |
| 115 | } |
| 116 | |
| 117 | void AndroidTaskRunner::RunDelayedTask() { |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 118 | uint64_t unused = 0; |
| 119 | if (read(delayed_timer_.get(), &unused, sizeof(unused)) != sizeof(unused) && |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 120 | errno != EAGAIN) { |
| 121 | PERFETTO_DPLOG("read"); |
| 122 | } |
| 123 | |
| 124 | std::function<void()> delayed_task; |
| 125 | TimePoint next_wake_up; |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 126 | { |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 127 | std::lock_guard<std::mutex> lock(lock_); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 128 | if (delayed_tasks_.empty()) |
| 129 | return; |
| 130 | auto it = delayed_tasks_.begin(); |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 131 | PERFETTO_DCHECK(!(GetTime() < it->first)); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 132 | delayed_task = std::move(it->second); |
| 133 | delayed_tasks_.erase(it); |
| 134 | if (!delayed_tasks_.empty()) |
| 135 | next_wake_up = delayed_tasks_.begin()->first; |
| 136 | } |
| 137 | if (next_wake_up) |
| 138 | ScheduleDelayedWakeUp(next_wake_up); |
| 139 | delayed_task(); |
| 140 | } |
| 141 | |
| 142 | void AndroidTaskRunner::ScheduleImmediateWakeUp() { |
| 143 | uint64_t value = 1; |
| 144 | if (write(immediate_event_.get(), &value, sizeof(value)) == -1 && |
| 145 | errno != EAGAIN) { |
| 146 | PERFETTO_DPLOG("write"); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void AndroidTaskRunner::ScheduleDelayedWakeUp(const TimePoint& time) { |
| 151 | PERFETTO_DCHECK(time); |
| 152 | struct itimerspec wake_up = {}; |
| 153 | wake_up.it_value = time; |
| 154 | if (timerfd_settime(delayed_timer_.get(), TFD_TIMER_ABSTIME, &wake_up, |
| 155 | nullptr) == -1) { |
| 156 | PERFETTO_DPLOG("timerfd_settime"); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void AndroidTaskRunner::PostTask(std::function<void()> task) { |
| 161 | bool was_empty; |
| 162 | { |
| 163 | std::lock_guard<std::mutex> lock(lock_); |
| 164 | was_empty = immediate_tasks_.empty(); |
| 165 | immediate_tasks_.push_back(std::move(task)); |
| 166 | } |
| 167 | if (was_empty) |
| 168 | ScheduleImmediateWakeUp(); |
| 169 | } |
| 170 | |
| 171 | void AndroidTaskRunner::PostDelayedTask(std::function<void()> task, |
| 172 | int delay_ms) { |
| 173 | PERFETTO_DCHECK(delay_ms >= 0); |
| 174 | auto runtime = GetTime().AdvanceByMs(delay_ms); |
| 175 | bool is_next = false; |
| 176 | { |
| 177 | std::lock_guard<std::mutex> lock(lock_); |
| 178 | auto it = delayed_tasks_.insert(std::make_pair(runtime, std::move(task))); |
| 179 | if (it == delayed_tasks_.begin()) |
| 180 | is_next = true; |
| 181 | } |
| 182 | if (is_next) |
| 183 | ScheduleDelayedWakeUp(runtime); |
| 184 | } |
| 185 | |
| 186 | void AndroidTaskRunner::AddFileDescriptorWatch(int fd, |
| 187 | std::function<void()> task) { |
| 188 | PERFETTO_DCHECK(fd >= 0); |
| 189 | { |
| 190 | std::lock_guard<std::mutex> lock(lock_); |
| 191 | PERFETTO_DCHECK(!watch_tasks_.count(fd)); |
| 192 | watch_tasks_[fd] = std::move(task); |
| 193 | } |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 194 | // It's safe for the callback to hang on to |this| as everything is |
| 195 | // unregistered in the destructor. |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 196 | auto callback = [](int signalled_fd, int events, void* data) -> int { |
| 197 | AndroidTaskRunner* task_runner = reinterpret_cast<AndroidTaskRunner*>(data); |
| 198 | return task_runner->OnFileDescriptorEvent(signalled_fd, events) ? 1 : 0; |
| 199 | }; |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 200 | PERFETTO_CHECK(ALooper_addFd(looper_, fd, ALOOPER_POLL_CALLBACK, |
| 201 | ALOOPER_EVENT_INPUT | ALOOPER_EVENT_ERROR | |
| 202 | ALOOPER_EVENT_HANGUP, |
| 203 | std::move(callback), this) != -1); |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | bool AndroidTaskRunner::OnFileDescriptorEvent(int signalled_fd, int events) { |
| 207 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 208 | if (!(events & (ALOOPER_EVENT_INPUT | ALOOPER_EVENT_ERROR | |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 209 | ALOOPER_EVENT_HANGUP | ALOOPER_EVENT_INVALID))) { |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 210 | return true; |
Sami Kyostila | 7ae3448 | 2017-11-28 13:17:07 +0000 | [diff] [blame] | 211 | } |
Sami Kyostila | 73d41c8 | 2017-11-24 18:38:46 +0000 | [diff] [blame] | 212 | std::function<void()> task; |
| 213 | { |
| 214 | std::lock_guard<std::mutex> lock(lock_); |
| 215 | auto it = watch_tasks_.find(signalled_fd); |
| 216 | if (it == watch_tasks_.end()) |
| 217 | return false; |
| 218 | task = it->second; |
| 219 | } |
| 220 | task(); |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | void AndroidTaskRunner::RemoveFileDescriptorWatch(int fd) { |
| 225 | PERFETTO_DCHECK(fd >= 0); |
| 226 | { |
| 227 | std::lock_guard<std::mutex> lock(lock_); |
| 228 | PERFETTO_DCHECK(watch_tasks_.count(fd)); |
| 229 | watch_tasks_.erase(fd); |
| 230 | } |
| 231 | ALooper_removeFd(looper_, fd); |
| 232 | } |
| 233 | |
| 234 | } // namespace base |
| 235 | } // namespace perfetto |