Compare commits

...

11 Commits

Author SHA1 Message Date
Hongcai Deng a451179f3a
Merge a16fde8070 into aa48e3cd9a 2025-09-27 14:22:57 +08:00
YuQing aa48e3cd9a upgrade version in spec file 2025-09-20 17:12:30 +08:00
YuQing ec8e47f831 tests/test_fast_buffer.c support all types 2025-09-12 14:08:46 +08:00
YuQing d9d6255621 test_fast_buffer.c support int2hex 2025-09-11 12:07:45 +08:00
YuQing 2f75958a4a test_fast_buffer.c support -t option 2025-09-11 10:05:25 +08:00
YuQing a4cae13e07 add function fc_ftoa 2025-09-10 15:06:17 +08:00
YuQing f136821c0d getIpaddrByNameEx: IPv4 has priority over IPv6 2025-09-10 15:05:15 +08:00
YuQing b97f23ced2 bytes_to_human_str support round off 2025-09-03 15:23:27 +08:00
YuQing 84a1f90a9a bytes to human readalbe string more gracefully 2025-08-29 13:25:58 +08:00
YuQing 3f19715e45 add function bytes_to_human_str 2025-08-29 11:36:10 +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
11 changed files with 427 additions and 67 deletions

View File

@ -1,6 +1,11 @@
Version 1.79 2025-08-20
Version 1.80 2025-09-10
* getIpaddrByNameEx: IPv4 has priority over IPv6
* shared_func.[hc]: add function fc_ftoa
Version 1.79 2025-08-29
* logger.h export function log_it_ex3
* shared_func.[hc]: add function bytes_to_human_str
Version 1.78 2025-08-07
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6

View File

@ -3,7 +3,7 @@
%define CommitVersion %(echo $COMMIT_VERSION)
Name: libfastcommon
Version: 1.0.79
Version: 1.0.80
Release: 1%{?dist}
Summary: c common functions library extracted from my open source projects FastDFS
License: LGPL

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

@ -112,6 +112,13 @@ extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind);
#define SYNC_LOG_BUFF_DEF_INTERVAL 10
#define TIME_NONE -1
#define FC_BYTES_ONE_KB ( 1 << 10)
#define FC_BYTES_ONE_MB ( 1 << 20)
#define FC_BYTES_ONE_GB ( 1 << 30)
#define FC_BYTES_ONE_TB (1LL << 40)
#define FC_BYTES_ONE_PB (1LL << 50)
#define FC_BYTES_ONE_EB (1LL << 60)
#if defined(IOV_MAX) && IOV_MAX > 256
#define FC_IOV_BATCH_SIZE 256
#else

View File

