: v7: Threadlets: A generic task offloading framework.
cover : v7: Threadlets: A generic task offloading framework.
Hi,
This is the v7 of the patch-series to have a generic asynchronous task offloading framework (called threadlets) within qemu.
Request to consider pulling this series as discussed during the Qemu-devel call.
V6 can be found here:
http://www.mail-archive.com/[email protected]/msg44344.html
Changes from v6:
* Chaged name cancel_threadletwork to deque_threadletwork to avoid possible confusion (suggested by Balbir).
* Fixed the logic in deque_threadletwork_on_queue.
* Modified submit_threadletwork_to_queue() to avoid spurious cond_signal.
* Removed the unnecessary check for acb->active in paio_cancel()
---
Aneesh Kumar K.V (1):
Introduce threadlets
Gautham R Shenoy (2):
Make paio subsystem use threadlets
Add helper functions to enable virtio-9p make use of the threadlets
Makefile.objs | 3 - docs/async-support.txt | 141 ++++++++++++++++++++++++++++++++++ hw/virtio-9p.c | 168 ++++++++++++++++++++++++++++++++++++++++ posix-aio-compat.c | 200 ++++++++++-------------------------------------- qemu-threadlets.c | 188 +++++++++++++++++++++++++++++++++++++++++++++ qemu-threadlets.h | 49 ++++++++++++ vl.c | 3 + 7 files changed, 593 insertions(+), 159 deletions(-) create mode 100644 docs/async-support.txt create mode 100644 qemu-threadlets.c create mode 100644 qemu-threadlets.h
Signature · 1 line
arun
PATCH 1/3 Introduce threadlets
From: 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 with an associated pool of threads.
[[email protected]: Facelift of the code, Documentation, cancel_threadlet and other helpers]
--- Makefile.objs | 3 + docs/async-support.txt | 141 +++++++++++++++++++++++++++++++++++++++++ qemu-threadlets.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++ qemu-threadlets.h | 48 ++++++++++++++ 4 files changed, 358 insertions(+), 1 deletions(-) create mode 100644 docs/async-support.txt create mode 100644 qemu-threadlets.c create mode 100644 qemu-threadlets.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-$(CONFIG_POSIX) += qemu-thread.o +block-obj-$(CONFIG_POSIX) += qemu-threadlets.o block-obj-$(CONFIG_POSIX) += posix-aio-compat.o block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
@@ -124,7 +126,6 @@ endif common-obj-y += $(addprefix ui/, $(ui-obj-y)) common-obj-y += iov.o acl.o -common-obj-$(CONFIG_THREAD) += qemu-thread.o common-obj-y += notify.o event_notifier.o common-obj-y += qemu-timer.o
docs/async-support.txt new file +141 -0
141 lines not on this page · raw message
qemu-threadlets.c new file +167 -0
167 lines not on this page · raw message
qemu-threadlets.h new file +48 -0
48 lines not on this page · raw message
Re: [Qemu-devel] [PATCH 1/3] Introduce threadlets
Quoted 15 lines · Arun R B
From: 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 with an associated pool of threads.
Quoted 7 lines
[[email protected]: Facelift of the code, Documentation,
cancel_threadletand other helpers]Signed-off-by Aneesh Kumar K.V <[email protected]>Signed-off-by Gautham R Shenoy <[email protected]>Signed-off-by Sripathi Kodi <[email protected]>Signed-off-by Arun R Bharadwaj <[email protected]>
Signature · 2 lines
Three Cheers, Balbir
PATCH 2/3 Make paio subsystem use threadlets
In reply to Arun R BharadwajFrom: Gautham R Shenoy <[email protected]>
This 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 | 170 ++++++++++------------------------------------------ 1 files changed, 33 insertions(+), 137 deletions(-)
posix-aio-compat.c +33 -137
@@ -29,6 +29,7 @@#include "block_int.h" #include "block/raw-posix-aio.h" +#include "qemu-threadlets.h" struct qemu_paiocb {
@@ -44,13 +45,13 @@ struct qemu_paiocb {int ev_signo;off_t aio_offset; -QTAILQ_ENTRY (qemu_paiocb) node;int aio_type;ssize_t ret;int active;struct qemu_paiocb *next;int async_context_id; +ThreadletWork work; }; typedef struct PosixAioState {
@@ -59,15 +60,6 @@ typedef struct PosixAioState { } 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
@@ -85,39 +77,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;
@@ -301,106 +260,53 @@ 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; + struct qemu_paiocb *aiocb = container_of(work, struct qemu_paiocb, work); +ssize_t ret =0 ; pid =getpid (); + aiocb->active =1 ; -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); - -while (QTAILQ_EMPTY (&request_list) && - !(ret ==ETIMEDOUT )) { - ret =cond_timedwait (&cond, &lock, &ts); - } - -if (QTAILQ_EMPTY (&request_list)) -break ; - - aiocb =QTAILQ_FIRST (&request_list); -QTAILQ_REMOVE (&request_list, aiocb, node); - aiocb->active =1 ; - idle_threads--; -mutex_unlock (&lock); - -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 : - ret =handle_aiocb_ioctl (aiocb); -break ; -default : -fprintf (stderr,"invalid aio request (0x%x)\n" , aiocb->aio_type); - ret = -EINVAL ; -break ; - } - -mutex_lock (&lock); - aiocb->ret = ret; - idle_threads++; -mutex_unlock (&lock); - -if (kill (pid, aiocb->ev_signo))die ("kill failed" ); +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 : + ret =handle_aiocb_ioctl (aiocb); +break ; +default : +fprintf (stderr,"invalid aio request (0x%x)\n" , aiocb->aio_type); + ret = -EINVAL ; +break ; } - idle_threads--; - cur_threads--; -mutex_unlock (&lock); - -return NULL ; -} + aiocb->ret = ret; -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; +submit_threadletwork (&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; }
@@ -536,14 +442,15 @@ 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 (!deque_threadletwork (&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
@@ -618,7 +525,6 @@ int paio_init(void)struct sigaction act;PosixAioState *s;int fds[2 ]; -int ret;if (posix_aio_state)return 0 ;
@@ -645,16 +551,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 ; }
Re: [Qemu-devel] [PATCH 2/3] Make paio subsystem use threadlets
Quoted 21 lines · Arun R Bharadwaj
static
ssize_tqemu_paio_return(struct qemu_paiocb *aiocb){
ssize_tret;- mutex_lock(&lock);
ret =aiocb->ret;
- mutex_unlock(&lock);
-
return ret;
}posix-aio-compat.c +2
-3@@ -536,14 +442,15 @@ 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 (!deque_threadletwork(&acb->work)) { + acb->ret = -ECANCELED;
posix-aio-compat.c static void paio_cancel(BlockDriverAIOCB *blockacb) · line 456
Quoted 10 lines
posix-aio-compat.c +3
-1+ } 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
Here is the assembly listing of what happens next:
454:posix-aio-compat.c **** while (qemu_paio_error(acb) == EINPROGRESS)
539 0347 48F7D8 negq %rax 540 034a 83F873 cmpl $115, %eax 541 034d 7581 jne .L46
543 .L58:
545 0350 EBFD jmp .L58
This while loop is an infinite loop. The compiler doesn't need to load acb->ret from memory.
The reason this loop worked before threadlets was because qemu_paio_return() used to acquire a lock to access acb->ret, forcing the return value to be loaded each iteration:
static ssize_t qemu_paio_return(struct qemu_paiocb *aiocb)
{
ssize_t ret;
mutex_lock(&lock);
ret = aiocb->ret;
mutex_unlock(&lock);
return ret;
}Please use synchronization to wait on the request like Anthony suggested.
Stefan
PATCH 3/3 Add helper functions to enable virtio-9p make use of the threadlets
In reply to Arun R BharadwajFrom: Gautham R Shenoy <[email protected]>
infrastructure for offloading blocking tasks such as making posix calls on to the helper threads and handle the post_posix_operations() from the context of the iothread. This frees the vcpu thread to process any other guest operations while the processing of v9fs_io is in progress.
--- hw/virtio-9p.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++ posix-aio-compat.c | 48 ++++++--------- qemu-threadlets.c | 21 +++++++ qemu-threadlets.h | 1 vl.c | 3 + 5 files changed, 211 insertions(+), 30 deletions(-)
hw/virtio-9p.c +167 -1
@@ -18,6 +18,7 @@#include "fsdev/qemu-fsdev.h" #include "virtio-9p-debug.h" #include "virtio-9p-xattr.h" +#include "qemu-threadlets.h" int debug_9p_pdu;
@@ -33,6 +34,149 @@ enum { Oappend = 0x80, }; +struct v9fs_post_op { +QTAILQ_ENTRY (v9fs_post_op) node; +void (*func)(void *arg); +void *arg; +}; + +static struct { +int rfd; +int wfd; +QemuMutex lock; + QTAILQ_HEAD(, v9fs_post_op) post_op_list; +} v9fs_async_struct; + +static void die2 (int err,const char *what) +{ +fprintf (stderr,"%s failed: %s\n" , what,strerror (err)); +abort (); +} + +static void die (const char *what) +{ +die2 (errno, what); +} + +#define ASYNC_MAX_PROCESS 5 + +/** +* v9fs_process_post_ops: Process any pending v9fs_post_posix_operation +* @arg: Not used. +* +* This function serves as a callback to the iothread to be called into whenever +* the v9fs_async_struct.wfd is written into. This thread goes through the list +* of v9fs_post_posix_operations() and executes them. In the process, it might +* queue more job on the asynchronous thread pool. +*/ +static void v9fs_process_post_ops (void *arg) +{ +int count =0 ; +struct v9fs_post_op *post_op; +int ret; +char byte; + +qemu_mutex_lock (&v9fs_async_struct.lock); +do { + ret =read (v9fs_async_struct.rfd, &byte,sizeof (byte)); + }while (ret >=0 && errno !=EAGAIN ); + +for (count =0 ; count <ASYNC_MAX_PROCESS ; count++) { +if (QTAILQ_EMPTY (&(v9fs_async_struct.post_op_list))) { +break ; + } + post_op =QTAILQ_FIRST (&(v9fs_async_struct.post_op_list)); +QTAILQ_REMOVE (&(v9fs_async_struct.post_op_list), post_op, node); + +qemu_mutex_unlock (&v9fs_async_struct.lock); + post_op->func (post_op->arg); +qemu_free (post_op); +qemu_mutex_lock (&v9fs_async_struct.lock); + } +qemu_mutex_unlock (&v9fs_async_struct.lock); +} + +/** +* v9fs_async_signal: Inform the io-thread of completion of async job. +* +* This function is used to inform the iothread that a particular +* async-operation pertaining to v9fs has been completed and that the io thread +* can handle the v9fs_post_posix_operation. +* +* This is based on the aio_signal_handler +*/ +static inline void v9fs_async_signal (void ) +{ +char byte =0 ; +ssize_t ret; +int tries =0 ; + +qemu_mutex_lock (&v9fs_async_struct.lock); +do { +assert (tries !=100 ); + ret =write (v9fs_async_struct.wfd, &byte,sizeof (byte)); + tries++; + }while (ret <0 && errno ==EAGAIN ); +qemu_mutex_unlock (&v9fs_async_struct.lock); + +if (ret <0 && errno !=EAGAIN ) { +die ("write() in v9fs" ); + } + +if (kill (getpid (),SIGUSR2 )) { +die ("kill failed" ); + } +} + +/** +* v9fs_async_helper_done: Marks the completion of the v9fs_async job +* @func: v9fs_post_posix_func() for post-processing invoked in the context of +* the io-thread +* @arg: Argument to func. +* +* This function is called from the context of one of the asynchronous threads +* in the thread pool. This is called when the asynchronous thread has finished +* executing a v9fs_posix_operation. It's purpose is to initiate the process of +* informing the io-thread that the v9fs_posix_operation has completed. +*/ +static void v9fs_async_helper_done (void (*func)(void *arg),void *arg) +{ +struct v9fs_post_op *post_op; + + post_op =qemu_mallocz (sizeof (*post_op)); + post_op->func = func; + post_op->arg = arg; + +qemu_mutex_lock (&v9fs_async_struct.lock); +QTAILQ_INSERT_TAIL (&(v9fs_async_struct.post_op_list), post_op, node); +qemu_mutex_unlock (&v9fs_async_struct.lock); + +v9fs_async_signal (); +} + +/** +* v9fs_do_async_posix: Offload v9fs_posix_operation onto async thread. +* @vs: V9fsOPState variable for the OP operation. +* @posix_fn: The posix function which has to be offloaded onto async thread. +* @post_fn_ptr: Address of the location to hold the post_fn corresponding to +* the posix_fn +* @post_fn: The post processing function corresponding to the posix_fn. +* +* This function is a helper to offload posix_operation on to the asynchronous +* thread pool. It sets up the associations with the post_function that needs to +* be invoked by from the context of the iothread once the posix_fn has been +* executed. +*/ +static void v9fs_do_async_posix (ThreadletWork *work , +void (*posix_fn)(ThreadletWork *work), +void (**post_fn_ptr)(void *arg), +void (*post_fn)(void *arg)) +{ + *post_fn_ptr = post_fn; + work->func = posix_fn; +submit_threadletwork (work); +} +static int omode_to_uflags (int8_t mode) {int ret =0 ;
@@ -3639,7 +3783,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)int i, len;struct stat stat;FsTypeEntry *fse; - +int fds[2 ]; s = (V9fsState *)virtio_common_init("virtio-9p", VIRTIO_ID_9P,
@@ -3722,5 +3866,27 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) s->tag_len; s->vdev.get_config = virtio_9p_get_config; +if (qemu_pipe (fds) ==-1 ) { +fprintf (stderr,"failed to create fd's for virtio-9p\n" ); +exit (1 ); + } + + v9fs_async_struct.rfd = fds[0 ]; + v9fs_async_struct.wfd = fds[1 ]; + +printf ("v9fs: rfd: %d\n" , v9fs_async_struct.rfd); +printf ("v9fs: wfd: %d\n" , v9fs_async_struct.wfd); + +fcntl (fds[0 ],F_SETFL ,O_NONBLOCK ); +fcntl (fds[1 ],F_SETFL ,O_NONBLOCK ); + +qemu_set_fd_handler (fds[0 ], v9fs_process_post_ops,NULL ,NULL ); +QTAILQ_INIT (&v9fs_async_struct.post_op_list); +qemu_mutex_init (&(v9fs_async_struct.lock)); +/* Create async queue. */ + + (void )v9fs_do_async_posix; + (void )v9fs_async_helper_done; +return &s->vdev; }
posix-aio-compat.c +19 -29
84 lines not on this page · raw message
qemu-threadlets.c +21 -0
@@ -15,12 +15,28 @@#include "qemu-threadlets.h" #include "osdep.h" +#include <signal.h> #define MAX_GLOBAL_THREADS 64#define MIN_GLOBAL_THREADS 64static ThreadletQueue globalqueue;static int globalqueue_init; +static void threadlet_io_completion_signal_handler (int signum) +{ +qemu_service_io (); +} + +static void threadlet_register_signal_handler (void ) +{ +struct sigaction act; + +sigfillset (&act.sa_mask); + act.sa_flags =0 ;/* do not restart syscalls to interrupt select() */ + act.sa_handler = threadlet_io_completion_signal_handler; +sigaction (SIGUSR2 , &act,NULL ); +} +static void *threadlet_worker (void *data) {ThreadletQueue *queue = data;
@@ -165,3 +181,8 @@ void threadlet_queue_init(ThreadletQueue *queue,qemu_mutex_init (&(queue->lock));qemu_cond_init (&(queue->cond)); } + +void threadlet_init (void ) +{ +threadlet_register_signal_handler (); +}
qemu-threadlets.h +1 -0
@@ -45,4 +45,5 @@ extern int deque_threadletwork_on_queue(ThreadletQueue *queue,extern int deque_threadletwork (ThreadletWork *work);extern void threadlet_queue_init (ThreadletQueue *queue,int max_threads,int min_threads); +extern void threadlet_init (void );#endif
vl.c +3 -0
@@ -148,6 +148,7 @@ int main(int argc, char **argv)#include "qemu-config.h" #include "qemu-objects.h" #include "qemu-options.h" +#include "qemu-threadlets.h" #ifdef CONFIG_VIRTFS #include "fsdev/qemu-fsdev.h" #endif
@@ -2922,6 +2923,8 @@ int main(int argc, char **argv, char **envp)exit (1 ); } +threadlet_init (); +/* init generic devices */ if (qemu_opts_foreach (qemu_find_opts ("device" ), device_init_func,NULL ,1 ) !=0 )exit (1 );
Re: [Qemu-devel] [PATCH 3/3] Add helper functions to enable virtio-9p make use of the threadlets
Quoted 8 lines · Arun R Bharadwaj
From: Gautham R Shenoy <[email protected]>
infrastructure for offloading blocking tasks such as making posix calls on to the helper threads and handle the
post_posix_operations()from the context of the iothread. This frees the vcpu thread to process any other guest operations while the processing ofv9fs_iois in progress.Signed-off-by Gautham R Shenoy <[email protected]>
(Please note this correction)
Quoted 378 lines
Signed-off-by Sripathi Kodi <[email protected]>Signed-off-by Arun R Bharadwaj <[email protected]>--- hw/virtio-9p.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++ posix-aio-compat.c | 48 ++++++--------- qemu-threadlets.c | 21 +++++++ qemu-threadlets.h | 1 vl.c | 3 + 5 files changed, 211 insertions(+), 30 deletions(-)hw/virtio-9p.c +167
-1@@ -18,6 +18,7 @@#include "fsdev/qemu-fsdev.h" #include "virtio-9p-debug.h" #include "virtio-9p-xattr.h" +#include "qemu-threadlets.h" int debug_9p_pdu;@@ -33,6 +34,149 @@ enum { Oappend = 0x80, }; +struct v9fs_post_op { +QTAILQ_ENTRY (v9fs_post_op) node; +void (*func)(void *arg); +void *arg; +}; + +static struct { +int rfd; +int wfd; +QemuMutex lock; + QTAILQ_HEAD(, v9fs_post_op) post_op_list; +} v9fs_async_struct; + +static void die2 (int err,const char *what) +{ +fprintf (stderr,"%s failed: %s\n" , what,strerror (err)); +abort (); +} + +static void die (const char *what) +{ +die2 (errno, what); +} + +#define ASYNC_MAX_PROCESS 5 + +/** +* v9fs_process_post_ops: Process any pending v9fs_post_posix_operation +* @arg: Not used. +* +* This function serves as a callback to the iothread to be called into whenever +* the v9fs_async_struct.wfd is written into. This thread goes through the list +* of v9fs_post_posix_operations() and executes them. In the process, it might +* queue more job on the asynchronous thread pool. +*/ +static void v9fs_process_post_ops (void *arg) +{ +int count =0 ; +struct v9fs_post_op *post_op; +int ret; +char byte; + +qemu_mutex_lock (&v9fs_async_struct.lock); +do { + ret =read (v9fs_async_struct.rfd, &byte,sizeof (byte)); + }while (ret >=0 && errno !=EAGAIN ); + +for (count =0 ; count <ASYNC_MAX_PROCESS ; count++) { +if (QTAILQ_EMPTY (&(v9fs_async_struct.post_op_list))) { +break ; + } + post_op =QTAILQ_FIRST (&(v9fs_async_struct.post_op_list)); +QTAILQ_REMOVE (&(v9fs_async_struct.post_op_list), post_op, node); + +qemu_mutex_unlock (&v9fs_async_struct.lock); + post_op->func (post_op->arg); +qemu_free (post_op); +qemu_mutex_lock (&v9fs_async_struct.lock); + } +qemu_mutex_unlock (&v9fs_async_struct.lock); +} + +/** +* v9fs_async_signal: Inform the io-thread of completion of async job. +* +* This function is used to inform the iothread that a particular +* async-operation pertaining to v9fs has been completed and that the io thread +* can handle the v9fs_post_posix_operation. +* +* This is based on the aio_signal_handler +*/ +static inline void v9fs_async_signal (void ) +{ +char byte =0 ; +ssize_t ret; +int tries =0 ; + +qemu_mutex_lock (&v9fs_async_struct.lock); +do { +assert (tries !=100 ); + ret =write (v9fs_async_struct.wfd, &byte,sizeof (byte)); + tries++; + }while (ret <0 && errno ==EAGAIN ); +qemu_mutex_unlock (&v9fs_async_struct.lock); + +if (ret <0 && errno !=EAGAIN ) { +die ("write() in v9fs" ); + } + +if (kill (getpid (),SIGUSR2 )) { +die ("kill failed" ); + } +} + +/** +* v9fs_async_helper_done: Marks the completion of the v9fs_async job +* @func: v9fs_post_posix_func() for post-processing invoked in the context of +* the io-thread +* @arg: Argument to func. +* +* This function is called from the context of one of the asynchronous threads +* in the thread pool. This is called when the asynchronous thread has finished +* executing a v9fs_posix_operation. It's purpose is to initiate the process of +* informing the io-thread that the v9fs_posix_operation has completed. +*/ +static void v9fs_async_helper_done (void (*func)(void *arg),void *arg) +{ +struct v9fs_post_op *post_op; + + post_op =qemu_mallocz (sizeof (*post_op)); + post_op->func = func; + post_op->arg = arg; + +qemu_mutex_lock (&v9fs_async_struct.lock); +QTAILQ_INSERT_TAIL (&(v9fs_async_struct.post_op_list), post_op, node); +qemu_mutex_unlock (&v9fs_async_struct.lock); + +v9fs_async_signal (); +} + +/** +* v9fs_do_async_posix: Offload v9fs_posix_operation onto async thread. +* @vs: V9fsOPState variable for the OP operation. +* @posix_fn: The posix function which has to be offloaded onto async thread. +* @post_fn_ptr: Address of the location to hold the post_fn corresponding to +* the posix_fn +* @post_fn: The post processing function corresponding to the posix_fn. +* +* This function is a helper to offload posix_operation on to the asynchronous +* thread pool. It sets up the associations with the post_function that needs to +* be invoked by from the context of the iothread once the posix_fn has been +* executed. +*/ +static void v9fs_do_async_posix (ThreadletWork *work , +void (*posix_fn)(ThreadletWork *work), +void (**post_fn_ptr)(void *arg), +void (*post_fn)(void *arg)) +{ + *post_fn_ptr = post_fn; + work->func = posix_fn; +submit_threadletwork (work); +} +static int omode_to_uflags (int8_t mode) {int ret =0 ;@@ -3639,7 +3783,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)int i, len;struct stat stat;FsTypeEntry *fse; - +int fds[2 ]; s = (V9fsState *)virtio_common_init("virtio-9p", VIRTIO_ID_9P,@@ -3722,5 +3866,27 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) s->tag_len; s->vdev.get_config = virtio_9p_get_config; +if (qemu_pipe (fds) ==-1 ) { +fprintf (stderr,"failed to create fd's for virtio-9p\n" ); +exit (1 ); + } + + v9fs_async_struct.rfd = fds[0 ]; + v9fs_async_struct.wfd = fds[1 ]; + +printf ("v9fs: rfd: %d\n" , v9fs_async_struct.rfd); +printf ("v9fs: wfd: %d\n" , v9fs_async_struct.wfd); + +fcntl (fds[0 ],F_SETFL ,O_NONBLOCK ); +fcntl (fds[1 ],F_SETFL ,O_NONBLOCK ); + +qemu_set_fd_handler (fds[0 ], v9fs_process_post_ops,NULL ,NULL ); +QTAILQ_INIT (&v9fs_async_struct.post_op_list); +qemu_mutex_init (&(v9fs_async_struct.lock)); +/* Create async queue. */ + + (void )v9fs_do_async_posix; + (void )v9fs_async_helper_done; +return &s->vdev; }posix-aio-compat.c +19
-29@@ -260,6 +260,8 @@ static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)return nbytes; } +static PosixAioState *posix_aio_state; +static void aio_thread (ThreadletWork *work) {pid_t pid;@@ -288,6 +290,16 @@ static void aio_thread(ThreadletWork *work) aiocb->ret = ret; +if (posix_aio_state) { +char byte =0 ; +ssize_t ret; + + ret =write (posix_aio_state->wfd, &byte,sizeof (byte)); +if (ret <0 && errno !=EAGAIN ) { +die ("write()" ); + } + } +if (kill (pid, aiocb->ev_signo)) {die ("kill failed" ); }@@ -402,22 +414,6 @@ static int posix_aio_flush(void *opaque)return !!s->first_aio; } -static PosixAioState *posix_aio_state; - -static void aio_signal_handler (int signum) -{ -if (posix_aio_state) { -char byte =0 ; -ssize_t ret; - - ret =write (posix_aio_state->wfd, &byte,sizeof (byte)); -if (ret <0 && errno !=EAGAIN ) -die ("write()" ); - } - -qemu_service_io (); -} -static void paio_remove (struct qemu_paiocb *acb) {struct qemu_paiocb **pacb;@@ -442,13 +438,13 @@ static void paio_cancel(BlockDriverAIOCB *blockacb)struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;int active =0 ; -if (!acb->active) { -if (!deque_threadletwork (&acb->work)) { - acb->ret = -ECANCELED ; - }else { - active =1 ; - } - }else if (acb->ret == -EINPROGRESS ) { +if (!deque_threadletwork (&acb->work)) { + acb->ret = -ECANCELED ; + }else { + active =1 ; + } + +if (acb->ret == -EINPROGRESS ) { active =1 ; }@@ -522,7 +518,6 @@ BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,int paio_init (void ) { -struct sigaction act;PosixAioState *s;int fds[2 ];@@ -531,11 +526,6 @@ int paio_init(void) s =qemu_malloc (sizeof (PosixAioState)); -sigfillset (&act.sa_mask); - act.sa_flags =0 ;/* do not restart syscalls to interrupt select() */ - act.sa_handler = aio_signal_handler; -sigaction (SIGUSR2 , &act,NULL ); - s->first_aio =NULL ;if (qemu_pipe (fds) ==-1 ) {fprintf (stderr,"failed to create pipe\n" );qemu-threadlets.c +21
-0@@ -15,12 +15,28 @@#include "qemu-threadlets.h" #include "osdep.h" +#include <signal.h> #define MAX_GLOBAL_THREADS 64#define MIN_GLOBAL_THREADS 64static ThreadletQueue globalqueue;static int globalqueue_init; +static void threadlet_io_completion_signal_handler (int signum) +{ +qemu_service_io (); +} + +static void threadlet_register_signal_handler (void ) +{ +struct sigaction act; + +sigfillset (&act.sa_mask); + act.sa_flags =0 ;/* do not restart syscalls to interrupt select() */ + act.sa_handler = threadlet_io_completion_signal_handler; +sigaction (SIGUSR2 , &act,NULL ); +} +static void *threadlet_worker (void *data) {ThreadletQueue *queue = data;@@ -165,3 +181,8 @@ void threadlet_queue_init(ThreadletQueue *queue,qemu_mutex_init (&(queue->lock));qemu_cond_init (&(queue->cond)); } + +void threadlet_init (void ) +{ +threadlet_register_signal_handler (); +}qemu-threadlets.h +1
-0@@ -45,4 +45,5 @@ extern int deque_threadletwork_on_queue(ThreadletQueue *queue,extern int deque_threadletwork (ThreadletWork *work);extern void threadlet_queue_init (ThreadletQueue *queue,int max_threads,int min_threads); +extern void threadlet_init (void );#endif vl.c +3
-0@@ -148,6 +148,7 @@ int main(int argc, char **argv)#include "qemu-config.h" #include "qemu-objects.h" #include "qemu-options.h" +#include "qemu-threadlets.h" #ifdef CONFIG_VIRTFS #include "fsdev/qemu-fsdev.h" #endif @@ -2922,6 +2923,8 @@ int main(int argc, char **argv, char **envp)exit (1 ); } +threadlet_init (); +/* init generic devices */ if (qemu_opts_foreach (qemu_find_opts ("device" ), device_init_func,NULL ,1 ) !=0 )exit (1 );
Re: [Qemu-devel] [PATCH 3/3] Add helper functions to enable virtio-9p make use of the threadlets
In reply to Arun R BharadwajQuoted 193 lines · Arun R Bharadwaj
From: Gautham R Shenoy <[email protected]>
infrastructure for offloading blocking tasks such as making posix calls on to the helper threads and handle the
post_posix_operations()from the context of the iothread. This frees the vcpu thread to process any other guest operations while the processing ofv9fs_iois in progress.Signed-off-by Gautham R Shenoy <[email protected]>Signed-off-by Sripathi Kodi <[email protected]>Signed-off-by Arun R Bharadwaj <[email protected]>--- hw/virtio-9p.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++ posix-aio-compat.c | 48 ++++++--------- qemu-threadlets.c | 21 +++++++ qemu-threadlets.h | 1 vl.c | 3 + 5 files changed, 211 insertions(+), 30 deletions(-)hw/virtio-9p.c +145
-1@@ -18,6 +18,7 @@#include "fsdev/qemu-fsdev.h" #include "virtio-9p-debug.h" #include "virtio-9p-xattr.h" +#include "qemu-threadlets.h" int debug_9p_pdu;@@ -33,6 +34,149 @@ enum { Oappend = 0x80, }; +struct v9fs_post_op { +QTAILQ_ENTRY (v9fs_post_op) node; +void (*func)(void *arg); +void *arg; +}; + +static struct { +int rfd; +int wfd; +QemuMutex lock; + QTAILQ_HEAD(, v9fs_post_op) post_op_list; +} v9fs_async_struct; + +static void die2 (int err,const char *what) +{ +fprintf (stderr,"%s failed: %s\n" , what,strerror (err)); +abort (); +} + +static void die (const char *what) +{ +die2 (errno, what); +} + +#define ASYNC_MAX_PROCESS 5 + +/** +* v9fs_process_post_ops: Process any pending v9fs_post_posix_operation +* @arg: Not used. +* +* This function serves as a callback to the iothread to be called into whenever +* the v9fs_async_struct.wfd is written into. This thread goes through the list +* of v9fs_post_posix_operations() and executes them. In the process, it might +* queue more job on the asynchronous thread pool. +*/ +static void v9fs_process_post_ops (void *arg) +{ +int count =0 ; +struct v9fs_post_op *post_op; +int ret; +char byte; + +qemu_mutex_lock (&v9fs_async_struct.lock); +do { + ret =read (v9fs_async_struct.rfd, &byte,sizeof (byte)); + }while (ret >=0 && errno !=EAGAIN ); + +for (count =0 ; count <ASYNC_MAX_PROCESS ; count++) { +if (QTAILQ_EMPTY (&(v9fs_async_struct.post_op_list))) { +break ; + } + post_op =QTAILQ_FIRST (&(v9fs_async_struct.post_op_list)); +QTAILQ_REMOVE (&(v9fs_async_struct.post_op_list), post_op, node); + +qemu_mutex_unlock (&v9fs_async_struct.lock); + post_op->func (post_op->arg); +qemu_free (post_op); +qemu_mutex_lock (&v9fs_async_struct.lock); + } +qemu_mutex_unlock (&v9fs_async_struct.lock); +} + +/** +* v9fs_async_signal: Inform the io-thread of completion of async job. +* +* This function is used to inform the iothread that a particular +* async-operation pertaining to v9fs has been completed and that the io thread +* can handle the v9fs_post_posix_operation. +* +* This is based on the aio_signal_handler +*/ +static inline void v9fs_async_signal (void ) +{ +char byte =0 ; +ssize_t ret; +int tries =0 ; + +qemu_mutex_lock (&v9fs_async_struct.lock); +do { +assert (tries !=100 ); + ret =write (v9fs_async_struct.wfd, &byte,sizeof (byte)); + tries++; + }while (ret <0 && errno ==EAGAIN ); +qemu_mutex_unlock (&v9fs_async_struct.lock); + +if (ret <0 && errno !=EAGAIN ) { +die ("write() in v9fs" ); + } + +if (kill (getpid (),SIGUSR2 )) { +die ("kill failed" ); + } +} + +/** +* v9fs_async_helper_done: Marks the completion of the v9fs_async job +* @func: v9fs_post_posix_func() for post-processing invoked in the context of +* the io-thread +* @arg: Argument to func. +* +* This function is called from the context of one of the asynchronous threads +* in the thread pool. This is called when the asynchronous thread has finished +* executing a v9fs_posix_operation. It's purpose is to initiate the process of +* informing the io-thread that the v9fs_posix_operation has completed. +*/ +static void v9fs_async_helper_done (void (*func)(void *arg),void *arg) +{ +struct v9fs_post_op *post_op; + + post_op =qemu_mallocz (sizeof (*post_op)); + post_op->func = func; + post_op->arg = arg; + +qemu_mutex_lock (&v9fs_async_struct.lock); +QTAILQ_INSERT_TAIL (&(v9fs_async_struct.post_op_list), post_op, node); +qemu_mutex_unlock (&v9fs_async_struct.lock); + +v9fs_async_signal (); +} + +/** +* v9fs_do_async_posix: Offload v9fs_posix_operation onto async thread. +* @vs: V9fsOPState variable for the OP operation. +* @posix_fn: The posix function which has to be offloaded onto async thread. +* @post_fn_ptr: Address of the location to hold the post_fn corresponding to +* the posix_fn +* @post_fn: The post processing function corresponding to the posix_fn. +* +* This function is a helper to offload posix_operation on to the asynchronous +* thread pool. It sets up the associations with the post_function that needs to +* be invoked by from the context of the iothread once the posix_fn has been +* executed. +*/ +static void v9fs_do_async_posix (ThreadletWork *work , +void (*posix_fn)(ThreadletWork *work), +void (**post_fn_ptr)(void *arg), +void (*post_fn)(void *arg)) +{ + *post_fn_ptr = post_fn; + work->func = posix_fn; +submit_threadletwork (work); +} +static int omode_to_uflags (int8_t mode) {int ret =0 ;@@ -3639,7 +3783,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)int i, len;struct stat stat;FsTypeEntry *fse; - +int fds[2 ]; s = (V9fsState *)virtio_common_init("virtio-9p", VIRTIO_ID_9P,@@ -3722,5 +3866,27 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) s->tag_len; s->vdev.get_config = virtio_9p_get_config;
hw/virtio-9p.c VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) · line 3878
Quoted 10 lines
hw/virtio-9p.c +10
-0+if (qemu_pipe (fds) ==-1 ) { +fprintf (stderr,"failed to create fd's for virtio-9p\n" ); +exit (1 ); + } + + v9fs_async_struct.rfd = fds[0 ]; + v9fs_async_struct.wfd = fds[1 ]; + +printf ("v9fs: rfd: %d\n" , v9fs_async_struct.rfd); +printf ("v9fs: wfd: %d\n" , v9fs_async_struct.wfd);
I still see these debug messages. Please take care of them.
- JV
Quoted 183 lines
hw/virtio-9p.c +12
-0+ +fcntl (fds[0 ],F_SETFL ,O_NONBLOCK ); +fcntl (fds[1 ],F_SETFL ,O_NONBLOCK ); + +qemu_set_fd_handler (fds[0 ], v9fs_process_post_ops,NULL ,NULL ); +QTAILQ_INIT (&v9fs_async_struct.post_op_list); +qemu_mutex_init (&(v9fs_async_struct.lock)); +/* Create async queue. */ + + (void )v9fs_do_async_posix; + (void )v9fs_async_helper_done; +return &s->vdev; }posix-aio-compat.c +19
-29@@ -260,6 +260,8 @@ static ssize_t handle_aiocb_rw(struct qemu_paiocb *aiocb)return nbytes; } +static PosixAioState *posix_aio_state; +static void aio_thread (ThreadletWork *work) {pid_t pid;@@ -288,6 +290,16 @@ static void aio_thread(ThreadletWork *work) aiocb->ret = ret; +if (posix_aio_state) { +char byte =0 ; +ssize_t ret; + + ret =write (posix_aio_state->wfd, &byte,sizeof (byte)); +if (ret <0 && errno !=EAGAIN ) { +die ("write()" ); + } + } +if (kill (pid, aiocb->ev_signo)) {die ("kill failed" ); }@@ -402,22 +414,6 @@ static int posix_aio_flush(void *opaque)return !!s->first_aio; } -static PosixAioState *posix_aio_state; - -static void aio_signal_handler (int signum) -{ -if (posix_aio_state) { -char byte =0 ; -ssize_t ret; - - ret =write (posix_aio_state->wfd, &byte,sizeof (byte)); -if (ret <0 && errno !=EAGAIN ) -die ("write()" ); - } - -qemu_service_io (); -} -static void paio_remove (struct qemu_paiocb *acb) {struct qemu_paiocb **pacb;@@ -442,13 +438,13 @@ static void paio_cancel(BlockDriverAIOCB *blockacb)struct qemu_paiocb *acb = (struct qemu_paiocb *)blockacb;int active =0 ; -if (!acb->active) { -if (!deque_threadletwork (&acb->work)) { - acb->ret = -ECANCELED ; - }else { - active =1 ; - } - }else if (acb->ret == -EINPROGRESS ) { +if (!deque_threadletwork (&acb->work)) { + acb->ret = -ECANCELED ; + }else { + active =1 ; + } + +if (acb->ret == -EINPROGRESS ) { active =1 ; }@@ -522,7 +518,6 @@ BlockDriverAIOCB *paio_ioctl(BlockDriverState *bs, int fd,int paio_init (void ) { -struct sigaction act;PosixAioState *s;int fds[2 ];@@ -531,11 +526,6 @@ int paio_init(void) s =qemu_malloc (sizeof (PosixAioState)); -sigfillset (&act.sa_mask); - act.sa_flags =0 ;/* do not restart syscalls to interrupt select() */ - act.sa_handler = aio_signal_handler; -sigaction (SIGUSR2 , &act,NULL ); - s->first_aio =NULL ;if (qemu_pipe (fds) ==-1 ) {fprintf (stderr,"failed to create pipe\n" );qemu-threadlets.c +21
-0@@ -15,12 +15,28 @@#include "qemu-threadlets.h" #include "osdep.h" +#include <signal.h> #define MAX_GLOBAL_THREADS 64#define MIN_GLOBAL_THREADS 64static ThreadletQueue globalqueue;static int globalqueue_init; +static void threadlet_io_completion_signal_handler (int signum) +{ +qemu_service_io (); +} + +static void threadlet_register_signal_handler (void ) +{ +struct sigaction act; + +sigfillset (&act.sa_mask); + act.sa_flags =0 ;/* do not restart syscalls to interrupt select() */ + act.sa_handler = threadlet_io_completion_signal_handler; +sigaction (SIGUSR2 , &act,NULL ); +} +static void *threadlet_worker (void *data) {ThreadletQueue *queue = data;@@ -165,3 +181,8 @@ void threadlet_queue_init(ThreadletQueue *queue,qemu_mutex_init (&(queue->lock));qemu_cond_init (&(queue->cond)); } + +void threadlet_init (void ) +{ +threadlet_register_signal_handler (); +}qemu-threadlets.h +1
-0@@ -45,4 +45,5 @@ extern int deque_threadletwork_on_queue(ThreadletQueue *queue,extern int deque_threadletwork (ThreadletWork *work);extern void threadlet_queue_init (ThreadletQueue *queue,int max_threads,int min_threads); +extern void threadlet_init (void );#endif vl.c +3
-0@@ -148,6 +148,7 @@ int main(int argc, char **argv)#include "qemu-config.h" #include "qemu-objects.h" #include "qemu-options.h" +#include "qemu-threadlets.h" #ifdef CONFIG_VIRTFS #include "fsdev/qemu-fsdev.h" #endif @@ -2922,6 +2923,8 @@ int main(int argc, char **argv, char **envp)exit (1 ); } +threadlet_init (); +/* init generic devices */ if (qemu_opts_foreach (qemu_find_opts ("device" ), device_init_func,NULL ,1 ) !=0 )exit (1 );
Re: [Qemu-devel] [PATCH 3/3] Add helper functions to enable virtio-9p make use of the threadlets
In reply to Arun R BharadwajMy review comments from the last version still stand:
http://permalink.gmane.org/gmane.comp.emulators.qemu/82796
Stefan