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 /* See man epoll(7) for this define path */
57 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
60 /* Polling variables compatibility for epoll */
64 LPOLLRDNORM
= EPOLLRDNORM
,
65 LPOLLRDBAND
= EPOLLRDBAND
,
66 LPOLLWRNORM
= EPOLLWRNORM
,
67 LPOLLWRBAND
= EPOLLWRBAND
,
72 LPOLLRDHUP
= EPOLLRDHUP
,
73 /* Close on exec feature of epoll */
74 LTTNG_CLOEXEC
= EPOLL_CLOEXEC
,
77 struct compat_epoll_event
{
79 uint32_t nb_fd
; /* Current number of fd in events */
80 uint32_t alloc_size
; /* Size of events array */
81 uint32_t init_size
; /* Initial size of events array */
82 struct epoll_event
*events
;
84 #define lttng_poll_event compat_epoll_event
86 static inline int __lttng_epoll_get_prev_fd(struct lttng_poll_event
*events
,
87 int index
, uint32_t nb_fd
)
90 assert(index
!= nb_fd
);
92 if (index
== 0 || nb_fd
== 0) {
95 return events
->events
[index
- 1].data
.fd
;
100 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
101 * being the index of the events array.
103 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
104 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
105 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
106 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
107 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
108 __lttng_epoll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
111 * Create the epoll set. No memory allocation is done here.
113 extern int compat_epoll_create(struct lttng_poll_event
*events
,
114 int size
, int flags
);
115 #define lttng_poll_create(events, size, flags) \
116 compat_epoll_create(events, size, flags)
119 * Wait on epoll set with the number of fd registered to the lttng_poll_event
120 * data structure (events).
122 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
);
123 #define lttng_poll_wait(events, timeout) \
124 compat_epoll_wait(events, timeout)
127 * Add a fd to the epoll set and resize the epoll_event structure if needed.
129 extern int compat_epoll_add(struct lttng_poll_event
*events
,
130 int fd
, uint32_t req_events
);
131 #define lttng_poll_add(events, fd, req_events) \
132 compat_epoll_add(events, fd, req_events)
135 * Remove a fd from the epoll set.
137 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
138 #define lttng_poll_del(events, fd) \
139 compat_epoll_del(events, fd)
142 * Set up the poll set limits variable poll_max_size
144 extern int compat_epoll_set_max_size(void);
145 #define lttng_poll_set_max_size() \
146 compat_epoll_set_max_size()
149 * This function memset with zero the structure since it can be reused at each
150 * round of a main loop. Being in a loop and using a non static number of fds,
151 * this function must be called to insure coherent events with associted fds.
153 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
155 if (events
&& events
->events
) {
156 memset(events
->events
, 0,
157 events
->nb_fd
* sizeof(struct epoll_event
));
162 * Initialize an already allocated poll event data structure. For epoll(), the
163 * epfd is set to -1 to indicate that it's not usable.
165 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
167 memset(events
, 0, sizeof(struct lttng_poll_event
));
168 /* Set fd to -1 so if clean before created, we don't close 0. */
173 * Clean the events structure of a lttng_poll_event. It's the caller
174 * responsability to free the lttng_poll_event memory.
176 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
184 if (events
->epfd
>= 0) {
185 ret
= close(events
->epfd
);
191 __lttng_poll_free((void *) events
->events
);
194 #else /* HAVE_EPOLL */
196 * Fallback on poll(2) API
199 /* Needed for some poll event values */
204 /* Needed for some poll event values */
213 /* Polling variables compatibility for poll */
217 LPOLLRDNORM
= POLLRDNORM
,
218 LPOLLRDBAND
= POLLRDBAND
,
219 LPOLLWRNORM
= POLLWRNORM
,
220 LPOLLWRBAND
= POLLWRBAND
,
223 LPOLLRDHUP
= POLLRDHUP
,
224 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
228 #error "Please add support for your OS."
229 #endif /* __linux__ */
231 LPOLLHUP
= POLLHUP
| POLLNVAL
,
232 /* Close on exec feature does not exist for poll(2) */
233 LTTNG_CLOEXEC
= 0xdead,
236 struct compat_poll_event_array
{
237 uint32_t nb_fd
; /* Current number of fd in events */
238 uint32_t alloc_size
; /* Size of events array */
239 /* Initial size of the pollset. We never shrink below that. */
241 struct pollfd
*events
;
244 struct compat_poll_event
{
246 * Modified by the wait action. Updated using current fields if the
247 * need_update flag is set.
249 struct compat_poll_event_array wait
;
251 * This is modified by add/del actions being the _current_ flow of
252 * execution before a poll wait is done.
254 struct compat_poll_event_array current
;
256 /* Indicate if wait.events need to be updated from current. */
259 #define lttng_poll_event compat_poll_event
261 static inline int __lttng_poll_get_prev_fd(struct lttng_poll_event
*events
,
262 int index
, uint32_t nb_fd
)
265 assert(index
!= nb_fd
);
267 if (index
== 0 || nb_fd
== 0) {
270 return events
->current
.events
[index
- 1].fd
;
275 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
276 * being the index of the events array.
278 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
279 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
280 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->wait.nb_fd
281 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
282 #define LTTNG_POLL_GET_PREV_FD(e, i, nb_fd) \
283 __lttng_poll_get_prev_fd(LTTNG_REF(e), i, nb_fd)
286 * Create a pollfd structure of size 'size'.
288 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
289 #define lttng_poll_create(events, size, flags) \
290 compat_poll_create(events, size)
293 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
296 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
);
297 #define lttng_poll_wait(events, timeout) \
298 compat_poll_wait(events, timeout)
301 * Add the fd to the pollfd structure. Resize if needed.
303 extern int compat_poll_add(struct lttng_poll_event
*events
,
304 int fd
, uint32_t req_events
);
305 #define lttng_poll_add(events, fd, req_events) \
306 compat_poll_add(events, fd, req_events)
309 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
310 * pollfd, data is copied from the old pollfd to the new and, finally, the old
313 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
314 #define lttng_poll_del(events, fd) \
315 compat_poll_del(events, fd)
318 * Set up the poll set limits variable poll_max_size
320 extern int compat_poll_set_max_size(void);
321 #define lttng_poll_set_max_size() \
322 compat_poll_set_max_size()
325 * No need to reset a pollfd structure for poll(2)
327 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
331 * Initialize an already allocated poll event data structure.
333 static inline void lttng_poll_init(struct lttng_poll_event
*events
)
335 memset(events
, 0, sizeof(struct lttng_poll_event
));
339 * Clean the events structure of a lttng_poll_event. It's the caller
340 * responsability to free the lttng_poll_event memory.
342 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
345 __lttng_poll_free((void *) events
->wait
.events
);
346 __lttng_poll_free((void *) events
->current
.events
);
350 #endif /* HAVE_EPOLL */
352 #endif /* _LTT_POLL_H */