Compare commits
No commits in common. "master" and "V1.0.81" have entirely different histories.
10
HISTORY
10
HISTORY
|
|
@ -1,14 +1,4 @@
|
||||||
|
|
||||||
Version 1.84 2026-01-16
|
|
||||||
* fast_task_queue.h: add function free_queue_task_arg_offset
|
|
||||||
* add function double2str and double_to_comma_str
|
|
||||||
|
|
||||||
Version 1.83 2025-11-15
|
|
||||||
* fast_task_queue.h: remove field finish_callback
|
|
||||||
|
|
||||||
Version 1.82 2025-11-04
|
|
||||||
* set use_io_uring explicitly
|
|
||||||
|
|
||||||
Version 1.81 2025-10-05
|
Version 1.81 2025-10-05
|
||||||
* support Linux io_uring
|
* support Linux io_uring
|
||||||
* free_queue support parameter: need_shrink and set task->shrinked
|
* free_queue support parameter: need_shrink and set task->shrinked
|
||||||
|
|
|
||||||
2
INSTALL
2
INSTALL
|
|
@ -21,5 +21,5 @@ Debian, Ubuntu etc.:
|
||||||
# the command lines as:
|
# the command lines as:
|
||||||
|
|
||||||
git clone https://github.com/happyfish100/libfastcommon.git
|
git clone https://github.com/happyfish100/libfastcommon.git
|
||||||
cd libfastcommon; git checkout V1.0.84
|
cd libfastcommon; git checkout V1.0.81
|
||||||
./make.sh clean && ./make.sh && sudo ./make.sh install
|
./make.sh clean && ./make.sh && sudo ./make.sh install
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,3 @@
|
||||||
libfastcommon (1.0.83-1) unstable; urgency=medium
|
|
||||||
|
|
||||||
* upgrade to 1.0.83-1
|
|
||||||
|
|
||||||
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 10:47:37 +0000
|
|
||||||
|
|
||||||
libfastcommon (1.0.83-1) unstable; urgency=medium
|
|
||||||
|
|
||||||
* upgrade to 1.0.83-1
|
|
||||||
|
|
||||||
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 10:00:00 +0000
|
|
||||||
|
|
||||||
libfastcommon (1.0.83-1) unstable; urgency=medium
|
|
||||||
|
|
||||||
* upgrade to 1.0.83-1
|
|
||||||
|
|
||||||
-- YuQing <384681@qq.com> Sun, 23 Nov 2025 09:05:57 +0000
|
|
||||||
|
|
||||||
libfastcommon (1.0.78-1) unstable; urgency=medium
|
libfastcommon (1.0.78-1) unstable; urgency=medium
|
||||||
|
|
||||||
* upgrade to 1.0.78-1
|
* upgrade to 1.0.78-1
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
%define CommitVersion %(echo $COMMIT_VERSION)
|
%define CommitVersion %(echo $COMMIT_VERSION)
|
||||||
|
|
||||||
Name: libfastcommon
|
Name: libfastcommon
|
||||||
Version: 1.0.84
|
Version: 1.0.81
|
||||||
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
|
||||||
|
|
@ -19,10 +19,10 @@ Requires: %__cp %__mv %__chmod %__grep %__mkdir %__install %__id
|
||||||
|
|
||||||
%define kernel_major %(uname -r | cut -d'.' -f1)
|
%define kernel_major %(uname -r | cut -d'.' -f1)
|
||||||
%define kernel_minor %(uname -r | cut -d'.' -f2)
|
%define kernel_minor %(uname -r | cut -d'.' -f2)
|
||||||
%define kernel_ver_int %(expr %{kernel_major} \\* 100 + %{kernel_minor})
|
%define kernel_ver_int %(expr %{kernel_major} \* 100 + %{kernel_minor})
|
||||||
%if %{kernel_ver_int} >= 514
|
%if %{kernel_ver_int} >= 514
|
||||||
BuildRequires: liburing-devel >= 2.4
|
BuildRequires: liburing-devel >= 2.5
|
||||||
Requires: liburing >= 2.4
|
Requires: liburing >= 2.5
|
||||||
%endif
|
%endif
|
||||||
|
|
||||||
%description
|
%description
|
||||||
|
|
|
||||||
23
src/base64.c
23
src/base64.c
|
|
@ -297,7 +297,9 @@ char *base64_decode_auto(struct base64_context *context, const char *src, \
|
||||||
|
|
||||||
memcpy(pBuff, src, nSrcLen);
|
memcpy(pBuff, src, nSrcLen);
|
||||||
memset(pBuff + nSrcLen, context->pad_ch, nPadLen);
|
memset(pBuff + nSrcLen, context->pad_ch, nPadLen);
|
||||||
|
|
||||||
base64_decode(context, pBuff, nNewLen, dest, dest_len);
|
base64_decode(context, pBuff, nNewLen, dest, dest_len);
|
||||||
|
|
||||||
if (pBuff != tmpBuff)
|
if (pBuff != tmpBuff)
|
||||||
{
|
{
|
||||||
free(pBuff);
|
free(pBuff);
|
||||||
|
|
@ -383,23 +385,16 @@ char *base64_decode(struct base64_context *context, const char *src, \
|
||||||
|
|
||||||
if (cycle != 0)
|
if (cycle != 0)
|
||||||
{
|
{
|
||||||
*dest = '\0';
|
*dest = '\0';
|
||||||
*dest_len = 0;
|
*dest_len = 0;
|
||||||
fprintf(stderr, "Input to decode not an even multiple of "
|
fprintf(stderr, "Input to decode not an even multiple of " \
|
||||||
"4 characters; pad with %c\n", context->pad_ch);
|
"4 characters; pad with %c\n", context->pad_ch);
|
||||||
return dest;
|
return dest;
|
||||||
}
|
|
||||||
|
|
||||||
if (dummies > 2)
|
|
||||||
{
|
|
||||||
*dest = '\0';
|
|
||||||
*dest_len = 0;
|
|
||||||
fprintf(stderr, "pad char: %c count %d exceeds 2\n",
|
|
||||||
context->pad_ch, dummies);
|
|
||||||
return dest;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*dest_len = (pDest - dest) - dummies;
|
*dest_len = (pDest - dest) - dummies;
|
||||||
*(dest + (*dest_len)) = '\0';
|
*(dest + (*dest_len)) = '\0';
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@
|
||||||
static int task_alloc_init(struct fast_task_info *task,
|
static int task_alloc_init(struct fast_task_info *task,
|
||||||
struct fast_task_queue *queue)
|
struct fast_task_queue *queue)
|
||||||
{
|
{
|
||||||
task->arg = (char *)task + free_queue_task_arg_offset(queue);
|
task->arg = (char *)task + ALIGNED_TASK_INFO_SIZE + queue->padding_size;
|
||||||
task->send.ptr = &task->send.holder;
|
task->send.ptr = &task->send.holder;
|
||||||
task->send.ptr->size = queue->min_buff_size;
|
task->send.ptr->size = queue->min_buff_size;
|
||||||
if (queue->malloc_whole_block) {
|
if (queue->malloc_whole_block) {
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ struct nio_thread_data;
|
||||||
struct fast_task_info;
|
struct fast_task_info;
|
||||||
|
|
||||||
typedef int (*ThreadLoopCallback) (struct nio_thread_data *pThreadData);
|
typedef int (*ThreadLoopCallback) (struct nio_thread_data *pThreadData);
|
||||||
|
typedef int (*TaskFinishCallback) (struct fast_task_info *task);
|
||||||
typedef void (*TaskCleanUpCallback) (struct fast_task_info *task);
|
typedef void (*TaskCleanUpCallback) (struct fast_task_info *task);
|
||||||
typedef int (*TaskInitCallback)(struct fast_task_info *task, void *arg);
|
typedef int (*TaskInitCallback)(struct fast_task_info *task, void *arg);
|
||||||
typedef void (*TaskReleaseCallback)(struct fast_task_info *task);
|
typedef void (*TaskReleaseCallback)(struct fast_task_info *task);
|
||||||
|
|
@ -148,6 +149,7 @@ struct fast_task_info
|
||||||
struct fc_list_head dlink; //for polling queue
|
struct fc_list_head dlink; //for polling queue
|
||||||
} polling; //for RDMA busy polling
|
} polling; //for RDMA busy polling
|
||||||
TaskContinueCallback continue_callback; //for continue stage
|
TaskContinueCallback continue_callback; //for continue stage
|
||||||
|
TaskFinishCallback finish_callback;
|
||||||
struct nio_thread_data *thread_data;
|
struct nio_thread_data *thread_data;
|
||||||
struct sf_network_handler *handler; //network handler for libserverframe nio
|
struct sf_network_handler *handler; //network handler for libserverframe nio
|
||||||
struct fast_task_info *next; //for free queue and deleted list
|
struct fast_task_info *next; //for free queue and deleted list
|
||||||
|
|
@ -236,11 +238,6 @@ static inline int free_queue_alloc_connections(struct fast_task_queue *queue)
|
||||||
return queue->allocator.info.element_total_count;
|
return queue->allocator.info.element_total_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int free_queue_task_arg_offset(struct fast_task_queue *queue)
|
|
||||||
{
|
|
||||||
return ALIGNED_TASK_INFO_SIZE + queue->padding_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
int free_queue_get_new_buffer_size(const int min_buff_size,
|
int free_queue_get_new_buffer_size(const int min_buff_size,
|
||||||
const int max_buff_size, const int expect_size, int *new_size);
|
const int max_buff_size, const int expect_size, int *new_size);
|
||||||
|
|
||||||
|
|
|
||||||
220
src/ioevent.c
220
src/ioevent.c
|
|
@ -46,40 +46,36 @@ int kqueue_ev_convert(int16_t event, uint16_t flags)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int ioevent_init(IOEventPoller *ioevent, const char *service_name,
|
int ioevent_init(IOEventPoller *ioevent, const char *service_name,
|
||||||
const bool use_io_uring, const int size, const int timeout_ms,
|
const int size, const int timeout_ms, const int extra_events)
|
||||||
const int extra_events)
|
|
||||||
{
|
{
|
||||||
|
#if IOEVENT_USE_URING
|
||||||
|
int result;
|
||||||
|
#else
|
||||||
int bytes;
|
int bytes;
|
||||||
|
|
||||||
ioevent->iterator.index = 0;
|
ioevent->iterator.index = 0;
|
||||||
ioevent->iterator.count = 0;
|
ioevent->iterator.count = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
ioevent->service_name = service_name;
|
ioevent->service_name = service_name;
|
||||||
ioevent->size = size;
|
ioevent->size = size;
|
||||||
ioevent->extra_events = extra_events;
|
ioevent->extra_events = extra_events;
|
||||||
|
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#if IOEVENT_USE_URING
|
ioevent->poll_fd = epoll_create(ioevent->size);
|
||||||
ioevent->use_io_uring = use_io_uring;
|
if (ioevent->poll_fd < 0) {
|
||||||
if (use_io_uring) {
|
return errno != 0 ? errno : ENOMEM;
|
||||||
int result;
|
|
||||||
if ((result=io_uring_queue_init(size, &ioevent->ring, 0)) < 0) {
|
|
||||||
return -result;
|
|
||||||
}
|
|
||||||
ioevent->cqe = NULL;
|
|
||||||
ioevent->submit_count = 0;
|
|
||||||
ioevent->send_zc_logged = false;
|
|
||||||
ioevent->send_zc_done_notify = false;
|
|
||||||
} else {
|
|
||||||
#endif
|
|
||||||
ioevent->poll_fd = epoll_create(ioevent->size);
|
|
||||||
if (ioevent->poll_fd < 0) {
|
|
||||||
return errno != 0 ? errno : ENOMEM;
|
|
||||||
}
|
|
||||||
bytes = sizeof(struct epoll_event) * size;
|
|
||||||
ioevent->events = (struct epoll_event *)fc_malloc(bytes);
|
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
}
|
||||||
#endif
|
bytes = sizeof(struct epoll_event) * size;
|
||||||
|
ioevent->events = (struct epoll_event *)fc_malloc(bytes);
|
||||||
|
#elif IOEVENT_USE_URING
|
||||||
|
if ((result=io_uring_queue_init(size, &ioevent->ring, 0)) < 0) {
|
||||||
|
return -result;
|
||||||
|
}
|
||||||
|
ioevent->cqe = NULL;
|
||||||
|
ioevent->submit_count = 0;
|
||||||
|
ioevent->send_zc_logged = false;
|
||||||
|
ioevent->send_zc_done_notify = false;
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
ioevent->poll_fd = kqueue();
|
ioevent->poll_fd = kqueue();
|
||||||
if (ioevent->poll_fd < 0) {
|
if (ioevent->poll_fd < 0) {
|
||||||
|
|
@ -97,14 +93,12 @@ int ioevent_init(IOEventPoller *ioevent, const char *service_name,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if IOEVENT_USE_URING
|
#if IOEVENT_USE_URING
|
||||||
if (!ioevent->use_io_uring) {
|
|
||||||
#endif
|
#else
|
||||||
if (ioevent->events == NULL) {
|
if (ioevent->events == NULL) {
|
||||||
close(ioevent->poll_fd);
|
close(ioevent->poll_fd);
|
||||||
ioevent->poll_fd = -1;
|
ioevent->poll_fd = -1;
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -115,49 +109,38 @@ int ioevent_init(IOEventPoller *ioevent, const char *service_name,
|
||||||
void ioevent_destroy(IOEventPoller *ioevent)
|
void ioevent_destroy(IOEventPoller *ioevent)
|
||||||
{
|
{
|
||||||
#if IOEVENT_USE_URING
|
#if IOEVENT_USE_URING
|
||||||
if (ioevent->use_io_uring) {
|
io_uring_queue_exit(&ioevent->ring);
|
||||||
io_uring_queue_exit(&ioevent->ring);
|
#else
|
||||||
} else {
|
if (ioevent->events != NULL) {
|
||||||
#endif
|
free(ioevent->events);
|
||||||
if (ioevent->events != NULL) {
|
ioevent->events = NULL;
|
||||||
free(ioevent->events);
|
}
|
||||||
ioevent->events = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ioevent->poll_fd >= 0) {
|
if (ioevent->poll_fd >= 0) {
|
||||||
close(ioevent->poll_fd);
|
close(ioevent->poll_fd);
|
||||||
ioevent->poll_fd = -1;
|
ioevent->poll_fd = -1;
|
||||||
}
|
}
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int ioevent_attach(IOEventPoller *ioevent, const int fd,
|
int ioevent_attach(IOEventPoller *ioevent, const int fd,
|
||||||
const int e, void *data)
|
const int e, void *data)
|
||||||
{
|
{
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#if IOEVENT_USE_URING
|
struct epoll_event ev;
|
||||||
if (ioevent->use_io_uring) {
|
memset(&ev, 0, sizeof(ev));
|
||||||
struct io_uring_sqe *sqe = io_uring_get_sqe(&ioevent->ring);
|
ev.events = e | ioevent->extra_events;
|
||||||
if (sqe == NULL) {
|
ev.data.ptr = data;
|
||||||
return ENOSPC;
|
return epoll_ctl(ioevent->poll_fd, EPOLL_CTL_ADD, fd, &ev);
|
||||||
}
|
#elif IOEVENT_USE_URING
|
||||||
io_uring_prep_poll_multishot(sqe, fd, e | ioevent->extra_events);
|
struct io_uring_sqe *sqe = io_uring_get_sqe(&ioevent->ring);
|
||||||
sqe->user_data = (long)data;
|
if (sqe == NULL) {
|
||||||
ioevent->submit_count++;
|
return ENOSPC;
|
||||||
return 0;
|
}
|
||||||
} else {
|
io_uring_prep_poll_multishot(sqe, fd, e | ioevent->extra_events);
|
||||||
#endif
|
sqe->user_data = (long)data;
|
||||||
struct epoll_event ev;
|
ioevent->submit_count++;
|
||||||
memset(&ev, 0, sizeof(ev));
|
return 0;
|
||||||
ev.events = e | ioevent->extra_events;
|
|
||||||
ev.data.ptr = data;
|
|
||||||
return epoll_ctl(ioevent->poll_fd, EPOLL_CTL_ADD, fd, &ev);
|
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
struct kevent ev[2];
|
struct kevent ev[2];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
@ -179,29 +162,22 @@ int ioevent_attach(IOEventPoller *ioevent, const int fd,
|
||||||
int ioevent_modify(IOEventPoller *ioevent, const int fd,
|
int ioevent_modify(IOEventPoller *ioevent, const int fd,
|
||||||
const int e, void *data)
|
const int e, void *data)
|
||||||
{
|
{
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#if IOEVENT_USE_URING
|
struct epoll_event ev;
|
||||||
if (ioevent->use_io_uring) {
|
memset(&ev, 0, sizeof(ev));
|
||||||
struct io_uring_sqe *sqe = io_uring_get_sqe(&ioevent->ring);
|
ev.events = e | ioevent->extra_events;
|
||||||
if (sqe == NULL) {
|
ev.data.ptr = data;
|
||||||
return ENOSPC;
|
return epoll_ctl(ioevent->poll_fd, EPOLL_CTL_MOD, fd, &ev);
|
||||||
}
|
#elif IOEVENT_USE_URING
|
||||||
io_uring_prep_poll_update(sqe, sqe->user_data, sqe->user_data,
|
struct io_uring_sqe *sqe = io_uring_get_sqe(&ioevent->ring);
|
||||||
e | ioevent->extra_events, IORING_POLL_UPDATE_EVENTS);
|
if (sqe == NULL) {
|
||||||
sqe->user_data = (long)data;
|
return ENOSPC;
|
||||||
ioevent->submit_count++;
|
}
|
||||||
return 0;
|
io_uring_prep_poll_update(sqe, sqe->user_data, sqe->user_data,
|
||||||
} else {
|
e | ioevent->extra_events, IORING_POLL_UPDATE_EVENTS);
|
||||||
#endif
|
sqe->user_data = (long)data;
|
||||||
struct epoll_event ev;
|
ioevent->submit_count++;
|
||||||
memset(&ev, 0, sizeof(ev));
|
return 0;
|
||||||
ev.events = e | ioevent->extra_events;
|
|
||||||
ev.data.ptr = data;
|
|
||||||
return epoll_ctl(ioevent->poll_fd, EPOLL_CTL_MOD, fd, &ev);
|
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
struct kevent ev[2];
|
struct kevent ev[2];
|
||||||
int result;
|
int result;
|
||||||
|
|
@ -236,25 +212,18 @@ int ioevent_modify(IOEventPoller *ioevent, const int fd,
|
||||||
|
|
||||||
int ioevent_detach(IOEventPoller *ioevent, const int fd)
|
int ioevent_detach(IOEventPoller *ioevent, const int fd)
|
||||||
{
|
{
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#if IOEVENT_USE_URING
|
return epoll_ctl(ioevent->poll_fd, EPOLL_CTL_DEL, fd, NULL);
|
||||||
if (ioevent->use_io_uring) {
|
#elif IOEVENT_USE_URING
|
||||||
struct io_uring_sqe *sqe = io_uring_get_sqe(&ioevent->ring);
|
struct io_uring_sqe *sqe = io_uring_get_sqe(&ioevent->ring);
|
||||||
if (sqe == NULL) {
|
if (sqe == NULL) {
|
||||||
return ENOSPC;
|
return ENOSPC;
|
||||||
}
|
}
|
||||||
io_uring_prep_cancel_fd(sqe, fd, 0);
|
io_uring_prep_cancel_fd(sqe, fd, 0);
|
||||||
/* set sqe->flags MUST after io_uring_prep_xxx */
|
/* set sqe->flags MUST after io_uring_prep_xxx */
|
||||||
sqe->flags = IOSQE_CQE_SKIP_SUCCESS;
|
sqe->flags = IOSQE_CQE_SKIP_SUCCESS;
|
||||||
ioevent->submit_count++;
|
ioevent->submit_count++;
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
|
||||||
#endif
|
|
||||||
return epoll_ctl(ioevent->poll_fd, EPOLL_CTL_DEL, fd, NULL);
|
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
struct kevent ev[1];
|
struct kevent ev[1];
|
||||||
int r, w;
|
int r, w;
|
||||||
|
|
@ -273,25 +242,18 @@ int ioevent_detach(IOEventPoller *ioevent, const int fd)
|
||||||
|
|
||||||
int ioevent_poll(IOEventPoller *ioevent)
|
int ioevent_poll(IOEventPoller *ioevent)
|
||||||
{
|
{
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#if IOEVENT_USE_URING
|
return epoll_wait(ioevent->poll_fd, ioevent->events,
|
||||||
if (ioevent->use_io_uring) {
|
ioevent->size, ioevent->timeout);
|
||||||
int result;
|
#elif IOEVENT_USE_URING
|
||||||
result = io_uring_wait_cqe_timeout(&ioevent->ring,
|
int result;
|
||||||
&ioevent->cqe, &ioevent->timeout);
|
result = io_uring_wait_cqe_timeout(&ioevent->ring,
|
||||||
if (result < 0) {
|
&ioevent->cqe, &ioevent->timeout);
|
||||||
errno = -result;
|
if (result < 0) {
|
||||||
return -1;
|
errno = -result;
|
||||||
}
|
return -1;
|
||||||
return 0;
|
}
|
||||||
} else {
|
return 0;
|
||||||
#endif
|
|
||||||
return epoll_wait(ioevent->poll_fd, ioevent->events,
|
|
||||||
ioevent->size, ioevent->timeout_ms);
|
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
return kevent(ioevent->poll_fd, NULL, 0, ioevent->events,
|
return kevent(ioevent->poll_fd, NULL, 0, ioevent->events,
|
||||||
ioevent->size, &ioevent->timeout);
|
ioevent->size, &ioevent->timeout);
|
||||||
|
|
|
||||||
|
|
@ -25,12 +25,10 @@
|
||||||
#define IOEVENT_TIMEOUT (1 << 20)
|
#define IOEVENT_TIMEOUT (1 << 20)
|
||||||
#define IOEVENT_NOTIFY (1 << 21) //for io_uring send_zc done callback
|
#define IOEVENT_NOTIFY (1 << 21) //for io_uring send_zc done callback
|
||||||
|
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
#define IOEVENT_EDGE_TRIGGER EPOLLET
|
#define IOEVENT_EDGE_TRIGGER EPOLLET
|
||||||
#endif
|
|
||||||
|
|
||||||
#if IOEVENT_USE_EPOLL
|
|
||||||
#define IOEVENT_READ EPOLLIN
|
#define IOEVENT_READ EPOLLIN
|
||||||
#define IOEVENT_WRITE EPOLLOUT
|
#define IOEVENT_WRITE EPOLLOUT
|
||||||
#define IOEVENT_ERROR (EPOLLERR | EPOLLPRI | EPOLLHUP)
|
#define IOEVENT_ERROR (EPOLLERR | EPOLLPRI | EPOLLHUP)
|
||||||
|
|
@ -79,30 +77,25 @@ typedef struct ioevent_puller {
|
||||||
const char *service_name;
|
const char *service_name;
|
||||||
int size; //max events (fd)
|
int size; //max events (fd)
|
||||||
int extra_events;
|
int extra_events;
|
||||||
|
|
||||||
#if IOEVENT_USE_URING
|
#if IOEVENT_USE_URING
|
||||||
struct io_uring ring;
|
struct io_uring ring;
|
||||||
int submit_count;
|
int submit_count;
|
||||||
bool send_zc_logged;
|
bool send_zc_logged;
|
||||||
bool send_zc_done_notify; //if callback when send_zc done
|
bool send_zc_done_notify; //if callback when send_zc done
|
||||||
bool use_io_uring;
|
#else
|
||||||
#endif
|
|
||||||
|
|
||||||
int poll_fd;
|
int poll_fd;
|
||||||
struct {
|
struct {
|
||||||
int index;
|
int index;
|
||||||
int count;
|
int count;
|
||||||
} iterator; //for deal event loop
|
} iterator; //for deal event loop
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
struct epoll_event *events;
|
struct epoll_event *events;
|
||||||
int timeout_ms; //for epoll
|
int timeout; //in milliseconds
|
||||||
#if IOEVENT_USE_URING
|
#elif IOEVENT_USE_URING
|
||||||
struct io_uring_cqe *cqe;
|
struct io_uring_cqe *cqe;
|
||||||
struct __kernel_timespec timeout;
|
struct __kernel_timespec timeout;
|
||||||
#endif
|
|
||||||
bool zero_timeout;
|
|
||||||
|
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
struct kevent *events;
|
struct kevent *events;
|
||||||
struct timespec timeout;
|
struct timespec timeout;
|
||||||
|
|
@ -111,11 +104,17 @@ typedef struct ioevent_puller {
|
||||||
timespec_t timeout;
|
timespec_t timeout;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef OS_LINUX
|
||||||
|
bool zero_timeout;
|
||||||
|
#endif
|
||||||
|
|
||||||
} IOEventPoller;
|
} IOEventPoller;
|
||||||
|
|
||||||
#if OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#define IOEVENT_GET_EVENTS(ioevent, index) \
|
#define IOEVENT_GET_EVENTS(ioevent, index) \
|
||||||
(ioevent)->events[index].events
|
(ioevent)->events[index].events
|
||||||
|
#elif IOEVENT_USE_URING
|
||||||
|
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
#define IOEVENT_GET_EVENTS(ioevent, index) kqueue_ev_convert( \
|
#define IOEVENT_GET_EVENTS(ioevent, index) kqueue_ev_convert( \
|
||||||
(ioevent)->events[index].filter, (ioevent)->events[index].flags)
|
(ioevent)->events[index].filter, (ioevent)->events[index].flags)
|
||||||
|
|
@ -126,9 +125,11 @@ typedef struct ioevent_puller {
|
||||||
#error port me
|
#error port me
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#define IOEVENT_GET_DATA(ioevent, index) \
|
#define IOEVENT_GET_DATA(ioevent, index) \
|
||||||
(ioevent)->events[index].data.ptr
|
(ioevent)->events[index].data.ptr
|
||||||
|
#elif IOEVENT_USE_URING
|
||||||
|
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
#define IOEVENT_GET_DATA(ioevent, index) \
|
#define IOEVENT_GET_DATA(ioevent, index) \
|
||||||
(ioevent)->events[index].udata
|
(ioevent)->events[index].udata
|
||||||
|
|
@ -139,9 +140,11 @@ typedef struct ioevent_puller {
|
||||||
#error port me
|
#error port me
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef OS_LINUX
|
#if IOEVENT_USE_EPOLL
|
||||||
#define IOEVENT_CLEAR_DATA(ioevent, index) \
|
#define IOEVENT_CLEAR_DATA(ioevent, index) \
|
||||||
(ioevent)->events[index].data.ptr = NULL
|
(ioevent)->events[index].data.ptr = NULL
|
||||||
|
#elif IOEVENT_USE_URING
|
||||||
|
|
||||||
#elif IOEVENT_USE_KQUEUE
|
#elif IOEVENT_USE_KQUEUE
|
||||||
#define IOEVENT_CLEAR_DATA(ioevent, index) \
|
#define IOEVENT_CLEAR_DATA(ioevent, index) \
|
||||||
(ioevent)->events[index].udata = NULL
|
(ioevent)->events[index].udata = NULL
|
||||||
|
|
@ -157,8 +160,7 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int ioevent_init(IOEventPoller *ioevent, const char *service_name,
|
int ioevent_init(IOEventPoller *ioevent, const char *service_name,
|
||||||
const bool use_io_uring, const int size, const int timeout_ms,
|
const int size, const int timeout_ms, const int extra_events);
|
||||||
const int extra_events);
|
|
||||||
void ioevent_destroy(IOEventPoller *ioevent);
|
void ioevent_destroy(IOEventPoller *ioevent);
|
||||||
|
|
||||||
int ioevent_attach(IOEventPoller *ioevent, const int fd,
|
int ioevent_attach(IOEventPoller *ioevent, const int fd,
|
||||||
|
|
@ -172,24 +174,16 @@ static inline void ioevent_set_timeout(IOEventPoller *ioevent,
|
||||||
const int timeout_ms)
|
const int timeout_ms)
|
||||||
{
|
{
|
||||||
#if IOEVENT_USE_EPOLL
|
#if IOEVENT_USE_EPOLL
|
||||||
ioevent->timeout_ms = timeout_ms;
|
ioevent->timeout = timeout_ms;
|
||||||
#else
|
#else
|
||||||
#if IOEVENT_USE_URING
|
ioevent->timeout.tv_sec = timeout_ms / 1000;
|
||||||
if (!ioevent->use_io_uring) {
|
ioevent->timeout.tv_nsec = 1000000 * (timeout_ms % 1000);
|
||||||
ioevent->timeout_ms = timeout_ms;
|
|
||||||
} else {
|
|
||||||
#endif
|
|
||||||
ioevent->timeout.tv_sec = timeout_ms / 1000;
|
|
||||||
ioevent->timeout.tv_nsec = 1000000 * (timeout_ms % 1000);
|
|
||||||
|
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef OS_LINUX
|
#ifdef OS_LINUX
|
||||||
ioevent->zero_timeout = (timeout_ms == 0);
|
ioevent->zero_timeout = (timeout_ms == 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int ioevent_poll_ex(IOEventPoller *ioevent, const int timeout_ms)
|
static inline int ioevent_poll_ex(IOEventPoller *ioevent, const int timeout_ms)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
#include "ioevent_loop.h"
|
#include "ioevent_loop.h"
|
||||||
|
|
||||||
#if IOEVENT_USE_URING
|
#if IOEVENT_USE_URING
|
||||||
static int ioevent_process_by_uring(IOEventPoller *ioevent)
|
static int ioevent_process(IOEventPoller *ioevent)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
unsigned head;
|
unsigned head;
|
||||||
|
|
@ -85,7 +85,7 @@ static int ioevent_process_by_uring(IOEventPoller *ioevent)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#else
|
||||||
|
|
||||||
static void deal_ioevents(IOEventPoller *ioevent)
|
static void deal_ioevents(IOEventPoller *ioevent)
|
||||||
{
|
{
|
||||||
|
|
@ -109,7 +109,7 @@ static void deal_ioevents(IOEventPoller *ioevent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ioevent_process_by_poll(IOEventPoller *ioevent)
|
static int ioevent_process(IOEventPoller *ioevent)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
|
|
@ -130,6 +130,8 @@ static int ioevent_process_by_poll(IOEventPoller *ioevent)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
static void deal_timeouts(FastTimerEntry *head)
|
static void deal_timeouts(FastTimerEntry *head)
|
||||||
{
|
{
|
||||||
FastTimerEntry *entry;
|
FastTimerEntry *entry;
|
||||||
|
|
@ -145,7 +147,10 @@ static void deal_timeouts(FastTimerEntry *head)
|
||||||
/* must set NULL because NOT in time wheel */
|
/* must set NULL because NOT in time wheel */
|
||||||
current->prev = current->next = NULL;
|
current->prev = current->next = NULL;
|
||||||
pEventEntry = (IOEventEntry *)current;
|
pEventEntry = (IOEventEntry *)current;
|
||||||
pEventEntry->callback(pEventEntry->fd, IOEVENT_TIMEOUT, current);
|
if (pEventEntry != NULL)
|
||||||
|
{
|
||||||
|
pEventEntry->callback(pEventEntry->fd, IOEVENT_TIMEOUT, current);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -201,38 +206,20 @@ int ioevent_loop(struct nio_thread_data *thread_data,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if IOEVENT_USE_URING
|
#if IOEVENT_USE_URING
|
||||||
if (thread_data->ev_puller.use_io_uring) {
|
if (thread_data->ev_puller.submit_count > 0) {
|
||||||
if (thread_data->ev_puller.submit_count > 0) {
|
if ((result=ioevent_uring_submit(&thread_data->ev_puller)) != 0) {
|
||||||
if ((result=ioevent_uring_submit(&thread_data->
|
logError("file: "__FILE__", line: %d, "
|
||||||
ev_puller)) != 0)
|
"io_uring_submit fail, errno: %d, error info: %s",
|
||||||
{
|
__LINE__, result, STRERROR(result));
|
||||||
logError("file: "__FILE__", line: %d, "
|
return result;
|
||||||
"io_uring_submit fail, errno: %d, error info: %s",
|
|
||||||
__LINE__, result, STRERROR(result));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (sched_pull) {
|
if (sched_pull) {
|
||||||
#if IOEVENT_USE_URING
|
if ((result=ioevent_process(&thread_data->ev_puller)) != 0) {
|
||||||
if (thread_data->ev_puller.use_io_uring) {
|
return result;
|
||||||
if ((result=ioevent_process_by_uring(&thread_data->
|
|
||||||
ev_puller)) != 0)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
#endif
|
|
||||||
if ((result=ioevent_process_by_poll(&thread_data->
|
|
||||||
ev_puller)) != 0)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
#if IOEVENT_USE_URING
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (thread_data->busy_polling_callback != NULL) {
|
if (thread_data->busy_polling_callback != NULL) {
|
||||||
|
|
@ -240,7 +227,7 @@ int ioevent_loop(struct nio_thread_data *thread_data,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (thread_data->deleted_list != NULL) {
|
if (thread_data->deleted_list != NULL) {
|
||||||
//count = 0;
|
count = 0;
|
||||||
while (thread_data->deleted_list != NULL) {
|
while (thread_data->deleted_list != NULL) {
|
||||||
task = thread_data->deleted_list;
|
task = thread_data->deleted_list;
|
||||||
thread_data->deleted_list = task->next;
|
thread_data->deleted_list = task->next;
|
||||||
|
|
@ -254,7 +241,7 @@ int ioevent_loop(struct nio_thread_data *thread_data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
clean_up_callback(task);
|
clean_up_callback(task);
|
||||||
//count++;
|
count++;
|
||||||
}
|
}
|
||||||
//logInfo("cleanup task count: %d", count);
|
//logInfo("cleanup task count: %d", count);
|
||||||
}
|
}
|
||||||
|
|
@ -293,7 +280,7 @@ int ioevent_loop(struct nio_thread_data *thread_data,
|
||||||
|
|
||||||
int ioevent_set(struct fast_task_info *task, struct nio_thread_data *pThread,
|
int ioevent_set(struct fast_task_info *task, struct nio_thread_data *pThread,
|
||||||
int sock, short event, IOEventCallback callback,
|
int sock, short event, IOEventCallback callback,
|
||||||
const int timeout)
|
const int timeout, const bool use_iouring)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
|
|
@ -301,7 +288,7 @@ int ioevent_set(struct fast_task_info *task, struct nio_thread_data *pThread,
|
||||||
task->event.fd = sock;
|
task->event.fd = sock;
|
||||||
task->event.callback = callback;
|
task->event.callback = callback;
|
||||||
#if IOEVENT_USE_URING
|
#if IOEVENT_USE_URING
|
||||||
if (pThread->ev_puller.use_io_uring) {
|
if (use_iouring) {
|
||||||
if (FC_URING_OP_TYPE(task) == IORING_OP_NOP) {
|
if (FC_URING_OP_TYPE(task) == IORING_OP_NOP) {
|
||||||
if ((result=uring_prep_first_recv(task)) != 0) {
|
if ((result=uring_prep_first_recv(task)) != 0) {
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ int ioevent_loop(struct nio_thread_data *thread_data,
|
||||||
|
|
||||||
int ioevent_set(struct fast_task_info *task, struct nio_thread_data *pThread,
|
int ioevent_set(struct fast_task_info *task, struct nio_thread_data *pThread,
|
||||||
int sock, short event, IOEventCallback callback,
|
int sock, short event, IOEventCallback callback,
|
||||||
const int timeout);
|
const int timeout, const bool use_iouring);
|
||||||
|
|
||||||
int ioevent_reset(struct fast_task_info *task, int new_fd, short event);
|
int ioevent_reset(struct fast_task_info *task, int new_fd, short event);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,6 @@ int fast_multi_sock_client_init_ex(FastMultiSockClient *client,
|
||||||
fms_client_get_current_time_ms_func get_current_time_ms_func,
|
fms_client_get_current_time_ms_func get_current_time_ms_func,
|
||||||
const int init_recv_buffer_size, const int timeout_ms)
|
const int init_recv_buffer_size, const int timeout_ms)
|
||||||
{
|
{
|
||||||
const bool use_io_uring = false;
|
|
||||||
int result;
|
int result;
|
||||||
int new_init_recv_buffer_size;
|
int new_init_recv_buffer_size;
|
||||||
int i;
|
int i;
|
||||||
|
|
@ -67,7 +66,7 @@ int fast_multi_sock_client_init_ex(FastMultiSockClient *client,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((result=ioevent_init(&client->ioevent, "client",
|
if ((result=ioevent_init(&client->ioevent, "client",
|
||||||
use_io_uring, entry_count, timeout_ms, 0)) != 0)
|
entry_count, timeout_ms, 0)) != 0)
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"ioevent_init fail, errno: %d, error info: %s",
|
"ioevent_init fail, errno: %d, error info: %s",
|
||||||
|
|
|
||||||
|
|
@ -3178,7 +3178,7 @@ key_t fc_ftok(const char *path, const int proj_id)
|
||||||
return (((proj_id & 0xFF) << 24) | (hash_code & 0xFFFFFF));
|
return (((proj_id & 0xFF) << 24) | (hash_code & 0xFFFFFF));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int add_thousands_separator(char *str, const int len)
|
static void add_thousands_separator(char *str, const int len)
|
||||||
{
|
{
|
||||||
int new_len;
|
int new_len;
|
||||||
int addings;
|
int addings;
|
||||||
|
|
@ -3191,7 +3191,7 @@ static int add_thousands_separator(char *str, const int len)
|
||||||
|
|
||||||
if (len <= 3)
|
if (len <= 3)
|
||||||
{
|
{
|
||||||
return len;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*str == '-')
|
if (*str == '-')
|
||||||
|
|
@ -3227,8 +3227,6 @@ static int add_thousands_separator(char *str, const int len)
|
||||||
add_count++;
|
add_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *int2str(const int n, char *buff, const bool thousands_separator)
|
const char *int2str(const int n, char *buff, const bool thousands_separator)
|
||||||
|
|
@ -4525,33 +4523,3 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *double2str(const double d, const int scale,
|
|
||||||
char *buff, const bool thousands_separator)
|
|
||||||
{
|
|
||||||
int len;
|
|
||||||
int front_len;
|
|
||||||
int tail_len;
|
|
||||||
int new_len;
|
|
||||||
char *point;
|
|
||||||
char fragment[32];
|
|
||||||
|
|
||||||
len = fc_ftoa(d, scale, buff);
|
|
||||||
*(buff + len) = '\0';
|
|
||||||
if (!thousands_separator) {
|
|
||||||
return buff;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scale <= 0) {
|
|
||||||
add_thousands_separator(buff, len);
|
|
||||||
return buff;
|
|
||||||
}
|
|
||||||
|
|
||||||
tail_len = 1 + scale;
|
|
||||||
front_len = len - tail_len;
|
|
||||||
point = buff + front_len;
|
|
||||||
memcpy(fragment, point, tail_len + 1);
|
|
||||||
new_len = add_thousands_separator(buff, front_len);
|
|
||||||
memcpy(buff + new_len, fragment, tail_len + 1);
|
|
||||||
return buff;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1191,23 +1191,6 @@ static inline const char *long_to_comma_str(const int64_t n, char *buff)
|
||||||
return long2str(n, buff, true);
|
return long2str(n, buff, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** convert double to string
|
|
||||||
* parameters:
|
|
||||||
* d: the double number
|
|
||||||
* scale: number of decimal places (round off)
|
|
||||||
* buff: output buffer
|
|
||||||
* thousands_separator: if add thousands separator
|
|
||||||
* return: string buffer
|
|
||||||
*/
|
|
||||||
const char *double2str(const double d, const int scale,
|
|
||||||
char *buff, const bool thousands_separator);
|
|
||||||
|
|
||||||
static inline const char *double_to_comma_str(const double d,
|
|
||||||
const int scale, char *buff)
|
|
||||||
{
|
|
||||||
return double2str(d, scale, buff, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *bytes_to_human_str(const int64_t bytes, char *buff);
|
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
|
||||||
|
|
@ -1653,29 +1636,16 @@ static inline int fc_ltostr_ex(int64_t n, char *buff, const int padding_len)
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(OS_LINUX) && defined(__GNUC__) && __GNUC__ >= 8
|
|
||||||
#pragma GCC diagnostic push
|
|
||||||
#pragma GCC diagnostic ignored "-Wstringop-overflow"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
fill_len = padding_len - len;
|
fill_len = padding_len - len;
|
||||||
memmove(buff + fill_len, buff, len + 1);
|
memmove(buff + fill_len, buff, len + 1);
|
||||||
memset(buff, '0', fill_len);
|
memset(buff, '0', fill_len);
|
||||||
|
|
||||||
#if defined(OS_LINUX) && defined(__GNUC__) && __GNUC__ >= 8
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return padding_len;
|
return padding_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int fc_ltostr(int64_t n, char *buff)
|
static inline int fc_ltostr(int64_t n, char *buff)
|
||||||
{
|
{
|
||||||
int len;
|
const int padding_len = 0;
|
||||||
|
return fc_ltostr_ex(n, buff, padding_len);
|
||||||
len = fc_itoa(n, buff);
|
|
||||||
*(buff + len) = '\0';
|
|
||||||
return len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
|
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
|
||||||
|
|
|
||||||
|
|
@ -28,68 +28,16 @@
|
||||||
#include "fastcommon/fast_allocator.h"
|
#include "fastcommon/fast_allocator.h"
|
||||||
|
|
||||||
#define LOOP_COUNT (30 * 1000 * 1000)
|
#define LOOP_COUNT (30 * 1000 * 1000)
|
||||||
#define THREAD_COUNT 2
|
|
||||||
#define barrier() __asm__ __volatile__("" ::: "memory")
|
#define barrier() __asm__ __volatile__("" ::: "memory")
|
||||||
|
|
||||||
static volatile int64_t sum;
|
|
||||||
static pthread_mutex_t lock;
|
|
||||||
|
|
||||||
static void *atomic_thread_func(void *arg)
|
|
||||||
{
|
|
||||||
int k;
|
|
||||||
for (k=1; k<=LOOP_COUNT; k++) {
|
|
||||||
//__sync_synchronize();
|
|
||||||
//barrier();
|
|
||||||
__sync_add_and_fetch(&sum, k);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *mutex_thread_func(void *arg)
|
|
||||||
{
|
|
||||||
int k;
|
|
||||||
for (k=1; k<=LOOP_COUNT; k++) {
|
|
||||||
pthread_mutex_lock(&lock);
|
|
||||||
sum += k;
|
|
||||||
pthread_mutex_unlock(&lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef void *(*thread_func)(void *arg);
|
|
||||||
|
|
||||||
static int test(const char *caption, thread_func thread_run)
|
|
||||||
{
|
|
||||||
int64_t start_time;
|
|
||||||
char time_buff[32];
|
|
||||||
pthread_t tids[THREAD_COUNT];
|
|
||||||
int i;
|
|
||||||
int result;
|
|
||||||
|
|
||||||
start_time = get_current_time_ms();
|
|
||||||
sum = 0;
|
|
||||||
|
|
||||||
for (i=0; i<THREAD_COUNT; i++) {
|
|
||||||
if ((result=pthread_create(tids + i, NULL, thread_run, NULL)) != 0) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i=0; i<THREAD_COUNT; i++) {
|
|
||||||
pthread_join(tids[i], NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%s add, LOOP_COUNT: %s, sum: %"PRId64", time used: "
|
|
||||||
"%"PRId64" ms\n", caption, int_to_comma_str(LOOP_COUNT, time_buff),
|
|
||||||
sum, get_current_time_ms() - start_time);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
int k;
|
||||||
|
int64_t sum;
|
||||||
|
int64_t start_time;
|
||||||
|
char time_buff[32];
|
||||||
|
pthread_mutex_t lock;
|
||||||
|
|
||||||
log_init();
|
log_init();
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
@ -101,13 +49,33 @@ int main(int argc, char *argv[])
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
start_time = get_current_time_ms();
|
||||||
|
sum = 0;
|
||||||
|
for (k=1; k<=LOOP_COUNT; k++) {
|
||||||
|
//__sync_synchronize();
|
||||||
|
//barrier();
|
||||||
|
__sync_add_and_fetch(&sum, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("atom add, LOOP_COUNT: %s, sum: %"PRId64", time used: "
|
||||||
|
"%"PRId64" ms\n", int_to_comma_str(LOOP_COUNT, time_buff),
|
||||||
|
sum, get_current_time_ms() - start_time);
|
||||||
|
|
||||||
printf("lock 1: %d\n", pthread_mutex_lock(&lock));
|
printf("lock 1: %d\n", pthread_mutex_lock(&lock));
|
||||||
printf("lock 2: %d\n", pthread_mutex_lock(&lock));
|
printf("lock 2: %d\n", pthread_mutex_lock(&lock));
|
||||||
printf("unlock 1: %d\n", pthread_mutex_unlock(&lock));
|
printf("unlock 1: %d\n", pthread_mutex_unlock(&lock));
|
||||||
printf("unlock 2: %d\n", pthread_mutex_unlock(&lock));
|
printf("unlock 2: %d\n", pthread_mutex_unlock(&lock));
|
||||||
|
|
||||||
test("atom", atomic_thread_func);
|
start_time = get_current_time_ms();
|
||||||
test("lock", mutex_thread_func);
|
sum = 0;
|
||||||
|
for (k=1; k<=LOOP_COUNT; k++) {
|
||||||
|
pthread_mutex_lock(&lock);
|
||||||
|
sum += k;
|
||||||
|
pthread_mutex_unlock(&lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("locked add, LOOP_COUNT: %s, sum: %"PRId64", time used: "
|
||||||
|
"%"PRId64" ms\n", int_to_comma_str(LOOP_COUNT, time_buff),
|
||||||
|
sum, get_current_time_ms() - start_time);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue