Compare commits

...

8 Commits

Author SHA1 Message Date
Hongcai Deng 8f156869f1
Merge a16fde8070 into b1f3c7894e 2025-08-09 10:08:57 +08:00
YuQing b1f3c7894e add functions short2HEX, int2HEX, long2HEX 2025-08-08 21:49:43 +08:00
YuQing ec2db7cd33 replace sprintf and snprintf as necessary 2025-08-07 19:55:41 +08:00
YuQing 63ef9aa8f4 add functions short2hex, int2hex, long2hex etc. 2025-08-06 14:16:06 +08:00
YuQing 1d2f938a30 add null terminator after fc_itoa 2025-08-05 21:14:45 +08:00
YuQing e4898affdd fast_buffer support options: binary_mode and check_capacity 2025-08-05 11:44:04 +08:00
YuQing 558670bc63 performance opt.: replace sprintf as necessary 2025-08-04 17:34:57 +08:00
Hongcai Deng a16fde8070 fix: compile error when build .so using .a
```
src/libfastcommon.a(hash.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
```

env: Ubuntu 14.04, gcc 4.8
2017-01-29 14:20:18 +08:00
27 changed files with 740 additions and 216 deletions

1
.gitignore vendored
View File

@ -64,6 +64,7 @@ src/tests/test_thread_local
src/tests/test_memcpy
src/tests/mblock_benchmark
src/tests/cpool_benchmark
src/tests/test_fast_buffer
# other
*.swp

View File

@ -1,8 +1,10 @@
Version 1.78 2025-08-03
Version 1.78 2025-08-07
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
* add files: spinlock.[hc]
* shared_func.[hc]: change int2buff, buff2int etc. functions to static inline
* shared_func.[hc]: add functions short2hex, int2hex, long2hex etc.
* performance opt.: replace sprintf and snprintf as necessary
Version 1.77 2025-03-18
* impl. shorten_path for /./ and /../

View File

@ -66,7 +66,7 @@ libfastcommon.a: $(FAST_STATIC_OBJS)
.c:
$(COMPILE) -o $@ $< $(FAST_STATIC_OBJS) $(LIB_PATH) $(INC_PATH)
.c.o:
$(COMPILE) -c -o $@ $< $(INC_PATH)
$(COMPILE) -c -fPIC -o $@ $< $(INC_PATH)
.c.lo:
$(COMPILE) -c -fPIC -o $@ $< $(INC_PATH)
install:

View File

@ -47,7 +47,7 @@ int array_allocator_init_ex(ArrayAllocatorContext *ctx,
start = end;
}
snprintf(name, sizeof(name), "%s-array", name_prefix);
fc_combine_two_string(name_prefix, "array", '-', name);
return fast_allocator_init_ex(&ctx->allocator, name,
obj_size, NULL, regions, region - regions, 0,
0.9999, reclaim_interval, need_lock);

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

@ -48,7 +48,7 @@ int buffered_file_writer_open_ex(BufferedFileWriter *writer,
return ENOMEM;
}
snprintf(writer->filename, sizeof(writer->filename), "%s", filename);
fc_safe_strcpy(writer->filename, filename);
writer->fd = open(writer->filename, O_WRONLY |
O_CREAT | O_TRUNC | O_CLOEXEC, mode);
if (writer->fd < 0)

View File

@ -540,6 +540,8 @@ static inline int fc_fallocate(int fd, const int64_t size)
#endif
#define FC_MACRO_STRINGIFY(x) #x
#define FC_MACRO_TOSTRING(x) FC_MACRO_STRINGIFY(x)
#ifdef __cplusplus
}

View File

