MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
EventQueue.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 "EventQueue.hpp"
6#include <chrono>
7#include <cstdint>
8#include <memory>
9#include <optional>
10#include <utility>
12#include <rdma/fi_eq.h>
13#include <rdma/fi_errno.h>
14#include "Exception.hpp"
15#include "Fabric.hpp"
16
18{
19
21 {
23 attr.size = 8; // default size, this should be parameterized
24 return attr;
25 }
26
27 ::fi_eq_attr EventQueue::Attributes::raw() const noexcept
28 {
29 ::fi_eq_attr raw{};
30 raw.size = size;
31 raw.wait_obj = FI_WAIT_UNSPEC; // default wait object, this should be parameterized
32 raw.wait_set = nullptr; // only used if wait_obj is FI_WAIT_SET
33 raw.flags = 0; // if signaling vector is required, this should use the flag "FI_AFFINITY"
34 raw.signaling_vector = 0; // this should indicate the CPU core that interrupts associated with the eq should target.
35 return raw;
36 }
37
38 std::shared_ptr<EventQueue> EventQueue::open(std::shared_ptr<Fabric> fabric, EventQueue::Attributes const& attr)
39 {
40 ::fid_eq* eq;
41 auto eq_attr = attr.raw();
42
43 fiCall(::fi_eq_open, "Failed to open event queue", fabric->raw(), &eq_attr, &eq, nullptr);
44
45 // expose the private constructor to std::make_shared inside this function
46 struct MakeSharedEnabler : public EventQueue
47 {
48 MakeSharedEnabler(::fid_eq* raw, std::shared_ptr<Fabric> fabric)
49 : EventQueue(raw, fabric)
50 {}
51 };
52
53 return std::make_shared<MakeSharedEnabler>(eq, fabric);
54 }
55
56 std::optional<Event> EventQueue::read()
57 {
58 std::uint32_t eventType;
59 ::fi_eq_entry entry;
60
61 auto const ret = fi_eq_read(_raw, &eventType, &entry, sizeof(entry), 0);
62
63 return handleReadResult(ret, eventType, entry);
64 }
65
66 std::optional<Event> EventQueue::readBlocking(std::chrono::steady_clock::duration timeout)
67 {
68 auto timeoutMs = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
69 if (timeoutMs == 0)
70 {
71 return read();
72 }
73
74 std::uint32_t eventType;
75 ::fi_eq_entry entry;
76
77 auto const ret = fi_eq_sread(_raw, &eventType, &entry, sizeof(entry), timeoutMs, 0);
78
79 return handleReadResult(ret, eventType, entry);
80 }
81
82 EventQueue::EventQueue(::fid_eq* raw, std::shared_ptr<Fabric> fabric)
83 : _raw(raw)
84 , _fabric(std::move(fabric))
85 {}
86
88 {
89 catchAndLogFabricError([this]() { close(); }, "Failed to close event queue");
90 }
91
92 ::fid_eq* EventQueue::raw() noexcept
93 {
94 return _raw;
95 }
96
97 ::fid_eq const* EventQueue::raw() const noexcept
98 {
99 return _raw;
100 }
101
103 {
104 if (_raw)
105 {
106 fiCall(::fi_close, "Failed to close event queue", &_raw->fid);
107 _raw = nullptr;
108 }
109 }
110
111 std::optional<Event> EventQueue::handleReadResult(ssize_t ret, std::uint32_t eventType, ::fi_eq_entry const& entry)
112 {
113 if (ret == -FI_EAGAIN)
114 {
115 // No event available
116 return std::nullopt;
117 }
118
119 if (ret == -FI_EAVAIL)
120 {
121 ::fi_eq_err_entry eq_err{};
122 fi_eq_readerr(_raw, &eq_err, 0);
123
124 return Event::fromError(this->shared_from_this(), &eq_err);
125 }
126
127 if (ret < 0)
128 {
129 throw FabricException::make(ret, "Failed to read from event queue: {}", ::fi_strerror(ret));
130 }
131
132 switch (eventType)
133 {
134 case FI_CONNECTED:
135 case FI_CONNREQ:
136 case FI_SHUTDOWN: return Event::fromRawCMEntry(*reinterpret_cast<fi_eq_cm_entry const*>(&entry), eventType); break;
137 case FI_NOTIFY:
138 case FI_MR_COMPLETE:
139 case FI_AV_COMPLETE:
140 case FI_JOIN_COMPLETE: return Event::fromRawEntry(entry, eventType); break;
141 default: MXL_WARN("Unhandled event on event queue: code: {}", eventType); return std::nullopt;
142 }
143 }
144}
#define MXL_WARN(...)
Definition Logging.hpp:56
std::optional< Event > handleReadResult(ssize_t, std::uint32_t, ::fi_eq_entry const &)
Handle the result of a blocking or non-blocking read.
void close()
Releases all underlying resources. This is called from the destructor and the move-assignment operato...
std::shared_ptr< Fabric > _fabric
std::optional< Event > readBlocking(std::chrono::steady_clock::duration timeout)
Perform a blocking read on the event queue.
::fid_eq * raw() noexcept
Returns a raw handle to the event queue.
EventQueue(EventQueue const &)=delete
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.
std::optional< Event > read()
Perform a non-blocking read on the event queue. If no event is available in the queue,...
static Event fromRawCMEntry(::fi_eq_cm_entry const &raw, std::uint32_t eventType)
create an Event from a libfabric Connection Management event
Definition Event.cpp:91
static Event fromRawEntry(::fi_eq_entry const &raw, std::uint32_t eventType)
Definition Event.cpp:86
static Event fromError(std::shared_ptr< EventQueue > queue, ::fi_eq_err_entry const *raw)
create an Event from a libfabric event error entry
Definition Event.cpp:104
static FabricException make(int fiErrno, fmt::format_string< T... > fmt, T &&... args)
Create a new exception object.
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.
Attributes to configure newly created event queues.
static Attributes defaults()
Creates a configuration with all values set to reasonable defaults.
::fi_eq_attr raw() const noexcept
Returns the raw libfabric version of this object.