add functions short2HEX, int2HEX, long2HEX

use_iouring
YuQing 2025-08-08 21:49:43 +08:00
parent ec2db7cd33
commit b1f3c7894e
6 changed files with 162 additions and 46 deletions

View File

@ -24,6 +24,7 @@
#include <fcntl.h>
#include <errno.h>
#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, \

View File

@ -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;

View File

@ -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<filename_array.count; i++)
{
snprintf(full_filename, sizeof(full_filename), "%s%s",
log_filepath, filename_array.filenames[i]);
fc_concat_two_string(log_filepath, filename_array.
filenames[i], full_filename);
if (unlink(full_filename) != 0)
{
if (errno != ENOENT)
@ -737,6 +736,7 @@ static void *log_gzip_func(void *args)
char log_filepath[MAX_PATH_SIZE];
char full_filename[MAX_PATH_SIZE + 32];
char output[512];
const char *gzip_cmd_filename;
int prefix_len;
int result;
int i;
@ -764,11 +764,10 @@ static void *log_gzip_func(void *args)
continue;
}
snprintf(full_filename, sizeof(full_filename), "%s%s",
log_filepath, filename_array.filenames[i]);
snprintf(cmd, sizeof(cmd), "%s %s",
get_gzip_command_filename(), full_filename);
gzip_cmd_filename = get_gzip_command_filename();
fc_concat_two_string(log_filepath, filename_array.
filenames[i], full_filename);
fc_combine_two_string(gzip_cmd_filename, full_filename, ' ', cmd);
result = getExecResult(cmd, output, sizeof(output));
if (result != 0)
{

View File

@ -47,6 +47,7 @@
bool g_set_cloexec = false;
const char *g_lower_hex_chars = "0123456789abcdef";
const char *g_upper_hex_chars = "0123456789ABCDEF";
char *formatDatetime(const time_t nTime, \
const char *szDateFormat, \
@ -2567,7 +2568,6 @@ int get_time_item_from_str(const char *pValue, const char *item_name,
char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
{
const unsigned char upper_hex_chars[] = "0123456789ABCDEF";
const unsigned char *pSrc;
const unsigned char *pEnd;
char *pDest;
@ -2590,8 +2590,8 @@ char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
else
{
*pDest++ = '%';
*pDest++ = upper_hex_chars[(*pSrc) >> 4];
*pDest++ = upper_hex_chars[(*pSrc) & 0x0F];
*pDest++ = g_upper_hex_chars[(*pSrc) >> 4];
*pDest++ = g_upper_hex_chars[(*pSrc) & 0x0F];
}
}

View File

@ -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,

View File

@ -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<LOOP; i++) {
cache_binlog_filename_by_sprintf(data_path, subdir_name,
subdirs, ++id, full_filename1, sizeof(full_filename1));
fast_buffer_reset(&buffer);
log_pack_by_sprintf(&record, &buffer, have_extra_field);
}
sprintf_time_ms = (get_current_time_us() - start_time_us) / 1000;
sprintf_time_us = (get_current_time_us() - start_time_us);
start_time_us = get_current_time_us();
for (i=0; i<LOOP; i++) {
cache_binlog_filename_by_append(data_path, subdir_name,
subdirs, ++id, full_filename2, sizeof(full_filename2));
fast_buffer_reset(&buffer);
log_pack_by_append(&record, &buffer, have_extra_field);
}
append_time_ms = (get_current_time_us() - start_time_us) / 1000;
append_time_us = (get_current_time_us() - start_time_us);
if (append_time_ms > 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);