@ -136,8 +136,8 @@ static int log_print_header(LogContext *pContext)
if (pContext->current_size < 0)
{
result = errno != 0 ? errno : EACCES;
fprintf(stderr, "lseek file \"%s\" fail, " \
"errno: %d, error info: %s\n", \
fprintf(stderr, "lseek file \"%s\" fail, "
"errno: %d, error info: %s\n",
pContext->log_filename, result, STRERROR(result));
}
else {

View File

@ -3251,6 +3251,110 @@ const char *long2str(const int64_t n, char *buff, const bool thousands_separator
return buff;
}
static int format_bytes_string(const int64_t input_bytes,
const int64_t unit_bytes, char *buff)
{
int64_t remain;
int n;
int fragment;
char *p;
n = input_bytes / unit_bytes;
remain = input_bytes - (n * unit_bytes);
if (n < 10)
{
fragment = (remain * 10LL + unit_bytes / 2) / unit_bytes;
if (fragment == 10)
{
++n;
fragment = 0;
}
p = buff + fc_itoa(n, buff);
if (n < 10)
{
*p++ = '.';
p += fc_itoa(fragment, p);
}
}
else
{
if (remain >= unit_bytes / 2)
{
++n;
}
p = buff + fc_itoa(n, buff);
}
return p - buff;
}
const char *bytes_to_human_str(const int64_t bytes, char *buff)
{
char *p;
if (bytes < FC_BYTES_ONE_TB)
{
if (bytes < FC_BYTES_ONE_MB)
{
if (bytes < FC_BYTES_ONE_KB)
{
p = buff + fc_itoa(bytes, buff);
*p++ = ' ';
*p++ = ' '; //for alignment
}
else
{
p = buff + format_bytes_string(bytes, FC_BYTES_ONE_KB, buff);
*p++ = ' ';
*p++ = 'K';
}
}
else
{
if (bytes < FC_BYTES_ONE_GB)
{
p = buff + format_bytes_string(bytes, FC_BYTES_ONE_MB, buff);
*p++ = ' ';
*p++ = 'M';
}
else
{
p = buff + format_bytes_string(bytes, FC_BYTES_ONE_GB, buff);
*p++ = ' ';
*p++ = 'G';
}
}
}
else
{
if (bytes < FC_BYTES_ONE_EB)
{
if (bytes < FC_BYTES_ONE_PB)
{
p = buff + format_bytes_string(bytes, FC_BYTES_ONE_TB, buff);
*p++ = ' ';
*p++ = 'T';
}
else
{
p = buff + format_bytes_string(bytes, FC_BYTES_ONE_PB, buff);
*p++ = ' ';
*p++ = 'P';
}
}
else
{
p = buff + format_bytes_string(bytes, FC_BYTES_ONE_EB, buff);
*p++ = ' ';
*p++ = 'E';
}
}
*p++ = 'B';
*p = '\0';
return buff;
}
bool starts_with(const char *str, const char *needle)
{
int str_len;
@ -4302,6 +4406,119 @@ int fc_itoa(int64_t n, char *buff)
return (start - buff) + len;
}
int fc_ftoa(double d, const int scale, char *buff)
{
int len;
int i;
bool positive;
int64_t n;
double fraction;
char *p;
positive = (d >= 0.00);
switch (scale)
{
case 0:
d += (positive ? 5e-1 : -5e-1);
break;
case 1:
d += (positive ? 5e-2 : -5e-2);
break;
case 2:
d += (positive ? 5e-3 : -5e-3);
break;
case 3:
d += (positive ? 5e-4 : -5e-4);
break;
case 4:
d += (positive ? 5e-5 : -5e-5);
break;
case 5:
d += (positive ? 5e-6 : -5e-6);
break;
case 6:
d += (positive ? 5e-7 : -5e-7);
break;
case 7:
d += (positive ? 5e-8 : -5e-8);
break;
case 8:
d += (positive ? 5e-9 : -5e-9);
break;
case 9:
d += (positive ? 5e-10 : -5e-10);
break;
case 10:
d += (positive ? 5e-11 : -5e-11);
break;
case 11:
d += (positive ? 5e-12 : -5e-12);
break;
case 12:
d += (positive ? 5e-13 : -5e-13);
break;
case 13:
d += (positive ? 5e-14 : -5e-14);
break;
case 14:
d += (positive ? 5e-15 : -5e-15);
break;
case 15:
d += (positive ? 5e-16 : -5e-16);
break;
case 16:
d += (positive ? 5e-17 : -5e-17);
break;
default:
d += (positive ? 5e-18 : -5e-18);
break;
}
n = (int64_t)d;
if (n > -10 && n < 10)
{
if (positive)
{
*buff = '0' + n;
len = 1;
}
else
{
*buff = '-';
*(buff+1) = '0' + (-1) * n;
len = 2;
}
}
else
{
len = fc_itoa(n, buff);
}
if (scale <= 0)
{
return len;
}
p = buff + len;
*p++ = '.';
if (positive)
{
fraction = d - (double)n;
}
else
{
fraction = (double)n - d;
}
for (i=0; i<scale; i++)
{
fraction *= 10;
n = (int)fraction;
fraction -= n;
*p++ = '0' + n;
}
return p - buff;
}
int fc_compare_int64_ptr(const int64_t *n1, const int64_t *n2)
{
return fc_compare_int64(*n1, *n2);

View File

@ -1191,6 +1191,8 @@ static inline const char *long_to_comma_str(const int64_t n, char *buff)
return long2str(n, buff, true);
}
const char *bytes_to_human_str(const int64_t bytes, char *buff);
/** if the string starts with the needle string
* parameters:
* str: the string to detect
@ -1601,12 +1603,28 @@ bool fc_path_contains(const string_t *path, const string_t *needle,
/** itoa output as decimal number
* parameters:
* n: the number to convert
* buff: store the converted string
* return: string length
* n: the integer number to convert
* buff: store the converted string, NOT null-terminated
* return: converted string length
*/
int fc_itoa(int64_t n, char *buff);
/** ftoa output as decimal number
* parameters:
* d: the double number to convert
* scale: number of decimal places (round off)
* buff: store the converted string, NOT null-terminated
* return: converted string length
*/
int fc_ftoa(double d, const int scale, char *buff);
/** output as decimal number
* parameters:
* n: the integer number to convert
* buff: store the converted string, null-terminated
* padding_len: padding length (padding with charactor '0')
* return: converted string length
*/
static inline int fc_ltostr_ex(int64_t n, char *buff, const int padding_len)
{
int len;

View File

@ -1238,18 +1238,18 @@ in_addr_64_t getIpaddrByNameEx(const char *name, char *buff,
return addr4.s_addr;
}
*af = AF_UNSPEC;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // 支持IPv4和IPv6
hints.ai_family = AF_UNSPEC;
if (getaddrinfo(name, NULL, &hints, &res) != 0)
{
*af = AF_UNSPEC;
return INADDR_NONE;
}
ip_addr = INADDR_NONE;
for (p = res; p != NULL; p = p->ai_next)
{
*af = p->ai_family;
if (p->ai_family == AF_INET) // 处理IPv4地址
if (p->ai_family == AF_INET) //IPv4 address
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
if (buff != NULL)
@ -1260,11 +1260,11 @@ in_addr_64_t getIpaddrByNameEx(const char *name, char *buff,
}
}
*af = p->ai_family;
ip_addr = ipv4->sin_addr.s_addr;
freeaddrinfo(res);
return ip_addr;
break;
}
else if (p->ai_family == AF_INET6) // 处理IPv6地址
else if (p->ai_family == AF_INET6) //IPv6 address
{
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
if (buff != NULL)
@ -1281,15 +1281,14 @@ in_addr_64_t getIpaddrByNameEx(const char *name, char *buff,
}
}
*af = p->ai_family;
ip_addr = *((in_addr_64_t *)((char *)&ipv6->sin6_addr + 8));
freeaddrinfo(res);
return ip_addr;
continue;
}
}
freeaddrinfo(res);
*af = AF_UNSPEC;
return INADDR_NONE;
return ip_addr;
}
int getIpaddrsByName(const char *name,

View File

@ -26,6 +26,14 @@
#include "fastcommon/fast_buffer.h"
#include "fastcommon/sched_thread.h"
typedef enum {
TEST_TYPE_NONE = 0,
TEST_TYPE_ITOA,
TEST_TYPE_FTOA,
TEST_TYPE_INT2HEX,
TEST_TYPE_APPEND
} TestType;
typedef enum {
DA_SLICE_TYPE_FILE = 'F', /* in file slice */
DA_SLICE_TYPE_CACHE = 'C', /* in memory cache */
@ -152,20 +160,41 @@ static inline int cache_binlog_filename_by_append(
return p - full_filename;
}
static void usage(const char *program)
{
fprintf(stderr, "Usage: %s [-t {itoa | ftoa | int2hex | append | all}]\n",
program);
}
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;
const char *data_path = "/opt/fastcfs/fdir/data";
const char *subdir_name = "binlog";
const uint32_t subdirs = 256;
int result;
TestType test_type = TEST_TYPE_ITOA;
TestType type_start;
TestType type_last;
uint64_t id = 123456;
double d = 123.456;
int ch;
int i;
int len;
int64_t start_time_us;
int append_time_us;
int convert_time_us;
int sprintf_time_us;
double ratio;
FastBuffer buffer;
DATrunkSpaceLogRecord record;
char full_filename1[PATH_MAX];
char full_filename2[PATH_MAX];
char buff[32] = {0};
char *caption;
log_init();
g_current_time = time(NULL);
@ -175,53 +204,127 @@ int main(int argc, char *argv[])
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;
type_start = type_last = TEST_TYPE_ITOA;
while ((ch=getopt(argc, argv, "ht:")) != -1) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 't':
if (strcasecmp(optarg, "itoa") == 0) {
type_start = type_last = TEST_TYPE_ITOA;
} else if (strcasecmp(optarg, "ftoa") == 0) {
type_start = type_last = TEST_TYPE_FTOA;
} else if (strcasecmp(optarg, "int2hex") == 0) {
type_start = type_last = TEST_TYPE_INT2HEX;
} else if (strcasecmp(optarg, "append") == 0) {
type_start = type_last = TEST_TYPE_APPEND;
} else if (strcasecmp(optarg, "all") == 0) {
type_start = TEST_TYPE_ITOA;
type_last = TEST_TYPE_APPEND;
} else {
fprintf(stderr, "invalid type: %s\n", optarg);
return EINVAL;
}
break;
default:
usage(argv[0]);
return EINVAL;
}
}
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));
for (test_type=type_start; test_type<=type_last; test_type++) {
if (test_type == TEST_TYPE_APPEND) {
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;
}
start_time_us = get_current_time_us();
for (i=0; i<LOOP; i++) {
switch (test_type) {
case TEST_TYPE_APPEND:
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);
break;
case TEST_TYPE_ITOA:
sprintf(buff, "%"PRId64, id);
break;
case TEST_TYPE_FTOA:
sprintf(buff, "%.2f", d);
break;
case TEST_TYPE_INT2HEX:
sprintf(buff, "%x", (int)id);
break;
default:
break;
}
}
sprintf_time_us = (get_current_time_us() - start_time_us);
start_time_us = get_current_time_us();
for (i=0; i<LOOP; i++) {
switch (test_type) {
case TEST_TYPE_APPEND:
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);
break;
case TEST_TYPE_ITOA:
len = fc_itoa(id, buff);
*(buff + len) = '\0';
break;
case TEST_TYPE_FTOA:
len = fc_ftoa(d, 2, buff);
*(buff + len) = '\0';
break;
case TEST_TYPE_INT2HEX:
int2hex(id, buff, 0);
break;
default:
break;
}
}
convert_time_us = (get_current_time_us() - start_time_us);
if (convert_time_us > 0) {
ratio = (double)sprintf_time_us / (double)convert_time_us;
} else {
ratio = 1.0;
}
switch (test_type) {
case TEST_TYPE_ITOA:
caption = "itoa";
break;
case TEST_TYPE_FTOA:
caption = "ftoa";
break;
case TEST_TYPE_INT2HEX:
caption = "int2hex";
break;
case TEST_TYPE_APPEND:
caption = "append";
break;
default:
caption = "unkown";
break;
}
printf("sprintf time: %d ms, %s time: %d ms, "
"sprintf time / %s time: %d%%\n",
sprintf_time_us / 1000, caption, convert_time_us / 1000,
caption, (int)(ratio * 100.00));
}
fast_buffer_destroy(&buffer);
return 0;

