2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
13 #include <sys/types.h>
18 #include <common/error.h>
19 #include <common/defaults.h>
20 #include <common/macros.h>
21 #include <common/utils.h>
26 * Maximum number of fd we can monitor.
28 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
29 * be used for the maximum size of the poll set. If this interface is not
30 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
31 * of the available low memory divided by the registration cost in bytes which
32 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
35 static 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.
72 int compat_epoll_create(struct lttng_poll_event
*events
, int size
, int flags
)
76 if (events
== NULL
|| size
<= 0) {
81 if (lttng_poll_set_max_size()) {
86 /* Don't bust the limit here */
87 if (size
> poll_max_size
) {
91 ret
= compat_glibc_epoll_create(size
, flags
);
93 /* At this point, every error is fatal */
94 PERROR("epoll_create1");
100 /* This *must* be freed by using lttng_poll_free() */
101 events
->events
= zmalloc(size
* sizeof(struct epoll_event
));
102 if (events
->events
== NULL
) {
103 PERROR("zmalloc epoll set");
107 events
->alloc_size
= events
->init_size
= size
;
113 ret
= close(events
->epfd
);
122 * Add a fd to the epoll set with requesting events.
125 int compat_epoll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
128 struct epoll_event ev
;
130 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
131 ERR("Bad compat epoll add arguments");
136 * Zero struct epoll_event to ensure all representations of its
139 memset(&ev
, 0, sizeof(ev
));
140 ev
.events
= req_events
;
143 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_ADD
, fd
, &ev
);
147 /* If exist, it's OK. */
151 /* Print PERROR and goto end not failing. Show must go on. */
152 PERROR("epoll_ctl ADD");
155 PERROR("epoll_ctl ADD fatal");
170 * Remove a fd from the epoll set.
173 int compat_epoll_del(struct lttng_poll_event
*events
, int fd
)
177 if (events
== NULL
|| fd
< 0 || events
->nb_fd
== 0) {
181 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_DEL
, fd
, NULL
);
186 /* Print PERROR and goto end not failing. Show must go on. */
187 PERROR("epoll_ctl DEL");
190 PERROR("epoll_ctl DEL fatal");
205 * Set an fd's events.
208 int compat_epoll_mod(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
211 struct epoll_event ev
;
213 if (events
== NULL
|| fd
< 0 || events
->nb_fd
== 0) {
218 * Zero struct epoll_event to ensure all representations of its
221 memset(&ev
, 0, sizeof(ev
));
222 ev
.events
= req_events
;
225 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_MOD
, fd
, &ev
);
230 /* Print PERROR and goto end not failing. Show must go on. */
231 PERROR("epoll_ctl MOD");
234 PERROR("epoll_ctl MOD fatal");
247 * Wait on epoll set. This is a blocking call of timeout value.
250 int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
,
256 if (events
== NULL
|| events
->events
== NULL
) {
257 ERR("Wrong arguments in compat_epoll_wait");
261 if (events
->nb_fd
== 0) {
267 * Resize if needed before waiting. We could either expand the array or
268 * shrink it down. It's important to note that after this step, we are
269 * ensured that the events argument of the epoll_wait call will be large
270 * enough to hold every possible returned events.
272 new_size
= 1U << utils_get_count_order_u32(events
->nb_fd
);
273 if (new_size
!= events
->alloc_size
&& new_size
>= events
->init_size
) {
274 ret
= resize_poll_event(events
, new_size
);
276 /* ENOMEM problem at this point. */
282 ret
= epoll_wait(events
->epfd
, events
->events
, events
->nb_fd
, timeout
);
283 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
285 if (errno
!= EINTR
) {
286 PERROR("epoll_wait");
292 * Since the returned events are set sequentially in the "events" structure
293 * we only need to return the epoll_wait value and iterate over it.
302 * Setup poll set maximum size.
305 int compat_epoll_set_max_size(void)
307 int ret
, fd
, retval
= 0;
311 fd
= open(COMPAT_EPOLL_PROC_PATH
, O_RDONLY
);
314 * Failing on opening [1] is not an error per see. [1] was
315 * introduced in Linux 2.6.28 but epoll is available since
316 * 2.5.44. Hence, goto end and set a default value without
317 * setting an error return value.
319 * [1] /proc/sys/fs/epoll/max_user_watches
325 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
327 * Allow reading a file smaller than buf, but keep space for
330 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
331 PERROR("read set max size");
335 buf
[size_ret
] = '\0';
336 poll_max_size
= atoi(buf
);
343 if (!poll_max_size
) {
344 poll_max_size
= DEFAULT_POLL_SIZE
;
346 DBG("epoll set max size is %d", poll_max_size
);
This page took 0.055061 seconds and 4 git commands to generate.