-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy patherl_check_io.c
More file actions
3344 lines (2994 loc) · 110 KB
/
Copy patherl_check_io.c
File metadata and controls
3344 lines (2994 loc) · 110 KB
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* %CopyrightBegin%
*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright Ericsson AB 2006-2025. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
/*
* Description: Check I/O
*
* Author: Rickard Green
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#define ERL_CHECK_IO_C__
#ifndef WANT_NONBLOCKING
# define WANT_NONBLOCKING
#endif
#include "sys.h"
#include "global.h"
#include "erl_port.h"
#include "erl_port_task.h"
#include "erl_check_io.h"
#include "erl_thr_progress.h"
#include "erl_bif_unique.h"
#include "erl_proc_sig_queue.h"
#include "dtrace-wrapper.h"
#include "lttng-wrapper.h"
#define ERTS_WANT_TIMER_WHEEL_API
#include "erl_time.h"
#if 0
#define DEBUG_PRINT(FMT, ...) do { erts_printf(FMT "\r\n", ##__VA_ARGS__); fflush(stdout); } while(0)
#define DEBUG_PRINT_FD(FMT, STATE, ...) \
do { \
const char buff[128]; \
DEBUG_PRINT("%d: " FMT " (ev=%s, ac=%s, flg=%s, cnt=%d)", \
(STATE) ? (STATE)->fd : (ErtsSysFdType)-1, ##__VA_ARGS__, \
ev2str((STATE) ? (STATE)->events : ERTS_POLL_EV_NONE), \
ev2str((STATE) ? (STATE)->active_events : ERTS_POLL_EV_NONE), \
event_state_flag_to_str((STATE) ? (STATE)->flags : ERTS_EV_FLAG_CLEAR, \
buff, sizeof(buff)), (STATE)->count); \
} while(0)
#define DEBUG_PRINT_MODE
#else
#define DEBUG_PRINT(...)
#endif
#ifndef DEBUG_PRINT_FD
#define DEBUG_PRINT_FD(...)
#endif
#ifndef ERTS_SYS_CONTINOUS_FD_NUMBERS
# include "safe_hash.h"
# define DRV_EV_STATE_HTAB_SIZE 1024
#endif
typedef enum {
ERTS_EV_TYPE_NONE = 0,
ERTS_EV_TYPE_DRV_SEL = 1, /* driver_select */
ERTS_EV_TYPE_STOP_USE = 2, /* pending stop_select */
ERTS_EV_TYPE_NIF = 3, /* enif_select */
ERTS_EV_TYPE_STOP_NIF = 4 /* pending nif stop */
} EventStateType;
typedef enum {
ERTS_EV_FLAG_CLEAR = 0x0,
ERTS_EV_FLAG_USED = 0x1, /* ERL_DRV_USE has been turned on */
#if ERTS_POLL_USE_SCHEDULER_POLLING
ERTS_EV_FLAG_SCHEDULER = 0x2, /* Set when the fd has been migrated
to scheduler pollset */
ERTS_EV_FLAG_IN_SCHEDULER = 0x4, /* Set when the fd is currently in
scheduler pollset */
ERTS_EV_FLAG_NIF_SELECT = 0x8, /* Set if a nif select message is in-flight */
#else
ERTS_EV_FLAG_SCHEDULER = ERTS_EV_FLAG_CLEAR,
ERTS_EV_FLAG_IN_SCHEDULER = ERTS_EV_FLAG_CLEAR,
ERTS_EV_FLAG_NIF_SELECT = ERTS_EV_FLAG_CLEAR,
#endif
#ifdef ERTS_POLL_USE_FALLBACK /* This has to be an ifdef */
ERTS_EV_FLAG_FALLBACK = 0x10, /* Set when kernel poll rejected fd
and it was put in the nkp version */
#else
ERTS_EV_FLAG_FALLBACK = ERTS_EV_FLAG_CLEAR,
#endif
ERTS_EV_FLAG_WANT_ERROR = 0x20, /* ERL_NIF_SELECT_ERROR turned on */
/* Combinations, defined only to be displayed by debugger (gdb) */
ERTS_EV_FLAG_USED_FALLBACK = ERTS_EV_FLAG_USED | ERTS_EV_FLAG_FALLBACK,
ERTS_EV_FLAG_USED_SCHEDULER = ERTS_EV_FLAG_USED | ERTS_EV_FLAG_SCHEDULER,
ERTS_EV_FLAG_USED_IN_SCHEDULER = ERTS_EV_FLAG_USED | ERTS_EV_FLAG_SCHEDULER | ERTS_EV_FLAG_IN_SCHEDULER,
ERTS_EV_FLAG_UNUSED_SCHEDULER = ERTS_EV_FLAG_SCHEDULER,
ERTS_EV_FLAG_UNUSED_IN_SCHEDULER = ERTS_EV_FLAG_SCHEDULER | ERTS_EV_FLAG_IN_SCHEDULER
} EventStateFlags;
static const char* event_state_flag_to_str(EventStateFlags f, const char *buff, int len)
{
switch ((int)f) {
case ERTS_EV_FLAG_CLEAR: return "CLEAR";
case ERTS_EV_FLAG_USED: return "USED";
case ERTS_EV_FLAG_FALLBACK: return "FLBK";
case ERTS_EV_FLAG_FALLBACK | ERTS_EV_FLAG_USED: return "USED|FLBK";
case ERTS_EV_FLAG_WANT_ERROR: return "WANT_ERROR";
#if ERTS_POLL_USE_SCHEDULER_POLLING
case ERTS_EV_FLAG_NIF_SELECT: return "NIF_SELECT";
case ERTS_EV_FLAG_USED | ERTS_EV_FLAG_NIF_SELECT: return "USED|NIF_SELECT";
case ERTS_EV_FLAG_SCHEDULER: return "SCHD";
case ERTS_EV_FLAG_SCHEDULER | ERTS_EV_FLAG_USED: return "USED|SCHD";
case ERTS_EV_FLAG_SCHEDULER | ERTS_EV_FLAG_NIF_SELECT: return "SCHD|NIF_SELECT";
case ERTS_EV_FLAG_SCHEDULER | ERTS_EV_FLAG_IN_SCHEDULER: return "IN_SCHD";
case ERTS_EV_FLAG_SCHEDULER | ERTS_EV_FLAG_IN_SCHEDULER |
ERTS_EV_FLAG_NIF_SELECT: return "IN_SCHD|NIF_SELECT";
case ERTS_EV_FLAG_SCHEDULER | ERTS_EV_FLAG_IN_SCHEDULER
| ERTS_EV_FLAG_USED: return "USED|IN_SCHD";
#endif
default:
snprintf((char *)buff, len, "ERROR(%d)", f);
return buff;
}
}
/* How many events that can be handled at once by one erts_poll_wait call */
#define ERTS_CHECK_IO_POLL_RES_LEN 512
/* Each I/O Poll Thread has one ErtsPollThread each. The ps field
can point to either a private ErtsPollSet or a shared one.
At the moment only kqueue and epoll pollsets can be
shared across threads.
*/
typedef struct erts_poll_thread
{
ErtsPollSet *ps;
ErtsPollResFd *pollres;
ErtsThrPrgrData *tpd;
int pollres_len;
} ErtsPollThread;
/* pollsetv contains pointers to the ErtsPollSets that are in use.
* Which pollset to use is determined by hashing the fd.
*/
static ErtsPollSet **pollsetv;
static ErtsPollThread *psiv;
#if ERTS_POLL_USE_FALLBACK
static ErtsPollSet *flbk_pollset;
#endif
#if ERTS_POLL_USE_SCHEDULER_POLLING
ErtsPollSet *sched_pollset;
#endif
typedef struct {
#ifndef ERTS_SYS_CONTINOUS_FD_NUMBERS
SafeHashBucket hb;
#endif
ErtsSysFdType fd;
struct {
ErtsDrvSelectDataState *select; /* ERTS_EV_TYPE_DRV_SEL */
ErtsNifSelectDataState *nif; /* ERTS_EV_TYPE_NIF */
union {
erts_driver_t* drv_ptr; /* ERTS_EV_TYPE_STOP_USE */
ErtsResource* resource; /* ERTS_EV_TYPE_STOP_NIF */
} stop;
} driver;
/* The events that have been selected upon, that is the events that
* driver_select/enif_select has been issued for. */
ErtsPollEvents events;
/* The events currently active in the pollset. This can be both fewer and more
* events than what are in the `events` fields, but normally they are the same.
*
* The scenarios when they differ are:
* - When a driver_select has triggered, but ready_input has not yet been called.
* In this scenario `events` will have the event, but it will not be active
* until erts_io_notify_port_task_executed has been called.
* - When an FD has been migrated to the sched_pollset, but has then been
* deselected. In this scenario the FD will remain in the scheduler pollset
* until it is triggered. In this scenario the event till be active, but not
* part of `events`.
*/
ErtsPollEvents active_events;
EventStateType type;
EventStateFlags flags;
int count; /* Number of times this fd has triggered
without being deselected. */
Eterm last_select_pid; /* Last process that called enif_select */
} ErtsDrvEventState;
struct drv_ev_state_shared {
union {
erts_mtx_t lck;
byte _cache_line_alignment[ERTS_ALC_CACHE_LINE_ALIGN_SIZE(sizeof(erts_mtx_t))];
} locks[ERTS_CHECK_IO_DRV_EV_STATE_LOCK_CNT];
#ifdef ERTS_SYS_CONTINOUS_FD_NUMBERS
int max_fds;
erts_atomic_t len;
ErtsDrvEventState *v;
erts_mtx_t grow_lock; /* prevent lock-hogging of racing growers */
#else
SafeHash tab;
int num_prealloc;
ErtsDrvEventState *prealloc_first;
erts_spinlock_t prealloc_lock;
#endif
};
int ERTS_WRITE_UNLIKELY(erts_no_pollsets) = 1;
int ERTS_WRITE_UNLIKELY(erts_no_poll_threads) = 1;
struct drv_ev_state_shared drv_ev_state;
/* Used by etp */
ErtsPollEvents etp_poll_ev_none = ERTS_POLL_EV_NONE;
ErtsPollEvents etp_poll_ev_in = ERTS_POLL_EV_IN;
ErtsPollEvents etp_poll_ev_out = ERTS_POLL_EV_OUT;
ErtsPollEvents etp_poll_ev_err = ERTS_POLL_EV_ERR;
ErtsPollEvents etp_poll_ev_nval = ERTS_POLL_EV_NVAL;
static ERTS_INLINE int fd_hash(ErtsSysFdType fd)
{
#ifdef ERTS_SYS_CONTINOUS_FD_NUMBERS
int hash = (int)fd;
#else
int hash = (int)(SWord)fd;
hash ^= (hash >> 9);
#endif
return hash;
}
static ERTS_INLINE erts_mtx_t* fd_mtx(ErtsSysFdType fd)
{
return &drv_ev_state.locks[fd_hash(fd) % ERTS_CHECK_IO_DRV_EV_STATE_LOCK_CNT].lck;
}
#ifdef ERTS_SYS_CONTINOUS_FD_NUMBERS
static ERTS_INLINE ErtsDrvEventState *get_drv_ev_state(ErtsSysFdType fd)
{
return &drv_ev_state.v[(int) fd];
}
#define new_drv_ev_state(State, fd) (State)
#define erase_drv_ev_state(State)
static ERTS_INLINE int grow_drv_ev_state(ErtsSysFdType fd) {
int i;
int old_len;
int new_len;
if ((unsigned)fd >= (unsigned)erts_atomic_read_nob(&drv_ev_state.len)) {
if (fd < 0 || fd >= drv_ev_state.max_fds)
return 0;
erts_mtx_lock(&drv_ev_state.grow_lock);
old_len = erts_atomic_read_nob(&drv_ev_state.len);
if (fd >= old_len) {
new_len = erts_poll_new_table_len(old_len, fd + 1);
if (new_len > drv_ev_state.max_fds)
new_len = drv_ev_state.max_fds;
for (i=0; i<ERTS_CHECK_IO_DRV_EV_STATE_LOCK_CNT; i++) { /* lock all fd's */
erts_mtx_lock(&drv_ev_state.locks[i].lck);
}
drv_ev_state.v = (drv_ev_state.v
? erts_realloc(ERTS_ALC_T_DRV_EV_STATE,
drv_ev_state.v,
sizeof(ErtsDrvEventState)*new_len)
: erts_alloc(ERTS_ALC_T_DRV_EV_STATE,
sizeof(ErtsDrvEventState)*new_len));
ERTS_CT_ASSERT(ERTS_EV_TYPE_NONE == 0);
sys_memzero(drv_ev_state.v+old_len,
sizeof(ErtsDrvEventState) * (new_len - old_len));
for (i = old_len; i < new_len; i++) {
drv_ev_state.v[i].fd = (ErtsSysFdType) i;
drv_ev_state.v[i].last_select_pid = NIL;
}
erts_atomic_set_nob(&drv_ev_state.len, new_len);
for (i=0; i<ERTS_CHECK_IO_DRV_EV_STATE_LOCK_CNT; i++) {
erts_mtx_unlock(&drv_ev_state.locks[i].lck);
}
}
/*else already grown by racing thread */
erts_mtx_unlock(&drv_ev_state.grow_lock);
}
return 1;
}
static int drv_ev_state_len(void)
{
return erts_atomic_read_nob(&drv_ev_state.len);
}
#else /* !ERTS_SYS_CONTINOUS_FD_NUMBERS */
static ERTS_INLINE ErtsDrvEventState *get_drv_ev_state(ErtsSysFdType fd)
{
ErtsDrvEventState tmpl;
tmpl.fd = fd;
return (ErtsDrvEventState *) safe_hash_get(&drv_ev_state.tab, (void *) &tmpl);
}
static ERTS_INLINE ErtsDrvEventState* new_drv_ev_state(ErtsDrvEventState *state,
ErtsSysFdType fd)
{
ErtsDrvEventState tmpl;
if (state)
return state;
tmpl.fd = fd;
tmpl.driver.select = NULL;
tmpl.driver.nif = NULL;
tmpl.driver.stop.drv_ptr = NULL;
tmpl.events = 0;
tmpl.active_events = 0;
tmpl.type = ERTS_EV_TYPE_NONE;
tmpl.flags = 0;
tmpl.count = 0;
tmpl.last_select_pid = NIL;
return (ErtsDrvEventState *) safe_hash_put(&drv_ev_state.tab, (void *) &tmpl);
}
static ERTS_INLINE void erase_drv_ev_state(ErtsDrvEventState *state)
{
safe_hash_erase(&drv_ev_state.tab, (void *) state);
}
static int drv_ev_state_len(void)
{
return erts_atomic_read_nob(&drv_ev_state.tab.nitems);
}
#endif /* !ERTS_SYS_CONTINOUS_FD_NUMBERS */
static void stale_drv_select(Eterm id, ErtsDrvEventState *state, int mode);
static void drv_select_steal(ErlDrvPort ix, ErtsDrvEventState *state,
int mode, int on);
static void nif_select_steal(ErtsDrvEventState *state, int mode,
ErtsResource* resource, Eterm ref);
static void print_drv_select_op(erts_dsprintf_buf_t *dsbufp,
ErlDrvPort ix, ErtsSysFdType fd, int mode, int on);
static void print_nif_select_op(erts_dsprintf_buf_t*, ErtsSysFdType,
int mode, ErtsResource*, Eterm ref);
#ifdef ERTS_SYS_CONTINOUS_FD_NUMBERS
static void drv_select_large_fd_error(ErlDrvPort, ErtsSysFdType, int, int);
static void nif_select_large_fd_error(ErtsSysFdType, int, ErtsResource*,Eterm ref);
#endif
static void
steal_pending_stop_use(erts_dsprintf_buf_t*, ErlDrvPort, ErtsDrvEventState*,
int mode, int on);
static void
steal_pending_stop_nif(erts_dsprintf_buf_t *dsbufp, ErtsResource*,
ErtsDrvEventState *state, int mode, int on);
static ERTS_INLINE void
check_fd_cleanup(ErtsDrvEventState *state,
ErtsDrvSelectDataState **free_select,
ErtsNifSelectDataState **free_nif);
static void clear_select_event(struct erts_nif_select_event* e);
static ERTS_INLINE void iready(Eterm id, ErtsDrvEventState *state);
static ERTS_INLINE void oready(Eterm id, ErtsDrvEventState *state);
#ifdef DEBUG_PRINT_MODE
static char *drvmode2str(int mode);
static char *nifmode2str(enum ErlNifSelectFlags mode, const char *buff, int sz);
#endif
static ERTS_INLINE void
init_iotask(ErtsIoTask *io_task, ErtsSysFdType fd)
{
erts_port_task_handle_init(&io_task->task);
io_task->fd = fd;
}
static ERTS_INLINE int
is_iotask_active(ErtsIoTask *io_task)
{
if (erts_port_task_is_scheduled(&io_task->task))
return 1;
return 0;
}
static ERTS_INLINE ErtsDrvSelectDataState *
alloc_drv_select_data(ErtsSysFdType fd)
{
ErtsDrvSelectDataState *dsp = erts_alloc(ERTS_ALC_T_DRV_SEL_D_STATE,
sizeof(ErtsDrvSelectDataState));
dsp->inport = NIL;
dsp->outport = NIL;
init_iotask(&dsp->iniotask, fd);
init_iotask(&dsp->outiotask, fd);
return dsp;
}
static ERTS_INLINE ErtsNifSelectDataState *
alloc_nif_select_data(void)
{
ErtsNifSelectDataState *dsp = erts_alloc(ERTS_ALC_T_NIF_SEL_D_STATE,
sizeof(ErtsNifSelectDataState));
dsp->in.pid = NIL;
dsp->out.pid = NIL;
dsp->err.pid = NIL;
return dsp;
}
static ERTS_INLINE void
free_drv_select_data(ErtsDrvSelectDataState *dsp)
{
ASSERT(!erts_port_task_is_scheduled(&dsp->iniotask.task));
ASSERT(!erts_port_task_is_scheduled(&dsp->outiotask.task));
erts_free(ERTS_ALC_T_DRV_SEL_D_STATE, dsp);
}
static ERTS_INLINE void
free_nif_select_data(ErtsNifSelectDataState *dsp)
{
erts_free(ERTS_ALC_T_NIF_SEL_D_STATE, dsp);
}
static ERTS_INLINE int
get_pollset_id(ErtsSysFdType fd)
{
return fd_hash(fd) % erts_no_pollsets;
}
static ERTS_INLINE ErtsPollSet *
get_pollset(ErtsSysFdType fd)
{
return pollsetv[get_pollset_id(fd)];
}
#if ERTS_POLL_USE_FALLBACK
static ERTS_INLINE ErtsPollSet *
get_fallback_pollset(void)
{
return flbk_pollset;
}
#endif
#if ERTS_POLL_USE_SCHEDULER_POLLING
static ERTS_INLINE ErtsPollSet *
get_scheduler_pollset(void)
{
return sched_pollset;
}
#endif
/*
* Place a fd within a pollset. This will automatically use
* the fallback ps if needed and update the scheduler pollset
* if used.
*
* Note that for simplicity this function does not add a new fd to
* the scheduler pollset. That is done by the functions that manage
* the pollset migration.
*
*/
static ERTS_INLINE ErtsPollEvents
erts_io_control_wakeup(ErtsDrvEventState *state, ErtsPollOp op,
ErtsPollEvents pe, int *wake_poller)
{
ErtsSysFdType fd = state->fd;
ErtsPollEvents res = 0;
EventStateFlags flags = state->flags;
ERTS_LC_ASSERT(erts_lc_mtx_is_locked(fd_mtx(state->fd)));
if (!(flags & ERTS_EV_FLAG_FALLBACK)) {
#if ERTS_POLL_USE_SCHEDULER_POLLING
if (flags & ERTS_EV_FLAG_SCHEDULER) {
ASSERT(op != ERTS_POLL_OP_ADD);
if (op == ERTS_POLL_OP_MOD && (flags & ERTS_EV_FLAG_IN_SCHEDULER) && (pe & ERTS_POLL_EV_IN)) {
/* The FD already is enabled in scheduler pollset and we are trying to re-insert */
res |= ERTS_POLL_EV_IN;
} else if (op == ERTS_POLL_OP_MOD && !(flags & ERTS_EV_FLAG_IN_SCHEDULER) && !(pe & ERTS_POLL_EV_IN)) {
/* The FD already is disabled in scheduler pollset and we are trying to disable it */
res |= 0;
} else {
res |= erts_poll_control(get_scheduler_pollset(), fd, op, pe & ERTS_POLL_EV_IN, wake_poller);
if (op == ERTS_POLL_OP_DEL) {
state->flags &= ~(ERTS_EV_FLAG_SCHEDULER|ERTS_EV_FLAG_IN_SCHEDULER);
} else if (pe & ERTS_POLL_EV_IN) {
state->flags |= ERTS_EV_FLAG_IN_SCHEDULER;
} else {
state->flags &= ~ERTS_EV_FLAG_IN_SCHEDULER;
}
}
pe &= ~ERTS_POLL_EV_IN;
if (state->flags & ERTS_EV_FLAG_IN_SCHEDULER) {
ERTS_ASSERT(state->active_events & ERTS_POLL_EV_IN);
} else {
ERTS_ASSERT(!(state->active_events & ERTS_POLL_EV_IN));
}
}
#endif
res |= erts_poll_control(get_pollset(fd), fd, op, pe, wake_poller);
#if ERTS_POLL_USE_FALLBACK
if (op == ERTS_POLL_OP_ADD && res == ERTS_POLL_EV_NVAL) {
/* When an add fails with NVAL, the poll/kevent operation could not
put that fd in the pollset, so we instead put it into a fallback pollset */
state->flags |= ERTS_EV_FLAG_FALLBACK;
res = erts_poll_control_flbk(get_fallback_pollset(), fd, op, pe, wake_poller);
}
} else {
ASSERT(op != ERTS_POLL_OP_ADD);
res = erts_poll_control_flbk(get_fallback_pollset(), fd, op, pe, wake_poller);
#endif
}
return res;
}
static ERTS_INLINE ErtsPollEvents
erts_io_control(ErtsDrvEventState *state, ErtsPollOp op, ErtsPollEvents pe)
{
int wake_poller = 0;
return erts_io_control_wakeup(state, op, pe, &wake_poller);
}
/* ToDo: Was inline in erl_check_io.h but now need struct erts_poll_thread */
void
erts_io_notify_port_task_executed(ErtsPortTaskType type,
ErtsPortTaskHandle *pthp,
void (*reset_handle)(ErtsPortTaskHandle *))
{
ErtsIoTask *itp = ErtsContainerStruct(pthp, ErtsIoTask, task);
ErtsSysFdType fd = itp->fd;
erts_mtx_t *mtx = fd_mtx(fd);
int active_events, new_events = 0;
ErtsDrvEventState *state;
ErtsDrvSelectDataState *free_select = NULL;
ErtsNifSelectDataState *free_nif = NULL;
ERTS_MSACC_PUSH_AND_SET_STATE_M_X(ERTS_MSACC_STATE_CHECK_IO);
erts_mtx_lock(mtx);
state = get_drv_ev_state(fd);
reset_handle(pthp);
active_events = state->active_events;
if (state->type == ERTS_EV_TYPE_DRV_SEL) {
switch (type) {
case ERTS_PORT_TASK_INPUT:
DEBUG_PRINT_FD("executed ready_input", state);
if (!(active_events & ERTS_POLL_EV_IN)
&& (state->events & ERTS_POLL_EV_IN)) {
ASSERT(!(state->flags & ERTS_EV_FLAG_SCHEDULER) &&
"active_events should always have ERTS_POLL_EV_IN if we are in scheduler");
active_events |= ERTS_POLL_EV_IN;
#if ERTS_POLL_USE_SCHEDULER_POLLING
if (erts_sched_poll_enabled() &&
!(state->flags & ERTS_EV_FLAG_FALLBACK) &&
state->count++ > 10) {
int wake_poller = 0;
DEBUG_PRINT_FD("moving to scheduler ps", state);
new_events = erts_poll_control(get_scheduler_pollset(), fd, ERTS_POLL_OP_ADD,
ERTS_POLL_EV_IN, &wake_poller);
state->flags |= ERTS_EV_FLAG_SCHEDULER|ERTS_EV_FLAG_IN_SCHEDULER;
state->active_events = active_events;
} else
#endif
{
new_events = active_events;
}
}
break;
case ERTS_PORT_TASK_OUTPUT:
DEBUG_PRINT_FD("executed ready_output", state);
if (!(active_events & ERTS_POLL_EV_OUT)
&& (state->events & ERTS_POLL_EV_OUT)) {
active_events |= ERTS_POLL_EV_OUT;
new_events = active_events;
}
break;
default:
erts_exit(ERTS_ABORT_EXIT, "Invalid IO port task type");
break;
}
if (state->active_events != active_events) {
ASSERT(new_events);
state->active_events = active_events;
new_events = erts_io_control(state, ERTS_POLL_OP_MOD, new_events);
}
/* We were unable to re-insert the fd into the pollset, signal the callback. */
if (new_events & ERTS_POLL_EV_NVAL) {
if (state->active_events & ERTS_POLL_EV_IN)
iready(state->driver.select->inport, state);
if (state->active_events & ERTS_POLL_EV_OUT)
oready(state->driver.select->outport, state);
state->active_events = 0;
active_events = 0;
#if ERTS_POLL_USE_SCHEDULER_POLLING
/* The error has only happened for scheduler or normal pollset,
so when cleaning up we should also delete it from the other pollset.
*/
erts_io_control(state, ERTS_POLL_OP_DEL, 0);
#endif
}
}
if (!active_events)
check_fd_cleanup(state, &free_select, &free_nif);
erts_mtx_unlock(mtx);
if (free_select)
free_drv_select_data(free_select);
if (free_nif)
free_nif_select_data(free_nif);
ERTS_MSACC_POP_STATE_M_X();
}
static ERTS_INLINE void
abort_task(Eterm id, ErtsPortTaskHandle *pthp, EventStateType type)
{
if (is_not_nil(id) && erts_port_task_is_scheduled(pthp)) {
erts_port_task_abort(pthp);
ASSERT(erts_is_port_alive(id));
}
}
static ERTS_INLINE void
abort_tasks(ErtsDrvEventState *state, int mode)
{
switch (mode) {
case 0: check_type:
switch (state->type) {
case ERTS_EV_TYPE_NIF:
case ERTS_EV_TYPE_NONE:
return;
default:
ASSERT(state->type == ERTS_EV_TYPE_DRV_SEL);
}
ERTS_FALLTHROUGH();
case ERL_DRV_READ|ERL_DRV_WRITE:
case ERL_DRV_WRITE:
ASSERT(state->type == ERTS_EV_TYPE_DRV_SEL);
abort_task(state->driver.select->outport,
&state->driver.select->outiotask.task,
state->type);
if (mode == ERL_DRV_WRITE)
break;
ERTS_FALLTHROUGH();
case ERL_DRV_READ:
ASSERT(state->type == ERTS_EV_TYPE_DRV_SEL);
abort_task(state->driver.select->inport,
&state->driver.select->iniotask.task,
state->type);
break;
default:
goto check_type;
}
}
typedef struct {
Eterm message;
ErlNifEvent evt;
} ErtsNifSelectSignalData;
static void prepare_select_msg(struct erts_nif_select_event* e,
ErlNifEvent evt,
enum ErlNifSelectFlags mode,
Eterm recipient,
ErtsResource* resource,
Eterm msg,
ErlNifEnv* msg_env,
Eterm event_atom)
{
ErtsMessage* mp;
ErtsNifSelectSignalData* xsig;
Eterm* hp, *hp_start;
Uint hsz = sizeof(ErtsNifSelectSignalData) / sizeof(Eterm); /* size of msg ptr */
clear_select_event(e);
if (mode & ERL_NIF_SELECT_CUSTOM_MSG) {
if (msg_env) {
mp = erts_create_message_from_nif_env(msg_env, hsz);
ERL_MESSAGE_TERM(mp) = msg;
hp = &mp->hfrag.mem[0];
hp_start = hp;
}
else {
Eterm msgsz = size_object(msg);
hsz += msgsz;
mp = erts_alloc_message(hsz, &hp);
hp_start = hp;
ERL_MESSAGE_TERM(mp) = copy_struct(msg, msgsz, &hp, &mp->hfrag.off_heap);
}
}
else {
ErtsBinary* bin;
Eterm resource_term, ref_term, tuple;
/* {select, Resource, Ref, EventAtom} */
hsz += 5 + ERTS_MAGIC_REF_THING_SIZE;
if (is_internal_ref(msg))
hsz += ERTS_REF_THING_SIZE;
else
ASSERT(is_immed(msg));
mp = erts_alloc_message(hsz, &hp);
hp_start = hp;
bin = ERTS_MAGIC_BIN_FROM_UNALIGNED_DATA(resource);
resource_term = erts_mk_magic_ref(&hp, &mp->hfrag.off_heap, &bin->binary);
if (is_internal_ref(msg)) {
Uint32* refn = internal_ref_numbers(msg);
write_ref_thing(hp, refn[0], refn[1], refn[2]);
ref_term = make_internal_ref(hp);
hp += ERTS_REF_THING_SIZE;
}
else {
ASSERT(is_immed(msg));
ref_term = msg;
}
tuple = TUPLE4(hp, am_select, resource_term, ref_term, event_atom);
hp += 5;
ERL_MESSAGE_TERM(mp) = tuple;
ASSERT(hp == hp_start + hsz - sizeof(ErtsNifSelectSignalData)/sizeof(Eterm));
}
mp->hfrag.used_size = hp - hp_start;
if (mode & ERL_NIF_SELECT_READ && ERTS_POLL_USE_SCHEDULER_POLLING) {
/* Save original msg ptr */
xsig = (ErtsNifSelectSignalData*)hp;
xsig->message = ERL_MESSAGE_TERM(mp);
xsig->evt = evt;
/* Setting to THE_NON_VALUE so that
erts_proc_sig_send_nif_select puts proper signal tag here */
ERL_MESSAGE_TERM(mp) = THE_NON_VALUE;
}
ASSERT(is_not_nil(recipient));
e->pid = recipient;
e->mp = mp;
}
#if ERTS_POLL_USE_SCHEDULER_POLLING
static void
erts_io_clear_nif_select(ErtsSysFdType fd, ErtsDrvEventState *state) {
erts_mtx_t *mtx = NULL;
if (!state) {
mtx = fd_mtx(fd);
erts_mtx_lock(mtx);
state = get_drv_ev_state(fd);
}
if (state->flags & ERTS_EV_FLAG_NIF_SELECT) {
DEBUG_PRINT_FD("Clear ERTS_EV_FLAG_NIF_SELECT (%d) on sched %d", state,
erts_atomic_read_nob(&erts_port_task_outstanding_io_tasks),
erts_get_scheduler_id());
state->flags &= ~ERTS_EV_FLAG_NIF_SELECT;
erts_port_task_dec_outstanding_io_tasks();
}
if (mtx)
erts_mtx_unlock(mtx);
}
void
erts_io_clear_nif_select_handles(ErtsSchedulerData *esdp) {
for (int i = 0; i < sizeof(esdp->nif_select_fds) / sizeof(ErtsSysFdType); i++) {
if (esdp->nif_select_fds[i] != ERTS_SYS_FD_INVALID) {
DEBUG_PRINT("%d: Clear in sched %d's state", esdp->nif_select_fds[i], esdp->no);
erts_io_clear_nif_select(esdp->nif_select_fds[i], NULL);
esdp->nif_select_fds[i] = ERTS_SYS_FD_INVALID;
}
}
}
#endif
#if ERTS_POLL_USE_SCHEDULER_POLLING
Eterm
erts_io_handle_nif_select(ErtsMessage *sig) {
ErtsNifSelectSignalData* xsig = (ErtsNifSelectSignalData*) (char *) (&sig->hfrag.mem[0]
+ sig->hfrag.used_size);
ASSERT(sig->hfrag.used_size < sig->hfrag.alloc_size);
if (erts_sched_poll_enabled()) {
ErtsSchedulerData *esdp = erts_get_scheduler_data();
for (int i = 0; i < sizeof(esdp->nif_select_fds) / sizeof(ErtsSysFdType); i++) {
if (esdp->nif_select_fds[i] == ERTS_SYS_FD_INVALID) {
DEBUG_PRINT("%d: Put in sched %d's state", xsig->evt, esdp->no);
esdp->nif_select_fds[i] = xsig->evt;
goto done;
}
}
/* There were no slots left, clear the flag and reset the poll counter */
erts_io_clear_nif_select(xsig->evt, NULL);
}
done:
return xsig->message;
}
#endif
static ERTS_INLINE void send_select_msg(struct erts_nif_select_event* e)
{
#if ERTS_POLL_USE_SCHEDULER_POLLING
if (ERL_MESSAGE_TERM(e->mp) == THE_NON_VALUE) {
/* If message term is THE_NON_VALUE this should be sent as a proc signal */
if (!erts_proc_sig_send_nif_select(e->pid, e->mp)) {
ErtsNifSelectSignalData* xsig = (ErtsNifSelectSignalData*) (char *) (&e->mp->hfrag.mem[0]
+ e->mp->hfrag.used_size);
erts_io_clear_nif_select(xsig->evt, NULL);
clear_select_event(e);
}
} else
#endif
{
/* Otherwise send as normal message */
Process* rp = erts_proc_lookup(e->pid);
ASSERT(is_value(ERL_MESSAGE_TERM(e->mp)));
if (!rp) {
clear_select_event(e);
return;
}
erts_queue_message(rp, 0, e->mp, ERL_MESSAGE_TERM(e->mp), am_system);
}
}
static void clear_select_event(struct erts_nif_select_event* e)
{
if (is_not_nil(e->pid)) {
/* Discard unsent message */
ASSERT(e->mp);
if (ERL_MESSAGE_TERM(e->mp) == THE_NON_VALUE) {
ErtsNifSelectSignalData* xsig = (ErtsNifSelectSignalData*) (char *) (&e->mp->hfrag.mem[0]
+ e->mp->hfrag.used_size);
ERL_MESSAGE_TERM(e->mp) = xsig->message;
}
erts_cleanup_messages(e->mp);
e->mp = NULL;
e->pid = NIL;
}
}
static void
deselect(ErtsDrvEventState *state, int mode)
{
ErtsPollEvents rm_events;
ERTS_LC_ASSERT(erts_lc_mtx_is_locked(fd_mtx(state->fd)));
abort_tasks(state, mode);
if (!mode) {
rm_events = state->events;
} else {
rm_events = 0;
ASSERT(state->type == ERTS_EV_TYPE_DRV_SEL);
if (mode & ERL_DRV_READ) {
state->driver.select->inport = NIL;
rm_events |= ERTS_POLL_EV_IN;
}
if (mode & ERL_DRV_WRITE) {
state->driver.select->outport = NIL;
rm_events |= ERTS_POLL_EV_OUT;
}
}
state->events &= ~rm_events;
state->active_events &= ~rm_events;
if (!(state->events)) {
erts_io_control(state, ERTS_POLL_OP_DEL, 0);
switch (state->type) {
case ERTS_EV_TYPE_NIF:
#if ERTS_POLL_USE_SCHEDULER_POLLING
erts_io_clear_nif_select(state->fd, state);
#endif
clear_select_event(&state->driver.nif->in);
clear_select_event(&state->driver.nif->out);
clear_select_event(&state->driver.nif->err);
enif_release_resource(state->driver.stop.resource->data);
state->driver.stop.resource = NULL;
break;
case ERTS_EV_TYPE_DRV_SEL:
state->driver.select->inport = NIL;
state->driver.select->outport = NIL;
break;
case ERTS_EV_TYPE_NONE:
break;
default:
ASSERT(0);
break;
}
state->type = ERTS_EV_TYPE_NONE;
state->flags = ERTS_EV_FLAG_CLEAR;
state->count = 0;
state->last_select_pid = NIL;
} else {
ErtsPollEvents new_events =
erts_io_control(state, ERTS_POLL_OP_MOD, state->active_events);
/* We were unable to re-insert the fd into the pollset, signal the callback. */
if (new_events & ERTS_POLL_EV_NVAL) {
if (state->active_events & ERTS_POLL_EV_IN)
iready(state->driver.select->inport, state);
if (state->active_events & ERTS_POLL_EV_OUT)
oready(state->driver.select->outport, state);
state->active_events = 0;
}
}
}
#ifdef ERTS_SYS_CONTINOUS_FD_NUMBERS
# define IS_FD_UNKNOWN(state) ((state)->type == ERTS_EV_TYPE_NONE)
#else
# define IS_FD_UNKNOWN(state) ((state) == NULL)
#endif
static ERTS_INLINE void
check_fd_cleanup(ErtsDrvEventState *state,
ErtsDrvSelectDataState **free_select,
ErtsNifSelectDataState **free_nif)
{
ERTS_LC_ASSERT(erts_lc_mtx_is_locked(fd_mtx(state->fd)));
*free_select = NULL;
if (state->driver.select
&& (state->type != ERTS_EV_TYPE_DRV_SEL)
&& !is_iotask_active(&state->driver.select->iniotask)
&& !is_iotask_active(&state->driver.select->outiotask)) {
*free_select = state->driver.select;
state->driver.select = NULL;
}
*free_nif = NULL;
if (state->driver.nif && (state->type != ERTS_EV_TYPE_NIF)) {
*free_nif = state->driver.nif;
state->driver.nif = NULL;
}
if (((state->type != ERTS_EV_TYPE_NONE)
| (state->driver.nif != NULL)
| (state->driver.select != NULL)) == 0) {
state->flags = ERTS_EV_FLAG_CLEAR;
erase_drv_ev_state(state);
}
}
#ifdef __WIN32__
# define MUST_DEFER(MAY_SLEEP) 1
#else
# define MUST_DEFER(MAY_SLEEP) (MAY_SLEEP)
#endif
int
driver_select(ErlDrvPort ix, ErlDrvEvent e, int mode, int on)
{
void (*stop_select_fn)(ErlDrvEvent, void*) = NULL;
Port *prt = erts_drvport2port(ix);
Eterm id = erts_drvport2id(ix);
ErtsSysFdType fd = (ErtsSysFdType) e;
ErtsPollEvents ctl_events = (ErtsPollEvents) 0;
ErtsPollEvents old_events;
ErtsPollEvents new_events;
ErtsPollOp ctl_op = ERTS_POLL_OP_MOD;
ErtsDrvEventState *state;
int wake_poller = 0;
int ret;