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() : ebase_(nullptr), has_error_(false), in_loop_(false) {}
44
~IOEventLoop()45 IOEventLoop::~IOEventLoop() {
46 events_.clear();
47 if (ebase_ != nullptr) {
48 event_base_free(ebase_);
49 }
50 }
51
EnsureInit()52 bool IOEventLoop::EnsureInit() {
53 if (ebase_ == nullptr) {
54 event_config* cfg = event_config_new();
55 if (cfg != nullptr) {
56 event_config_set_flag(cfg, EVENT_BASE_FLAG_PRECISE_TIMER);
57 if (event_config_avoid_method(cfg, "epoll") != 0) {
58 LOG(ERROR) << "event_config_avoid_method";
59 return false;
60 }
61 ebase_ = event_base_new_with_config(cfg);
62 // perf event files support reporting available data via poll methods. However, it doesn't
63 // work well with epoll. Because perf_poll() in kernel/events/core.c uses a report and reset
64 // way to report poll events. If perf_poll() is called twice, it may return POLLIN for the
65 // first time, and no events for the second time. And epoll may call perf_poll() more than
66 // once to confirm events. A failed situation is below:
67 // When profiling SimpleperfExampleOfKotlin on Pixel device with `-g --duration 10`, the
68 // kernel fills up the buffer before we call epoll_ctl(EPOLL_CTL_ADD). Then the POLLIN event
69 // is returned when calling epoll_ctl(), while no events are returned when calling
70 // epoll_wait(). As a result, simpleperf doesn't receive any poll wakeup events.
71 if (strcmp(event_base_get_method(ebase_), "poll") != 0) {
72 LOG(ERROR) << "event_base_get_method isn't poll: " << event_base_get_method(ebase_);
73 return false;
74 }
75 event_config_free(cfg);
76 if (event_base_priority_init(ebase_, 2) != 0) {
77 LOG(ERROR) << "event_base_priority_init failed";
78 return false;
79 }
80 }
81 if (ebase_ == nullptr) {
82 LOG(ERROR) << "failed to create event_base";
83 return false;
84 }
85 }
86 return true;
87 }
88
EventCallbackFn(int,int16_t,void * arg)89 void IOEventLoop::EventCallbackFn(int, int16_t, void* arg) {
90 IOEvent* e = static_cast<IOEvent*>(arg);
91 if (!e->callback()) {
92 e->loop->has_error_ = true;
93 e->loop->ExitLoop();
94 }
95 }
96
MakeFdNonBlocking(int fd)97 static bool MakeFdNonBlocking(int fd) {
98 int flags = fcntl(fd, F_GETFL, 0);
99 if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
100 PLOG(ERROR) << "fcntl() failed";
101 return false;
102 }
103 return true;
104 }
105
AddReadEvent(int fd,const std::function<bool ()> & callback,IOEventPriority priority)106 IOEventRef IOEventLoop::AddReadEvent(int fd, const std::function<bool()>& callback,
107 IOEventPriority priority) {
108 if (!MakeFdNonBlocking(fd)) {
109 return nullptr;
110 }
111 return AddEvent(fd, EV_READ | EV_PERSIST, nullptr, callback, priority);
112 }
113
AddWriteEvent(int fd,const std::function<bool ()> & callback,IOEventPriority priority)114 IOEventRef IOEventLoop::AddWriteEvent(int fd, const std::function<bool()>& callback,
115 IOEventPriority priority) {
116 if (!MakeFdNonBlocking(fd)) {
117 return nullptr;
118 }
119 return AddEvent(fd, EV_WRITE | EV_PERSIST, nullptr, callback, priority);
120 }
121
AddSignalEvent(int sig,const std::function<bool ()> & callback,IOEventPriority priority)122 bool IOEventLoop::AddSignalEvent(int sig, const std::function<bool()>& callback,
123 IOEventPriority priority) {
124 return AddEvent(sig, EV_SIGNAL | EV_PERSIST, nullptr, callback, priority) != nullptr;
125 }
126
AddSignalEvents(std::vector<int> sigs,const std::function<bool ()> & callback,IOEventPriority priority)127 bool IOEventLoop::AddSignalEvents(std::vector<int> sigs, const std::function<bool()>& callback,
128 IOEventPriority priority) {
129 for (auto sig : sigs) {
130 if (!AddSignalEvent(sig, callback, priority)) {
131 return false;
132 }
133 }
134 return true;
135 }
136
AddPeriodicEvent(timeval duration,const std::function<bool ()> & callback,IOEventPriority priority)137 IOEventRef IOEventLoop::AddPeriodicEvent(timeval duration, const std::function<bool()>& callback,
138 IOEventPriority priority) {
139 return AddEvent(-1, EV_PERSIST, &duration, callback, priority);
140 }
141
AddOneTimeEvent(timeval duration,const std::function<bool ()> & callback,IOEventPriority priority)142 IOEventRef IOEventLoop::AddOneTimeEvent(timeval duration, const std::function<bool()>& callback,
143 IOEventPriority priority) {
144 return AddEvent(-1, 0, &duration, callback, priority);
145 }
146
AddEvent(int fd_or_sig,int16_t events,timeval * timeout,const std::function<bool ()> & callback,IOEventPriority priority)147 IOEventRef IOEventLoop::AddEvent(int fd_or_sig, int16_t events, timeval* timeout,
148 const std::function<bool()>& callback, IOEventPriority priority) {
149 if (!EnsureInit()) {
150 return nullptr;
151 }
152 std::unique_ptr<IOEvent> e(new IOEvent(this, callback));
153 e->e = event_new(ebase_, fd_or_sig, events, EventCallbackFn, e.get());
154 if (e->e == nullptr) {
155 LOG(ERROR) << "event_new() failed";
156 return nullptr;
157 }
158 event_priority_set(e->e, priority);
159 if (event_add(e->e, timeout) != 0) {
160 LOG(ERROR) << "event_add() failed";
161 return nullptr;
162 }
163 if (timeout != nullptr) {
164 e->timeout = *timeout;
165 }
166 e->enabled = true;
167 events_.push_back(std::move(e));
168 return events_.back().get();
169 }
170
RunLoop()171 bool IOEventLoop::RunLoop() {
172 in_loop_ = true;
173 if (event_base_dispatch(ebase_) == -1) {
174 LOG(ERROR) << "event_base_dispatch() failed";
175 in_loop_ = false;
176 return false;
177 }
178 if (has_error_) {
179 return false;
180 }
181 return true;
182 }
183
ExitLoop()184 bool IOEventLoop::ExitLoop() {
185 if (in_loop_) {
186 if (event_base_loopbreak(ebase_) == -1) {
187 LOG(ERROR) << "event_base_loopbreak() failed";
188 return false;
189 }
190 in_loop_ = false;
191 }
192 return true;
193 }
194
DisableEvent(IOEventRef ref)195 bool IOEventLoop::DisableEvent(IOEventRef ref) {
196 if (ref->enabled) {
197 if (event_del(ref->e) != 0) {
198 LOG(ERROR) << "event_del() failed";
199 return false;
200 }
201 ref->enabled = false;
202 }
203 return true;
204 }
205
EnableEvent(IOEventRef ref)206 bool IOEventLoop::EnableEvent(IOEventRef ref) {
207 if (!ref->enabled) {
208 timeval* timeout =
209 (ref->timeout.tv_sec != 0 || ref->timeout.tv_usec != 0) ? &ref->timeout : nullptr;
210 if (event_add(ref->e, timeout) != 0) {
211 LOG(ERROR) << "event_add() failed";
212 return false;
213 }
214 ref->enabled = true;
215 }
216 return true;
217 }
218
DelEvent(IOEventRef ref)219 bool IOEventLoop::DelEvent(IOEventRef ref) {
220 DisableEvent(ref);
221 IOEventLoop* loop = ref->loop;
222 for (auto it = loop->events_.begin(); it != loop->events_.end(); ++it) {
223 if (it->get() == ref) {
224 loop->events_.erase(it);
225 break;
226 }
227 }
228 return true;
229 }
230
231 } // namespace simpleperf
232