1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
#define NXT_POLL_ADD 0
#define NXT_POLL_CHANGE 1
#define NXT_POLL_DELETE 2
typedef struct {
/*
* A file descriptor is stored in hash entry to allow
* nxt_poll_fd_hash_test() to not dereference a pointer to
* nxt_fd_event_t which may be invalid if the file descriptor has
* been already closed and the nxt_fd_event_t's memory has been freed.
*/
nxt_socket_t fd;
uint32_t index;
void *event;
} nxt_poll_hash_entry_t;
static nxt_int_t nxt_poll_create(nxt_event_engine_t *engine,
nxt_uint_t mchanges, nxt_uint_t mevents);
static void nxt_poll_free(nxt_event_engine_t *engine);
static void nxt_poll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev);
static void nxt_poll_disable(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static nxt_bool_t nxt_poll_close(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static void nxt_poll_enable_read(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static void nxt_poll_enable_write(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static void nxt_poll_disable_read(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static void nxt_poll_disable_write(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static void nxt_poll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev);
static void nxt_poll_block_write(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static void nxt_poll_oneshot_read(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static void nxt_poll_oneshot_write(nxt_event_engine_t *engine,
nxt_fd_event_t *ev);
static void nxt_poll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev,
nxt_uint_t op, nxt_uint_t events);
static nxt_int_t nxt_poll_commit_changes(nxt_event_engine_t *engine);
static nxt_int_t nxt_poll_set_add(nxt_event_engine_t *engine,
nxt_fd_event_t *ev, int events);
static nxt_int_t nxt_poll_set_change(nxt_event_engine_t *engine,
nxt_fd_t fd, int events);
static nxt_int_t nxt_poll_set_delete(nxt_event_engine_t *engine, nxt_fd_t fd);
static void nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout);
static nxt_poll_hash_entry_t *nxt_poll_fd_hash_get(nxt_event_engine_t *engine,
nxt_fd_t fd);
static nxt_int_t nxt_poll_fd_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
static void nxt_poll_fd_hash_destroy(nxt_event_engine_t *engine,
nxt_lvlhsh_t *lh);
const nxt_event_interface_t nxt_poll_engine = {
"poll",
nxt_poll_create,
nxt_poll_free,
nxt_poll_enable,
nxt_poll_disable,
nxt_poll_disable,
nxt_poll_close,
nxt_poll_enable_read,
nxt_poll_enable_write,
nxt_poll_disable_read,
nxt_poll_disable_write,
nxt_poll_block_read,
nxt_poll_block_write,
nxt_poll_oneshot_read,
nxt_poll_oneshot_write,
nxt_poll_enable_read,
NULL,
NULL,
NULL,
NULL,
nxt_poll,
&nxt_unix_conn_io,
NXT_NO_FILE_EVENTS,
NXT_NO_SIGNAL_EVENTS,
};
static const nxt_lvlhsh_proto_t nxt_poll_fd_hash_proto nxt_aligned(64) =
{
NXT_LVLHSH_LARGE_MEMALIGN,
nxt_poll_fd_hash_test,
nxt_lvlhsh_alloc,
nxt_lvlhsh_free,
};
static nxt_int_t
nxt_poll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges,
nxt_uint_t mevents)
{
engine->u.poll.mchanges = mchanges;
engine->u.poll.changes = nxt_malloc(sizeof(nxt_poll_change_t) * mchanges);
if (engine->u.poll.changes != NULL) {
return NXT_OK;
}
return NXT_ERROR;
}
static void
nxt_poll_free(nxt_event_engine_t *engine)
{
nxt_debug(&engine->task, "poll free");
nxt_free(engine->u.poll.set);
nxt_free(engine->u.poll.changes);
nxt_poll_fd_hash_destroy(engine, &engine->u.poll.fd_hash);
nxt_memzero(&engine->u.poll, sizeof(nxt_poll_engine_t));
}
static void
nxt_poll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
ev->read = NXT_EVENT_ACTIVE;
ev->write = NXT_EVENT_ACTIVE;
nxt_poll_change(engine, ev, NXT_POLL_ADD, POLLIN | POLLOUT);
}
static void
nxt_poll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
if (ev->read != NXT_EVENT_INACTIVE && ev->write != NXT_EVENT_INACTIVE) {
ev->read = NXT_EVENT_INACTIVE;
ev->write = NXT_EVENT_INACTIVE;
nxt_poll_change(engine, ev, NXT_POLL_DELETE, 0);
}
}
static nxt_bool_t
nxt_poll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
nxt_poll_disable(engine, ev);
return ev->changing;
}
static void
nxt_poll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
nxt_uint_t op, events;
ev->read = NXT_EVENT_ACTIVE;
if (ev->write == NXT_EVENT_INACTIVE) {
op = NXT_POLL_ADD;
events = POLLIN;
} else {
op = NXT_POLL_CHANGE;
events = POLLIN | POLLOUT;
}
nxt_poll_change(engine, ev, op, events);
}
static void
nxt_poll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
nxt_uint_t op, events;
ev->write = NXT_EVENT_ACTIVE;
if (ev->read == NXT_EVENT_INACTIVE) {
op = NXT_POLL_ADD;
events = POLLOUT;
} else {
op = NXT_POLL_CHANGE;
events = POLLIN | POLLOUT;
}
nxt_poll_change(engine, ev, op, events);
}
static void
nxt_poll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
nxt_uint_t op, events;
ev->read = NXT_EVENT_INACTIVE;
if (ev->write == NXT_EVENT_INACTIVE) {
op = NXT_POLL_DELETE;
events = 0;
} else {
op = NXT_POLL_CHANGE;
events = POLLOUT;
}
nxt_poll_change(engine, ev, op, events);
}
static void
nxt_poll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
nxt_uint_t op, events;
ev->write = NXT_EVENT_INACTIVE;
if (ev->read == NXT_EVENT_INACTIVE) {
op = NXT_POLL_DELETE;
events = 0;
} else {
op = NXT_POLL_CHANGE;
events = POLLIN;
}
nxt_poll_change(engine, ev, op, events);
}
static void
nxt_poll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
if (ev->read != NXT_EVENT_INACTIVE) {
nxt_poll_disable_read(engine, ev);
}
}
static void
nxt_poll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
if (ev->write != NXT_EVENT_INACTIVE) {
nxt_poll_disable_write(engine, ev);
}
}
static void
nxt_poll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
nxt_uint_t op;
op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) ?
NXT_POLL_ADD : NXT_POLL_CHANGE;
ev->read = NXT_EVENT_ONESHOT;
ev->write = NXT_EVENT_INACTIVE;
nxt_poll_change(engine, ev, op, POLLIN);
}
static void
nxt_poll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev)
{
nxt_uint_t op;
op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) ?
NXT_POLL_ADD : NXT_POLL_CHANGE;
ev->read = NXT_EVENT_INACTIVE;
ev->write = NXT_EVENT_ONESHOT;
nxt_poll_change(engine, ev, op, POLLOUT);
}
/*
* poll changes are batched to improve instruction and data cache
* locality of several lvlhsh operations followed by poll() call.
*/
static void
nxt_poll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, nxt_uint_t op,
nxt_uint_t events)
{
nxt_poll_change_t *change;
nxt_debug(ev->task, "poll change: fd:%d op:%d ev:%XD", ev->fd, op, events);
if (engine->u.poll.nchanges >= engine->u.poll.mchanges) {
(void) nxt_poll_commit_changes(engine);
}
ev->changing = 1;
change = &engine->u.poll.changes[engine->u.poll.nchanges++];
change->op = op;
change->events = events;
change->event = ev;
}
static nxt_int_t
nxt_poll_commit_changes(nxt_event_engine_t *engine)
{
nxt_int_t ret, retval;
nxt_fd_event_t *ev;
nxt_poll_change_t *change, *end;
nxt_debug(&engine->task, "poll changes:%ui", engine->u.poll.nchanges);
retval = NXT_OK;
change = engine->u.poll.changes;
end = change + engine->u.poll.nchanges;
do {
ev = change->event;
ev->changing = 0;
switch (change->op) {
case NXT_POLL_ADD:
ret = nxt_poll_set_add(engine, ev, change->events);
if (nxt_fast_path(ret == NXT_OK)) {
goto next;
}
break;
case NXT_POLL_CHANGE:
ret = nxt_poll_set_change(engine, ev->fd, change->events);
if (nxt_fast_path(ret == NXT_OK)) {
goto next;
}
break;
case NXT_POLL_DELETE:
ret = nxt_poll_set_delete(engine, ev->fd);
if (nxt_fast_path(ret == NXT_OK)) {
goto next;
}
break;
}
nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler,
ev->task, ev, ev->data);
retval = NXT_ERROR;
next:
change++;
} while (change < end);
engine->u.poll.nchanges = 0;
return retval;
}
static nxt_int_t
nxt_poll_set_add(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int events)
{
nxt_int_t ret;
nxt_uint_t max_nfds;
struct pollfd *pfd;
nxt_lvlhsh_query_t lhq;
nxt_poll_hash_entry_t *phe;
nxt_debug(&engine->task, "poll add event: fd:%d ev:%04Xi", ev->fd, events);
if (engine->u.poll.nfds >= engine->u.poll.max_nfds) {
max_nfds = engine->u.poll.max_nfds + 512; /* 4K */
pfd = nxt_realloc(engine->u.poll.set, sizeof(struct pollfd) * max_nfds);
if (nxt_slow_path(pfd == NULL)) {
return NXT_ERROR;
}
engine->u.poll.set = pfd;
engine->u.poll.max_nfds = max_nfds;
}
phe = nxt_malloc(sizeof(nxt_poll_hash_entry_t));
if (nxt_slow_path(phe == NULL)) {
return NXT_ERROR;
}
phe->fd = ev->fd;
phe->index = engine->u.poll.nfds;
phe->event = ev;
pfd = &engine->u.poll.set[engine->u.poll.nfds++];
pfd->fd = ev->fd;
pfd->events = events;
pfd->revents = 0;
lhq.key_hash = nxt_murmur_hash2(&ev->fd, sizeof(nxt_fd_t));
lhq.replace = 0;
lhq.value = phe;
lhq.proto = &nxt_poll_fd_hash_proto;
lhq.data = engine;
ret = nxt_lvlhsh_insert(&engine->u.poll.fd_hash, &lhq);
if (nxt_fast_path(ret == NXT_OK)) {
return NXT_OK;
}
nxt_free(phe);
return NXT_ERROR;
}
static nxt_int_t
nxt_poll_set_change(nxt_event_engine_t *engine, nxt_fd_t fd, int events)
{
nxt_poll_hash_entry_t *phe;
nxt_debug(&engine->task, "poll change event: fd:%d ev:%04Xi",
fd, events);
phe = nxt_poll_fd_hash_get(engine, fd);
if (nxt_fast_path(phe != NULL)) {
engine->u.poll.set[phe->index].events = events;
return NXT_OK;
}
return NXT_ERROR;
}
static nxt_int_t
nxt_poll_set_delete(nxt_event_engine_t *engine, nxt_fd_t fd)
{
nxt_int_t ret;
nxt_uint_t index, nfds;
nxt_lvlhsh_query_t lhq;
nxt_poll_hash_entry_t *phe;
nxt_debug(&engine->task, "poll delete event: fd:%d", fd);
lhq.key_hash = nxt_murmur_hash2(&fd, sizeof(nxt_fd_t));
lhq.proto = &nxt_poll_fd_hash_proto;
lhq.data = engine;
ret = nxt_lvlhsh_delete(&engine->u.poll.fd_hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
return NXT_ERROR;
}
phe = lhq.value;
index = phe->index;
engine->u.poll.nfds--;
nfds = engine->u.poll.nfds;
if (index != nfds) {
engine->u.poll.set[index] = engine->u.poll.set[nfds];
phe = nxt_poll_fd_hash_get(engine, engine->u.poll.set[nfds].fd);
phe->index = index;
}
nxt_free(lhq.value);
return NXT_OK;
}
static void
nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout)
{
int nevents;
nxt_fd_t fd;
nxt_err_t err;
nxt_bool_t error;
nxt_uint_t i, events, level;
struct pollfd *pfd;
nxt_fd_event_t *ev;
nxt_poll_hash_entry_t *phe;
if (engine->u.poll.nchanges != 0) {
if (nxt_poll_commit_changes(engine) != NXT_OK) {
/* Error handlers have been enqueued on failure. */
timeout = 0;
}
}
nxt_debug(&engine->task, "poll() events:%ui timeout:%M",
engine->u.poll.nfds, timeout);
nevents = poll(engine->u.poll.set, engine->u.poll.nfds, timeout);
err = (nevents == -1) ? nxt_errno : 0;
nxt_thread_time_update(engine->task.thread);
nxt_debug(&engine->task, "poll(): %d", nevents);
if (nevents == -1) {
level = (err == NXT_EINTR) ? NXT_LOG_INFO : NXT_LOG_ALERT;
nxt_log(&engine->task, level, "poll() failed %E", err);
return;
}
for (i = 0; i < engine->u.poll.nfds && nevents != 0; i++) {
pfd = &engine->u.poll.set[i];
events = pfd->revents;
if (events == 0) {
continue;
}
fd = pfd->fd;
phe = nxt_poll_fd_hash_get(engine, fd);
if (nxt_slow_path(phe == NULL)) {
nxt_log(&engine->task, NXT_LOG_CRIT,
"poll() returned invalid fd:%d ev:%04Xd rev:%04uXi",
fd, pfd->events, events);
/* Mark the poll entry to ignore it by the kernel. */
pfd->fd = -1;
goto next;
}
ev = phe->event;
nxt_debug(ev->task, "poll: fd:%d ev:%04uXi rd:%d %wr:%d",
fd, events, ev->read, ev->write);
if (nxt_slow_path((events & POLLNVAL) != 0)) {
nxt_log(ev->task, NXT_LOG_CRIT,
"poll() error fd:%d ev:%04Xd rev:%04uXi",
fd, pfd->events, events);
/* Mark the poll entry to ignore it by the kernel. */
pfd->fd = -1;
nxt_work_queue_add(&engine->fast_work_queue,
ev->error_handler, ev->task, ev, ev->data);
goto next;
}
/*
* On a socket's remote end close:
*
* Linux, FreeBSD, and Solaris set POLLIN;
* MacOSX sets POLLIN and POLLHUP;
* NetBSD sets POLLIN, and poll(2) claims this explicitly:
*
* If the remote end of a socket is closed, poll()
* returns a POLLIN event, rather than a POLLHUP.
*
* On error:
*
* Linux sets POLLHUP and POLLERR only;
* FreeBSD adds POLLHUP to POLLIN or POLLOUT, although poll(2)
* claims the opposite:
*
* Note that POLLHUP and POLLOUT should never be
* present in the revents bitmask at the same time.
*
* Solaris and NetBSD do not add POLLHUP or POLLERR;
* MacOSX sets POLLHUP only.
*
* If an implementation sets POLLERR or POLLHUP only without POLLIN
* or POLLOUT, the "error" variable enqueues only one active handler.
*/
error = (((events & (POLLERR | POLLHUP)) != 0)
&& ((events & (POLLIN | POLLOUT)) == 0));
if ((events & POLLIN) || (error && ev->read_handler != NULL)) {
error = 0;
ev->read_ready = 1;
if (ev->read == NXT_EVENT_ONESHOT) {
ev->read = NXT_EVENT_INACTIVE;
nxt_poll_change(engine, ev, NXT_POLL_DELETE, 0);
}
nxt_work_queue_add(ev->read_work_queue, ev->read_handler,
ev->task, ev, ev->data);
}
if ((events & POLLOUT) || (error && ev->write_handler != NULL)) {
ev->write_ready = 1;
if (ev->write == NXT_EVENT_ONESHOT) {
ev->write = NXT_EVENT_INACTIVE;
nxt_poll_change(engine, ev, NXT_POLL_DELETE, 0);
}
nxt_work_queue_add(ev->write_work_queue, ev->write_handler,
ev->task, ev, ev->data);
}
next:
nevents--;
}
}
static nxt_poll_hash_entry_t *
nxt_poll_fd_hash_get(nxt_event_engine_t *engine, nxt_fd_t fd)
{
nxt_lvlhsh_query_t lhq;
nxt_poll_hash_entry_t *phe;
lhq.key_hash = nxt_murmur_hash2(&fd, sizeof(nxt_fd_t));
lhq.proto = &nxt_poll_fd_hash_proto;
lhq.data = engine;
if (nxt_lvlhsh_find(&engine->u.poll.fd_hash, &lhq) == NXT_OK) {
phe = lhq.value;
return phe;
}
nxt_log(&engine->task, NXT_LOG_CRIT, "fd %d not found in hash", fd);
return NULL;
}
static nxt_int_t
nxt_poll_fd_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
{
nxt_event_engine_t *engine;
nxt_poll_hash_entry_t *phe;
phe = data;
/* nxt_murmur_hash2() is unique for 4 bytes. */
engine = lhq->data;
if (nxt_fast_path(phe->fd == engine->u.poll.set[phe->index].fd)) {
return NXT_OK;
}
nxt_log(&engine->task, NXT_LOG_CRIT,
"fd %d in hash mismatches fd %d in poll set",
phe->fd, engine->u.poll.set[phe->index].fd);
return NXT_DECLINED;
}
static void
nxt_poll_fd_hash_destroy(nxt_event_engine_t *engine, nxt_lvlhsh_t *lh)
{
nxt_lvlhsh_each_t lhe;
nxt_lvlhsh_query_t lhq;
nxt_poll_hash_entry_t *phe;
nxt_memzero(&lhe, sizeof(nxt_lvlhsh_each_t));
lhe.proto = &nxt_poll_fd_hash_proto;
lhq.proto = &nxt_poll_fd_hash_proto;
for ( ;; ) {
phe = nxt_lvlhsh_each(lh, &lhe);
if (phe == NULL) {
return;
}
lhq.key_hash = nxt_murmur_hash2(&phe->fd, sizeof(nxt_fd_t));
if (nxt_lvlhsh_delete(lh, &lhq) != NXT_OK) {
nxt_log(&engine->task, NXT_LOG_CRIT,
"event fd %d not found in hash", phe->fd);
}
nxt_free(phe);
}
}
|