16#include <rdma/fabric.h>
17#include <rdma/fi_eq.h>
43 return std::holds_alternative<Idle>(
_state);
68 MXL_WARN(
"Shutdown requested while waiting to activate, aborting.");
73 if (
_proto->hasPendingWork())
75 MXL_INFO(
"Target removed that had outstanding writes, transitioning to removing state.");
76 return Removing{.fiAddr = state.fiAddr, .deadline = std::chrono::steady_clock::now() +
RemoveTimeout};
83 [](
Done state) ->
State {
return state; },
90 auto const state = std::get_if<Removing>(&
_state);
96 if (std::chrono::steady_clock::now() >= state->deadline)
98 MXL_WARN(
"Writes to the removed target did not complete within {} ms, removing it anyway.",
112 return std::holds_alternative<Done>(
_state);
118 if (
auto const state = std::get_if<Activated>(&
_state); state !=
nullptr)
120 _proto->transferGrain(ep, localIndex, remoteIndex, remotePayloadOffset, sliceRange, state->fiAddr);
126 if (
auto const state = std::get_if<Activated>(&
_state); state !=
nullptr)
128 _proto->transferSamples(ep, headIndex,
count, state->fiAddr);
136 [](
Idle const&) {
return true; },
138 [](
Removing const&) {
return true; },
139 [](
Done const&) {
return false; },
146 if (
auto error = completion.
tryErr(); error)
148 MXL_ERROR(
"Received a completion error: {}", error->toString());
164 requireCapability(info, FI_WRITE,
"Interface is missing required remote write capability");
170 cqAttr.size = std::max(cqAttr.size, info.
txSize());
173 cqAttr.size = *options.
cqDepth;
184 cqAttr.waitObject = FI_WAIT_NONE;
192 endpoint.bind(cq, FI_TRANSMIT | FI_RECV);
204 proto->registerMemory(domain);
208 MakeUniqueEnabler(
Endpoint ep, std::unique_ptr<EgressProtocolTemplate> proto)
213 return std::make_unique<MakeUniqueEnabler>(std::move(endpoint), std::move(proto));
217 :
_proto(std::move(proto))
225 throw Exception::exists(
"A target with endpoint id {} has already been added to this initiator.", targetInfo.
id);
229 auto proto =
_proto->createInstance(token, targetInfo);
230 proto->registerMemory(
_endpoint.domain());
244 if (remote.canEvict())
258 auto const deadline = std::chrono::steady_clock::now() +
ShutdownTimeout;
268 if (std::chrono::steady_clock::now() >= deadline)
288 std::uint16_t startSlice, std::uint16_t endSlice)
322 auto const deadline = std::chrono::steady_clock::now() + timeout;
325 if (std::holds_alternative<Initiator::Ready>(
makeProgress()))
330 auto const timeUntilDeadline = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - std::chrono::steady_clock::now());
331 if (timeUntilDeadline <= std::chrono::milliseconds::zero())
374 for (
auto const& [_, remote] :
_targets)
376 if (remote.hasPendingWork())
389 if (timeout == std::chrono::milliseconds::zero())
396 if (
auto completion =
_endpoint.completionQueue()->readBlocking(timeout); completion)
405 while (
auto completion =
_endpoint.completionQueue()->read())
415 auto failure = std::exception_ptr{};
426 failure = std::current_exception();
432 std::rethrow_exception(failure);
451 std::erase_if(
_targets, [](
auto const& item) {
return item.second.canEvict(); });
459 MXL_ERROR(
"Dropping completion for unknown target.");
463 it->second.handleCompletion(
_endpoint, completion);
Internal shared-memory headers for discrete grains and event slots.
static std::shared_ptr< AddressVector > open(std::shared_ptr< Domain > domain, Attributes attr=Attributes::defaults())
Create an AddressVector.
static std::shared_ptr< CompletionQueue > open(std::shared_ptr< Domain > domain, Attributes const &attr=Attributes::defaults())
Create a new completion queue in the specified domain.
static bool isWaitObjectSupportedForEFA() noexcept
Check if wait any wait object type supported by the EFA provider for this libfabric version.
Type to represent a completion entry from a CompletionQueue.
Token token() const noexcept
Get the token that was associated with the operation that has completed.
static Token randomToken()
std::uintptr_t Token
A token used to associate a completion entry with an endpoint.
std::optional< Error > tryErr() const noexcept
Attempt to access the error variant. Returns std::nullopt if this is a data entry.
static std::shared_ptr< Domain > open(std::shared_ptr< Fabric > fabric)
Open a new domain associated with the specified fabric.
RAII Wrapper around a libfabric endpoint (fi_ep).
std::shared_ptr< AddressVector > addressVector() const
Get the associated addres vector.
std::uintptr_t Id
An id type that can be used to reference this endpoint from completions and events.
static Endpoint create(std::shared_ptr< Domain > domain, FabricInfoView info)
Allocates (active) new endpoint.
static constexpr auto const EQReadInterval
The minimum interval at which the event queue will be polled when doing a blocking read of both (even...
static std::shared_ptr< EventQueue > open(std::shared_ptr< Fabric >, Attributes const &attr=Attributes::defaults())
Create a new event queue associated with the specified fabric.
Custom exception type used in the fabrics library.
static Exception make(mxlStatus status, fmt::format_string< T... > fmt, T &&... args)
Make any type of exception.
static Exception notFound(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_NOT_FOUND exception.
static Exception exists(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_EXISTS exception.
static Exception invalidState(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_STATE exception.
An internal exception type that extends.
bool isInterrupted() const noexcept
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
std::size_t txSize() const noexcept
Return the transmit queue size of the endpoint.
static std::shared_ptr< Fabric > open(FabricInfoView info)
Open a fabric object based on the provided FabricInfoView.
std::variant< Ready, NotReady, Interrupted > MakeProgressResult
static MxlRegions forReader(mxlFlowReader)
A single endpoint within an RDMInitiator.
void handleCompletion(Endpoint &ep, Completion completion)
Handle a completion event for this endpoint.
bool canEvict() const noexcept
Returns true if the target was shut down and can be evicted from the initiator.
void transferGrain(Endpoint const &ep, std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t remotePayloadOffset, SliceRange const &sliceRange)
Post a grain data transfer request to this endpoint.
bool isIdle() const noexcept
Returns true if the endpoint is idle and could be actived.
RDMInitiatorTarget(std::unique_ptr< EgressProtocol > proto, TargetInfo remoteInfo)
Construct a new RDMInitiatorEndpoint object.
bool hasPendingWork() const noexcept
Returns true if there's pending work for this target.
std::variant< Idle, Activated, Removing, Done > State
The various states that the endpoint can be in are stored inside a variant that we move from and then...
void finishRemoval(Endpoint &ep)
Complete the removal once all outstanding writes have completed or RemoveTimeout has elapsed.
void transferSamples(Endpoint const &ep, std::uint64_t headIndex, std::size_t count)
Post a sample data transfer request to this endpoint.
void shutdown(Endpoint &ep)
Stop transfers to this target.
void activate(Endpoint &ep)
Try to activate the endpoint.
std::unique_ptr< EgressProtocol > _proto
static constexpr auto RemoveTimeout
Upper bound for the removing state. A target that disappears may never complete the outstanding write...
std::map< Completion::Token, RDMInitiatorTarget > _targets
virtual void removeTarget(TargetInfo const &remoteTargetInfo) final
Remove a target from the initiator.
std::unique_ptr< EgressProtocolTemplate > _proto
std::map< Endpoint::Id, Completion::Token > _remoteEndpoints
RDMInitiator(Endpoint ep, std::unique_ptr< EgressProtocolTemplate > proto)
Construct a new RDMInitiator object.
Initiator::MakeProgressResult afterProgressResult() const noexcept
Returns true if any of the endpoints contained in this initiator have pending work.
virtual void addTarget(TargetInfo const &remoteTargetInfo) final
Add a target to the initiator.
void processCompletion(Completion completion)
Consume a completion entry.
static constexpr auto ShutdownPollInterval
Sleep time between two queue reads in shutdown().
void blockOnCQ(std::chrono::steady_clock::duration timeout)
Block on the completion queue until a completion arrives or the timeout elapses, then process all ava...
void evictRemovedTargets()
Complete the removal of targets whose outstanding writes have completed, and evict them.
virtual void transferGrainToTarget(Endpoint::Id targetId, std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t payloadOffset, std::uint16_t startSlice, std::uint16_t endSlice) final
Transfer a grain to a specific target.
virtual Initiator::MakeProgressResult makeProgressBlocking(std::chrono::steady_clock::duration timeout) final
Attempts to progress execution, including connection management and data operations.
static std::unique_ptr< RDMInitiator > setup(mxlFabricsInitiatorConfig const &config, FabricInfoView info, InitiatorSetupOptions const &options={})
Set up a new RDMInitiator.
virtual void transferSamples(std::uint64_t headIndex, std::size_t count) final
Transfer samples to all targets.
static constexpr auto ShutdownTimeout
Upper bound for shutdown(). It is longer than RDMInitiatorTarget::RemoveTimeout, so that each removal...
void activateIdleEndpoints()
Try to activate any idle endpoints.
RDMInitiatorTarget & findRemoteByEndpoint(Endpoint::Id id)
Find a remote target by its endpoint id.
void drainCQ()
Process completions until the completion queue is empty.
virtual void transferGrain(std::uint64_t grainIndex, std::uint16_t startSlice, std::uint16_t endSlice) final
Transfer a grain to all targets.
virtual Initiator::MakeProgressResult makeProgress() final
Attempts to progress execution, including connection management and data operations.
virtual void shutdown() final
Shut down the initiator gracefully.
void forEachTarget(F &&transfer)
Offer a transfer to every target.
RDMInitiatorTarget & findRemoteByToken(Completion::Token token)
Find a remote target by its completion token.
A SliceRange represents a range of slices within a grain.
static SliceRange make(std::uint16_t start, std::uint16_t end)
Create a SliceRange representing the range [start, end).
@ MXL_FABRICS_IFACE_CAP_BLOCKING_OPERATIONS
@ MXL_FABRICS_PROVIDER_EFA
struct mxlFabricsInitiatorConfig_t mxlFabricsInitiatorConfig
void drainEventQueue(EventQueue &eq)
Read all entries from a connectionless endpoint's event queue and log the errors among them.
std::unique_ptr< EgressProtocolTemplate > selectEgressProtocol(DataLayout const &layout, std::vector< Region > regions)
Select an appropriate egress protocol based on the data layout.
void requireCapability(FabricInfoView info, std::uint64_t cap, std::string_view message)
constexpr auto const MXL_GRAIN_PAYLOAD_OFFSET
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
mxlFabricsInterfaceConfig interface
mxlFabricsProvider provider
mxlFabricsInterfaceCaps caps
static Attributes defaults()
Returns the a CompletionQueueAttr with all of its attributes set to reasonable defaults.
Provider-independent tuning options for initiator setup.
std::optional< std::size_t > cqDepth
Desired completion-queue depth.
The endpoint activated state.
The endpoint is done and can be evicted from the initiator.
TargetInfo contains all the information required by an initiator to operate transfers to the given ta...