Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 216 additions & 9 deletions src/ucp/core/ucp_ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ __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);

#define UCP_EP_FENCE_PROGRESS_BATCH 16

typedef struct {
double reg_growth;
double reg_overhead;
Expand Down Expand Up @@ -232,7 +234,6 @@ static void ucp_ep_deallocate(ucp_ep_h 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");
Expand Down Expand Up @@ -271,19 +272,23 @@ static ucp_ep_h ucp_ep_allocate(ucp_worker_h worker, const char *peer_name)
#endif
ep->ext->peer_mem = NULL;
ep->ext->unflushed_lanes = 0;
ep->ext->lane_generation = 0;
ep->ext->fence_seq = 0;
ep->ext->fence_inflight_req = NULL;
ep->ext->fence_status = UCS_OK;
ep->ext->fence_lanes_dirty = 0;
ep->ext->fence_pending_scheduled = 0;
ep->ext->uct_eps = NULL;
ep->ext->flush_sys_dev_map = 0;
ucs_queue_head_init(&ep->ext->fence_pending_q);

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);
}
memset(ep->uct_eps, 0, sizeof(ep->uct_eps));
#if ENABLE_DEBUG_DATA
ucs_snprintf_zero(ep->peer_name, UCP_WORKER_ADDRESS_NAME_MAX, "%s",
peer_name);
Expand Down Expand Up @@ -518,13 +523,201 @@ static int ucp_ep_wireup_eps_progress_filter(const ucs_callbackq_elem_t *elem,
return (elem->cb == ucp_wireup_eps_progress) && (elem->arg == arg);
}

static UCS_F_ALWAYS_INLINE ucs_queue_elem_t*
ucp_ep_fence_req_elem(uct_pending_req_t *uct_req)
{
ucp_request_t *req = ucs_container_of(uct_req, ucp_request_t, send.uct);

return &req->send.fence_pending_elem;
}

static unsigned ucp_ep_fence_pending_progress(void *arg);

static int
ucp_ep_fence_pending_progress_filter(const ucs_callbackq_elem_t *elem,
void *arg)
{
return (elem->cb == ucp_ep_fence_pending_progress) && (elem->arg == arg);
}

static UCS_F_ALWAYS_INLINE void ucp_ep_fence_pending_schedule(ucp_ep_h ep)
{
if (ep->ext->fence_pending_scheduled) {
return;
}

ep->ext->fence_pending_scheduled = 1;
ucs_callbackq_add_oneshot(&ep->worker->uct->progress_q, ep,
ucp_ep_fence_pending_progress, ep);
ucp_worker_signal_internal(ep->worker);
}

void ucp_ep_fence_pending_resume(ucp_ep_h ep)
{
if (!ucs_queue_is_empty(&ep->ext->fence_pending_q)) {
ucp_ep_fence_pending_schedule(ep);
}
}

void ucp_ep_fence_pending_add(ucp_ep_h ep, uct_pending_req_t *uct_req)
{
ucs_queue_head_t *queue = &ep->ext->fence_pending_q;
ucp_request_t *new_req =
ucs_container_of(uct_req, ucp_request_t, send.uct);
ucs_queue_elem_t *elem = ucp_ep_fence_req_elem(uct_req);
ucs_queue_iter_t iter;
ucp_request_t *queued_req;

for (iter = ucs_queue_iter_begin(queue);
!ucs_queue_iter_end(queue, iter);
iter = ucs_queue_iter_next(iter)) {
queued_req = ucs_container_of(*iter, ucp_request_t,
send.fence_pending_elem);
if (queued_req->send.fence_seq > new_req->send.fence_seq) {
elem->next = *iter;
*iter = elem;
ucp_ep_fence_pending_schedule(ep);
return;
}
}

ucs_queue_push(queue, elem);
ucp_ep_fence_pending_schedule(ep);
}

