kvm tools: Process virito blk requests in separate thread
PATCH 1/1 kvm tools: Process virito blk requests in separate thread
Currently, all blk requests are processed in notify_vq() which is in the context of ioeventfd thread: ioeventfd__thread(). The processing in notify_vq() may take a long time to complete.
We should make notify_vq() return as soon as possible, since all devices are sharing the single ioeventfd thread. Otherwise, it will block other device's notify_vq() being called and starve other devices.
In virtio net's notify_vq(), we simply signal the tx/rx handle thread and return.
This patch makes virtio blk's notify_vq() just notify the blk thread instead of doing the real hard read/write work. Tests show that the overhead of the notification operations introduced by this patch is small.
--- tools/kvm/virtio/blk.c | 26 +++++++++++++++++++++++++- 1 files changed, 25 insertions(+), 1 deletions(-)
tools/kvm/virtio/blk.c +25 -1
@@ -51,6 +51,11 @@ struct blk_dev {struct virt_queue vqs[NUM_VIRT_QUEUES ];struct blk_dev_req reqs[VIRTIO_BLK_QUEUE_SIZE ]; + +pthread_t io_thread; +int io_efd; + +struct kvm *kvm; };static LIST_HEAD (bdevs);
@@ -176,11 +181,26 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 pfn)return 0 ; } +static void *virtio_blk_thread (void *dev) +{ +struct blk_dev *bdev = dev; +u64 data; + +while (1 ) { +read (bdev->io_efd, &data,sizeof (u64)); +virtio_blk_do_io (bdev->kvm, &bdev->vqs[0 ], bdev); + } + +pthread_exit (NULL ); +return NULL ; +} +static int notify_vq (struct kvm *kvm,void *dev,u32 vq) {struct blk_dev *bdev = dev; +u64 data =1 ; -virtio_blk_do_io (kvm, &bdev->vqs[vq], bdev); +write (bdev->io_efd, &data,sizeof (data));return 0 ; }
@@ -228,6 +248,8 @@ void virtio_blk__init(struct kvm *kvm, struct disk_image *disk) .capacity = disk->size / SECTOR_SIZE, .seg_max = DISK_SEG_MAX, }, + .io_efd = eventfd(0, 0), + .kvm = kvm, };virtio_trans_init (&bdev->vtrans,VIRTIO_PCI );
@@ -244,6 +266,8 @@ void virtio_blk__init(struct kvm *kvm, struct disk_image *disk)disk_image__set_callback (bdev->disk, virtio_blk_complete); +pthread_create (&bdev->io_thread,NULL , virtio_blk_thread, bdev); +if (compat_id !=-1 ) compat_id =compat__add_message ("virtio-blk device was not detected" , "While you have requested a virtio-blk device, "
Signature · 1 line
1.7.7.3
Quoted 10 lines · Asias He
Currently, all blk requests are processed in
notify_vq()which is in the context of ioeventfd thread:ioeventfd__thread(). The processing innotify_vq()may take a long time to complete.We should make
notify_vq()return as soon as possible, since all devices are sharing the single ioeventfd thread. Otherwise, it will block other device'snotify_vq()being called and starve other devices.In virtio net's
notify_vq(), we simply signal the tx/rx handle thread and return.
Why not use the threadpool?
Quoted 8 lines
This patch makes virtio blk's
notify_vq()just notify the blk thread instead of doing the real hard read/write work. Tests show that the overhead of the notification operations introduced by this patch is small.Signed-off-by Asias He <[email protected]>---
Signature · 1 line
Sasha.
Quoted 5 lines · Sasha Levin
Quoted 4 lines · Asias He
Currently, all blk requests are processed in
notify_vq()which is in the context of ioeventfd thread:ioeventfd__thread(). The processing innotify_vq()may take a long time to complete.
Quoted 8 lines
Quoted 6 lines
We should make
notify_vq()return as soon as possible, since all devices are sharing the single ioeventfd thread. Otherwise, it will block other device'snotify_vq()being called and starve other devices.In virtio net's
notify_vq(), we simply signal the tx/rx handle thread and return.Why not use the threadpool?
No.
1) In thread pool model, each job handling operation:thread_pool__do_job() takes about 6 or 7 mutex_{lock,unlock} ops. Most of the mutex are global (job_mutex) which are contented by the threads in the pool. It's fine for the non performance critical virtio devices, such as console, rng, etc. But it's not optimal for net and blk devices.
2) Using dedicated threads to handle blk requests opens the door for user to set different IO priority for the blk threads.
3) It also reduces the contentions between net and blk devices if they do not share the thead pool.
--->> the thread pool lock flow <<---
thread_pool__do_job() {
mutex_lock(&jobinfo->mutex);
if (jobinfo->signalcount++ == 0)
thread_pool__job_push_locked(job) {
mutex_lock(&job_mutex);
thread_pool__job_push(job);
mutex_unlock(&job_mutex);
}
mutex_unlock(&jobinfo->mutex);
mutex_lock(&job_muex);
pthread_cond_signal(&job_cond);
mutex_unlock(&job_mutex);}
thread_pool__threadfunc()
{
for (;;) {
mutex_lock(&job_mutex);
pthread_cond_wait(&job_cond, &job_mutex);
mutex_unlock(&job_mutex);
if (curjob)
thread_pool__handle_job(curjob);
}
}thread_pool__handle_job()
{
while (job) {
job->callback(job->kvm, job->data);
mutex_lock(&job->mutex);
thread_pool__job_push_locked(job);
mutex_unlock(&job->mutex);
job = thread_pool__job_pop_locked();
}}
Signature · 1 line
Asias He
Quoted 6 lines · Asias He
Quoted 4 lines
Quoted 2 lines
In virtio net's
notify_vq(), we simply signal the tx/rx handle thread and return.Why not use the threadpool?
No.
Sasha?
Quoted 9 lines · Pekka Enberg
Quoted 6 lines · Asias He
Quoted 4 lines
Quoted 2 lines
In virtio net's
notify_vq(), we simply signal the tx/rx handle thread and return.Why not use the threadpool?
No.
Sasha?
I was looking into the concept of adding 'dedicated' threads to the threadpool, since when the threadpool was added originally one of the purposes was to have all worker threads in a single place.
This way we could still have threads in one place, which would make it easier to control all of them but still won't hurt performance of those threads.
We can merge this patch, and I'll do the dedicated thread patch on top of it.
Signature · 1 line
Sasha.
Quoted 10 lines · Sasha Levin
I was looking into the concept of adding 'dedicated' threads to the threadpool, since when the threadpool was added originally one of the purposes was to have all worker threads in a single place.
This way we could still have threads in one place, which would make it easier to control all of them but still won't hurt performance of those threads.
We can merge this patch, and I'll do the dedicated thread patch on top of it.
Nah. Lets add the new API and change this patch to use it instead.