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>
36 * Maximum number of fd we can monitor.
38 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
39 * be used for the maximum size of the poll set. If this interface is not
40 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
41 * of the available low memory divided by the registration cost in bytes which
42 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
45 static unsigned int poll_max_size
;
48 * Resize the epoll events structure of the new size.
50 * Return 0 on success or else -1 with the current events pointer untouched.
52 static int resize_poll_event(struct lttng_poll_event
*events
,
55 struct epoll_event
*ptr
;
59 ptr
= realloc(events
->events
, new_size
* sizeof(*ptr
));
61 PERROR("realloc epoll add");
64 if (new_size
> events
->alloc_size
) {
65 /* Zero newly allocated memory */
66 memset(ptr
+ events
->alloc_size
, 0,
67 (new_size
- events
->alloc_size
) * sizeof(*ptr
));
70 events
->alloc_size
= new_size
;
79 * Create epoll set and allocate returned events structure.
82 int compat_epoll_create(struct lttng_poll_event
*events
, int size
, int flags
)
86 if (events
== NULL
|| size
<= 0) {
91 if (lttng_poll_set_max_size()) {
96 /* Don't bust the limit here */
97 if (size
> poll_max_size
) {
101 ret
= compat_glibc_epoll_create(size
, flags
);
103 /* At this point, every error is fatal */
104 PERROR("epoll_create1");
110 /* This *must* be freed by using lttng_poll_free() */
111 events
->events
= zmalloc(size
* sizeof(struct epoll_event
));
112 if (events
->events
== NULL
) {
113 PERROR("zmalloc epoll set");
117 events
->alloc_size
= events
->init_size
= size
;
123 ret
= close(events
->epfd
);
132 * Add a fd to the epoll set with requesting events.
135 int compat_epoll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
138 struct epoll_event ev
;
140 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
141 ERR("Bad compat epoll add arguments");
146 * Zero struct epoll_event to ensure all representations of its
149 memset(&ev
, 0, sizeof(ev
));
150 ev
.events
= req_events
;
153 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_ADD
, fd
, &ev
);
157 /* If exist, it's OK. */
161 /* Print PERROR and goto end not failing. Show must go on. */
162 PERROR("epoll_ctl ADD");
165 PERROR("epoll_ctl ADD fatal");
180 * Remove a fd from the epoll set.
183 int compat_epoll_del(struct lttng_poll_event
*events
, int fd
)
187 if (events
== NULL
|| fd
< 0 || events
->nb_fd
== 0) {
191 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_DEL
, fd
, NULL
);
196 /* Print PERROR and goto end not failing. Show must go on. */
197 PERROR("epoll_ctl DEL");
200 PERROR("epoll_ctl DEL fatal");
215 * Set an fd's events.
218 int compat_epoll_mod(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
221 struct epoll_event ev
;
223 if (events
== NULL
|| fd
< 0 || events
->nb_fd
== 0) {
228 * Zero struct epoll_event to ensure all representations of its
231 memset(&ev
, 0, sizeof(ev
));
232 ev
.events
= req_events
;
235 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_MOD
, fd
, &ev
);
240 /* Print PERROR and goto end not failing. Show must go on. */
241 PERROR("epoll_ctl MOD");
244 PERROR("epoll_ctl MOD fatal");
257 * Wait on epoll set. This is a blocking call of timeout value.
260 int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
,
266 if (events
== NULL
|| events
->events
== NULL
) {
267 ERR("Wrong arguments in compat_epoll_wait");
271 if (events
->nb_fd
== 0) {
277 * Resize if needed before waiting. We could either expand the array or
278 * shrink it down. It's important to note that after this step, we are
279 * ensured that the events argument of the epoll_wait call will be large
280 * enough to hold every possible returned events.
282 new_size
= 1U << utils_get_count_order_u32(events
->nb_fd
);
283 if (new_size
!= events
->alloc_size
&& new_size
>= events
->init_size
) {
284 ret
= resize_poll_event(events
, new_size
);
286 /* ENOMEM problem at this point. */
292 ret
= epoll_wait(events
->epfd
, events
->events
, events
->nb_fd
, timeout
);
293 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
295 if (errno
!= EINTR
) {
296 PERROR("epoll_wait");
302 * Since the returned events are set sequentially in the "events" structure
303 * we only need to return the epoll_wait value and iterate over it.
312 * Setup poll set maximum size.
315 int compat_epoll_set_max_size(void)
317 int ret
, fd
, retval
= 0;
321 fd
= open(COMPAT_EPOLL_PROC_PATH
, O_RDONLY
);
324 * Failing on opening [1] is not an error per see. [1] was
325 * introduced in Linux 2.6.28 but epoll is available since
326 * 2.5.44. Hence, goto end and set a default value without
327 * setting an error return value.
329 * [1] /proc/sys/fs/epoll/max_user_watches
335 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
337 * Allow reading a file smaller than buf, but keep space for
340 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
341 PERROR("read set max size");
345 buf
[size_ret
] = '\0';
346 poll_max_size
= atoi(buf
);
353 if (!poll_max_size
) {
354 poll_max_size
= DEFAULT_POLL_SIZE
;
356 DBG("epoll set max size is %d", poll_max_size
);
This page took 0.059872 seconds and 4 git commands to generate.