Second threading model.
cover Second threading model.
Hi,
This patch implements the second threading model. The first model can be referred here:
http://www.mail-archive.com/[email protected]/msg43846.html
The features of the second threading model:
* The VCPU thread runs the Qemu code until the first blocking call is encountered.
* The work to be done in the blocking call is assigned to an asynchronous thread and the VCPU thread continues running the Qemu code.
* If any blocking call is encountered by the asynchronous thread, it simply waits for the blocking call to finish and continues to run the succeeding part after that.
* There is no constant context switching between the VCPU thread and the asynchronous thread.
* The code flow is simple to understand when compared to the first threading model.
I have run the following test to measure the performance:
To test the v9fs_write call which is converted to the second threading model, I run X dd threads in parallel on the virtfs exported directory. The following is the command I use for running 2 dd threads in parallel:
time dd if=/dev/zero of=null1 bs=4k count=1000 oflag=sync & time dd if=/dev/zero of=null1 bs=4k count=1000 oflag=sync
Here is how the performance comparison between the original code, first threading model and second threading model looks like:
1 2 3 5 10
no threading: real time(s) 18.1 46.4 99.2 360.8 1518.6
system time(s) 18.0 38.3 66.8 211.7 551.6
throughput(kbps) 226 353 373 235 2681st model: real time 18.8 48.6 77.7 164.5 451.3
system time 0.6 0.2 0.18 0.68 2.4
throughput 218 336 474 624 9142nd model: real time 23.1 37.8 57.1 166.5 430
system time 0.9 1.8 0.24 0.7 3.0
throughput 177 432 430 611 940This patchset needs to be applied on top of the threadlets infrastructure series available here:
http://www.mail-archive.com/[email protected]/msg43842.html
The following series implements...
---
Sripathi Kodi (9):
Add read-write lock to QEMU
Introduce lock fid_list_lock to protect the fid list. Global rename lock
Convert stat into 2nd threading model Convert wstat into 2nd threading model Convert open into 2nd threading model Convert walk into 2nd threading model Convert read into 2nd threading model Convert write into 2nd threading model.
hw/virtio-9p.c | 950 +++++++++++++++++++++++--------------------------------- hw/virtio-9p.h | 27 ++ qemu-thread.c | 40 ++ qemu-thread.h | 10 + 4 files changed, 463 insertions(+), 564 deletions(-)
Signature · 1 line
arun
PATCH 1/9 Add read-write lock to QEMU
From: Sripathi Kodi <[email protected]>
--- qemu-thread.c | 40 ++++++++++++++++++++++++++++++++++++++++ qemu-thread.h | 10 ++++++++++ 2 files changed, 50 insertions(+), 0 deletions(-)
qemu-thread.c +40 -0
@@ -90,6 +90,46 @@ void qemu_mutex_unlock(QemuMutex *mutex)error_exit (err, __func__); } +void qemu_rwmutex_init (QemuRWMutex *mutex) +{ +int err; + + err =pthread_rwlock_init (&mutex->lock,NULL ); +if (err) { +error_exit (err, __func__); + } +} + +void qemu_rwmutex_rdlock (QemuRWMutex *mutex) +{ +int err; + + err =pthread_rwlock_rdlock (&mutex->lock); +if (err) { +error_exit (err, __func__); + } +} + +void qemu_rwmutex_wrlock (QemuRWMutex *mutex) +{ +int err; + + err =pthread_rwlock_wrlock (&mutex->lock); +if (err) { +error_exit (err, __func__); + } +} + +void qemu_rwmutex_unlock (QemuRWMutex *mutex) +{ +int err; + + err =pthread_rwlock_unlock (&mutex->lock); +if (err) { +error_exit (err, __func__); + } +} +void qemu_cond_init (QemuCond *cond) {int err;
qemu-thread.h +10 -0
@@ -7,6 +7,10 @@ struct QemuMutex {pthread_mutex_t lock; }; +struct QemuRWMutex { +pthread_rwlock_t lock; +}; +struct QemuCond {pthread_cond_t cond; };
@@ -16,6 +20,7 @@ struct QemuThread { };typedef struct QemuMutex QemuMutex ; +typedef struct QemuRWMutex QemuRWMutex ;typedef struct QemuCond QemuCond ;typedef struct QemuThread QemuThread ;
@@ -26,6 +31,11 @@ int qemu_mutex_trylock(QemuMutex *mutex);int qemu_mutex_timedlock (QemuMutex *mutex,uint64_t msecs);void qemu_mutex_unlock (QemuMutex *mutex); +void qemu_rwmutex_init (QemuRWMutex *mutex); +void qemu_rwmutex_rdlock (QemuRWMutex *mutex); +void qemu_rwmutex_wrlock (QemuRWMutex *mutex); +void qemu_rwmutex_unlock (QemuRWMutex *mutex); +void qemu_cond_init (QemuCond *cond);void qemu_cond_destroy (QemuCond *cond);void qemu_cond_signal (QemuCond *cond);
PATCH 2/9 Introduce lock fid_list_lock to protect the fid list.
In reply to Arun R BharadwajFrom: Sripathi Kodi <[email protected]>
Currently it is a normal mutex, but I will change it into a rw mutex. This should be held in read mode while looking up and in write mode while updating the list.
--- hw/virtio-9p.c | 32 +++++++++++++++++++++++++++++--- hw/virtio-9p.h | 2 ++ 2 files changed, 31 insertions(+), 3 deletions(-)
hw/virtio-9p.c +29 -3
@@ -18,7 +18,6 @@#include "fsdev/qemu-fsdev.h" #include "virtio-9p-debug.h" #include "virtio-9p-xattr.h" -#include "qemu-threadlets.h" int debug_9p_pdu;
@@ -552,7 +551,8 @@ static size_t v9fs_string_size(V9fsString *str)return str->size; } -static V9fsFidState *lookup_fid (V9fsState *s,int32_t fid) +/* fid_list_lock should be held on entry */ +static V9fsFidState *__lookup_fid (V9fsState *s,int32_t fid) {V9fsFidState *f;
@@ -565,12 +565,26 @@ static V9fsFidState *lookup_fid(V9fsState *s, int32_t fid)return NULL ; } +static V9fsFidState *lookup_fid (V9fsState *s,int32_t fid) +{ +V9fsFidState *f; + +qemu_rwmutex_rdlock (&s->fid_list_lock); + f =__lookup_fid (s, fid); +qemu_rwmutex_unlock (&s->fid_list_lock); + +return f; +} + +static V9fsFidState *alloc_fid (V9fsState *s,int32_t fid) {V9fsFidState *f; - f =lookup_fid (s, fid); +qemu_rwmutex_wrlock (&s->fid_list_lock); + f =__lookup_fid (s, fid);if (f) { +qemu_rwmutex_unlock (&s->fid_list_lock);return NULL ; }
@@ -582,6 +596,7 @@ static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) f->next = s->fid_list; s->fid_list = f; +qemu_rwmutex_unlock (&(s->fid_list_lock));return f; }
@@ -619,11 +634,13 @@ free_value:return retval; } +/* On entry, fid_list_lock and fid_lock should NOT be held */ static int free_fid (V9fsState *s,int32_t fid) {int retval =0 ;V9fsFidState **fidpp, *fidp; +qemu_rwmutex_wrlock (&s->fid_list_lock);for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {if ((*fidpp)->fid == fid) {break ;
@@ -631,12 +648,18 @@ static int free_fid(V9fsState *s, int32_t fid) }if (*fidpp ==NULL ) { +qemu_rwmutex_unlock (&s->fid_list_lock);return -ENOENT ; } fidp = *fidpp; *fidpp = fidp->next; +/* We have removed the node from the list, so we can release +* the fid_list_lock now +*/ +qemu_rwmutex_unlock (&s->fid_list_lock); +if (fidp->fid_type ==P9_FID_FILE ) {v9fs_do_close (s, fidp->fs.fd); }else if (fidp->fid_type ==P9_FID_DIR ) {
@@ -2922,6 +2945,7 @@ static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)* Fixup fid's pointing to the old name to * start pointing to the new name */ +qemu_rwmutex_rdlock (&s->fid_list_lock);for (fidp = s->fid_list; fidp; fidp = fidp->next) {if (vs->fidp == fidp) {/*
@@ -2937,6 +2961,7 @@ static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs) strlen(vs->fidp->path.data)); } } +qemu_rwmutex_unlock (&s->fid_list_lock);v9fs_string_copy (&vs->fidp->path, &vs->name); } }
@@ -3880,6 +3905,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)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)); +qemu_rwmutex_init (&(s->fid_list_lock));/* Create async queue. */ (void )v9fs_do_async_posix;
hw/virtio-9p.h +2 -0
@@ -7,6 +7,7 @@#include <utime.h> #include "file-op-9p.h" +#include "qemu-threadlets.h" /* The feature bitmap for virtio 9P */ /* The mount point is specified in a config variable */
@@ -206,6 +207,7 @@ typedef struct V9fsStateV9fsPDU pdus[MAX_REQ ]; QLIST_HEAD(, V9fsPDU) free_list;V9fsFidState *fid_list; +QemuRWMutex fid_list_lock;FileOperations *ops;FsContext ctx;uint16_t tag_len;
PATCH 3/9 Global rename lock
In reply to Arun R BharadwajFrom: Sripathi Kodi <[email protected]>
--- hw/virtio-9p.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
hw/virtio-9p.c +3 -0
@@ -21,6 +21,8 @@int debug_9p_pdu; +QemuRWMutex global_rename_lock; +enum { Oread =0x00 , Owrite =0x01 ,
@@ -3906,6 +3908,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)QTAILQ_INIT (&v9fs_async_struct.post_op_list);qemu_mutex_init (&(v9fs_async_struct.lock));qemu_rwmutex_init (&(s->fid_list_lock)); +qemu_rwmutex_init (&global_rename_lock);/* Create async queue. */ (void )v9fs_do_async_posix;
PATCH 4/9 Convert stat into 2nd threading model
In reply to Arun R BharadwajFrom: Sripathi Kodi <[email protected]>
In this model we hand over the vcpu thread only executes till the first blocking operation. It then hands over the call to the worker thread, which does everything needed to complete the call. It can make multiple blocking calls. It finally signals the IO thread to do complete_pdu().
Gautham suggested that I use a generic function to call complete_pdu, which could further simplify the post_op structure. However, some of the calls (stat included) need to free up some memory after calling complete_pdu. Handling this becomes messy if I use a common complete function.
Review comments welcome. Review comment from Gautham is a necessity.
--- hw/virtio-9p.c | 58 ++++++++++++++++++++++++++++++++------------------------ hw/virtio-9p.h | 4 ++++ 2 files changed, 37 insertions(+), 25 deletions(-)
hw/virtio-9p.c +33 -25
@@ -1381,54 +1381,62 @@ out:v9fs_string_free (&aname); } -static void v9fs_stat_post_lstat (V9fsState *s,V9fsStatState *vs,int err) +/* This is called by the IO thread */ +static void v9fs_stat_do_complete (void *opaque) { -if (err ==-1 ) { - err = -errno; +V9fsStatState *vs = (V9fsStatState *)opaque; +complete_pdu (vs->s, vs->pdu, vs->err); +v9fs_stat_free (&vs->v9stat); +qemu_free (vs); +} + +/* This is called by the async thread. It does everything +* other than calling complete_pdu +*/ +static void v9fs_stat_worker (ThreadletWork *work) +{ +V9fsStatState *vs =container_of (work, V9fsStatState, work); + + vs->fidp =lookup_fid (vs->s, vs->fid); +if (vs->fidp ==NULL ) { + vs->err = -ENOENT ; goto out; } - err =stat_to_v9stat (s, &vs->fidp->path, &vs->stbuf, &vs->v9stat); -if (err) { +qemu_rwmutex_rdlock (&global_rename_lock); + vs->err =v9fs_do_lstat (vs->s, &vs->fidp->path, &vs->stbuf); +if (vs->err ==-1 ) { + vs->err = -errno; + goto out; + } + vs->err =stat_to_v9stat (vs->s, &vs->fidp->path, &vs->stbuf, &vs->v9stat); +if (vs->err) { goto out; } vs->offset +=pdu_marshal (vs->pdu, vs->offset,"wS" ,0 , &vs->v9stat); - err = vs->offset; + vs->err = vs->offset; out: -complete_pdu (s, vs->pdu, err); -v9fs_stat_free (&vs->v9stat); -qemu_free (vs); +qemu_rwmutex_unlock (&global_rename_lock); +v9fs_async_helper_done (v9fs_stat_do_complete, vs); }static void v9fs_stat (V9fsState *s,V9fsPDU *pdu) { -int32_t fid;V9fsStatState *vs; -ssize_t err =0 ; vs =qemu_malloc (sizeof (*vs)); vs->pdu = pdu; vs->offset =7 ; + vs->s = s;memset (&vs->v9stat,0 ,sizeof (vs->v9stat)); -pdu_unmarshal (vs->pdu, vs->offset,"d" , &fid); - - vs->fidp =lookup_fid (s, fid); -if (vs->fidp ==NULL ) { - err = -ENOENT ; - goto out; - } +pdu_unmarshal (vs->pdu, vs->offset,"d" , &vs->fid); - err =v9fs_do_lstat (s, &vs->fidp->path, &vs->stbuf); -v9fs_stat_post_lstat (s, vs, err); + vs->work.func = v9fs_stat_worker; +submit_threadlet (&vs->work);return ; - -out: -complete_pdu (s, vs->pdu, err); -v9fs_stat_free (&vs->v9stat); -qemu_free (vs); } static void v9fs_getattr_post_lstat(V9fsState *s, V9fsStatStateDotl *vs,
hw/virtio-9p.h +4 -0
@@ -248,6 +248,10 @@ typedef struct V9fsStatState {V9fsStat v9stat;V9fsFidState *fidp;struct stat stbuf; +V9fsState *s; +int32_t fid; +int32_t err; +ThreadletWork work; } V9fsStatState; typedef struct V9fsStatDotl {
PATCH 5/9 Convert wstat into 2nd threading model
In reply to Arun R BharadwajFrom: Sripathi Kodi <[email protected]>
In this model we hand over the vcpu thread only executes till
the first blocking operation. It then hands over the call to the worker thread, which does everything needed to complete the call. It can make multiple blocking calls. It finally signals the IO thread to do complete_pdu().
Simplification of code due to this thread model is obvious in this patch.
Gautham suggested that I use a generic function to call complete_pdu, which could further simplify the post_op structure. However, some of the calls (stat included) need to free up some memory after calling complete_pdu. Handling this becomes messy if I use a common complete function.
Review comments welcome. Review comment from Gautham is a necessity.
--- hw/virtio-9p.c | 238 +++++++++++++++++++++----------------------------------- hw/virtio-9p.h | 4 + 2 files changed, 94 insertions(+), 148 deletions(-)
hw/virtio-9p.c +90 -148
@@ -2873,45 +2873,13 @@ out:qemu_free (vs); } -static void v9fs_wstat_post_truncate (V9fsState *s,V9fsWstatState *vs,int err) -{ -if (err <0 ) { - goto out; - } - - err = vs->offset; - -out: -v9fs_stat_free (&vs->v9stat); -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} - -static void v9fs_wstat_post_rename (V9fsState *s,V9fsWstatState *vs,int err) -{ -if (err <0 ) { - goto out; - } -if (vs->v9stat.length !=-1 ) { -if (v9fs_do_truncate (s, &vs->fidp->path, vs->v9stat.length) <0 ) { - err = -errno; - } - } -v9fs_wstat_post_truncate (s, vs, err); -return ; - -out: -v9fs_stat_free (&vs->v9stat); -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} -static int v9fs_complete_rename (V9fsState *s,V9fsRenameState *vs) {int err =0 ;char *old_name, *new_name;char *end; +qemu_rwmutex_wrlock (&global_rename_lock);if (vs->newdirfid !=-1 ) {V9fsFidState *dirfidp; dirfidp =lookup_fid (s, vs->newdirfid);
@@ -2955,6 +2923,9 @@ static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs)* Fixup fid's pointing to the old name to * start pointing to the new name */ +/* FIXME: Since we are holding the global rename +* lock in write mode there is no need to hold fid_list_lock +*/ qemu_rwmutex_rdlock (&s->fid_list_lock);for (fidp = s->fid_list; fidp; fidp = fidp->next) {if (vs->fidp == fidp) {
@@ -2976,41 +2947,14 @@ static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs) } } out: +qemu_rwmutex_unlock (&global_rename_lock);v9fs_string_free (&vs->name);return err; }static void v9fs_rename_post_rename (V9fsState *s,V9fsRenameState *vs,int err) { -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} - -static void v9fs_wstat_post_chown (V9fsState *s,V9fsWstatState *vs,int err) -{ -if (err <0 ) { - goto out; - } - -if (vs->v9stat.name.size !=0 ) { -V9fsRenameState *vr; - - vr =qemu_mallocz (sizeof (V9fsRenameState)); - vr->newdirfid =-1 ; - vr->pdu = vs->pdu; - vr->fidp = vs->fidp; - vr->offset = vs->offset; - vr->name.size = vs->v9stat.name.size; - vr->name.data =qemu_strdup (vs->v9stat.name.data); - - err =v9fs_complete_rename (s, vr); -qemu_free (vr); - } -v9fs_wstat_post_rename (s, vs, err); -return ; - -out: -v9fs_stat_free (&vs->v9stat); +/* FIXME: Needed? v9fs_stat_free(&vs->v9stat); */ complete_pdu (s, vs->pdu, err);qemu_free (vs); }
@@ -3043,31 +2987,55 @@ out:qemu_free (vs); } -static void v9fs_wstat_post_utime (V9fsState *s,V9fsWstatState *vs,int err) +static void v9fs_wstat_do_complete (void *opaque) { -if (err <0 ) { +V9fsWstatState *vs = (V9fsWstatState *)opaque; +complete_pdu (vs->s, vs->pdu, vs->err); +v9fs_stat_free (&vs->v9stat); +qemu_free (vs); +} + +static void v9fs_wstat_worker (ThreadletWork *work) +{ +uint32_t v9_mode; +V9fsWstatState *vs =container_of (work, V9fsWstatState, work); + + vs->fidp =lookup_fid (vs->s, vs->fid); +if (vs->fidp ==NULL ) { + vs->err = -EINVAL ; goto out; } -if (vs->v9stat.n_gid !=-1 || vs->v9stat.n_uid !=-1 ) { -if (v9fs_do_chown (s, &vs->fidp->path, vs->v9stat.n_uid, - vs->v9stat.n_gid)) { - err = -errno; +/* do we need to sync the file? */ +if (donttouch_stat (&vs->v9stat)) { + vs->err =v9fs_do_fsync (vs->s, vs->fidp->fs.fd); +if (vs->err ==-1 ) { + vs->err = -errno; + goto out; } } -v9fs_wstat_post_chown (s, vs, err); -return ; -out: -v9fs_stat_free (&vs->v9stat); -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} +if (vs->v9stat.mode !=-1 ) { +qemu_rwmutex_rdlock (&global_rename_lock); + vs->err =v9fs_do_lstat (vs->s, &vs->fidp->path, &vs->stbuf); +if (vs->err ==-1 ) { + vs->err = -errno; + goto out_unlock; + } + v9_mode =stat_to_v9mode (&vs->stbuf); -static void v9fs_wstat_post_chmod (V9fsState *s,V9fsWstatState *vs,int err) -{ -if (err <0 ) { - goto out; +if ((vs->v9stat.mode &P9_STAT_MODE_TYPE_BITS ) != + (v9_mode &P9_STAT_MODE_TYPE_BITS )) { +/* Attempting to change the type */ + vs->err = -EIO ; + goto out_unlock; + } +if (v9fs_do_chmod (vs->s, &vs->fidp->path,v9mode_to_mode (vs->v9stat.mode, + &vs->v9stat.extension))) { + vs->err = -errno; + goto out_unlock; + } +qemu_rwmutex_unlock (&global_rename_lock); }if (vs->v9stat.mtime !=-1 || vs->v9stat.atime !=-1 ) {
@@ -3085,99 +3053,73 @@ static void v9fs_wstat_post_chmod(V9fsState *s, V9fsWstatState *vs, int err) times[1 ].tv_nsec =UTIME_OMIT ; } -if (v9fs_do_utimensat (s, &vs->fidp->path, times)) { - err = -errno; +qemu_rwmutex_rdlock (&global_rename_lock); +if (v9fs_do_utimensat (vs->s, &vs->fidp->path, times)) { + vs->err = -errno; + goto out_unlock; } +qemu_rwmutex_unlock (&global_rename_lock); } -v9fs_wstat_post_utime (s, vs, err); -return ; - -out: -v9fs_stat_free (&vs->v9stat); -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} - -static void v9fs_wstat_post_fsync (V9fsState *s,V9fsWstatState *vs,int err) -{ -if (err ==-1 ) { - err = -errno; +if (vs->v9stat.n_gid !=-1 || vs->v9stat.n_uid !=-1 ) { +qemu_rwmutex_rdlock (&global_rename_lock); +if (v9fs_do_chown (vs->s, &vs->fidp->path, vs->v9stat.n_uid, + vs->v9stat.n_gid)) { + vs->err = -errno; + goto out_unlock; + } +qemu_rwmutex_unlock (&global_rename_lock); } -v9fs_stat_free (&vs->v9stat); -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} -static void v9fs_wstat_post_lstat (V9fsState *s,V9fsWstatState *vs,int err) -{ -uint32_t v9_mode; - -if (err ==-1 ) { - err = -errno; - goto out; - } +if (vs->v9stat.name.size !=0 ) { +V9fsRenameState *vr; - v9_mode =stat_to_v9mode (&vs->stbuf); + vr =qemu_mallocz (sizeof (V9fsRenameState)); + vr->newdirfid=-1 ; + vr->pdu = vs->pdu; + vr->fidp = vs->fidp; + vr->offset = vs->offset; + vr->name.size = vs->v9stat.name.size; + vr->name.data =qemu_strdup (vs->v9stat.name.data); -if ((vs->v9stat.mode &P9_STAT_MODE_TYPE_BITS ) != - (v9_mode &P9_STAT_MODE_TYPE_BITS )) { -/* Attempting to change the type */ - err = -EIO ; + vs->err =v9fs_complete_rename (vs->s, vr); +qemu_free (vr); +if (vs->err <0 ) { goto out; + } } -if (v9fs_do_chmod (s, &vs->fidp->path,v9mode_to_mode (vs->v9stat.mode, - &vs->v9stat.extension))) { - err = -errno; - } -v9fs_wstat_post_chmod (s, vs, err); -return ; +if (vs->v9stat.length !=-1 ) { +qemu_rwmutex_rdlock (&global_rename_lock); +if (v9fs_do_truncate (vs->s, &vs->fidp->path, vs->v9stat.length) <0 ) { + vs->err = -errno; + goto out_unlock; + } +qemu_rwmutex_unlock (&global_rename_lock); + } + + goto out; +out_unlock: +qemu_rwmutex_unlock (&global_rename_lock); out: -v9fs_stat_free (&vs->v9stat); -complete_pdu (s, vs->pdu, err); -qemu_free (vs); +v9fs_async_helper_done (v9fs_wstat_do_complete, vs); }static void v9fs_wstat (V9fsState *s,V9fsPDU *pdu) { -int32_t fid;V9fsWstatState *vs; -int err =0 ; vs =qemu_malloc (sizeof (*vs)); vs->pdu = pdu; vs->offset =7 ; + vs->s = s; -pdu_unmarshal (pdu, vs->offset,"dwS" , &fid, &vs->unused, &vs->v9stat); - - vs->fidp =lookup_fid (s, fid); -if (vs->fidp ==NULL ) { - err = -EINVAL ; - goto out; - } - -/* do we need to sync the file? */ -if (donttouch_stat (&vs->v9stat)) { - err =v9fs_do_fsync (s, vs->fidp->fs.fd); -v9fs_wstat_post_fsync (s, vs, err); -return ; - } +pdu_unmarshal (pdu, vs->offset,"dwS" , &vs->fid, &vs->unused, &vs->v9stat); -if (vs->v9stat.mode !=-1 ) { - err =v9fs_do_lstat (s, &vs->fidp->path, &vs->stbuf); -v9fs_wstat_post_lstat (s, vs, err); -return ; - } - -v9fs_wstat_post_chmod (s, vs, err); + vs->work.func = v9fs_wstat_worker; +submit_threadlet (&vs->work);return ; - -out: -v9fs_stat_free (&vs->v9stat); -complete_pdu (s, vs->pdu, err); -qemu_free (vs); }static void v9fs_statfs_post_statfs (V9fsState *s,V9fsStatfsState *vs,int err)
hw/virtio-9p.h +4 -0
@@ -354,6 +354,10 @@ typedef struct V9fsWstatStateV9fsStat v9stat;V9fsFidState *fidp;struct stat stbuf; +V9fsState *s; +int32_t fid; +int32_t err; +ThreadletWork work; } V9fsWstatState; typedef struct V9fsSymlinkState
PATCH 6/9 Convert open into 2nd threading model
In reply to Arun R BharadwajFrom: Sripathi Kodi <[email protected]>
--- hw/virtio-9p.c | 115 +++++++++++++++++++++++--------------------------------- hw/virtio-9p.h | 4 ++ 2 files changed, 51 insertions(+), 68 deletions(-)
hw/virtio-9p.c +47 -68
@@ -1797,6 +1797,13 @@ out:v9fs_walk_complete (s, vs, err); } +static void v9fs_open_do_complete (void *opaque) +{ +V9fsOpenState *vs = (V9fsOpenState *)opaque; +complete_pdu (vs->s, vs->pdu, vs->err); +qemu_free (vs); +} +static int32_t get_iounit (V9fsState *s,V9fsString *name) {struct statfs stbuf;
@@ -1817,61 +1824,37 @@ static int32_t get_iounit(V9fsState *s, V9fsString *name)return iounit; } -static void v9fs_open_post_opendir (V9fsState *s,V9fsOpenState *vs,int err) -{ -if (vs->fidp->fs.dir ==NULL ) { - err = -errno; - goto out; - } - vs->fidp->fid_type =P9_FID_DIR ; - vs->offset +=pdu_marshal (vs->pdu, vs->offset,"Qd" , &vs->qid,0 ); - err = vs->offset; -out: -complete_pdu (s, vs->pdu, err); -qemu_free (vs); - -} - -static void v9fs_open_post_getiounit (V9fsState *s,V9fsOpenState *vs) -{ -int err; - vs->offset +=pdu_marshal (vs->pdu, vs->offset,"Qd" , &vs->qid, vs->iounit); - err = vs->offset; -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} - -static void v9fs_open_post_open (V9fsState *s,V9fsOpenState *vs,int err) +static void v9fs_open_worker (ThreadletWork *work) { -if (vs->fidp->fs.fd ==-1 ) { - err = -errno; +int flags; +V9fsOpenState *vs =container_of (work, V9fsOpenState, work); + vs->fidp =lookup_fid (vs->s, vs->fid); +if (vs->fidp ==NULL ) { + vs->err = -ENOENT ; goto out; } - vs->fidp->fid_type =P9_FID_FILE ; - vs->iounit =get_iounit (s, &vs->fidp->path); -v9fs_open_post_getiounit (s, vs); -return ; -out: -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} - -static void v9fs_open_post_lstat (V9fsState *s,V9fsOpenState *vs,int err) -{ -int flags; - -if (err) { - err = -errno; +BUG_ON (vs->fidp->fid_type !=P9_FID_NONE ); +qemu_rwmutex_rdlock (&global_rename_lock); + vs->err =v9fs_do_lstat (vs->s, &vs->fidp->path, &vs->stbuf); +if (vs->err) { + vs->err = -errno; goto out; }stat_to_qid (&vs->stbuf, &vs->qid);if (S_ISDIR (vs->stbuf.st_mode)) { - vs->fidp->fs.dir =v9fs_do_opendir (s, &vs->fidp->path); -v9fs_open_post_opendir (s, vs, err); + vs->fidp->fs.dir =v9fs_do_opendir (vs->s, &vs->fidp->path); + +if (vs->fidp->fs.dir ==NULL ) { + vs->err = -errno; + goto out; + } + vs->fidp->fid_type =P9_FID_DIR ; + vs->offset +=pdu_marshal (vs->pdu, vs->offset,"Qd" , &vs->qid,0 ); + vs->err = vs->offset; }else { -if (s->proto_version ==V9FS_PROTO_2000L ) { +if (vs->s->proto_version ==V9FS_PROTO_2000L ) { flags = vs->mode; flags &= ~(O_NOCTTY |O_ASYNC |O_CREAT );/* Ignore direct disk access hint until the server supports it. */
@@ -1879,47 +1862,43 @@ static void v9fs_open_post_lstat(V9fsState *s, V9fsOpenState *vs, int err) }else { flags =omode_to_uflags (vs->mode); } - vs->fidp->fs.fd =v9fs_do_open (s, &vs->fidp->path, flags); -v9fs_open_post_open (s, vs, err); + vs->fidp->fs.fd =v9fs_do_open (vs->s, &vs->fidp->path, flags); + +if (vs->fidp->fs.fd ==-1 ) { + vs->err = -errno; + goto out; + } + vs->fidp->fid_type =P9_FID_FILE ; + vs->iounit =get_iounit (vs->s, &vs->fidp->path); + vs->offset +=pdu_marshal (vs->pdu, vs->offset,"Qd" , &vs->qid, + vs->iounit); + vs->err = vs->offset; } -return ; out: -complete_pdu (s, vs->pdu, err); -qemu_free (vs); +qemu_rwmutex_unlock (&global_rename_lock); +v9fs_async_helper_done (v9fs_open_do_complete, vs); }static void v9fs_open (V9fsState *s,V9fsPDU *pdu) { -int32_t fid;V9fsOpenState *vs; -ssize_t err =0 ; vs =qemu_malloc (sizeof (*vs)); vs->pdu = pdu; vs->offset =7 ; vs->mode =0 ; + vs->err =0 ; + vs->s = s;if (s->proto_version ==V9FS_PROTO_2000L ) { -pdu_unmarshal (vs->pdu, vs->offset,"dd" , &fid, &vs->mode); +pdu_unmarshal (vs->pdu, vs->offset,"dd" , &vs->fid, &vs->mode); }else { -pdu_unmarshal (vs->pdu, vs->offset,"db" , &fid, &vs->mode); +pdu_unmarshal (vs->pdu, vs->offset,"db" , &vs->fid, &vs->mode); } - vs->fidp =lookup_fid (s, fid); -if (vs->fidp ==NULL ) { - err = -ENOENT ; - goto out; - } - -BUG_ON (vs->fidp->fid_type !=P9_FID_NONE ); - - err =v9fs_do_lstat (s, &vs->fidp->path, &vs->stbuf); - -v9fs_open_post_lstat (s, vs, err); + vs->work.func = v9fs_open_worker; +submit_threadlet (&vs->work);return ; -out: -complete_pdu (s, pdu, err); -qemu_free (vs); }static void v9fs_post_lcreate (V9fsState *s,V9fsLcreateState *vs,int err)
hw/virtio-9p.h +4 -0
@@ -306,6 +306,10 @@ typedef struct V9fsOpenState {V9fsQID qid;struct stat stbuf;int iounit; +V9fsState *s; +int32_t fid; +int32_t err; +ThreadletWork work; } V9fsOpenState; typedef struct V9fsReadState {
PATCH 7/9 Convert walk into 2nd threading model
In reply to Arun R BharadwajFrom: Sripathi Kodi <[email protected]>
--- hw/virtio-9p.c | 161 ++++++++++++++++++++++---------------------------------- hw/virtio-9p.h | 5 ++ 2 files changed, 68 insertions(+), 98 deletions(-)
hw/virtio-9p.c +63 -98
@@ -1635,9 +1635,10 @@ out:qemu_free (vs); } -static void v9fs_walk_complete (V9fsState *s,V9fsWalkState *vs,int err) +static void v9fs_walk_do_complete (void *opaque) { -complete_pdu (s, vs->pdu, err); +V9fsWalkState *vs = (V9fsWalkState *)opaque; +complete_pdu (vs->s, vs->pdu, vs->err);if (vs->nwnames) {for (vs->name_idx =0 ; vs->name_idx < vs->nwnames; vs->name_idx++) {
@@ -1647,6 +1648,7 @@ static void v9fs_walk_complete(V9fsState *s, V9fsWalkState *vs, int err)qemu_free (vs->wnames);qemu_free (vs->qids); } +qemu_free (vs); }static void v9fs_walk_marshal (V9fsWalkState *vs)
@@ -1660,70 +1662,78 @@ static void v9fs_walk_marshal(V9fsWalkState *vs) } } -static void v9fs_walk_post_newfid_lstat (V9fsState *s,V9fsWalkState *vs, -int err) +static void v9fs_walk_worker (ThreadletWork *work) { -if (err ==-1 ) { -free_fid (s, vs->newfidp->fid); -v9fs_string_free (&vs->path); - err = -ENOENT ; +V9fsWalkState *vs =container_of (work, V9fsWalkState, work); + + vs->fidp =lookup_fid (vs->s, vs->fid); +if (vs->fidp ==NULL ) { + vs->err = -ENOENT ; goto out; } -stat_to_qid (&vs->stbuf, &vs->qids[vs->name_idx]); - - vs->name_idx++; -if (vs->name_idx < vs->nwnames) { -v9fs_string_sprintf (&vs->path,"%s/%s" , vs->newfidp->path.data, - vs->wnames[vs->name_idx].data); -v9fs_string_copy (&vs->newfidp->path, &vs->path); +qemu_rwmutex_rdlock (&global_rename_lock); +/* FIXME: is this really valid? */ +if (vs->fid == vs->newfid) { +BUG_ON (vs->fidp->fid_type !=P9_FID_NONE ); +v9fs_string_init (&vs->path); + vs->name_idx =0 ; - err =v9fs_do_lstat (s, &vs->newfidp->path, &vs->stbuf); -v9fs_walk_post_newfid_lstat (s, vs, err); -return ; - } +while (vs->name_idx < vs->nwnames) { +v9fs_string_sprintf (&vs->path,"%s/%s" , + vs->fidp->path.data, vs->wnames[vs->name_idx].data); +v9fs_string_copy (&vs->fidp->path, &vs->path); -v9fs_string_free (&vs->path); -v9fs_walk_marshal (vs); - err = vs->offset; -out: -v9fs_walk_complete (s, vs, err); -} + vs->err =v9fs_do_lstat (vs->s, &vs->fidp->path, &vs->stbuf); +if (vs->err ==-1 ) { +v9fs_string_free (&vs->path); + vs->err = -ENOENT ; + goto out; + } +stat_to_qid (&vs->stbuf, &vs->qids[vs->name_idx]); + vs->name_idx++; + } + }else { + vs->newfidp =alloc_fid (vs->s, vs->newfid); +if (vs->newfidp ==NULL ) { + vs->err = -EINVAL ; + goto out; + } -static void v9fs_walk_post_oldfid_lstat (V9fsState *s,V9fsWalkState *vs, -int err) -{ -if (err ==-1 ) { -v9fs_string_free (&vs->path); - err = -ENOENT ; - goto out; - } + vs->newfidp->uid = vs->fidp->uid; +v9fs_string_init (&vs->path); + vs->name_idx =0 ; +v9fs_string_copy (&vs->newfidp->path, &vs->fidp->path); -stat_to_qid (&vs->stbuf, &vs->qids[vs->name_idx]); - vs->name_idx++; -if (vs->name_idx < vs->nwnames) { +while (vs->name_idx < vs->nwnames) { +v9fs_string_sprintf (&vs->path,"%s/%s" , vs->newfidp->path.data, + vs->wnames[vs->name_idx].data); +v9fs_string_copy (&vs->newfidp->path, &vs->path); -v9fs_string_sprintf (&vs->path,"%s/%s" , - vs->fidp->path.data, vs->wnames[vs->name_idx].data); -v9fs_string_copy (&vs->fidp->path, &vs->path); + vs->err =v9fs_do_lstat (vs->s, &vs->newfidp->path, &vs->stbuf); - err =v9fs_do_lstat (s, &vs->fidp->path, &vs->stbuf); -v9fs_walk_post_oldfid_lstat (s, vs, err); -return ; +if (vs->err ==-1 ) { +free_fid (vs->s, vs->newfidp->fid); +v9fs_string_free (&vs->path); + vs->err = -ENOENT ; + goto out; + } +stat_to_qid (&vs->stbuf, &vs->qids[vs->name_idx]); + vs->name_idx++; + } } -v9fs_string_free (&vs->path);v9fs_walk_marshal (vs); - err = vs->offset; + vs->err = vs->offset; out: -v9fs_walk_complete (s, vs, err); +qemu_rwmutex_unlock (&global_rename_lock); +v9fs_async_helper_done (v9fs_walk_do_complete, vs); } +static void v9fs_walk (V9fsState *s,V9fsPDU *pdu) { -int32_t fid, newfid;V9fsWalkState *vs; -int err =0 ;int i; vs =qemu_malloc (sizeof (*vs));
@@ -1731,9 +1741,10 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) vs->wnames =NULL ; vs->qids =NULL ; vs->offset =7 ; + vs->s = s; - vs->offset +=pdu_unmarshal (vs->pdu, vs->offset,"ddw" , &fid, - &newfid, &vs->nwnames); + vs->offset +=pdu_unmarshal (vs->pdu, vs->offset,"ddw" , &vs->fid, + &vs->newfid, &vs->nwnames);if (vs->nwnames) { vs->wnames =qemu_mallocz (sizeof (vs->wnames[0 ]) * vs->nwnames);
@@ -1746,55 +1757,9 @@ static void v9fs_walk(V9fsState *s, V9fsPDU *pdu) } } - vs->fidp =lookup_fid (s, fid); -if (vs->fidp ==NULL ) { - err = -ENOENT ; - goto out; - } - -/* FIXME: is this really valid? */ -if (fid == newfid) { - -BUG_ON (vs->fidp->fid_type !=P9_FID_NONE ); -v9fs_string_init (&vs->path); - vs->name_idx =0 ; - -if (vs->name_idx < vs->nwnames) { -v9fs_string_sprintf (&vs->path,"%s/%s" , - vs->fidp->path.data, vs->wnames[vs->name_idx].data); -v9fs_string_copy (&vs->fidp->path, &vs->path); - - err =v9fs_do_lstat (s, &vs->fidp->path, &vs->stbuf); -v9fs_walk_post_oldfid_lstat (s, vs, err); -return ; - } - }else { - vs->newfidp =alloc_fid (s, newfid); -if (vs->newfidp ==NULL ) { - err = -EINVAL ; - goto out; - } - - vs->newfidp->uid = vs->fidp->uid; -v9fs_string_init (&vs->path); - vs->name_idx =0 ; -v9fs_string_copy (&vs->newfidp->path, &vs->fidp->path); - -if (vs->name_idx < vs->nwnames) { -v9fs_string_sprintf (&vs->path,"%s/%s" , vs->newfidp->path.data, - vs->wnames[vs->name_idx].data); -v9fs_string_copy (&vs->newfidp->path, &vs->path); - - err =v9fs_do_lstat (s, &vs->newfidp->path, &vs->stbuf); -v9fs_walk_post_newfid_lstat (s, vs, err); -return ; - } - } - -v9fs_walk_marshal (vs); - err = vs->offset; -out: -v9fs_walk_complete (s, vs, err); + vs->work.func = v9fs_walk_worker; +submit_threadlet (&vs->work); +return ; }static void v9fs_open_do_complete (void *opaque)
hw/virtio-9p.h +5 -0
@@ -296,6 +296,11 @@ typedef struct V9fsWalkState {V9fsString path;V9fsString *wnames;struct stat stbuf; +V9fsState *s; +int32_t fid; +int32_t newfid; +int32_t err; +ThreadletWork work; } V9fsWalkState; typedef struct V9fsOpenState {
PATCH 8/9 Convert read into 2nd threading model
In reply to Arun R BharadwajFrom: Sripathi Kodi <[email protected]>
--- hw/virtio-9p.c | 253 +++++++++++++++++++------------------------------------- hw/virtio-9p.h | 4 + 2 files changed, 91 insertions(+), 166 deletions(-)
hw/virtio-9p.c +87 -166
@@ -2001,158 +2001,112 @@ out:complete_pdu (s, pdu, err); } -static void v9fs_read_post_readdir (V9fsState *,V9fsReadState *,ssize_t ); - -static void v9fs_read_post_seekdir (V9fsState *s,V9fsReadState *vs,ssize_t err) -{ -if (err) { - goto out; - } -v9fs_stat_free (&vs->v9stat); -v9fs_string_free (&vs->name); - vs->offset +=pdu_marshal (vs->pdu, vs->offset,"d" , vs->count); - vs->offset += vs->count; - err = vs->offset; -out: -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -return ; -} - -static void v9fs_read_post_dir_lstat (V9fsState *s,V9fsReadState *vs, -ssize_t err) -{ -if (err) { - err = -errno; - goto out; - } - err =stat_to_v9stat (s, &vs->name, &vs->stbuf, &vs->v9stat); -if (err) { - goto out; - } - - vs->len =pdu_marshal (vs->pdu, vs->offset +4 + vs->count,"S" , - &vs->v9stat); -if ((vs->len != (vs->v9stat.size +2 )) || - ((vs->count + vs->len) > vs->max_count)) { -v9fs_do_seekdir (s, vs->fidp->fs.dir, vs->dir_pos); -v9fs_read_post_seekdir (s, vs, err); -return ; - } - vs->count += vs->len; -v9fs_stat_free (&vs->v9stat); -v9fs_string_free (&vs->name); - vs->dir_pos = vs->dent->d_off; - vs->dent =v9fs_do_readdir (s, vs->fidp->fs.dir); -v9fs_read_post_readdir (s, vs, err); -return ; -out: -v9fs_do_seekdir (s, vs->fidp->fs.dir, vs->dir_pos); -v9fs_read_post_seekdir (s, vs, err); -return ; - -} - -static void v9fs_read_post_readdir (V9fsState *s,V9fsReadState *vs,ssize_t err) +static void v9fs_read_do_complete (void *opaque) { -if (vs->dent) { -memset (&vs->v9stat,0 ,sizeof (vs->v9stat)); -v9fs_string_init (&vs->name); -v9fs_string_sprintf (&vs->name,"%s/%s" , vs->fidp->path.data, - vs->dent->d_name); - err =v9fs_do_lstat (s, &vs->name, &vs->stbuf); -v9fs_read_post_dir_lstat (s, vs, err); -return ; - } - - vs->offset +=pdu_marshal (vs->pdu, vs->offset,"d" , vs->count); - vs->offset += vs->count; - err = vs->offset; -complete_pdu (s, vs->pdu, err); +V9fsReadState *vs = (V9fsReadState *)opaque; +complete_pdu (vs->s, vs->pdu, vs->err);qemu_free (vs); -return ; -} - -static void v9fs_read_post_telldir (V9fsState *s,V9fsReadState *vs,ssize_t err) -{ - vs->dent =v9fs_do_readdir (s, vs->fidp->fs.dir); -v9fs_read_post_readdir (s, vs, err); -return ; -} - -static void v9fs_read_post_rewinddir (V9fsState *s,V9fsReadState *vs, -ssize_t err) -{ - vs->dir_pos =v9fs_do_telldir (s, vs->fidp->fs.dir); -v9fs_read_post_telldir (s, vs, err); -return ; } -static void v9fs_read_post_preadv (V9fsState *s,V9fsReadState *vs,ssize_t err) +static void v9fs_read_worker (ThreadletWork *work) { -if (err <0 ) { -/* IO error return the error */ - err = -errno; +V9fsReadState *vs =container_of (work, V9fsReadState, work); + vs->fidp =lookup_fid (vs->s, vs->fid); +if (vs->fidp ==NULL ) { + vs->err = -EINVAL ; goto out; } - vs->total += vs->len; - vs->sg =adjust_sg (vs->sg, vs->len, &vs->cnt); -if (vs->total < vs->count && vs->len >0 ) { -do { -if (0 ) { -print_sg (vs->sg, vs->cnt); +if (vs->fidp->fid_type ==P9_FID_DIR ) { +qemu_rwmutex_rdlock (&global_rename_lock); + vs->max_count = vs->count; + vs->count =0 ; +if (vs->off ==0 ) { +v9fs_do_rewinddir (vs->s, vs->fidp->fs.dir); + } + vs->dir_pos =v9fs_do_telldir (vs->s, vs->fidp->fs.dir); + vs->dent =v9fs_do_readdir (vs->s, vs->fidp->fs.dir); +while (vs->dent) { +memset (&vs->v9stat,0 ,sizeof (vs->v9stat)); +v9fs_string_init (&vs->name); +v9fs_string_sprintf (&vs->name,"%s/%s" , vs->fidp->path.data, + vs->dent->d_name); + vs->err =v9fs_do_lstat (vs->s, &vs->name, &vs->stbuf); +if (vs->err) { + vs->err = -errno; +v9fs_do_seekdir (vs->s, vs->fidp->fs.dir, vs->dir_pos); +/* FIXME: Is there memory leak here? */ +qemu_rwmutex_unlock (&global_rename_lock); + goto out; } + vs->err =stat_to_v9stat (vs->s, &vs->name, &vs->stbuf, &vs->v9stat); +if (vs->err) { +v9fs_do_seekdir (vs->s, vs->fidp->fs.dir, vs->dir_pos); +/* FIXME: Is there memory leak here? */ +qemu_rwmutex_unlock (&global_rename_lock); + goto out; + } + vs->len =pdu_marshal (vs->pdu, vs->offset +4 + vs->count,"S" , + &vs->v9stat); +if ((vs->len != (vs->v9stat.size +2 )) || + ((vs->count + vs->len) > vs->max_count)) { +v9fs_do_seekdir (vs->s, vs->fidp->fs.dir, vs->dir_pos); +v9fs_stat_free (&vs->v9stat); +v9fs_string_free (&vs->name); +break ; + } + vs->count += vs->len; +v9fs_stat_free (&vs->v9stat); +v9fs_string_free (&vs->name); + vs->dir_pos = vs->dent->d_off; + vs->dent =v9fs_do_readdir (vs->s, vs->fidp->fs.dir); + } + vs->offset +=pdu_marshal (vs->pdu, vs->offset,"d" , vs->count); + vs->offset += vs->count; + vs->err = vs->offset; +qemu_rwmutex_unlock (&global_rename_lock); + }else if (vs->fidp->fid_type ==P9_FID_FILE ) { + vs->sg = vs->iov; +/* FIXME: Is it safe to call pdu_marshal from async thread context? */ +pdu_marshal (vs->pdu, vs->offset +4 ,"v" , vs->sg, &vs->cnt); + vs->sg =cap_sg (vs->sg, vs->count, &vs->cnt); +if (vs->total <= vs->count) { vs->len =v9fs_do_preadv (s, vs->fidp->fs.fd, vs->sg, vs->cnt, - vs->off); + vs->off);if (vs->len >0 ) { vs->off += vs->len; } - }while (vs->len ==-1 && errno ==EINTR ); -if (vs->len ==-1 ) { - err = -errno; + err = vs->len; } -v9fs_read_post_preadv (s, vs, err); -return ; - } - vs->offset +=pdu_marshal (vs->pdu, vs->offset,"d" , vs->total); - vs->offset += vs->count; - err = vs->offset; - -out: -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} - -static void v9fs_xattr_read (V9fsState *s,V9fsReadState *vs) -{ -ssize_t err =0 ; -int read_count; -int64_t xattr_len; - - xattr_len = vs->fidp->fs.xattr.len; - read_count = xattr_len - vs->off; -if (read_count > vs->count) { - read_count = vs->count; - }else if (read_count <0 ) { + }else if (vs->fidp->fid_type ==P9_FID_XATTR ) { +int read_count; +int64_t xattr_len; + + xattr_len = vs->fidp->fs.xattr.len; + read_count = xattr_len - vs->off; +if (read_count > vs->count) { + read_count = vs->count; + }else if (read_count <0 ) {/* * read beyond XATTR value */ read_count =0 ; - } - vs->offset +=pdu_marshal (vs->pdu, vs->offset,"d" , read_count); - vs->offset +=pdu_pack (vs->pdu, vs->offset, + } + vs->offset +=pdu_marshal (vs->pdu, vs->offset,"d" , read_count); + vs->offset +=pdu_pack (vs->pdu, vs->offset, ((char *)vs->fidp->fs.xattr.value) + vs->off, read_count); - err = vs->offset; -complete_pdu (s, vs->pdu, err); -qemu_free (vs); + vs->err = vs->offset; + }else { + vs->err = -EINVAL ; + } +out: +v9fs_async_helper_done (v9fs_read_do_complete, vs); +return ; }static void v9fs_read (V9fsState *s,V9fsPDU *pdu) { -int32_t fid;V9fsReadState *vs; -ssize_t err =0 ; vs =qemu_malloc (sizeof (*vs)); vs->pdu = pdu;
@@ -2160,46 +2114,13 @@ static void v9fs_read(V9fsState *s, V9fsPDU *pdu) vs->total =0 ; vs->len =0 ; vs->count =0 ; + vs->s = s; -pdu_unmarshal (vs->pdu, vs->offset,"dqd" , &fid, &vs->off, &vs->count); - - vs->fidp =lookup_fid (s, fid); -if (vs->fidp ==NULL ) { - err = -EINVAL ; - goto out; - } +pdu_unmarshal (vs->pdu, vs->offset,"dqd" , &vs->fid, &vs->off, &vs->count); -if (vs->fidp->fid_type ==P9_FID_DIR ) { - vs->max_count = vs->count; - vs->count =0 ; -if (vs->off ==0 ) { -v9fs_do_rewinddir (s, vs->fidp->fs.dir); - } -v9fs_read_post_rewinddir (s, vs, err); -return ; - }else if (vs->fidp->fid_type ==P9_FID_FILE ) { - vs->sg = vs->iov; -pdu_marshal (vs->pdu, vs->offset +4 ,"v" , vs->sg, &vs->cnt); - vs->sg =cap_sg (vs->sg, vs->count, &vs->cnt); -if (vs->total <= vs->count) { - vs->len =v9fs_do_preadv (s, vs->fidp->fs.fd, vs->sg, vs->cnt, - vs->off); -if (vs->len >0 ) { - vs->off += vs->len; - } - err = vs->len; -v9fs_read_post_preadv (s, vs, err); - } -return ; - }else if (vs->fidp->fid_type ==P9_FID_XATTR ) { -v9fs_xattr_read (s, vs); -return ; - }else { - err = -EINVAL ; - } -out: -complete_pdu (s, pdu, err); -qemu_free (vs); + vs->work.func = v9fs_read_worker; +submit_threadlet (&vs->work); +return ; } typedef struct V9fsReadDirState {
hw/virtio-9p.h +4 -0
@@ -334,6 +334,10 @@ typedef struct V9fsReadState {int32_t len;int32_t cnt;int32_t max_count; +V9fsState *s; +int32_t fid; +int32_t err; +ThreadletWork work; } V9fsReadState; typedef struct V9fsWriteState {
PATCH 9/9 Convert write into 2nd threading model.
In reply to Arun R BharadwajFrom: Sripathi Kodi <[email protected]>
--- hw/virtio-9p.c | 92 +++++++++++++++++++++----------------------------------- hw/virtio-9p.h | 4 ++ 2 files changed, 39 insertions(+), 57 deletions(-)
hw/virtio-9p.c +35 -57
@@ -2244,40 +2244,6 @@ out:return ; } -static void v9fs_write_post_pwritev (V9fsState *s,V9fsWriteState *vs, -ssize_t err) -{ -if (err <0 ) { -/* IO error return the error */ - err = -errno; - goto out; - } - vs->total += vs->len; - vs->sg =adjust_sg (vs->sg, vs->len, &vs->cnt); -if (vs->total < vs->count && vs->len >0 ) { -do { -if (0 ) { -print_sg (vs->sg, vs->cnt); - } - vs->len =v9fs_do_pwritev (s, vs->fidp->fs.fd, vs->sg, vs->cnt, - vs->off); -if (vs->len >0 ) { - vs->off += vs->len; - } - }while (vs->len ==-1 && errno ==EINTR ); -if (vs->len ==-1 ) { - err = -errno; - } -v9fs_write_post_pwritev (s, vs, err); -return ; - } - vs->offset +=pdu_marshal (vs->pdu, vs->offset,"d" , vs->total); - err = vs->offset; -out: -complete_pdu (s, vs->pdu, err); -qemu_free (vs); -} -static void v9fs_xattr_write (V9fsState *s,V9fsWriteState *vs) {int i, to_copy;
@@ -2320,32 +2286,26 @@ out:qemu_free (vs); } -static void v9fs_write (V9fsState *s,V9fsPDU *pdu) +static void v9fs_write_do_complete (void *opaque) { -int32_t fid; -V9fsWriteState *vs; -ssize_t err; - - vs =qemu_malloc (sizeof (*vs)); +V9fsWriteState *vs = (V9fsWriteState *)opaque; - vs->pdu = pdu; - vs->offset =7 ; - vs->sg = vs->iov; - vs->total =0 ; - vs->len =0 ; - -pdu_unmarshal (vs->pdu, vs->offset,"dqdv" , &fid, &vs->off, &vs->count, - vs->sg, &vs->cnt); +complete_pdu (vs->s, vs->pdu, vs->err); +qemu_free (vs); +} - vs->fidp =lookup_fid (s, fid); +static void v9fs_write_worker (ThreadletWork *work) +{ +V9fsWriteState *vs =container_of (work, V9fsWriteState, work); + vs->fidp =lookup_fid (vs->s, vs->fid);if (vs->fidp ==NULL ) { - err = -EINVAL ; + vs->err = -EINVAL ; goto out; }if (vs->fidp->fid_type ==P9_FID_FILE ) {if (vs->fidp->fs.fd ==-1 ) { - err = -EINVAL ; + vs->err = -EINVAL ; goto out; } }else if (vs->fidp->fid_type ==P9_FID_XATTR ) {
@@ -2355,7 +2315,7 @@ static void v9fs_write(V9fsState *s, V9fsPDU *pdu)v9fs_xattr_write (s, vs);return ; }else { - err = -EINVAL ; + vs->err = -EINVAL ; goto out; } vs->sg =cap_sg (vs->sg, vs->count, &vs->cnt);
@@ -2364,13 +2324,31 @@ static void v9fs_write(V9fsState *s, V9fsPDU *pdu)if (vs->len >0 ) { vs->off += vs->len; } - err = vs->len; -v9fs_write_post_pwritev (s, vs, err); + vs->err = vs->len; } -return ; out: -complete_pdu (s, vs->pdu, err); -qemu_free (vs); +v9fs_async_helper_done (v9fs_write_do_complete, vs); +} + +static void v9fs_write (V9fsState *s,V9fsPDU *pdu) +{ +V9fsWriteState *vs; + + vs =qemu_malloc (sizeof (*vs)); + + vs->pdu = pdu; + vs->offset =7 ; + vs->sg = vs->iov; + vs->total =0 ; + vs->len =0 ; + vs->s = s; + +pdu_unmarshal (vs->pdu, vs->offset,"dqdv" , &vs->fid, &vs->off, &vs->count, + vs->sg, &vs->cnt); + + vs->work.func = v9fs_write_worker; +submit_threadlet (&vs->work); +return ; }static void v9fs_create_post_getiounit (V9fsState *s,V9fsCreateState *vs)
hw/virtio-9p.h +4 -0
@@ -351,6 +351,10 @@ typedef struct V9fsWriteState {struct iovec iov[128 ];/* FIXME: bad, bad, bad */ struct iovec *sg;int cnt; +V9fsState *s; +int32_t fid; +int32_t err; +ThreadletWork work; } V9fsWriteState; typedef struct V9fsRemoveState {