Commit | Line | Data |
---|---|---|
5eb91c98 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * more details. | |
12 | * | |
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. | |
16 | */ | |
17 | ||
18 | #ifndef _LTT_POLL_H | |
19 | #define _LTT_POLL_H | |
20 | ||
21 | #include <string.h> | |
22 | #include <unistd.h> | |
23 | ||
24 | #include <lttng-share.h> | |
25 | ||
26 | /* | |
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. | |
29 | */ | |
30 | #define LTTNG_POLL_DEFAULT_SIZE 65535 | |
31 | ||
32 | /* | |
33 | * Maximum number of fd we can monitor. | |
34 | * | |
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. | |
40 | * | |
41 | * For poll(2), the max fds must not exceed RLIMIT_NOFILE given by | |
42 | * getrlimit(2). | |
43 | */ | |
44 | extern unsigned int poll_max_size; | |
45 | ||
46 | /* | |
47 | * Used by lttng_poll_clean to free the events structure in a lttng_poll_event. | |
48 | */ | |
49 | static inline void __lttng_poll_free(void *events) | |
50 | { | |
51 | free(events); | |
52 | } | |
53 | ||
54 | /* | |
55 | * epoll(7) implementation. | |
56 | */ | |
57 | #ifdef HAVE_EPOLL | |
58 | #include <sys/epoll.h> | |
59 | ||
60 | /* See man epoll(7) for this define path */ | |
61 | #define LTTNG_EPOLL_PROC_PATH "/proc/sys/fs/epoll/max_user_watches" | |
62 | ||
63 | enum { | |
64 | /* Polling variables compatibility for epoll */ | |
65 | LPOLLIN = EPOLLIN, | |
66 | LPOLLPRI = EPOLLPRI, | |
67 | LPOLLOUT = EPOLLOUT, | |
68 | LPOLLRDNORM = EPOLLRDNORM, | |
69 | LPOLLRDBAND = EPOLLRDBAND, | |
70 | LPOLLWRNORM = EPOLLWRNORM, | |
71 | LPOLLWRBAND = EPOLLWRBAND, | |
72 | LPOLLMSG = EPOLLMSG, | |
73 | LPOLLERR = EPOLLERR, | |
74 | LPOLLHUP = EPOLLHUP, | |
75 | LPOLLNVAL = EPOLLHUP, | |
76 | LPOLLRDHUP = EPOLLRDHUP, | |
77 | /* Close on exec feature of epoll */ | |
78 | LTTNG_CLOEXEC = EPOLL_CLOEXEC, | |
79 | }; | |
80 | ||
81 | struct compat_epoll_event { | |
82 | int epfd; | |
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; | |
86 | }; | |
87 | #define lttng_poll_event compat_epoll_event | |
88 | ||
89 | /* | |
90 | * For the following calls, consider 'e' to be a lttng_poll_event pointer and i | |
91 | * being the index of the events array. | |
92 | */ | |
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 | |
97 | ||
98 | /* | |
99 | * Create the epoll set. No memory allocation is done here. | |
100 | */ | |
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); | |
105 | ||
106 | /* | |
107 | * Wait on epoll set with the number of fd registered to the lttng_poll_event | |
108 | * data structure (events). | |
109 | */ | |
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); | |
113 | ||
114 | /* | |
115 | * Add a fd to the epoll set and resize the epoll_event structure if needed. | |
116 | */ | |
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); | |
121 | ||
122 | /* | |
123 | * Remove a fd from the epoll set. | |
124 | */ | |
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); | |
128 | ||
129 | /* | |
130 | * Set up the poll set limits variable poll_max_size | |
131 | */ | |
132 | extern void compat_epoll_set_max_size(void); | |
133 | #define lttng_poll_set_max_size(void) \ | |
134 | compat_epoll_set_max_size(void); | |
135 | ||
136 | /* | |
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. | |
140 | */ | |
141 | static inline void lttng_poll_reset(struct lttng_poll_event *events) | |
142 | { | |
143 | if (events && events->events) { | |
144 | memset(events->events, 0, | |
145 | events->nb_fd * sizeof(struct epoll_event)); | |
146 | } | |
147 | } | |
148 | ||
149 | /* | |
150 | * Clean the events structure of a lttng_poll_event. It's the caller | |
151 | * responsability to free the lttng_poll_event memory. | |
152 | */ | |
153 | static inline void lttng_poll_clean(struct lttng_poll_event *events) | |
154 | { | |
155 | if (events) { | |
156 | close(events->epfd); | |
157 | __lttng_poll_free((void *) events->events); | |
158 | } | |
159 | } | |
160 | ||
161 | #else /* HAVE_EPOLL */ | |
162 | /* | |
163 | * Fallback on poll(2) API | |
164 | */ | |
165 | ||
166 | /* Needed for some poll event values */ | |
167 | #ifndef __USE_XOPEN | |
168 | #define __USE_XOPEN | |
169 | #endif | |
170 | ||
171 | /* Needed for some poll event values */ | |
172 | #ifndef __USE_GNU | |
173 | #define __USE_GNU | |
174 | #endif | |
175 | ||
176 | #include <poll.h> | |
177 | #include <stdint.h> | |
178 | ||
179 | enum { | |
180 | /* Polling variables compatibility for poll */ | |
181 | LPOLLIN = POLLIN, | |
182 | LPOLLPRI = POLLPRI, | |
183 | LPOLLOUT = POLLOUT, | |
184 | LPOLLRDNORM = POLLRDNORM, | |
185 | LPOLLRDBAND = POLLRDBAND, | |
186 | LPOLLWRNORM = POLLWRNORM, | |
187 | LPOLLWRBAND = POLLWRBAND, | |
188 | LPOLLMSG = POLLMSG, | |
189 | LPOLLERR = POLLERR, | |
190 | LPOLLHUP = POLLHUP | POLLNVAL, | |
191 | LPOLLRDHUP = POLLRDHUP, | |
192 | /* Close on exec feature does not exist for poll(2) */ | |
193 | LTTNG_CLOEXEC = 0xdead, | |
194 | }; | |
195 | ||
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; | |
200 | }; | |
201 | #define lttng_poll_event compat_poll_event | |
202 | ||
203 | /* | |
204 | * For the following calls, consider 'e' to be a lttng_poll_event pointer and i | |
205 | * being the index of the events array. | |
206 | */ | |
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 | |
211 | ||
212 | /* | |
213 | * Create a pollfd structure of size 'size'. | |
214 | */ | |
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); | |
218 | ||
219 | /* | |
220 | * Wait on poll(2) event with nb_fd registered to the lttng_poll_event data | |
221 | * structure. | |
222 | */ | |
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); | |
226 | ||
227 | /* | |
228 | * Add the fd to the pollfd structure. Resize if needed. | |
229 | */ | |
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); | |
234 | ||
235 | /* | |
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 | |
238 | * one is freed(). | |
239 | */ | |
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); | |
243 | ||
244 | /* | |
245 | * Set up the poll set limits variable poll_max_size | |
246 | */ | |
247 | extern void compat_poll_set_max_size(void); | |
248 | #define lttng_poll_set_max_size(void) \ | |
249 | compat_poll_set_max_size(void); | |
250 | ||
251 | /* | |
252 | * No need to reset a pollfd structure for poll(2) | |
253 | */ | |
254 | static inline void lttng_poll_reset(struct lttng_poll_event *events) | |
255 | {} | |
256 | ||
257 | /* | |
258 | * Clean the events structure of a lttng_poll_event. It's the caller | |
259 | * responsability to free the lttng_poll_event memory. | |
260 | */ | |
261 | static inline void lttng_poll_clean(struct lttng_poll_event *events) | |
262 | { | |
263 | if (events) { | |
264 | __lttng_poll_free((void *) events->events); | |
265 | } | |
266 | } | |
267 | ||
268 | #endif /* HAVE_EPOLL */ | |
269 | ||
270 | #endif /* _LTT_POLL_H */ |