@ -511,7 +511,7 @@ static ConnectionInfo *get_conn(ConnectionPool *cp,
{
if (cm->head == NULL)
{
if ((cp->max_count_per_entry > 0) &&
if ((cp->max_count_per_entry > 0) &&
(cm->total_count >= cp->max_count_per_entry))
{
format_ip_address(conn->ip_addr, formatted_ip);

View File

@ -462,8 +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)
{
snprintf(pServerInfo->ip_addr, sizeof(pServerInfo->ip_addr),
"%s", ip_addr);
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

@ -26,23 +26,28 @@
#include "fc_memory.h"
#include "fast_buffer.h"
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity)
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity,
const bool binary_mode, const bool check_capacity)
{
buffer->length = 0;
buffer->binary_mode = binary_mode;
if (init_capacity > 0)
{
buffer->alloc_size = init_capacity;
buffer->check_capacity = check_capacity;
}
else
{
buffer->alloc_size = 256;
buffer->check_capacity = true;
}
buffer->data = (char *)fc_malloc(buffer->alloc_size);
if (buffer->data == NULL)
{
return ENOMEM;
}
*(buffer->data) = '\0';
fast_buffer_set_null_terminator(buffer);
return 0;
}
@ -88,7 +93,9 @@ int fast_buffer_set_capacity(FastBuffer *buffer, const int capacity)
if (buffer->length > 0) {
memcpy(buff, buffer->data, buffer->length);
*(buff + buffer->length) = '\0';
if (!buffer->binary_mode) {
*(buff + buffer->length) = '\0';
}
}
free(buffer->data);
@ -127,7 +134,7 @@ int fast_buffer_append(FastBuffer *buffer, const char *format, ...)
}
else
{
*(buffer->data + buffer->length) = '\0'; //restore
fast_buffer_set_null_terminator(buffer); //restore
}
}
return result;
@ -148,7 +155,7 @@ int fast_buffer_append_buff(FastBuffer *buffer, const char *data, const int len)
memcpy(buffer->data + buffer->length, data, len);
buffer->length += len;
*(buffer->data + buffer->length) = '\0';
fast_buffer_set_null_terminator(buffer);
return 0;
}
@ -171,32 +178,6 @@ int fast_buffer_append_binary(FastBuffer *buffer,
return 0;
}
int fast_buffer_append_int(FastBuffer *buffer, const int n)
{
int result;
if ((result=fast_buffer_check(buffer, 16)) != 0)
{
return result;
}
buffer->length += sprintf(buffer->data + buffer->length, "%d", n);
return 0;
}
int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n)
{
int result;
if ((result=fast_buffer_check(buffer, 32)) != 0)
{
return result;
}
buffer->length += sprintf(buffer->data + buffer->length, "%"PRId64, n);
return 0;
}
int fast_buffer_append_file(FastBuffer *buffer, const char *filename)
{
struct stat st;

View File

@ -17,12 +17,14 @@
#define __FAST_BUFFER_H__
#include <stdint.h>
#include "common_define.h"
#include "shared_func.h"
typedef struct fast_buffer {
char *data;
int alloc_size;
int length;
char *data;
int alloc_size;
int length;
bool binary_mode;
bool check_capacity;
} FastBuffer;
#ifdef __cplusplus
@ -39,26 +41,39 @@ static inline char *fast_buffer_data(FastBuffer *buffer)
return buffer->data;
}
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity);
int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity,
const bool binary_mode, const bool check_capacity);
static inline int fast_buffer_init1(FastBuffer *buffer, const int init_capacity)
{
const bool binary_mode = false;
const bool check_capacity = true;
return fast_buffer_init_ex(buffer, init_capacity,
binary_mode, check_capacity);
}
static inline int fast_buffer_init(FastBuffer *buffer)
{
return fast_buffer_init_ex(buffer, 0);
const int init_capacity = 0;
return fast_buffer_init1(buffer, init_capacity);
}
#define fast_buffer_set_null_terminator(buffer) \
if (!buffer->binary_mode) *(buffer->data + buffer->length) = '\0'
#define fast_buffer_check(buffer, inc_len) \
((buffer)->check_capacity ? fast_buffer_check_inc_size(buffer, inc_len) : 0)
#define fast_buffer_clear(buffer) fast_buffer_reset(buffer)
static inline void fast_buffer_reset(FastBuffer *buffer)
{
buffer->length = 0;
*buffer->data = '\0';
fast_buffer_set_null_terminator(buffer);
}
void fast_buffer_destroy(FastBuffer *buffer);
#define fast_buffer_check(buffer, inc_len) \
fast_buffer_check_inc_size(buffer, inc_len)
int fast_buffer_set_capacity(FastBuffer *buffer, const int capacity);
static inline int fast_buffer_check_capacity(FastBuffer *buffer,
@ -86,9 +101,47 @@ int fast_buffer_append_buff(FastBuffer *buffer,
int fast_buffer_append_binary(FastBuffer *buffer,
const void *data, const int len);
int fast_buffer_append_int(FastBuffer *buffer, const int n);
static inline int fast_buffer_append_char(FastBuffer *buffer, const char ch)
{
int result;
int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n);
if ((result=fast_buffer_check(buffer, 1)) != 0)
{
return result;
}
*(buffer->data + buffer->length++) = ch;
fast_buffer_set_null_terminator(buffer);
return 0;
}
static inline int fast_buffer_append_int(FastBuffer *buffer, const int n)
{
int result;
if ((result=fast_buffer_check(buffer, 16)) != 0)
{
return result;
}
buffer->length += fc_itoa(n, buffer->data + buffer->length);
fast_buffer_set_null_terminator(buffer);
return 0;
}
static inline int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n)
{
int result;
if ((result=fast_buffer_check(buffer, 32)) != 0)
{
return result;
}
buffer->length += fc_itoa(n, buffer->data + buffer->length);
fast_buffer_set_null_terminator(buffer);
return 0;
}
int fast_buffer_append_file(FastBuffer *buffer, const char *filename);

View File

