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
)
49 * epoll(7) implementation.
52 #include <sys/epoll.h>
55 /* See man epoll(7) for this define path */
56 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
59 /* Polling variables compatibility for epoll */
63 LPOLLRDNORM
= EPOLLRDNORM
,
64 LPOLLRDBAND
= EPOLLRDBAND
,
65 LPOLLWRNORM
= EPOLLWRNORM
,
66 LPOLLWRBAND
= EPOLLWRBAND
,
71 LPOLLRDHUP
= EPOLLRDHUP
,
72 /* Close on exec feature of epoll */
73 LTTNG_CLOEXEC
= EPOLL_CLOEXEC
,
76 struct compat_epoll_event
{
78 uint32_t nb_fd
; /* Current number of fd in events */
79 uint32_t events_size
; /* Size of events array */
80 struct epoll_event
*events
;
82 #define lttng_poll_event compat_epoll_event
85 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
86 * being the index of the events array.
88 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
89 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
90 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
91 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
94 * Create the epoll set. No memory allocation is done here.
96 extern int compat_epoll_create(struct lttng_poll_event
*events
,
98 #define lttng_poll_create(events, size, flags) \
99 compat_epoll_create(events, size, flags)
102 * Wait on epoll set with the number of fd registered to the lttng_poll_event
103 * data structure (events).
105 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
);
106 #define lttng_poll_wait(events, timeout) \
107 compat_epoll_wait(events, timeout)
110 * Add a fd to the epoll set and resize the epoll_event structure if needed.
112 extern int compat_epoll_add(struct lttng_poll_event
*events
,
113 int fd
, uint32_t req_events
);
114 #define lttng_poll_add(events, fd, req_events) \
115 compat_epoll_add(events, fd, req_events)
118 * Remove a fd from the epoll set.
120 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
121 #define lttng_poll_del(events, fd) \
122 compat_epoll_del(events, fd)
125 * Set up the poll set limits variable poll_max_size
127 extern void compat_epoll_set_max_size(void);
128 #define lttng_poll_set_max_size() \
129 compat_epoll_set_max_size()
132 * This function memset with zero the structure since it can be reused at each
133 * round of a main loop. Being in a loop and using a non static number of fds,
134 * this function must be called to insure coherent events with associted fds.
136 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
138 if (events
&& events
->events
) {
139 memset(events
->events
, 0,
140 events
->nb_fd
* sizeof(struct epoll_event
));
145 * Clean the events structure of a lttng_poll_event. It's the caller
146 * responsability to free the lttng_poll_event memory.
148 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
153 ret
= close(events
->epfd
);
157 __lttng_poll_free((void *) events
->events
);
161 #else /* HAVE_EPOLL */
163 * Fallback on poll(2) API
166 /* Needed for some poll event values */
171 /* Needed for some poll event values */
180 /* Polling variables compatibility for poll */
184 LPOLLRDNORM
= POLLRDNORM
,
185 LPOLLRDBAND
= POLLRDBAND
,
186 LPOLLWRNORM
= POLLWRNORM
,
187 LPOLLWRBAND
= POLLWRBAND
,
190 LPOLLRDHUP
= POLLRDHUP
,
191 #elif defined(__FreeBSD__)
195 #error "Please add support for your OS."
196 #endif /* __linux__ */
198 LPOLLHUP
= POLLHUP
| POLLNVAL
,
199 /* Close on exec feature does not exist for poll(2) */
200 LTTNG_CLOEXEC
= 0xdead,
203 struct compat_poll_event
{
204 uint32_t nb_fd
; /* Current number of fd in events */
205 uint32_t events_size
; /* Size of events array */
206 struct pollfd
*events
;
208 #define lttng_poll_event compat_poll_event
211 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
212 * being the index of the events array.
214 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].fd
215 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].revents
216 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
217 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
220 * Create a pollfd structure of size 'size'.
222 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
223 #define lttng_poll_create(events, size, flags) \
224 compat_poll_create(events, size)
227 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
230 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
);
231 #define lttng_poll_wait(events, timeout) \
232 compat_poll_wait(events, timeout)
235 * Add the fd to the pollfd structure. Resize if needed.
237 extern int compat_poll_add(struct lttng_poll_event
*events
,
238 int fd
, uint32_t req_events
);
239 #define lttng_poll_add(events, fd, req_events) \
240 compat_poll_add(events, fd, req_events)
243 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
244 * pollfd, data is copied from the old pollfd to the new and, finally, the old
247 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
248 #define lttng_poll_del(events, fd) \
249 compat_poll_del(events, fd)
252 * Set up the poll set limits variable poll_max_size
254 extern void compat_poll_set_max_size(void);
255 #define lttng_poll_set_max_size() \
256 compat_poll_set_max_size()
259 * No need to reset a pollfd structure for poll(2)
261 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
265 * Clean the events structure of a lttng_poll_event. It's the caller
266 * responsability to free the lttng_poll_event memory.
268 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
271 __lttng_poll_free((void *) events
->events
);
275 #endif /* HAVE_EPOLL */
277 #endif /* _LTT_POLL_H */
This page took 0.038125 seconds and 5 git commands to generate.