blob: 239fff9708b81fc788eb10be7a64fe93d46caf1c [file] [log] [blame]
Yabin Cui3e4c5952016-07-26 15:03:27 -07001/*
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 Cuifaa7b922021-01-11 17:35:57 -080024namespace simpleperf {
25
Yabin Cui3e4c5952016-07-26 15:03:27 -070026struct IOEvent {
27 IOEventLoop* loop;
28 event* e;
Yabin Cuic8571d42018-06-06 11:20:39 -070029 timeval timeout;
Yabin Cui3e4c5952016-07-26 15:03:27 -070030 std::function<bool()> callback;
Yabin Cuia383c112016-10-19 11:04:56 -070031 bool enabled;
Yabin Cui3e4c5952016-07-26 15:03:27 -070032
33 IOEvent(IOEventLoop* loop, const std::function<bool()>& callback)
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020034 : loop(loop), e(nullptr), timeout({}), callback(callback), enabled(false) {}
Yabin Cui3e4c5952016-07-26 15:03:27 -070035
36 ~IOEvent() {
37 if (e != nullptr) {
38 event_free(e);
39 }
40 }
41};
42
Yabin Cuiacf04b22018-04-18 13:10:40 -070043IOEventLoop::IOEventLoop()
44 : ebase_(nullptr), has_error_(false), use_precise_timer_(false), in_loop_(false) {}
Yabin Cui3e4c5952016-07-26 15:03:27 -070045
46IOEventLoop::~IOEventLoop() {
Yabin Cui26968e62017-01-30 11:34:24 -080047 events_.clear();
Yabin Cui3e4c5952016-07-26 15:03:27 -070048 if (ebase_ != nullptr) {
49 event_base_free(ebase_);
50 }
51}
52
Yabin Cui76624282017-10-23 12:02:28 -070053bool 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 Cui3e4c5952016-07-26 15:03:27 -070061bool IOEventLoop::EnsureInit() {
62 if (ebase_ == nullptr) {
Yabin Cui76624282017-10-23 12:02:28 -070063 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 Cuib4595042018-04-19 17:01:44 -070068 if (event_config_avoid_method(cfg, "epoll") != 0) {
69 LOG(ERROR) << "event_config_avoid_method";
70 return false;
71 }
Yabin Cui76624282017-10-23 12:02:28 -070072 ebase_ = event_base_new_with_config(cfg);
Yabin Cuib4595042018-04-19 17:01:44 -070073 // 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 Cui76624282017-10-23 12:02:28 -070086 event_config_free(cfg);
Yabin Cuic3cc93b2022-03-31 16:46:16 -070087 if (event_base_priority_init(ebase_, 2) != 0) {
88 LOG(ERROR) << "event_base_priority_init failed";
89 return false;
90 }
Yabin Cui76624282017-10-23 12:02:28 -070091 }
Yabin Cui3e4c5952016-07-26 15:03:27 -070092 if (ebase_ == nullptr) {
Yabin Cui76624282017-10-23 12:02:28 -070093 LOG(ERROR) << "failed to create event_base";
Yabin Cui3e4c5952016-07-26 15:03:27 -070094 return false;
95 }
96 }
97 return true;
98}
99
Chih-Hung Hsieh84280792018-08-13 10:31:02 -0700100void IOEventLoop::EventCallbackFn(int, int16_t, void* arg) {
Yabin Cui3e4c5952016-07-26 15:03:27 -0700101 IOEvent* e = static_cast<IOEvent*>(arg);
102 if (!e->callback()) {
103 e->loop->has_error_ = true;
104 e->loop->ExitLoop();
105 }
106}
107
Yabin Cuidbbda302016-07-28 12:55:41 -0700108static 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 Cuic3cc93b2022-03-31 16:46:16 -0700117IOEventRef IOEventLoop::AddReadEvent(int fd, const std::function<bool()>& callback,
118 IOEventPriority priority) {
Yabin Cui825e56b2016-08-26 18:25:21 -0700119 if (!MakeFdNonBlocking(fd)) {
120 return nullptr;
121 }
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700122 return AddEvent(fd, EV_READ | EV_PERSIST, nullptr, callback, priority);
Yabin Cuidbbda302016-07-28 12:55:41 -0700123}
124
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700125IOEventRef IOEventLoop::AddWriteEvent(int fd, const std::function<bool()>& callback,
126 IOEventPriority priority) {
Yabin Cuia383c112016-10-19 11:04:56 -0700127 if (!MakeFdNonBlocking(fd)) {
128 return nullptr;
129 }
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700130 return AddEvent(fd, EV_WRITE | EV_PERSIST, nullptr, callback, priority);
Yabin Cuia383c112016-10-19 11:04:56 -0700131}
132
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700133bool 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 Cui3e4c5952016-07-26 15:03:27 -0700136}
137
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700138bool IOEventLoop::AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback,
139 IOEventPriority priority) {
Yabin Cui3e4c5952016-07-26 15:03:27 -0700140 for (auto sig : sigs) {
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700141 if (!AddSignalEvent(sig, callback, priority)) {
Yabin Cui3e4c5952016-07-26 15:03:27 -0700142 return false;
143 }
144 }
145 return true;
146}
147
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700148IOEventRef IOEventLoop::AddPeriodicEvent(timeval duration, const std::function<bool()>& callback,
149 IOEventPriority priority) {
150 return AddEvent(-1, EV_PERSIST, &duration, callback, priority);
Yabin Cui3e4c5952016-07-26 15:03:27 -0700151}
152
Chih-Hung Hsieh84280792018-08-13 10:31:02 -0700153IOEventRef IOEventLoop::AddEvent(int fd_or_sig, int16_t events, timeval* timeout,
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700154 const std::function<bool()>& callback, IOEventPriority priority) {
Yabin Cui3e4c5952016-07-26 15:03:27 -0700155 if (!EnsureInit()) {
Yabin Cui825e56b2016-08-26 18:25:21 -0700156 return nullptr;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700157 }
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 Cui825e56b2016-08-26 18:25:21 -0700162 return nullptr;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700163 }
Yabin Cuic3cc93b2022-03-31 16:46:16 -0700164 event_priority_set(e->e, priority);
Yabin Cui3e4c5952016-07-26 15:03:27 -0700165 if (event_add(e->e, timeout) != 0) {
166 LOG(ERROR) << "event_add() failed";
Yabin Cui825e56b2016-08-26 18:25:21 -0700167 return nullptr;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700168 }
Yabin Cuic8571d42018-06-06 11:20:39 -0700169 if (timeout != nullptr) {
170 e->timeout = *timeout;
171 }
Yabin Cuia383c112016-10-19 11:04:56 -0700172 e->enabled = true;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700173 events_.push_back(std::move(e));
Yabin Cui825e56b2016-08-26 18:25:21 -0700174 return events_.back().get();
Yabin Cui3e4c5952016-07-26 15:03:27 -0700175}
176
177bool IOEventLoop::RunLoop() {
Yabin Cuiacf04b22018-04-18 13:10:40 -0700178 in_loop_ = true;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700179 if (event_base_dispatch(ebase_) == -1) {
180 LOG(ERROR) << "event_base_dispatch() failed";
Yabin Cuiacf04b22018-04-18 13:10:40 -0700181 in_loop_ = false;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700182 return false;
183 }
184 if (has_error_) {
185 return false;
186 }
187 return true;
188}
189
190bool IOEventLoop::ExitLoop() {
Yabin Cuiacf04b22018-04-18 13:10:40 -0700191 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 Cui3e4c5952016-07-26 15:03:27 -0700197 }
198 return true;
199}
Yabin Cui825e56b2016-08-26 18:25:21 -0700200
Yabin Cuia383c112016-10-19 11:04:56 -0700201bool 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
212bool IOEventLoop::EnableEvent(IOEventRef ref) {
213 if (!ref->enabled) {
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200214 timeval* timeout =
215 (ref->timeout.tv_sec != 0 || ref->timeout.tv_usec != 0) ? &ref->timeout : nullptr;
Yabin Cuic8571d42018-06-06 11:20:39 -0700216 if (event_add(ref->e, timeout) != 0) {
Yabin Cuia383c112016-10-19 11:04:56 -0700217 LOG(ERROR) << "event_add() failed";
218 return false;
219 }
220 ref->enabled = true;
221 }
222 return true;
223}
224
Yabin Cui825e56b2016-08-26 18:25:21 -0700225bool IOEventLoop::DelEvent(IOEventRef ref) {
Yabin Cuia383c112016-10-19 11:04:56 -0700226 DisableEvent(ref);
Yabin Cui825e56b2016-08-26 18:25:21 -0700227 IOEventLoop* loop = ref->loop;
228 for (auto it = loop->events_.begin(); it != loop->events_.end(); ++it) {
229 if (it->get() == ref) {
Yabin Cui825e56b2016-08-26 18:25:21 -0700230 loop->events_.erase(it);
231 break;
232 }
233 }
234 return true;
235}
Yabin Cuifaa7b922021-01-11 17:35:57 -0800236
237} // namespace simpleperf