diff --git a/include/spdk/thread.h b/include/spdk/thread.h index 0626c5773..1b0d3fde5 100644 --- a/include/spdk/thread.h +++ b/include/spdk/thread.h @@ -511,6 +511,33 @@ struct spdk_poller *spdk_poller_register(spdk_poller_fn fn, void *arg, uint64_t period_microseconds); +/** + * Register a poller on the current thread with arbitrary name. + * + * The poller can be unregistered by calling spdk_poller_unregister(). + * + * \param fn This function will be called every `period_microseconds`. + * \param arg Argument passed to fn. + * \param period_microseconds How often to call `fn`. If 0, call `fn` as often + * as possible. + * \param name Human readable name for the poller. Pointer of the poller function + * name is set if NULL. + * + * \return a pointer to the poller registered on the current thread on success + * or NULL on failure. + */ +struct spdk_poller *spdk_poller_register_named(spdk_poller_fn fn, + void *arg, + uint64_t period_microseconds, + const char *name); + +/* + * \brief Register a poller on the current thread with setting its name + * to the string of the poller function name. + */ +#define SPDK_POLLER_REGISTER(fn, arg, period_microseconds) \ + spdk_poller_register_named(fn, arg, period_microseconds, #fn) + /** * Unregister a poller on the current thread. * diff --git a/include/spdk_internal/thread.h b/include/spdk_internal/thread.h index 93fc918c8..99dc03525 100644 --- a/include/spdk_internal/thread.h +++ b/include/spdk_internal/thread.h @@ -37,6 +37,7 @@ #include "spdk/stdinc.h" #include "spdk/thread.h" +#define SPDK_MAX_POLLER_NAME_LEN 256 #define SPDK_MAX_THREAD_NAME_LEN 256 enum spdk_poller_state { @@ -71,6 +72,8 @@ struct spdk_poller { spdk_poller_fn fn; void *arg; struct spdk_thread *thread; + + char name[SPDK_MAX_POLLER_NAME_LEN + 1]; }; struct spdk_thread { diff --git a/lib/thread/thread.c b/lib/thread/thread.c index e82fefb49..5ef3858a1 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -182,8 +182,8 @@ _free_thread(struct spdk_thread *thread) TAILQ_FOREACH_SAFE(poller, &thread->active_pollers, tailq, ptmp) { if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) { - SPDK_WARNLOG("poller %p still registered at thread exit\n", - poller); + SPDK_WARNLOG("poller %s still registered at thread exit\n", + poller->name); } TAILQ_REMOVE(&thread->active_pollers, poller, tailq); free(poller); @@ -191,15 +191,15 @@ _free_thread(struct spdk_thread *thread) TAILQ_FOREACH_SAFE(poller, &thread->timed_pollers, tailq, ptmp) { if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) { - SPDK_WARNLOG("poller %p still registered at thread exit\n", - poller); + SPDK_WARNLOG("poller %s still registered at thread exit\n", + poller->name); } TAILQ_REMOVE(&thread->timed_pollers, poller, tailq); free(poller); } TAILQ_FOREACH_SAFE(poller, &thread->paused_pollers, tailq, ptmp) { - SPDK_WARNLOG("poller %p still registered at thread exit\n", poller); + SPDK_WARNLOG("poller %s still registered at thread exit\n", poller->name); TAILQ_REMOVE(&thread->paused_pollers, poller, tailq); free(poller); } @@ -332,23 +332,23 @@ spdk_thread_exit(struct spdk_thread *thread) TAILQ_FOREACH(poller, &thread->active_pollers, tailq) { if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) { - SPDK_ERRLOG("thread %s still has active poller %p\n", - thread->name, poller); + SPDK_ERRLOG("thread %s still has active poller %s\n", + thread->name, poller->name); return -EBUSY; } } TAILQ_FOREACH(poller, &thread->timed_pollers, tailq) { if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) { - SPDK_ERRLOG("thread %s still has active timed poller %p\n", - thread->name, poller); + SPDK_ERRLOG("thread %s still has active timed poller %s\n", + thread->name, poller->name); return -EBUSY; } } TAILQ_FOREACH(poller, &thread->paused_pollers, tailq) { - SPDK_ERRLOG("thread %s still has paused poller %p\n", - thread->name, poller); + SPDK_ERRLOG("thread %s still has paused poller %s\n", + thread->name, poller->name); return -EBUSY; } @@ -566,7 +566,7 @@ spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now) #ifdef DEBUG if (poller_rc == -1) { - SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Poller %p returned -1\n", poller); + SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Poller %s returned -1\n", poller->name); } #endif @@ -605,7 +605,7 @@ spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now) #ifdef DEBUG if (timer_rc == -1) { - SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Timed poller %p returned -1\n", poller); + SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Timed poller %s returned -1\n", poller->name); } #endif @@ -817,10 +817,11 @@ spdk_thread_send_critical_msg(struct spdk_thread *thread, spdk_msg_fn fn) return -EIO; } -struct spdk_poller * -spdk_poller_register(spdk_poller_fn fn, - void *arg, - uint64_t period_microseconds) +static struct spdk_poller * +_spdk_poller_register(spdk_poller_fn fn, + void *arg, + uint64_t period_microseconds, + const char *name) { struct spdk_thread *thread; struct spdk_poller *poller; @@ -843,6 +844,12 @@ spdk_poller_register(spdk_poller_fn fn, return NULL; } + if (name) { + snprintf(poller->name, sizeof(poller->name), "%s", name); + } else { + snprintf(poller->name, sizeof(poller->name), "%p", fn); + } + poller->state = SPDK_POLLER_STATE_WAITING; poller->fn = fn; poller->arg = arg; @@ -863,6 +870,23 @@ spdk_poller_register(spdk_poller_fn fn, return poller; } +struct spdk_poller * +spdk_poller_register(spdk_poller_fn fn, + void *arg, + uint64_t period_microseconds) +{ + return _spdk_poller_register(fn, arg, period_microseconds, NULL); +} + +struct spdk_poller * +spdk_poller_register_named(spdk_poller_fn fn, + void *arg, + uint64_t period_microseconds, + const char *name) +{ + return _spdk_poller_register(fn, arg, period_microseconds, name); +} + void spdk_poller_unregister(struct spdk_poller **ppoller) {