Optimize and test IO timeout by setsockopt in fiber.

This commit is contained in:
zhengshuxin 2024-07-26 11:00:21 +08:00
parent bd9e5c0d52
commit a4b4bc0cc6
7 changed files with 38 additions and 11 deletions

View File

@ -27,6 +27,7 @@ CFLAGS = -c -g -W \
-DUSE_FAST_RING \
-O3 \
-DUSE_CLOCK_GETTIME \
-DDEBUG_READY \
#-DUSE_VALGRIND \
#-DUSE_INLINE_MEMCPY\
#-DUSE_FAST_TIME \

View File

@ -41,6 +41,7 @@ TIMER_CACHE *timer_cache_create(void)
ring_init(&cache->caches);
cache->cache_max = 1000;
cache->objs = array_create(100, ARRAY_F_UNORDER);
cache->objs2 = array_create(100, ARRAY_F_UNORDER);
return cache;
}
@ -69,6 +70,7 @@ void timer_cache_free(TIMER_CACHE *cache)
}
array_free(cache->objs, NULL);
array_free(cache->objs2, NULL);
mem_free(cache);
}

View File

@ -23,6 +23,7 @@ struct TIMER_CACHE {
RING caches; // Caching the TIMER_CACHE_NODE memory
int cache_max;
ARRAY *objs; // Holding any object temporarily.
ARRAY *objs2; // Holding any object temporarily.
};
TIMER_CACHE *timer_cache_create(void);

View File

@ -99,10 +99,24 @@ typedef struct IO_URING_CTX {
/**
* for each connection fd
*/
#define PIN_FILE(f) { \
SET_TIME((f)->stamp); \
(f)->line = __LINE__; \
SAFE_STRNCPY((f)->tag, __FUNCTION__, sizeof((f)->tag)); \
}
struct FILE_EVENT {
RING me;
ACL_FIBER *fiber_r;
ACL_FIBER *fiber_w;
#ifdef DEBUG_READY
long long stamp;
char tag[32];
int line;
#endif
socket_t fd;
int id;
unsigned status;

View File

@ -85,8 +85,6 @@ struct ACL_FIBER {
FIBER_LOCAL **locals;
int nlocal;
#define DEBUG_READY
#ifdef DEBUG_READY
int cline;
int lline;

View File

@ -231,10 +231,15 @@ static void wakeup_timers(TIMER_CACHE *timers, long long now)
FIBER_READY(fb);
}
array_append(timers->objs2, node);
next = TIMER_NEXT(timers, node);
timer_cache_free_node(timers, node);
node = next;
}
while ((node = (TIMER_CACHE_NODE*) array_pop_back(timers->objs2))) {
timer_cache_free_node(timers, node);
}
}
static void fiber_io_loop(ACL_FIBER *self fiber_unused, void *ctx)
@ -508,9 +513,13 @@ int fiber_wait_read(FILE_EVENT *fe)
#endif
}
fe->fiber_r->wstatus &= ~FIBER_WAIT_READ;
curr->wstatus &= ~FIBER_WAIT_READ;
fe->fiber_r = NULL;
#ifdef DEBUG_READY
PIN_FILE(fe);
#endif
if (!(fe->type & TYPE_INTERNAL)) {
WAITER_DEC(__thread_fiber->event);
}
@ -521,7 +530,6 @@ int fiber_wait_read(FILE_EVENT *fe)
// fiber's wakeup process wasn't from read_callback normally.
event_del_read(__thread_fiber->event, fe, 1);
acl_fiber_set_error(curr->errnum);
fiber_timer_del(curr);
return -1;
} else if (curr->flag & FIBER_F_TIMER) {
// If the IO reading timeout set in setsockopt.
@ -534,8 +542,6 @@ int fiber_wait_read(FILE_EVENT *fe)
acl_fiber_set_errno(curr, FIBER_EAGAIN);
acl_fiber_set_error(FIBER_EAGAIN);
return -1;
} else if ((fe->mask & EVENT_SO_RCVTIMEO) && fe->r_timeout > 0) {
fiber_timer_del(curr);
}
// else: the IO read event should has been removed in read_callback.
@ -589,7 +595,7 @@ int fiber_wait_write(FILE_EVENT *fe)
acl_fiber_switch();
fe->fiber_w->wstatus &= ~FIBER_WAIT_WRITE;
curr->wstatus &= ~FIBER_WAIT_WRITE;
fe->fiber_w = NULL;
if (!(fe->type & TYPE_INTERNAL)) {
@ -599,7 +605,6 @@ int fiber_wait_write(FILE_EVENT *fe)
if (acl_fiber_canceled(curr)) {
event_del_write(__thread_fiber->event, fe, 1);
acl_fiber_set_error(curr->errnum);
fiber_timer_del(curr);
return -1;
} else if (curr->flag & FIBER_F_TIMER) {
curr->flag &= ~FIBER_F_TIMER;
@ -608,8 +613,6 @@ int fiber_wait_write(FILE_EVENT *fe)
acl_fiber_set_errno(curr, FIBER_EAGAIN);
acl_fiber_set_error(FIBER_EAGAIN);
return -1;
} else if ((fe->mask & EVENT_SO_SNDTIMEO) && fe->w_timeout > 0) {
fiber_timer_del(curr);
}
return ret;

View File

@ -42,6 +42,10 @@ static void handle_poll_read(EVENT *ev, FILE_EVENT *fe, POLLFD *pfd)
pfd->fe->fiber_r = NULL;
#ifdef DEBUG_READY
PIN_FILE(pfd->fe);
#endif
if (!(pfd->pfd->events & POLLOUT)) {
ring_detach(&pfd->me);
pfd->fe = NULL;
@ -211,6 +215,10 @@ static void poll_event_clean(EVENT *ev, POLL_EVENT *pe)
#endif
event_del_read(ev, pfd->fe, 0);
pfd->fe->fiber_r = NULL;
#ifdef DEBUG_READY
PIN_FILE(pfd->fe);
#endif
}
if (pfd->pfd->events & POLLOUT) {
CLR_WRITEWAIT(pfd->fe);