forked from openucx/ucx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucp_ep.c
More file actions
4859 lines (4119 loc) · 172 KB
/
Copy pathucp_ep.c
File metadata and controls
4859 lines (4119 loc) · 172 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
/**
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2020. ALL RIGHTS RESERVED.
* Copyright (C) Los Alamos National Security, LLC. 2019 ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "ucp_ep.h"
#include "ucp_worker.h"
#include "ucp_am.h"
#include "ucp_rkey.h"
#include "ucp_ep.inl"
#include "ucp_request.inl"
#include <ucp/wireup/wireup_ep.h>
#include <ucp/wireup/wireup.h>
#include <ucp/wireup/wireup_cm.h>
#include <ucp/tag/eager.h>
#include <ucp/tag/offload.h>
#include <ucp/proto/proto_common.h>
#include <ucp/proto/proto_common.inl>
#include <ucp/proto/proto_debug.h>
#include <ucp/rndv/rndv.h>
#include <ucp/stream/stream.h>
#include <ucp/core/ucp_listener.h>
#include <ucp/rma/rma.inl>
#include <ucp/rma/rma.h>
#include <ucs/datastruct/queue.h>
#include <ucs/type/init_once.h>
#include <ucs/debug/memtrack_int.h>
#include <ucs/debug/log.h>
#include <ucs/debug/debug_int.h>
#include <ucs/sys/string.h>
#include <ucs/sys/sock.h>
#include <ucs/vfs/base/vfs_obj.h>
#include <string.h>
__KHASH_IMPL(ucp_ep_peer_mem_hash, kh_inline, uint64_t,
ucp_ep_peer_mem_data_t, 1,
kh_int64_hash_func, kh_int64_hash_equal);
typedef struct {
double reg_growth;
double reg_overhead;
double overhead;
double latency;
size_t bw;
} ucp_ep_thresh_params_t;
/**
* Argument for the setting failed lanes of UCP endpoint
*/
typedef struct {
ucp_ep_h ucp_ep; /**< UCP endpoint which has failed lanes. */
ucp_lane_map_t lanes; /**< Bitmask of failed lanes. */
ucs_status_t status; /**< Failure status for failed lanes. */
} ucp_ep_set_lanes_failed_arg_t;
/**
* Argument for discarding UCP endpoint's lanes
*/
typedef struct ucp_ep_discard_lanes_arg {
uct_ep_t failed_ep;
/* How many discarding operations on UCT lanes are in-progress if purging of
the UCP endpoint is required */
unsigned discard_counter;
/* How many destroy operations on UCT will be called */
unsigned destroy_counter;
/* UCP endpoint which should be discarded */
ucp_ep_h ucp_ep;
/* Config to deactivate when discard completes */
ucp_worker_cfg_index_t deactivate_cfg_index;
/* Completion status of operations after discarding is * done */
ucs_status_t status;
} ucp_ep_discard_lanes_arg_t;
extern const ucp_request_send_proto_t ucp_stream_am_proto;
extern const ucp_request_send_proto_t ucp_am_proto;
extern const ucp_request_send_proto_t ucp_am_reply_proto;
#ifdef ENABLE_STATS
static ucs_stats_class_t ucp_ep_stats_class = {
.name = "ucp_ep",
.num_counters = UCP_EP_STAT_LAST,
.class_id = UCS_STATS_CLASS_ID_INVALID,
.counter_names = {
[UCP_EP_STAT_TAG_TX_EAGER] = "tx_eager",
[UCP_EP_STAT_TAG_TX_EAGER_SYNC] = "tx_eager_sync",
[UCP_EP_STAT_TAG_TX_RNDV] = "tx_rndv"
}
};
#endif
static ucs_status_t ucp_ep_failed_op(uct_ep_h ep);
static ssize_t ucp_ep_failed_bc_op(uct_ep_h ep);
static void ucp_ep_failed_destroy(uct_ep_h ep);
static void ucp_ep_recovery_arg_free(ucp_ep_h ep);
static uct_iface_h ucp_failed_tl_iface;
static ucs_init_once_t ucp_failed_tl_iface_once = UCS_INIT_ONCE_INITIALIZER;
static const uct_iface_ops_t ucp_failed_tl_iface_ops = {
.ep_put_short = (uct_ep_put_short_func_t)ucp_ep_failed_op,
.ep_put_bcopy = (uct_ep_put_bcopy_func_t)ucp_ep_failed_bc_op,
.ep_put_zcopy = (uct_ep_put_zcopy_func_t)ucp_ep_failed_op,
.ep_get_short = (uct_ep_get_short_func_t)ucp_ep_failed_op,
.ep_get_bcopy = (uct_ep_get_bcopy_func_t)ucp_ep_failed_op,
.ep_get_zcopy = (uct_ep_get_zcopy_func_t)ucp_ep_failed_op,
.ep_am_short = (uct_ep_am_short_func_t)ucp_ep_failed_op,
.ep_am_short_iov = (uct_ep_am_short_iov_func_t)ucp_ep_failed_op,
.ep_am_bcopy = (uct_ep_am_bcopy_func_t)ucp_ep_failed_bc_op,
.ep_am_zcopy = (uct_ep_am_zcopy_func_t)ucp_ep_failed_op,
.ep_atomic_cswap64 = (uct_ep_atomic_cswap64_func_t)ucp_ep_failed_op,
.ep_atomic_cswap32 = (uct_ep_atomic_cswap32_func_t)ucp_ep_failed_op,
.ep_atomic64_post = (uct_ep_atomic64_post_func_t)ucp_ep_failed_op,
.ep_atomic32_post = (uct_ep_atomic32_post_func_t)ucp_ep_failed_op,
.ep_atomic64_fetch = (uct_ep_atomic64_fetch_func_t)ucp_ep_failed_op,
.ep_atomic32_fetch = (uct_ep_atomic32_fetch_func_t)ucp_ep_failed_op,
.ep_tag_eager_short = (uct_ep_tag_eager_short_func_t)ucp_ep_failed_op,
.ep_tag_eager_bcopy = (uct_ep_tag_eager_bcopy_func_t)ucp_ep_failed_op,
.ep_tag_eager_zcopy = (uct_ep_tag_eager_zcopy_func_t)ucp_ep_failed_op,
.ep_tag_rndv_zcopy = (uct_ep_tag_rndv_zcopy_func_t)ucp_ep_failed_op,
.ep_tag_rndv_cancel = (uct_ep_tag_rndv_cancel_func_t)ucp_ep_failed_op,
.ep_tag_rndv_request = (uct_ep_tag_rndv_request_func_t)ucp_ep_failed_op,
.ep_pending_add = (uct_ep_pending_add_func_t)ucs_empty_function_return_busy,
.ep_pending_purge = (uct_ep_pending_purge_func_t)ucs_empty_function_return_success,
.ep_flush = (uct_ep_flush_func_t)ucp_ep_failed_op,
.ep_fence = (uct_ep_fence_func_t)ucp_ep_failed_op,
.ep_check = (uct_ep_check_func_t)ucs_empty_function_return_success,
.ep_connect_to_ep = (uct_ep_connect_to_ep_func_t)ucp_ep_failed_op,
.ep_destroy = ucp_ep_failed_destroy,
.ep_get_address = (uct_ep_get_address_func_t)ucp_ep_failed_op
};
static ucp_ep_discard_lanes_arg_t ucp_failed_tl_ep_discard_arg = {
.deactivate_cfg_index = UCP_WORKER_CFG_INDEX_NULL,
.status = UCS_ERR_CANCELED
};
static void ucp_failed_tl_iface_init(void)
{
uct_iface_close_func_t stub_close;
ucs_status_t status;
UCS_INIT_ONCE(&ucp_failed_tl_iface_once) {
status = ucp_stub_iface_open(UCS_ERR_CANCELED, &ucp_failed_tl_iface);
if (status != UCS_OK) {
ucs_fatal("failed to create failed tl iface stub");
}
stub_close = ucp_failed_tl_iface->ops.iface_close;
ucp_failed_tl_iface->ops = ucp_failed_tl_iface_ops;
ucp_failed_tl_iface->ops.iface_close = stub_close;
ucp_failed_tl_ep_discard_arg.failed_ep.iface = ucp_failed_tl_iface;
}
}
UCS_STATIC_CLEANUP {
UCS_CLEANUP_ONCE(&ucp_failed_tl_iface_once) {
uct_iface_close(ucp_failed_tl_iface);
}
}
uct_iface_h ucp_failed_tl_iface_get(void)
{
ucp_failed_tl_iface_init();
return ucp_failed_tl_iface;
}
int ucp_is_uct_ep_failed(uct_ep_h uct_ep)
{
return uct_ep->iface->ops.ep_flush == (uct_ep_flush_func_t)ucp_ep_failed_op;
}
static int ucp_ep_recovery_is_lane_connected(ucp_ep_h ep, ucp_lane_index_t lane)
{
uct_ep_h uct_ep = ucp_ep_get_lane(ep, lane);
return (uct_ep != NULL) && !ucp_is_uct_ep_failed(uct_ep) &&
!ucp_wireup_ep_test(uct_ep);
}
void ucp_ep_config_key_reset(ucp_ep_config_key_t *key)
{
ucp_lane_index_t i;
memset(key, 0, sizeof(*key));
key->num_lanes = 0;
for (i = 0; i < UCP_MAX_LANES; ++i) {
key->lanes[i].rsc_index = UCP_NULL_RESOURCE;
key->lanes[i].dst_md_index = UCP_NULL_RESOURCE;
key->lanes[i].dst_sys_dev = UCS_SYS_DEVICE_ID_UNKNOWN;
key->lanes[i].path_index = 0;
key->lanes[i].lane_types = 0;
key->lanes[i].port_speed = 0;
key->lanes[i].seg_size = 0;
}
key->am_lane = UCP_NULL_LANE;
key->wireup_msg_lane = UCP_NULL_LANE;
key->cm_lane = UCP_NULL_LANE;
key->keepalive_lane = UCP_NULL_LANE;
key->rkey_ptr_lane = UCP_NULL_LANE;
key->tag_lane = UCP_NULL_LANE;
key->rma_bw_md_map = 0;
key->rma_md_map = 0;
key->reachable_md_map = 0;
key->dst_md_cmpts = NULL;
key->err_mode = UCP_ERR_HANDLING_MODE_NONE;
key->flags = 0;
key->dst_version = UCP_API_MINOR;
memset(key->am_bw_lanes, UCP_NULL_LANE, sizeof(key->am_bw_lanes));
memset(key->rma_lanes, UCP_NULL_LANE, sizeof(key->rma_lanes));
memset(key->rma_bw_lanes, UCP_NULL_LANE, sizeof(key->rma_bw_lanes));
memset(key->amo_lanes, UCP_NULL_LANE, sizeof(key->amo_lanes));
}
static void ucp_ep_deallocate(ucp_ep_h ep)
{
UCS_STATS_NODE_FREE(ep->stats);
ucs_free(ep->ext->uct_eps);
ucs_free(ep->ext);
ucs_strided_alloc_put(&ep->worker->ep_alloc, ep);
}
static ucp_ep_h ucp_ep_allocate(ucp_worker_h worker, const char *peer_name)
{
ucp_ep_h ep;
ucp_lane_index_t lane;
ucs_status_t status;
ep = ucs_strided_alloc_get(&worker->ep_alloc, "ucp_ep");
if (ep == NULL) {
ucs_error("Failed to allocate ep");
goto err;
}
ep->ext = ucs_malloc(sizeof(*ep->ext), "ucp_ep_ext");
if (ep->ext == NULL) {
ucs_error("failed to allocate ep extension");
goto err_free_ep;
}
ep->ext->ep = ep;
ep->refcount = 0;
ep->cfg_index = UCP_WORKER_CFG_INDEX_NULL;
ep->worker = worker;
ep->am_lane = UCP_NULL_LANE;
ep->flags = 0;
ep->conn_sn = UCP_EP_MATCH_CONN_SN_MAX;
#if UCS_ENABLE_ASSERT
ep->refcounts.create =
ep->refcounts.flush =
ep->refcounts.discard =
ep->refcounts.probe = 0;
#endif
ep->ext->user_data = NULL;
ep->ext->cm_idx = UCP_NULL_RESOURCE;
ep->ext->local_ep_id = UCS_PTR_MAP_KEY_INVALID;
ep->ext->remote_ep_id = UCS_PTR_MAP_KEY_INVALID;
ep->ext->err_cb = NULL;
ep->ext->close_req = NULL;
#if UCS_ENABLE_ASSERT
ep->ext->ka_last_round = 0;
#endif
ep->ext->peer_mem = NULL;
ep->ext->unflushed_lanes = 0;
ep->ext->fence_seq = 0;
ep->ext->uct_eps = NULL;
ep->ext->flush_sys_dev_map = 0;
UCS_STATIC_ASSERT(sizeof(ep->ext->ep_match) >=
sizeof(ep->ext->flush_state));
memset(&ep->ext->ep_match, 0, sizeof(ep->ext->ep_match));
ucs_hlist_head_init(&ep->ext->proto_reqs);
for (lane = 0; lane < UCP_MAX_FAST_PATH_LANES; ++lane) {
ucp_ep_set_lane(ep, lane, NULL);
}
#if ENABLE_DEBUG_DATA
ucs_snprintf_zero(ep->peer_name, UCP_WORKER_ADDRESS_NAME_MAX, "%s",
peer_name);
#endif
/* Create statistics */
status = UCS_STATS_NODE_ALLOC(&ep->stats, &ucp_ep_stats_class,
worker->stats, "-%p", ep);
if (status != UCS_OK) {
goto err_free_ep_ext;
}
return ep;
err_free_ep_ext:
ucs_free(ep->ext);
err_free_ep:
ucs_strided_alloc_put(&worker->ep_alloc, ep);
err:
return NULL;
}
static int ucp_ep_shall_use_indirect_id(ucp_context_h context,
unsigned ep_init_flags)
{
return !(ep_init_flags & UCP_EP_INIT_FLAG_INTERNAL) &&
((context->config.ext.proto_indirect_id == UCS_CONFIG_ON) ||
((context->config.ext.proto_indirect_id == UCS_CONFIG_AUTO) &&
(ep_init_flags & UCP_EP_INIT_ERR_MODE_FAILOVER_MASK)));
}
void ucp_ep_peer_mem_destroy(ucp_context_h context,
ucp_ep_peer_mem_data_t *ppln_data)
{
ucp_md_map_t md_map;
ucs_status_t UCS_V_UNUSED status;
md_map = (ppln_data->md_index == UCP_NULL_RESOURCE) ?
0 : UCS_BIT(ppln_data->md_index);
status = ucp_mem_rereg_mds(context, 0, NULL, 0, 0, NULL,
UCS_MEMORY_TYPE_UNKNOWN, NULL,
&ppln_data->uct_memh, &md_map);
ucs_assertv(status == UCS_OK, "%s", ucs_status_string(status));
ucp_rkey_destroy(ppln_data->rkey);
}
ucp_ep_peer_mem_data_t*
ucp_ep_peer_mem_get(ucp_context_h context, ucp_ep_h ep, uint64_t address,
size_t size, const void *rkey_buf,
ucs_memory_type_t local_mem_type,
ucp_md_index_t rkey_ptr_md_index)
{
khash_t(ucp_ep_peer_mem_hash) *peer_mem = ep->ext->peer_mem;
ucp_lane_index_t mem_type_rma_lane;
ucp_ep_peer_mem_data_t *data;
ucp_ep_h mem_type_ep;
ucp_md_map_t md_map;
unsigned rkey_index;
khiter_t iter;
ucs_status_t status;
int ret;
ucs_assert(local_mem_type != UCS_MEMORY_TYPE_UNKNOWN);
if (ucs_unlikely(peer_mem == NULL)) {
ep->ext->peer_mem = peer_mem = kh_init(ucp_ep_peer_mem_hash);
}
iter = kh_put(ucp_ep_peer_mem_hash, peer_mem, address, &ret);
ucs_assert_always(ret != UCS_KH_PUT_FAILED);
data = &kh_val(ep->ext->peer_mem, iter);
if (ucs_likely(ret == UCS_KH_PUT_KEY_PRESENT)) {
if (ucs_likely(size <= data->size)) {
return data; /* found element with proper size */
}
ucp_ep_peer_mem_destroy(context, data);
}
data->size = size;
ucp_ep_rkey_unpack_internal(ep, rkey_buf, 0, UCS_BIT(rkey_ptr_md_index), 0,
UCS_SYS_DEVICE_ID_UNKNOWN, &data->rkey);
rkey_index = ucs_bitmap2idx(data->rkey->md_map, rkey_ptr_md_index);
status = uct_rkey_ptr(data->rkey->tl_rkey[rkey_index].cmpt,
&data->rkey->tl_rkey[rkey_index].rkey, address,
&data->local_ptr);
if (status != UCS_OK) {
ucp_rkey_destroy(data->rkey);
data->size = 0; /* Make sure hash element is updated next time */
return NULL;
}
/* Register remote memory segment with memtype ep MD. Without
* registration fetching data from GPU to CPU will be performance
* inefficient. */
mem_type_ep = ep->worker->mem_type_ep[local_mem_type];
ucs_assert(mem_type_ep != NULL);
md_map = 0;
mem_type_rma_lane = ucp_ep_config(mem_type_ep)->key.rma_bw_lanes[0];
data->md_index = ucp_ep_md_index(mem_type_ep, mem_type_rma_lane);
status = ucp_mem_rereg_mds(
ep->worker->context, UCS_BIT(data->md_index),
data->local_ptr, data->size,
UCT_MD_MEM_ACCESS_RMA | UCT_MD_MEM_FLAG_HIDE_ERRORS,
NULL, UCS_MEMORY_TYPE_HOST, NULL, &data->uct_memh,
&md_map);
if (status != UCS_OK) {
data->md_index = UCP_NULL_RESOURCE;
data->uct_memh = NULL;
} else {
ucs_assertv(md_map == UCS_BIT(data->md_index),
"mdmap=0x%lx, md_index=%u", md_map, data->md_index);
}
return data;
}
ucs_status_t ucp_ep_create_base(ucp_worker_h worker, unsigned ep_init_flags,
const char *peer_name, const char *message,
ucp_ep_h *ep_p)
{
ucs_status_t status;
ucp_ep_h ep;
ep = ucp_ep_allocate(worker, peer_name);
if (ep == NULL) {
status = UCS_ERR_NO_MEMORY;
goto err;
}
ucp_stream_ep_init(ep);
ucp_am_ep_init(ep);
if (ucp_ep_shall_use_indirect_id(ep->worker->context, ep_init_flags)) {
ucp_ep_update_flags(ep, UCP_EP_FLAG_INDIRECT_ID, 0);
}
status = UCS_PTR_MAP_PUT(ep, &worker->ep_map, ep,
ep->flags & UCP_EP_FLAG_INDIRECT_ID,
&ep->ext->local_ep_id);
if ((status != UCS_OK) && (status != UCS_ERR_NO_PROGRESS)) {
ucs_error("ep %p: failed to allocate ID: %s", ep,
ucs_status_string(status));
goto err_ep_deallocate;
}
ucp_ep_flush_state_reset(ep);
/* Create endpoint VFS node on demand to avoid memory bloat */
ucs_vfs_obj_set_dirty(worker, ucp_worker_vfs_refresh);
/* Insert new UCP endpoint to the UCP worker */
if (ep_init_flags & UCP_EP_INIT_FLAG_INTERNAL) {
ucp_ep_update_flags(ep, UCP_EP_FLAG_INTERNAL, 0);
ucs_list_add_tail(&worker->internal_eps, &ep->ext->ep_list);
} else {
ucs_list_add_tail(&worker->all_eps, &ep->ext->ep_list);
ucs_assert(ep->worker->num_all_eps < UINT_MAX);
++ep->worker->num_all_eps;
}
ucp_ep_refcount_add(ep, create);
*ep_p = ep;
ucs_debug("created ep %p to %s %s", ep, ucp_ep_peer_name(ep), message);
return UCS_OK;
err_ep_deallocate:
ucp_ep_deallocate(ep);
err:
return status;
}
static int
ucp_ep_local_disconnect_progress_remove_filter(const ucs_callbackq_elem_t *elem,
void *arg)
{
ucp_ep_h ep = (ucp_ep_h)arg;
ucp_request_t *req;
if (elem->cb != ucp_ep_local_disconnect_progress) {
return 0;
}
req = (ucp_request_t*)elem->arg;
if (ep != req->send.ep) {
return 0;
}
/* Expect that only EP flush request can be remained in the callback queue,
* because reply UCP EP created for sending WIREUP_MSG/EP_REMOVED message is
* not exposed to a user */
ucs_assert(req->flags & UCP_REQUEST_FLAG_RELEASED);
ucs_assert(req->send.uct.func == ucp_ep_flush_progress_pending);
ucp_request_complete_send(req, req->status);
return 1;
}
static unsigned ucp_ep_set_lanes_failed_progress(void *arg)
{
ucp_ep_set_lanes_failed_arg_t *failed_arg = arg;
ucp_ep_h ucp_ep = failed_arg->ucp_ep;
ucp_worker_h worker = ucp_ep->worker;
UCS_ASYNC_BLOCK(&worker->async);
ucp_ep_set_lanes_failed(ucp_ep, failed_arg->lanes, failed_arg->status);
UCS_ASYNC_UNBLOCK(&worker->async);
ucs_free(failed_arg);
return 1;
}
static int ucp_ep_set_failed_remove_filter(const ucs_callbackq_elem_t *elem,
void *arg)
{
ucp_ep_set_lanes_failed_arg_t *set_ep_failed_arg = elem->arg;
if ((elem->cb == ucp_ep_set_lanes_failed_progress) &&
(set_ep_failed_arg->ucp_ep == arg)) {
ucs_free(set_ep_failed_arg);
return 1;
}
return 0;
}
static int ucp_ep_wireup_eps_progress_filter(const ucs_callbackq_elem_t *elem,
void *arg)
{
return (elem->cb == ucp_wireup_eps_progress) && (elem->arg == arg);
}
static int ucp_ep_remove_filter(const ucs_callbackq_elem_t *elem, void *arg)
{
if (ucp_wireup_msg_ack_cb_pred(elem, arg) ||
ucp_listener_accept_cb_remove_filter(elem, arg) ||
ucp_ep_local_disconnect_progress_remove_filter(elem, arg) ||
ucp_ep_set_failed_remove_filter(elem, arg) ||
ucp_ep_wireup_eps_progress_filter(elem, arg)) {
return 1;
}
return 0;
}
void ucp_ep_destroy_base(ucp_ep_h ep)
{
ucp_worker_h worker = ep->worker;
ucp_ep_peer_mem_data_t data;
ucp_ep_refcount_field_assert(ep, refcount, ==, 0);
ucp_ep_refcount_assert(ep, create, ==, 0);
ucp_ep_refcount_assert(ep, flush, ==, 0);
ucp_ep_refcount_assert(ep, discard, ==, 0);
ucp_ep_refcount_assert(ep, probe, ==, 0);
ucs_assert(ucs_hlist_is_empty(&ep->ext->proto_reqs));
if (!(ep->flags & UCP_EP_FLAG_INTERNAL)) {
ucs_assert(worker->num_all_eps > 0);
--worker->num_all_eps;
}
ucp_worker_keepalive_remove_ep(ep);
ucp_ep_release_id(ep);
ucs_list_del(&ep->ext->ep_list);
if (!ucp_ep_has_cm_lane(ep) && (ep->ext->recovery_arg != NULL)) {
ucp_ep_recovery_arg_free(ep);
}
ucs_vfs_obj_remove(ep);
ucs_callbackq_remove_oneshot(&worker->uct->progress_q, ep,
ucp_ep_remove_filter, ep);
UCS_STATS_NODE_FREE(ep->stats);
if (ep->ext->peer_mem != NULL) {
kh_foreach_value(ep->ext->peer_mem, data, {
ucp_ep_peer_mem_destroy(worker->context, &data);
});
kh_destroy(ucp_ep_peer_mem_hash, ep->ext->peer_mem);
}
ucp_ep_deallocate(ep);
}
void ucp_ep_delete(ucp_ep_h ep)
{
ucp_ep_refcount_assert(ep, create, ==, 1);
ucp_ep_refcount_remove(ep, create);
}
void ucp_ep_flush_state_reset(ucp_ep_h ep)
{
ucp_ep_flush_state_t *flush_state = &ep->ext->flush_state;
ucs_assert(!(ep->flags & UCP_EP_FLAG_ON_MATCH_CTX));
ucs_assert(!(ep->flags & UCP_EP_FLAG_FLUSH_STATE_VALID) ||
((flush_state->send_sn == 0) &&
(flush_state->cmpl_sn == 0) &&
ucs_hlist_is_empty(&flush_state->reqs)));
flush_state->send_sn = 0;
flush_state->cmpl_sn = 0;
flush_state->mem_in_progress = 0;
ucs_hlist_head_init(&flush_state->reqs);
ucp_ep_update_flags(ep, UCP_EP_FLAG_FLUSH_STATE_VALID, 0);
}
void ucp_ep_flush_state_invalidate(ucp_ep_h ep)
{
ucs_assert(ucs_hlist_is_empty(&ucp_ep_flush_state(ep)->reqs));
ucp_ep_update_flags(ep, 0, UCP_EP_FLAG_FLUSH_STATE_VALID);
}
/* Since release function resets EP ID to @ref UCS_PTR_MAP_KEY_INVALID and PTR
* MAP considers @ref UCS_PTR_MAP_KEY_INVALID as direct key, release EP ID is
* re-entrant function */
void ucp_ep_release_id(ucp_ep_h ep)
{
ucs_status_t status;
/* Don't use ucp_ep_local_id() function here to avoid assertion failure,
* because local_ep_id can be set to @ref UCS_PTR_MAP_KEY_INVALID */
status = UCS_PTR_MAP_DEL(ep, &ep->worker->ep_map, ep->ext->local_ep_id);
if ((status != UCS_OK) && (status != UCS_ERR_NO_PROGRESS)) {
ucs_warn("ep %p local id 0x%" PRIxPTR ": ucs_ptr_map_del failed: %s",
ep, ucp_ep_local_id(ep), ucs_status_string(status));
}
ep->ext->local_ep_id = UCS_PTR_MAP_KEY_INVALID;
}
/* TODO: err_mode field could be part of flags */
void ucp_ep_config_key_set_err_mode(ucp_ep_config_key_t *key,
unsigned ep_init_flags)
{
if (ep_init_flags & UCP_EP_INIT_ERR_MODE_FAILOVER) {
key->err_mode = UCP_ERR_HANDLING_MODE_FAILOVER;
} else if (ep_init_flags & UCP_EP_INIT_ERR_MODE_PEER_FAILURE) {
key->err_mode = UCP_ERR_HANDLING_MODE_PEER;
} else {
key->err_mode = UCP_ERR_HANDLING_MODE_NONE;
}
}
void ucp_ep_config_key_init_flags(ucp_ep_config_key_t *key,
unsigned ep_init_flags)
{
if (ucs_test_all_flags(ep_init_flags,
UCP_EP_INIT_CREATE_AM_LANE | UCP_EP_INIT_CM_PHASE)) {
key->flags |= UCP_EP_CONFIG_KEY_FLAG_INTERMEDIATE;
}
}
ucs_status_t
ucp_ep_config_err_mode_check_mismatch(ucp_ep_h ep,
ucp_err_handling_mode_t err_mode)
{
if (!ucp_ep_err_mode_eq(ep, err_mode)) {
ucs_error("ep %p: asymmetric endpoint configuration is not supported,"
" error handling level mismatch (expected: %d, got: %d)",
ep, ucp_ep_config(ep)->key.err_mode, err_mode);
return UCS_ERR_UNSUPPORTED;
}
return UCS_OK;
}
/* Handles a case where the existing endpoint is incomplete */
static ucs_status_t
ucp_ep_adjust_params(ucp_ep_h ep, const ucp_ep_params_t *params)
{
ucs_status_t status;
if (params->field_mask & UCP_EP_PARAM_FIELD_ERR_HANDLING_MODE) {
status = ucp_ep_config_err_mode_check_mismatch(ep, params->err_mode);
if (status != UCS_OK) {
return status;
}
}
if (params->field_mask & UCP_EP_PARAM_FIELD_ERR_HANDLER) {
ep->ext->user_data = params->err_handler.arg;
ep->ext->err_cb = params->err_handler.cb;
}
if (params->field_mask & UCP_EP_PARAM_FIELD_USER_DATA) {
/* user_data overrides err_handler.arg */
ep->ext->user_data = params->user_data;
ep->flags |= UCP_EP_FLAG_USER_DATA_PARAM;
}
return UCS_OK;
}
ucs_status_t ucp_ep_evaluate_perf(ucp_ep_h ep,
const ucp_ep_evaluate_perf_param_t *param,
ucp_ep_evaluate_perf_attr_t *attr)
{
const ucp_worker_h worker = ep->worker;
const ucp_context_h context = worker->context;
const ucp_ep_config_key_t *key = &ucp_ep_config(ep)->key;
double max_bandwidth = 0;
ucp_rsc_index_t max_bandwidth_rsc_index = 0;
ucp_rsc_index_t rsc_index;
double bandwidth;
ucp_lane_index_t lane;
ucp_worker_iface_t *wiface;
uct_iface_attr_t *iface_attr;
ucs_linear_func_t estimated_time;
if (!ucs_test_all_flags(attr->field_mask,
UCP_EP_PERF_ATTR_FIELD_ESTIMATED_TIME &
UCP_EP_PERF_PARAM_FIELD_MESSAGE_SIZE)) {
return UCS_ERR_INVALID_PARAM;
}
for (lane = 0; lane < ucp_ep_num_lanes(ep); ++lane) {
if (lane == key->cm_lane) {
/* Skip CM lanes for bandwidth calculation */
continue;
}
rsc_index = key->lanes[lane].rsc_index;
wiface = worker->ifaces[rsc_index];
bandwidth = ucp_tl_iface_bandwidth(context,
&wiface->attr.bandwidth);
if (bandwidth > max_bandwidth) {
max_bandwidth = bandwidth;
max_bandwidth_rsc_index = rsc_index;
}
}
iface_attr = ucp_worker_iface_get_attr(worker,
max_bandwidth_rsc_index);
estimated_time.c = ucp_tl_iface_latency(context, &iface_attr->latency);
estimated_time.m = param->message_size / max_bandwidth;
attr->estimated_time = estimated_time.c + estimated_time.m;
return UCS_OK;
}
ucs_status_t ucp_worker_mem_type_eps_create(ucp_worker_h worker)
{
ucp_context_h context = worker->context;
unsigned pack_flags = ucp_worker_default_address_pack_flags(worker);
ucp_unpacked_address_t local_address;
ucs_memory_type_t mem_type;
ucs_status_t status;
void *address_buffer;
size_t address_length;
ucp_tl_bitmap_t mem_access_tls;
char ep_name[UCP_WORKER_ADDRESS_NAME_MAX];
unsigned addr_indices[UCP_MAX_LANES];
ucp_lane_index_t num_lanes;
ucp_rsc_index_t rsc_index;
ucs_memory_type_for_each(mem_type) {
ucp_context_memaccess_tl_bitmap(context, UCS_BIT(mem_type), 0,
&mem_access_tls);
/* Exclude transports that map remote memory via rkey pointer
* since mem_type EP is for in-process communication */
UCS_STATIC_BITMAP_FOR_EACH_BIT(rsc_index, &mem_access_tls) {
if (context->tl_mds[context->tl_rscs[rsc_index].md_index].attr.flags &
UCT_MD_FLAG_RKEY_PTR) {
UCS_STATIC_BITMAP_RESET(&mem_access_tls, rsc_index);
}
}
if (UCP_MEM_IS_HOST(mem_type) ||
UCS_STATIC_BITMAP_IS_ZERO(mem_access_tls)) {
continue;
}
status = ucp_address_pack(worker, NULL, &mem_access_tls, pack_flags,
context->config.ext.worker_addr_version, NULL,
UINT_MAX, &address_length, &address_buffer);
if (status != UCS_OK) {
goto err_cleanup_eps;
}
status = ucp_address_unpack(worker, address_buffer, pack_flags,
&local_address);
if (status != UCS_OK) {
goto err_free_address_buffer;
}
ucs_snprintf_zero(ep_name, UCP_WORKER_ADDRESS_NAME_MAX,
"mem_type_ep:%s", ucs_memory_type_names[mem_type]);
/* create memtype UCP EPs after blocking async context, because they set
* INTERNAL flag (setting EP flags is expected to be guarded) */
UCS_ASYNC_BLOCK(&worker->async);
status = ucp_ep_create_to_worker_addr(worker, &ucp_tl_bitmap_max,
&local_address,
UCP_EP_INIT_FLAG_MEM_TYPE |
UCP_EP_INIT_FLAG_INTERNAL,
ep_name, addr_indices,
&worker->mem_type_ep[mem_type]);
if (status != UCS_OK) {
UCS_ASYNC_UNBLOCK(&worker->async);
goto err_free_address_list;
}
num_lanes = ucp_ep_num_lanes(worker->mem_type_ep[mem_type]);
ucs_assertv_always(num_lanes <= 2, "num_lanes=%u", num_lanes);
UCS_ASYNC_UNBLOCK(&worker->async);
ucs_free(local_address.address_list);
ucs_free(address_buffer);
}
return UCS_OK;
err_free_address_list:
ucs_free(local_address.address_list);
err_free_address_buffer:
ucs_free(address_buffer);
err_cleanup_eps:
ucp_worker_mem_type_eps_destroy(worker);
return status;
}
void ucp_worker_mem_type_eps_destroy(ucp_worker_h worker)
{
ucs_memory_type_t mem_type;
ucp_ep_h ep;
/* Destroy memtype UCP EPs after blocking async context, because cleanup
* lanes set FAILED flag (setting EP flags is expected to be guarded) */
UCS_ASYNC_BLOCK(&worker->async);
ucs_memory_type_for_each(mem_type) {
ep = worker->mem_type_ep[mem_type];
if (ep == NULL) {
continue;
}
ucs_debug("memtype ep %p: destroy", ep);
ucs_assert(ep->flags & UCP_EP_FLAG_INTERNAL);
ucp_ep_destroy_internal(ep);
worker->mem_type_ep[mem_type] = NULL;
}
UCS_ASYNC_UNBLOCK(&worker->async);
}
static ucs_status_t ucp_ep_init_create_wireup(ucp_ep_h ep,
unsigned ep_init_flags,
ucp_wireup_ep_t **wireup_ep)
{
ucp_ep_config_key_t key;
uct_ep_h uct_ep;
ucs_status_t status;
ucp_worker_cfg_index_t cfg_index;
ucs_assert(ep_init_flags & UCP_EP_INIT_CM_WIREUP_CLIENT);
ucs_assert(ucp_worker_num_cm_cmpts(ep->worker) != 0);
ucp_ep_config_key_reset(&key);
ucp_ep_config_key_set_err_mode(&key, ep_init_flags);
ucp_ep_config_key_init_flags(&key, ep_init_flags);
key.num_lanes = 1;
/* all operations will use the first lane, which is a stub endpoint before
* reconfiguration */
key.am_lane = 0;
if (ucp_ep_init_flags_has_cm(ep_init_flags)) {
key.cm_lane = 0;
/* Send keepalive on wireup_ep (which will send on aux_ep) */
if (ep_init_flags & UCP_EP_INIT_ERR_MODE_FAILOVER_MASK) {
key.keepalive_lane = 0;
}
} else {
key.wireup_msg_lane = 0;
}
status = ucp_worker_get_ep_config(ep->worker, &key, ep_init_flags,
&cfg_index);
if (status != UCS_OK) {
return status;
}
ucp_ep_set_cfg_index(ep, cfg_index, 1);
if (!ucp_ep_has_cm_lane(ep)) {
ucp_ep_update_flags(ep, UCP_EP_FLAG_CONNECT_REQ_QUEUED, 0);
}
status = ucp_wireup_ep_create(ep, &uct_ep);
if (status != UCS_OK) {
return status;
}
ucp_ep_set_lane(ep, 0, uct_ep);
*wireup_ep = ucs_derived_of(ucp_ep_get_lane(ep, 0), ucp_wireup_ep_t);
return UCS_OK;
}
ucs_status_t
ucp_ep_create_to_worker_addr(ucp_worker_h worker,
const ucp_tl_bitmap_t *local_tl_bitmap,
const ucp_unpacked_address_t *remote_address,
unsigned ep_init_flags, const char *message,
unsigned *addr_indices, ucp_ep_h *ep_p)
{
ucp_tl_bitmap_t ep_tl_bitmap;
ucs_status_t status;
ucp_ep_h ep;
int am_need_flush;
/* allocate endpoint */
status = ucp_ep_create_base(worker, ep_init_flags, remote_address->name,
message, &ep);
if (status != UCS_OK) {
goto err;
}
/* initialize transport endpoints */
status = ucp_wireup_init_lanes(ep, ep_init_flags, local_tl_bitmap,
remote_address, addr_indices,
&am_need_flush);
if (status != UCS_OK) {
goto err_delete;
}
ucp_ep_get_tl_bitmap(&ucp_ep_config(ep)->key, &ep_tl_bitmap);
ucp_tl_bitmap_validate(&ep_tl_bitmap, local_tl_bitmap);
*ep_p = ep;
return UCS_OK;
err_delete:
ucp_ep_delete(ep);
err:
return status;
}
static ucs_status_t ucp_ep_create_to_sock_addr(ucp_worker_h worker,
const ucp_ep_params_t *params,
ucp_ep_h *ep_p)
{
char peer_name[UCS_SOCKADDR_STRING_LEN];
ucp_wireup_ep_t *wireup_ep;
ucs_status_t status;
ucp_ep_h ep;
unsigned ep_init_flags;
if (!(params->field_mask & UCP_EP_PARAM_FIELD_SOCK_ADDR)) {
ucs_error("destination socket address is missing");
status = UCS_ERR_INVALID_PARAM;
goto err;
}
UCP_CHECK_PARAM_NON_NULL(params->sockaddr.addr, status, goto err);
/* allocate endpoint */
ucs_sockaddr_str(params->sockaddr.addr, peer_name, sizeof(peer_name));
ep_init_flags = ucp_ep_init_flags(worker, params);
status = ucp_ep_create_base(worker, ep_init_flags, peer_name,
"from api call", &ep);
if (status != UCS_OK) {
goto err;
}
status = ucp_ep_init_create_wireup(ep, ep_init_flags, &wireup_ep);
if (status != UCS_OK) {
goto err_delete;
}
if (UCP_PARAM_VALUE(EP, params, flags, FLAGS, 0) &
UCP_EP_PARAMS_FLAGS_SEND_CLIENT_ID) {
wireup_ep->flags |= UCP_WIREUP_EP_FLAG_SEND_CLIENT_ID;
}
status = ucp_ep_adjust_params(ep, params);
if (status != UCS_OK) {
goto err_cleanup_lanes;
}
status = ucp_ep_client_cm_connect_start(ep, params);
if (status != UCS_OK) {
goto err_cleanup_lanes;
}
*ep_p = ep;
return UCS_OK;
err_cleanup_lanes:
ucp_ep_cleanup_lanes(ep);
err_delete:
ucp_ep_delete(ep);
err:
return status;
}
static ucs_status_t
ucp_sa_data_v1_unpack(const ucp_wireup_sockaddr_data_base_t *sa_data,
unsigned *ep_init_flags_p,
const void** worker_addr_p)
{
const ucp_wireup_sockaddr_data_v1_t *sa_data_v1 =
ucs_derived_of(sa_data, ucp_wireup_sockaddr_data_v1_t);
if (sa_data_v1->addr_mode != UCP_WIREUP_SA_DATA_CM_ADDR) {
ucs_error("sa_data_v1 contains unsupported address mode %u",
sa_data_v1->addr_mode);
return UCS_ERR_UNSUPPORTED;
}
*ep_init_flags_p = ucp_ep_err_mode_init_flags(sa_data->header);