2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
12 #include <common/defaults.hpp>
13 #include <common/error.hpp>
14 #include <common/macros.hpp>
15 #include <common/utils.hpp>
25 #include <sys/types.h>
29 * Maximum number of fd we can monitor.
31 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
32 * be used for the maximum size of the poll set. If this interface is not
33 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
34 * of the available low memory divided by the registration cost in bytes which
35 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
38 static unsigned int poll_max_size
;
41 * Resize the epoll events structure of the new size.
43 * Return 0 on success or else -1 with the current events pointer untouched.
45 static int resize_poll_event(struct lttng_poll_event
*events
, uint32_t new_size
)
47 struct epoll_event
*ptr
;
51 ptr
= (epoll_event
*) realloc(events
->events
, new_size
* sizeof(*ptr
));
53 PERROR("realloc epoll add");
56 if (new_size
> events
->alloc_size
) {
57 /* Zero newly allocated memory */
58 memset(ptr
+ events
->alloc_size
, 0, (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 count
, int flags
)
76 if (events
== nullptr || count
<= 0) {
81 if (lttng_poll_set_max_size()) {
86 /* Don't bust the limit here */
87 if (count
> poll_max_size
) {
88 count
= poll_max_size
;
91 ret
= compat_glibc_epoll_create(count
, 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
= calloc
<epoll_event
>(count
);
102 if (events
->events
== nullptr) {
103 PERROR("zmalloc epoll set");
107 events
->alloc_size
= events
->init_size
= count
;
113 ret
= close(events
->epfd
);
122 * Add a fd to the epoll set with requesting events.
124 int compat_epoll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
127 struct epoll_event ev
;
129 if (events
== nullptr || events
->events
== nullptr || fd
< 0) {
130 ERR("Bad compat epoll add arguments");
135 * Zero struct epoll_event to ensure all representations of its
138 memset(&ev
, 0, sizeof(ev
));
139 ev
.events
= req_events
;
142 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_ADD
, fd
, &ev
);
146 /* If exist, it's OK. */
150 /* Print PERROR and goto end not failing. Show must go on. */
151 PERROR("epoll_ctl ADD");
154 PERROR("epoll_ctl ADD fatal");
169 * Remove a fd from the epoll set.
171 int compat_epoll_del(struct lttng_poll_event
*events
, int fd
)
175 if (events
== nullptr || fd
< 0 || events
->nb_fd
== 0) {
179 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_DEL
, fd
, nullptr);
184 /* Print PERROR and goto end not failing. Show must go on. */
185 PERROR("epoll_ctl DEL");
188 PERROR("epoll_ctl DEL fatal");
203 * Set an fd's events.
205 int compat_epoll_mod(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
208 struct epoll_event ev
;
210 if (events
== nullptr || fd
< 0 || events
->nb_fd
== 0) {
215 * Zero struct epoll_event to ensure all representations of its
218 memset(&ev
, 0, sizeof(ev
));
219 ev
.events
= req_events
;
222 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_MOD
, fd
, &ev
);
227 /* Print PERROR and goto end not failing. Show must go on. */
228 PERROR("epoll_ctl MOD");
231 PERROR("epoll_ctl MOD fatal");
244 * Wait on epoll set. This is a blocking call of timeout value.
246 int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
, bool interruptible
)
251 if (events
== nullptr || events
->events
== nullptr) {
252 ERR("Wrong arguments in compat_epoll_wait");
256 if (events
->nb_fd
== 0) {
262 * Resize if needed before waiting. We could either expand the array or
263 * shrink it down. It's important to note that after this step, we are
264 * ensured that the events argument of the epoll_wait call will be large
265 * enough to hold every possible returned events.
267 new_size
= 1U << utils_get_count_order_u32(events
->nb_fd
);
268 if (new_size
!= events
->alloc_size
&& new_size
>= events
->init_size
) {
269 ret
= resize_poll_event(events
, new_size
);
271 /* ENOMEM problem at this point. */
277 ret
= epoll_wait(events
->epfd
, events
->events
, events
->nb_fd
, timeout
);
278 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
280 if (errno
!= EINTR
) {
281 PERROR("epoll_wait");
287 * Since the returned events are set sequentially in the "events" structure
288 * we only need to return the epoll_wait value and iterate over it.
297 * Setup poll set maximum size.
299 int compat_epoll_set_max_size()
301 int ret
, fd
, retval
= 0;
305 fd
= open(COMPAT_EPOLL_PROC_PATH
, O_RDONLY
);
308 * Failing on opening [1] is not an error per see. [1] was
309 * introduced in Linux 2.6.28 but epoll is available since
310 * 2.5.44. Hence, goto end and set a default value without
311 * setting an error return value.
313 * [1] /proc/sys/fs/epoll/max_user_watches
319 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
321 * Allow reading a file smaller than buf, but keep space for
324 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
325 PERROR("read set max size");
329 buf
[size_ret
] = '\0';
330 poll_max_size
= atoi(buf
);
337 if (!poll_max_size
) {
338 poll_max_size
= DEFAULT_POLL_SIZE
;
340 DBG("epoll set max size is %d", poll_max_size
);
344 #else /* HAVE_EPOLL */
346 #include <sys/resource.h>
347 #include <sys/time.h>
350 * Maximum number of fd we can monitor.
352 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
355 static unsigned int poll_max_size
;
358 * Resize the epoll events structure of the new size.
360 * Return 0 on success or else -1 with the current events pointer untouched.
362 static int resize_poll_event(struct compat_poll_event_array
*array
, uint32_t new_size
)
368 /* Refuse to resize the array more than the max size. */
369 if (new_size
> poll_max_size
) {
373 ptr
= (struct pollfd
*) realloc(array
->events
, new_size
* sizeof(*ptr
));
375 PERROR("realloc epoll add");
378 if (new_size
> array
->alloc_size
) {
379 /* Zero newly allocated memory */
380 memset(ptr
+ array
->alloc_size
, 0, (new_size
- array
->alloc_size
) * sizeof(*ptr
));
383 array
->alloc_size
= new_size
;
392 * Update events with the current events object.
394 static int update_current_events(struct lttng_poll_event
*events
)
397 struct compat_poll_event_array
*current
, *wait
;
399 LTTNG_ASSERT(events
);
401 current
= &events
->current
;
402 wait
= &events
->wait
;
404 wait
->nb_fd
= current
->nb_fd
;
405 if (current
->alloc_size
!= wait
->alloc_size
) {
406 ret
= resize_poll_event(wait
, current
->alloc_size
);
411 memcpy(wait
->events
, current
->events
, current
->nb_fd
* sizeof(*current
->events
));
413 /* Update is done. */
414 events
->need_update
= 0;
423 * Create pollfd data structure.
425 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
427 struct compat_poll_event_array
*current
, *wait
;
429 if (events
== NULL
|| size
<= 0) {
430 ERR("Wrong arguments for poll create");
434 if (!poll_max_size
) {
435 if (lttng_poll_set_max_size()) {
440 /* Don't bust the limit here */
441 if (size
> poll_max_size
) {
442 size
= poll_max_size
;
445 /* Reset everything before begining the allocation. */
446 memset(events
, 0, sizeof(struct lttng_poll_event
));
448 current
= &events
->current
;
449 wait
= &events
->wait
;
451 /* This *must* be freed by using lttng_poll_free() */
452 wait
->events
= calloc
<struct pollfd
>(size
);
453 if (wait
->events
== NULL
) {
454 PERROR("Failed to allocate wait events array during poll initialization");
458 wait
->alloc_size
= wait
->init_size
= size
;
460 current
->events
= calloc
<struct pollfd
>(size
);
461 if (current
->events
== NULL
) {
462 PERROR("Failed to allocate current events array during poll initialization");
466 current
->alloc_size
= current
->init_size
= size
;
475 * Add fd to pollfd data structure with requested events.
477 int compat_poll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
479 int new_size
, ret
, i
;
480 struct compat_poll_event_array
*current
;
482 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
483 ERR("Bad compat poll add arguments");
487 current
= &events
->current
;
489 /* Check if fd we are trying to add is already there. */
490 for (i
= 0; i
< current
->nb_fd
; i
++) {
491 if (current
->events
[i
].fd
== fd
) {
497 /* Resize array if needed. */
498 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
+ 1);
499 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
500 ret
= resize_poll_event(current
, new_size
);
506 current
->events
[current
->nb_fd
].fd
= fd
;
507 current
->events
[current
->nb_fd
].events
= req_events
;
509 events
->need_update
= 1;
511 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
520 * Modify an fd's events..
522 int compat_poll_mod(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
525 struct compat_poll_event_array
*current
;
527 if (events
== NULL
|| events
->current
.nb_fd
== 0 || events
->current
.events
== NULL
||
529 ERR("Bad compat poll mod arguments");
533 current
= &events
->current
;
535 for (i
= 0; i
< current
->nb_fd
; i
++) {
536 if (current
->events
[i
].fd
== fd
) {
537 current
->events
[i
].events
= req_events
;
538 events
->need_update
= 1;
544 * The epoll flavor doesn't flag modifying a non-included FD as an
555 * Remove a fd from the pollfd structure.
557 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
559 int i
, count
= 0, ret
;
561 struct compat_poll_event_array
*current
;
563 if (events
== NULL
|| events
->current
.nb_fd
== 0 || events
->current
.events
== NULL
||
568 /* Ease our life a bit. */
569 current
= &events
->current
;
571 for (i
= 0; i
< current
->nb_fd
; i
++) {
572 /* Don't put back the fd we want to delete */
573 if (current
->events
[i
].fd
!= fd
) {
574 current
->events
[count
].fd
= current
->events
[i
].fd
;
575 current
->events
[count
].events
= current
->events
[i
].events
;
580 /* The fd was not in our set, return no error as with epoll. */
581 if (current
->nb_fd
== count
) {
585 /* No fd duplicate should be ever added into array. */
586 LTTNG_ASSERT(current
->nb_fd
- 1 == count
);
587 current
->nb_fd
= count
;
589 /* Resize array if needed. */
590 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
);
591 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
&&
592 current
->nb_fd
!= 0) {
593 ret
= resize_poll_event(current
, new_size
);
599 events
->need_update
= 1;
609 * Wait on poll() with timeout. Blocking call.
611 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
, bool interruptible
)
613 int ret
, active_fd_count
;
614 size_t pos
= 0, consecutive_entries
= 0, non_idle_pos
;
616 if (events
== NULL
|| events
->current
.events
== NULL
) {
617 ERR("poll wait arguments error");
621 if (events
->current
.nb_fd
== 0) {
622 /* Return an invalid error to be consistent with epoll. */
624 events
->wait
.nb_fd
= 0;
628 if (events
->need_update
) {
629 ret
= update_current_events(events
);
637 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
638 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
640 if (errno
!= EINTR
) {
646 active_fd_count
= ret
;
649 * Move all active pollfd structs to the beginning of the
650 * array to emulate compat-epoll behaviour.
652 if (active_fd_count
== events
->wait
.nb_fd
) {
656 while (consecutive_entries
!= active_fd_count
) {
657 struct pollfd
*current
= &events
->wait
.events
[pos
];
658 struct pollfd idle_entry
;
660 if (current
->revents
!= 0) {
661 consecutive_entries
++;
668 /* Look for next non-idle entry. */
669 while (events
->wait
.events
[++non_idle_pos
].revents
== 0)
672 /* Swap idle and non-idle entries. */
673 idle_entry
= *current
;
674 *current
= events
->wait
.events
[non_idle_pos
];
675 events
->wait
.events
[non_idle_pos
] = idle_entry
;
677 consecutive_entries
++;
688 * Setup poll set maximum size.
690 int compat_poll_set_max_size(void)
695 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
697 PERROR("getrlimit poll RLIMIT_NOFILE");
702 poll_max_size
= lim
.rlim_cur
;
704 if (poll_max_size
== 0) {
705 poll_max_size
= DEFAULT_POLL_SIZE
;
707 DBG("poll set max size set to %u", poll_max_size
);
711 #endif /* !HAVE_EPOLL */
This page took 0.043295 seconds and 4 git commands to generate.