2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
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 with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, 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>
54 /* See man epoll(7) for this define path */
55 #define COMPAT_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
58 /* Polling variables compatibility for epoll */
62 LPOLLRDNORM
= EPOLLRDNORM
,
63 LPOLLRDBAND
= EPOLLRDBAND
,
64 LPOLLWRNORM
= EPOLLWRNORM
,
65 LPOLLWRBAND
= EPOLLWRBAND
,
70 LPOLLRDHUP
= EPOLLRDHUP
,
71 /* Close on exec feature of epoll */
72 LTTNG_CLOEXEC
= EPOLL_CLOEXEC
,
75 struct compat_epoll_event
{
77 uint32_t nb_fd
; /* Current number of fd in events */
78 uint32_t events_size
; /* Size of events array */
79 struct epoll_event
*events
;
81 #define lttng_poll_event compat_epoll_event
84 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
85 * being the index of the events array.
87 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
88 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
89 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
90 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
93 * Create the epoll set. No memory allocation is done here.
95 extern int compat_epoll_create(struct lttng_poll_event
*events
,
97 #define lttng_poll_create(events, size, flags) \
98 compat_epoll_create(events, size, flags)
101 * Wait on epoll set with the number of fd registered to the lttng_poll_event
102 * data structure (events).
104 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
);
105 #define lttng_poll_wait(events, timeout) \
106 compat_epoll_wait(events, timeout)
109 * Add a fd to the epoll set and resize the epoll_event structure if needed.
111 extern int compat_epoll_add(struct lttng_poll_event
*events
,
112 int fd
, uint32_t req_events
);
113 #define lttng_poll_add(events, fd, req_events) \
114 compat_epoll_add(events, fd, req_events)
117 * Remove a fd from the epoll set.
119 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
120 #define lttng_poll_del(events, fd) \
121 compat_epoll_del(events, fd)
124 * Set up the poll set limits variable poll_max_size
126 extern void compat_epoll_set_max_size(void);
127 #define lttng_poll_set_max_size() \
128 compat_epoll_set_max_size()
131 * This function memset with zero the structure since it can be reused at each
132 * round of a main loop. Being in a loop and using a non static number of fds,
133 * this function must be called to insure coherent events with associted fds.
135 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
137 if (events
&& events
->events
) {
138 memset(events
->events
, 0,
139 events
->nb_fd
* sizeof(struct epoll_event
));
144 * Clean the events structure of a lttng_poll_event. It's the caller
145 * responsability to free the lttng_poll_event memory.
147 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
151 __lttng_poll_free((void *) events
->events
);
155 #else /* HAVE_EPOLL */
157 * Fallback on poll(2) API
160 /* Needed for some poll event values */
165 /* Needed for some poll event values */
174 /* Polling variables compatibility for poll */
178 LPOLLRDNORM
= POLLRDNORM
,
179 LPOLLRDBAND
= POLLRDBAND
,
180 LPOLLWRNORM
= POLLWRNORM
,
181 LPOLLWRBAND
= POLLWRBAND
,
184 LPOLLHUP
= POLLHUP
| POLLNVAL
,
185 LPOLLRDHUP
= POLLRDHUP
,
186 /* Close on exec feature does not exist for poll(2) */
187 LTTNG_CLOEXEC
= 0xdead,
190 struct compat_poll_event
{
191 uint32_t nb_fd
; /* Current number of fd in events */
192 uint32_t events_size
; /* Size of events array */
193 struct pollfd
*events
;
195 #define lttng_poll_event compat_poll_event
198 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
199 * being the index of the events array.
201 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].fd
202 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].revents
203 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
204 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
207 * Create a pollfd structure of size 'size'.
209 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
210 #define lttng_poll_create(events, size, flags) \
211 compat_poll_create(events, size)
214 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
217 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
);
218 #define lttng_poll_wait(events, timeout) \
219 compat_poll_wait(events, timeout)
222 * Add the fd to the pollfd structure. Resize if needed.
224 extern int compat_poll_add(struct lttng_poll_event
*events
,
225 int fd
, uint32_t req_events
);
226 #define lttng_poll_add(events, fd, req_events) \
227 compat_poll_add(events, fd, req_events)
230 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
231 * pollfd, data is copied from the old pollfd to the new and, finally, the old
234 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
235 #define lttng_poll_del(events, fd) \
236 compat_poll_del(events, fd)
239 * Set up the poll set limits variable poll_max_size
241 extern void compat_poll_set_max_size(void);
242 #define lttng_poll_set_max_size() \
243 compat_poll_set_max_size()
246 * No need to reset a pollfd structure for poll(2)
248 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
252 * Clean the events structure of a lttng_poll_event. It's the caller
253 * responsability to free the lttng_poll_event memory.
255 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
258 __lttng_poll_free((void *) events
->events
);
262 #endif /* HAVE_EPOLL */
264 #endif /* _LTT_POLL_H */
This page took 0.035096 seconds and 5 git commands to generate.