qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Venkateswararao Jujjuri <jvrao@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [0/25] Async threading for VirtFS using glib threads & coroutines.
Date: Fri, 13 May 2011 18:29:07 -0700	[thread overview]
Message-ID: <4DCDDAE3.2030201@linux.vnet.ibm.com> (raw)
In-Reply-To: <4DCDCA6F.9000503@linux.vnet.ibm.com>

On 05/13/2011 05:18 PM, Venkateswararao Jujjuri wrote:
> On 05/13/2011 12:26 PM, Aneesh Kumar K.V wrote:
>> On Fri, 13 May 2011 09:55:03 +0100, Stefan 
>> Hajnoczi<stefanha@gmail.com>  wrote:
>>> On Thu, May 12, 2011 at 01:57:22PM -0700, Venkateswararao Jujjuri 
>>> (JV) wrote:
>>>> VirtFS (fileserver base on 9P) performs many blocking system calls 
>>>> in the
>>>> vCPU context. This effort is to move the blocking calls out of vCPU/IO
>>>> thread context, into asynchronous threads.
>>>>
>>>> Anthony's " Add hard build dependency on glib" patch and
>>>> Kevin/Stefan's coroutine effort is a prerequisite.
>>>>
>>>> This patch set contains:
>>>>   - Converting all 9pfs calls into coroutines.
>>>>   - Each 9P operation will be modified for:
>>>>      - Remove post* functions. These are our call back functions 
>>>> which makes
>>>>        the code very hard to read. Now with coroutines, we can 
>>>> achieve the same
>>>>        state machine model with nice sequential code flow.
>>>>      - Move errno access near to the local_syscall()
>>>>      - Introduce asynchronous threading
>>>>
>>>> This series has the basic infrastructure and few routines like
>>>> mkdir,monod,unlink,readdir,xattr,lstat, etc converted.
>>>> Currently we are working on converting and testing other 9P 
>>>> operations also
>>>> into this model and those patches will follow shortly.
>>>>
>>>> Removing callback functions made some of the patches little lengthy.
>>> This long patch series adds temporary structs and marshalling code for
>>> each file system operation - I think none of this is necessary.  
>>> Instead
>>> we can exploit coroutines more:
>>>
>>> The point of coroutines is that you can suspend a thread of control (a
>>> call-stack, not an OS-level thread) and can re-enter it later.  We
>>> should make coroutines thread-safe (i.e. work outside of the global
>>> mutex) and then allow switching a coroutine from a QEMU thread to a
>>> worker thread and back again:
>>>
>>> int coroutine_fn v9fs_co_readdir(V9fsState *s, V9fsFidState *fidp,
>>>                                   struct dirent **dent)
>>> {
>>>      int ret = 0;
>>>
>>>      v9fs_co_run_in_worker({
>>>          errno = 0;
>>>          *dent = s->ops->readdir(&s->ctx, fidp->fs.dir);
>>>          if (!*dent&&  errno) {
>>>              ret = -errno;
>>>          }
>>>      });
>>>      return ret;
>>> }
>>>
>>> v9fs_co_readdir() can be called from a QEMU thread.  The block of code
>>> inside v9fs_co_run_in_worker() will be executed in a worker thread.
>>> Notice that no marshalling variables is necessary at all; we can use 
>>> the
>>> function arguments and local variables because this is still the same
>>> function!
>>>
>>> When control reaches the end of the v9fs_co_run_in_worker() block,
>>> execution is resumed in a QEMU thread and the function then returns 
>>> ret.
>>> It would be incorrect to return inside the v9fs_co_run_in_worker() 
>>> block
>>> because at that point we're still inside the worker thread.
>>>
>>> Here is how v9fs_co_run_in_worker() does its magic:
>>>
>>> #define v9fs_co_run_in_worker(block) \
>>> { \
>>>      BH *co_bh; \
>>> \
>>>      co_bh = qemu_bh_new(co_run_in_worker_bh, qemu_coroutine_self()); \
>>>      qemu_bh_schedule(co_bh); \
>>>      qemu_coroutine_yield(); /* re-entered in worker thread */ \
>>>      qemu_bh_delete(co_bh); \
>>> \
>>>      block; \
>>> \
>>>      qemu_coroutine_yield(); /* re-entered in QEMU thread */ \
>>> }
>>>
>>> void co_run_in_worker_bh(void *opaque)
>>> {
>>>      Coroutine *co = opaque;
>>>
>>>      g_thread_pool_push(pool, co, NULL);
>>> }
>>>
>>> void worker_thread_fn(gpointer data, gpointer user_data)
>>> {
>>>      Coroutine *co = user_data;
>>>      char byte = 0;
>>>      ssize_t len;
>>>
>>>      qemu_coroutine_enter(co, NULL);
>>>
>>>      g_async_queue_push(v9fs_pool.completed, co);
>>>      do {
>>>          len = write(v9fs_pool.wfd,&byte, sizeof(byte));
>>>      } while (len == -1&&  errno == EINTR);
>>> }
>>>
>>> void process_req_done(void *arg)
>>> {
>>>      Coroutine *co;
>>>      char byte;
>>>      ssize_t len;
>>>
>>>      do {
>>>          len = read(v9fs_pool.rfd,&byte, sizeof(byte));
>>>      } while (len == -1&&  errno == EINTR);
>>>
>>>      while ((co = g_async_queue_try_pop(v9fs_pool.completed)) != 
>>> NULL) {
>>>          qemu_coroutine_enter(co, NULL);
>>>      }
>>> }
>>>
>>> I typed this code out in the email, it has not been compiled or tested.
>>>
>>> If you decide to eliminate coroutines entirely in the future and use
>>> worker threads exclusively to process requests, then there are clearly
>>> marked sections in the code: anything inside v9fs_co_run_in_worker()
>>> must be thread-safe already and anything outside it needs to be audited
>>> and made thread-safe.  The changes required are smaller than those if
>>> your current patch series was applied.  I wanted to mention this point
>>> to show that this doesn't paint virtfs into a corner.
>>>
>>> So where does this leave virtfs?  No marshalling is necessary and
>>> blocking operations can be performed inline using
>>> v9fs_co_run_in_worker() blocks.  The codebase will be a lot smaller.
>>>
>>> Does this seem reasonable?
>>>
>> Do we really need bottom halfs here ? can't we achieve the same with
>> v9fs_qemu_submit_request() and making the glib thread
>> function callback (request.func())to do qemu_coroutine_enter()
>
>
> I had the same question. :)  Tested without BH and touch testing 
> worked fine.
>
>
> #define v9fs_co_run_in_worker(block) \
> { \
>     g_thread_pool_push(v9fs_pool.pool, qemu_coroutine_self(), NULL); \
>     qemu_coroutine_yield(); /* re-entered in worker thread */ \
> \
>     block; \
> \
>     qemu_coroutine_yield(); /* re-entered in QEMU thread */ \
> }

