MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
RCInitiator.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6
7#include <chrono>
8#include <cstdint>
9#include <map>
10#include <memory>
11#include <variant>
12#include "Domain.hpp"
13#include "Endpoint.hpp"
14#include "Event.hpp"
15#include "Initiator.hpp"
16#include "Protocol.hpp"
17
19{
20
24 {
25 public:
32 RCInitiatorEndpoint(Endpoint, std::unique_ptr<EgressProtocol> proto, TargetInfo info);
33
36 [[nodiscard]]
37 TargetInfo const& info() const noexcept;
38
42 [[nodiscard]]
43 bool hasPendingWork() const noexcept;
44
47 [[nodiscard]]
48 bool isIdle() const noexcept;
49
52 [[nodiscard]]
53 bool canEvict() const noexcept;
54
61 void finishFlush();
62
67 void shutdown();
68
75 void activate(std::shared_ptr<CompletionQueue> const& cq, std::shared_ptr<EventQueue> const& eq);
76
79 void consume(Event event);
80
83 void consume(Completion completion);
84
87 void transferGrain(std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t remotePayloadOffset, SliceRange const& sliceRange);
88
91 void transferSamples(std::uint64_t headIndex, std::size_t count);
92
96 constexpr static auto FlushTimeout = std::chrono::seconds(2);
97
98 private:
104 struct Idle
105 {
107 std::chrono::steady_clock::time_point idleSince;
108 };
109
115 {
117 };
118
124 {
126 };
127
136
142 struct Flushing
143 {
145 std::size_t pending;
148 std::chrono::steady_clock::time_point deadline;
149 };
150
157 {};
158
161 struct Done
162 {};
163
167 using State = std::variant<Idle, Connecting, Connected, Flushing, Disconnected, Done>;
168
169 private:
172 [[nodiscard]]
173 static Flushing startFlush(Endpoint ep, std::size_t pending, bool shutdownReceived, AfterFlush next);
174
179 [[nodiscard]]
180 Idle restart(Endpoint const& endpoint);
181
182 private:
183 // The protocol is declared first so that it is destroyed after the endpoint in _state. Writes can read from memory that the
184 // protocol owns until the endpoint is closed.
185 std::unique_ptr<EgressProtocol> _proto;
188 };
189
192 class RCInitiator /*final*/ : public Initiator
193 {
194 public:
205 [[nodiscard]]
206 static std::unique_ptr<RCInitiator> setup(mxlFabricsInitiatorConfig const& config, FabricInfoView info,
207 InitiatorSetupOptions const& options = {});
208
211 virtual void addTarget(TargetInfo const& targetInfo) final;
212
215 virtual void removeTarget(TargetInfo const& targetInfo) final;
216
219 virtual void transferGrain(std::uint64_t grainIndex, std::uint16_t startSlice, std::uint16_t endSlice) final;
220
223 virtual void transferGrainToTarget(Endpoint::Id targetId, std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t payloadOffset,
224 std::uint16_t startSlice, std::uint16_t endSlice) final;
225
228 virtual void transferSamples(std::uint64_t headIndex, std::size_t count) final;
229
233
236 virtual Initiator::MakeProgressResult makeProgressBlocking(std::chrono::steady_clock::duration) final;
237
242 virtual void shutdown() final;
243
244 private:
247 [[nodiscard]]
249
252 [[nodiscard]]
253 bool hasTarget() const noexcept;
254
257 constexpr static auto EQPollInterval = std::chrono::milliseconds(100);
258
261 constexpr static auto ShutdownTimeout = RCInitiatorEndpoint::FlushTimeout + std::chrono::seconds(1);
262
265 constexpr static auto ShutdownPollInterval = std::chrono::milliseconds(1);
266
267 /*\brief Construct the RCInitiator.
268 *
269 * \param domain The domain to create the initiator on.
270 * \param cq The completion queue to use for all endpoints created by this initiator.
271 * \param eq The event queue to use for all endpoints created by this initiator.
272 */
273 RCInitiator(std::shared_ptr<Domain> domain, std::shared_ptr<CompletionQueue> cq, std::shared_ptr<EventQueue> eq,
274 std::unique_ptr<EgressProtocolTemplate> proto);
275
278 [[nodiscard]]
279 Initiator::MakeProgressResult blockOnCQ(std::chrono::steady_clock::duration timeout);
280
283 void pollCQ();
284
287 void pollEQ();
288
292
296
300
303 void evictDeadEndpoints();
304
305 private:
306 std::shared_ptr<Domain> _domain;
307 std::shared_ptr<CompletionQueue> _cq;
308 std::shared_ptr<EventQueue> _eq;
309 std::unique_ptr<EgressProtocolTemplate> _proto;
310
311 std::map<Completion::Token, RCInitiatorEndpoint> _targets{};
312 };
313}
Type to represent a completion entry from a CompletionQueue.
RAII Wrapper around a libfabric endpoint (fi_ep).
Definition Endpoint.hpp:29
std::uintptr_t Id
An id type that can be used to reference this endpoint from completions and events.
Definition Endpoint.hpp:38
Type to represent an event entry from an EventQueue.
Definition Event.hpp:21
Non-owning view of a FabricInfo. Returned when iterating over a FabricInfoList.
Abstract base class for Initiator implementations.
Definition Initiator.hpp:31
std::variant< Ready, NotReady, Interrupted > MakeProgressResult
Definition Initiator.hpp:42
A single endpoint connected to a target that will be created when the user adds a target to an initia...
std::variant< Idle, Connecting, Connected, Flushing, Disconnected, Done > State
The various states that the endpoint can be in are stored inside a variant that we move from and then...
void transferGrain(std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t remotePayloadOffset, SliceRange const &sliceRange)
Post a data transfer request to this endpoint.
void shutdown()
Initiate a shutdown process.
RCInitiatorEndpoint(Endpoint, std::unique_ptr< EgressProtocol > proto, TargetInfo info)
Construct a new RCInitiatorEndpoint object.
AfterFlush
The state that the endpoint goes to when a flush is complete.
static constexpr auto FlushTimeout
Upper bound for the flushing state. Some providers do not report a local shutdown event,...
void consume(Event event)
Consume an event that was posted to the associated event queue.
static Flushing startFlush(Endpoint ep, std::size_t pending, bool shutdownReceived, AfterFlush next)
Create a flushing state that waits for the given number of outstanding operations.
Idle restart(Endpoint const &endpoint)
Restarts the endpoint. Produces a new Idle state from any previous state.
void transferSamples(std::uint64_t headIndex, std::size_t count)
Post a data transfer request to this endpoint.
TargetInfo const & info() const noexcept
Get the target info of the remote endpoint.
void finishFlush()
Move the endpoint out of the flushing state when the flush is complete.
bool isIdle() const noexcept
Returns true if the endpoint is idle and could be actived.
void activate(std::shared_ptr< CompletionQueue > const &cq, std::shared_ptr< EventQueue > const &eq)
Try to activate the endpoint.
std::unique_ptr< EgressProtocol > _proto
bool canEvict() const noexcept
Returns true if the endpoint was shut down and can be evicted from the initiator.
bool hasPendingWork() const noexcept
Returns true if there is any pending events that the endpoint is waiting for, and for which the queue...
std::shared_ptr< EventQueue > _eq
void activateIfIdle(RCInitiatorEndpoint &target)
Try to activate the endpoint if it is idle. Called before a transfer is posted to the endpoint.
void evictDeadEndpoints()
Evict any dead endpoints that are no longer used.
std::unique_ptr< EgressProtocolTemplate > _proto
virtual void transferSamples(std::uint64_t headIndex, std::size_t count) final
Transfer samples to all targets.
RCInitiator(std::shared_ptr< Domain > domain, std::shared_ptr< CompletionQueue > cq, std::shared_ptr< EventQueue > eq, std::unique_ptr< EgressProtocolTemplate > proto)
Initiator::MakeProgressResult afterProgressResult() const noexcept
Returns true if any of the endpoints contained in this initiator have pending work.
virtual void addTarget(TargetInfo const &targetInfo) final
Add a target to the initiator.
virtual Initiator::MakeProgressResult makeProgressBlocking(std::chrono::steady_clock::duration) final
Attempts to progress execution, including connection management and data operations.
Initiator::MakeProgressResult blockOnCQ(std::chrono::steady_clock::duration timeout)
Block on the completion queue with a timeout.
static constexpr auto EQPollInterval
When making progress and using blocking queue reads, this is the minimum interval at which the event ...
static constexpr auto ShutdownPollInterval
Sleep time between two queue reads in shutdown().
virtual void shutdown() final
Shut down the initiator gracefully.
void activateIdleEndpoints()
Try to activate any idle endpoints.
virtual Initiator::MakeProgressResult makeProgress() final
Attempts to progress execution, including connection management and data operations.
void finishFlushingEndpoints()
Move endpoints that have finished flushing to the done state.
std::shared_ptr< CompletionQueue > _cq
virtual void transferGrain(std::uint64_t grainIndex, std::uint16_t startSlice, std::uint16_t endSlice) final
Transfer a grain to all targets.
std::map< Completion::Token, RCInitiatorEndpoint > _targets
void pollEQ()
Poll the event queue and process the returned events until the queue is empty.
std::shared_ptr< Domain > _domain
bool hasTarget() const noexcept
Returns true if the initiator has at least 1 target added no matter what the state is.
static std::unique_ptr< RCInitiator > setup(mxlFabricsInitiatorConfig const &config, FabricInfoView info, InitiatorSetupOptions const &options={})
Set up a fresh initiator without any targets assigned to it.
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.
static constexpr auto ShutdownTimeout
Upper bound for shutdown(). It is longer than RCInitiatorEndpoint::FlushTimeout, so that each flush e...
virtual void removeTarget(TargetInfo const &targetInfo) final
Remove a target from the initiator.
void pollCQ()
Poll the completion queue and process the events until the queue is empty.
A SliceRange represents a range of slices within a grain.
struct mxlFabricsInitiatorConfig_t mxlFabricsInitiatorConfig
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
Provider-independent tuning options for initiator setup.
Definition Initiator.hpp:19
The endpoint is done and can be evicted from the initiator.
std::chrono::steady_clock::time_point deadline
std::chrono::steady_clock::time_point idleSince
TargetInfo contains all the information required by an initiator to operate transfers to the given ta...