@ -584,7 +584,7 @@ int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name,
if (name != NULL)
{
snprintf(mblock->info.name, sizeof(mblock->info.name), "%s", name);
fc_safe_strcpy(mblock->info.name, name);
}
else
{

View File

@ -142,7 +142,7 @@ int free_queue_init_ex2(struct fast_task_queue *queue, const char *name,
(int64_t)max_data_size);
*/
snprintf(aname, sizeof(aname), "%s-task", name);
fc_combine_two_string(name, "task", '-', aname);
return fast_mblock_init_ex1(&queue->allocator, aname,
queue->block_size, alloc_once, max_connections,
(fast_mblock_object_init_func)task_alloc_init,

View File

@ -768,23 +768,22 @@ int64_t fc_hash_inc_value(const HashData *old_data, const int inc,
{
if (old_data->value_len < *new_value_len)
{
memcpy(new_value, old_data->value, old_data->value_len);
new_value[old_data->value_len] = '\0';
n = strtoll(new_value, NULL, 10);
n += inc;
memcpy(new_value, old_data->value, old_data->value_len);
new_value[old_data->value_len] = '\0';
n = strtoll(new_value, NULL, 10);
n += inc;
}
else
{
n = inc;
}
*new_value_len = sprintf(new_value, "%"PRId64, n);
}
else
{
n = inc;
*new_value_len = sprintf(new_value, "%"PRId64, n);
}
*new_value_len = fc_itoa(n, new_value);
return n;
}

View File

@ -198,7 +198,7 @@ static int iniAnnotationFuncLocalIpGet(IniContext *context,
(int)(square_start - param), param);
index = atoi(square_start + 1);
} else {
snprintf(name_part, sizeof(name_part) - 1, "%s", param);
fc_safe_strcpy(name_part, param);
index = -2;
}
@ -322,7 +322,7 @@ static char *doReplaceVars(IniContext *pContext, const char *param,
logWarning("file: "__FILE__", line: %d, "
"NO set directives before, set value to %s",
__LINE__, param);
snprintf(output, FAST_INI_ITEM_VALUE_SIZE, "%s", param);
fc_strlcpy(output, param, FAST_INI_ITEM_VALUE_SIZE);
return output;
}
@ -652,7 +652,7 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
if (IS_URL_RESOURCE(szFilename))
{
*pContext->config_path = '\0';
snprintf(full_filename, sizeof(full_filename), "%s", szFilename);
fc_safe_strcpy(full_filename, szFilename);
}
else
{
@ -675,8 +675,7 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
memcpy(pContext->config_path, szFilename, len);
*(pContext->config_path + len) = '\0';
snprintf(full_filename, sizeof(full_filename), \
"%s", szFilename);
fc_safe_strcpy(full_filename, szFilename);
}
else
{
@ -699,9 +698,8 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
*(pContext->config_path + len) = '\0';
}
snprintf(full_filename, sizeof(full_filename), \
"%s/%s", pContext->config_path, szFilename);
fc_combine_full_filename(pContext->config_path,
szFilename, full_filename);
pLast = strrchr(szFilename, '/');
if (pLast != NULL)
{
@ -914,7 +912,7 @@ static int iniAddAnnotation(char *params)
return EFAULT;
}
snprintf(symbol, sizeof(symbol), "%s_init_annotation", func_name);
fc_combine_two_string(func_name, "init_annotation", '_', symbol);
init_func = dlsym(dlhandle, symbol);
if (init_func == NULL)
{
@ -1013,14 +1011,12 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
strncasecmp(pLine+1, "include", 7) == 0 && \
(*(pLine+8) == ' ' || *(pLine+8) == '\t'))
{
snprintf(pIncludeFilename, sizeof(pIncludeFilename),
"%s", pLine + 9);
fc_safe_strcpy(pIncludeFilename, pLine + 9);
fc_trim(pIncludeFilename);
if (IS_URL_RESOURCE(pIncludeFilename))
{
snprintf(full_filename, sizeof(full_filename),
"%s", pIncludeFilename);
}
{
fc_safe_strcpy(full_filename, pIncludeFilename);
}
else
{
if (IS_FILE_RESOURCE(pIncludeFilename))
@ -1034,25 +1030,23 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
if (*pTrueFilename == '/')
{
snprintf(full_filename, sizeof(full_filename), \
"%s", pTrueFilename);
fc_safe_strcpy(full_filename, pTrueFilename);
}
else
{
snprintf(full_filename, sizeof(full_filename), \
"%s/%s", pContext->config_path, \
pTrueFilename);
}
{
fc_combine_full_filename(pContext->config_path,
pTrueFilename, full_filename);
}
if (!fileExists(full_filename))
{
logError("file: "__FILE__", line: %d, " \
"include file \"%s\" not exists, " \
"line: \"%s\"", __LINE__, \
pTrueFilename, pLine);
result = ENOENT;
break;
}
{
logError("file: "__FILE__", line: %d, "
"include file \"%s\" not exists, "
"line: \"%s\"", __LINE__,
pTrueFilename, pLine);
result = ENOENT;
break;
}
}
pContext->current_section = &pContext->global;
@ -2629,8 +2623,7 @@ static char *iniProccessFor(char *content, const int content_len,
char *pRemain;
int remainLen;
valueLen = sprintf(value, "%d", i);
valueLen = fc_itoa(i, value);
pRemain = pForBlock;
remainLen = forBlockLen;
while (remainLen >= tagLen)
@ -2817,7 +2810,7 @@ do { \
break; \
} \
\
snprintf(targetItem.name, sizeof(targetItem.name), "%s", szItemName); \
fc_safe_strcpy(targetItem.name, szItemName); \
pItem = (IniItem *)bsearch(&targetItem, pSection->items, \
pSection->count, sizeof(IniItem), iniCompareByItemName); \
} while (0)

View File

@ -50,21 +50,21 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock);
static int check_and_mk_log_dir(const char *base_path)
{
char data_path[MAX_PATH_SIZE];
char log_path[MAX_PATH_SIZE];
snprintf(data_path, sizeof(data_path), "%s/logs", base_path);
if (!fileExists(data_path))
{
if (mkdir(data_path, 0755) != 0)
{
fprintf(stderr, "mkdir \"%s\" fail, " \
"errno: %d, error info: %s\n", \
data_path, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
}
fc_combine_full_filename(base_path, "logs", log_path);
if (!fileExists(log_path))
{
if (mkdir(log_path, 0755) != 0)
{
fprintf(stderr, "mkdir \"%s\" fail, "
"errno: %d, error info: %s\n",
log_path, errno, STRERROR(errno));
return errno != 0 ? errno : EPERM;
}
}
return 0;
return 0;
}
int log_init()
@ -249,7 +249,7 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename)
if (*(pContext->log_filename) == '\0')
{
snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename);
fc_strlcpy(pContext->log_filename, log_filename, MAX_PATH_SIZE);
return log_open(pContext);
}
@ -258,7 +258,7 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename)
return 0;
}
snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename);
fc_strlcpy(pContext->log_filename, log_filename, MAX_PATH_SIZE);
return log_reopen_ex(pContext);
}
@ -385,12 +385,11 @@ 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
{
snprintf(full_filename, sizeof(full_filename), "%s", old_filename);
fc_safe_strcpy(full_filename, old_filename);
}
if (unlink(full_filename) != 0)
@ -590,7 +589,9 @@ static int log_get_matched_files(LogContext *pContext,
the_time = get_current_time() - days_before * 86400;
localtime_r(&the_time, &tm);
memset(filename_prefix, 0, sizeof(filename_prefix));
len = sprintf(filename_prefix, "%s.", log_filename);
len = strlen(log_filename);
memcpy(filename_prefix, log_filename, len);
*(filename_prefix + len++) = '.';
strftime(filename_prefix + len, sizeof(filename_prefix) - len,
rotate_time_format_prefix, &tm);
prefix_filename_len = strlen(filename_prefix);
@ -651,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)
@ -703,7 +704,9 @@ int log_delete_old_files(void *args)
the_time -= 86400;
localtime_r(&the_time, &tm);
memset(old_filename, 0, sizeof(old_filename));
len = sprintf(old_filename, "%s.", pContext->log_filename);
len = strlen(pContext->log_filename);
memcpy(old_filename, pContext->log_filename, len);
*(old_filename + len++) = '.';
strftime(old_filename + len, sizeof(old_filename) - len,
pContext->rotate_time_format, &tm);
if ((result=log_delete_old_file(pContext, old_filename)) != 0)
@ -733,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;
@ -760,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)
{
@ -843,7 +846,9 @@ int log_rotate(LogContext *pContext)
localtime_r(&current_time, &tm);
memset(old_filename, 0, sizeof(old_filename));
len = sprintf(old_filename, "%s.", pContext->log_filename);
len = strlen(pContext->log_filename);
memcpy(old_filename, pContext->log_filename, len);
*(old_filename + len++) = '.';
strftime(old_filename + len, sizeof(old_filename) - len,
pContext->rotate_time_format, &tm);
if (access(old_filename, F_OK) == 0)
@ -1045,10 +1050,14 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \
}
if (caption != NULL)
{
buff_len = sprintf(pContext->pcurrent_buff, "%s - ", caption);
pContext->pcurrent_buff += buff_len;
}
{
buff_len = strlen(caption);
memcpy(pContext->pcurrent_buff, caption, buff_len);
pContext->pcurrent_buff += buff_len;
*pContext->pcurrent_buff++ = ' ';
*pContext->pcurrent_buff++ = '-';
*pContext->pcurrent_buff++ = ' ';
}
memcpy(pContext->pcurrent_buff, text, text_len);
pContext->pcurrent_buff += text_len;
*pContext->pcurrent_buff++ = '\n';

