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.
24 #include <common/common.h>
27 * Maximum number of fd we can monitor.
29 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
30 * be used for the maximum size of the poll set. If this interface is not
31 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
32 * of the available low memory divided by the registration cost in bytes which
33 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
35 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
38 extern unsigned int poll_max_size
;
41 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
43 static inline void __lttng_poll_free(void *events
)
51 * epoll(7) implementation.
54 #include <sys/epoll.h>
57 /* See man epoll(7) for this define path */
58 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
61 /* Polling variables compatibility for epoll */
65 LPOLLRDNORM
= EPOLLRDNORM
,
66 LPOLLRDBAND
= EPOLLRDBAND
,
67 LPOLLWRNORM
= EPOLLWRNORM
,
68 LPOLLWRBAND
= EPOLLWRBAND
,
73 LPOLLRDHUP
= EPOLLRDHUP
,
74 /* Close on exec feature of epoll */
75 LTTNG_CLOEXEC
= EPOLL_CLOEXEC
,
78 struct compat_epoll_event
{
80 uint32_t nb_fd
; /* Current number of fd in events */
81 uint32_t alloc_size
; /* Size of events array */
82 uint32_t init_size
; /* Initial size of events array */
83 struct epoll_event
*events
;
85 #define lttng_poll_event compat_epoll_event
88 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
89 * being the index of the events array.
91 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
92 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
93 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
94 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
97 * Create the epoll set. No memory allocation is done here.
99 extern int compat_epoll_create(struct lttng_poll_event
*events
,
100 int size
, int flags
);
101 #define lttng_poll_create(events, size, flags) \
102 compat_epoll_create(events, size, flags)
105 * Wait on epoll set with the number of fd registered to the lttng_poll_event
106 * data structure (events).
108 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
);
109 #define lttng_poll_wait(events, timeout) \
110 compat_epoll_wait(events, timeout)
113 * Add a fd to the epoll set and resize the epoll_event structure if needed.
115 extern int compat_epoll_add(struct lttng_poll_event
*events
,
116 int fd
, uint32_t req_events
);
117 #define lttng_poll_add(events, fd, req_events) \
118 compat_epoll_add(events, fd, req_events)
121 * Remove a fd from the epoll set.
123 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
124 #define lttng_poll_del(events, fd) \
125 compat_epoll_del(events, fd)
128 * Set up the poll set limits variable poll_max_size
130 extern void compat_epoll_set_max_size(void);
131 #define lttng_poll_set_max_size() \
132 compat_epoll_set_max_size()
135 * This function memset with zero the structure since it can be reused at each
136 * round of a main loop. Being in a loop and using a non static number of fds,
137 * this function must be called to insure coherent events with associted fds.
139 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
141 if (events
&& events
->events
) {
142 memset(events
->events
, 0,
143 events
->nb_fd
* sizeof(struct epoll_event
));
148 * Clean the events structure of a lttng_poll_event. It's the caller
149 * responsability to free the lttng_poll_event memory.
151 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
156 ret
= close(events
->epfd
);
160 __lttng_poll_free((void *) events
->events
);
164 #else /* HAVE_EPOLL */
166 * Fallback on poll(2) API
169 /* Needed for some poll event values */
174 /* Needed for some poll event values */
183 /* Polling variables compatibility for poll */
187 LPOLLRDNORM
= POLLRDNORM
,
188 LPOLLRDBAND
= POLLRDBAND
,
189 LPOLLWRNORM
= POLLWRNORM
,
190 LPOLLWRBAND
= POLLWRBAND
,
193 LPOLLRDHUP
= POLLRDHUP
,
194 #elif (defined(__FreeBSD__) || defined(__CYGWIN__))
198 #error "Please add support for your OS."
199 #endif /* __linux__ */
201 LPOLLHUP
= POLLHUP
| POLLNVAL
,
202 /* Close on exec feature does not exist for poll(2) */
203 LTTNG_CLOEXEC
= 0xdead,
206 struct compat_poll_event_array
{
207 uint32_t nb_fd
; /* Current number of fd in events */
208 uint32_t alloc_size
; /* Size of events array */
209 /* Initial size of the pollset. We never shrink below that. */
211 struct pollfd
*events
;
214 struct compat_poll_event
{
216 * Modified by the wait action. Updated using current fields if the
217 * need_update flag is set.
219 struct compat_poll_event_array wait
;
221 * This is modified by add/del actions being the _current_ flow of
222 * execution before a poll wait is done.
224 struct compat_poll_event_array current
;
225 /* Indicate if wait.events needs to be realloc. */
227 /* Indicate if wait.events need to be updated from current. */
230 #define lttng_poll_event compat_poll_event
233 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
234 * being the index of the events array.
236 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->wait.events[i].fd
237 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->wait.events[i].revents
238 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->wait.nb_fd
239 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->wait.events_size
242 * Create a pollfd structure of size 'size'.
244 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
245 #define lttng_poll_create(events, size, flags) \
246 compat_poll_create(events, size)
249 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
252 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
);
253 #define lttng_poll_wait(events, timeout) \
254 compat_poll_wait(events, timeout)
257 * Add the fd to the pollfd structure. Resize if needed.
259 extern int compat_poll_add(struct lttng_poll_event
*events
,
260 int fd
, uint32_t req_events
);
261 #define lttng_poll_add(events, fd, req_events) \
262 compat_poll_add(events, fd, req_events)
265 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
266 * pollfd, data is copied from the old pollfd to the new and, finally, the old
269 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
270 #define lttng_poll_del(events, fd) \
271 compat_poll_del(events, fd)
274 * Set up the poll set limits variable poll_max_size
276 extern void compat_poll_set_max_size(void);
277 #define lttng_poll_set_max_size() \
278 compat_poll_set_max_size()
281 * No need to reset a pollfd structure for poll(2)
283 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
287 * Clean the events structure of a lttng_poll_event. It's the caller
288 * responsability to free the lttng_poll_event memory.
290 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
293 __lttng_poll_free((void *) events
->wait
.events
);
294 __lttng_poll_free((void *) events
->current
.events
);
298 #endif /* HAVE_EPOLL */
300 #endif /* _LTT_POLL_H */