Skip to content

Commit 8ed97a4

Browse files
authored
[coro_io] fix ib_socket log & ib_buffer_pool (#962)
fix buffer pool
1 parent 576ff8c commit 8ed97a4

File tree

7 files changed

+201
-175
lines changed

7 files changed

+201
-175
lines changed

include/ylt/coro_io/coro_io.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,10 @@ async_sendfile(asio::ip::tcp::socket &socket, int fd, off_t offset,
782782
}
783783
#endif
784784

785-
enum protocal { tcp, tcp_with_ssl, rdma };
786785
struct endpoint {
786+
enum protocal { tcp, rdma };
787787
asio::ip::address address;
788-
asio::ip::port_type port;
788+
uint32_t port;
789789
protocal proto;
790790
};
791791

include/ylt/coro_io/ibverbs/ib_buffer.hpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ struct ib_buffer_t {
5555
IBV_ACCESS_REMOTE_READ |
5656
IBV_ACCESS_REMOTE_WRITE) {
5757
auto mr = ibv_reg_mr(dev->pd(), ptr, size, ib_flags);
58-
ELOG_INFO << "ibv_reg_mr regist: " << mr << " with pd:" << dev->pd();
58+
ELOG_TRACE << "ibv_reg_mr regist: " << mr << " with pd:" << dev->pd();
5959
if (mr != nullptr) [[unlikely]] {
60-
ELOG_INFO << "regist sge.lkey: " << mr->lkey << ", sge.addr: " << mr->addr
61-
<< ", sge.length: " << mr->length;
60+
ELOG_TRACE << "regist sge.lkey: " << mr->lkey
61+
<< ", sge.addr: " << mr->addr
62+
<< ", sge.length: " << mr->length;
6263
return ib_buffer_t{mr, std::move(dev)};
6364
}
6465
else {
@@ -144,7 +145,6 @@ class ib_buffer_pool_t : public std::enable_shared_from_this<ib_buffer_pool_t> {
144145
}
145146
void collect_free_inner(ib_buffer_t buffer) {
146147
if (buffer) {
147-
ELOG_TRACE << "collecting:" << (void*)buffer->addr;
148148
if (free_buffers_.size() * pool_config_.buffer_size <
149149
pool_config_.max_memory_usage) {
150150
ELOG_TRACE << "collect free buffer{data:" << buffer->addr << ",len"
@@ -158,9 +158,6 @@ class ib_buffer_pool_t : public std::enable_shared_from_this<ib_buffer_pool_t> {
158158
<< "} wont be collect";
159159
}
160160
}
161-
else {
162-
ELOG_TRACE << "collecting nullptr";
163-
}
164161
return;
165162
};
166163

@@ -173,9 +170,6 @@ class ib_buffer_pool_t : public std::enable_shared_from_this<ib_buffer_pool_t> {
173170
buffer.change_owner(weak_from_this());
174171
collect_free_inner(std::move(buffer));
175172
}
176-
else {
177-
ELOG_TRACE << "collecting nullptr";
178-
}
179173
return;
180174
};
181175
ib_buffer_t get_buffer() {
@@ -188,11 +182,11 @@ class ib_buffer_pool_t : public std::enable_shared_from_this<ib_buffer_pool_t> {
188182
pool_config_.buffer_size +
189183
total_memory_.load(std::memory_order_acquire)) [[unlikely]] {
190184
ELOG_WARN << "Memory out of pool limit";
191-
// return std::move(buffer);
185+
return std::move(buffer);
192186
}
193187
auto ptr = malloc(pool_config_.buffer_size);
194188
if (ptr == nullptr) {
195-
ELOG_TRACE << "Allocate failed.";
189+
ELOG_ERROR << "ib_buffer_pool allocate failed.";
196190
throw std::bad_alloc();
197191
}
198192
buffer =
@@ -284,9 +278,7 @@ inline ib_buffer_t::~ib_buffer_t() {
284278
}
285279

286280
inline void ib_buffer_t::collect() && {
287-
ELOG_TRACE << "collecting buffer";
288281
if (auto pool = owner_pool_.lock(); pool) {
289-
ELOG_TRACE << "has pool. collecting buffer continue";
290282
pool->collect_free_inner(std::move(*this));
291283
}
292284
}

include/ylt/coro_io/ibverbs/ib_device.hpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99
#include <system_error>
1010

11+
#include "asio/ip/address.hpp"
1112
#include "ylt/easylog.hpp"
1213

1314
namespace coro_io {
@@ -101,14 +102,25 @@ struct ib_deleter {
101102
}
102103
}
103104
void operator()(ibv_mr* ptr) const noexcept {
104-
ELOG_INFO << "ibv_reg_mr unregist: " << ptr;
105105
if (auto ret = ibv_dereg_mr(ptr); ret) [[unlikely]] {
106106
ELOG_ERROR << "ibv_dereg_mr failed: "
107107
<< std::make_error_code(std::errc{ret}).message();
108108
}
109109
}
110110
};
111111

112+
namespace detail {
113+
inline std::string gid_to_string(uint8_t (&a)[16]) noexcept {
114+
std::string ret;
115+
ret.resize(40);
116+
sprintf(ret.data(),
117+
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%"
118+
"02x%02x",
119+
a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10],
120+
a[11], a[12], a[13], a[14], a[15]);
121+
return ret;
122+
}
123+
112124
inline std::string mtu_str(ibv_mtu mtu) {
113125
std::string str;
114126
switch (mtu) {
@@ -132,7 +144,7 @@ inline std::string mtu_str(ibv_mtu mtu) {
132144
}
133145
return str;
134146
}
135-
147+
} // namespace detail
136148
class ib_device_t {
137149
public:
138150
ib_device_t(const ib_config_t& conf) {
@@ -150,13 +162,11 @@ class ib_device_t {
150162
throw std::system_error(ec);
151163
}
152164

153-
ELOG_INFO << "Active MTU: " << mtu_str(attr_.active_mtu) << ", "
154-
<< "Max MTU: " << mtu_str(attr_.max_mtu);
165+
ELOG_TRACE << "Active MTU: " << detail::mtu_str(attr_.active_mtu) << ", "
166+
<< "Max MTU: " << detail::mtu_str(attr_.max_mtu);
155167

156168
find_best_gid_index();
157169

158-
ELOG_INFO << "IBDevice " << name_ << ", best gid index " << gid_index_;
159-
160170
if (gid_index_ >= 0) {
161171
if (auto ec = ibv_query_gid(ctx_.get(), conf.port, gid_index_, &gid_);
162172
ec) {
@@ -166,6 +176,15 @@ class ib_device_t {
166176
<< ", error msg: " << err_code.message();
167177
throw std::system_error(err_code);
168178
}
179+
std::error_code err_code;
180+
gid_address_ =
181+
asio::ip::make_address(detail::gid_to_string(gid_.raw), err_code);
182+
if (err_code) {
183+
ELOG_ERROR << "IBDevice failed to convert gid to ip address of device "
184+
<< name_ << " by gid_index:" << gid_index_
185+
<< ", error msg: " << err_code.message();
186+
throw std::system_error(err_code);
187+
}
169188
}
170189
else {
171190
ELOG_ERROR << "gid index should greater than zero, now is: "
@@ -189,6 +208,7 @@ class ib_device_t {
189208
ibv_pd* pd() const noexcept { return pd_.get(); }
190209
int gid_index() const noexcept { return gid_index_; }
191210
const ibv_gid& gid() const noexcept { return gid_; }
211+
asio::ip::address gid_address() const noexcept { return gid_address_; }
192212
const ibv_port_attr& attr() const noexcept { return attr_; }
193213

194214
private:
@@ -225,6 +245,7 @@ class ib_device_t {
225245
std::unique_ptr<ibv_context, ib_deleter> ctx_;
226246
ibv_port_attr attr_;
227247
ibv_gid gid_;
248+
asio::ip::address gid_address_;
228249
ibv_device* device_;
229250
int gid_index_;
230251

include/ylt/coro_io/ibverbs/ib_io.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ inline async_simple::coro::Lazy<std::error_code> async_accept(
4343
if (ec) [[unlikely]] {
4444
co_return std::move(ec);
4545
}
46-
// TODO: SSL?
4746
auto ret = co_await ib_socket.accept(std::move(soc));
4847
ELOGV(INFO, "accept over:%s", ret.message().data());
4948
co_return ret;

0 commit comments

Comments
 (0)