Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "IOEventLoop.h" |
| 18 | |
| 19 | #include <event2/event.h> |
| 20 | #include <fcntl.h> |
| 21 | |
| 22 | #include <android-base/logging.h> |
| 23 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 24 | namespace simpleperf { |
| 25 | |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 26 | struct IOEvent { |
| 27 | IOEventLoop* loop; |
| 28 | event* e; |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 29 | timeval timeout; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 30 | std::function<bool()> callback; |
Yabin Cui | a383c11 | 2016-10-19 11:04:56 -0700 | [diff] [blame] | 31 | bool enabled; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 32 | |
| 33 | IOEvent(IOEventLoop* loop, const std::function<bool()>& callback) |
ThiƩbaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 34 | : loop(loop), e(nullptr), timeout({}), callback(callback), enabled(false) {} |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 35 | |
| 36 | ~IOEvent() { |
| 37 | if (e != nullptr) { |
| 38 | event_free(e); |
| 39 | } |
| 40 | } |
| 41 | }; |
| 42 | |
Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 43 | IOEventLoop::IOEventLoop() |
| 44 | : ebase_(nullptr), has_error_(false), use_precise_timer_(false), in_loop_(false) {} |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 45 | |
| 46 | IOEventLoop::~IOEventLoop() { |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 47 | events_.clear(); |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 48 | if (ebase_ != nullptr) { |
| 49 | event_base_free(ebase_); |
| 50 | } |
| 51 | } |
| 52 | |
Yabin Cui | 7662428 | 2017-10-23 12:02:28 -0700 | [diff] [blame] | 53 | bool IOEventLoop::UsePreciseTimer() { |
| 54 | if (ebase_ != nullptr) { |
| 55 | return false; // Too late to set the flag. |
| 56 | } |
| 57 | use_precise_timer_ = true; |
| 58 | return true; |
| 59 | } |
| 60 | |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 61 | bool IOEventLoop::EnsureInit() { |
| 62 | if (ebase_ == nullptr) { |
Yabin Cui | 7662428 | 2017-10-23 12:02:28 -0700 | [diff] [blame] | 63 | event_config* cfg = event_config_new(); |
| 64 | if (cfg != nullptr) { |
| 65 | if (use_precise_timer_) { |
| 66 | event_config_set_flag(cfg, EVENT_BASE_FLAG_PRECISE_TIMER); |
| 67 | } |
Yabin Cui | b459504 | 2018-04-19 17:01:44 -0700 | [diff] [blame] | 68 | if (event_config_avoid_method(cfg, "epoll") != 0) { |
| 69 | LOG(ERROR) << "event_config_avoid_method"; |
| 70 | return false; |
| 71 | } |
Yabin Cui | 7662428 | 2017-10-23 12:02:28 -0700 | [diff] [blame] | 72 | ebase_ = event_base_new_with_config(cfg); |
Yabin Cui | b459504 | 2018-04-19 17:01:44 -0700 | [diff] [blame] | 73 | // perf event files support reporting available data via poll methods. However, it doesn't |
| 74 | // work well with epoll. Because perf_poll() in kernel/events/core.c uses a report and reset |
| 75 | // way to report poll events. If perf_poll() is called twice, it may return POLLIN for the |
| 76 | // first time, and no events for the second time. And epoll may call perf_poll() more than |
| 77 | // once to confirm events. A failed situation is below: |
| 78 | // When profiling SimpleperfExampleOfKotlin on Pixel device with `-g --duration 10`, the |
| 79 | // kernel fills up the buffer before we call epoll_ctl(EPOLL_CTL_ADD). Then the POLLIN event |
| 80 | // is returned when calling epoll_ctl(), while no events are returned when calling |
| 81 | // epoll_wait(). As a result, simpleperf doesn't receive any poll wakeup events. |
| 82 | if (strcmp(event_base_get_method(ebase_), "poll") != 0) { |
| 83 | LOG(ERROR) << "event_base_get_method isn't poll: " << event_base_get_method(ebase_); |
| 84 | return false; |
| 85 | } |
Yabin Cui | 7662428 | 2017-10-23 12:02:28 -0700 | [diff] [blame] | 86 | event_config_free(cfg); |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 87 | if (event_base_priority_init(ebase_, 2) != 0) { |
| 88 | LOG(ERROR) << "event_base_priority_init failed"; |
| 89 | return false; |
| 90 | } |
Yabin Cui | 7662428 | 2017-10-23 12:02:28 -0700 | [diff] [blame] | 91 | } |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 92 | if (ebase_ == nullptr) { |
Yabin Cui | 7662428 | 2017-10-23 12:02:28 -0700 | [diff] [blame] | 93 | LOG(ERROR) << "failed to create event_base"; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
Chih-Hung Hsieh | 8428079 | 2018-08-13 10:31:02 -0700 | [diff] [blame] | 100 | void IOEventLoop::EventCallbackFn(int, int16_t, void* arg) { |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 101 | IOEvent* e = static_cast<IOEvent*>(arg); |
| 102 | if (!e->callback()) { |
| 103 | e->loop->has_error_ = true; |
| 104 | e->loop->ExitLoop(); |
| 105 | } |
| 106 | } |
| 107 | |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 108 | static bool MakeFdNonBlocking(int fd) { |
| 109 | int flags = fcntl(fd, F_GETFL, 0); |
| 110 | if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { |
| 111 | PLOG(ERROR) << "fcntl() failed"; |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 117 | IOEventRef IOEventLoop::AddReadEvent(int fd, const std::function<bool()>& callback, |
| 118 | IOEventPriority priority) { |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 119 | if (!MakeFdNonBlocking(fd)) { |
| 120 | return nullptr; |
| 121 | } |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 122 | return AddEvent(fd, EV_READ | EV_PERSIST, nullptr, callback, priority); |
Yabin Cui | dbbda30 | 2016-07-28 12:55:41 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 125 | IOEventRef IOEventLoop::AddWriteEvent(int fd, const std::function<bool()>& callback, |
| 126 | IOEventPriority priority) { |
Yabin Cui | a383c11 | 2016-10-19 11:04:56 -0700 | [diff] [blame] | 127 | if (!MakeFdNonBlocking(fd)) { |
| 128 | return nullptr; |
| 129 | } |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 130 | return AddEvent(fd, EV_WRITE | EV_PERSIST, nullptr, callback, priority); |
Yabin Cui | a383c11 | 2016-10-19 11:04:56 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 133 | bool IOEventLoop::AddSignalEvent(int sig, const std::function<bool()>& callback, |
| 134 | IOEventPriority priority) { |
| 135 | return AddEvent(sig, EV_SIGNAL | EV_PERSIST, nullptr, callback, priority) != nullptr; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 138 | bool IOEventLoop::AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback, |
| 139 | IOEventPriority priority) { |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 140 | for (auto sig : sigs) { |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 141 | if (!AddSignalEvent(sig, callback, priority)) { |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 148 | IOEventRef IOEventLoop::AddPeriodicEvent(timeval duration, const std::function<bool()>& callback, |
| 149 | IOEventPriority priority) { |
| 150 | return AddEvent(-1, EV_PERSIST, &duration, callback, priority); |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Chih-Hung Hsieh | 8428079 | 2018-08-13 10:31:02 -0700 | [diff] [blame] | 153 | IOEventRef IOEventLoop::AddEvent(int fd_or_sig, int16_t events, timeval* timeout, |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 154 | const std::function<bool()>& callback, IOEventPriority priority) { |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 155 | if (!EnsureInit()) { |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 156 | return nullptr; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 157 | } |
| 158 | std::unique_ptr<IOEvent> e(new IOEvent(this, callback)); |
| 159 | e->e = event_new(ebase_, fd_or_sig, events, EventCallbackFn, e.get()); |
| 160 | if (e->e == nullptr) { |
| 161 | LOG(ERROR) << "event_new() failed"; |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 162 | return nullptr; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 163 | } |
Yabin Cui | c3cc93b | 2022-03-31 16:46:16 -0700 | [diff] [blame] | 164 | event_priority_set(e->e, priority); |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 165 | if (event_add(e->e, timeout) != 0) { |
| 166 | LOG(ERROR) << "event_add() failed"; |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 167 | return nullptr; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 168 | } |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 169 | if (timeout != nullptr) { |
| 170 | e->timeout = *timeout; |
| 171 | } |
Yabin Cui | a383c11 | 2016-10-19 11:04:56 -0700 | [diff] [blame] | 172 | e->enabled = true; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 173 | events_.push_back(std::move(e)); |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 174 | return events_.back().get(); |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | bool IOEventLoop::RunLoop() { |
Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 178 | in_loop_ = true; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 179 | if (event_base_dispatch(ebase_) == -1) { |
| 180 | LOG(ERROR) << "event_base_dispatch() failed"; |
Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 181 | in_loop_ = false; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | if (has_error_) { |
| 185 | return false; |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | bool IOEventLoop::ExitLoop() { |
Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 191 | if (in_loop_) { |
| 192 | if (event_base_loopbreak(ebase_) == -1) { |
| 193 | LOG(ERROR) << "event_base_loopbreak() failed"; |
| 194 | return false; |
| 195 | } |
| 196 | in_loop_ = false; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 197 | } |
| 198 | return true; |
| 199 | } |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 200 | |
Yabin Cui | a383c11 | 2016-10-19 11:04:56 -0700 | [diff] [blame] | 201 | bool IOEventLoop::DisableEvent(IOEventRef ref) { |
| 202 | if (ref->enabled) { |
| 203 | if (event_del(ref->e) != 0) { |
| 204 | LOG(ERROR) << "event_del() failed"; |
| 205 | return false; |
| 206 | } |
| 207 | ref->enabled = false; |
| 208 | } |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool IOEventLoop::EnableEvent(IOEventRef ref) { |
| 213 | if (!ref->enabled) { |
ThiƩbaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 214 | timeval* timeout = |
| 215 | (ref->timeout.tv_sec != 0 || ref->timeout.tv_usec != 0) ? &ref->timeout : nullptr; |
Yabin Cui | c8571d4 | 2018-06-06 11:20:39 -0700 | [diff] [blame] | 216 | if (event_add(ref->e, timeout) != 0) { |
Yabin Cui | a383c11 | 2016-10-19 11:04:56 -0700 | [diff] [blame] | 217 | LOG(ERROR) << "event_add() failed"; |
| 218 | return false; |
| 219 | } |
| 220 | ref->enabled = true; |
| 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 225 | bool IOEventLoop::DelEvent(IOEventRef ref) { |
Yabin Cui | a383c11 | 2016-10-19 11:04:56 -0700 | [diff] [blame] | 226 | DisableEvent(ref); |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 227 | IOEventLoop* loop = ref->loop; |
| 228 | for (auto it = loop->events_.begin(); it != loop->events_.end(); ++it) { |
| 229 | if (it->get() == ref) { |
Yabin Cui | 825e56b | 2016-08-26 18:25:21 -0700 | [diff] [blame] | 230 | loop->events_.erase(it); |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | return true; |
| 235 | } |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 236 | |
| 237 | } // namespace simpleperf |