MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
FILogging.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 "FILogging.hpp"
6#include <cctype>
7#include <cstdlib>
8#include <algorithm>
9#include <atomic>
10#include <string_view>
11#include <rdma/fabric.h>
12#include <rdma/fi_ext.h>
13#include <rdma/providers/fi_log.h>
14#include <spdlog/common.h>
15#include "spdlog/spdlog.h"
16#include "Exception.hpp"
17#include "FabricVersion.hpp"
18
20{
21 namespace
22 {
23 constexpr auto fiLogLevelStrings = std::array<std::pair<std::string_view, ::fi_log_level>, 4>{
24 {{"trace", FI_LOG_TRACE}, {"debug", FI_LOG_DEBUG}, {"info", FI_LOG_INFO}, {"warn", FI_LOG_WARN}}
25 };
26
27 static int fiLogEnabled(const struct fi_provider* prov, enum fi_log_level level, enum fi_log_subsys subsys, std::uint64_t flags);
28 static int fiLogReady(const struct fi_provider* prov, enum fi_log_level level, enum fi_log_subsys subsys, std::uint64_t flags,
29 std::uint64_t* showtime);
30 void fiLog(const struct fi_provider* prov, enum fi_log_level level, enum fi_log_subsys subsys, char const*, int line, char const* msgIn);
31
32 class FILogging
33 {
34 public:
35 void init()
36 {
37 // It is safe to call a logging functions on the libfabric side
38 // while we initialize logging here. We just need to make sure
39 // to initialize logging only once.
40 if (_isInit.exchange(true, std::memory_order_relaxed))
41 {
42 return;
43 }
44
45 auto fiLogLevelCStr = ::getenv("FI_LOG_LEVEL");
46 if (fiLogLevelCStr != nullptr)
47 {
48 auto fiLogLevelStr = std::string{fiLogLevelCStr};
49 std::ranges::transform(fiLogLevelStr, fiLogLevelStr.begin(), [](unsigned char const c) { return std::tolower(c); });
50 auto it = std::ranges::find_if(fiLogLevelStrings,
51 [fiLogLevelStr](std::pair<std::string_view, ::fi_log_level> const& item) { return fiLogLevelStr == item.first; });
52 if (it != fiLogLevelStrings.end())
53 {
54 _level = it->second;
55 }
56 else
57 {
58 _level = FI_LOG_WARN;
59 }
60 }
61
62 auto ops = ::fi_ops_log{
63 .size = sizeof(::fi_ops_log),
64 .enabled = &fiLogEnabled,
65 .ready = &fiLogReady,
66 .log = &fiLog,
67 };
68
69 auto logging = ::fid_logging{
70 .fid = ::fid{},
71 .ops = &ops,
72 };
73
74 fiCall(::fi_import_log, "Failed to initialize logging", fiVersion(), 0, &logging);
75 }
76
77 private:
78 friend int fiLogEnabled(const struct fi_provider*, enum fi_log_level, enum fi_log_subsys, std::uint64_t);
79
80 [[nodiscard]]
81 fi_log_level level() const noexcept
82 {
83 return _level;
84 }
85
86 [[nodiscard]]
87 bool isProviderLoggingEnabled(const struct fi_provider*) const noexcept
88 {
89 return true;
90 }
91
92 [[nodiscard]]
93 bool isSubsystemLoggingEnabled(enum fi_log_subsys) const noexcept
94 {
95 return true;
96 }
97
98 std::atomic_bool _isInit;
99 ::fi_log_level _level;
100 };
101
102 FILogging logging;
103
104 char const* fiLogSubsystemName(enum fi_log_subsys subsys)
105 {
106 switch (subsys)
107 {
108 case FI_LOG_CORE: return "core";
109 case FI_LOG_FABRIC: return "fabric";
110 case FI_LOG_DOMAIN: return "domain";
111 case FI_LOG_EP_CTRL: return "ep_ctrl";
112 case FI_LOG_EP_DATA: return "ep_data";
113 case FI_LOG_AV: return "av";
114 case FI_LOG_CQ: return "cq";
115 case FI_LOG_EQ: return "eq";
116 case FI_LOG_MR: return "mr";
117 case FI_LOG_CNTR: return "cntr";
118 default: return "";
119 }
120 }
121
122 spdlog::level::level_enum fiLevelToSpdlogLevel(enum fi_log_level level)
123 {
124 switch (level)
125 {
126 case FI_LOG_WARN: return spdlog::level::warn;
127 case FI_LOG_TRACE: return spdlog::level::trace;
128 case FI_LOG_INFO: return spdlog::level::info;
129 case FI_LOG_DEBUG: return spdlog::level::debug;
130 default: return spdlog::level::warn;
131 }
132 }
133
134 int fiLogEnabled(const struct fi_provider* prov, enum fi_log_level level, enum fi_log_subsys subsys, std::uint64_t)
135 {
136 return (level <= logging.level() || logging.level() == FI_LOG_TRACE) && logging.isProviderLoggingEnabled(prov) &&
137 logging.isSubsystemLoggingEnabled(subsys);
138 }
139
140 int fiLogReady(const struct fi_provider*, enum fi_log_level, enum fi_log_subsys, std::uint64_t, std::uint64_t*)
141 {
142 return 0;
143 }
144
145 void fiLog(const struct fi_provider* prov, enum fi_log_level level, enum fi_log_subsys subsys, char const*, int line, char const* msgIn)
146 {
147 auto msg = std::string{msgIn};
148 if (msg.ends_with('\n'))
149 {
150 msg.pop_back();
151 }
152
153 spdlog::log(
154 {fmt::format("libfabric:{}:{}", fiLogSubsystemName(subsys), prov->name).c_str(), line, ""}, fiLevelToSpdlogLevel(level), "{}", msg);
155 }
156
157 }
158
159 // Initialize logging
161 {
162 logging.init();
163 }
164}
void fiInitLogging()
Initalize logging for libfabric. After this call, all libfabric internal logs will be written to the ...
std::uint32_t fiVersion() noexcept
Returns the libfabric version this library was compiled with.
int fiCall(F &&fun, std::string_view msg, T &&... args)
Call a libfabric function and check its return code.
gInfo flags
Mark the grain as invalid.