MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
CompletionQueue.cpp
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#include "CompletionQueue.hpp"
6#include <memory>
7#include <optional>
8#include <utility>
9#include <fcntl.h>
10#include <sys/types.h>
12#include <rdma/fi_eq.h>
13#include <rdma/fi_errno.h>
14#include "Completion.hpp"
15#include "Domain.hpp"
16#include "Exception.hpp"
17
19{
20
22 {
24 attr.size = DEFAULT_SIZE;
25 attr.waitObject = FI_WAIT_UNSPEC; // TODO: EFA will require no wait object
26 return attr;
27 }
28
29 ::fi_cq_attr CompletionQueue::Attributes::raw() const noexcept
30 {
31 ::fi_cq_attr raw{};
32 raw.size = size;
33 raw.wait_obj = waitObject;
34 raw.format = FI_CQ_FORMAT_DATA; // default format, this should be parameterized
35 raw.wait_cond = FI_CQ_COND_NONE; // default condition, this should be parameterized
36 raw.wait_set = nullptr; // only used if wait_obj is FI_WAIT_SET
37 raw.flags = 0; // if signaling vector is required, this should use the flag "FI_AFFINITY"
38 raw.signaling_vector = 0; // this should indicate the CPU core that interrupts associated with the cq should target.
39 return raw;
40 }
41
43 {
44 auto const fiVersion = ::fi_version();
45
46 // check that library version >= 2.4
47 return (FI_MAJOR(fiVersion) > 2) || ((FI_MAJOR(fiVersion) == 2) && (FI_MINOR(fiVersion) >= 4));
48 }
49
50 std::shared_ptr<CompletionQueue> CompletionQueue::open(std::shared_ptr<Domain> domain, CompletionQueue::Attributes const& attr)
51 {
52 ::fid_cq* cq;
53 auto cq_attr = attr.raw();
54
55 fiCall(::fi_cq_open, "Failed to open completion queue", domain->raw(), &cq_attr, &cq, nullptr);
56
57 // expose the private constructor to std::make_shared inside this function
58 struct MakeSharedEnabler : public CompletionQueue
59 {
60 MakeSharedEnabler(::fid_cq* raw, std::shared_ptr<Domain> domain)
61 : CompletionQueue(raw, domain)
62 {}
63 };
64
65 return std::make_shared<MakeSharedEnabler>(cq, domain);
66 }
67
68 std::optional<Completion> CompletionQueue::read()
69 {
70 fi_cq_data_entry entry;
71
72 ssize_t ret = fi_cq_read(_raw, &entry, 1);
73
74 return handleReadResult(ret, entry);
75 }
76
77 std::optional<Completion> CompletionQueue::readBlocking(std::chrono::steady_clock::duration timeout)
78 {
79 auto timeoutMs = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
80 if (timeoutMs == 0)
81 {
82 return read();
83 }
84
85 fi_cq_data_entry entry;
86
87 ssize_t ret = fi_cq_sread(_raw, &entry, 1, nullptr, timeoutMs);
88 return handleReadResult(ret, entry);
89 }
90
91 CompletionQueue::CompletionQueue(::fid_cq* raw, std::shared_ptr<Domain> domain)
92 : _raw(raw)
93 , _domain(std::move(domain))
94 {}
95
97 {
98 catchAndLogFabricError([this]() { close(); }, "Failed to close completion queue");
99 }
100
101 ::fid_cq* CompletionQueue::raw() noexcept
102 {
103 return _raw;
104 }
105
106 ::fid_cq const* CompletionQueue::raw() const noexcept
107 {
108 return _raw;
109 }
110
112 {
113 if (_raw)
114 {
115 MXL_DEBUG("Closing completion queue");
116
117 fiCall(::fi_close, "Failed to close completion queue", &_raw->fid);
118 _raw = nullptr;
119 }
120 }
121
122 std::optional<Completion> CompletionQueue::handleReadResult(ssize_t ret, ::fi_cq_data_entry const& entry)
123 {
124 if (ret == -FI_EAGAIN)
125 {
126 // No entry available
127 return std::nullopt;
128 }
129
130 if (ret == -FI_EAVAIL)
131 {
132 // An entry is available but in the error queue
133 // Zeroed: a non-zero err_data_size makes fi_cq_readerr copy into err_data.
134 ::fi_cq_err_entry err{};
135 if (auto const rc = ::fi_cq_readerr(_raw, &err, 0); rc < 0)
136 {
137 if (rc == -FI_EAGAIN)
138 {
139 return std::nullopt;
140 }
141 throw FabricException::make(static_cast<int>(rc), "Failed to read completion error: {}", ::fi_strerror(static_cast<int>(-rc)));
142 }
143
144 return Completion{
145 Completion::Error{err, this->shared_from_this()}
146 };
147 }
148
149 if (ret < 0)
150 {
151 throw FabricException::make(ret, "Failed to read completion from queue: {}", ::fi_strerror(ret));
152 }
153
154 return Completion{Completion::Data{entry}};
155 }
156}
#define MXL_DEBUG(...)
Definition Logging.hpp:34
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.
::fid_cq * raw() noexcept
Mutable accessor of the underlying fi_cq instance.
CompletionQueue(CompletionQueue const &)=delete
void close()
Releases all underlying resources. This is called from the destructor and the move-assignment operato...
static bool isWaitObjectSupportedForEFA() noexcept
Check if wait any wait object type supported by the EFA provider for this libfabric version.
std::optional< Completion > readBlocking(std::chrono::steady_clock::duration timeout)
Perform a blocking read on the completion queue.
std::optional< Completion > handleReadResult(ssize_t ret, ::fi_cq_data_entry const &entry)
Handle the result of a blocking or non-blocking read.
std::optional< Completion > read()
Perform a non-blocking read on the completion queue.
Data variant of the completion entry instance.
Error variant of the completion entry instance.
Type to represent a completion entry from a CompletionQueue.
static FabricException make(int fiErrno, fmt::format_string< T... > fmt, T &&... args)
Create a new exception object.
std::uint32_t fiVersion() noexcept
Returns the libfabric version this library was compiled with.
void catchAndLogFabricError(F &&fun, std::string_view msg)
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.
A collection of attributes that can be used to configure the operation of newly created completion qu...
static constexpr std::size_t DEFAULT_SIZE
The completion-queue depth used when no explicit depth is requested.
::fi_cq_attr raw() const noexcept
Returns the raw libfabric version of this object.
static Attributes defaults()
Returns the a CompletionQueueAttr with all of its attributes set to reasonable defaults.