void ucp_ep_fence_pending_purge(ucp_ep_h ep, ucs_status_t status)
{
ucp_request_t *req;

ucs_queue_for_each_extract(req, &ep->ext->fence_pending_q,
send.fence_pending_elem, 1) {
req->flags &= ~UCP_REQUEST_FLAG_FENCE_BLOCKED;
if (req->flags & UCP_REQUEST_FLAG_PROTO_SEND) {
ucp_proto_request_abort(req, status);
} else {
ucp_request_send_state_ff(req, status);
}
}
}

static UCS_F_ALWAYS_INLINE int
ucp_ep_fence_try_advance_epoch(ucp_ep_h ep, uint64_t fence_seq)
{
ucs_status_t status;

status = ucp_ep_fence_strong_nb(ep, fence_seq);
if (ucs_unlikely(status != UCS_OK)) {
ucp_ep_fence_pending_purge(ep, status);
return 0;
}

if (ucs_unlikely(ep->ext->fence_inflight_req == NULL)) {
return ep->ext->fence_status == UCS_OK;
}

return 0;
}

static int ucp_ep_fence_dispatch_request(ucp_ep_h ep)
{
ucp_ep_ext_t *ep_ext = ep->ext;
ucp_request_t *req;
ucs_status_t status;
int is_proto;
int batch;

req = ucs_queue_pull_elem_non_empty(&ep_ext->fence_pending_q,
ucp_request_t,
send.fence_pending_elem);

if ((req->flags & UCP_REQUEST_FLAG_PROTO_SEND) &&
((ep->cfg_index != req->send.proto_config->ep_cfg_index) ||
ep->worker->context->config.ext.proto_request_reset)) {
/* A request on the fence queue bypasses wireup pending replay. Restart
* it on the current endpoint configuration before its old callback can
* use a failed or replaced lane. Its fence epoch remains unchanged. */
ucp_trace_req(req, "restart fenced proto %s after reconfiguration",
req->send.proto_config->proto->name);
req->flags &= ~UCP_REQUEST_FLAG_FENCE_BLOCKED;
ucp_proto_request_restart(req);
return 1;
}

is_proto = req->flags & UCP_REQUEST_FLAG_PROTO_SEND;
req->flags &= ~UCP_REQUEST_FLAG_FENCE_BLOCKED;

for (batch = 0; batch < UCP_EP_FENCE_PROGRESS_BATCH; ++batch) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we confirm req->send.uct.func never frees the request when it returns UCS_INPROGRESS/UCS_ERR_NO_RESOURCE/UCP_STATUS_FENCE_DEFER? The batch loop and the subsequent requeue dereference req after the call, and the address_free annotation suggests the callback may free it.

/* coverity[address_free] */
status = req->send.uct.func(&req->send.uct);
if (status != UCS_INPROGRESS) {
break;
}
}

if ((status == UCS_INPROGRESS) ||
(status == UCS_ERR_NO_RESOURCE) ||
(status == UCP_STATUS_FENCE_DEFER)) {
req->flags |= UCP_REQUEST_FLAG_FENCE_BLOCKED;
ucs_queue_push_head(&ep_ext->fence_pending_q,
&req->send.fence_pending_elem);
if (ep_ext->fence_inflight_req == NULL) {
ucp_ep_fence_pending_schedule(ep);
}
return 0;
}

if (ucs_unlikely((status != UCS_OK) && UCS_STATUS_IS_ERR(status))) {
if (is_proto) {
ucp_proto_request_abort(req, status);
} else {
ucp_request_send_state_ff(req, status);
}
} else {
ucs_assert(status == UCS_OK);
}

return 1;
}

