1380ce85527a74dae304b2777de5a72c489b6b5c
[lttng-tools.git] / src / common / compat / compat-epoll.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <config.h>
27
28 #include <common/error.h>
29 #include <common/defaults.h>
30 #include <common/macros.h>
31 #include <common/utils.h>
32
33 #include "poll.h"
34
35 unsigned int poll_max_size;
36
37 /*
38 * Resize the epoll events structure of the new size.
39 *
40 * Return 0 on success or else -1 with the current events pointer untouched.
41 */
42 static int resize_poll_event(struct lttng_poll_event *events,
43 uint32_t new_size)
44 {
45 struct epoll_event *ptr;
46
47 assert(events);
48
49 ptr = realloc(events->events, new_size * sizeof(*ptr));
50 if (ptr == NULL) {
51 PERROR("realloc epoll add");
52 goto error;
53 }
54 events->events = ptr;
55 events->alloc_size = new_size;
56
57 return 0;
58
59 error:
60 return -1;
61 }
62
63 /*
64 * Create epoll set and allocate returned events structure.
65 */
66 int compat_epoll_create(struct lttng_poll_event *events, int size, int flags)
67 {
68 int ret;
69
70 if (events == NULL || size <= 0) {
71 goto error;
72 }
73
74 /* Don't bust the limit here */
75 if (size > poll_max_size && poll_max_size != 0) {
76 size = poll_max_size;
77 }
78
79 ret = epoll_create1(flags);
80 if (ret < 0) {
81 /* At this point, every error is fatal */
82 PERROR("epoll_create1");
83 goto error;
84 }
85
86 events->epfd = ret;
87
88 /* This *must* be freed by using lttng_poll_free() */
89 events->events = zmalloc(size * sizeof(struct epoll_event));
90 if (events->events == NULL) {
91 PERROR("zmalloc epoll set");
92 goto error_close;
93 }
94
95 events->alloc_size = events->init_size = size;
96 events->nb_fd = 0;
97
98 return 0;
99
100 error_close:
101 ret = close(events->epfd);
102 if (ret) {
103 PERROR("close");
104 }
105 error:
106 return -1;
107 }
108
109 /*
110 * Add a fd to the epoll set with requesting events.
111 */
112 int compat_epoll_add(struct lttng_poll_event *events, int fd, uint32_t req_events)
113 {
114 int ret;
115 struct epoll_event ev;
116
117 if (events == NULL || events->events == NULL || fd < 0) {
118 ERR("Bad compat epoll add arguments");
119 goto error;
120 }
121
122 ev.events = req_events;
123 ev.data.fd = fd;
124
125 ret = epoll_ctl(events->epfd, EPOLL_CTL_ADD, fd, &ev);
126 if (ret < 0) {
127 switch (errno) {
128 case EEXIST:
129 /* If exist, it's OK. */
130 goto end;
131 case ENOSPC:
132 case EPERM:
133 /* Print PERROR and goto end not failing. Show must go on. */
134 PERROR("epoll_ctl ADD");
135 goto end;
136 default:
137 PERROR("epoll_ctl ADD fatal");
138 goto error;
139 }
140 }
141
142 events->nb_fd++;
143
144 end:
145 return 0;
146
147 error:
148 return -1;
149 }
150
151 /*
152 * Remove a fd from the epoll set.
153 */
154 int compat_epoll_del(struct lttng_poll_event *events, int fd)
155 {
156 int ret;
157
158 if (events == NULL || fd < 0) {
159 goto error;
160 }
161
162 ret = epoll_ctl(events->epfd, EPOLL_CTL_DEL, fd, NULL);
163 if (ret < 0) {
164 switch (errno) {
165 case ENOENT:
166 case EPERM:
167 /* Print PERROR and goto end not failing. Show must go on. */
168 PERROR("epoll_ctl DEL");
169 goto end;
170 default:
171 PERROR("epoll_ctl DEL fatal");
172 goto error;
173 }
174 }
175
176 events->nb_fd--;
177
178 end:
179 return 0;
180
181 error:
182 return -1;
183 }
184
185 /*
186 * Wait on epoll set. This is a blocking call of timeout value.
187 */
188 int compat_epoll_wait(struct lttng_poll_event *events, int timeout)
189 {
190 int ret;
191 uint32_t new_size;
192
193 if (events == NULL || events->events == NULL) {
194 ERR("Wrong arguments in compat_epoll_wait");
195 goto error;
196 }
197
198 /*
199 * Resize if needed before waiting. We could either expand the array or
200 * shrink it down. It's important to note that after this step, we are
201 * ensured that the events argument of the epoll_wait call will be large
202 * enough to hold every possible returned events.
203 */
204 if (events->nb_fd > events->alloc_size) {
205 /* Expand if the nb_fd is higher than the actual size. */
206 new_size = max_t(uint32_t,
207 1U << utils_get_count_order_u32(events->nb_fd),
208 events->alloc_size << 1UL);
209 } else if ((events->nb_fd << 1UL) <= events->alloc_size &&
210 events->nb_fd >= events->init_size) {
211 /* Shrink if nb_fd multiplied by two is <= than the actual size. */
212 new_size = max_t(uint32_t,
213 utils_get_count_order_u32(events->nb_fd) >> 1U,
214 events->alloc_size >> 1U);
215 } else {
216 /* Indicate that we don't want to resize. */
217 new_size = 0;
218 }
219
220 if (new_size) {
221 ret = resize_poll_event(events, new_size);
222 if (ret < 0) {
223 /* ENOMEM problem at this point. */
224 goto error;
225 }
226 }
227
228 do {
229 ret = epoll_wait(events->epfd, events->events, events->nb_fd, timeout);
230 } while (ret == -1 && errno == EINTR);
231 if (ret < 0) {
232 /* At this point, every error is fatal */
233 PERROR("epoll_wait");
234 goto error;
235 }
236
237 /*
238 * Since the returned events are set sequentially in the "events" structure
239 * we only need to return the epoll_wait value and iterate over it.
240 */
241 return ret;
242
243 error:
244 return -1;
245 }
246
247 /*
248 * Setup poll set maximum size.
249 */
250 void compat_epoll_set_max_size(void)
251 {
252 int ret, fd;
253 size_t size_ret;
254 char buf[64];
255
256 poll_max_size = DEFAULT_POLL_SIZE;
257
258 fd = open(COMPAT_EPOLL_PROC_PATH, O_RDONLY);
259 if (fd < 0) {
260 return;
261 }
262
263 size_ret = lttng_read(fd, buf, sizeof(buf));
264 /*
265 * Allow reading a file smaller than buf, but keep space for
266 * final \0.
267 */
268 if (size_ret < 0 || size_ret >= sizeof(buf)) {
269 PERROR("read set max size");
270 goto error;
271 }
272 buf[size_ret] = '\0';
273
274 poll_max_size = atoi(buf);
275 if (poll_max_size == 0) {
276 /* Extra precaution */
277 poll_max_size = DEFAULT_POLL_SIZE;
278 }
279
280 DBG("epoll set max size is %d", poll_max_size);
281
282 error:
283 ret = close(fd);
284 if (ret) {
285 PERROR("close");
286 }
287 }
This page took 0.035306 seconds and 4 git commands to generate.