View File

@ -32,6 +32,7 @@ int main(int argc, char *argv[])
int fd;
int result;
int n;
int i;
char buf[1024];
if (argc < 2) {
@ -69,7 +70,7 @@ int main(int argc, char *argv[])
return errno != 0 ? errno : EIO;
}
for (int i=0; i<5; i++) {
for (i=0; i<5; i++) {
if (lseek(fd, 0, SEEK_SET) < 0) {
logError("file: "__FILE__", line: %d, " \
"lseek file %s fail, " \

View File

@ -22,6 +22,7 @@
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "fastcommon/logger.h"
int main(int argc, char *argv[])
@ -35,6 +36,15 @@ int main(int argc, char *argv[])
log_take_over_stderr();
log_take_over_stdout();
log_set_compress_log_flags(LOG_COMPRESS_FLAGS_ENABLED | LOG_COMPRESS_FLAGS_NEW_THREAD);
log_set_filename("/opt/fastcfs/fuse/test.log");
if (flock(g_log_context.log_fd, LOCK_EX) != 0) {
logError("flock fail");
}
flock(g_log_context.log_fd, LOCK_UN);
printf("sizeof(LogContext): %d, time_precision: %d, compress_log_flags: %d, "
"use_file_write_lock: %d\n", (int)sizeof(LogContext),
@ -46,7 +56,7 @@ int main(int argc, char *argv[])
"by log_it_ex, timestamp: %d", (int)time(NULL));
len = sprintf(buff, "this is by log_it_ex1, "
"timestamp: %d", (int)time(NULL));
"timestamp: %ld", (long)time(NULL));
log_it_ex1(&g_log_context, LOG_INFO, buff, len);
return 0;