2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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
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.
23 #include <sys/types.h>
28 #include <common/error.h>
29 #include <common/defaults.h>
33 unsigned int poll_max_size
;
36 * Resize the epoll events structure of the new size.
38 * Return 0 on success or else -1 with the current events pointer untouched.
40 static int resize_poll_event(struct lttng_poll_event
*events
,
43 struct epoll_event
*ptr
;
47 ptr
= realloc(events
->events
, new_size
* sizeof(*ptr
));
49 PERROR("realloc epoll add");
53 events
->alloc_size
= new_size
;
62 * Create epoll set and allocate returned events structure.
64 int compat_epoll_create(struct lttng_poll_event
*events
, int size
, int flags
)
68 if (events
== NULL
|| size
<= 0) {
72 /* Don't bust the limit here */
73 if (size
> poll_max_size
&& poll_max_size
!= 0) {
77 ret
= epoll_create1(flags
);
79 /* At this point, every error is fatal */
80 PERROR("epoll_create1");
86 /* This *must* be freed by using lttng_poll_free() */
87 events
->events
= zmalloc(size
* sizeof(struct epoll_event
));
88 if (events
->events
== NULL
) {
89 PERROR("zmalloc epoll set");
93 events
->alloc_size
= events
->init_size
= size
;
99 ret
= close(events
->epfd
);
108 * Add a fd to the epoll set with requesting events.
110 int compat_epoll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
113 struct epoll_event ev
;
115 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
116 ERR("Bad compat epoll add arguments");
120 ev
.events
= req_events
;
123 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_ADD
, fd
, &ev
);
127 /* If exist, it's OK. */
131 /* Print PERROR and goto end not failing. Show must go on. */
132 PERROR("epoll_ctl ADD");
135 PERROR("epoll_ctl ADD fatal");
150 * Remove a fd from the epoll set.
152 int compat_epoll_del(struct lttng_poll_event
*events
, int fd
)
156 if (events
== NULL
|| fd
< 0) {
160 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_DEL
, fd
, NULL
);
165 /* Print PERROR and goto end not failing. Show must go on. */
166 PERROR("epoll_ctl DEL");
169 PERROR("epoll_ctl DEL fatal");
172 PERROR("epoll_ctl del");
186 * Wait on epoll set. This is a blocking call of timeout value.
188 int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
)
193 if (events
== NULL
|| events
->events
== NULL
) {
194 ERR("Wrong arguments in compat_epoll_wait");
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.
204 if (events
->nb_fd
> events
->alloc_size
) {
205 /* Expand if the nb_fd is higher than the actual size. */
206 new_size
= events
->alloc_size
<< 1UL;
207 } else if ((events
->nb_fd
<< 1UL) <= events
->alloc_size
&&
208 events
->nb_fd
>= events
->init_size
) {
209 /* Shrink if nb_fd multiplied by two is <= than the actual size. */
210 new_size
= events
->alloc_size
>> 1UL;
212 /* Indicate that we don't want to resize. */
217 ret
= resize_poll_event(events
, new_size
);
219 /* ENOMEM problem at this point. */
225 ret
= epoll_wait(events
->epfd
, events
->events
, events
->nb_fd
, timeout
);
226 } while (ret
== -1 && errno
== EINTR
);
228 /* At this point, every error is fatal */
229 PERROR("epoll_wait");
234 * Since the returned events are set sequentially in the "events" structure
235 * we only need to return the epoll_wait value and iterate over it.
244 * Setup poll set maximum size.
246 void compat_epoll_set_max_size(void)
251 poll_max_size
= DEFAULT_POLL_SIZE
;
253 fd
= open(COMPAT_EPOLL_PROC_PATH
, O_RDONLY
);
258 ret
= read(fd
, buf
, sizeof(buf
));
260 PERROR("read set max size");
264 poll_max_size
= atoi(buf
);
265 if (poll_max_size
== 0) {
266 /* Extra precaution */
267 poll_max_size
= DEFAULT_POLL_SIZE
;
270 DBG("epoll set max size is %d", poll_max_size
);
This page took 0.034432 seconds and 4 git commands to generate.