0c40c4511d266731d7a556daab9aebb172de9982
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2019 Yannick Lamarre <ylamarre@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
13 #include <common/defaults.h>
14 #include <common/error.h>
15 #include <common/macros.h>
16 #include <common/utils.h>
24 #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
,
48 struct epoll_event
*ptr
;
52 ptr
= (epoll_event
*) realloc(events
->events
, new_size
* sizeof(*ptr
));
54 PERROR("realloc epoll add");
57 if (new_size
> events
->alloc_size
) {
58 /* Zero newly allocated memory */
59 memset(ptr
+ events
->alloc_size
, 0,
60 (new_size
- events
->alloc_size
) * sizeof(*ptr
));
63 events
->alloc_size
= new_size
;
72 * Create epoll set and allocate returned events structure.
74 int compat_epoll_create(struct lttng_poll_event
*events
, int size
, int flags
)
78 if (events
== NULL
|| size
<= 0) {
83 if (lttng_poll_set_max_size()) {
88 /* Don't bust the limit here */
89 if (size
> poll_max_size
) {
93 ret
= compat_glibc_epoll_create(size
, flags
);
95 /* At this point, every error is fatal */
96 PERROR("epoll_create1");
102 /* This *must* be freed by using lttng_poll_free() */
103 events
->events
= (epoll_event
*) zmalloc(size
* sizeof(struct epoll_event
));
104 if (events
->events
== NULL
) {
105 PERROR("zmalloc epoll set");
109 events
->alloc_size
= events
->init_size
= size
;
115 ret
= close(events
->epfd
);
124 * Add a fd to the epoll set with requesting events.
126 int compat_epoll_add(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
129 struct epoll_event ev
;
131 if (events
== NULL
|| events
->events
== NULL
|| fd
< 0) {
132 ERR("Bad compat epoll add arguments");
137 * Zero struct epoll_event to ensure all representations of its
140 memset(&ev
, 0, sizeof(ev
));
141 ev
.events
= req_events
;
144 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_ADD
, fd
, &ev
);
148 /* If exist, it's OK. */
152 /* Print PERROR and goto end not failing. Show must go on. */
153 PERROR("epoll_ctl ADD");
156 PERROR("epoll_ctl ADD fatal");
171 * 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.
207 int compat_epoll_mod(struct lttng_poll_event
*events
, int fd
, uint32_t req_events
)
210 struct epoll_event ev
;
212 if (events
== NULL
|| fd
< 0 || events
->nb_fd
== 0) {
217 * Zero struct epoll_event to ensure all representations of its
220 memset(&ev
, 0, sizeof(ev
));
221 ev
.events
= req_events
;
224 ret
= epoll_ctl(events
->epfd
, EPOLL_CTL_MOD
, fd
, &ev
);
229 /* Print PERROR and goto end not failing. Show must go on. */
230 PERROR("epoll_ctl MOD");
233 PERROR("epoll_ctl MOD fatal");
246 * Wait on epoll set. This is a blocking call of timeout value.
248 int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
,
254 if (events
== NULL
|| events
->events
== NULL
) {
255 ERR("Wrong arguments in compat_epoll_wait");
259 if (events
->nb_fd
== 0) {
265 * Resize if needed before waiting. We could either expand the array or
266 * shrink it down. It's important to note that after this step, we are
267 * ensured that the events argument of the epoll_wait call will be large
268 * enough to hold every possible returned events.
270 new_size
= 1U << utils_get_count_order_u32(events
->nb_fd
);
271 if (new_size
!= events
->alloc_size
&& new_size
>= events
->init_size
) {
272 ret
= resize_poll_event(events
, new_size
);
274 /* ENOMEM problem at this point. */
280 ret
= epoll_wait(events
->epfd
, events
->events
, events
->nb_fd
, timeout
);
281 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
283 if (errno
!= EINTR
) {
284 PERROR("epoll_wait");
290 * Since the returned events are set sequentially in the "events" structure
291 * we only need to return the epoll_wait value and iterate over it.
300 * Setup poll set maximum size.
302 int compat_epoll_set_max_size(void)
304 int ret
, fd
, retval
= 0;
308 fd
= open(COMPAT_EPOLL_PROC_PATH
, O_RDONLY
);
311 * Failing on opening [1] is not an error per see. [1] was
312 * introduced in Linux 2.6.28 but epoll is available since
313 * 2.5.44. Hence, goto end and set a default value without
314 * setting an error return value.
316 * [1] /proc/sys/fs/epoll/max_user_watches
322 size_ret
= lttng_read(fd
, buf
, sizeof(buf
));
324 * Allow reading a file smaller than buf, but keep space for
327 if (size_ret
< 0 || size_ret
>= sizeof(buf
)) {
328 PERROR("read set max size");
332 buf
[size_ret
] = '\0';
333 poll_max_size
= atoi(buf
);
340 if (!poll_max_size
) {
341 poll_max_size
= DEFAULT_POLL_SIZE
;
343 DBG("epoll set max size is %d", poll_max_size
);
347 #else /* HAVE_EPOLL */
349 #include <sys/resource.h>
350 #include <sys/time.h>
353 * Maximum number of fd we can monitor.
355 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
358 static unsigned int poll_max_size
;
361 * Resize the epoll events structure of the new size.
363 * Return 0 on success or else -1 with the current events pointer untouched.
365 static int resize_poll_event(struct compat_poll_event_array
*array
,
372 /* Refuse to resize the array more than the max size. */
373 if (new_size
> poll_max_size
) {
377 ptr
= (struct pollfd
*) realloc(array
->events
, new_size
* sizeof(*ptr
));
379 PERROR("realloc epoll add");
382 if (new_size
> array
->alloc_size
) {
383 /* Zero newly allocated memory */
384 memset(ptr
+ array
->alloc_size
, 0,
385 (new_size
- array
->alloc_size
) * sizeof(*ptr
));
388 array
->alloc_size
= new_size
;
397 * Update events with the current events object.
399 static int update_current_events(struct lttng_poll_event
*events
)
402 struct compat_poll_event_array
*current
, *wait
;
404 LTTNG_ASSERT(events
);
406 current
= &events
->current
;
407 wait
= &events
->wait
;
409 wait
->nb_fd
= current
->nb_fd
;
410 if (current
->alloc_size
!= wait
->alloc_size
) {
411 ret
= resize_poll_event(wait
, current
->alloc_size
);
416 memcpy(wait
->events
, current
->events
,
417 current
->nb_fd
* sizeof(*current
->events
));
419 /* Update is done. */
420 events
->need_update
= 0;
429 * Create pollfd data structure.
431 int compat_poll_create(struct lttng_poll_event
*events
, int size
)
433 struct compat_poll_event_array
*current
, *wait
;
435 if (events
== NULL
|| size
<= 0) {
436 ERR("Wrong arguments for poll create");
440 if (!poll_max_size
) {
441 if (lttng_poll_set_max_size()) {
446 /* Don't bust the limit here */
447 if (size
> poll_max_size
) {
448 size
= poll_max_size
;
451 /* Reset everything before begining the allocation. */
452 memset(events
, 0, sizeof(struct lttng_poll_event
));
454 current
= &events
->current
;
455 wait
= &events
->wait
;
457 /* This *must* be freed by using lttng_poll_free() */
458 wait
->events
= (struct pollfd
*) zmalloc(size
* sizeof(struct pollfd
));
459 if (wait
->events
== NULL
) {
460 PERROR("zmalloc struct pollfd");
464 wait
->alloc_size
= wait
->init_size
= size
;
466 current
->events
= (struct pollfd
*) zmalloc(size
* sizeof(struct pollfd
));
467 if (current
->events
== NULL
) {
468 PERROR("zmalloc struct current pollfd");
472 current
->alloc_size
= current
->init_size
= size
;
481 * Add fd to pollfd data structure with requested events.
483 int compat_poll_add(struct lttng_poll_event
*events
, int fd
,
486 int new_size
, ret
, i
;
487 struct compat_poll_event_array
*current
;
489 if (events
== NULL
|| events
->current
.events
== NULL
|| fd
< 0) {
490 ERR("Bad compat poll add arguments");
494 current
= &events
->current
;
496 /* Check if fd we are trying to add is already there. */
497 for (i
= 0; i
< current
->nb_fd
; i
++) {
498 if (current
->events
[i
].fd
== fd
) {
504 /* Resize array if needed. */
505 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
+ 1);
506 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
) {
507 ret
= resize_poll_event(current
, new_size
);
513 current
->events
[current
->nb_fd
].fd
= fd
;
514 current
->events
[current
->nb_fd
].events
= req_events
;
516 events
->need_update
= 1;
518 DBG("fd %d of %d added to pollfd", fd
, current
->nb_fd
);
527 * Modify an fd's events..
529 int compat_poll_mod(struct lttng_poll_event
*events
, int fd
,
533 struct compat_poll_event_array
*current
;
535 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
536 events
->current
.events
== NULL
|| fd
< 0) {
537 ERR("Bad compat poll mod arguments");
541 current
= &events
->current
;
543 for (i
= 0; i
< current
->nb_fd
; i
++) {
544 if (current
->events
[i
].fd
== fd
) {
545 current
->events
[i
].events
= req_events
;
546 events
->need_update
= 1;
552 * The epoll flavor doesn't flag modifying a non-included FD as an
563 * Remove a fd from the pollfd structure.
565 int compat_poll_del(struct lttng_poll_event
*events
, int fd
)
567 int i
, count
= 0, ret
;
569 struct compat_poll_event_array
*current
;
571 if (events
== NULL
|| events
->current
.nb_fd
== 0 ||
572 events
->current
.events
== NULL
|| fd
< 0) {
576 /* Ease our life a bit. */
577 current
= &events
->current
;
579 for (i
= 0; i
< current
->nb_fd
; i
++) {
580 /* Don't put back the fd we want to delete */
581 if (current
->events
[i
].fd
!= fd
) {
582 current
->events
[count
].fd
= current
->events
[i
].fd
;
583 current
->events
[count
].events
= current
->events
[i
].events
;
588 /* The fd was not in our set, return no error as with epoll. */
589 if (current
->nb_fd
== count
) {
593 /* No fd duplicate should be ever added into array. */
594 LTTNG_ASSERT(current
->nb_fd
- 1 == count
);
595 current
->nb_fd
= count
;
597 /* Resize array if needed. */
598 new_size
= 1U << utils_get_count_order_u32(current
->nb_fd
);
599 if (new_size
!= current
->alloc_size
&& new_size
>= current
->init_size
600 && current
->nb_fd
!= 0) {
601 ret
= resize_poll_event(current
, new_size
);
607 events
->need_update
= 1;
617 * Wait on poll() with timeout. Blocking call.
619 int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
,
622 int ret
, active_fd_count
;
623 size_t pos
= 0, consecutive_entries
= 0, non_idle_pos
;
625 if (events
== NULL
|| events
->current
.events
== NULL
) {
626 ERR("poll wait arguments error");
630 if (events
->current
.nb_fd
== 0) {
631 /* Return an invalid error to be consistent with epoll. */
633 events
->wait
.nb_fd
= 0;
637 if (events
->need_update
) {
638 ret
= update_current_events(events
);
646 ret
= poll(events
->wait
.events
, events
->wait
.nb_fd
, timeout
);
647 } while (!interruptible
&& ret
== -1 && errno
== EINTR
);
649 if (errno
!= EINTR
) {
655 active_fd_count
= ret
;
658 * Move all active pollfd structs to the beginning of the
659 * array to emulate compat-epoll behaviour.
661 if (active_fd_count
== events
->wait
.nb_fd
) {
665 while (consecutive_entries
!= active_fd_count
) {
666 struct pollfd
*current
= &events
->wait
.events
[pos
];
667 struct pollfd idle_entry
;
669 if (current
->revents
!= 0) {
670 consecutive_entries
++;
677 /* Look for next non-idle entry. */
678 while (events
->wait
.events
[++non_idle_pos
].revents
== 0);
680 /* Swap idle and non-idle entries. */
681 idle_entry
= *current
;
682 *current
= events
->wait
.events
[non_idle_pos
];
683 events
->wait
.events
[non_idle_pos
] = idle_entry
;
685 consecutive_entries
++;
696 * Setup poll set maximum size.
698 int compat_poll_set_max_size(void)
703 ret
= getrlimit(RLIMIT_NOFILE
, &lim
);
705 PERROR("getrlimit poll RLIMIT_NOFILE");
710 poll_max_size
= lim
.rlim_cur
;
712 if (poll_max_size
== 0) {
713 poll_max_size
= DEFAULT_POLL_SIZE
;
715 DBG("poll set max size set to %u", poll_max_size
);
719 #endif /* !HAVE_EPOLL */
This page took 0.045797 seconds and 4 git commands to generate.