Thread pool synchronization for replay
RFC PATCH 11/22 Thread pool synchronization for replay
Making thread pool deterministic. All thread work is done in synchronous mode by executing the worker and callback functions when "checkpoint" in code is passed or instruction is executed. These events are written to the log attached to this instruction or "checkpoint".
Signed-off-by Pavel Dovgalyuk <[email protected]>
---
include/block/thread-pool.h +59 -1
@@ -25,17 +25,75 @@#include "block/block_int.h" typedef int ThreadPoolFunc (void *opaque); +typedef struct ThreadPoolElement ThreadPoolElement ; + +enum ThreadState { +THREAD_QUEUED , +THREAD_ACTIVE , +THREAD_DONE , +THREAD_CANCELED , +}; + +struct ThreadPool { +EventNotifier notifier; +AioContext *ctx; +QemuMutex lock; +QemuCond check_cancel; +QemuCond worker_stopped; +QemuSemaphore sem; +int max_threads; +QEMUBH *new_thread_bh; + +/* The following variables are only accessed from one AioContext. */ + QLIST_HEAD(, ThreadPoolElement) head; + +/* The following variables are protected by lock. */ + QTAILQ_HEAD(, ThreadPoolElement) request_list; + +/* The following tasks are executed by replay */ + QTAILQ_HEAD(, ThreadPoolElement) replay_list; +int cur_threads; +int idle_threads; +int new_threads;/* backlog of threads we need to create */ +int pending_threads;/* threads created but not running yet */ +int pending_cancellations;/* whether we need a cond_broadcast */ +bool stopping; +};typedef struct ThreadPool ThreadPool ; +struct ThreadPoolElement { +BlockDriverAIOCB common; +ThreadPool *pool; +ThreadPoolFunc *func; +void *arg; + +/* Moving state out of THREAD_QUEUED is protected by lock. After +* that, only the worker thread can write to it. Reads and writes +* of state and ret are ordered with memory barriers. +*/ +enum ThreadState state; +int ret; + +/* Access to this list is protected by lock. */ +QTAILQ_ENTRY (ThreadPoolElement) reqs; + +/* Access to this list is protected by the global mutex. */ +QLIST_ENTRY (ThreadPoolElement) all; +}; + + +ThreadPool *thread_pool_new (struct AioContext *ctx);void thread_pool_free (ThreadPool *pool);BlockDriverAIOCB *thread_pool_submit_aio (ThreadPool *pool,ThreadPoolFunc *func,void *arg, -BlockDriverCompletionFunc *cb,void *opaque); +BlockDriverCompletionFunc *cb,void *opaque, +bool replay,uint64_t replay_step); int coroutine_fn thread_pool_submit_co(ThreadPool *pool, ThreadPoolFunc *func, void *arg);void thread_pool_submit (ThreadPool *pool,ThreadPoolFunc *func,void *arg); +void thread_pool_work (struct ThreadPool *pool,struct ThreadPoolElement *req);#endif
tests/test-thread-pool.c +3 -3
@@ -55,7 +55,7 @@ static void test_submit_aio(void) {WorkerTestData data = { .n =0 , .ret = -EINPROGRESS }; data.aiocb =thread_pool_submit_aio (pool, worker_cb, &data, - done_cb, &data); + done_cb, &data, false,0 );/* The callbacks are not called until after the first wait. */ active =1 ;
@@ -119,7 +119,7 @@ static void test_submit_many(void)for (i =0 ; i <100 ; i++) { data[i].n =0 ; data[i].ret = -EINPROGRESS ; -thread_pool_submit_aio (pool, worker_cb, &data[i], done_cb, &data[i]); +thread_pool_submit_aio (pool, worker_cb, &data[i], done_cb, &data[i], false,0 ); } active =100 ;
@@ -148,7 +148,7 @@ static void test_cancel(void) data[i].n =0 ; data[i].ret = -EINPROGRESS ; data[i].aiocb =thread_pool_submit_aio (pool, long_cb, &data[i], - done_cb, &data[i]); + done_cb, &data[i], false,0 ); }/* Starting the threads may be left to a bottom half. Let it
thread-pool.c +36 -65
158 lines not on this page · raw message