I guess there is a need for BH. :)
Without that .. little stress with smp=2 causing

Co-routine re-entered recursively
Aborted

- JV




>
> void v9fs_qemu_process_req_done(void *arg)
> {
>     Coroutine *co;
>     char byte;
>     ssize_t len;
>
>     do {
>         len = read(v9fs_pool.rfd, &byte, sizeof(byte));
>     } while (len == -1 &&  errno == EINTR);
>
>     while ((co = g_async_queue_try_pop(v9fs_pool.completed)) != NULL) {
>         qemu_coroutine_enter(co, NULL);
>     }
> }
>
> void v9fs_thread_routine(gpointer data, gpointer user_data)
> {
>     Coroutine *co = data;
>     char byte = 0;
>     ssize_t len;
>
>     qemu_coroutine_enter(co, NULL);
>
>     g_async_queue_push(v9fs_pool.completed, co);
>     do {
>         len = write(v9fs_pool.wfd, &byte, sizeof(byte));
>     } while (len == -1 && errno == EINTR);
> }
>
> This model makes the code simple and also in one shot we can convert 
> all v9fs_do_syscalls
> into asynchronous threads. But as Aneesh raised will there be any 
> additional overhead
> for the additional jumps?  We can quickly test it out too.
>
> For this to work; First we need to consolidate errno and then convert 
> v9fs_do_syscalls
> with v9fs_co_run_in_worker(). After that we can post patches folding 
> in the Post* functions.
> Just a thought.
>
> Thanks,
> JV
>
>> like:
>>
>> int v9fs_co_readdir(V9fsState *s, V9fsFidState *fidp, struct dirent 
>> **dent)
>> {
>>       v9fs_wthread_enter();
>>       s->ops->readdir(&s->ctx, fidp->fs.dir);
>>       v9fs_wthread_exit();
>> }
>>
>> v9fs_worker_thread_enter()
>> {
>>      v9fs_qemu_submit_request(v9fs_worker_request);
>>      qemu_coroutine_yield();
>> }
>>
>> v9fs_coroutine_woker_func()
>> {
>>        qemu_coroutine_enter(coroutine, NULL);
>> }
>>
>>
>> I also wonder whether additional bottom halfs and additional
>> setcontext/setjmp that we end up with will have a performance impact 
>> compared
>> to what we have currently ?
>>
>> -aneesh
>
>

  reply	other threads:[~2011-05-14  1:29 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-12 20:57 [Qemu-devel] [0/25] Async threading for VirtFS using glib threads & coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 01/25] [virtio-9p] Add infrastructure to support glib threads and coroutines Venkateswararao Jujjuri (JV)
