Skip to content

Commit 1fb9370

Browse files
author
sunce4t
committed
Change GPU memory detection logics in baidu_rpc_protocol
1 parent b856888 commit 1fb9370

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

src/brpc/policy/baidu_rpc_protocol.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ ParseResult ParseRpcMessage(butil::IOBuf* source, Socket* socket,
115115

116116
#if BRPC_WITH_GDR
117117
void* prefetch_d2h_data = NULL;
118-
uint64_t data_meta = source->get_first_data_meta();
119-
bool is_gpu_memory = (data_meta > 0 && data_meta <= UINT_MAX);
118+
uint32_t data_meta = source->get_first_data_meta_high32();
119+
bool is_gpu_memory = (data_meta == static_cast<uint32_t>(butil::IOBuf::GPU_MEMORY));
120120
butil::gdr::BlockPoolAllocator* host_allocator = butil::gdr::BlockPoolAllocators::singleton()->get_cpu_allocator();
121121
if (is_gpu_memory) {
122122
prefetch_d2h_data = host_allocator->AllocateRaw(prefetch_d2h_size);
@@ -859,8 +859,8 @@ void ProcessRpcRequest(InputMessageBase* msg_base) {
859859
int body_without_attachment_size = req_size - meta.attachment_size();
860860
#if BRPC_WITH_GDR
861861
int meta_size = msg->meta.size();
862-
uint64_t data_meta = msg->payload.get_first_data_meta();
863-
bool is_gpu_memory = (data_meta > 0 && data_meta <= UINT_MAX);
862+
uint32_t data_meta = msg->payload.get_first_data_meta_high32();
863+
bool is_gpu_memory = (data_meta == static_cast<uint32_t>(butil::IOBuf::GPU_MEMORY));
864864
if(is_gpu_memory) {
865865
int64_t real_prefetch_d2h_size = msg->meta.get_first_data_meta();
866866
if (header_size + meta_size + body_without_attachment_size <= real_prefetch_d2h_size) {
@@ -1054,8 +1054,8 @@ void ProcessRpcResponse(InputMessageBase* msg_base) {
10541054
butil::IOBuf* res_buf_ptr = &msg->payload;
10551055

10561056
#if BRPC_WITH_GDR
1057-
uint64_t data_meta = msg->payload.get_first_data_meta();
1058-
bool is_gpu_memory = (data_meta > 0 && data_meta <= UINT_MAX);
1057+
uint32_t data_meta = msg->payload.get_first_data_meta_high32();
1058+
bool is_gpu_memory = (data_meta == static_cast<uint32_t>(butil::IOBuf::GPU_MEMORY));
10591059
#endif // BRPC_WITH_GDR
10601060
if (meta.has_attachment_size()) {
10611061
if (meta.attachment_size() > res_size) {

src/brpc/rdma/rdma_endpoint.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,8 @@ int RdmaEndpoint::PostRecv(uint32_t num, bool zerocopy) {
10561056
void* device_ptr = device_allocator->AllocateRaw(g_rdma_recv_block_size);
10571057
auto deleter = [device_allocator](void* data) { device_allocator->DeallocateRaw(data); };
10581058
lkey = device_allocator->get_lkey(device_ptr);
1059-
_rbuf[_rq_received].append_user_data_with_meta(device_ptr, g_rdma_recv_block_size, deleter , lkey);
1059+
uint64_t data_meta = (static_cast<uint64_t>(butil::IOBuf::GPU_MEMORY) << 32) | lkey;
1060+
_rbuf[_rq_received].append_user_data_with_meta(device_ptr, g_rdma_recv_block_size, deleter , data_meta);
10601061
_rbuf_data[_rq_received] = device_ptr;
10611062
#else
10621063
butil::IOBufAsZeroCopyOutputStream os(&_rbuf[_rq_received],

src/butil/iobuf.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,21 @@ uint64_t IOBuf::get_first_data_meta() {
11931193
if (!(r.block->flags & IOBUF_BLOCK_FLAGS_USER_DATA)) {
11941194
return 0;
11951195
}
1196-
return r.block->u.data_meta;
1196+
return (r.block->u.data_meta & 0x00000000FFFFFFFF);
1197+
}
1198+
1199+
// only when user use append_user_data_with_meta(), lkey is stored in data_meta
1200+
// We add this function for GDR, we want to know whether the data is in Host memory or GPU memory
1201+
// since lkey is uint32_t type, thus we use the high 32 bit to store
1202+
uint32_t IOBuf::get_first_data_meta_high32() {
1203+
if (_ref_num() == 0) {
1204+
return 0;
1205+
}
1206+
IOBuf::BlockRef const& r = _ref_at(0);
1207+
if (!(r.block->flags & IOBUF_BLOCK_FLAGS_USER_DATA)) {
1208+
return 0;
1209+
}
1210+
return (uint32_t)(r.block->u.data_meta >> 32);
11971211
}
11981212

11991213
void* IOBuf::get_first_data_ptr() {

src/butil/iobuf.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ friend class SingleIOBuf;
7070
static const size_t DEFAULT_BLOCK_SIZE = 8192;
7171
static const size_t INITIAL_CAP = 32; // must be power of 2
7272

73+
enum MemoryMeta {
74+
HOST_MEMORY = 0,
75+
GPU_MEMORY = 1
76+
};
77+
7378
struct Block;
7479

7580
// can't directly use `struct iovec' here because we also need to access the
@@ -265,6 +270,11 @@ friend class SingleIOBuf;
265270
// The meta is specified with append_user_data_with_meta before.
266271
// 0 means the meta is invalid.
267272
uint64_t get_first_data_meta();
273+
274+
// Get the high 32 bits of the data meta of the first byte in this IOBuf.
275+
// The meta is specified with append_user_data_with_meta before.
276+
// we use 0 to specify host memory, 1 to specify GPU memory
277+
uint32_t get_first_data_meta_high32();
268278
void* get_first_data_ptr();
269279

270280
// Resizes the buf to a length of n characters.

0 commit comments

Comments
 (0)