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>
30 #include <common/macros.h>
31 #include <common/utils.h>
35 unsigned int poll_max_size
;
38 * Resize the epoll events structure of the new size.
40 * Return 0 on success or else -1 with the current events pointer untouched.
42 static int resize_poll_event(struct lttng_poll_event
*events
,
45 struct epoll_event
*ptr
;
49 ptr
= realloc(events
->events
, new_size
* sizeof(*ptr
));
51 PERROR("realloc epoll add");
54 if (new_size
> events
->alloc_size
) {
55 /* Zero newly allocated memory */
56 memset(ptr
+ events
->alloc_size
, 0,
57 (new_size
- events
->alloc_size
) * sizeof(*ptr
));
60 events
->alloc_size
= new_size
;
69 * Create epoll set and allocate returned events structure.
71 int compat_epoll_create(struct lttng_poll_event
*events
, int size
, int flags
)
75 if (events
== NULL
|| size
<= 0) {
79 /* Don't bust the limit here */
80 if (size
> poll_max_size
&& poll_max_size
!= 0) {
84 ret
= compat_glibc_epoll_create(size
, flags
);
86 /* At this point, every error is fatal */
87 PERROR("epoll_create1");
93 /* This *must* be freed by using lttng_poll_free() */
94 events
->events
= zmalloc(size
* sizeof(struct epoll_event
));
95 if (events
->events
== NULL
) {
96 PERROR("zmalloc epoll set");
100 events
->alloc_size
= events
->init_size
= size
;
106 ret
= close(events
->epfd
);
115 * Add a fd to the epoll set with requesting events.
117 int compat_epoll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
120 struct epoll_event ev
;
122 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
123 ERR("Bad compat epoll add arguments");
128 * Zero struct epoll_event to ensure all representations of its
131 memset(&ev
, 0, sizeof(ev
));
132 ev
.events
= req_events
;
135 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_ADD
, fd
, &ev
);
139 /* If exist, it's OK. */
143 /* Print PERROR and goto end not failing. Show must go on. */
144 PERROR("epoll_ctl ADD");
147 PERROR("epoll_ctl ADD fatal");
162 * Remove a fd from the epoll set.
164 int compat_epoll_del(struct lttng_poll_event
*events
, int fd
)
168 if (events
== NULL
|| fd
< 0) {
172 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_DEL
, fd
, NULL
);
177 /* Print PERROR and goto end not failing. Show must go on. */
178 PERROR("epoll_ctl DEL");
181 PERROR("epoll_ctl DEL fatal");
196 * Wait on epoll set. This is a blocking call of timeout value.
198 int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
)
203 if (events
== NULL
|| events
->events
== NULL
) {
204 ERR("Wrong arguments in compat_epoll_wait");
209 * Resize if needed before waiting. We could either expand the array or
210 * shrink it down. It's important to note that after this step, we are
211 * ensured that the events argument of the epoll_wait call will be large
212 * enough to hold every possible returned events.
214 if (events
->nb_fd
> events
->alloc_size
) {
215 /* Expand if the nb_fd is higher than the actual size. */
216 new_size
= max_t(uint32_t,
217 1U << utils_get_count_order_u32(events
->nb_fd
),
218 events
->alloc_size
<< 1UL);
219 } else if ((events
->nb_fd
<< 1UL) <= events
->alloc_size
&&
220 events
->nb_fd
>= events
->init_size
) {
221 /* Shrink if nb_fd multiplied by two is <= than the actual size. */
222 new_size
= max_t(uint32_t,
223 utils_get_count_order_u32(events
->nb_fd
) >> 1U,
224 events
->alloc_size
>> 1U);
226 /* Indicate that we don't want to resize. */
231 ret
= resize_poll_event(events
, new_size
);
233 /* ENOMEM problem at this point. */
239 ret
= epoll_wait(events
->epfd
, events
->events
, events
->nb_fd
, timeout
);
240 } while (ret
== -1 && errno
== EINTR
);
242 /* At this point, every error is fatal */
243 PERROR("epoll_wait");
248 * Since the returned events are set sequentially in the "events" structure
249 * we only need to return the epoll_wait value and iterate over it.
258 * Setup poll set maximum size.
260 void compat_epoll_set_max_size(void)
266 poll_max_size
= DEFAULT_POLL_SIZE
;
268 fd
= open(COMPAT_EPOLL_PROC_PATH
, O_RDONLY
);
273 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
275 * Allow reading a file smaller than buf, but keep space for
278 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
279 PERROR("read set max size");
282 buf
[size_ret
] = '\0';
284 poll_max_size
= atoi(buf
);
285 if (poll_max_size
== 0) {
286 /* Extra precaution */
287 poll_max_size
= DEFAULT_POLL_SIZE
;
290 DBG("epoll set max size is %d", poll_max_size
);
This page took 0.036903 seconds and 4 git commands to generate.