mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2026-04-24 17:49:42 +08:00
polish code with new pre-commit rule (#2923)
This commit is contained in:
+24
-24
@@ -3,13 +3,13 @@
|
||||
* @brief RDMA connection management for key-value cache
|
||||
* @version 1.0.0
|
||||
* @copyright Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@@ -32,22 +32,22 @@
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sstream>
|
||||
#include <netdb.h>
|
||||
#include <sstream>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <cstring>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <cstring>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
#include "kvcache_rdma.h"
|
||||
#include "util.h"
|
||||
@@ -88,8 +88,8 @@ struct QpInfo {
|
||||
intBuffer[0] = htonl(lid);
|
||||
intBuffer[1] = htonl(qpn);
|
||||
intBuffer[2] = htonl(psn);
|
||||
memcpy(buffer + 12, gid.raw, sizeof(gid.raw));
|
||||
intBuffer[7] = htonl(static_cast<uint32_t>(mtu));
|
||||
memcpy(buffer + 12, gid.raw, sizeof(gid.raw));
|
||||
intBuffer[7] = htonl(static_cast<uint32_t>(mtu));
|
||||
}
|
||||
|
||||
/// @brief Deserialize QP info from buffer
|
||||
@@ -102,7 +102,7 @@ struct QpInfo {
|
||||
mtu = static_cast<ibv_mtu>(ntohl(intBuffer[7]));
|
||||
}
|
||||
|
||||
static const size_t size = 12 + sizeof(gid.raw) + 4;
|
||||
static const size_t size = 12 + sizeof(gid.raw) + 4;
|
||||
};
|
||||
|
||||
/// @brief RDMA connection context
|
||||
@@ -137,13 +137,13 @@ struct Connection {
|
||||
|
||||
std::vector<void *> send_write_cache_key_remote_ptr_list;
|
||||
std::vector<uint32_t> send_write_cache_key_remote_rkey_list;
|
||||
std::vector<void *> send_write_cache_value_remote_ptr_list;
|
||||
std::vector<void *> send_write_cache_value_remote_ptr_list;
|
||||
std::vector<uint32_t> send_write_cache_value_remote_rkey_list;
|
||||
|
||||
// For rdma read operations
|
||||
std::vector<void*> read_bufs;
|
||||
std::vector<ibv_mr*> read_mrs;
|
||||
|
||||
|
||||
// Work completion tracking
|
||||
int wc_count;
|
||||
int wc_target_count;
|
||||
@@ -208,4 +208,4 @@ int setup_listening_socket(int port);
|
||||
int configure_epoll(int sockfd);
|
||||
std::vector<std::string> get_net_ifname();
|
||||
|
||||
#endif // FASTDEPLOY_KVCACHE_CONNECTION_H
|
||||
#endif // FASTDEPLOY_KVCACHE_CONNECTION_H
|
||||
|
||||
@@ -61,30 +61,30 @@ private:
|
||||
uint32_t rkey, const std::string &ip,
|
||||
const std::string &port);
|
||||
|
||||
bool execute_rdma_writes(struct RdmaContext* ctx, int layer_idx,
|
||||
const std::vector<int64_t>& local_block_ids,
|
||||
bool is_key, std::vector<uint64_t>& remote_addr,
|
||||
bool execute_rdma_writes(struct RdmaContext* ctx, int layer_idx,
|
||||
const std::vector<int64_t>& local_block_ids,
|
||||
bool is_key, std::vector<uint64_t>& remote_addr,
|
||||
uint32_t rkey);
|
||||
|
||||
void prepare_write_requests(struct ibv_sge* sge_list,
|
||||
|
||||
void prepare_write_requests(struct ibv_sge* sge_list,
|
||||
struct ibv_send_wr* send_wr_list,
|
||||
int layer_idx,
|
||||
int layer_idx,
|
||||
const std::vector<int64_t>& local_block_ids,
|
||||
bool is_key,
|
||||
std::vector<uint64_t>& remote_addr,
|
||||
bool is_key,
|
||||
std::vector<uint64_t>& remote_addr,
|
||||
uint32_t rkey);
|
||||
|
||||
bool execute_read_verification(struct RdmaContext* ctx,
|
||||
size_t block_idx,
|
||||
uint64_t remote_addr,
|
||||
|
||||
bool execute_read_verification(struct RdmaContext* ctx,
|
||||
size_t block_idx,
|
||||
uint64_t remote_addr,
|
||||
uint32_t rkey,
|
||||
int layer_idx,
|
||||
const std::string& ip,
|
||||
const std::string& ip,
|
||||
const std::string& port);
|
||||
|
||||
bool post_send_with_retry(struct RdmaContext* ctx,
|
||||
struct ibv_send_wr* wr_list,
|
||||
size_t inflight_wr,
|
||||
|
||||
bool post_send_with_retry(struct RdmaContext* ctx,
|
||||
struct ibv_send_wr* wr_list,
|
||||
size_t inflight_wr,
|
||||
bool need_poll);
|
||||
|
||||
// Connection management
|
||||
@@ -119,7 +119,7 @@ private:
|
||||
std::map<std::string, struct RdmaContext*> conn_map; // Active connections map
|
||||
std::mutex mutex_; // Thread synchronization mutex
|
||||
int rdma_event_channel_epoll_fd; // Epoll file descriptor
|
||||
struct ibv_pd *g_pd = NULL; // fd
|
||||
struct ibv_pd *g_pd = NULL; // fd
|
||||
int RDMACommunicator_status; // Communicator status flag
|
||||
bool start_client_listener = false; // Client listener flag
|
||||
};
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
* @brief Logging module for key-value cache system
|
||||
* @version 1.0.0
|
||||
* @copyright Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
@@ -43,7 +43,7 @@ typedef enum {
|
||||
KV_LOG_LEVEL_ERROR = 3
|
||||
} KVLogLevel;
|
||||
|
||||
void debug_log(KVLogLevel level, bool enable_to_terminal, const char *filefunc,
|
||||
void debug_log(KVLogLevel level, bool enable_to_terminal, const char *filefunc,
|
||||
int line, const char *fmt, ...) __attribute__ ((format (printf, 5, 6)));
|
||||
|
||||
/**
|
||||
@@ -107,11 +107,11 @@ void debug_log(KVLogLevel level, bool enable_to_terminal, const char *filefunc,
|
||||
LOGD(fmt, __VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define LOGD_RAW(fmt, arg...) do { \
|
||||
#define LOGD_RAW(fmt, arg...) do { \
|
||||
if (ENV_ENABLE_RAW("KV_IS_DEBUG_ENABLED")) { \
|
||||
GET_CURRENT_TIME(); \
|
||||
fprintf(stdout, "[%s][DBG][KV_CACHE][%s:%d] " \
|
||||
fmt "\n", str, \
|
||||
FILE_NAME(__FILE__), __LINE__, ## arg); \
|
||||
} \
|
||||
} while (0)
|
||||
} while (0)
|
||||
|
||||
@@ -15,12 +15,12 @@
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <cstring>
|
||||
#include "log.h"
|
||||
|
||||
#define PATH_MAX 4096 /* # chars in a path name including nul */
|
||||
#define RDMA_WR_LIST_MAX_SIZE 32
|
||||
#define RDMA_SQ_MAX_SIZE 1024
|
||||
#define RDMA_SQ_MAX_SIZE 1024
|
||||
|
||||
#define RDMA_DEFAULT_PORT 20001
|
||||
#define RDMA_TCP_CONNECT_SIZE 1024
|
||||
@@ -54,19 +54,19 @@ enum class QpStatus {
|
||||
inline void busid_to_int64(const char *busId, int64_t *id) {
|
||||
char hexStr[17] = {0};
|
||||
int hexOffset = 0;
|
||||
|
||||
|
||||
// Filter valid hex characters
|
||||
for (int i = 0; hexOffset < sizeof(hexStr) - 1 && busId[i] != '\0'; i++) {
|
||||
char c = busId[i];
|
||||
if (c == '.' || c == ':') continue;
|
||||
|
||||
if ((c >= '0' && c <= '9') ||
|
||||
(c >= 'A' && c <= 'F') ||
|
||||
|
||||
if ((c >= '0' && c <= '9') ||
|
||||
(c >= 'A' && c <= 'F') ||
|
||||
(c >= 'a' && c <= 'f')) {
|
||||
hexStr[hexOffset++] = c;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
*id = strtol(hexStr, NULL, 16);
|
||||
}
|
||||
|
||||
@@ -78,45 +78,45 @@ public:
|
||||
bool is_up;
|
||||
bool is_running;
|
||||
bool is_loopback;
|
||||
|
||||
|
||||
bool isUsable() const {
|
||||
return is_up && is_running && !is_loopback;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
static std::vector<InterfaceInfo> getAllInterfaces() {
|
||||
std::vector<InterfaceInfo> interfaces;
|
||||
struct ifaddrs *ifaddrs_ptr = nullptr;
|
||||
|
||||
|
||||
if (getifaddrs(&ifaddrs_ptr) == -1) {
|
||||
return interfaces;
|
||||
}
|
||||
|
||||
|
||||
for (struct ifaddrs *ifa = ifaddrs_ptr; ifa != nullptr; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr == nullptr) continue;
|
||||
if (ifa->ifa_addr->sa_family != AF_INET) continue;
|
||||
|
||||
|
||||
InterfaceInfo info;
|
||||
info.name = ifa->ifa_name;
|
||||
info.is_up = (ifa->ifa_flags & IFF_UP) != 0;
|
||||
info.is_running = (ifa->ifa_flags & IFF_RUNNING) != 0;
|
||||
info.is_loopback = (ifa->ifa_flags & IFF_LOOPBACK) != 0;
|
||||
|
||||
|
||||
struct sockaddr_in* sa = (struct sockaddr_in*)ifa->ifa_addr;
|
||||
char ip_str[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &sa->sin_addr, ip_str, INET_ADDRSTRLEN);
|
||||
info.ip = ip_str;
|
||||
|
||||
|
||||
interfaces.push_back(info);
|
||||
}
|
||||
|
||||
|
||||
freeifaddrs(ifaddrs_ptr);
|
||||
return interfaces;
|
||||
}
|
||||
|
||||
|
||||
static std::string getFirstUsableInterface() {
|
||||
auto interfaces = getAllInterfaces();
|
||||
|
||||
|
||||
for (const auto& iface : interfaces) {
|
||||
if (iface.isUsable()) {
|
||||
return iface.name;
|
||||
@@ -124,14 +124,14 @@ public:
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
static void displayAllInterfaces() {
|
||||
auto interfaces = getAllInterfaces();
|
||||
|
||||
|
||||
printf("Available network interfaces:\n");
|
||||
for (const auto& iface : interfaces) {
|
||||
printf(" %s: %s [%s%s%s]\n",
|
||||
iface.name.c_str(),
|
||||
printf(" %s: %s [%s%s%s]\n",
|
||||
iface.name.c_str(),
|
||||
iface.ip.c_str(),
|
||||
iface.is_up ? "UP" : "DOWN",
|
||||
iface.is_running ? ",RUNNING" : "",
|
||||
@@ -157,13 +157,13 @@ private:
|
||||
bool relax_ordering_enabled_;
|
||||
int ib_timeout_;
|
||||
const char* rdma_nics_;
|
||||
|
||||
|
||||
// Private constructor for singleton pattern
|
||||
KVCacheConfig() {
|
||||
// Initialize configuration from environment variables
|
||||
rdma_gid_index_ = parse_int_value(
|
||||
std::getenv("KVCACHE_RDMA_GID_INDEX"), 3, "KVCACHE_RDMA_GID_INDEX");
|
||||
|
||||
|
||||
// Parse optional RDMA port override
|
||||
const char* port_value = std::getenv("SET_RDMA_DEST_PORT");
|
||||
has_rdma_dest_port_override_ = false; // 默认为false
|
||||
@@ -177,7 +177,7 @@ private:
|
||||
}
|
||||
|
||||
const char* env_interface = std::getenv("KVCACHE_SOCKET_IFNAME");
|
||||
|
||||
|
||||
if (env_interface && env_interface[0] != '\0') {
|
||||
socket_interface_ = env_interface;
|
||||
printf("Using specified interface: %s\n", socket_interface_);
|
||||
@@ -194,14 +194,14 @@ private:
|
||||
}
|
||||
NetworkInterfaceManager::displayAllInterfaces();
|
||||
}
|
||||
|
||||
|
||||
socket_interface_ = std::getenv("KVCACHE_SOCKET_IFNAME");
|
||||
debug_file_path_ = std::getenv("KVCACHE_DEBUG_FILE");
|
||||
error_file_path_ = std::getenv("KVCACHE_ERROR_FILE");
|
||||
|
||||
|
||||
gdrcopy_flush_enabled_ = parse_bool_value(std::getenv("KVCACHE_GDRCOPY_FLUSH_ENABLE"));
|
||||
verify_read_enabled_ = parse_bool_value(std::getenv("KVCACHE_VERIFY_READ"));
|
||||
debug_mode_enabled_ = parse_bool_value(std::getenv("KVCACHE_DEBUG")) ||
|
||||
debug_mode_enabled_ = parse_bool_value(std::getenv("KVCACHE_DEBUG")) ||
|
||||
parse_bool_value(std::getenv("KV_IS_DEBUG_ENABLED"));
|
||||
debug_output_enabled_ = parse_bool_value(std::getenv("KVCACHE_DEBUG_OUTPUT"));
|
||||
|
||||
@@ -215,29 +215,29 @@ private:
|
||||
|
||||
rdma_nics_ = std::getenv("KVCACHE_RDMA_NICS");
|
||||
}
|
||||
|
||||
|
||||
// Helper methods
|
||||
bool parse_bool_value(const char* value) {
|
||||
if (!value) return false;
|
||||
|
||||
|
||||
std::string str_value(value);
|
||||
std::transform(str_value.begin(), str_value.end(), str_value.begin(), ::tolower);
|
||||
|
||||
return (str_value == "1" || str_value == "true" ||
|
||||
|
||||
return (str_value == "1" || str_value == "true" ||
|
||||
str_value == "on" || str_value == "yes");
|
||||
}
|
||||
|
||||
|
||||
int parse_int_value(const char* value, int default_value, const char* env_name) {
|
||||
if (!value) return default_value;
|
||||
|
||||
|
||||
try {
|
||||
return std::stoi(std::string(value));
|
||||
} catch (const std::invalid_argument& e) {
|
||||
fprintf(stderr, "Invalid value for %s: '%s', using default: %d\n",
|
||||
fprintf(stderr, "Invalid value for %s: '%s', using default: %d\n",
|
||||
env_name, value, default_value);
|
||||
return default_value;
|
||||
} catch (const std::out_of_range& e) {
|
||||
fprintf(stderr, "%s value out of range: '%s', using default: %d\n",
|
||||
fprintf(stderr, "%s value out of range: '%s', using default: %d\n",
|
||||
env_name, value, default_value);
|
||||
return default_value;
|
||||
}
|
||||
@@ -247,7 +247,7 @@ public:
|
||||
// Prevent copying and assignment
|
||||
KVCacheConfig(const KVCacheConfig&) = delete;
|
||||
KVCacheConfig& operator=(const KVCacheConfig&) = delete;
|
||||
|
||||
|
||||
// Get singleton instance
|
||||
static KVCacheConfig& getInstance() {
|
||||
static KVCacheConfig instance;
|
||||
@@ -255,14 +255,14 @@ public:
|
||||
}
|
||||
|
||||
int get_ib_timeout() const { return ib_timeout_; }
|
||||
|
||||
|
||||
// Configuration retrieval methods
|
||||
int get_rdma_gid_index() const { return rdma_gid_index_; }
|
||||
|
||||
|
||||
int resolve_rdma_dest_port(int default_port) const {
|
||||
return has_rdma_dest_port_override_ ? rdma_dest_port_override_ : default_port;
|
||||
}
|
||||
|
||||
|
||||
int resolve_rdma_dest_port(const std::string& default_port) const {
|
||||
try {
|
||||
return resolve_rdma_dest_port(std::stoi(default_port));
|
||||
@@ -271,45 +271,45 @@ public:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const char* get_socket_interface() const { return socket_interface_; }
|
||||
const char* get_debug_file_path() const { return debug_file_path_; }
|
||||
const char* get_error_file_path() const { return error_file_path_; }
|
||||
const char* get_rdma_nics() const { return rdma_nics_; }
|
||||
|
||||
|
||||
// Feature check methods
|
||||
bool is_gdrcopy_flush_enabled() const { return gdrcopy_flush_enabled_; }
|
||||
bool is_verify_read_enabled() const { return verify_read_enabled_; }
|
||||
bool is_debug_mode_enabled() const { return debug_mode_enabled_; }
|
||||
bool is_debug_output_enabled() const { return debug_output_enabled_; }
|
||||
bool is_relax_ordering_enabled() const { return relax_ordering_enabled_; }
|
||||
|
||||
|
||||
// Display configuration
|
||||
void displayConfiguration() const {
|
||||
INFO("KVCache Configuration:\n");
|
||||
INFO("Init KVCacheConfig RDMA GID Index: %d\n", rdma_gid_index_);
|
||||
|
||||
|
||||
if (has_rdma_dest_port_override_) {
|
||||
INFO("Init KVCacheConfig RDMA Destination Port Override: %d\n", rdma_dest_port_override_);
|
||||
}
|
||||
|
||||
|
||||
if (socket_interface_) {
|
||||
INFO("Init KVCacheConfig Socket Interface: %s\n", socket_interface_);
|
||||
}
|
||||
|
||||
|
||||
INFO("Init KVCacheConfig GDRCopy Flush: %s\n", gdrcopy_flush_enabled_ ? "enabled" : "disabled");
|
||||
INFO("Init KVCacheConfig Verify Read: %s\n", verify_read_enabled_ ? "enabled" : "disabled");
|
||||
INFO("Init KVCacheConfig Debug Mode: %s\n", debug_mode_enabled_ ? "enabled" : "disabled");
|
||||
INFO("Init KVCacheConfig Debug Output: %s\n", debug_output_enabled_ ? "enabled" : "disabled");
|
||||
|
||||
|
||||
if (debug_file_path_) {
|
||||
INFO("Init KVCacheConfig Debug File: %s\n", debug_file_path_);
|
||||
}
|
||||
|
||||
|
||||
if (error_file_path_) {
|
||||
INFO("Init KVCacheConfig Error File: %s\n", error_file_path_);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user