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.
25 #include <common/common.h>
28 * Maximum number of fd we can monitor.
30 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
31 * be used for the maximum size of the poll set. If this interface is not
32 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
33 * of the available low memory divided by the registration cost in bytes which
34 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
36 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
39 extern unsigned int poll_max_size
;
42 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
44 static inline void __lttng_poll_free(void *events
)
50 * epoll(7) implementation.
53 #include <sys/epoll.h>
56 #include <common/compat/fcntl.h>
58 /* See man epoll(7) for this define path */
59 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
62 /* Polling variables compatibility for epoll */
66 LPOLLRDNORM
= EPOLLRDNORM
,
67 LPOLLRDBAND
= EPOLLRDBAND
,
68 LPOLLWRNORM
= EPOLLWRNORM
,
69 LPOLLWRBAND
= EPOLLWRBAND
,
74 LPOLLRDHUP
= EPOLLRDHUP
,
75 /* Close on exec feature of epoll */
76 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
77 LTTNG_CLOEXEC
= EPOLL_CLOEXEC
,
80 * EPOLL_CLOEXEC was added in glibc 2.8 (usually used in conjunction with
81 * epoll_create1(..)), but since neither EPOLL_CLOEXEC exists nor
82 * epoll_create1(..), we set it to FD_CLOEXEC so that we can pass it
83 * directly to fcntl(..) instead.
85 LTTNG_CLOEXEC
= FD_CLOEXEC
,
89 struct compat_epoll_event
{
91 uint32_t nb_fd
; /* Current number of fd in events */
92 uint32_t alloc_size
; /* Size of events array */
93 uint32_t init_size
; /* Initial size of events array */
94 struct epoll_event
*events
;
96 #define lttng_poll_event compat_epoll_event
98 static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event
*events
,
99 int index
, uint32_t nb_fd
)
102 assert(index
!= nb_fd
);
104 if (index
== 0 || nb_fd
== 0) {
107 return events
->events
[index
- 1].data
.fd
;
112 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
113 * being the index of the events array.
115 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
116 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
117 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
118 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
119 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
120 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
122 /* Create the epoll set. */
123 extern int compat_epoll_create(struct lttng_poll_event
*events
,
124 int size
, int flags
);
125 #define lttng_poll_create(events, size, flags) \
126 compat_epoll_create(events, size, flags)
128 #if defined(HAVE_EPOLL_CREATE1) && defined(EPOLL_CLOEXEC)
129 static inline int compat_glibc_epoll_create(int size
__attribute__((unused
)),
132 return epoll_create1(flags
);
135 static inline int compat_glibc_epoll_create(int size
, int flags
)
138 * epoll_create1 was added in glibc 2.9, but unfortunatly reverting to
139 * epoll_create(..) also means that we lose the possibility to
140 * directly set the EPOLL_CLOEXEC, so try and do it anyway but through
143 int efd
= epoll_create(size
);
144 assert(fcntl(efd
, F_SETFD
, flags
) != -1);
150 * Wait on epoll set with the number of fd registered to the lttng_poll_event
151 * data structure (events).
153 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
);
154 #define lttng_poll_wait(events, timeout) \
155 compat_epoll_wait(events, timeout)
158 * Add a fd to the epoll set and resize the epoll_event structure if needed.
160 extern int compat_epoll_add(struct lttng_poll_event
*events
,
161 int fd
, uint32_t req_events
);
162 #define lttng_poll_add(events, fd, req_events) \
163 compat_epoll_add(events, fd, req_events)
166 * Remove a fd from the epoll set.
168 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
169 #define lttng_poll_del(events, fd) \
170 compat_epoll_del(events, fd)
173 * Modify an fd's events in the epoll set.
175 extern int compat_epoll_mod(struct lttng_poll_event
*events
,
176 int fd
, uint32_t req_events
);
177 #define lttng_poll_mod(events, fd, req_events) \
178 compat_epoll_mod(events, fd, req_events)
181 * Set up the poll set limits variable poll_max_size
183 extern int compat_epoll_set_max_size(void);
184 #define lttng_poll_set_max_size() \
185 compat_epoll_set_max_size()
188 * This function memset with zero the structure since it can be reused at each
189 * round of a main loop. Being in a loop and using a non static number of fds,
190 * this function must be called to insure coherent events with associted fds.
192 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
194 if (events
&& events
->events
) {
195 memset(events
->events
, 0,
196 events
->nb_fd
* sizeof(struct epoll_event
));
201 * Initialize an already allocated poll event data structure. For epoll(), the
202 * epfd is set to -1 to indicate that it's not usable.
204 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
206 memset(events
, 0, sizeof(struct lttng_poll_event
));
207 /* Set fd to -1 so if clean before created, we don't close 0. */
212 * Clean the events structure of a lttng_poll_event. It's the caller
213 * responsability to free the lttng_poll_event memory.
215 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
223 if (events
->epfd
>= 0) {
224 ret
= close(events
->epfd
);
230 __lttng_poll_free((void *) events
->events
);
233 #else /* HAVE_EPOLL */
235 * Fallback on poll(2) API
238 /* Needed for some poll event values */
243 /* Needed for some poll event values */
252 /* Polling variables compatibility for poll */
256 LPOLLRDNORM
= POLLRDNORM
,
257 LPOLLRDBAND
= POLLRDBAND
,
258 LPOLLWRNORM
= POLLWRNORM
,
259 LPOLLWRBAND
= POLLWRBAND
,
262 LPOLLRDHUP
= POLLRDHUP
,
263 #elif (defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__sun__) || defined(__APPLE__))
267 #error "Please add support for your OS."
268 #endif /* __linux__ */
270 LPOLLHUP
= POLLHUP
| POLLNVAL
,
271 /* Close on exec feature does not exist for poll(2) */
272 LTTNG_CLOEXEC
= 0xdead,
275 struct compat_poll_event_array
{
276 uint32_t nb_fd
; /* Current number of fd in events */
277 uint32_t alloc_size
; /* Size of events array */
278 /* Initial size of the pollset. We never shrink below that. */
280 struct pollfd
*events
;
283 struct compat_poll_event
{
285 * Modified by the wait action. Updated using current fields if the
286 * need_update flag is set.
288 struct compat_poll_event_array wait
;
290 * This is modified by add/del actions being the _current_ flow of
291 * execution before a poll wait is done.
293 struct compat_poll_event_array current
;
295 /* Indicate if wait.events need to be updated from current. */
298 #define lttng_poll_event compat_poll_event
300 static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event
*events
,
301 int index
, uint32_t nb_fd
)
304 assert(index
!= nb_fd
);
306 if (index
== 0 || nb_fd
== 0) {
309 return events
->current
.events
[index
- 1].fd
;
314 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
315 * being the index of the events array.
316 * LTTNG_POLL_GETNB is always used after lttng_poll_wait, thus we can use the
317 * current list for test compatibility purposes.
319 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
320 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
321 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->current.nb_fd
322 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
323 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
324 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
327 * Create a pollfd structure of size 'size'.
329 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
330 #define lttng_poll_create(events, size, flags) \
331 compat_poll_create(events, size)
334 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
337 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
);
338 #define lttng_poll_wait(events, timeout) \
339 compat_poll_wait(events, timeout)
342 * Add the fd to the pollfd structure. Resize if needed.
344 extern int compat_poll_add(struct lttng_poll_event
*events
,
345 int fd
, uint32_t req_events
);
346 #define lttng_poll_add(events, fd, req_events) \
347 compat_poll_add(events, fd, req_events)
350 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
351 * pollfd, data is copied from the old pollfd to the new and, finally, the old
354 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
355 #define lttng_poll_del(events, fd) \
356 compat_poll_del(events, fd)
359 * Modify an fd's events in the poll set.
361 extern int compat_poll_mod(struct lttng_poll_event
*events
,
362 int fd
, uint32_t req_events
);
363 #define lttng_poll_mod(events, fd, req_events) \
364 compat_poll_mod(events, fd, req_events)
367 * Set up the poll set limits variable poll_max_size
369 extern int compat_poll_set_max_size(void);
370 #define lttng_poll_set_max_size() \
371 compat_poll_set_max_size()
374 * No need to reset a pollfd structure for poll(2)
376 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
380 * Initialize an already allocated poll event data structure.
382 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
384 memset(events
, 0, sizeof(struct lttng_poll_event
));
388 * Clean the events structure of a lttng_poll_event. It's the caller
389 * responsability to free the lttng_poll_event memory.
391 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
394 __lttng_poll_free((void *) events
->wait
.events
);
395 __lttng_poll_free((void *) events
->current
.events
);
399 #endif /* HAVE_EPOLL */
401 #endif /* _LTT_POLL_H */