From nobody Sun Nov 24 10:25:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1724781506944329.4760215364769; Tue, 27 Aug 2024 10:58:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sj0Q5-0005ns-EN; Tue, 27 Aug 2024 13:55:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sj0Px-0005FF-20 for qemu-devel@nongnu.org; Tue, 27 Aug 2024 13:55:45 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sj0Pl-0001Zc-3H for qemu-devel@nongnu.org; Tue, 27 Aug 2024 13:55:44 -0400 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sj0PX-0002MW-Nt; Tue, 27 Aug 2024 19:55:19 +0200 From: "Maciej S. Szmigiero" To: Peter Xu , Fabiano Rosas Cc: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Eric Blake , Markus Armbruster , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Avihai Horon , Joao Martins , qemu-devel@nongnu.org Subject: [PATCH v2 05/17] thread-pool: Implement non-AIO (generic) pool support Date: Tue, 27 Aug 2024 19:54:24 +0200 Message-ID: <54947c3a1df713f5b69d8296938f3da41116ffe0.1724701542.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=37.28.154.113; envelope-from=mail@maciej.szmigiero.name; helo=vps-vb.mhejs.net X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1724781507217116600 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" Migration code wants to manage device data sending threads in one place. QEMU has an existing thread pool implementation, however it was limited to queuing AIO operations only and essentially had a 1:1 mapping between the current AioContext and the ThreadPool in use. Implement what is necessary to queue generic (non-AIO) work on a ThreadPool too. This brings a few new operations on a pool: * thread_pool_set_minmax_threads() explicitly sets the minimum and maximum thread count in the pool. * thread_pool_join() operation waits until all the submitted work requests have finished. * thread_pool_poll() lets the new thread and / or thread completion bottom halves run (if they are indeed scheduled to be run). It is useful for thread pool users that need to launch or terminate new threads without returning to the QEMU main loop. Signed-off-by: Maciej S. Szmigiero --- include/block/thread-pool.h | 10 ++++- tests/unit/test-thread-pool.c | 2 +- util/thread-pool.c | 77 ++++++++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/include/block/thread-pool.h b/include/block/thread-pool.h index b484c4780ea6..1769496056cd 100644 --- a/include/block/thread-pool.h +++ b/include/block/thread-pool.h @@ -37,9 +37,15 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, void *arg, GDestroyNotify arg_destroy, BlockCompletionFunc *cb, void *opaque); int coroutine_fn thread_pool_submit_co(ThreadPoolFunc *func, void *arg); -void thread_pool_submit(ThreadPoolFunc *func, - void *arg, GDestroyNotify arg_destroy); +BlockAIOCB *thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, + void *arg, GDestroyNotify arg_destroy, + BlockCompletionFunc *cb, void *opaque); =20 +void thread_pool_join(ThreadPool *pool); +void thread_pool_poll(ThreadPool *pool); + +void thread_pool_set_minmax_threads(ThreadPool *pool, + int min_threads, int max_threads); void thread_pool_update_params(ThreadPool *pool, struct AioContext *ctx); =20 #endif diff --git a/tests/unit/test-thread-pool.c b/tests/unit/test-thread-pool.c index e4afb9e36292..469c0f7057b6 100644 --- a/tests/unit/test-thread-pool.c +++ b/tests/unit/test-thread-pool.c @@ -46,7 +46,7 @@ static void done_cb(void *opaque, int ret) static void test_submit(void) { WorkerTestData data =3D { .n =3D 0 }; - thread_pool_submit(worker_cb, &data, NULL); + thread_pool_submit(NULL, worker_cb, &data, NULL, NULL, NULL); while (data.n =3D=3D 0) { aio_poll(ctx, true); } diff --git a/util/thread-pool.c b/util/thread-pool.c index 69a87ee79252..2bf3be875a51 100644 --- a/util/thread-pool.c +++ b/util/thread-pool.c @@ -60,6 +60,7 @@ struct ThreadPool { QemuMutex lock; QemuCond worker_stopped; QemuCond request_cond; + QemuCond no_requests_cond; QEMUBH *new_thread_bh; =20 /* The following variables are only accessed from one AioContext. */ @@ -73,6 +74,7 @@ struct ThreadPool { int pending_threads; /* threads created but not running yet */ int min_threads; int max_threads; + size_t requests_executing; }; =20 static void *worker_thread(void *opaque) @@ -107,6 +109,10 @@ static void *worker_thread(void *opaque) req =3D QTAILQ_FIRST(&pool->request_list); QTAILQ_REMOVE(&pool->request_list, req, reqs); req->state =3D THREAD_ACTIVE; + + assert(pool->requests_executing < SIZE_MAX); + pool->requests_executing++; + qemu_mutex_unlock(&pool->lock); =20 ret =3D req->func(req->arg); @@ -118,6 +124,14 @@ static void *worker_thread(void *opaque) =20 qemu_bh_schedule(pool->completion_bh); qemu_mutex_lock(&pool->lock); + + assert(pool->requests_executing > 0); + pool->requests_executing--; + + if (pool->requests_executing =3D=3D 0 && + QTAILQ_EMPTY(&pool->request_list)) { + qemu_cond_signal(&pool->no_requests_cond); + } } =20 pool->cur_threads--; @@ -243,13 +257,16 @@ static const AIOCBInfo thread_pool_aiocb_info =3D { .cancel_async =3D thread_pool_cancel, }; =20 -BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, - void *arg, GDestroyNotify arg_destroy, - BlockCompletionFunc *cb, void *opaque) +BlockAIOCB *thread_pool_submit(ThreadPool *pool, ThreadPoolFunc *func, + void *arg, GDestroyNotify arg_destroy, + BlockCompletionFunc *cb, void *opaque) { ThreadPoolElement *req; AioContext *ctx =3D qemu_get_current_aio_context(); - ThreadPool *pool =3D aio_get_thread_pool(ctx); + + if (!pool) { + pool =3D aio_get_thread_pool(ctx); + } =20 /* Assert that the thread submitting work is the same running the pool= */ assert(pool->ctx =3D=3D qemu_get_current_aio_context()); @@ -275,6 +292,18 @@ BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *fun= c, return &req->common; } =20 +BlockAIOCB *thread_pool_submit_aio(ThreadPoolFunc *func, + void *arg, GDestroyNotify arg_destroy, + BlockCompletionFunc *cb, void *opaque) +{ + return thread_pool_submit(NULL, func, arg, arg_destroy, cb, opaque); +} + +void thread_pool_poll(ThreadPool *pool) +{ + aio_bh_poll(pool->ctx); +} + typedef struct ThreadPoolCo { Coroutine *co; int ret; @@ -297,18 +326,38 @@ int coroutine_fn thread_pool_submit_co(ThreadPoolFunc= *func, void *arg) return tpc.ret; } =20 -void thread_pool_submit(ThreadPoolFunc *func, - void *arg, GDestroyNotify arg_destroy) +void thread_pool_join(ThreadPool *pool) { - thread_pool_submit_aio(func, arg, arg_destroy, NULL, NULL); + /* Assert that the thread waiting is the same running the pool */ + assert(pool->ctx =3D=3D qemu_get_current_aio_context()); + + qemu_mutex_lock(&pool->lock); + + if (pool->requests_executing > 0 || + !QTAILQ_EMPTY(&pool->request_list)) { + qemu_cond_wait(&pool->no_requests_cond, &pool->lock); + } + assert(pool->requests_executing =3D=3D 0 && + QTAILQ_EMPTY(&pool->request_list)); + + qemu_mutex_unlock(&pool->lock); + + aio_bh_poll(pool->ctx); + + assert(QLIST_EMPTY(&pool->head)); } =20 -void thread_pool_update_params(ThreadPool *pool, AioContext *ctx) +void thread_pool_set_minmax_threads(ThreadPool *pool, + int min_threads, int max_threads) { + assert(min_threads >=3D 0); + assert(max_threads > 0); + assert(max_threads >=3D min_threads); + qemu_mutex_lock(&pool->lock); =20 - pool->min_threads =3D ctx->thread_pool_min; - pool->max_threads =3D ctx->thread_pool_max; + pool->min_threads =3D min_threads; + pool->max_threads =3D max_threads; =20 /* * We either have to: @@ -330,6 +379,12 @@ void thread_pool_update_params(ThreadPool *pool, AioCo= ntext *ctx) qemu_mutex_unlock(&pool->lock); } =20 +void thread_pool_update_params(ThreadPool *pool, AioContext *ctx) +{ + thread_pool_set_minmax_threads(pool, + ctx->thread_pool_min, ctx->thread_pool_= max); +} + static void thread_pool_init_one(ThreadPool *pool, AioContext *ctx) { if (!ctx) { @@ -342,6 +397,7 @@ static void thread_pool_init_one(ThreadPool *pool, AioC= ontext *ctx) qemu_mutex_init(&pool->lock); qemu_cond_init(&pool->worker_stopped); qemu_cond_init(&pool->request_cond); + qemu_cond_init(&pool->no_requests_cond); pool->new_thread_bh =3D aio_bh_new(ctx, spawn_thread_bh_fn, pool); =20 QLIST_INIT(&pool->head); @@ -382,6 +438,7 @@ void thread_pool_free(ThreadPool *pool) qemu_mutex_unlock(&pool->lock); =20 qemu_bh_delete(pool->completion_bh); + qemu_cond_destroy(&pool->no_requests_cond); qemu_cond_destroy(&pool->request_cond); qemu_cond_destroy(&pool->worker_stopped); qemu_mutex_destroy(&pool->lock);