2011-05-13  5:48   ` Stefan Hajnoczi
2011-05-13 15:47     ` Venkateswararao Jujjuri
2011-05-13 15:49       ` Stefan Hajnoczi
2011-05-12 20:57 ` [Qemu-devel] [PATCH 02/25] [virtio-9p] Change all pdu handlers to coroutines Venkateswararao Jujjuri (JV)
2011-05-13  6:19   ` Stefan Hajnoczi
2011-05-12 20:57 ` [Qemu-devel] [PATCH 03/25] [virtio-9p] Remove post functions for v9fs_readlink Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 04/25] [virtio-9p] clean up v9fs_readlink Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 05/25] [virtio-9p] Move errno into v9fs_do_readlink Venkateswararao Jujjuri (JV)
2011-05-13  8:56   ` Stefan Hajnoczi
2011-05-12 20:57 ` [Qemu-devel] [PATCH 06/25] [virtio-9p] coroutines for readlink Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 07/25] [virtio-9p] Remove post functions for v9fs_mkdir Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 08/25] [virtio-9p] clean up v9fs_mkdir Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 09/25] hw/9pfs: Add yield support for readdir related coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 10/25] hw/9pfs: Update v9fs_readdir to use coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 11/25] hw/9pfs: Add yield support to statfs coroutine Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 12/25] hw/9pfs: Update v9fs_statfs to use coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 13/25] hw/9pfs: Add yield support to lstat coroutine Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 14/25] hw/9pfs: Update v9fs_getattr to use coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 15/25] hw/9pfs: Add yield support to setattr related coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 16/25] hw/9pfs: Update v9fs_setattr to use coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 17/25] hw/9pfs: Add yield support to xattr related coroutine Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 18/25] hw/9pfs: Update v9fs_xattrwalk to coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 19/25] hw/9pfs: Update v9fs_xattrcreate to use coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 20/25] hw/9pfs: Add yield support to mknod coroutine Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 21/25] hw/9pfs: Update v9fs_mknod to use coroutines Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 22/25] [virtio-9p] coroutine and threading for mkdir Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 23/25] [virtio-9p] Remove post functions for v9fs_remove Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 24/25] [virtio-9p] clean up v9fs_remove Venkateswararao Jujjuri (JV)
2011-05-12 20:57 ` [Qemu-devel] [PATCH 25/25] [virtio-9p] coroutine and threading for remove/unlink Venkateswararao Jujjuri (JV)
2011-05-13  8:55 ` [Qemu-devel] [0/25] Async threading for VirtFS using glib threads & coroutines Stefan Hajnoczi
2011-05-13 16:52   ` Anthony Liguori
2011-05-13 19:26   ` Aneesh Kumar K.V
2011-05-14  0:18     ` Venkateswararao Jujjuri
2011-05-14  1:29       ` Venkateswararao Jujjuri [this message]
2011-05-14  6:33         ` Stefan Hajnoczi
2011-05-24 21:00       ` Jamie Lokier
2011-05-25  7:16         ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4DCDDAE3.2030201@linux.vnet.ibm.com \
    --to=jvrao@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).