diff --git a/include/spdk/thread.h b/include/spdk/thread.h index b0520ac05..457df8bde 100644 --- a/include/spdk/thread.h +++ b/include/spdk/thread.h @@ -191,6 +191,11 @@ struct spdk_thread *spdk_allocate_thread(spdk_thread_pass_msg msg_fn, */ void spdk_free_thread(void); +/** + * Get count of allocated threads. + */ +uint32_t spdk_thread_get_count(void); + /** * Get a handle to the current thread. * diff --git a/lib/bdev/bdev.c b/lib/bdev/bdev.c index 642daa780..a35330aeb 100644 --- a/lib/bdev/bdev.c +++ b/lib/bdev/bdev.c @@ -635,10 +635,10 @@ spdk_bdev_initialize(spdk_bdev_init_cb cb_fn, void *cb_arg) /** * Ensure no more than half of the total buffers end up local caches, by - * using spdk_env_get_core_count() to determine how many local caches we need + * using spdk_thread_get_count() to determine how many local caches we need * to account for. */ - cache_size = BUF_SMALL_POOL_SIZE / (2 * spdk_env_get_core_count()); + cache_size = BUF_SMALL_POOL_SIZE / (2 * spdk_thread_get_count()); snprintf(mempool_name, sizeof(mempool_name), "buf_small_pool_%d", getpid()); g_bdev_mgr.buf_small_pool = spdk_mempool_create(mempool_name, @@ -652,7 +652,7 @@ spdk_bdev_initialize(spdk_bdev_init_cb cb_fn, void *cb_arg) return; } - cache_size = BUF_LARGE_POOL_SIZE / (2 * spdk_env_get_core_count()); + cache_size = BUF_LARGE_POOL_SIZE / (2 * spdk_thread_get_count()); snprintf(mempool_name, sizeof(mempool_name), "buf_large_pool_%d", getpid()); g_bdev_mgr.buf_large_pool = spdk_mempool_create(mempool_name, diff --git a/lib/thread/thread.c b/lib/thread/thread.c index 713737c0c..c981ab3d2 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -75,6 +75,7 @@ struct spdk_thread { }; static TAILQ_HEAD(, spdk_thread) g_threads = TAILQ_HEAD_INITIALIZER(g_threads); +static uint32_t g_thread_count = 0; static struct spdk_thread * _get_thread(void) @@ -137,6 +138,7 @@ spdk_allocate_thread(spdk_thread_pass_msg msg_fn, thread->thread_ctx = thread_ctx; TAILQ_INIT(&thread->io_channels); TAILQ_INSERT_TAIL(&g_threads, thread, tailq); + g_thread_count++; if (name) { _set_thread_name(name); thread->name = strdup(name); @@ -161,6 +163,8 @@ spdk_free_thread(void) return; } + assert(g_thread_count > 0); + g_thread_count--; TAILQ_REMOVE(&g_threads, thread, tailq); free(thread->name); free(thread); @@ -168,6 +172,17 @@ spdk_free_thread(void) pthread_mutex_unlock(&g_devlist_mutex); } +uint32_t +spdk_thread_get_count(void) +{ + /* + * Return cached value of the current thread count. We could acquire the + * lock and iterate through the TAILQ of threads to count them, but that + * count could still be invalidated after we release the lock. + */ + return g_thread_count; +} + struct spdk_thread * spdk_get_thread(void) { diff --git a/test/common/lib/test_env.c b/test/common/lib/test_env.c index e0fcf828c..bc21c7178 100644 --- a/test/common/lib/test_env.c +++ b/test/common/lib/test_env.c @@ -316,9 +316,3 @@ spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr return 0; } - -uint32_t -spdk_env_get_core_count(void) -{ - return 1; -}