-
Notifications
You must be signed in to change notification settings - Fork 3
/
fabtget.c
4941 lines (4055 loc) · 138 KB
/
fabtget.c
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
/**
* Copyright (c) 2021-2022, UChicago Argonne, LLC and The HDF Group.
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <assert.h>
#include <err.h>
#include <inttypes.h> /* PRIu32 */
#include <libgen.h> /* basename(3) */
#include <limits.h> /* INT_MAX */
#include <sched.h> /* CPU_SET(3) */
#include <signal.h>
#include <stdalign.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strcmp(3), strdup(3) */
#include <unistd.h> /* getopt(3), sysconf(3) */
#include <sys/epoll.h>
#include <rdma/fabric.h>
#include <rdma/fi_cm.h> /* fi_listen, fi_getname */
#include <rdma/fi_domain.h>
#include <rdma/fi_endpoint.h>
#include <rdma/fi_rma.h> /* struct fi_msg_rma */
#include <rdma/fi_tagged.h> /* struct fi_msg_tagged */
#include "hlog.h"
#define arraycount(a) (sizeof(a) / sizeof(a[0]))
#ifndef transfer_unused
#define transfer_unused __attribute__((unused))
#endif
/*
* Message definitions
*/
typedef struct {
uint64_t bits[2];
} nonce_t;
typedef struct initial_msg {
nonce_t nonce;
uint32_t nsources;
uint32_t id;
uint32_t addrlen;
char addr[512];
} initial_msg_t;
typedef struct ack_msg {
uint32_t addrlen;
char addr[512];
} ack_msg_t;
typedef struct vector_msg {
uint32_t niovs;
uint32_t pad;
struct {
uint64_t addr, len, key;
} iov[12];
} vector_msg_t;
typedef struct progress_msg {
uint64_t nfilled;
uint64_t nleftover;
} progress_msg_t;
/* Communication buffers */
typedef enum {
xft_ack,
xft_fragment,
xft_initial,
xft_progress,
xft_rdma_write,
xft_vector
} xfc_type_t;
typedef enum { xfp_first = 0x1, xfp_last = 0x2 } xfc_place_t;
typedef enum { xfo_program = 0, xfo_nic = 1 } xfc_owner_t;
typedef struct {
struct fi_context ctx; // this has to be the first member
uint32_t type : 4;
uint32_t owner : 1;
uint32_t place : 2;
uint32_t nchildren : 8;
uint32_t cancelled : 1;
uint32_t unused : 16;
} xfer_context_t;
typedef struct completion {
uint64_t flags;
size_t len;
xfer_context_t *xfc;
} completion_t;
typedef struct bufhdr {
xfer_context_t xfc;
uint64_t raddr;
size_t nused;
size_t nallocated;
struct fid_mr *mr;
void *desc;
uint64_t tag;
struct fid_ep *ep;
max_align_t pad;
} bufhdr_t;
typedef struct fragment {
bufhdr_t hdr;
bufhdr_t *parent;
} fragment_t;
typedef struct bytebuf {
bufhdr_t hdr;
char payload[];
} bytebuf_t;
typedef struct progbuf {
bufhdr_t hdr;
progress_msg_t msg;
} progbuf_t;
typedef struct vecbuf {
bufhdr_t hdr;
vector_msg_t msg;
} vecbuf_t;
typedef struct fifo {
uint64_t insertions;
uint64_t removals;
size_t index_mask; // for some integer n > 0, 2^n - 1 == index_mask
uint64_t closed; /* close position: no insertions or removals may
* take place at or after this position.
*/
bufhdr_t *hdr[];
} fifo_t;
typedef struct buflist {
uint64_t access;
size_t nfull;
size_t nallocated;
bufhdr_t *buf[];
} buflist_t;
/* Communication terminals: sources and sinks */
typedef enum {
loop_continue,
loop_end,
loop_error,
loop_canceled
} loop_control_t;
struct terminal;
typedef struct terminal terminal_t;
struct terminal {
/* trade(t, ready, completed) */
loop_control_t (*trade)(terminal_t *, fifo_t *, fifo_t *);
};
typedef struct {
terminal_t terminal;
size_t idx;
size_t txbuflen;
size_t entirelen;
} sink_t;
typedef struct {
terminal_t terminal;
size_t idx;
size_t txbuflen;
size_t entirelen;
} source_t;
typedef struct seqsource {
uint64_t next_key;
} seqsource_t;
/*
* Communications state definitions
*/
struct cxn;
typedef struct cxn cxn_t;
struct session;
typedef struct session session_t;
struct worker;
typedef struct worker worker_t;
typedef struct {
bool local, remote;
} eof_state_t;
struct cxn {
uint32_t magic;
loop_control_t (*loop)(worker_t *, session_t *);
void (*shutdown)(cxn_t *);
void (*cancel)(cxn_t *);
bool (*cancellation_complete)(cxn_t *);
struct fid_ep *ep;
fi_addr_t peer_addr;
struct fid_cq *cq;
int cq_wait_fd; /* if we're using FI_WAIT_FD, the descriptor
* to use with epoll(2) to sleep until I/O is
* ready
*/
struct fid_av *av;
session_t *parent; // pointer to the connection's current session_t
bool sent_first; /* receiving: set to `true` once this receiver sends an
* acknowledgement for the transmitter's original
* message
*
* transmitting: set to `true` once this transmitter
* sends an initial message to its peer
*/
bool cancelled;
bool ended;
loop_control_t end_reason;
bool started;
/* Receiver needs to send an empty vector.msg.niovs == 0 to close,
* sender needs to send progress.msg.nleftover == 0, record having
* received the remote close in `eof.remote` and having completed
* sending it in `eof.local`.
*/
eof_state_t eof;
seqsource_t keys;
};
typedef struct {
fifo_t *posted; // buffers posted for vector messages
fifo_t *rcvd; // buffers holding received vector messages
seqsource_t tags;
uint64_t ignore;
} rxctl_t;
typedef struct {
fifo_t *ready; // message buffers ready to transmit
fifo_t *posted; // buffers posted with messages
buflist_t *pool; // unused buffers
seqsource_t tags;
uint64_t ignore;
unsigned rotate_ready_countdown; // counts down to 0, then resets
// to rotate_ready_interval
} txctl_t;
typedef struct {
cxn_t cxn;
uint64_t nfull;
fifo_t *tgtposted; // posted RDMA target buffers in order of issuance
struct {
xfer_context_t xfc;
struct iovec iov[12];
void *desc[12];
struct fid_mr *mr[12];
uint64_t raddr[12];
ssize_t niovs;
ack_msg_t msg;
} ack;
struct {
struct iovec iov[12];
void *desc[12];
struct fid_mr *mr[12];
uint64_t raddr[12];
ssize_t niovs;
initial_msg_t msg;
} initial;
txctl_t vec;
rxctl_t progress;
unsigned split_vector_countdown; // counts down to 0, then resets
// to split_vector_interval
} rcvr_t;
typedef struct {
cxn_t cxn;
fifo_t *wrposted; // posted RDMA writes in order of issuance
size_t bytes_progress;
rxctl_t vec;
txctl_t progress;
struct {
xfer_context_t xfc;
void *desc;
struct fid_mr *mr;
initial_msg_t msg;
} initial;
struct {
xfer_context_t xfc;
void *desc;
struct fid_mr *mr;
ack_msg_t msg;
} ack;
struct {
struct iovec iov[12];
void *desc[12];
struct iovec iov2[12];
void *desc2[12];
struct fid_mr *mr;
} payload;
struct {
buflist_t *pool; // unused fragment headers
size_t offset; // offset into buffer at head of ready_for_cxn
} fragment;
struct fi_rma_iov riov[12], riov2[12];
size_t nriovs;
size_t next_riov;
bool phase;
bool rcvd_ack; /* set to `true` once this transmitter receives
* an acknowledgement from its peer for the initial
* message
*/
unsigned split_progress_countdown; // counts down to 0, then resets
// to split_progress_interval
} xmtr_t;
/* On each loop, a worker checks its poll set for any completions.
* If `loops_since_mark < UINT16_MAX`, a worker increases it by
* one, and increases `ctxs_serviced_since_mark` by the number
* of queues that actually held one or more completions. If
* `loops_since_mark == UINT16_MAX`, then a worker updates
* `average`, `average = (average + 256 * ctxs_serviced_since_mark /
* (UINT16_MAX + 1)) / 2`, and resets `loops_since_mark` and
* `ctxs_serviced_since_mark` to 0.
*/
typedef struct {
/* A fixed-point number with 8 bits right of the decimal point: */
volatile atomic_uint_fast16_t average;
uint_fast16_t loops_since_mark;
uint32_t ctxs_serviced_since_mark;
int max_loop_contexts;
int min_loop_contexts;
} load_t;
#define WORKER_SESSIONS_MAX 8
#define WORKERS_MAX 128
#define SESSIONS_MAX (WORKER_SESSIONS_MAX * WORKERS_MAX)
struct session {
terminal_t *terminal;
cxn_t *cxn;
fifo_t *ready_for_cxn;
fifo_t *ready_for_terminal;
bool waitable;
};
typedef struct worker_stats worker_stats_t;
struct worker_stats {
struct {
uint64_t waitable;
uint64_t total;
} epoll_loops;
struct {
uint64_t no_io_ready;
uint64_t no_session_ready;
uint64_t total;
} half_loops;
};
struct worker {
pthread_t thd;
sigset_t epoll_sigset;
load_t load;
terminal_t *term[WORKER_SESSIONS_MAX];
session_t session[WORKER_SESSIONS_MAX];
volatile _Atomic size_t nsessions[2]; // number of sessions in each half
// of session[]
struct fid_poll *pollset[2];
pthread_mutex_t mtx[2]; /* mtx[0] protects pollset[0] and the first half
* of session[]; mtx[1], pollset[1] and the second
* half
*/
pthread_cond_t sleep; /* Used in conjunction with workers_mtx. */
volatile atomic_bool shutting_down;
volatile atomic_bool canceled;
bool failed;
bool pollable;
struct {
buflist_t *tx;
buflist_t *rx;
} paybufs; /* Reservoirs for free payload buffers. */
seqsource_t keys;
worker_stats_t stats;
int epoll_fd; /* returned by epoll_create(2) */
};
typedef struct {
struct fi_context ctx; // this has to be the first member
sink_t sink;
rcvr_t rcvr;
session_t sess;
} get_session_t;
typedef struct {
struct fid_ep *listen_ep;
struct fid_cq *listen_cq;
struct fid_av *av;
get_session_t *session;
} get_state_t;
typedef struct {
source_t source;
xmtr_t xmtr;
session_t sess;
} put_session_t;
typedef struct {
struct fid_av *av;
put_session_t *session;
fi_addr_t peer_addr;
} put_state_t;
typedef int (*personality_t)(void);
typedef struct {
struct fid_domain *domain;
struct fid_fabric *fabric;
struct fi_info *info;
size_t mr_maxsegs;
size_t rx_maxsegs;
size_t tx_maxsegs;
size_t rma_maxsegs;
seqsource_t keys;
bool contiguous;
bool expect_cancellation;
bool reregister;
bool waitfd;
bool mr_endpoint;
size_t local_sessions;
size_t total_sessions;
personality_t personality;
int nextcpu;
struct {
unsigned first, last;
} processors;
volatile bool cancelled;
pthread_t cancel_thd;
pthread_t main_thd;
char peer_addr_buf[256];
char *peer_addr;
char *address_filename;
} state_t;
HLOG_OUTLET_MEDIUM_DEFN(err, all, 0, HLOG_OUTLET_S_ON);
HLOG_OUTLET_SHORT_DEFN(average, all);
HLOG_OUTLET_SHORT_DEFN(close, all);
HLOG_OUTLET_SHORT_DEFN(signal, all);
HLOG_OUTLET_SHORT_DEFN(noisy_params, all);
HLOG_OUTLET_SHORT_DEFN(params, noisy_params);
HLOG_OUTLET_SHORT_DEFN(tx_start, all);
HLOG_OUTLET_SHORT_DEFN(session_loop, all);
HLOG_OUTLET_SHORT_DEFN(write, all);
HLOG_OUTLET_SHORT_DEFN(rxctl, all);
HLOG_OUTLET_SHORT_DEFN(protocol, all);
HLOG_OUTLET_SHORT_DEFN(proto_vector, protocol);
HLOG_OUTLET_SHORT_DEFN(proto_progress, protocol);
HLOG_OUTLET_SHORT_DEFN(txctl, all);
HLOG_OUTLET_SHORT_DEFN(txdefer, all);
HLOG_OUTLET_SHORT_DEFN(memreg, all);
HLOG_OUTLET_SHORT_DEFN(msg, all);
HLOG_OUTLET_SHORT_DEFN(payverify, all);
HLOG_OUTLET_FLAGS_DEFN(payload, all, HLOG_F_NO_PREFIX | HLOG_F_NO_SUFFIX);
HLOG_OUTLET_SHORT_DEFN(paybuf, all);
HLOG_OUTLET_SHORT_DEFN(paybuflist, paybuf);
HLOG_OUTLET_SHORT_DEFN(completion, all);
HLOG_OUTLET_SHORT_DEFN(worker_stats, all);
HLOG_OUTLET_SHORT_DEFN(multitx, all);
HLOG_OUTLET_SHORT_DEFN(session, all);
HLOG_OUTLET_SHORT_DEFN(ooo, all);
HLOG_OUTLET_SHORT_DEFN(addr, all);
HLOG_OUTLET_SHORT_DEFN(poll, all);
HLOG_OUTLET_SHORT_DEFN(leak, all);
static const unsigned split_progress_interval = 2047;
static const unsigned split_vector_interval = 15;
static const unsigned rotate_ready_interval = 3;
static state_t global_state = {.domain = NULL,
.fabric = NULL,
.info = NULL,
.personality = NULL,
.local_sessions = 1,
.total_sessions = 1,
.processors = {.first = 0, .last = INT_MAX},
.cancelled = 0,
.peer_addr = NULL};
static pthread_mutex_t workers_mtx = PTHREAD_MUTEX_INITIALIZER;
static worker_t workers[WORKERS_MAX];
static _Atomic size_t nworkers_running;
static size_t nworkers_allocated;
static pthread_cond_t nworkers_cond = PTHREAD_COND_INITIALIZER;
static bool workers_assignment_suspended = false;
static struct {
int signum;
struct sigaction saved_action;
} siglist[] = {{.signum = SIGHUP},
{.signum = SIGINT},
{.signum = SIGQUIT},
{.signum = SIGTERM}};
static struct sigaction saved_wakeup1_action;
static struct sigaction saved_wakeup2_action;
static const struct {
uint64_t rx;
uint64_t tx;
} payload_access = {.rx = FI_RECV | FI_REMOTE_WRITE, .tx = FI_SEND};
static int
get(void);
static const char *
xfc_type_to_string(xfc_type_t t)
{
switch (t) {
case xft_ack:
return "ack";
case xft_fragment:
return "fragment";
case xft_initial:
return "initial";
case xft_progress:
return "progress";
case xft_rdma_write:
return "rdma_write";
case xft_vector:
return "vector";
default:
return "<unknown>";
}
}
static char *
completion_flags_to_string(const uint64_t flags, char *const buf,
const size_t bufsize)
{
char *next = buf;
static const struct {
uint64_t flag;
const char *name;
} flag_to_name[] = {
{.flag = FI_RECV, .name = "recv"},
{.flag = FI_SEND, .name = "send"},
{.flag = FI_MSG, .name = "msg"},
{.flag = FI_RMA, .name = "rma"},
{.flag = FI_WRITE, .name = "write"},
{.flag = FI_TAGGED, .name = "tagged"},
{.flag = FI_COMPLETION, .name = "completion"},
{.flag = FI_DELIVERY_COMPLETE, .name = "delivery complete"}};
size_t i;
size_t bufleft = bufsize;
uint64_t found = 0, residue;
const char *delim = "<";
if (bufsize < 1)
return NULL;
buf[0] = '\0';
for (i = 0; i < arraycount(flag_to_name); i++) {
uint64_t curflag = flag_to_name[i].flag;
const char *name = flag_to_name[i].name;
if ((flags & curflag) == 0)
continue;
found |= curflag;
int nprinted = snprintf(next, bufleft, "%s%s", delim, name);
delim = ",";
if (nprinted < 0 || (size_t) nprinted >= bufleft)
continue;
next += nprinted;
bufleft -= (size_t) nprinted;
}
residue = flags & ~found;
while (residue != 0) {
uint64_t oresidue = residue;
residue = residue & (residue - 1);
uint64_t lsb = oresidue ^ residue;
int nprinted = snprintf(next, bufleft, "%s0x%" PRIx64, delim, lsb);
delim = ",";
if (nprinted < 0 || (size_t) nprinted >= bufleft)
continue;
next += nprinted;
bufleft -= (size_t) nprinted;
}
if (next != buf)
(void) snprintf(next, bufleft, ">");
return buf;
}
static const uint64_t desired_rx_flags = FI_RECV | FI_MSG;
static const uint64_t desired_tagged_rx_flags = FI_RECV | FI_TAGGED;
static const uint64_t desired_tagged_tx_flags = FI_SEND | FI_TAGGED;
static uint64_t _Atomic next_key_pool = 512;
static char txbuf[] = "If this message was received in error then please "
"print it out and shred it.";
#define bailout_for_ofi_ret(ret, ...) \
bailout_for_ofi_ret_impl(ret, __func__, __LINE__, __VA_ARGS__)
#define warn_about_ofi_ret(ret, ...) \
warn_about_ofi_ret_impl(ret, __func__, __LINE__, __VA_ARGS__)
static void
warnv_about_ofi_ret_impl(int ret, const char *fn, int lineno, const char *fmt,
va_list ap)
{
fprintf(stderr, "%s.%d: ", fn, lineno);
vfprintf(stderr, fmt, ap);
fprintf(stderr, ": %s\n", fi_strerror(-ret));
}
static void
warn_about_ofi_ret_impl(int ret, const char *fn, int lineno, const char *fmt,
...)
{
va_list ap;
va_start(ap, fmt);
warnv_about_ofi_ret_impl(ret, fn, lineno, fmt, ap);
va_end(ap);
}
static void
bailout_for_ofi_ret_impl(int ret, const char *fn, int lineno, const char *fmt,
...)
{
va_list ap;
va_start(ap, fmt);
warnv_about_ofi_ret_impl(ret, fn, lineno, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
#if 0
static int
maxsize(size_t l, size_t r)
{
return (l > r) ? l : r;
}
#endif
static int
minsize(size_t l, size_t r)
{
return (l < r) ? l : r;
}
static inline bool
size_is_power_of_2(size_t size)
{
return ((size - 1) & size) == 0;
}
static fifo_t *
fifo_create(size_t size)
{
if (!size_is_power_of_2(size))
return NULL;
fifo_t *f = malloc(offsetof(fifo_t, hdr[0]) + sizeof(f->hdr[0]) * size);
if (f == NULL)
return NULL;
f->insertions = f->removals = 0;
f->index_mask = size - 1;
f->closed = UINT64_MAX;
return f;
}
/* Return true if the head of the FIFO (the removal point) is at or past
* the close position. Otherwise, return false.
*/
static inline bool
fifo_eoget(const fifo_t *f)
{
return f->closed <= f->removals;
}
/* Return true if the tail of the FIFO (the insertion point) is at or
* past the close position. Otherwise, return false.
*/
static inline bool
fifo_eoput(const fifo_t *f)
{
return f->closed <= f->insertions;
}
/* Set the close position to the current head of the FIFO (the removal
* point). Every `fifo_get` that follows will fail, and `fifo_eoget`
* will be true.
*/
static inline void
fifo_get_close(fifo_t *f)
{
assert(!fifo_eoget(f));
f->closed = f->removals;
}
/* Set the close position to the current tail of the FIFO (the insertion
* point). Every `fifo_put` that follows will fail, and `fifo_eoput`
* will be true.
*/
static inline void
fifo_put_close(fifo_t *f)
{
assert(!fifo_eoput(f));
f->closed = f->insertions;
}
static void
fifo_destroy(fifo_t *f)
{
free(f);
}
/* See `fifo_get`: this is a variant that does not respect the close
* position.
*/
static inline bufhdr_t *
fifo_alt_get(fifo_t *f)
{
assert(f->insertions >= f->removals);
if (f->insertions == f->removals)
return NULL;
bufhdr_t *h = f->hdr[f->removals & (uint64_t) f->index_mask];
f->removals++;
return h;
}
/* Return NULL if the FIFO is empty or if the FIFO has been read up to
* the close position. Otherwise, remove and return the next item on the
* FIFO.
*/
static inline bufhdr_t *
fifo_get(fifo_t *f)
{
if (fifo_eoget(f))
return NULL;
return fifo_alt_get(f);
}
/* See `fifo_empty`: this is a variant that does not respect the close
* position.
*/
static inline bool
fifo_alt_empty(fifo_t *f)
{
return f->insertions == f->removals;
}
/* Return true if the FIFO is empty or if the FIFO has been read up
* to the close position. Otherwise, return false.
*/
static inline bool
fifo_empty(fifo_t *f)
{
return fifo_eoget(f) || fifo_alt_empty(f);
}
static inline size_t
fifo_nfull(fifo_t *f)
{
return f->insertions - f->removals;
}
static inline size_t
fifo_nempty(fifo_t *f)
{
return f->index_mask + 1 - fifo_nfull(f);
}
/* Return NULL if the FIFO is empty or if the FIFO has been read up to
* the close position. Otherwise, return the next item on the FIFO without
* removing it.
*/
static inline bufhdr_t *
fifo_peek(fifo_t *f)
{
assert(f->insertions >= f->removals);
if (fifo_empty(f))
return NULL;
return f->hdr[f->removals & (uint64_t) f->index_mask];
}
/* See `fifo_full`: this is a variant that does not respect the close
* position.
*/
static inline bool
fifo_alt_full(fifo_t *f)
{
return f->insertions - f->removals == f->index_mask + 1;
}
/* Return true if the FIFO is full or if the FIFO has been written up
* to the close position. Otherwise, return false.
*/
static inline bool
fifo_full(fifo_t *f)
{
return fifo_eoput(f) || fifo_alt_full(f);
}
/* See `fifo_put`: this is a variant that does not respect the close
* position.
*/
static inline bool
fifo_alt_put(fifo_t *f, bufhdr_t *h)
{
assert(f->insertions - f->removals <= f->index_mask + 1);
if (f->insertions - f->removals > f->index_mask)
return false;
f->hdr[f->insertions & (uint64_t) f->index_mask] = h;
f->insertions++;
return true;
}
/* If the FIFO is full or if it has been written up to the close
* position, then return false without changing the FIFO.
* Otherwise, add item `h` to the tail of the FIFO.
*/
static inline bool
fifo_put(fifo_t *f, bufhdr_t *h)
{
if (fifo_eoput(f))
return false;
return fifo_alt_put(f, h);
}
static bufhdr_t *
buflist_get(buflist_t *bl)
{
if (bl->nfull == 0)
return NULL;
return bl->buf[--bl->nfull];
}
static bool
buflist_put(buflist_t *bl, bufhdr_t *h)
{
if (bl->nfull == bl->nallocated)
return false;
bl->buf[bl->nfull++] = h;
return true;
}
static bool
session_init(session_t *s, cxn_t *c, terminal_t *t)
{
memset(s, 0, sizeof(*s));
s->waitable = false;
s->cxn = c;
s->terminal = t;
if ((s->ready_for_cxn = fifo_create(64)) == NULL)
return NULL;
if ((s->ready_for_terminal = fifo_create(64)) == NULL) {
fifo_destroy(s->ready_for_cxn);
return NULL;
}
return s;
}
static void
seqsource_init(seqsource_t *s)
{
memset(s, 0, sizeof(*s));
}
static uint64_t
seqsource_get(seqsource_t *s)
{
if (s->next_key % 256 == 0) {
s->next_key = atomic_fetch_add_explicit(&next_key_pool, 256,
memory_order_relaxed);
}
return s->next_key++;
}
static bool
seqsource_unget(seqsource_t *s, uint64_t got)
{
if (got + 1 != s->next_key)
return false;
s->next_key--;
return true;
}
static bufhdr_t *
buf_alloc(size_t paylen)
{
bufhdr_t *h;
if ((h = calloc(1, offsetof(bytebuf_t, payload[0]) + paylen)) == NULL)
return NULL;
h->nallocated = paylen;
return h;
}
static void
buf_free(bufhdr_t *h)
{
free(h);
}
static bytebuf_t *
bytebuf_alloc(size_t paylen)
{
return (bytebuf_t *) buf_alloc(paylen);
}
static fragment_t *
fragment_alloc(void)
{
bufhdr_t *h;
fragment_t *f;
if ((h = buf_alloc(sizeof(*f) - sizeof(bufhdr_t))) == NULL)
return NULL;
f = (fragment_t *) h;
h->xfc.type = xft_fragment;
return f;
}
static progbuf_t *
progbuf_alloc(void)
{
bufhdr_t *h;
progbuf_t *pb;
if ((h = buf_alloc(sizeof(*pb) - sizeof(bufhdr_t))) == NULL)
return NULL;
pb = (progbuf_t *) h;
h->xfc.type = xft_progress;
return pb;
}
static vecbuf_t *
vecbuf_alloc(void)
{
bufhdr_t *h;
vecbuf_t *vb;
if ((h = buf_alloc(sizeof(*vb) - sizeof(bufhdr_t))) == NULL)
return NULL;
vb = (vecbuf_t *) h;
vb->msg.pad = 0;
h->xfc.type = xft_vector;
return vb;
}
static int
buf_mr_bind(bufhdr_t *h, struct fid_ep *ep)
{
int rc;
if (ep == NULL)
h->ep = NULL;