From b1f3c7894ec03943b1df058b2975ef1a1f811ee5 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 8 Aug 2025 21:49:43 +0800 Subject: [PATCH] add functions short2HEX, int2HEX, long2HEX --- src/base64.c | 11 ++--- src/connection_pool.h | 9 +--- src/logger.c | 17 ++++--- src/shared_func.c | 6 +-- src/shared_func.h | 87 +++++++++++++++++++++++++++++++----- src/tests/test_fast_buffer.c | 78 +++++++++++++++++++++++++++++--- 6 files changed, 162 insertions(+), 46 deletions(-) diff --git a/src/base64.c b/src/base64.c index 6c80050..49758fe 100644 --- a/src/base64.c +++ b/src/base64.c @@ -24,6 +24,7 @@ #include #include #include "fc_memory.h" +#include "shared_func.h" #include "base64.h" /** @@ -53,15 +54,11 @@ void base64_set_line_length(struct base64_context *context, const int length) * Usually contains only a combination of chars \n and \r. * Could be any chars not in set A-Z a-z 0-9 + /. */ -void base64_set_line_separator(struct base64_context *context, \ +void base64_set_line_separator(struct base64_context *context, const char *pLineSeparator) { - context->line_sep_len = snprintf(context->line_separator, \ - sizeof(context->line_separator), "%s", pLineSeparator); - if (context->line_sep_len >= sizeof(context->line_separator)) - { - context->line_sep_len = sizeof(context->line_separator) - 1; - } + context->line_sep_len = fc_safe_strcpy(context-> + line_separator, pLineSeparator); } void base64_init_ex(struct base64_context *context, const int nLineLength, \ diff --git a/src/connection_pool.h b/src/connection_pool.h index 716c5c3..8f44f20 100644 --- a/src/connection_pool.h +++ b/src/connection_pool.h @@ -462,14 +462,7 @@ int conn_pool_parse_server_info(const char *pServerStr, static inline void conn_pool_set_server_info(ConnectionInfo *pServerInfo, const char *ip_addr, const int port) { - int len; - - len = strlen(ip_addr); - if (len >= sizeof(pServerInfo->ip_addr)) { - len = sizeof(pServerInfo->ip_addr) - 1; - } - memcpy(pServerInfo->ip_addr, ip_addr, len); - *(pServerInfo->ip_addr + len) = '\0'; + fc_safe_strcpy(pServerInfo->ip_addr, ip_addr); pServerInfo->port = port; pServerInfo->af = is_ipv6_addr(ip_addr) ? AF_INET6 : AF_INET; pServerInfo->sock = -1; diff --git a/src/logger.c b/src/logger.c index 388be81..837083f 100644 --- a/src/logger.c +++ b/src/logger.c @@ -385,8 +385,7 @@ static int log_delete_old_file(LogContext *pContext, char full_filename[MAX_PATH_SIZE + 128]; if (NEED_COMPRESS_LOG(pContext->compress_log_flags)) { - snprintf(full_filename, sizeof(full_filename), "%s%s", - old_filename, GZIP_EXT_NAME_STR); + fc_concat_two_string(old_filename, GZIP_EXT_NAME_STR, full_filename); } else { @@ -653,8 +652,8 @@ static int log_delete_matched_old_files(LogContext *pContext, log_get_file_path(pContext, log_filepath); for (i=0; i> 4]; - *pDest++ = upper_hex_chars[(*pSrc) & 0x0F]; + *pDest++ = g_upper_hex_chars[(*pSrc) >> 4]; + *pDest++ = g_upper_hex_chars[(*pSrc) & 0x0F]; } } diff --git a/src/shared_func.h b/src/shared_func.h index 87381f6..ab543ac 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -47,6 +47,7 @@ extern "C" { extern bool g_set_cloexec; extern const char *g_lower_hex_chars; + extern const char *g_upper_hex_chars; static inline void fc_enable_fd_cloexec(const bool cloexec) { @@ -340,7 +341,7 @@ static inline double buff2double(const char *buff) return *p; } -static inline void padding_hex(char *buff, const int len, const int padding_len) +static inline int padding_hex(char *buff, const int len, const int padding_len) { char *p; char *end; @@ -349,14 +350,14 @@ static inline void padding_hex(char *buff, const int len, const int padding_len) int fill_len; if (padding_len == len) { - return; + return len; } else if (padding_len > len) { fill_len = padding_len - len; memmove(buff + fill_len, buff, len + 1); memset(buff, '0', fill_len); - return; + return padding_len; } else if (*buff != '0') { - return; + return len; } end = buff + len; @@ -373,9 +374,10 @@ static inline void padding_hex(char *buff, const int len, const int padding_len) new_len = end - p; memmove(buff, p, new_len + 1); + return new_len; } -static inline void short2hex(const short n, char *buff, const int padding_len) +static inline int short2hex(const short n, char *buff, const int padding_len) { unsigned char *p; @@ -385,10 +387,23 @@ static inline void short2hex(const short n, char *buff, const int padding_len) *p++ = g_lower_hex_chars[(n >> 4) & 0x0F]; *p++ = g_lower_hex_chars[n & 0x0F]; *p = '\0'; - padding_hex(buff, 4, padding_len); + return padding_hex(buff, 4, padding_len); } -static inline void int2hex(const int n, char *buff, const int padding_len) +static inline int short2HEX(const short n, char *buff, const int padding_len) +{ + unsigned char *p; + + p = (unsigned char *)buff; + *p++ = g_upper_hex_chars[(n >> 12) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 8) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 4) & 0x0F]; + *p++ = g_upper_hex_chars[n & 0x0F]; + *p = '\0'; + return padding_hex(buff, 4, padding_len); +} + +static inline int int2hex(const int n, char *buff, const int padding_len) { unsigned char *p; @@ -402,10 +417,27 @@ static inline void int2hex(const int n, char *buff, const int padding_len) *p++ = g_lower_hex_chars[(n >> 4) & 0x0F]; *p++ = g_lower_hex_chars[n & 0x0F]; *p = '\0'; - padding_hex(buff, 8, padding_len); + return padding_hex(buff, 8, padding_len); } -static inline void long2hex(const int64_t n, +static inline int int2HEX(const int n, char *buff, const int padding_len) +{ + unsigned char *p; + + p = (unsigned char *)buff; + *p++ = g_upper_hex_chars[(n >> 28) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 24) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 20) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 16) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 12) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 8) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 4) & 0x0F]; + *p++ = g_upper_hex_chars[n & 0x0F]; + *p = '\0'; + return padding_hex(buff, 8, padding_len); +} + +static inline int long2hex(const int64_t n, char *buff, const int padding_len) { unsigned char *p; @@ -428,7 +460,33 @@ static inline void long2hex(const int64_t n, *p++ = g_lower_hex_chars[(n >> 4) & 0x0F]; *p++ = g_lower_hex_chars[n & 0x0F]; *p = '\0'; - padding_hex(buff, 16, padding_len); + return padding_hex(buff, 16, padding_len); +} + +static inline int long2HEX(const int64_t n, + char *buff, const int padding_len) +{ + unsigned char *p; + + p = (unsigned char *)buff; + *p++ = g_upper_hex_chars[(n >> 60) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 56) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 52) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 48) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 44) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 40) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 36) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 32) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 28) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 24) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 20) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 16) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 12) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 8) & 0x0F]; + *p++ = g_upper_hex_chars[(n >> 4) & 0x0F]; + *p++ = g_upper_hex_chars[n & 0x0F]; + *p = '\0'; + return padding_hex(buff, 16, padding_len); } /** trim leading spaces ( \t\r\n) @@ -1244,7 +1302,9 @@ static inline int fc_combine_two_string_ex( memcpy(combined_str, first_str, first_len); p = combined_str + first_len; - *p++ = seperator; + if (seperator != '\0') { + *p++ = seperator; + } memcpy(p, second_str, second_len); p += second_len; *p = '\0'; @@ -1256,9 +1316,12 @@ static inline int fc_combine_two_string_ex( seperator, combined, size) #define fc_combine_two_string(first, second, seperator, combined) \ - fc_combine_two_string_s(first, second, seperator, \ + fc_combine_two_string_s(first, second, seperator, \ combined, sizeof(combined)) +#define fc_concat_two_string(first, second, combined) \ + fc_combine_two_string(first, second, '\0', combined) + static inline int fc_get_full_filename_ex( const char *base_path_str, const int base_path_len, const char *filename_str, const int filename_len, diff --git a/src/tests/test_fast_buffer.c b/src/tests/test_fast_buffer.c index 664a512..89dcf16 100644 --- a/src/tests/test_fast_buffer.c +++ b/src/tests/test_fast_buffer.c @@ -100,6 +100,58 @@ static inline void log_pack_by_sprintf(const DATrunkSpaceLogRecord } } +#define BINLOG_FILENAME_PREFIX_STR "binlog." +#define BINLOG_FILENAME_PREFIX_LEN (sizeof(BINLOG_FILENAME_PREFIX_STR) - 1) + +static inline int cache_binlog_filename_by_sprintf( + const char *data_path, const char *subdir_name, + const uint32_t subdirs, const uint64_t id, + char *full_filename, const int size) +{ + int path_index; + path_index = id % subdirs; + return sprintf(full_filename, "%s/%s/%02X/%02X/%s%08"PRIX64, + data_path, subdir_name, path_index, path_index, + BINLOG_FILENAME_PREFIX_STR, id); +} + +static inline int cache_binlog_filename_by_append( + const char *data_path, const char *subdir_name, + const uint32_t subdirs, const uint64_t id, + char *full_filename, const int size) +{ + int path_index; + int path_len; + int subdir_len; + char *p; + + path_index = id % subdirs; + path_len = strlen(data_path); + subdir_len = strlen(subdir_name); + p = full_filename; + memcpy(p, data_path, path_len); + p += path_len; + *p++ = '/'; + memcpy(p, subdir_name, subdir_len); + p += subdir_len; + *p++ = '/'; + *p++ = g_upper_hex_chars[(path_index >> 4) & 0x0F]; + *p++ = g_upper_hex_chars[path_index & 0x0F]; + *p++ = '/'; + *p++ = g_upper_hex_chars[(path_index >> 4) & 0x0F]; + *p++ = g_upper_hex_chars[path_index & 0x0F]; + *p++ = '/'; + memcpy(p, BINLOG_FILENAME_PREFIX_STR, BINLOG_FILENAME_PREFIX_LEN); + p += BINLOG_FILENAME_PREFIX_LEN; + if (id <= UINT32_MAX) { + p += int2HEX(id, p, 8); + } else { + p += long2HEX(id, p, 8); + } + + return p - full_filename; +} + int main(int argc, char *argv[]) { const bool binary_mode = true; @@ -109,8 +161,8 @@ int main(int argc, char *argv[]) int result; int i; int64_t start_time_us; - int append_time_ms; - int sprintf_time_ms; + int append_time_us; + int sprintf_time_us; double ratio; FastBuffer buffer; DATrunkSpaceLogRecord record; @@ -134,29 +186,41 @@ int main(int argc, char *argv[]) record.storage.offset = 12345; record.storage.size = 64; + + const char *data_path = "/opt/fastcfs/fdir/data"; + const char *subdir_name = "binlog"; + const uint32_t subdirs = 256; + uint64_t id = 123456; + char full_filename1[PATH_MAX]; + char full_filename2[PATH_MAX]; + start_time_us = get_current_time_us(); for (i=0; i 0) { - ratio = (double)sprintf_time_ms / (double)append_time_ms; + if (append_time_us > 0) { + ratio = (double)sprintf_time_us / (double)append_time_us; } else { ratio = 1.0; } printf("sprintf time: %d ms, append time: %d ms, " "sprintf time / append time: %d%%\n", - sprintf_time_ms, append_time_ms, + sprintf_time_us / 1000, append_time_us / 1000, (int)(ratio * 100.00)); fast_buffer_destroy(&buffer);