View File

@ -85,7 +85,7 @@ int fast_multi_sock_client_init_ex(FastMultiSockClient *client,
}
for (i=0; i<entry_count; i++) {
if ((result=fast_buffer_init_ex(&entries[i].recv_buffer,
if ((result=fast_buffer_init1(&entries[i].recv_buffer,
new_init_recv_buffer_size)) != 0)
{
return result;

View File

@ -50,7 +50,7 @@ int write_to_pid_file(const char *pidFilename)
char buff[32];
int len;
len = sprintf(buff, "%d", (int)getpid());
len = fc_itoa(getpid(), buff);
return writeToFile(pidFilename, buff, len);
}

View File

@ -294,15 +294,18 @@ FCServerInfo *fc_server_get_by_ip_port_ex(FCServerConfig *ctx,
static inline void fc_server_set_group_ptr_name(FCServerGroupInfo *ginfo,
const char *group_name)
{
int len;
ginfo->group_name.str = ginfo->name_buff;
ginfo->group_name.len = snprintf(ginfo->name_buff,
sizeof(ginfo->name_buff) - 1, "%s", group_name);
if (ginfo->group_name.len == 0) {
return;
len = strlen(group_name);
if (len >= sizeof(ginfo->name_buff)) {
len = sizeof(ginfo->name_buff) - 1;
}
fc_trim(ginfo->group_name.str);
ginfo->group_name.len = strlen(ginfo->group_name.str);
memcpy(ginfo->name_buff, group_name, len);
*(ginfo->name_buff + len) = '\0';
fc_trim(ginfo->name_buff);
ginfo->group_name.len = strlen(ginfo->name_buff);
}
static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo,
@ -310,8 +313,8 @@ static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo,
{
ginfo->filter.ip_prefix.str = ginfo->filter.prefix_buff;
if (ip_prefix != NULL) {
ginfo->filter.ip_prefix.len = snprintf(ginfo->filter.prefix_buff,
sizeof(ginfo->filter.prefix_buff) - 1, "%s", ip_prefix);
ginfo->filter.ip_prefix.len = fc_safe_strcpy(
ginfo->filter.prefix_buff, ip_prefix);
}
}

View File

@ -46,6 +46,8 @@
#endif
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, \
@ -145,7 +147,7 @@ char *getAbsolutePath(const char *filename, char *szAbsPath, \
if (szPath[0] == '/')
{
snprintf(szAbsPath, pathSize, "%s", szPath);
fc_strlcpy(szAbsPath, szPath, pathSize);
}
else
{
@ -165,12 +167,13 @@ char *getAbsolutePath(const char *filename, char *szAbsPath, \
if (szPath[0] != '\0')
{
snprintf(szAbsPath, pathSize, "%s/%s", cwd, szPath);
}
fc_get_full_filepath_ex(cwd, strlen(cwd), szPath,
strlen(szPath), szAbsPath, pathSize);
}
else
{
snprintf(szAbsPath, pathSize, "%s", cwd);
}
{
fc_strlcpy(szAbsPath, cwd, pathSize);
}
}
return szAbsPath;
@ -205,8 +208,7 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
filename = exeFilename;
for (i=0; i<3; i++)
{
snprintf(cwd, sizeof(cwd), "%s/%s", \
search_paths[i], filename);
fc_combine_full_filename(search_paths[i], filename, cwd);
if (fileExists(cwd))
{
strcpy(szPath, search_paths[i]);
@ -225,11 +227,11 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
}
}
else
{
snprintf(szAbsFilename, maxSize, "%s/%s", \
szPath, filename);
return szAbsFilename;
}
{
fc_get_full_filename_ex(szPath, strlen(szPath), filename,
strlen(filename), szAbsFilename, maxSize);
return szAbsFilename;
}
}
else
{
@ -241,8 +243,9 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
if (*szPath == '/')
{
snprintf(szAbsFilename, maxSize, "%s/%s", szPath, filename);
}
fc_get_full_filename_ex(szPath, strlen(szPath), filename,
strlen(filename), szAbsFilename, maxSize);
}
else
{
if (getcwd(cwd, sizeof(cwd)) == NULL)
@ -261,14 +264,14 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
if (*szPath != '\0')
{
snprintf(szAbsFilename, maxSize, "%s/%s/%s", \
snprintf(szAbsFilename, maxSize, "%s/%s/%s",
cwd, szPath, filename);
}
else
{
snprintf(szAbsFilename, maxSize, "%s/%s", \
cwd, filename);
}
{
fc_get_full_filename_ex(cwd, strlen(cwd), filename,
strlen(filename), szAbsFilename, maxSize);
}
}
return szAbsFilename;
@ -327,16 +330,16 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
continue;
}
sprintf(fullpath, "%s/%s", path, dirp->d_name);
fc_combine_full_filepath(path, dirp->d_name, fullpath);
memset(&statbuf, 0, sizeof(statbuf));
if (lstat(fullpath, &statbuf) < 0)
{
continue;
}
if ((bAllOwners || (statbuf.st_uid == myuid)) && S_ISDIR(statbuf.st_mode))
{
sprintf(filepath, "%s/cmdline", fullpath);
fc_combine_full_filename(fullpath, "cmdline", filepath);
if ((fd=open(filepath, O_RDONLY | O_CLOEXEC))<0)
{
continue;
@ -357,12 +360,12 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
ptr = strrchr(buf, '/');
if (ptr == NULL)
{
snprintf(procname, 64, "%s", buf);
fc_safe_strcpy(procname, buf);
}
else
{
snprintf(procname, 64, "%s", ptr + 1);
}
{
fc_safe_strcpy(procname, ptr + 1);
}
if (strcmp(procname, pTargetProg) == 0)
{
@ -530,16 +533,17 @@ char *bin2hex(const char *s, const int len, char *szHexBuff)
{
unsigned char *p;
unsigned char *pEnd;
int nLen;
nLen = 0;
char *dest;
dest = szHexBuff;
pEnd = (unsigned char *)s + len;
for (p=(unsigned char *)s; p<pEnd; p++)
{
nLen += sprintf(szHexBuff + nLen, "%02x", *p);
}
szHexBuff[nLen] = '\0';
{
*dest++ = g_lower_hex_chars[*p >> 4];
*dest++ = g_lower_hex_chars[*p & 0x0F];
}
*dest = '\0';
return szHexBuff;
}
@ -1450,13 +1454,13 @@ int writeToFile(const char *filename, const char *buff, const int file_size)
return 0;
}
int safeWriteToFile(const char *filename, const char *buff, \
int safeWriteToFile(const char *filename, const char *buff,
const int file_size)
{
char tmpFilename[PATH_MAX];
int result;
snprintf(tmpFilename, sizeof(tmpFilename), "%s.tmp", filename);
fc_combine_two_string(filename, "tmp", '.', tmpFilename);
if ((result=writeToFile(tmpFilename, buff, file_size)) != 0)
{
return result;
@ -1553,13 +1557,11 @@ int fc_copy_to_path(const char *src_filename, const char *dest_path)
fname = strrchr(src_filename, '/');
if (fname == NULL)
{
snprintf(dest_filename, sizeof(dest_filename),
"%s/%s", dest_path, src_filename);
fc_combine_full_filename(dest_path, src_filename, dest_filename);
}
else
{
snprintf(dest_filename, sizeof(dest_filename),
"%s%s", dest_path, fname);
fc_combine_full_filename(dest_path, fname, dest_filename);
}
return fc_copy_file(src_filename, dest_filename);
@ -2566,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)
{
static unsigned char hex_chars[] = "0123456789ABCDEF";
const unsigned char *pSrc;
const unsigned char *pEnd;
char *pDest;
@ -2589,8 +2590,8 @@ char *urlencode(const char *src, const int src_len, char *dest, int *dest_len)
else
{
*pDest++ = '%';
*pDest++ = hex_chars[(*pSrc) >> 4];
*pDest++ = hex_chars[(*pSrc) & 0x0F];
*pDest++ = g_upper_hex_chars[(*pSrc) >> 4];
*pDest++ = g_upper_hex_chars[(*pSrc) & 0x0F];
}
}
@ -3231,7 +3232,7 @@ static void add_thousands_separator(char *str, const int len)
const char *int2str(const int n, char *buff, const bool thousands_separator)
{
int len;
len = sprintf(buff, "%d", n);
len = fc_ltostr(n, buff);
if (thousands_separator)
{
add_thousands_separator(buff, len);
@ -3242,7 +3243,7 @@ const char *int2str(const int n, char *buff, const bool thousands_separator)
const char *long2str(const int64_t n, char *buff, const bool thousands_separator)
{
int len;
len = sprintf(buff, "%"PRId64, n);
len = fc_ltostr(n, buff);
if (thousands_separator)
{
add_thousands_separator(buff, len);
@ -4212,15 +4213,13 @@ int fc_safe_write_file_init(SafeWriteFileInfo *fi,
{
char full_filename[PATH_MAX];
snprintf(full_filename, sizeof(full_filename), "%s/%s",
file_path, redo_filename);
fc_combine_full_filename(file_path, redo_filename, full_filename);
if ((fi->filename=fc_strdup(full_filename)) == NULL)
{
return ENOMEM;
}
snprintf(full_filename, sizeof(full_filename), "%s/%s",
file_path, tmp_filename);
fc_combine_full_filename(file_path, tmp_filename, full_filename);
if ((fi->tmp_filename=fc_strdup(full_filename)) == NULL)
{
return ENOMEM;

View File

@ -46,6 +46,8 @@ extern "C" {
#endif
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)
{
@ -339,6 +341,154 @@ static inline double buff2double(const char *buff)
return *p;
}
static inline int padding_hex(char *buff, const int len, const int padding_len)
{
char *p;
char *end;
char *stop;
int new_len;
int fill_len;
if (padding_len == len) {
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 padding_len;
} else if (*buff != '0') {
return len;
}
end = buff + len;
if (padding_len <= 0) {
stop = end - 1;
} else {
stop = end - padding_len;
}
p = buff + 1;
while (p < stop && *p == '0') {
++p;
}
new_len = end - p;
memmove(buff, p, new_len + 1);
return new_len;
}
static inline int short2hex(const short n, char *buff, const int padding_len)
{
unsigned char *p;
p = (unsigned char *)buff;
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
*p++ = g_lower_hex_chars[n & 0x0F];
*p = '\0';
return padding_hex(buff, 4, 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;
p = (unsigned char *)buff;
*p++ = g_lower_hex_chars[(n >> 28) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 24) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 20) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 16) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
*p++ = g_lower_hex_chars[n & 0x0F];
*p = '\0';
return padding_hex(buff, 8, padding_len);
}
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;
p = (unsigned char *)buff;
*p++ = g_lower_hex_chars[(n >> 60) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 56) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 52) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 48) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 44) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 40) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 36) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 32) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 28) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 24) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 20) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 16) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 12) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 8) & 0x0F];
*p++ = g_lower_hex_chars[(n >> 4) & 0x0F];
*p++ = g_lower_hex_chars[n & 0x0F];
*p = '\0';
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)
* parameters:
* pStr: the string to trim
@ -1138,6 +1288,72 @@ static inline int resolve_path(const char *from, const char *filename,
size, NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS);
}
static inline int fc_combine_two_string_ex(
const char *first_str, const int first_len,
const char *second_str, const int second_len,
const char seperator, char *combined_str, const int size)
{
char *p;
if (first_len + 1 + second_len >= size) {
return snprintf(combined_str, size, "%s%c%s",
first_str, seperator, second_str);
}
memcpy(combined_str, first_str, first_len);
p = combined_str + first_len;
if (seperator != '\0') {
*p++ = seperator;
}
memcpy(p, second_str, second_len);
p += second_len;
*p = '\0';
return p - combined_str;
}
#define fc_combine_two_string_s(first, second, seperator, combined, size) \
fc_combine_two_string_ex(first, strlen(first), second, strlen(second), \
seperator, combined, size)
#define fc_combine_two_string(first, second, seperator, combined) \
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,
char *full_filename, const int size)
{
const char seperator = '/';
return fc_combine_two_string_ex(base_path_str, base_path_len,
filename_str, filename_len, seperator, full_filename, size);
}
#define fc_get_full_filename(base_path_str, base_path_len, \
filename_str, filename_len, full_filename) \
fc_get_full_filename_ex(base_path_str, base_path_len, \
filename_str, filename_len, full_filename, sizeof(full_filename))
#define fc_combine_full_filename(base_path, filename, full_filename) \
fc_get_full_filename(base_path, strlen(base_path), \
filename, strlen(filename), full_filename)
#define fc_get_full_filepath_ex(base_path_str, base_path_len, \
filepath_str, filepath_len, full_filename, size) \
fc_get_full_filename_ex(base_path_str, base_path_len, \
filepath_str, filepath_len, full_filename, size)
#define fc_get_full_filepath(base_path_str, base_path_len, \
filepath_str, filepath_len, full_filename) \
fc_get_full_filename(base_path_str, base_path_len, \
filepath_str, filepath_len, full_filename)
#define fc_combine_full_filepath(base_path, filepath, full_filename) \
fc_combine_full_filename(base_path, filepath, full_filename)
/** get gzip command full filename
* return: the gzip command full filename
*/
@ -1273,6 +1489,47 @@ bool fc_path_contains(const string_t *path, const string_t *needle,
*/
int fc_itoa(int64_t n, char *buff);
static inline int fc_ltostr_ex(int64_t n, char *buff, const int padding_len)
{
int len;
int fill_len;
len = fc_itoa(n, buff);
*(buff + len) = '\0';
if (padding_len <= len) {
return len;
}
fill_len = padding_len - len;
memmove(buff + fill_len, buff, len + 1);
memset(buff, '0', fill_len);
return padding_len;
}
static inline int fc_ltostr(int64_t n, char *buff)
{
const int padding_len = 0;
return fc_ltostr_ex(n, buff, padding_len);
}
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
{
int len;
len = strlen(src);
if (len < size) {
memcpy(dest, src, len + 1);
} else {
len = size - 1;
memcpy(dest, src, len);
*(dest + len) = '\0';
}
return len;
}
#define fc_safe_strcpy(dest, src) fc_strlcpy(dest, src, sizeof(dest))
/** sleep in microseconds
* parameters:
* microseconds: microseconds to sleep

View File

@ -1194,7 +1194,7 @@ char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize)
}
else
{
snprintf(buff, bufferSize, "%s", ent->h_name);
fc_strlcpy(buff, ent->h_name, bufferSize);
}
return buff;
@ -1402,14 +1402,12 @@ int nbaccept(int sock, const int timeout, int *err_no)
int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
{
sockaddr_convert_t convert;
char bind_ip_prompt[256];
int result;
memset(&convert, 0, sizeof(convert));
convert.sa.addr.sa_family = af;
if (bind_ipaddr == NULL || *bind_ipaddr == '\0')
{
*bind_ip_prompt = '\0';
if (af == AF_INET)
{
convert.len = sizeof(convert.sa.addr4);
@ -1429,11 +1427,15 @@ int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
{
return result;
}
sprintf(bind_ip_prompt, "bind ip %s, ", bind_ipaddr);
}
if (bind(sock, &convert.sa.addr, convert.len) < 0)
{
if (bind(sock, &convert.sa.addr, convert.len) < 0) {
char bind_ip_prompt[256];
if (bind_ipaddr == NULL || *bind_ipaddr == '\0') {
*bind_ip_prompt = '\0';
} else {
sprintf(bind_ip_prompt, "bind ip %s, ", bind_ipaddr);
}
logError("file: "__FILE__", line: %d, "
"%sbind port %d failed, "
"errno: %d, error info: %s.",
@ -2577,10 +2579,14 @@ static inline int formatifmac(char *buff, const int buff_size,
return 0;
}
dest = buff + sprintf(buff, "%02x", *hwaddr);
dest = buff;
*dest++ = g_lower_hex_chars[*hwaddr >> 4];
*dest++ = g_lower_hex_chars[*hwaddr & 0x0F];
for (ptr=hwaddr+1; ptr<end; ptr++)
{
dest += sprintf(dest, ":%02x", *ptr);
*dest++ = ':';
*dest++ = g_lower_hex_chars[*ptr >> 4];
*dest++ = g_lower_hex_chars[*ptr & 0x0F];
}
return dest - buff;
}
@ -2629,7 +2635,7 @@ static int getifmac(FastIFConfig *config)
fc_trim(output);
if (*output != '\0')
{
snprintf(config->mac, sizeof(config->mac), "%s", output);
fc_safe_strcpy(config->mac, output);
}
}
}
@ -2733,7 +2739,7 @@ int getifconfigs(FastIFConfig *if_configs, const int max_count, int *count)
return ENOSPC;
}
sprintf(config->name, "%s", ifc->ifa_name);
strcpy(config->name, ifc->ifa_name);
(*count)++;
}

View File

@ -194,9 +194,9 @@ int get_boot_time(struct timeval *boot_time)
#define SET_MNT_FIELDS(left, fstypename, mntfromname, mntonname) \
do { \
snprintf(left.f_fstypename, sizeof(left.f_fstypename), "%s", fstypename); \
snprintf(left.f_mntfromname, sizeof(left.f_mntfromname), "%s", mntfromname); \
snprintf(left.f_mntonname, sizeof(left.f_mntonname), "%s", mntonname); \
fc_safe_strcpy(left.f_fstypename, fstypename); \
fc_safe_strcpy(left.f_mntfromname, mntfromname); \
fc_safe_strcpy(left.f_mntonname, mntonname); \
} while (0)
#ifdef OS_LINUX
@ -286,12 +286,9 @@ int get_mounted_filesystems(struct fast_statfs *stats,
toLowercase(fstypename);
if (get_device_type(fstypename, &stats[*count].device_type) == 0)
{
snprintf(stats[*count].f_mntfromname, sizeof(stats[*count].
f_mntfromname), "%s", mntfromname);
snprintf(stats[*count].f_mntonname, sizeof(stats[*count].
f_mntonname), "%s", mntonname);
snprintf(stats[*count].f_fstypename, sizeof(stats[*count].
f_fstypename), "%s", fstypename);
fc_safe_strcpy(stats[*count].f_mntfromname, mntfromname);
fc_safe_strcpy(stats[*count].f_mntonname, mntonname);
fc_safe_strcpy(stats[*count].f_fstypename, fstypename);
(*count)++;
}
}
@ -841,8 +838,7 @@ int get_processes(struct fast_process_info **processes, int *count)
for (i=0; i<nproc; i++)
{
process->field_count = 9;
snprintf(process->comm, sizeof(process->comm),
"%s", procs[i].ki_comm);
fc_safe_strcpy(process->comm, procs[i].ki_comm);
process->pid = procs[i].ki_pid;
process->ppid = procs[i].ki_ppid;
process->starttime = procs[i].ki_start;
@ -1041,8 +1037,7 @@ static int get_block_size_by_write(const char *path, int *block_size)
return result;
}
snprintf(tmp_filename, sizeof(tmp_filename),
"%s/.blksize-test.tmp", path);
fc_combine_full_filename(path, ".blksize-test.tmp", tmp_filename);
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT |
O_DIRECT | O_CLOEXEC, 0755)) < 0)
{

View File

@ -11,7 +11,7 @@ ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blo
test_server_id_func test_pipe test_atomic test_file_write_hole test_file_lock \
test_pthread_wait test_thread_pool test_data_visible test_mutex_lock_perf \
test_queue_perf test_normalize_path test_sorted_array test_sorted_queue \
test_thread_local test_memcpy mblock_benchmark cpool_benchmark
test_thread_local test_memcpy test_fast_buffer mblock_benchmark cpool_benchmark
all: $(ALL_PRGS)

View File

@ -0,0 +1,228 @@
/*
* Copyright (c) 2020 YuQing <384681@qq.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the Lesser GNU General Public License, version 3
* or later ("LGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "fastcommon/logger.h"
#include "fastcommon/fast_buffer.h"
#include "fastcommon/sched_thread.h"
typedef enum {
DA_SLICE_TYPE_FILE = 'F', /* in file slice */
DA_SLICE_TYPE_CACHE = 'C', /* in memory cache */
DA_SLICE_TYPE_ALLOC = 'A' /* allocate slice (index and space allocate only) */
} DASliceType;
typedef struct {
int64_t version;
uint64_t trunk_id; //0 for not inited
uint32_t length; //data length
uint32_t offset; //space offset
uint32_t size; //space size
} DAPieceFieldStorage;
typedef struct {
int64_t version; //for stable sort only
uint64_t oid; //object ID
uint64_t fid; //field ID (key)
uint32_t extra; //such as slice offset
char op_type;
DASliceType slice_type;
DAPieceFieldStorage storage;
} DATrunkSpaceLogRecord;
static inline void log_pack_by_append(const DATrunkSpaceLogRecord
*record, FastBuffer *buffer, const bool have_extra_field)
{
fast_buffer_append_int64(buffer, (uint32_t)g_current_time);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_int64(buffer, record->storage.version);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_int64(buffer, record->oid);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_int64(buffer, record->fid);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_char(buffer, record->op_type);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_int64(buffer, record->storage.trunk_id);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_int64(buffer, record->storage.length);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_int64(buffer, record->storage.offset);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_int64(buffer, record->storage.size);
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_char(buffer, record->slice_type);
if (have_extra_field) {
fast_buffer_append_char(buffer, ' ');
fast_buffer_append_char(buffer, record->extra);
fast_buffer_append_char(buffer, '\n');
} else {
fast_buffer_append_char(buffer, '\n');
}
}
static inline void log_pack_by_sprintf(const DATrunkSpaceLogRecord
*record, FastBuffer *buffer, const bool have_extra_field)
{
buffer->length += sprintf(buffer->data + buffer->length,
"%u %"PRId64" %"PRId64" %"PRId64" %c %"PRId64" %u %u %u %c",
(uint32_t)g_current_time, record->storage.version,
record->oid, record->fid, record->op_type,
record->storage.trunk_id, record->storage.length,
record->storage.offset, record->storage.size,
record->slice_type);
if (have_extra_field) {
buffer->length += sprintf(buffer->data + buffer->length,
" %u\n", record->extra);
} else {
*(buffer->data + buffer->length++) = '\n';
}
}
#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;
const bool check_capacity = false;
const bool have_extra_field = false;
const int LOOP = 10 * 1000 * 1000;
int result;
int i;
int64_t start_time_us;
int append_time_us;
int sprintf_time_us;
double ratio;
FastBuffer buffer;
DATrunkSpaceLogRecord record;
log_init();
g_current_time = time(NULL);
if ((result=fast_buffer_init_ex(&buffer, 256,
binary_mode, check_capacity)) != 0)
{
return result;
}
memset(&record, 0, sizeof(record));
record.op_type = 'C';
record.slice_type = DA_SLICE_TYPE_FILE;
record.storage.version = 1111;
record.oid = 9007211709265131LL;
record.fid = 0;
record.storage.trunk_id = 61;
record.storage.length = 62;
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_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_us = (get_current_time_us() - start_time_us);
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_us / 1000, append_time_us / 1000,
(int)(ratio * 100.00));
fast_buffer_destroy(&buffer);
return 0;
}

View File

@ -112,7 +112,7 @@ int main(int argc, char *argv[])
setup_mblock_stat_task();
if ((result=fast_buffer_init_ex(&buffer, 1024)) != 0) {
if ((result=fast_buffer_init1(&buffer, 1024)) != 0) {
return result;
}
fc_server_to_config_string(&ctx, &buffer);

View File

@ -210,7 +210,7 @@ int fc_thread_pool_init_ex(FCThreadPool *pool, const char *name,
return result;
}
snprintf(pool->name, sizeof(pool->name), "%s", name);
fc_safe_strcpy(pool->name, name);
pool->stack_size = stack_size;
pool->max_idle_time = max_idle_time;
if (min_idle_count > limit) {