static unsigned ucp_ep_fence_pending_progress(void *arg)
{
ucp_ep_h ep = arg;
ucp_ep_ext_t *ep_ext = ep->ext;
ucp_request_t *req;

ep_ext->fence_pending_scheduled = 0;

while (!ucs_queue_is_empty(&ep_ext->fence_pending_q)) {
if (ucs_unlikely(ep_ext->fence_inflight_req != NULL)) {
break;
}

req = ucs_queue_head_elem_non_empty(&ep_ext->fence_pending_q,
ucp_request_t,
send.fence_pending_elem);
if (ucs_unlikely(req->send.fence_seq > ep_ext->fence_seq)) {
if (!ucp_ep_fence_try_advance_epoch(ep,
req->send.fence_seq)) {
break;
}
}

if (!ucp_ep_fence_dispatch_request(ep)) {
break;
}
}

return 0;
}

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)) {
ucp_ep_wireup_eps_progress_filter(elem, arg) ||
ucp_ep_fence_pending_progress_filter(elem, arg)) {
return 1;
}

Expand All @@ -542,6 +735,7 @@ void ucp_ep_destroy_base(ucp_ep_h ep)
ucp_ep_refcount_assert(ep, discard, ==, 0);
ucp_ep_refcount_assert(ep, probe, ==, 0);
ucs_assert(ucs_hlist_is_empty(&ep->ext->proto_reqs));
ucs_assert(ucs_queue_is_empty(&ep->ext->fence_pending_q));

if (!(ep->flags & UCP_EP_FLAG_INTERNAL)) {
ucs_assert(worker->num_all_eps > 0);
Expand Down Expand Up @@ -1505,7 +1699,9 @@ static void ucp_ep_discard_lanes_callback(void *request, ucs_status_t status,
}

ucs_trace("ep %p: discard lanes completed", arg->ucp_ep);
ucp_ep_reqs_purge(arg->ucp_ep, arg->status);
ucp_ep_reqs_purge(
arg->ucp_ep, arg->status,
arg->deactivate_cfg_index == UCP_WORKER_CFG_INDEX_NULL);
ucp_ep_config_deactivate_worker_ifaces(arg->ucp_ep->worker,
arg->deactivate_cfg_index);
ucp_ep_release_discard_arg(arg);
Expand Down Expand Up @@ -1562,7 +1758,7 @@ static void ucp_ep_discard_lanes(ucp_ep_h ep, ucp_lane_map_t lanes,
ucs_error("ep %p: failed to allocate memory for discarding lanes"
" argument", ep);
ucp_ep_cleanup_lanes(ep); /* Just close all UCT endpoints */
ucp_ep_reqs_purge(ep, discard_status);
ucp_ep_reqs_purge(ep, discard_status, 1);
return;
}

Expand Down Expand Up @@ -4500,12 +4696,17 @@ void ucp_ep_req_purge(ucp_ep_h ucp_ep, ucp_request_t *req,
}
}

