Compare commits
11 Commits
1bdc163269
...
a451179f3a
| Author | SHA1 | Date |
|---|---|---|
|
|
a451179f3a | |
|
|
aa48e3cd9a | |
|
|
ec8e47f831 | |
|
|
d9d6255621 | |
|
|
2f75958a4a | |
|
|
a4cae13e07 | |
|
|
f136821c0d | |
|
|
b97f23ced2 | |
|
|
84a1f90a9a | |
|
|
3f19715e45 | |
|
|
a16fde8070 |
7
HISTORY
7
HISTORY
|
|
@ -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
|
* logger.h export function log_it_ex3
|
||||||
|
* shared_func.[hc]: add function bytes_to_human_str
|
||||||
|
|
||||||
Version 1.78 2025-08-07
|
Version 1.78 2025-08-07
|
||||||
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
|
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
%define CommitVersion %(echo $COMMIT_VERSION)
|
%define CommitVersion %(echo $COMMIT_VERSION)
|
||||||
|
|
||||||
Name: libfastcommon
|
Name: libfastcommon
|
||||||
Version: 1.0.79
|
Version: 1.0.80
|
||||||
Release: 1%{?dist}
|
Release: 1%{?dist}
|
||||||
Summary: c common functions library extracted from my open source projects FastDFS
|
Summary: c common functions library extracted from my open source projects FastDFS
|
||||||
License: LGPL
|
License: LGPL
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ libfastcommon.a: $(FAST_STATIC_OBJS)
|
||||||
.c:
|
.c:
|
||||||
$(COMPILE) -o $@ $< $(FAST_STATIC_OBJS) $(LIB_PATH) $(INC_PATH)
|
$(COMPILE) -o $@ $< $(FAST_STATIC_OBJS) $(LIB_PATH) $(INC_PATH)
|
||||||
.c.o:
|
.c.o:
|
||||||
$(COMPILE) -c -o $@ $< $(INC_PATH)
|
$(COMPILE) -c -fPIC -o $@ $< $(INC_PATH)
|
||||||
.c.lo:
|
.c.lo:
|
||||||
$(COMPILE) -c -fPIC -o $@ $< $(INC_PATH)
|
$(COMPILE) -c -fPIC -o $@ $< $(INC_PATH)
|
||||||
install:
|
install:
|
||||||
|
|
|
||||||
|
|
@ -112,6 +112,13 @@ extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind);
|
||||||
#define SYNC_LOG_BUFF_DEF_INTERVAL 10
|
#define SYNC_LOG_BUFF_DEF_INTERVAL 10
|
||||||
#define TIME_NONE -1
|
#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
|
#if defined(IOV_MAX) && IOV_MAX > 256
|
||||||
#define FC_IOV_BATCH_SIZE 256
|
#define FC_IOV_BATCH_SIZE 256
|
||||||
#else
|
#else
|
||||||
|
|
|
||||||
|
|
@ -136,8 +136,8 @@ static int log_print_header(LogContext *pContext)
|
||||||
if (pContext->current_size < 0)
|
if (pContext->current_size < 0)
|
||||||
{
|
{
|
||||||
result = errno != 0 ? errno : EACCES;
|
result = errno != 0 ? errno : EACCES;
|
||||||
fprintf(stderr, "lseek file \"%s\" fail, " \
|
fprintf(stderr, "lseek file \"%s\" fail, "
|
||||||
"errno: %d, error info: %s\n", \
|
"errno: %d, error info: %s\n",
|
||||||
pContext->log_filename, result, STRERROR(result));
|
pContext->log_filename, result, STRERROR(result));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
|
|
@ -3251,6 +3251,110 @@ const char *long2str(const int64_t n, char *buff, const bool thousands_separator
|
||||||
return buff;
|
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)
|
bool starts_with(const char *str, const char *needle)
|
||||||
{
|
{
|
||||||
int str_len;
|
int str_len;
|
||||||
|
|
@ -4302,6 +4406,119 @@ int fc_itoa(int64_t n, char *buff)
|
||||||
return (start - buff) + len;
|
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)
|
int fc_compare_int64_ptr(const int64_t *n1, const int64_t *n2)
|
||||||
{
|
{
|
||||||
return fc_compare_int64(*n1, *n2);
|
return fc_compare_int64(*n1, *n2);
|
||||||
|
|
|
||||||
|
|
@ -1191,6 +1191,8 @@ static inline const char *long_to_comma_str(const int64_t n, char *buff)
|
||||||
return long2str(n, buff, true);
|
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
|
/** if the string starts with the needle string
|
||||||
* parameters:
|
* parameters:
|
||||||
* str: the string to detect
|
* 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
|
/** itoa output as decimal number
|
||||||
* parameters:
|
* parameters:
|
||||||
* n: the number to convert
|
* n: the integer number to convert
|
||||||
* buff: store the converted string
|
* buff: store the converted string, NOT null-terminated
|
||||||
* return: string length
|
* return: converted string length
|
||||||
*/
|
*/
|
||||||
int fc_itoa(int64_t n, char *buff);
|
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)
|
static inline int fc_ltostr_ex(int64_t n, char *buff, const int padding_len)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
|
|
|
||||||
|
|
@ -1238,18 +1238,18 @@ in_addr_64_t getIpaddrByNameEx(const char *name, char *buff,
|
||||||
return addr4.s_addr;
|
return addr4.s_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*af = AF_UNSPEC;
|
||||||
memset(&hints, 0, sizeof hints);
|
memset(&hints, 0, sizeof hints);
|
||||||
hints.ai_family = AF_UNSPEC; // 支持IPv4和IPv6
|
hints.ai_family = AF_UNSPEC;
|
||||||
if (getaddrinfo(name, NULL, &hints, &res) != 0)
|
if (getaddrinfo(name, NULL, &hints, &res) != 0)
|
||||||
{
|
{
|
||||||
*af = AF_UNSPEC;
|
|
||||||
return INADDR_NONE;
|
return INADDR_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ip_addr = INADDR_NONE;
|
||||||
for (p = res; p != NULL; p = p->ai_next)
|
for (p = res; p != NULL; p = p->ai_next)
|
||||||
{
|
{
|
||||||
*af = p->ai_family;
|
if (p->ai_family == AF_INET) //IPv4 address
|
||||||
if (p->ai_family == AF_INET) // 处理IPv4地址
|
|
||||||
{
|
{
|
||||||
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
|
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
|
||||||
if (buff != NULL)
|
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;
|
ip_addr = ipv4->sin_addr.s_addr;
|
||||||
freeaddrinfo(res);
|
break;
|
||||||
return ip_addr;
|
|
||||||
}
|
}
|
||||||
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;
|
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
|
||||||
if (buff != NULL)
|
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));
|
ip_addr = *((in_addr_64_t *)((char *)&ipv6->sin6_addr + 8));
|
||||||
freeaddrinfo(res);
|
continue;
|
||||||
return ip_addr;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
*af = AF_UNSPEC;
|
return ip_addr;
|
||||||
return INADDR_NONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int getIpaddrsByName(const char *name,
|
int getIpaddrsByName(const char *name,
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,14 @@
|
||||||
#include "fastcommon/fast_buffer.h"
|
#include "fastcommon/fast_buffer.h"
|
||||||
#include "fastcommon/sched_thread.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 {
|
typedef enum {
|
||||||
DA_SLICE_TYPE_FILE = 'F', /* in file slice */
|
DA_SLICE_TYPE_FILE = 'F', /* in file slice */
|
||||||
DA_SLICE_TYPE_CACHE = 'C', /* in memory cache */
|
DA_SLICE_TYPE_CACHE = 'C', /* in memory cache */
|
||||||
|
|
@ -152,20 +160,41 @@ static inline int cache_binlog_filename_by_append(
|
||||||
return p - full_filename;
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
const bool binary_mode = true;
|
const bool binary_mode = true;
|
||||||
const bool check_capacity = false;
|
const bool check_capacity = false;
|
||||||
const bool have_extra_field = false;
|
const bool have_extra_field = false;
|
||||||
const int LOOP = 10 * 1000 * 1000;
|
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;
|
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 i;
|
||||||
|
int len;
|
||||||
int64_t start_time_us;
|
int64_t start_time_us;
|
||||||
int append_time_us;
|
int convert_time_us;
|
||||||
int sprintf_time_us;
|
int sprintf_time_us;
|
||||||
double ratio;
|
double ratio;
|
||||||
FastBuffer buffer;
|
FastBuffer buffer;
|
||||||
DATrunkSpaceLogRecord record;
|
DATrunkSpaceLogRecord record;
|
||||||
|
char full_filename1[PATH_MAX];
|
||||||
|
char full_filename2[PATH_MAX];
|
||||||
|
char buff[32] = {0};
|
||||||
|
char *caption;
|
||||||
|
|
||||||
log_init();
|
log_init();
|
||||||
g_current_time = time(NULL);
|
g_current_time = time(NULL);
|
||||||
|
|
@ -175,53 +204,127 @@ int main(int argc, char *argv[])
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&record, 0, sizeof(record));
|
type_start = type_last = TEST_TYPE_ITOA;
|
||||||
record.op_type = 'C';
|
while ((ch=getopt(argc, argv, "ht:")) != -1) {
|
||||||
record.slice_type = DA_SLICE_TYPE_FILE;
|
switch (ch) {
|
||||||
record.storage.version = 1111;
|
case 'h':
|
||||||
record.oid = 9007211709265131LL;
|
usage(argv[0]);
|
||||||
record.fid = 0;
|
return 0;
|
||||||
record.storage.trunk_id = 61;
|
case 't':
|
||||||
record.storage.length = 62;
|
if (strcasecmp(optarg, "itoa") == 0) {
|
||||||
record.storage.offset = 12345;
|
type_start = type_last = TEST_TYPE_ITOA;
|
||||||
record.storage.size = 64;
|
} else if (strcasecmp(optarg, "ftoa") == 0) {
|
||||||
|
type_start = type_last = TEST_TYPE_FTOA;
|
||||||
|
} else if (strcasecmp(optarg, "int2hex") == 0) {
|
||||||
const char *data_path = "/opt/fastcfs/fdir/data";
|
type_start = type_last = TEST_TYPE_INT2HEX;
|
||||||
const char *subdir_name = "binlog";
|
} else if (strcasecmp(optarg, "append") == 0) {
|
||||||
const uint32_t subdirs = 256;
|
type_start = type_last = TEST_TYPE_APPEND;
|
||||||
uint64_t id = 123456;
|
} else if (strcasecmp(optarg, "all") == 0) {
|
||||||
char full_filename1[PATH_MAX];
|
type_start = TEST_TYPE_ITOA;
|
||||||
char full_filename2[PATH_MAX];
|
type_last = TEST_TYPE_APPEND;
|
||||||
|
} else {
|
||||||
start_time_us = get_current_time_us();
|
fprintf(stderr, "invalid type: %s\n", optarg);
|
||||||
for (i=0; i<LOOP; i++) {
|
return EINVAL;
|
||||||
cache_binlog_filename_by_sprintf(data_path, subdir_name,
|
}
|
||||||
subdirs, ++id, full_filename1, sizeof(full_filename1));
|
break;
|
||||||
fast_buffer_reset(&buffer);
|
default:
|
||||||
log_pack_by_sprintf(&record, &buffer, have_extra_field);
|
usage(argv[0]);
|
||||||
}
|
return EINVAL;
|
||||||
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, "
|
for (test_type=type_start; test_type<=type_last; test_type++) {
|
||||||
"sprintf time / append time: %d%%\n",
|
if (test_type == TEST_TYPE_APPEND) {
|
||||||
sprintf_time_us / 1000, append_time_us / 1000,
|
memset(&record, 0, sizeof(record));
|
||||||
(int)(ratio * 100.00));
|
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);
|
fast_buffer_destroy(&buffer);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ int main(int argc, char *argv[])
|
||||||
int fd;
|
int fd;
|
||||||
int result;
|
int result;
|
||||||
int n;
|
int n;
|
||||||
|
int i;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
|
|
@ -69,7 +70,7 @@ int main(int argc, char *argv[])
|
||||||
return errno != 0 ? errno : EIO;
|
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) {
|
if (lseek(fd, 0, SEEK_SET) < 0) {
|
||||||
logError("file: "__FILE__", line: %d, " \
|
logError("file: "__FILE__", line: %d, " \
|
||||||
"lseek file %s fail, " \
|
"lseek file %s fail, " \
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/file.h>
|
||||||
#include "fastcommon/logger.h"
|
#include "fastcommon/logger.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
|
@ -36,6 +37,15 @@ int main(int argc, char *argv[])
|
||||||
log_take_over_stdout();
|
log_take_over_stdout();
|
||||||
log_set_compress_log_flags(LOG_COMPRESS_FLAGS_ENABLED | LOG_COMPRESS_FLAGS_NEW_THREAD);
|
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, "
|
printf("sizeof(LogContext): %d, time_precision: %d, compress_log_flags: %d, "
|
||||||
"use_file_write_lock: %d\n", (int)sizeof(LogContext),
|
"use_file_write_lock: %d\n", (int)sizeof(LogContext),
|
||||||
g_log_context.time_precision,
|
g_log_context.time_precision,
|
||||||
|
|
@ -46,7 +56,7 @@ int main(int argc, char *argv[])
|
||||||
"by log_it_ex, timestamp: %d", (int)time(NULL));
|
"by log_it_ex, timestamp: %d", (int)time(NULL));
|
||||||
|
|
||||||
len = sprintf(buff, "this is by log_it_ex1, "
|
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);
|
log_it_ex1(&g_log_context, LOG_INFO, buff, len);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue