qemu: Threadlets: A generic task offloading framework
v4 cover qemu: Threadlets: A generic task offloading framework
Hi,
This is the v4 of the patch-series to have a generic asynchronous task offloading framework (called threadlets) within qemu.
V3 can be found here:
http://lists.gnu.org/archive/html/qemu-devel/2010-06/index.html
Changes from V3:
=====================================================================
- Did away with the qemu-thread wrappers for handling pthread_attr_t type following review comments for V3.
- Added qemu-thread wrappers for pthread_barrier_init() and pthread_barrier_wait().
- Added a flush_threadlet_queue() helper which allows the caller to wait till all the queued tasks have finished processing.
- Added a global queue that can be used by most subsystems to offload tasks. The flexibility to allow individual subsystems to create their private queue with associated thread-pool has been retained.
- Fixed the Copyrights in the newly introduced file to reflect the copyrights of the borrowed code.
- Renamed the helper functions to reflect their use. Added comments for each of the helpers.
- Typedef'd the structs to adhere to the qemu coding style.
Description
=====================================================================
This patch series decouples the asynchronous threading framework implementation from posix-aio-compat.c to implement a generic asynchronous task offloading threading framework called threadlets which can be used by other subsystems within QEMU.
Currently within QEMU, the AIO subsystem (paio) creates a bunch of asynchronous threads to offload any blocking operations so that the vcpu threads and the IO thread can go back to servicing any other guest requests.
This offloading framework can be used by subsystems such as virtio-9p, Asynchronous encoding for vnc-server, so that the vcpu thread can offload blocking operations on to the asynchronous threads and resume servicing any other guest requests. The asynchronous threads, after finishing the blocking operations can then transfer the control over to the IO thread so that the latter can handle the post_blocking_operation().
The patch series passed fsstress test without any issues.
Could it be considered for inclusion ?
---
Aneesh Kumar K.V (1):
qemu: Generic task offloading framework: threadlets
Gautham R Shenoy (2):
qemu: Add qemu-barrier support to qemu-thread framework.
qemu: Convert AIO code to use threadlets.
Makefile.objs | 3 + async-work.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++ async-work.h | 69 +++++++++++++++++++ posix-aio-compat.c | 152 +++++++----------------------------------- qemu-thread.c | 23 ++++++ qemu-thread.h | 9 +++ 6 files changed, 313 insertions(+), 129 deletions(-) create mode 100644 async-work.c create mode 100644 async-work.h
Signature · 2 lines
Thanks and Regards gautham.
PATCH v4 1/3 qemu: Add qemu-barrier support to qemu-thread framework.
--- qemu-thread.c | 23 +++++++++++++++++++++++ qemu-thread.h | 9 +++++++++ 2 files changed, 32 insertions(+), 0 deletions(-)
qemu-thread.c +23 -0
@@ -161,3 +161,26 @@ int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)return pthread_equal (thread1->thread, thread2->thread); } +void qemu_barrier_init (QemuBarrier *barr1,int nr_threads) +{ +int err; + + err =pthread_barrier_init (&barr1->barr,NULL , nr_threads); + +if (err) { +error_exit (err, __func__); + } +} + +int qemu_barrier_wait (QemuBarrier *barr1) +{ +int ret; + + ret =pthread_barrier_wait (&barr1->barr); + +if (ret !=0 && ret !=PTHREAD_BARRIER_SERIAL_THREAD ) { +error_exit (ret, __func__); + } + +return ret; +}
qemu-thread.h +9 -0
@@ -15,9 +15,14 @@ struct QemuThread {pthread_t thread; }; +struct QemuBarrier { +pthread_barrier_t barr; +}; +typedef struct QemuMutex QemuMutex ;typedef struct QemuCond QemuCond ;typedef struct QemuThread QemuThread ; +typedef struct QemuBarrier QemuBarrier ;void qemu_mutex_init (QemuMutex *mutex);void qemu_mutex_lock (QemuMutex *mutex);
@@ -31,10 +36,14 @@ void qemu_cond_broadcast(QemuCond *cond);void qemu_cond_wait (QemuCond *cond,QemuMutex *mutex);int qemu_cond_timedwait (QemuCond *cond,QemuMutex *mutex,uint64_t msecs); +void qemu_barrier_init (QemuBarrier *barr,int nr_threads); +void qemu_thread_create (QemuThread *thread,void *(*start_routine)(void *),void *arg);void qemu_thread_signal (QemuThread *thread,int sig);void qemu_thread_self (QemuThread *thread);int qemu_thread_equal (QemuThread *thread1,QemuThread *thread2); + +int qemu_barrier_wait (QemuBarrier *barr);#endif
[Qemu-devel] Re: [PATCH V4 1/3] qemu: Add qemu-barrier support to qemu-thread framework.
Quoted 2 lines · Gautham R Shenoy
Signed-off-by Gautham R Shenoy<[email protected]>
I don't see barrier being used and I don't think it ought to be used. threadlets are meant provide a means to execute a synchronous function asynchronously. It should never be necessary to synchronize threadlets using a barrier.
Regards,
Anthony Liguori
Quoted 72 lines
--- qemu-thread.c | 23 +++++++++++++++++++++++ qemu-thread.h | 9 +++++++++ 2 files changed, 32 insertions(+), 0 deletions(-)qemu-thread.c +23
-0@@ -161,3 +161,26 @@ int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)return pthread_equal (thread1->thread, thread2->thread); } +void qemu_barrier_init (QemuBarrier *barr1,int nr_threads) +{ +int err; + + err =pthread_barrier_init (&barr1->barr,NULL , nr_threads); + +if (err) { +error_exit (err, __func__); + } +} + +int qemu_barrier_wait (QemuBarrier *barr1) +{ +int ret; + + ret =pthread_barrier_wait (&barr1->barr); + +if (ret !=0 && ret !=PTHREAD_BARRIER_SERIAL_THREAD ) { +error_exit (ret, __func__); + } + +return ret; +}qemu-thread.h +9
-0@@ -15,9 +15,14 @@ struct QemuThread {pthread_t thread; }; +struct QemuBarrier { +pthread_barrier_t barr; +}; +typedef struct QemuMutex QemuMutex ;typedef struct QemuCond QemuCond ;typedef struct QemuThread QemuThread ; +typedef struct QemuBarrier QemuBarrier ;void qemu_mutex_init (QemuMutex *mutex);void qemu_mutex_lock (QemuMutex *mutex);@@ -31,10 +36,14 @@ void qemu_cond_broadcast(QemuCond *cond);void qemu_cond_wait (QemuCond *cond,QemuMutex *mutex);int qemu_cond_timedwait (QemuCond *cond,QemuMutex *mutex,uint64_t msecs); +void qemu_barrier_init (QemuBarrier *barr,int nr_threads); +void qemu_thread_create (QemuThread *thread,void *(*start_routine)(void *),void *arg);void qemu_thread_signal (QemuThread *thread,int sig);void qemu_thread_self (QemuThread *thread);int qemu_thread_equal (QemuThread *thread1,QemuThread *thread2); + +int qemu_barrier_wait (QemuBarrier *barr);#endif
[Qemu-devel] Re: [PATCH V4 1/3] qemu: Add qemu-barrier support to qemu-thread framework.
Quoted 7 lines · Anthony Liguori
Quoted 1 line · Gautham R Shenoy
Signed-off-by Gautham R Shenoy<[email protected]>I don't see barrier being used and I don't think it ought to be used. threadlets are meant provide a means to execute a synchronous function asynchronously. It should never be necessary to synchronize threadlets using a barrier.
It's used for flush_threadlet_queue.
Paolo
PATCH v4 2/3 qemu: Generic task offloading framework: threadlets
In reply to Gautham R ShenoyFrom: Aneesh Kumar K.V <[email protected]>
This patch creates a generic asynchronous-task-offloading infrastructure named threadlets. The core idea has been borrowed from the threading framework that is being used by paio.
The reason for creating this generic infrastructure is so that other subsystems, such as virtio-9p could make use of it for offloading tasks that could block.
The patch creates a global queue on-to which subsystems can queue their tasks to be executed asynchronously.
The patch also provides API's that allow a subsystem to create a private queue. API's that allow a subsystem to wait till all the earlier queued tasks have been executed, is also provided.
[[email protected]: Facelift of the code, cancel_threadlet, flush_threadlet_queue and other minor helpers.]
--- Makefile.objs | 3 + async-work.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ async-work.h | 69 +++++++++++++++++++++ 3 files changed, 257 insertions(+), 1 deletions(-) create mode 100644 async-work.c create mode 100644 async-work.h
Makefile.objs +2 -1
@@ -9,6 +9,8 @@ qobject-obj-y += qerror.o block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-config.o +block-obj-y += qemu-thread.o +block-obj-y += async-work.o block-obj-$(CONFIG_POSIX) += posix-aio-compat.o block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
@@ -109,7 +111,6 @@ common-obj-y += iov.o common-obj-$(CONFIG_VNC_TLS) += vnc-tls.o vnc-auth-vencrypt.o common-obj-$(CONFIG_VNC_SASL) += vnc-auth-sasl.o common-obj-$(CONFIG_COCOA) += cocoa.o -common-obj-$(CONFIG_IOTHREAD) += qemu-thread.o common-obj-y += notify.o event_notifier.o common-obj-y += qemu-timer.o
async-work.c new file +186 -0
186 lines not on this page · raw message
async-work.h new file +69 -0
69 lines not on this page · raw message
[Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Makefile.objs qobject-obj-y += qerror.o · line 13
Quoted 2 lines
Makefile.objs +2
-0+block-obj-y += qemu-thread.o +block-obj-y += async-work.o
These should be (at least for now) block-obj-$(CONFIG_POSIX).
async-work.c line 43
Quoted 5 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }
Using qemu_cond_timedwait is a hack for not properly broadcasting the condvar in flush_threadlet_queue.
async-work.c line 46
Quoted 2 lines
async-work.c new file +2
-0+if (QTAILQ_EMPTY (&(queue->request_list))) + goto check_exit;
What's the reason for the goto? {...} works just as well.
async-work.c line 133
Quoted 13 lines
async-work.c new file +13
-0+/** +* flush_threadlet_queue: Wait till completion of all the submitted tasks +* @queue: Queue containing the tasks we're waiting on. +*/ +void flush_threadlet_queue (ThreadletQueue *queue) +{ +qemu_mutex_lock (&queue->lock); + queue->exit =1 ; + +qemu_barrier_init (&queue->barr, queue->cur_threads +1 ); +qemu_mutex_unlock (&queue->lock); + +qemu_barrier_wait (&queue->barr);
Can be implemented just as well with queue->cond and a loop waiting for queue->cur_threads == 0. This would remove the need to implement barriers in qemu-threads (especially for Win32). Anyway whoever will contribute Win32 qemu-threads can do it, since it's not hard.
async-work.c line 186
Quoted 4 lines
async-work.c new file +4
-0+int cancel_threadlet_common (ThreadletWork *work) +{ +return cancel_threadlet (&globalqueue, work); +}
I would prefer *_threadlet to be the globalqueue function (and flush_threadlets) and queue_*_threadlet to be the special-queue function. I should have spoken earlier probably, but please consider this if there will be a v5.
Quoted 1 line
+ * Generalization based on posix-aio emulation code.
No need to specify these as long as the original authors are attributed properly.
async-work.h line 56
Quoted 13 lines
async-work.h new file +13
-0+static inline void threadlet_queue_init (ThreadletQueue *queue, +int max_threads,int min_threads) +{ + queue->cur_threads =0 ; + queue->idle_threads =0 ; + queue->exit =0 ; + queue->max_threads = max_threads; + queue->min_threads = min_threads; +QTAILQ_INIT (&(queue->request_list)); +QTAILQ_INIT (&(queue->threadlet_work_pool)); +qemu_mutex_init (&(queue->lock)); +qemu_cond_init (&(queue->cond)); +}
No need to make this inline.
async-work.h line 68
Quoted 11 lines
async-work.h new file +11
-0+extern void threadlet_submit (ThreadletQueue *queue, +ThreadletWork *work); + +extern void threadlet_submit_common (ThreadletWork *work); + +extern int cancel_threadlet (ThreadletQueue *queue,ThreadletWork *work); +extern int cancel_threadlet_common (ThreadletWork *work); + + +extern void flush_threadlet_queue (ThreadletQueue *queue); +extern void flush_common_threadlet_queue (void );
Please make the position of the verb consistent (e.g. "submit_threadlet").
Paolo
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 10 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
async-work.c line 43
Quoted 5 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.
Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
-- Jamie
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 14 lines · Jamie Lokier
Quoted 12 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
async-work.c line 43
Quoted 6 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.
Quoted 3 lines
Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
Yup, that's the intention of the code.
We signal instead of broadcast because broadcasting causes all threads to wake up which hurts performance. AFAICT, there is no correctness issue in using signal vs. broadcast.
Regards,
Anthony Liguori
Quoted 2 lines
-- Jamie
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
In reply to Jamie LokierQuoted 12 lines · Jamie Lokier
Quoted 10 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
async-work.c line 43
Quoted 5 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.
Quoted 2 lines
Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
Unnecessary idle threads are immediately expired as soon as the threadlet exits if ncecessary, since here
+ queue->idle_threads++;
+
+check_exit:
+ if (queue->exit || ((queue->idle_threads > 0) &&
+ (queue->cur_threads > queue->min_threads))) {
+ /* We exit the queue or we retain minimum number of threads */
+ break;+ }
queue->idle_threads > 0 will always be true (so maybe that should be changed into an assertion: "this thread is idle, so there must be idle threads").
The min/max_threads parameters of the queue are currently immutable, so it can never happen that a thread has to be expired while it's waiting.
It may well become true in the future, in which case the condvar will
have to be broadcast when min_threads changes.
I may well be wrong of course. :)
Paolo
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 10 lines · Paolo Bonzini
Quoted 9 lines · Jamie Lokier
Quoted 8 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
Quoted 5 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
Unnecessary idle threads are immediately expired as soon as the threadlet exits if ncecessary, since here
If a threadlet is waiting to consume more work, unless we do a pthread_cancel (I dislike cancellation) it will keep waiting until it gets more work (which would mean it's not actually idle)...
Quoted 11 lines
async-work.c new file +6
-0+ queue->idle_threads++; + +check_exit: +if (queue->exit || ((queue->idle_threads >0 ) && + (queue->cur_threads > queue->min_threads))) { +/* We exit the queue or we retain minimum number of threads */
(quoted diff) +2
-0+ break; + }
queue->idle_threads> 0 will always be true (so maybe that should be changed into an assertion: "this thread is idle, so there must be idle threads").
queue->exit could be true though so it's necessary to at least check that condition. I agree though that via the normal fall through path, queue->idle_threads can never be non-zero.
Regards,
Anthony Liguori
Quoted 3 lines
The min/max_threads parameters of the queue are currently immutable, so it can never happen that a thread has to be expired while it's waiting. It may well become true in the future, in which case the condvar will have to be broadcast when
min_threadschanges.I may well be wrong of course. :)
I think the code has been modified such that you are correct.
Quoted 1 line
Paolo
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 17 lines · Anthony Liguori
Quoted 16 lines · Paolo Bonzini
Quoted 14 lines · Jamie Lokier
Quoted 10 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
Quoted 5 lines
+ while (QTAILQ_EMPTY(&(queue->request_list))&&
+ (ret != ETIMEDOUT)) {
+ ret = qemu_cond_timedwait(&(queue->cond),
+ &(queue->lock), 10*100000);
+ }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
Quoted 6 lines
Quoted 2 lines
Unnecessary idle threads are immediately expired as soon as the threadlet exits if ncecessary, since here
If a threadlet is waiting to consume more work, unless we do a
pthread_cancel(I dislike cancellation) it will keep waiting until it gets more work (which would mean it's not actually idle)...
Agreed---no cancellation, please.
BTW it's obviously okay with signaling the condition when a threadlet is submitted. But when something affects all queue's workers (flush_threadlet_queue) you want a broadcast and using expiration as a substitute is fishy.
Quoted 9 lines
Quoted 9 lines
+ queue->idle_threads++;
async-work.c new file +7
-0+ +check_exit: +if (queue->exit || ((queue->idle_threads >0 ) && + (queue->cur_threads > queue->min_threads))) { +/* We exit the queue or we retain minimum number of threads */ +break ; + }
Quoted 6 lines
Quoted 3 lines
queue->idle_threads> 0 will always be true (so maybe that should be changed into an assertion: "this thread is idle, so there must be idle threads").
queue->exitcould be true though so it's necessary to at least check that condition.
Yes, of course. The correct test should be:
if (queue->exit || queue->cur_threads > queue->min_threads)
But queue->idle_threads will be > 0 even if coming via the goto (which should be eliminated).
Or maybe no. After flushing you still want min_threads threads to run.
The correct thing then would be:
do {
...
assert (queue->idle_threads > 0);
if (queue->exit) {
/* Threads waiting on the barrier cannot do work. */
queue->idle_threads--;
qemu_mutex_unlock(&(queue->lock));
qemu_barrier_wait(&queue->barr);
qemu_mutex_lock(&(queue->lock));
queue->idle_threads++;
}
} while (queue->cur_threads <= queue->min_threads);
queue->idle_threads--;
queue->cur_threads--;
qemu_mutex_unlock(&queue->lock);
return NULL;So, if min_threads were changed, broadcasting the condition would be enough to exit unwanted threads one at a time, as soon as it grabs the lock.
Paolo
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 21 lines · Paolo Bonzini
Quoted 20 lines · Anthony Liguori
Quoted 18 lines · Paolo Bonzini
Quoted 9 lines · Jamie Lokier
Quoted 8 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
Quoted 5 lines
+ while (QTAILQ_EMPTY(&(queue->request_list))&&
+ (ret != ETIMEDOUT)) {
+ ret = qemu_cond_timedwait(&(queue->cond),
+ &(queue->lock), 10*100000);
+ }Quoted 5 lines
Quoted 2 lines
Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
Unnecessary idle threads are immediately expired as soon as the threadlet exits if ncecessary, since here
Quoted 7 lines
Quoted 3 lines
If a threadlet is waiting to consume more work, unless we do a
pthread_cancel(I dislike cancellation) it will keep waiting until it gets more work (which would mean it's not actually idle)...Agreed---no cancellation, please.
BTW it's obviously okay with signaling the condition when a threadlet is submitted. But when something affects all queue's workers (
flush_threadlet_queue) you want a broadcast and using expiration as a substitute is fishy.
IMHO, there shouldn't be a need for flush_threadlet_queue. It doesn't look used in the aio conversion and if virtio-9p needs it, I suspect something is wrong.
Regards,
Anthony Liguori
Quoted 45 lines
Quoted 9 lines
Quoted 9 lines
+ queue->idle_threads++;
async-work.c new file +7
-0+ +check_exit: +if (queue->exit || ((queue->idle_threads >0 ) && + (queue->cur_threads > queue->min_threads))) { +/* We exit the queue or we retain minimum number of threads */ +break ; + }Quoted 6 lines
Quoted 3 lines
queue->idle_threads> 0 will always be true (so maybe that should be changed into an assertion: "this thread is idle, so there must be idle threads").
queue->exitcould be true though so it's necessary to at least check that condition.Yes, of course. The correct test should be:
if (queue->exit || queue->cur_threads > queue->min_threads)But
queue->idle_threadswill be > 0 even if coming via the goto (which should be eliminated).Or maybe no. After flushing you still want
min_threadsthreads to run. The correct thing then would be:do { ... assert (queue->idle_threads > 0); if (queue->exit) { /* Threads waiting on the barrier cannot do work. */ queue->idle_threads--; qemu_mutex_unlock(&(queue->lock)); qemu_barrier_wait(&queue->barr); qemu_mutex_lock(&(queue->lock)); queue->idle_threads++; } } while (queue->cur_threads <= queue->min_threads); queue->idle_threads--; queue->cur_threads--; qemu_mutex_unlock(&queue->lock); return NULL;So, if
min_threadswere changed, broadcasting the condition would be enough to exit unwanted threads one at a time, as soon as it grabs the lock.Paolo
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 33 lines · Anthony Liguori
Quoted 32 lines · Paolo Bonzini
Quoted 21 lines · Anthony Liguori
Quoted 19 lines · Paolo Bonzini
Quoted 16 lines · Jamie Lokier
Quoted 11 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
Quoted 5 lines
+ while (QTAILQ_EMPTY(&(queue->request_list))&&
+ (ret != ETIMEDOUT)) {
+ ret = qemu_cond_timedwait(&(queue->cond),
+ &(queue->lock), 10*100000);
+ }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
Quoted 6 lines
Quoted 2 lines
Unnecessary idle threads are immediately expired as soon as the threadlet exits if ncecessary, since here
If a threadlet is waiting to consume more work, unless we do a
pthread_cancel(I dislike cancellation) it will keep waiting until it gets more work (which would mean it's not actually idle)...Agreed---no cancellation, please.
Quoted 9 lines
Quoted 4 lines
BTW it's obviously okay with signaling the condition when a threadlet is submitted. But when something affects all queue's workers (
flush_threadlet_queue) you want a broadcast and using expiration as a substitute is fishy.IMHO, there shouldn't be a need for
flush_threadlet_queue. It doesn't look used in the aio conversion and if virtio-9p needs it, I suspect something is wrong.
I would need something like flush_threadlet_queue for the vnc server. I need it invnc_disconnect(), vnc_dpy_resize() and vnc_dpy_cpy() so wait (and/or abort) current
encoding jobs.
Signature · 2 lines
Corentin Chary http://xf.iksaif.net
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 7 lines · Corentin Chary
I would need something like
flush_threadlet_queuefor the vnc server. I need it invnc_disconnect(),vnc_dpy_resize()andvnc_dpy_cpy()so wait (and/or abort) current
encoding jobs.
I'm not sure threadlets are the right thing for the VNC server. The VNC server wants one dedicated thread. Threadlets are a thread pool. You could potentially use one thread per client but I doubt it would be worth it.
At any rate, flushing the full queue is overkill. You want to wait for your specific thread to terminate and you want to block execution until that happens. IOW, you want to join the thread.
Regards,
Anthony Liguori
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 9 lines · Anthony Liguori
Quoted 7 lines · Corentin Chary
I would need something like
flush_threadlet_queuefor the vnc server. I need it invnc_disconnect(),vnc_dpy_resize()andvnc_dpy_cpy()so wait (and/or abort) current
encoding jobs.
Quoted 8 lines
I'm not sure threadlets are the right thing for the VNC server. The VNC server wants one dedicated thread. Threadlets are a thread pool. You could potentially use one thread per client but I doubt it would be worth it.
At any rate, flushing the full queue is overkill. You want to wait for your specific thread to terminate and you want to block execution until that happens. IOW, you want to join the thread.
Oh right, I should have read the changelog more carefully, it's a global queue now ...
Thanks,
Signature · 2 lines
Corentin Chary http://xf.iksaif.net
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 15 lines · Corentin Chary
Quoted 13 lines · Anthony Liguori
Quoted 7 lines · Corentin Chary
I would need something like
flush_threadlet_queuefor the vnc server. I need it invnc_disconnect(),vnc_dpy_resize()andvnc_dpy_cpy()so wait (and/or abort) current
encoding jobs.I'm not sure threadlets are the right thing for the VNC server. The VNC server wants one dedicated thread. Threadlets are a thread pool. You could potentially use one thread per client but I doubt it would be worth it.
Quoted 7 lines
Quoted 4 lines
At any rate, flushing the full queue is overkill. You want to wait for your specific thread to terminate and you want to block execution until that happens. IOW, you want to join the thread.
Oh right, I should have read the changelog more carefully, it's a global queue now ...
Well, the APIs that allow the subsystems to create their own private queues is still retained. But having read what Anthony mentioned, I doubt if you would want to do that for a single helper thread :-)
Quoted 5 lines
Thanks,
Signature · 2 lines
Corentin Chary http://xf.iksaif.net
Signature · 2 lines
Thanks and Regards gautham
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
In reply to Anthony LiguoriQuoted 9 lines · Anthony Liguori
Quoted 4 lines · Paolo Bonzini
BTW it's obviously okay with signaling the condition when a threadlet is submitted. But when something affects all queue's workers (
flush_threadlet_queue) you want a broadcast and using expiration as a substitute is fishy.IMHO, there shouldn't be a need for
flush_threadlet_queue. It doesn't look used in the aio conversion and if virtio-9p needs it, I suspect something is wrong.
virtio-9p doesn't need it.
The API has been added for the vnc-server case, where a subsystem wants to wait on the threads of it's private queue to finish executing the already queued tasks. It's the responsibility of the subsystem to make sure that new tasks are not submitted during this interval.
I sought clarification regarding this earlier,
http://lists.gnu.org/archive/html/qemu-devel/2010-06/msg01382.html
But now I am beginning to doubt I understood the use-case correctly.
Quoted 4 lines
Regards,
Anthony Liguori
Signature · 2 lines
Thanks and Regards gautham
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
In reply to Anthony LiguoriQuoted 17 lines · Anthony Liguori
Quoted 16 lines · Paolo Bonzini
Quoted 14 lines · Jamie Lokier
Quoted 10 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
async-work.c line 43
Quoted 5 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
Quoted 6 lines
Quoted 2 lines
Unnecessary idle threads are immediately expired as soon as the threadlet exits if ncecessary, since here
If a threadlet is waiting to consume more work, unless we do a
pthread_cancel(I dislike cancellation) it will keep waiting until it gets more work (which would mean it's not actually idle)...
There's some mild abuse of the mutex/condvar going on.
As (queue->exit || queue->idle_threads > queue->min_threads) is a condition for breaking out of the loop, that condition ought to be checked in the mutex->cond_wait region, but it isn't.
It doesn't matter here because the queue is empty when queue->exit, and the idle > min_threads condition can't become true.
Quoted 4 lines
Quoted 4 lines
The min/max_threads parameters of the queue are currently immutable, so it can never happen that a thread has to be expired while it's waiting. It may well become true in the future, in which case the condvar will have to be broadcast when
min_threadschanges.
Broadcasting when min_threads decreases wouldn't be enough, because min_threads isn't checked inside the mutex->cond_wait region.
-- Jamie
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 25 lines · Jamie Lokier
Quoted 17 lines · Anthony Liguori
Quoted 16 lines · Paolo Bonzini
Quoted 14 lines · Jamie Lokier
Quoted 10 lines · Paolo Bonzini
These should be (at least for now) block-obj-$(CONFIG_POSIX).
async-work.c line 43
Quoted 5 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.Are you sure? It looks like it also expires idle threads after a fixed amount of idle time.
Quoted 6 lines
Quoted 2 lines
Unnecessary idle threads are immediately expired as soon as the threadlet exits if ncecessary, since here
If a threadlet is waiting to consume more work, unless we do a
pthread_cancel(I dislike cancellation) it will keep waiting until it gets more work (which would mean it's not actually idle)...
Quoted 8 lines
There's some mild abuse of the mutex/condvar going on.
As (
queue->exit||queue->idle_threads>queue->min_threads) is a condition for breaking out of the loop, that condition ought to be checked in themutex->cond_waitregion, but it isn't.It doesn't matter here because the queue is empty when
queue->exit, and the idle >min_threadscondition can't become true.
Sorry, thinko. It does matter when queue->exit, precisely because the queue is empty :-)
Even cond_broadcast after queue->exit is set isn't enough to remove the need for the timed wait hack.
Putting the whole condition inside the mutex->cond_wait region, not just empty queue test, will remove the need for timed wait. Broadcast is still needed, or alternatively a cond_signal from each exiting thread will allow them to wake and close without a thundering herd.
-- Jamie
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 2 lines · Jamie Lokier
Putting the whole condition inside the
mutex->cond_waitregion, not just empty queue test, will remove the need for timed wait.
Yes, the condition must include all the cases when you broadcast.
Quoted 3 lines
Broadcast
is still needed, or alternatively acond_signalfrom each exiting thread will allow them to wake and close without a thundering herd.
If the pthreads cause a thundering herd, that's the pthreads problem. A properly implemented condvar will just requeue the threads to the mutex.
NPTL does, the Win32 condvar most likely won't. :)
Anyway if Corentin doesn't need the flush_threadlets_queue all this can be dropped.
Paolo
[Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
In reply to Paolo BonziniQuoted 4 lines · Paolo Bonzini
Makefile.objs qobject-obj-y += qerror.o · line 13
Quoted 2 lines
Makefile.objs +2
-0+block-obj-y += qemu-thread.o +block-obj-y += async-work.oThese should be (at least for now) block-obj-$(CONFIG_POSIX).
Right. Will fix that.
Quoted 9 lines
async-work.c line 43
Quoted 6 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.
I think Anthony answered this one.
Quoted 5 lines
async-work.c line 46
Quoted 3 lines
async-work.c new file +2
-0+if (QTAILQ_EMPTY (&(queue->request_list))) + goto check_exit;What's the reason for the goto? {...} works just as well.
Yes {...} works.
Besides, this two step condition checking is broken and can cause the threads to exit even in the presence of unprocessed queued ThreadletWork items.
Will fix this in the v5 (hopefully there will be one :-))
Quoted 11 lines
Quoted 11 lines
async-work.c new file +10
-0+/** +* flush_threadlet_queue: Wait till completion of all the submitted tasks +* @queue: Queue containing the tasks we're waiting on. +*/ +void flush_threadlet_queue (ThreadletQueue *queue) +{ +qemu_mutex_lock (&queue->lock); + queue->exit =1 ; + +qemu_barrier_init (&queue->barr, queue->cur_threads +1 );
Quoted 8 lines
async-work.c line 133
Quoted 3 lines
async-work.c new file +3
-0+qemu_mutex_unlock (&queue->lock); + +qemu_barrier_wait (&queue->barr);Can be implemented just as well with
queue->condand a loop waiting forqueue->cur_threads== 0. This would remove the need to implement barriers in qemu-threads (especially for Win32). Anyway whoever will contribute Win32 qemu-threads can do it, since it's not hard.
That was the other option I had considered before going for barriers, for no particular reason. Now, considering that barriers are not welcome, I will implement this method.
Quoted 10 lines
async-work.c line 186
Quoted 5 lines
async-work.c new file +4
-0+int cancel_threadlet_common (ThreadletWork *work) +{ +return cancel_threadlet (&globalqueue, work); +}I would prefer
*_threadletto be the globalqueue function (andflush_threadlets) and queue_*_threadlet to be the special-queue function. I should have spoken earlier probably, but please consider this if there will be a v5.
Sure, will do that.
Quoted 5 lines
Quoted 2 lines
+ * Generalization based on posix-aio emulation code.
No need to specify these as long as the original authors are attributed properly.
Ok!
Quoted 16 lines
Quoted 4 lines
async-work.h new file +3
-0+static inline void threadlet_queue_init (ThreadletQueue *queue, +int max_threads,int min_threads) +{async-work.h line 56
Quoted 10 lines
async-work.h new file +10
-0+ queue->cur_threads =0 ; + queue->idle_threads =0 ; + queue->exit =0 ; + queue->max_threads = max_threads; + queue->min_threads = min_threads; +QTAILQ_INIT (&(queue->request_list)); +QTAILQ_INIT (&(queue->threadlet_work_pool)); +qemu_mutex_init (&(queue->lock)); +qemu_cond_init (&(queue->cond)); +}No need to make this inline.
Will fix this.
Quoted 14 lines
async-work.h line 68
Quoted 12 lines
async-work.h new file +11
-0+extern void threadlet_submit (ThreadletQueue *queue, +ThreadletWork *work); + +extern void threadlet_submit_common (ThreadletWork *work); + +extern int cancel_threadlet (ThreadletQueue *queue,ThreadletWork *work); +extern int cancel_threadlet_common (ThreadletWork *work); + + +extern void flush_threadlet_queue (ThreadletQueue *queue); +extern void flush_common_threadlet_queue (void );Please make the position of the verb consistent (e.g. "
submit_threadlet").
Overlooked threadlet_submit() in the rename process. It has to be submit_threadlet(). Will fix.
Thanks for the detailed review.
Regards
gautham.
Quoted 2 lines
Paolo
[Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 10 lines
Quoted 8 lines
async-work.c line 43
Quoted 5 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.I think Anthony answered this one.
I think he said that the code has been changed so I am right? :)
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
async-work.c new file +10
-0+/** +* flush_threadlet_queue: Wait till completion of all the submitted tasks +* @queue: Queue containing the tasks we're waiting on. +*/ +void flush_threadlet_queue (ThreadletQueue *queue) +{ +qemu_mutex_lock (&queue->lock); + queue->exit =1 ; + +qemu_barrier_init (&queue->barr, queue->cur_threads +1 );Quoted 8 lines
async-work.c line 133
Quoted 3 lines
async-work.c new file +3
-0+qemu_mutex_unlock (&queue->lock); + +qemu_barrier_wait (&queue->barr);Can be implemented just as well with
queue->condand a loop waiting forqueue->cur_threads== 0. This would remove the need to implement barriers in qemu-threads (especially for Win32). Anyway whoever will contribute Win32 qemu-threads can do it, since it's not hard.
Quoted 3 lines
That was the other option I had considered before going for barriers, for no particular reason. Now, considering that barriers are not welcome, I will implement this method.
I guess we decided flush isn't really useful at all. Might as well leave it out of v5 and implement it later, so the barrier and complicated exit condition are now unnecessary.
Thanks,
Paolo
Re: [Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 6 lines · Paolo Bonzini
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
async-work.c new file +5
-0+while (QTAILQ_EMPTY (&(queue->request_list))&& + (ret !=ETIMEDOUT )) { + ret =qemu_cond_timedwait (&(queue->cond), + &(queue->lock),10 *100000 ); + }
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.I think Anthony answered this one.
I think he said that the code has been changed so I am right? :)
You're right about the condition we check in the exit path but the timedwait is needed to expire an idle thread.
Regards,
Anthony Liguori
[Qemu-devel] Re: [PATCH V4 2/3] qemu: Generic task offloading framework: threadlets
Quoted 10 lines · Anthony Liguori
Quoted 9 lines · Paolo Bonzini
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
+ while (QTAILQ_EMPTY(&(queue->request_list))&&
+ (ret != ETIMEDOUT)) {
+ ret = qemu_cond_timedwait(&(queue->cond),
+ &(queue->lock), 10*100000);
+ }Using
qemu_cond_timedwaitis a hack for not properly broadcasting the condvar inflush_threadlet_queue.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
I think Anthony answered this one.
I think he said that the code has been changed so I am right? :)
You're right about the condition we check in the exit path but the timedwait is needed to expire an idle thread.
In posix-aio-compat, yes. In threadlets you'll expire excess idle threads after each threadlet has completed. If you want to keep the threads above the min_threads-th ready for 10 seconds, that's fine; but it's not what the v4 code does.
Paolo
PATCH v4 3/3 qemu: Convert AIO code to use threadlets.
In reply to Gautham R ShenoyThis patch makes the paio subsystem use the threadlet framework thereby decoupling asynchronous threading framework portion out of posix-aio-compat.c
The patch has been tested with fstress.
--- posix-aio-compat.c | 152 ++++++++-------------------------------------------- 1 files changed, 24 insertions(+), 128 deletions(-)
posix-aio-compat.c +24 -128
@@ -28,6 +28,7 @@#include "block_int.h" #include "block/raw-posix-aio.h" +#include "async-work.h" struct qemu_paiocb {
@@ -50,6 +51,7 @@ struct qemu_paiocb {struct qemu_paiocb *next;int async_context_id; +ThreadletWork work; }; typedef struct PosixAioState {
@@ -57,16 +59,6 @@ typedef struct PosixAioState {struct qemu_paiocb *first_aio; } PosixAioState; - -static pthread_mutex_t lock =PTHREAD_MUTEX_INITIALIZER ; -static pthread_cond_t cond =PTHREAD_COND_INITIALIZER ; -static pthread_t thread_id; -static pthread_attr_t attr; -static int max_threads =64 ; -static int cur_threads =0 ; -static int idle_threads =0 ; -static QTAILQ_HEAD(, qemu_paiocb) request_list; -#ifdef CONFIG_PREADV static int preadv_present =1 ;#else
@@ -84,39 +76,6 @@ static void die(const char *what)die2 (errno, what); } -static void mutex_lock (pthread_mutex_t *mutex) -{ -int ret =pthread_mutex_lock (mutex); -if (ret)die2 (ret,"pthread_mutex_lock" ); -} - -static void mutex_unlock (pthread_mutex_t *mutex) -{ -int ret =pthread_mutex_unlock (mutex); -if (ret)die2 (ret,"pthread_mutex_unlock" ); -} - -static int cond_timedwait (pthread_cond_t *cond,pthread_mutex_t *mutex, -struct timespec *ts) -{ -int ret =pthread_cond_timedwait (cond, mutex, ts); -if (ret && ret !=ETIMEDOUT )die2 (ret,"pthread_cond_timedwait" ); -return ret; -} - -static void cond_signal (pthread_cond_t *cond) -{ -int ret =pthread_cond_signal (cond); -if (ret)die2 (ret,"pthread_cond_signal" ); -} - -static void thread_create (pthread_t *thread,pthread_attr_t *attr, -void *(*start_routine)(void *),void *arg) -{ -int ret =pthread_create (thread, attr, start_routine, arg); -if (ret)die2 (ret,"pthread_create" ); -} -static ssize_t handle_aiocb_ioctl (struct qemu_paiocb *aiocb) {int ret;
@@ -300,47 +259,27 @@ static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)return nbytes; } -static void *aio_thread (void *unused) +static void aio_thread (ThreadletWork *work) { -pid_t pid; - - pid =getpid (); - -while (1 ) { -struct qemu_paiocb *aiocb; -ssize_t ret =0 ; -qemu_timeval tv; -struct timespec ts; - -qemu_gettimeofday (&tv); - ts.tv_sec = tv.tv_sec +10 ; - ts.tv_nsec =0 ; -mutex_lock (&lock); +pid_t pid; -while (QTAILQ_EMPTY (&request_list) && - !(ret ==ETIMEDOUT )) { - ret =cond_timedwait (&cond, &lock, &ts); - } + struct qemu_paiocb *aiocb = container_of(work, struct qemu_paiocb, work); +ssize_t ret =0 ; -if (QTAILQ_EMPTY (&request_list)) -break ; + pid =getpid (); - aiocb =QTAILQ_FIRST (&request_list); -QTAILQ_REMOVE (&request_list, aiocb, node); - aiocb->active =1 ; - idle_threads--; -mutex_unlock (&lock); + aiocb->active =1 ; -switch (aiocb->aio_type &QEMU_AIO_TYPE_MASK ) { -case QEMU_AIO_READ : -case QEMU_AIO_WRITE : +switch (aiocb->aio_type &QEMU_AIO_TYPE_MASK ) { +case QEMU_AIO_READ : +case QEMU_AIO_WRITE : ret =handle_aiocb_rw (aiocb);break ; -case QEMU_AIO_FLUSH : - ret =handle_aiocb_flush (aiocb); -break ; -case QEMU_AIO_IOCTL : +case QEMU_AIO_FLUSH : + ret =handle_aiocb_flush (aiocb); +break ; +case QEMU_AIO_IOCTL : ret =handle_aiocb_ioctl (aiocb);break ;default :
@@ -349,57 +288,25 @@ static void *aio_thread(void *unused)break ; } -mutex_lock (&lock); - aiocb->ret = ret; - idle_threads++; -mutex_unlock (&lock); + aiocb->ret = ret; -if (kill (pid, aiocb->ev_signo))die ("kill failed" ); - } - - idle_threads--; - cur_threads--; -mutex_unlock (&lock); - -return NULL ; -} - -static void spawn_thread (void ) -{ -sigset_t set, oldset; - - cur_threads++; - idle_threads++; - -/* block all signals */ -if (sigfillset (&set))die ("sigfillset" ); -if (sigprocmask (SIG_SETMASK , &set, &oldset))die ("sigprocmask" ); - -thread_create (&thread_id, &attr, aio_thread,NULL ); - -if (sigprocmask (SIG_SETMASK , &oldset,NULL ))die ("sigprocmask restore" ); +if (kill (pid, aiocb->ev_signo))die ("kill failed" ); }static void qemu_paio_submit (struct qemu_paiocb *aiocb) { aiocb->ret = -EINPROGRESS ; aiocb->active =0 ; -mutex_lock (&lock); -if (idle_threads ==0 && cur_threads < max_threads) -spawn_thread (); -QTAILQ_INSERT_TAIL (&request_list, aiocb, node); -mutex_unlock (&lock); -cond_signal (&cond); + + aiocb->work.func = aio_thread; +threadlet_submit_common (&aiocb->work); }static ssize_t qemu_paio_return (struct qemu_paiocb *aiocb) {ssize_t ret; -mutex_lock (&lock); ret = aiocb->ret; -mutex_unlock (&lock); -return ret; }
@@ -535,14 +442,14 @@ static void paio_cancel(BlockDriverAIOCB *blockacb)struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;int active =0 ; -mutex_lock (&lock);if (!acb->active) { -QTAILQ_REMOVE (&request_list, acb, node); - acb->ret = -ECANCELED ; +if (!cancel_threadlet_common (&acb->work)) + acb->ret = -ECANCELED ; +else + active =1 ; }else if (acb->ret == -EINPROGRESS ) { active =1 ; } -mutex_unlock (&lock);if (active) {/* fail safe: if the aio could not be canceled, we wait for
@@ -615,7 +522,6 @@ int paio_init(void)struct sigaction act;PosixAioState *s;int fds[2 ]; -int ret;if (posix_aio_state)return 0 ;
@@ -642,16 +548,6 @@ int paio_init(void)qemu_aio_set_fd_handler (s->rfd, posix_aio_read,NULL , posix_aio_flush, posix_aio_process_queue, s); - ret =pthread_attr_init (&attr); -if (ret) -die2 (ret,"pthread_attr_init" ); - - ret =pthread_attr_setdetachstate (&attr,PTHREAD_CREATE_DETACHED ); -if (ret) -die2 (ret,"pthread_attr_setdetachstate" ); - -QTAILQ_INIT (&request_list); - posix_aio_state = s;return 0 ; }
Quoted 5 lines · Gautham R Shenoy
Hi,
This is the v4 of the patch-series to have a generic asynchronous task offloading framework (called threadlets) within qemu.
Semantically, a threadlet is identical to a QEMUBH except that a QEMUBH holds the qemu_mutex and a threadlet doesn't.
I'd suggest naming these functions similar to QEMUBH to the point where it makes sense to use the same structure. Instead of the normal qemu_bh_schedule, I'd suggest having a qemu_bh_schedule_unlocked().
Also, please introduce a short document in docs/ that gives a brief overview of how to use unlocked bottom halves especially focusing on the considerations with respect to what should and shouldn't be done in these functions. It would be helpful (although optional) to document traditional bottom halves in the same file.
Regards,
Anthony Liguori
Quoted 70 lines
V3 can be found here:
http://lists.gnu.org/archive/html/qemu-devel/2010-06/index.htmlChanges from V3:
=====================================================================
- Did away with the qemu-thread wrappers for handlingpthread_attr_ttype following review comments for V3.- Added qemu-thread wrappers for
pthread_barrier_init()andpthread_barrier_wait().- Added a
flush_threadlet_queue()helper which allows the caller to wait till all the queued tasks have finished processing.- Added a global queue that can be used by most subsystems to offload tasks. The flexibility to allow individual subsystems to create their private queue with associated thread-pool has been retained.
- Fixed the Copyrights in the newly introduced file to reflect the copyrights of the borrowed code.
- Renamed the helper functions to reflect their use. Added comments for each of the helpers.
- Typedef'd the structs to adhere to the qemu coding style.
Description
=====================================================================
This patch series decouples the asynchronous threading framework implementation fromposix-aio-compat.cto implement a generic asynchronous task offloading threading framework called threadlets which can be used by other subsystems within QEMU.Currently within QEMU, the AIO subsystem (paio) creates a bunch of asynchronous threads to offload any blocking operations so that the vcpu threads and the IO thread can go back to servicing any other guest requests.
This offloading framework can be used by subsystems such as virtio-9p, Asynchronous encoding for vnc-server, so that the vcpu thread can offload blocking operations on to the asynchronous threads and resume servicing any other guest requests. The asynchronous threads, after finishing the blocking operations can then transfer the control over to the IO thread so that the latter can handle the
post_blocking_operation().The patch series passed fsstress test without any issues.
Could it be considered for inclusion ?
---
Aneesh Kumar K.V (1):
qemu: Generic task offloading framework: threadletsGautham R Shenoy (2):
qemu: Add qemu-barrier support to qemu-thread framework.
qemu: Convert AIO code to use threadlets.Makefile.objs | 3 + async-work.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++ async-work.h | 69 +++++++++++++++++++ posix-aio-compat.c | 152 +++++++----------------------------------- qemu-thread.c | 23 ++++++ qemu-thread.h | 9 +++ 6 files changed, 313 insertions(+), 129 deletions(-)create mode 100644
async-work.c
create mode 100644async-work.h
Quoted 6 lines · Anthony Liguori
Quoted 4 lines · Gautham R Shenoy
Hi,
This is the v4 of the patch-series to have a generic asynchronous task offloading framework (called threadlets) within qemu.
Quoted 6 lines
Semantically, a threadlet is identical to a QEMUBH except that a QEMUBH holds the
qemu_mutexand a threadlet doesn't.I'd suggest naming these functions similar to QEMUBH to the point where it makes sense to use the same structure. Instead of the normal
qemu_bh_schedule, I'd suggest having aqemu_bh_schedule_unlocked().
The API of threadlets is much more sensible than the one of bottom halves, especially with respect to allocation and possibility to use container_of instead of opaque.
I think it's much more sensible to keep Gautham's proposed API and long term change all bottom halves to use threadlets.
Quoted 4 lines
Also, please introduce a short document in docs/ that gives a brief overview of how to use unlocked bottom halves especially focusing on the considerations with respect to what should and shouldn't be done in these functions.
This is a nice idea though.
Paolo