From 54215a1e9bdf501616f8c9fea59f66744c26d337 Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Mon, 26 Apr 2021 19:17:23 +0900 Subject: [PATCH] thread: Factor out executing poller or timed poller into helper functions Signed-off-by: Shuhei Matsumoto Change-Id: I8792b698f5de85c24482d6781fc3d47afce0c414 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/7663 Community-CI: Mellanox Build Bot Reviewed-by: Ben Walker Reviewed-by: Jim Harris Reviewed-by: Konrad Sztyber Reviewed-by: Aleksey Marchuk Tested-by: SPDK CI Jenkins --- lib/thread/thread.c | 151 +++++++++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 66 deletions(-) diff --git a/lib/thread/thread.c b/lib/thread/thread.c index d91cffa84e..ea8fe9c607 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -603,6 +603,89 @@ thread_update_stats(struct spdk_thread *thread, uint64_t end, thread->tsc_last = end; } +static inline int +thread_execute_poller(struct spdk_thread *thread, struct spdk_poller *poller) +{ + int rc; + + if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { + TAILQ_REMOVE(&thread->active_pollers, poller, tailq); + free(poller); + return 0; + } else if (poller->state == SPDK_POLLER_STATE_PAUSING) { + TAILQ_REMOVE(&thread->active_pollers, poller, tailq); + TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq); + poller->state = SPDK_POLLER_STATE_PAUSED; + return 0; + } + + poller->state = SPDK_POLLER_STATE_RUNNING; + rc = poller->fn(poller->arg); + + poller->run_count++; + if (rc > 0) { + poller->busy_count++; + } + +#ifdef DEBUG + if (rc == -1) { + SPDK_DEBUGLOG(thread, "Poller %s returned -1\n", poller->name); + } +#endif + + if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { + TAILQ_REMOVE(&thread->active_pollers, poller, tailq); + free(poller); + } else if (poller->state != SPDK_POLLER_STATE_PAUSED) { + poller->state = SPDK_POLLER_STATE_WAITING; + } + + return rc; +} + +static inline int +thread_execute_timed_poller(struct spdk_thread *thread, struct spdk_poller *poller, + uint64_t now) +{ + int rc; + + if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { + TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); + free(poller); + return 0; + } else if (poller->state == SPDK_POLLER_STATE_PAUSING) { + TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); + TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq); + poller->state = SPDK_POLLER_STATE_PAUSED; + return 0; + } + + poller->state = SPDK_POLLER_STATE_RUNNING; + rc = poller->fn(poller->arg); + + poller->run_count++; + if (rc > 0) { + poller->busy_count++; + } + +#ifdef DEBUG + if (rc == -1) { + SPDK_DEBUGLOG(thread, "Timed poller %s returned -1\n", poller->name); + } +#endif + + if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { + TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); + free(poller); + } else if (poller->state != SPDK_POLLER_STATE_PAUSED) { + poller->state = SPDK_POLLER_STATE_WAITING; + TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); + poller_insert_timer(thread, poller, now); + } + + return rc; +} + static int thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now) { @@ -629,38 +712,7 @@ thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now) active_pollers_head, tailq, tmp) { int poller_rc; - if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { - TAILQ_REMOVE(&thread->active_pollers, poller, tailq); - free(poller); - continue; - } else if (poller->state == SPDK_POLLER_STATE_PAUSING) { - TAILQ_REMOVE(&thread->active_pollers, poller, tailq); - TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq); - poller->state = SPDK_POLLER_STATE_PAUSED; - continue; - } - - poller->state = SPDK_POLLER_STATE_RUNNING; - poller_rc = poller->fn(poller->arg); - - poller->run_count++; - if (poller_rc > 0) { - poller->busy_count++; - } - -#ifdef DEBUG - if (poller_rc == -1) { - SPDK_DEBUGLOG(thread, "Poller %s returned -1\n", poller->name); - } -#endif - - if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { - TAILQ_REMOVE(&thread->active_pollers, poller, tailq); - free(poller); - } else if (poller->state != SPDK_POLLER_STATE_PAUSED) { - poller->state = SPDK_POLLER_STATE_WAITING; - } - + poller_rc = thread_execute_poller(thread, poller); if (poller_rc > rc) { rc = poller_rc; } @@ -673,40 +725,7 @@ thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now) break; } - if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { - TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); - free(poller); - continue; - } else if (poller->state == SPDK_POLLER_STATE_PAUSING) { - TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); - TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq); - poller->state = SPDK_POLLER_STATE_PAUSED; - continue; - } - - poller->state = SPDK_POLLER_STATE_RUNNING; - timer_rc = poller->fn(poller->arg); - - poller->run_count++; - if (timer_rc > 0) { - poller->busy_count++; - } - -#ifdef DEBUG - if (timer_rc == -1) { - SPDK_DEBUGLOG(thread, "Timed poller %s returned -1\n", poller->name); - } -#endif - - if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { - TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); - free(poller); - } else if (poller->state != SPDK_POLLER_STATE_PAUSED) { - poller->state = SPDK_POLLER_STATE_WAITING; - TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); - poller_insert_timer(thread, poller, now); - } - + timer_rc = thread_execute_timed_poller(thread, poller, now); if (timer_rc > rc) { rc = timer_rc; }