blob: dbad7b790fa07fdf5edcc7098387e89c0dd3ae87 [file] [log] [blame]
Emmanuel Hocdet0ba4f482019-04-08 16:53:32 +00001/*
2 * FD polling functions for SunOS event ports.
3 *
4 * Copyright 2018 Joyent, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <unistd.h>
13#include <sys/time.h>
14#include <sys/types.h>
15
16#include <poll.h>
17#include <port.h>
18#include <errno.h>
19#include <syslog.h>
20
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/hathreads.h>
24#include <common/ticks.h>
25#include <common/time.h>
26#include <common/tools.h>
27
28#include <types/global.h>
29
30#include <proto/activity.h>
31#include <proto/fd.h>
32#include <proto/log.h>
33#include <proto/signal.h>
34
35/*
36 * Private data:
37 */
38static int evports_fd[MAX_THREADS]; // per-thread evports_fd
39static THREAD_LOCAL port_event_t *evports_evlist = NULL;
40static THREAD_LOCAL int evports_evlist_max = 0;
41
42/*
43 * Convert the "state" member of "fdtab" into an event ports event mask.
44 */
45static inline int evports_state_to_events(int state)
46{
47 int events = 0;
48
49 if (state & FD_EV_POLLED_W)
50 events |= POLLOUT;
51 if (state & FD_EV_POLLED_R)
52 events |= POLLIN;
53
54 return (events);
55}
56
57/*
58 * Associate or dissociate this file descriptor with the event port, using the
59 * specified event mask.
60 */
61static inline void evports_resync_fd(int fd, int events)
62{
63 if (events == 0)
64 port_dissociate(evports_fd[tid], PORT_SOURCE_FD, fd);
65 else
66 port_associate(evports_fd[tid], PORT_SOURCE_FD, fd, events, NULL);
67}
68
69static void _update_fd(int fd)
70{
71 int en;
72 int events;
73
74 en = fdtab[fd].state;
75
76 if (!(fdtab[fd].thread_mask & tid_bit) || !(en & FD_EV_POLLED_RW)) {
77 if (!(polled_mask[fd] & tid_bit)) {
78 /* fd was not watched, it's still not */
79 return;
80 }
81 /* fd totally removed from poll list */
82 events = 0;
83 _HA_ATOMIC_AND(&polled_mask[fd], ~tid_bit);
84 }
85 else {
86 /* OK fd has to be monitored, it was either added or changed */
87 events = evports_state_to_events(en);
88 _HA_ATOMIC_OR(&polled_mask[fd], tid_bit);
89 }
90 evports_resync_fd(fd, events);
91}
92
93/*
94 * Event Ports poller. This routine interacts with the file descriptor
95 * management data structures and routines; see the large block comment in
96 * "src/fd.c" for more information.
97 */
98
99REGPRM2 static void _do_poll(struct poller *p, int exp)
100{
101 int i;
102 int wait_time;
103 struct timespec timeout_ts;
104 unsigned int nevlist;
105 int fd, old_fd;
106 int status;
107
108 /*
109 * Scan the list of file descriptors with an updated status:
110 */
111 for (i = 0; i < fd_nbupdt; i++) {
112 fd = fd_updt[i];
113
114 _HA_ATOMIC_AND(&fdtab[fd].update_mask, ~tid_bit);
115 if (fdtab[fd].owner == NULL) {
116 activity[tid].poll_drop++;
117 continue;
118 }
119
120 _update_fd(fd);
121 }
122 fd_nbupdt = 0;
123 /* Scan the global update list */
124 for (old_fd = fd = update_list.first; fd != -1; fd = fdtab[fd].update.next) {
125 if (fd == -2) {
126 fd = old_fd;
127 continue;
128 }
129 else if (fd <= -3)
130 fd = -fd -4;
131 if (fd == -1)
132 break;
133 if (fdtab[fd].update_mask & tid_bit)
134 done_update_polling(fd);
135 else
136 continue;
137 if (!fdtab[fd].owner)
138 continue;
139 _update_fd(fd);
140 }
141
142 thread_harmless_now();
143
144 /*
145 * Determine how long to wait for events to materialise on the port.
146 */
147 wait_time = compute_poll_timeout(exp);
148 tv_entering_poll();
149 activity_count_runtime();
150
151 do {
152 int timeout = (global.tune.options & GTUNE_BUSY_POLLING) ? 0 : wait_time;
153 int interrupted = 0;
154 nevlist = 1; /* desired number of events to be retrieved */
155 timeout_ts.tv_sec = (timeout / 1000);
156 timeout_ts.tv_nsec = (timeout % 1000) * 1000000;
157
158 status = port_getn(evports_fd[tid],
159 evports_evlist,
160 evports_evlist_max,
161 &nevlist, /* updated to the number of events retrieved */
162 &timeout_ts);
163 if (status != 0) {
164 int e = errno;
165 switch (e) {
166 case ETIME:
167 /*
168 * Though the manual page has not historically made it
169 * clear, port_getn() can return -1 with an errno of
170 * ETIME and still have returned some number of events.
171 */
172 /* nevlist >= 0 */
173 break;
174 default:
175 nevlist = 0;
176 interrupted = 1;
177 break;
178 }
179 }
180 tv_update_date(timeout, nevlist);
181
182 if (nevlist || interrupted)
183 break;
184 if (timeout || !wait_time)
185 break;
186 if (signal_queue_len)
187 break;
188 if (tick_isset(exp) && tick_is_expired(exp, now_ms))
189 break;
190 } while(1);
191
192 tv_leaving_poll(wait_time, nevlist);
193
194 thread_harmless_end();
195
196 for (i = 0; i < nevlist; i++) {
197 unsigned int n = 0;
198 int events, rebind_events;
199 fd = evports_evlist[i].portev_object;
200 events = evports_evlist[i].portev_events;
201
202 if (fdtab[fd].owner == NULL) {
203 activity[tid].poll_dead++;
204 continue;
205 }
206
207 if (!(fdtab[fd].thread_mask & tid_bit)) {
208 activity[tid].poll_skip++;
209 continue;
210 }
211
212 /*
213 * By virtue of receiving an event for this file descriptor, it
214 * is no longer associated with the port in question. Store
215 * the previous event mask so that we may reassociate after
216 * processing is complete.
217 */
218 rebind_events = evports_state_to_events(fdtab[fd].state);
219 /* rebind_events != 0 */
220
221 /*
222 * Set bits based on the events we received from the port:
223 */
224 if (events & POLLIN)
225 n |= FD_POLL_IN;
226 if (events & POLLOUT)
227 n |= FD_POLL_OUT;
228 if (events & POLLERR)
229 n |= FD_POLL_ERR;
230 if (events & POLLHUP)
231 n |= FD_POLL_HUP;
232
233 /*
234 * Call connection processing callbacks. Note that it's
235 * possible for this processing to alter the required event
236 * port assocation; i.e., the "state" member of the "fdtab"
237 * entry. If it changes, the fd will be placed on the updated
238 * list for processing the next time we are called.
239 */
240 fd_update_events(fd, n);
241
242 /*
243 * This file descriptor was closed during the processing of
244 * polled events. No need to reassociate.
245 */
246 if (fdtab[fd].owner == NULL)
247 continue;
248
249 /*
250 * Reassociate with the port, using the same event mask as
251 * before. This call will not result in a dissociation as we
252 * asserted that _some_ events needed to be rebound above.
253 *
254 * Reassociating with the same mask allows us to mimic the
255 * level-triggered behaviour of poll(2). In the event that we
256 * are interested in the same events on the next turn of the
257 * loop, this represents no extra work.
258 *
259 * If this additional port_associate(3C) call becomes a
260 * performance problem, we would need to verify that we can
261 * correctly interact with the file descriptor cache and update
262 * list (see "src/fd.c") to avoid reassociating here, or to use
263 * a different events mask.
264 */
265 evports_resync_fd(fd, rebind_events);
266 }
267}
268
269static int init_evports_per_thread()
270{
271 int fd;
272
273 evports_evlist_max = global.tune.maxpollevents;
274 evports_evlist = calloc(evports_evlist_max, sizeof (port_event_t));
275 if (evports_evlist == NULL) {
276 goto fail_alloc;
277 }
278
279 if (MAX_THREADS > 1 && tid) {
280 if ((evports_fd[tid] = port_create()) == -1) {
281 goto fail_fd;
282 }
283 }
284
285 /* we may have to unregister some events initially registered on the
286 * original fd when it was alone, and/or to register events on the new
287 * fd for this thread. Let's just mark them as updated, the poller will
288 * do the rest.
289 */
290 for (fd = 0; fd < global.maxsock; fd++)
291 updt_fd_polling(fd);
292
293 return 1;
294
295 fail_fd:
296 free(evports_evlist);
297 evports_evlist = NULL;
298 evports_evlist_max = 0;
299 fail_alloc:
300 return 0;
301}
302
303static void deinit_evports_per_thread()
304{
305 if (MAX_THREADS > 1 && tid)
306 close(evports_fd[tid]);
307
308 free(evports_evlist);
309 evports_evlist = NULL;
310 evports_evlist_max = 0;
311}
312
313/*
314 * Initialisation of the event ports poller.
315 * Returns 0 in case of failure, non-zero in case of success.
316 */
317REGPRM1 static int _do_init(struct poller *p)
318{
319 p->private = NULL;
320
321 if ((evports_fd[tid] = port_create()) == -1) {
322 goto fail;
323 }
324
325 hap_register_per_thread_init(init_evports_per_thread);
326 hap_register_per_thread_deinit(deinit_evports_per_thread);
327
328 return 1;
329
330fail:
331 p->pref = 0;
332 return 0;
333}
334
335/*
336 * Termination of the event ports poller.
337 * All resources are released and the poller is marked as inoperative.
338 */
339REGPRM1 static void _do_term(struct poller *p)
340{
341 if (evports_fd[tid] != -1) {
342 close(evports_fd[tid]);
343 evports_fd[tid] = -1;
344 }
345
346 p->private = NULL;
347 p->pref = 0;
348
349 free(evports_evlist);
350 evports_evlist = NULL;
351 evports_evlist_max = 0;
352}
353
354/*
355 * Run-time check to make sure we can allocate the resources needed for
356 * the poller to function correctly.
357 * Returns 1 on success, otherwise 0.
358 */
359REGPRM1 static int _do_test(struct poller *p)
360{
361 int fd;
362
363 if ((fd = port_create()) == -1) {
364 return 0;
365 }
366
367 close(fd);
368 return 1;
369}
370
371/*
372 * Close and recreate the event port after fork(). Returns 1 on success,
373 * otherwise 0. If this function fails, "_do_term()" must be called to
374 * clean up the poller.
375 */
376REGPRM1 static int _do_fork(struct poller *p)
377{
378 if (evports_fd[tid] != -1) {
379 close(evports_fd[tid]);
380 }
381
382 if ((evports_fd[tid] = port_create()) == -1) {
383 return 0;
384 }
385
386 return 1;
387}
388
389/*
390 * This constructor must be called before main() to register the event ports
391 * poller.
392 */
393__attribute__((constructor))
394static void _do_register(void)
395{
396 struct poller *p;
397 int i;
398
399 if (nbpollers >= MAX_POLLERS)
400 return;
401
402 for (i = 0; i < MAX_THREADS; i++)
403 evports_fd[i] = -1;
404
405 p = &pollers[nbpollers++];
406
407 p->name = "evports";
408 p->pref = 300;
409 p->flags = 0;
410 p->private = NULL;
411
412 p->clo = NULL;
413 p->test = _do_test;
414 p->init = _do_init;
415 p->term = _do_term;
416 p->poll = _do_poll;
417 p->fork = _do_fork;
418}