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 <lttng-share.h>
27 * Value taken from the hard limit allowed by the kernel when using setrlimit
28 * with RLIMIT_NOFILE on an Intel i7 CPU and Linux 3.0.3.
30 #define LTTNG_POLL_DEFAULT_SIZE 65535
33 * Maximum number of fd we can monitor.
35 * For epoll(7), /proc/sys/fs/epoll/max_user_watches (since Linux 2.6.28) will
36 * be used for the maximum size of the poll set. If this interface is not
37 * available, according to the manpage, the max_user_watches value is 1/25 (4%)
38 * of the available low memory divided by the registration cost in bytes which
39 * is 90 bytes on a 32-bit kernel and 160 bytes on a 64-bit kernel.
41 * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by
44 extern unsigned int poll_max_size
;
47 * Used by lttng_poll_clean to free the events structure in a lttng_poll_event.
49 static inline void __lttng_poll_free(void *events
)
55 * epoll(7) implementation.
58 #include <sys/epoll.h>
60 /* See man epoll(7) for this define path */
61 #define LTTNG_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches"
64 /* Polling variables compatibility for epoll */
68 LPOLLRDNORM
= EPOLLRDNORM
,
69 LPOLLRDBAND
= EPOLLRDBAND
,
70 LPOLLWRNORM
= EPOLLWRNORM
,
71 LPOLLWRBAND
= EPOLLWRBAND
,
76 LPOLLRDHUP
= EPOLLRDHUP
,
77 /* Close on exec feature of epoll */
78 LTTNG_CLOEXEC
= EPOLL_CLOEXEC
,
81 struct compat_epoll_event
{
83 uint32_t nb_fd
; /* Current number of fd in events */
84 uint32_t events_size
; /* Size of events array */
85 struct epoll_event
*events
;
87 #define lttng_poll_event compat_epoll_event
90 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
91 * being the index of the events array.
93 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].data.fd
94 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].events
95 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
96 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
99 * Create the epoll set. No memory allocation is done here.
101 extern int compat_epoll_create(struct lttng_poll_event
*events
,
102 int size
, int flags
);
103 #define lttng_poll_create(events, size, flags) \
104 compat_epoll_create(events, size, flags);
107 * Wait on epoll set with the number of fd registered to the lttng_poll_event
108 * data structure (events).
110 extern int compat_epoll_wait(struct lttng_poll_event
*events
, int timeout
);
111 #define lttng_poll_wait(events, timeout) \
112 compat_epoll_wait(events, timeout);
115 * Add a fd to the epoll set and resize the epoll_event structure if needed.
117 extern int compat_epoll_add(struct lttng_poll_event
*events
,
118 int fd
, uint32_t req_events
);
119 #define lttng_poll_add(events, fd, req_events) \
120 compat_epoll_add(events, fd, req_events);
123 * Remove a fd from the epoll set.
125 extern int compat_epoll_del(struct lttng_poll_event
*events
, int fd
);
126 #define lttng_poll_del(events, fd) \
127 compat_epoll_del(events, fd);
130 * Set up the poll set limits variable poll_max_size
132 extern void compat_epoll_set_max_size(void);
133 #define lttng_poll_set_max_size(void) \
134 compat_epoll_set_max_size(void);
137 * This function memset with zero the structure since it can be reused at each
138 * round of a main loop. Being in a loop and using a non static number of fds,
139 * this function must be called to insure coherent events with associted fds.
141 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
143 if (events
&& events
->events
) {
144 memset(events
->events
, 0,
145 events
->nb_fd
* sizeof(struct epoll_event
));
150 * Clean the events structure of a lttng_poll_event. It's the caller
151 * responsability to free the lttng_poll_event memory.
153 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
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 LPOLLHUP
= POLLHUP
| POLLNVAL
,
191 LPOLLRDHUP
= POLLRDHUP
,
192 /* Close on exec feature does not exist for poll(2) */
193 LTTNG_CLOEXEC
= 0xdead,
196 struct compat_poll_event
{
197 uint32_t nb_fd
; /* Current number of fd in events */
198 uint32_t events_size
; /* Size of events array */
199 struct pollfd
*events
;
201 #define lttng_poll_event compat_poll_event
204 * For the following calls, consider 'e' to be a lttng_poll_event pointer and i
205 * being the index of the events array.
207 #define LTTNG_POLL_GETFD(e, i) LTTNG_REF(e)->events[i].fd
208 #define LTTNG_POLL_GETEV(e, i) LTTNG_REF(e)->events[i].revents
209 #define LTTNG_POLL_GETNB(e) LTTNG_REF(e)->nb_fd
210 #define LTTNG_POLL_GETSZ(e) LTTNG_REF(e)->events_size
213 * Create a pollfd structure of size 'size'.
215 extern int compat_poll_create(struct lttng_poll_event
*events
, int size
);
216 #define lttng_poll_create(events, size, flags) \
217 compat_poll_create(events, size);
220 * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data
223 extern int compat_poll_wait(struct lttng_poll_event
*events
, int timeout
);
224 #define lttng_poll_wait(events, timeout) \
225 compat_poll_wait(events, timeout);
228 * Add the fd to the pollfd structure. Resize if needed.
230 extern int compat_poll_add(struct lttng_poll_event
*events
,
231 int fd
, uint32_t req_events
);
232 #define lttng_poll_add(events, fd, req_events) \
233 compat_poll_add(events, fd, req_events);
236 * Remove the fd from the pollfd. Memory allocation is done to recreate a new
237 * pollfd, data is copied from the old pollfd to the new and, finally, the old
240 extern int compat_poll_del(struct lttng_poll_event
*events
, int fd
);
241 #define lttng_poll_del(events, fd) \
242 compat_poll_del(events, fd);
245 * Set up the poll set limits variable poll_max_size
247 extern void compat_poll_set_max_size(void);
248 #define lttng_poll_set_max_size(void) \
249 compat_poll_set_max_size(void);
252 * No need to reset a pollfd structure for poll(2)
254 static inline void lttng_poll_reset(struct lttng_poll_event
*events
)
258 * Clean the events structure of a lttng_poll_event. It's the caller
259 * responsability to free the lttng_poll_event memory.
261 static inline void lttng_poll_clean(struct lttng_poll_event
*events
)
264 __lttng_poll_free((void *) events
->events
);
268 #endif /* HAVE_EPOLL */
270 #endif /* _LTT_POLL_H */
This page took 0.038486 seconds and 5 git commands to generate.