diff --git a/lib_fiber/c/Makefile b/lib_fiber/c/Makefile index 30574a1e6..dfa05f931 100644 --- a/lib_fiber/c/Makefile +++ b/lib_fiber/c/Makefile @@ -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 \ diff --git a/lib_fiber/c/src/common/timer_cache.c b/lib_fiber/c/src/common/timer_cache.c index 9fcd5c184..07ce2ff2d 100644 --- a/lib_fiber/c/src/common/timer_cache.c +++ b/lib_fiber/c/src/common/timer_cache.c @@ -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); } diff --git a/lib_fiber/c/src/common/timer_cache.h b/lib_fiber/c/src/common/timer_cache.h index bb9ee6cbe..2dc676d8b 100644 --- a/lib_fiber/c/src/common/timer_cache.h +++ b/lib_fiber/c/src/common/timer_cache.h @@ -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); diff --git a/lib_fiber/c/src/event.h b/lib_fiber/c/src/event.h index b6a0a7a21..a2f3f9407 100644 --- a/lib_fiber/c/src/event.h +++ b/lib_fiber/c/src/event.h @@ -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; diff --git a/lib_fiber/c/src/fiber.h b/lib_fiber/c/src/fiber.h index 83f1c5446..1b665890c 100644 --- a/lib_fiber/c/src/fiber.h +++ b/lib_fiber/c/src/fiber.h @@ -85,8 +85,6 @@ struct ACL_FIBER { FIBER_LOCAL **locals; int nlocal; -#define DEBUG_READY - #ifdef DEBUG_READY int cline; int lline; diff --git a/lib_fiber/c/src/fiber_io.c b/lib_fiber/c/src/fiber_io.c index f849bdfca..6d70c037f 100644 --- a/lib_fiber/c/src/fiber_io.c +++ b/lib_fiber/c/src/fiber_io.c @@ -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; diff --git a/lib_fiber/c/src/hook/poll.c b/lib_fiber/c/src/hook/poll.c index dcabdaeb9..ca4f33716 100644 --- a/lib_fiber/c/src/hook/poll.c +++ b/lib_fiber/c/src/hook/poll.c @@ -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);