void ucp_ep_reqs_purge(ucp_ep_h ucp_ep, ucs_status_t status)
void ucp_ep_reqs_purge(ucp_ep_h ucp_ep, ucs_status_t status,
int purge_fence_pending)
{
ucs_hlist_head_t *proto_reqs = &ucp_ep->ext->proto_reqs;
ucp_ep_flush_state_t *flush_state;
ucp_request_t *req;

if (purge_fence_pending) {
ucp_ep_fence_pending_purge(ucp_ep, status);
}

while (!ucs_hlist_is_empty(proto_reqs)) {
req = ucs_hlist_head_elem(proto_reqs, ucp_request_t, send.list);
if (ucp_ep->worker->context->config.ext.proto_enable) {
Expand Down Expand Up @@ -4707,7 +4908,13 @@ ucs_status_t ucp_ep_realloc_lanes(ucp_ep_h ep, unsigned new_num_lanes)
0;

for (lane = old_num_lanes; lane < new_num_lanes; ++lane) {
ucp_ep_set_lane(ep, lane, NULL);
/* New storage is not initialized yet, so bypass lane change
* tracking. */
if (lane < UCP_MAX_FAST_PATH_LANES) {
ep->uct_eps[lane] = NULL;
} else {
ep_ext->uct_eps[lane - UCP_MAX_FAST_PATH_LANES] = NULL;
}
}

return UCS_OK;
Expand Down
33 changes: 32 additions & 1 deletion src/ucp/core/ucp_ep.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,18 @@ typedef struct ucp_ep_ext {

ucp_lane_map_t unflushed_lanes; /* Bitmap of lanes which have
unflushed operations */
uint64_t lane_generation; /* Incremented whenever a lane
is updated */
uint64_t fence_seq; /* Sequence number for fence
detection */

ucs_queue_head_t fence_pending_q; /* Fence-blocked requests */
ucp_request_t *fence_inflight_req; /* Active fence flush */
ucs_status_t fence_status; /* Current/last flush status */
/* Lane topology changed since fence lane tracking was normalized. */
uint8_t fence_lanes_dirty;
uint8_t fence_pending_scheduled; /* Progress armed */

/**
* UCT endpoints for every slow-path lane that has no room in the base endpoint
* structure. TODO allocate this array dynamically.
Expand Down Expand Up @@ -761,6 +770,21 @@ ucs_status_ptr_t ucp_ep_flush_internal(ucp_ep_h ep, unsigned req_flags,
const char *debug_name,
unsigned uct_flags);

/**
* @brief Flush a subset of endpoint lanes specified by @a lane_mask.
*
* Same as @ref ucp_ep_flush_internal, but only lanes whose bit is set in
* @a lane_mask are initially flushed. If the endpoint topology changes while
* the flush is active, current live lanes are added conservatively.
*/
ucs_status_ptr_t
ucp_ep_flush_lanes_internal(ucp_ep_h ep, unsigned req_flags,
const ucp_request_param_t *param,
ucp_request_t *worker_req,
ucp_request_callback_t flushed_cb,
const char *debug_name, unsigned uct_flags,
ucp_lane_map_t lane_mask);

void ucp_ep_config_key_set_err_mode(ucp_ep_config_key_t *key,
unsigned ep_init_flags);

Expand Down Expand Up @@ -936,9 +960,16 @@ void ucp_ep_req_purge(ucp_ep_h ucp_ep, ucp_request_t *req,
* @param [in] ucp_ep Endpoint object on which requests should be
* purged.
* @param [in] status Completion status.
* @param [in] purge_fence_pending Whether to purge requests waiting for a
* fence epoch. Reconfiguration keeps them so
* they can restart on the new topology.
*/
void ucp_ep_reqs_purge(ucp_ep_h ucp_ep, ucs_status_t status);
void ucp_ep_reqs_purge(ucp_ep_h ucp_ep, ucs_status_t status,
int purge_fence_pending);

void ucp_ep_fence_pending_add(ucp_ep_h ep, uct_pending_req_t *req);
void ucp_ep_fence_pending_purge(ucp_ep_h ep, ucs_status_t status);
void ucp_ep_fence_pending_resume(ucp_ep_h ep);

/**
* @brief Query local and/or remote socket address of endpoint @a ucp_ep.
Expand Down
13 changes: 13 additions & 0 deletions src/ucp/core/ucp_ep.inl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,21 @@ ucp_ep_get_lane(ucp_ep_h ep, ucp_lane_index_t lane_index)
static UCS_F_ALWAYS_INLINE void ucp_ep_set_lane(ucp_ep_h ep, size_t lane_index,
uct_ep_h uct_ep)
{
uct_ep_h old_uct_ep;

ucs_assert(lane_index != UCP_NULL_LANE);

old_uct_ep = ucp_ep_get_lane(ep, lane_index);
if (old_uct_ep == uct_ep) {
return;
}

++ep->ext->lane_generation;
if ((old_uct_ep != NULL) &&
(ep->ext->unflushed_lanes & UCS_BIT(lane_index))) {
ep->ext->fence_lanes_dirty = 1;
}

if (lane_index < UCP_MAX_FAST_PATH_LANES) {
ep->uct_eps[lane_index] = uct_ep;
} else {
Expand Down
Loading
Loading