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
24 namespace simpleperf {
25
26 struct IOEvent {
27 IOEventLoop* loop;
28 event* e;
29 timeval timeout;
30 std::function<bool()> callback;
31 bool enabled;
32
IOEventsimpleperf::IOEvent33 IOEvent(IOEventLoop* loop, const std::function<bool()>& callback)
34 : loop(loop), e(nullptr), timeout({}), callback(callback), enabled(false) {}
35
~IOEventsimpleperf::IOEvent36 ~IOEvent() {
37 if (e != nullptr) {
38 event_free(e);
39 }
40 }
41 };
42
IOEventLoop()43 IOEventLoop::IOEventLoop()
44 : ebase_(nullptr), has_error_(false), use_precise_timer_(false), in_loop_(false) {}
45
~IOEventLoop()46 IOEventLoop::~IOEventLoop() {
47 events_.clear();
48 if (ebase_ != nullptr) {
49 event_base_free(ebase_);
50 }
51 }
52
UsePreciseTimer()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
EnsureInit()61 bool IOEventLoop::EnsureInit() {
62 if (ebase_ == nullptr) {
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 }
68 if (event_config_avoid_method(cfg, "epoll") != 0) {
69 LOG(ERROR) << "event_config_avoid_method";
70 return false;
71 }
72 ebase_ = event_base_new_with_config(cfg);
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 }
86 event_config_free(cfg);
87 }
88 if (ebase_ == nullptr) {
89 LOG(ERROR) << "failed to create event_base";
90 return false;
91 }
92 }
93 return true;
94 }
95
EventCallbackFn(int,int16_t,void * arg)96 void IOEventLoop::EventCallbackFn(int, int16_t, void* arg) {
97 IOEvent* e = static_cast<IOEvent*>(arg);
98 if (!e->callback()) {
99 e->loop->has_error_ = true;
100 e->loop->ExitLoop();
101 }
102 }
103
MakeFdNonBlocking(int fd)104 static bool MakeFdNonBlocking(int fd) {
105 int flags = fcntl(fd, F_GETFL, 0);
106 if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
107 PLOG(ERROR) << "fcntl() failed";
108 return false;
109 }
110 return true;
111 }
112
AddReadEvent(int fd,const std::function<bool ()> & callback)113 IOEventRef IOEventLoop::AddReadEvent(int fd, const std::function<bool()>& callback) {
114 if (!MakeFdNonBlocking(fd)) {
115 return nullptr;
116 }
117 return AddEvent(fd, EV_READ | EV_PERSIST, nullptr, callback);
118 }
119
AddWriteEvent(int fd,const std::function<bool ()> & callback)120 IOEventRef IOEventLoop::AddWriteEvent(int fd, const std::function<bool()>& callback) {
121 if (!MakeFdNonBlocking(fd)) {
122 return nullptr;
123 }
124 return AddEvent(fd, EV_WRITE | EV_PERSIST, nullptr, callback);
125 }
126
AddSignalEvent(int sig,const std::function<bool ()> & callback)127 bool IOEventLoop::AddSignalEvent(int sig, const std::function<bool()>& callback) {
128 return AddEvent(sig, EV_SIGNAL | EV_PERSIST, nullptr, callback) != nullptr;
129 }
130
AddSignalEvents(std::vector<int> sigs,const std::function<bool ()> & callback)131 bool IOEventLoop::AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback) {
132 for (auto sig : sigs) {
133 if (!AddSignalEvent(sig, callback)) {
134 return false;
135 }
136 }
137 return true;
138 }
139
AddPeriodicEvent(timeval duration,const std::function<bool ()> & callback)140 IOEventRef IOEventLoop::AddPeriodicEvent(timeval duration, const std::function<bool()>& callback) {
141 return AddEvent(-1, EV_PERSIST, &duration, callback);
142 }
143
AddEvent(int fd_or_sig,int16_t events,timeval * timeout,const std::function<bool ()> & callback)144 IOEventRef IOEventLoop::AddEvent(int fd_or_sig, int16_t events, timeval* timeout,
145 const std::function<bool()>& callback) {
146 if (!EnsureInit()) {
147 return nullptr;
148 }
149 std::unique_ptr<IOEvent> e(new IOEvent(this, callback));
150 e->e = event_new(ebase_, fd_or_sig, events, EventCallbackFn, e.get());
151 if (e->e == nullptr) {
152 LOG(ERROR) << "event_new() failed";
153 return nullptr;
154 }
155 if (event_add(e->e, timeout) != 0) {
156 LOG(ERROR) << "event_add() failed";
157 return nullptr;
158 }
159 if (timeout != nullptr) {
160 e->timeout = *timeout;
161 }
162 e->enabled = true;
163 events_.push_back(std::move(e));
164 return events_.back().get();
165 }
166
RunLoop()167 bool IOEventLoop::RunLoop() {
168 in_loop_ = true;
169 if (event_base_dispatch(ebase_) == -1) {
170 LOG(ERROR) << "event_base_dispatch() failed";
171 in_loop_ = false;
172 return false;
173 }
174 if (has_error_) {
175 return false;
176 }
177 return true;
178 }
179
ExitLoop()180 bool IOEventLoop::ExitLoop() {
181 if (in_loop_) {
182 if (event_base_loopbreak(ebase_) == -1) {
183 LOG(ERROR) << "event_base_loopbreak() failed";
184 return false;
185 }
186 in_loop_ = false;
187 }
188 return true;
189 }
190
DisableEvent(IOEventRef ref)191 bool IOEventLoop::DisableEvent(IOEventRef ref) {
192 if (ref->enabled) {
193 if (event_del(ref->e) != 0) {
194 LOG(ERROR) << "event_del() failed";
195 return false;
196 }
197 ref->enabled = false;
198 }
199 return true;
200 }
201
EnableEvent(IOEventRef ref)202 bool IOEventLoop::EnableEvent(IOEventRef ref) {
203 if (!ref->enabled) {
204 timeval* timeout =
205 (ref->timeout.tv_sec != 0 || ref->timeout.tv_usec != 0) ? &ref->timeout : nullptr;
206 if (event_add(ref->e, timeout) != 0) {
207 LOG(ERROR) << "event_add() failed";
208 return false;
209 }
210 ref->enabled = true;
211 }
212 return true;
213 }
214
DelEvent(IOEventRef ref)215 bool IOEventLoop::DelEvent(IOEventRef ref) {
216 DisableEvent(ref);
217 IOEventLoop* loop = ref->loop;
218 for (auto it = loop->events_.begin(); it != loop->events_.end(); ++it) {
219 if (it->get() == ref) {
220 loop->events_.erase(it);
221 break;
222 }
223 }
224 return true;
225 }
226
227 } // namespace simpleperf
228