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.
24 #include <sys/types.h>
29 #include <common/error.h>
30 #include <common/defaults.h>
31 #include <common/macros.h>
32 #include <common/utils.h>
36 unsigned int poll_max_size
;
39 * Resize the epoll events structure of the new size.
41 * Return 0 on success or else -1 with the current events pointer untouched.
43 static int resize_poll_event(struct lttng_poll_event
*events
,
46 struct epoll_event
*ptr
;
50 ptr
= realloc(events
->events
, new_size
* sizeof(*ptr
));
52 PERROR("realloc epoll add");
55 if (new_size
> events
->alloc_size
) {
56 /* Zero newly allocated memory */
57 memset(ptr
+ events
->alloc_size
, 0,
58 (new_size
- events
->alloc_size
) * sizeof(*ptr
));
61 events
->alloc_size
= new_size
;
70 * Create epoll set and allocate returned events structure.
72 int compat_epoll_create(struct lttng_poll_event
*events
, int size
, int flags
)
76 if (events
== NULL
|| size
<= 0) {
81 ERR("poll_max_size not initialized yet");
85 /* Don't bust the limit here */
86 if (size
> poll_max_size
) {
90 ret
= compat_glibc_epoll_create(size
, flags
);
92 /* At this point, every error is fatal */
93 PERROR("epoll_create1");
99 /* This *must* be freed by using lttng_poll_free() */
100 events
->events
= zmalloc(size
* sizeof(struct epoll_event
));
101 if (events
->events
== NULL
) {
102 PERROR("zmalloc epoll set");
106 events
->alloc_size
= events
->init_size
= size
;
112 ret
= close(events
->epfd
);
121 * Add a fd to the epoll set with requesting events.
123 int compat_epoll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
126 struct epoll_event ev
;
128 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
129 ERR("Bad compat epoll add arguments");
134 * Zero struct epoll_event to ensure all representations of its
137 memset(&ev
, 0, sizeof(ev
));
138 ev
.events
= req_events
;
141 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_ADD
, fd
, &ev
);
145 /* If exist, it's OK. */
149 /* Print PERROR and goto end not failing. Show must go on. */
150 PERROR("epoll_ctl ADD");
153 PERROR("epoll_ctl ADD fatal");
168 * Remove a fd from the epoll set.
170 int compat_epoll_del(struct lttng_poll_event
*events
, int fd
)
174 if (events
== NULL
|| fd
< 0 || events
->nb_fd
== 0) {
178 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_DEL
, fd
, NULL
);
183 /* Print PERROR and goto end not failing. Show must go on. */
184 PERROR("epoll_ctl DEL");
187 PERROR("epoll_ctl DEL fatal");
202 * Wait on epoll set. This is a blocking call of timeout value.
204 int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
)
209 if (events
== NULL
|| events
->events
== NULL
) {
210 ERR("Wrong arguments in compat_epoll_wait");
213 assert(events
->nb_fd
>= 0);
215 if (events
->nb_fd
== 0) {
221 * Resize if needed before waiting. We could either expand the array or
222 * shrink it down. It's important to note that after this step, we are
223 * ensured that the events argument of the epoll_wait call will be large
224 * enough to hold every possible returned events.
226 new_size
= 1U << utils_get_count_order_u32(events
->nb_fd
);
227 if (new_size
!= events
->alloc_size
&& new_size
>= events
->init_size
) {
228 ret
= resize_poll_event(events
, new_size
);
230 /* ENOMEM problem at this point. */
236 ret
= epoll_wait(events
->epfd
, events
->events
, events
->nb_fd
, timeout
);
237 } while (ret
== -1 && errno
== EINTR
);
239 /* At this point, every error is fatal */
240 PERROR("epoll_wait");
245 * Since the returned events are set sequentially in the "events" structure
246 * we only need to return the epoll_wait value and iterate over it.
255 * Setup poll set maximum size.
257 int compat_epoll_set_max_size(void)
259 int ret
, fd
, retval
= 0;
263 fd
= open(COMPAT_EPOLL_PROC_PATH
, O_RDONLY
);
269 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
271 * Allow reading a file smaller than buf, but keep space for
274 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
275 PERROR("read set max size");
279 buf
[size_ret
] = '\0';
280 poll_max_size
= atoi(buf
);
287 if (!poll_max_size
) {
288 poll_max_size
= DEFAULT_POLL_SIZE
;
290 DBG("epoll set max size is %d", poll_max_size
);
This page took 0.041696 seconds and 4 git commands to generate.