performance improvement for windows guests, running on top of virtio block device
RFC PATCH 1/1 performance improvement for windows guests, running on top of virtio block device
The following patch allows us to improve Windows virtio block driver performance on small size requests. Additionally, it leads to reducing of cpu usage on write IOs
repository: /home/vadimr/work/win7/qemu
branch: master
commit 68290c4e9c96f345d544ca5d2b89f27a1e67e27a
Author: Vadim Rozenfeld <[email protected]> Date: Mon Jan 11 09:00:21 2010 +0200
[RFC][PATCH] small IOs performance for windows guests, running on top of virtio block device
hw/virtio-blk.c +2 -0
@@ -28,6 +28,7 @@ typedef struct VirtIOBlockchar serial_str[BLOCK_SERIAL_STRLEN +1 ];QEMUBH *bh;size_t config_size; +unsigned int pending; } VirtIOBlock;static VirtIOBlock *to_virtio_blk (VirtIODevice *vdev)
@@ -87,6 +88,8 @@ typedef struct VirtIOBlockReqstruct VirtIOBlockReq *next; } VirtIOBlockReq; +static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue
*vq);
+
static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
{
VirtIOBlock *s = req->dev;
@@ -95,6 +98,11 @@ static void virtio_blk_req_complete(VirtIOBlockReq
*req, int status)
virtqueue_push(s->vq, &req->elem, req->qiov.size +
sizeof(*req->in));
virtio_notify(&s->vdev, s->vq);+ if(--s->pending == 0) {
+ virtio_queue_set_notification(s->vq, 1);
+ virtio_blk_handle_output(&s->vdev, s->vq);
+ }
+
qemu_free(req);
}
@@ -340,6 +348,9 @@ static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
exit(1);
}+ if(++s->pending == 1)
+ virtio_queue_set_notification(s->vq, 0);
+
req->out = (void *)req->elem.out_sg[0].iov_base;
req->in = (void *)req->elem.in_sg[req->elem.in_num -1].iov_base;
Quoted 5 lines · Vadim Rozenfeld
The following patch allows us to improve Windows virtio block driver performance on small size requests. Additionally, it leads to reducing of cpu usage on write IOs
Note, this is not an improvement for Windows specifically.
Quoted 24 lines
hw/virtio-blk.c +3
-0@@ -28,6 +28,7 @@ typedef struct VirtIOBlockchar serial_str[BLOCK_SERIAL_STRLEN +1 ];QEMUBH *bh;size_t config_size; +unsigned int pending; } VirtIOBlock;static VirtIOBlock *to_virtio_blk (VirtIODevice *vdev)@@ -87,6 +88,8 @@ typedef struct VirtIOBlockReqstruct VirtIOBlockReq *next; } VirtIOBlockReq; +static void virtio_blk_handle_output (VirtIODevice *vdev,VirtQueue *vq); +static void virtio_blk_req_complete (VirtIOBlockReq *req,int status) {VirtIOBlock *s = req->dev;hw/virtio-blk.c +0
-0@@ -95,6 +98,11 @@ static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
hw/virtio-blk.c static void virtio_blk_req_complete(VirtIOBlockReq · line 108
Quoted 10 lines
hw/virtio-blk.c +5
-0virtqueue_push (s->vq,&req->elem, req->qiov.size +sizeof (*req->in));virtio_notify (&s->vdev, s->vq); +if (--s->pending ==0 ) { +virtio_queue_set_notification (s->vq,1 ); +virtio_blk_handle_output (&s->vdev, s->vq); + } +
Coding style: space after if. See the CODING_STYLE file.
Quoted 10 lines
hw/virtio-blk.c +3
-0@@ -340,6 +348,9 @@ static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)exit (1 ); } +if (++s->pending ==1 ) +virtio_queue_set_notification (s->vq,0 ); + req->out = (void *)req->elem.out_sg[0 ].iov_base; req->in = (void *)req->elem.in_sg[req->elem.in_num -
Quoted 3 lines
1].iov_base;
Coding style: space after if, braces after if.
Your patch is word wrapped, please send it correctly. Easiest using git send-email.
The patch has potential to reduce performance on volumes with multiple spindles. Consider two processes issuing sequential reads into a RAID array. With this patch, the reads will be executed sequentially rather than in parallel, so I think a follow-on patch to make the minimum depth a parameter (set by the guest? the host?) would be helpful.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 37 lines · Dor Laor
Quoted 36 lines · Avi Kivity
Quoted 4 lines · Vadim Rozenfeld
The following patch allows us to improve Windows virtio block driver performance on small size requests. Additionally, it leads to reducing of cpu usage on write IOs
Note, this is not an improvement for Windows specifically.
Quoted 28 lines
hw/virtio-blk.c +3
-0@@ -28,6 +28,7 @@ typedef struct VirtIOBlockchar serial_str[BLOCK_SERIAL_STRLEN +1 ];QEMUBH *bh;size_t config_size; +unsigned int pending; } VirtIOBlock;static VirtIOBlock *to_virtio_blk (VirtIODevice *vdev)@@ -87,6 +88,8 @@ typedef struct VirtIOBlockReqstruct VirtIOBlockReq *next; } VirtIOBlockReq; +static void virtio_blk_handle_output (VirtIODevice *vdev,VirtQueue *vq); +static void virtio_blk_req_complete (VirtIOBlockReq *req,int status) {VirtIOBlock *s =
req->dev;hw/virtio-blk.c +0
-0@@ -95,6 +98,11 @@ static void virtio_blk_req_complete(VirtIOBlockReq *req, int status)
virtqueue_push(s->vq,&req->elem, req->qiov.size + sizeof(*req->in));virtio_notify(&s->vdev, s->vq);
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
+
if(--s->pending == 0){
+virtio_queue_set_notification(s->vq, 1);
+virtio_blk_handle_output(&s->vdev, s->vq);The above line should be moved out of the 'if'.
Attached results with rhel5.4 (qemu0.11) for win2k8 32bit guest. Note the drastic reduction in cpu consumption.
Attachment did not survive the email server, so you'll have to trust me saying that cpu consumption was done from 65% -> 40% for reads and from 80% --> 30% for writes
Quoted 31 lines
Quoted 30 lines
Quoted 3 lines
+ }
+Coding style: space after if. See the
CODING_STYLEfile.Quoted 9 lines
hw/virtio-blk.c +3
-0@@ -340,6 +348,9 @@ static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)exit (1 ); } +if (++s->pending ==1 ) +virtio_queue_set_notification (s->vq,0 ); + req->out = (void *)req->elem.out_sg[0 ].iov_base;Quoted 3 lines
req->in= (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base;Coding style: space after if, braces after if.
Your patch is word wrapped, please send it correctly. Easiest using git send-email.
The patch has potential to reduce performance on volumes with multiple spindles. Consider two processes issuing sequential reads into a RAID array. With this patch, the reads will be executed sequentially rather than in parallel, so I think a follow-on patch to make the minimum depth a parameter (set by the guest? the host?) would be helpful.
Quoted 6 lines · Dor Laor
Quoted 2 lines
Attached results with rhel5.4 (qemu0.11) for win2k8 32bit guest. Note the drastic reduction in cpu consumption.
Attachment did not survive the email server, so you'll have to trust me saying that cpu consumption was done from 65% -> 40% for reads and from 80% --> 30% for writes
For what kind of workload, and using what qemu parameters, and cpu consumtion in the host or guest? Either way this is an awfull lot, did you use kernel AIO on the host?
Any chance you could publish the benchmark, guest and host configs so we have meaningfull numbers?
FYI below is the manually applied patch without all the wrapping:
hw/virtio-blk.c +12 -0
@@ -28,6 +28,7 @@ typedef struct VirtIOBlockchar serial_str[BLOCK_SERIAL_STRLEN +1 ];QEMUBH *bh;size_t config_size; +unsigned int pending; } VirtIOBlock;static VirtIOBlock *to_virtio_blk (VirtIODevice *vdev)
@@ -87,6 +88,8 @@ typedef struct VirtIOBlockReqstruct VirtIOBlockReq *next; } VirtIOBlockReq; +static void virtio_blk_handle_output (VirtIODevice *vdev,VirtQueue *vq); +static void virtio_blk_req_complete (VirtIOBlockReq *req,int status) {VirtIOBlock *s = req->dev;
@@ -95,6 +98,12 @@ static void virtio_blk_req_complete(Virtvirtqueue_push (s->vq, &req->elem, req->qiov.size +sizeof (*req->in));virtio_notify (&s->vdev, s->vq); +if (--s->pending ==0 ) { +virtio_queue_set_notification (s->vq,1 ); +virtio_blk_handle_output (&s->vdev, s->vq); + } + +qemu_free (req); }
@@ -340,6 +349,9 @@ static void virtio_blk_handle_output(Virexit (1 ); } +if (++s->pending ==1 ) +virtio_queue_set_notification (s->vq,0 ); + req->out = (void *)req->elem.out_sg[0 ].iov_base; req->in = (void *)req->elem.in_sg[req->elem.in_num -1 ].iov_base;
Quoted 5 lines · Christoph Hellwig
FYI below is the manually applied patch without all the wrapping:
static voidvirtio_blk_req_complete(VirtIOBlockReq *req, int status){ VirtIOBlock *s = req->dev;
hw/virtio-blk.c static void virtio_blk_req_complete(Virt · line 106
Quoted 9 lines
hw/virtio-blk.c +5
-0@@ -95,6 +98,12 @@ static void virtio_blk_req_complete(Virtvirtqueue_push (s->vq,&req->elem, req->qiov.size +sizeof (*req->in));virtio_notify (&s->vdev, s->vq); +if (--s->pending ==0 ) { +virtio_queue_set_notification (s->vq,1 ); +virtio_blk_handle_output (&s->vdev, s->vq); + } +
As Dor points out, the call to virtio_blk_handle_output() wants to be before the test for pending, so we scan the ring as early as possible
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 2 lines · Avi Kivity
As Dor points out, the call to
virtio_blk_handle_output()wants to be before the test for pending, so we scan the ring as early as possible
I just reposted the patch in a way that it applies to share the work I did when starting to review it.
Quoted 5 lines · Avi Kivity
The patch has potential to reduce performance on volumes with multiple spindles. Consider two processes issuing sequential reads into a RAID array. With this patch, the reads will be executed sequentially rather than in parallel, so I think a follow-on patch to make the minimum depth a parameter (set by the guest? the host?) would be helpful.
Let's think about the life cycle of I/O requests a bit.
We have an idle virtqueue (aka one virtio-blk device). The first (read) request comes in, we get the virtio notify from the guest, which calls into virtio_blk_handle_output. With the new code we now disable the notify once we start processing the first request. If the second request hits the queue before we call into virtio_blk_get_request the second time we're fine even with the new code as we keep picking it up. If however it hits after we leave virtio_blk_handle_output, but before we complete the first request we do indeed introduce additional latency.
So instead of disabling notify while requests are active we might want to only disable it while we are inside virtio_blk_handle_output. Something like the following minimally tested patch:
hw/virtio-blk.c +20 -0
@@ -328,7 +328,15 @@ static void virtio_blk_handle_output(Virint num_writes =0 ;BlockDriverState *old_bs =NULL ; +/* +* While we are processing requests there is no need to get further +* notifications from the guest - it'll just burn cpu cycles doing +* useless context switches into the host. +*/ +virtio_queue_set_notification (s->vq,0 ); +while ((req =virtio_blk_get_request (s))) { +handle_request:if (req->elem.out_num <1 || req->elem.in_num <1 ) {fprintf (stderr,"virtio-blk missing headers\n" );exit (1 );
@@ -358,6 +366,18 @@ static void virtio_blk_handle_output(Vir } } +/* +* Once we're done processing all pending requests re-enable the queue +* notification. If there's an entry pending after we enabled +* notification again we hit a race and need to process it before +* returning. +*/ +virtio_queue_set_notification (s->vq,1 ); + req =virtio_blk_get_request (s); +if (req) { + goto handle_request; + } +if (num_writes >0 ) {do_multiwrite (old_bs, blkreq, num_writes); }
Quoted 2 lines · Avi Kivity
As Dor points out, the call to
virtio_blk_handle_output()wants to be before the test for pending, so we scan the ring as early as possible
It could cause a race window where we add an entry to the ring after we run virtio_blk_handle_output, but before re-enabling the notification. But I think my variant of the patch that I just posted should deal with this in an even better way.
Quoted 20 lines · Christoph Hellwig
Quoted 6 lines · Avi Kivity
The patch has potential to reduce performance on volumes with multiple spindles. Consider two processes issuing sequential reads into a RAID array. With this patch, the reads will be executed sequentially rather than in parallel, so I think a follow-on patch to make the minimum depth a parameter (set by the guest? the host?) would be helpful.
Let's think about the life cycle of I/O requests a bit.
We have an idle virtqueue (aka one virtio-blk device). The first (read) request comes in, we get the virtio notify from the guest, which calls into
virtio_blk_handle_output. With the new code we now disable the notify once we start processing the first request. If the second request hits the queue before we call intovirtio_blk_get_requestthe second time we're fine even with the new code as we keep picking it up. If however it hits after we leavevirtio_blk_handle_output, but before we complete the first request we do indeed introduce additional latency.
Quoted 4 lines
So instead of disabling notify while requests are active we might want to only disable it while we are inside
virtio_blk_handle_output. Something like the following minimally tested patch:
I'd suggest that we get even more aggressive and install an idle bottom half that checks the queue for newly submitted requests. If we keep getting requests submitted before a new one completes, we'll never take an I/O exit.
The same approach is probably a good idea for virtio-net.
Regards,
Anthony Liguori
Quoted 10 lines · Christoph Hellwig
Quoted 3 lines · Avi Kivity
As Dor points out, the call to
virtio_blk_handle_output()wants to be before the test for pending, so we scan the ring as early as possibleIt could cause a race window where we add an entry to the ring after we run
virtio_blk_handle_output, but before re-enabling the notification. But I think my variant of the patch that I just posted should deal with this in an even better way.
When we've disabled notifications, we should use any opportunity we have in userspace to check the rings to see if anything was added.
Bottom halves are run at the very end of the event loop which means that they're guaranteed to be the last thing run. idle bottom halves can be rescheduled without causing an infinite loop and do not affect the select timeout (which normal bottom halves do).
Regards,
Anthony Liguori
Quoted 2 lines
Quoted 52 lines · Christoph Hellwig
Quoted 6 lines · Avi Kivity
The patch has potential to reduce performance on volumes with multiple spindles. Consider two processes issuing sequential reads into a RAID array. With this patch, the reads will be executed sequentially rather than in parallel, so I think a follow-on patch to make the minimum depth a parameter (set by the guest? the host?) would be helpful.
Let's think about the life cycle of I/O requests a bit.
We have an idle virtqueue (aka one virtio-blk device). The first (read) request comes in, we get the virtio notify from the guest, which calls into
virtio_blk_handle_output. With the new code we now disable the notify once we start processing the first request. If the second request hits the queue before we call intovirtio_blk_get_requestthe second time we're fine even with the new code as we keep picking it up. If however it hits after we leavevirtio_blk_handle_output, but before we complete the first request we do indeed introduce additional latency.So instead of disabling notify while requests are active we might want to only disable it while we are inside
virtio_blk_handle_output. Something like the following minimally tested patch:Index:
qemu/hw/virtio-blk.c
===================================================================hw/virtio-blk.c +11
-0@@ -328,7 +328,15 @@ static void virtio_blk_handle_output(Virint num_writes =0 ;BlockDriverState *old_bs =NULL ; +/* +* While we are processing requests there is no need to get further +* notifications from the guest - it'll just burn cpu cycles doing +* useless context switches into the host. +*/ +virtio_queue_set_notification (s->vq,0 ); +while ((req =virtio_blk_get_request (s))) { +handle_request:if (req->elem.out_num<1 || req->elem.in_num<1 ) {fprintf (stderr,"virtio-blk missing headers\n" );exit (1 );@@ -358,6 +366,18 @@ static void virtio_blk_handle_output(Vir } } +/* +* Once we're done processing all pending requests re-enable the queue +* notification. If there's an entry pending after we enabled
hw/virtio-blk.c static void virtio_blk_handle_output(Vir · line 381
Quoted 10 lines
hw/virtio-blk.c +9
-0+* notification again we hit a race and need to process it before +* returning. +*/ +virtio_queue_set_notification (s->vq,1 ); + req =virtio_blk_get_request (s); +if (req) { + goto handle_request; + } +
I don't think this will have much effect. First, the time spent in virtio_blk_handle_output() is a small fraction of total guest time, so the probability of the guest hitting the notifications closed window is low. Second, while we're in that function, the vcpu that kicked us is stalled, and other vcpus are likely to be locked out of the queue by the guest.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 7 lines · Anthony Liguori
Quoted 3 lines
So instead of disabling notify while requests are active we might want to only disable it while we are inside
virtio_blk_handle_output. Something like the following minimally tested patch:I'd suggest that we get even more aggressive and install an idle bottom half that checks the queue for newly submitted requests. If we keep getting requests submitted before a new one completes, we'll never take an I/O exit.
That has the downside of bouncing a cache line on unrelated exits. It probably doesn't matter with qemu as it is now, since it will bounce qemu_mutex, but it will hurt with large guests (especially if they have many rings).
IMO we should get things to work well without riding on unrelated exits, especially as we're trying to reduce those exits.
Quoted 1 line
The same approach is probably a good idea for virtio-net.
With vhost-net you don't see exits.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Avi Kivity
Quoted 7 lines · Anthony Liguori
Quoted 3 lines
So instead of disabling notify while requests are active we might want to only disable it while we are inside
virtio_blk_handle_output. Something like the following minimally tested patch:I'd suggest that we get even more aggressive and install an idle bottom half that checks the queue for newly submitted requests. If we keep getting requests submitted before a new one completes, we'll never take an I/O exit.
That has the downside of bouncing a cache line on unrelated exits.
The read and write sides of the ring are widely separated in physical memory specifically to avoid cache line bouncing.
Quoted 4 lines
It probably doesn't matter with qemu as it is now, since it will
bounceqemu_mutex, but it will hurt with large guests (especially if they have many rings).IMO we should get things to work well without riding on unrelated exits, especially as we're trying to reduce those exits.
A block I/O request can potentially be very, very long lived. By serializing requests like this, there's a high likelihood that it's going to kill performance with anything capable of processing multiple requests.
OTOH, if we aggressively poll the ring when we have an opportunity to, there's very little down side to that and it addresses the serialization problem.
Quoted 3 lines
Quoted 1 line
The same approach is probably a good idea for virtio-net.
With vhost-net you don't see exits.
The point is, when we've disabled notification, we should poll on the ring for additional requests instead of waiting for one to complete before looking at another one.
Even with vhost-net, this logic is still applicable although potentially harder to achieve.
Regards,
Anthony Liguori
Quoted 4 lines · Anthony Liguori
Quoted 1 line
That has the downside of bouncing a cache line on unrelated exits.
The read and write sides of the ring are widely separated in physical memory specifically to avoid cache line bouncing.
I meant, exits on random vcpus will cause the cacheline containing the notification disable flag to bounce around. As it is, we read it on the vcpu that owns the queue and write it on that vcpu or the I/O thread.
Quoted 6 lines
Quoted 4 lines
It probably doesn't matter with qemu as it is now, since it will
bounceqemu_mutex, but it will hurt with large guests (especially if they have many rings).IMO we should get things to work well without riding on unrelated exits, especially as we're trying to reduce those exits.
A block I/O request can potentially be very, very long lived. By serializing requests like this, there's a high likelihood that it's going to kill performance with anything capable of processing multiple requests.
Right, that's why I suggested having a queue depth at which disabling notification kicks in. The patch hardcodes this depth to 1, unpatched qemu is infinite, a good value is probably spindle count + VAT.
Quoted 1 line
OTOH, if we aggressively poll the ring when we have an opportunity to, there's very little down side to that and it addresses the serialization problem.
But we can't guarantee that we'll get those opportunities, so it doesn't address the problem in a general way. A guest that doesn't use hpet and only has a single virtio-blk device will not have any reason to exit to qemu.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Anthony Liguori
Quoted 1 line
That has the downside of bouncing a cache line on unrelated exits.
The read and write sides of the ring are widely separated in physical memory specifically to avoid cache line bouncing.
I meant, exits on random vcpus will cause the cacheline containing the notification disable flag to bounce around. As it is, we read it on the vcpu that owns the queue and write it on that vcpu or the I/O thread.
Bottom halves are always run from the IO thread.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
It probably doesn't matter with qemu as it is now, since it will
bounceqemu_mutex, but it will hurt with large guests (especially if they have many rings).IMO we should get things to work well without riding on unrelated exits, especially as we're trying to reduce those exits.
A block I/O request can potentially be very, very long lived. By serializing requests like this, there's a high likelihood that it's going to kill performance with anything capable of processing multiple requests.
Right, that's why I suggested having a queue depth at which disabling notification kicks in. The patch hardcodes this depth to 1, unpatched qemu is infinite, a good value is probably spindle count + VAT.
That means we would need a user visible option which is quite unfortunate.
Also, that logic only really makes sense with cache=off. With cache=writethrough, you can get pathological cases whereas you have an uncached access followed by cached accesses. In fact, with read-ahead, this is probably not an uncommon scenario.
Quoted 3 lines
Quoted 1 line
OTOH, if we aggressively poll the ring when we have an opportunity to, there's very little down side to that and it addresses the serialization problem.
But we can't guarantee that we'll get those opportunities, so it doesn't address the problem in a general way. A guest that doesn't use hpet and only has a single virtio-blk device will not have any reason to exit to qemu.
We can mitigate this with a timer but honestly, we need to do perf measurements to see. My feeling is that we will need some more aggressive form of polling than just waiting for IO completion. I don't think queue depth is enough because it assumes that all requests are equal. When dealing with cache=off or even just storage with it's own cache, that's simply not the case.
Regards,
Anthony Liguori
Quoted 7 lines · Avi Kivity
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
OTOH, if we aggressively poll the ring when we have an opportunity to, there's very little down side to that and it addresses the serialization problem.
But we can't guarantee that we'll get those opportunities, so it doesn't address the problem in a general way. A guest that doesn't use hpet and only has a single virtio-blk device will not have any reason to exit to qemu.
We can mitigate this with a timer but honestly, we need to do perf measurements to see. My feeling is that we will need some more aggressive form of polling than just waiting for IO completion. I don't think queue depth is enough because it assumes that all requests are equal. When dealing with cache=off or even just storage with it's own cache, that's simply not the case.
Maybe we can adapt behaviour dynamically based on how fast results come in.
Based on our experiences with virtio-net, what I'd suggest is to make a lot of tunable options (ring size, various tx mitigation schemes, timeout durations, etc) and then we can do some deep performance studies to see how things interact with each other.
I think we should do that before making any changes because I'm deeply concerned that we'll introduce significant performance regressions.
Regards,
Anthony Liguori
Quoted 10 lines · Anthony Liguori
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Anthony Liguori
Quoted 1 line
That has the downside of bouncing a cache line on unrelated exits.
The read and write sides of the ring are widely separated in physical memory specifically to avoid cache line bouncing.
I meant, exits on random vcpus will cause the cacheline containing the notification disable flag to bounce around. As it is, we read it on the vcpu that owns the queue and write it on that vcpu or the I/O thread.
Bottom halves are always run from the IO thread.
Okay, so that won't be an issue.
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
It probably doesn't matter with qemu as it is now, since it will
bounceqemu_mutex, but it will hurt with large guests (especially if they have many rings).IMO we should get things to work well without riding on unrelated exits, especially as we're trying to reduce those exits.
A block I/O request can potentially be very, very long lived. By serializing requests like this, there's a high likelihood that it's going to kill performance with anything capable of processing multiple requests.
Right, that's why I suggested having a queue depth at which disabling notification kicks in. The patch hardcodes this depth to 1, unpatched qemu is infinite, a good value is probably spindle count + VAT.
That means we would need a user visible option which is quite unfortunate.
We could guess it, perhaps.
Quoted 1 line
Also, that logic only really makes sense with cache=off. With cache=writethrough, you can get pathological cases whereas you have an uncached access followed by cached accesses. In fact, with read-ahead, this is probably not an uncommon scenario.
So you'd increase the disable depths when cache!=none.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
OTOH, if we aggressively poll the ring when we have an opportunity to, there's very little down side to that and it addresses the serialization problem.
But we can't guarantee that we'll get those opportunities, so it doesn't address the problem in a general way. A guest that doesn't use hpet and only has a single virtio-blk device will not have any reason to exit to qemu.
We can mitigate this with a timer but honestly, we need to do perf measurements to see. My feeling is that we will need some more aggressive form of polling than just waiting for IO completion. I don't think queue depth is enough because it assumes that all requests are equal. When dealing with cache=off or even just storage with it's own cache, that's simply not the case.
Maybe we can adapt behaviour dynamically based on how fast results come in.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 5 lines · Anthony Liguori
Based on our experiences with virtio-net, what I'd suggest is to make a lot of tunable options (ring size, various tx mitigation schemes, timeout durations, etc) and then we can do some deep performance studies to see how things interact with each other.
I think we should do that before making any changes because I'm deeply concerned that we'll introduce significant performance regressions.
I agree. We can start with this patch, with a tunable depth, defaulting to current behaviour.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 8 lines · Avi Kivity
Quoted 5 lines · Anthony Liguori
Based on our experiences with virtio-net, what I'd suggest is to make a lot of tunable options (ring size, various tx mitigation schemes, timeout durations, etc) and then we can do some deep performance studies to see how things interact with each other.
I think we should do that before making any changes because I'm deeply concerned that we'll introduce significant performance regressions.
I agree. We can start with this patch, with a tunable depth, defaulting to current behaviour.
I wouldn't be opposed to that provided we made it clear that these options were not supported long term. I don't want management tools (like libvirt) to start relying on them.
Regards,
Anthony Liguori
Quoted 12 lines · Anthony Liguori
Quoted 8 lines · Avi Kivity
Quoted 5 lines · Anthony Liguori
Based on our experiences with virtio-net, what I'd suggest is to make a lot of tunable options (ring size, various tx mitigation schemes, timeout durations, etc) and then we can do some deep performance studies to see how things interact with each other.
I think we should do that before making any changes because I'm deeply concerned that we'll introduce significant performance regressions.
I agree. We can start with this patch, with a tunable depth, defaulting to current behaviour.
I wouldn't be opposed to that provided we made it clear that these options were not supported long term. I don't want management tools (like libvirt) to start relying on them.
x-option-name for experimental options?
-device disk,if=virtio,x-queue-depth-suppress-notify=4
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 9 lines · Avi Kivity
Quoted 8 lines · Anthony Liguori
Quoted 7 lines · Avi Kivity
Quoted 5 lines · Anthony Liguori
Based on our experiences with virtio-net, what I'd suggest is to make a lot of tunable options (ring size, various tx mitigation schemes, timeout durations, etc) and then we can do some deep performance studies to see how things interact with each other.
I think we should do that before making any changes because I'm deeply concerned that we'll introduce significant performance regressions.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
I agree. We can start with this patch, with a tunable depth, defaulting to current behaviour.
I wouldn't be opposed to that provided we made it clear that these options were not supported long term. I don't want management tools (like libvirt) to start relying on them.
x-option-name for experimental options?
-device disk,if=virtio,x-queue-depth-suppress-notify=4
Sounds reasonable to me.
Regards,
Anthony Liguori
Quoted 42 lines · Anthony Liguori
Quoted 6 lines · Avi Kivity
Quoted 5 lines · Anthony Liguori
Quoted 3 lines
So instead of disabling notify while requests are active we might want to only disable it while we are inside
virtio_blk_handle_output. Something like the following minimally tested patch:Quoted 7 lines
Quoted 5 lines
I'd suggest that we get even more aggressive and install an idle bottom half that checks the queue for newly submitted requests. If we keep getting requests submitted before a new one completes, we'll never take an I/O exit.
That has the downside of bouncing a cache line on unrelated exits.
The read and write sides of the ring are widely separated in physical memory specifically to avoid cache line bouncing.
Quoted 6 lines
It probably doesn't matter with qemu as it is now, since it will
bounceqemu_mutex, but it will hurt with large guests (especially if they have many rings).IMO we should get things to work well without riding on unrelated exits, especially as we're trying to reduce those exits.
A block I/O request can potentially be very, very long lived. By serializing requests like this, there's a high likelihood that it's going to kill performance with anything capable of processing multiple requests.
OTOH, if we aggressively poll the ring when we have an opportunity to, there's very little down side to that and it addresses the serialization problem.
Quoted 3 lines
Quoted 1 line
The same approach is probably a good idea for virtio-net.
With vhost-net you don't see exits.
The point is, when we've disabled notification, we should poll on the ring for additional requests instead of waiting for one to complete before looking at another one.
Quoted 7 lines
Even with vhost-net, this logic is still applicable although potentially harder to achieve.
Regards,
Anthony Liguori
vhost net does this already: it has a mode where it poll when skbs have left send queue: for tap this is when they have crossed the bridge, for packet socket this is when they have been transmitted.
Signature · 1 line
MST
Quoted 53 lines · Anthony Liguori
Quoted 11 lines · Avi Kivity
Quoted 5 lines · Anthony Liguori
Quoted 1 line
That has the downside of bouncing a cache line on unrelated exits.
The read and write sides of the ring are widely separated in physical memory specifically to avoid cache line bouncing.
I meant, exits on random vcpus will cause the cacheline containing the notification disable flag to bounce around. As it is, we read it on the vcpu that owns the queue and write it on that vcpu or the I/O thread.
Bottom halves are always run from the IO thread.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
It probably doesn't matter with qemu as it is now, since it will
bounceqemu_mutex, but it will hurt with large guests (especially if they have many rings).IMO we should get things to work well without riding on unrelated exits, especially as we're trying to reduce those exits.
Quoted 8 lines
Quoted 4 lines
A block I/O request can potentially be very, very long lived. By serializing requests like this, there's a high likelihood that it's going to kill performance with anything capable of processing multiple requests.
Right, that's why I suggested having a queue depth at which disabling notification kicks in. The patch hardcodes this depth to 1, unpatched qemu is infinite, a good value is probably spindle count + VAT.
That means we would need a user visible option which is quite unfortunate.
Also, that logic only really makes sense with cache=off. With cache=writethrough, you can get pathological cases whereas you have an uncached access followed by cached accesses. In fact, with read-ahead, this is probably not an uncommon scenario.
Quoted 8 lines
Quoted 3 lines
OTOH, if we aggressively poll the ring when we have an opportunity to, there's very little down side to that and it addresses the serialization problem.
But we can't guarantee that we'll get those opportunities, so it doesn't address the problem in a general way. A guest that doesn't use hpet and only has a single virtio-blk device will not have any reason to exit to qemu.
We can mitigate this with a timer but honestly, we need to do perf measurements to see. My feeling is that we will need some more aggressive form of polling than just waiting for IO completion. I don't think queue depth is enough because it assumes that all requests are equal. When dealing with cache=off or even just storage with it's own cache, that's simply not the case.
Quoted 4 lines
Regards,
Anthony Liguori
BTW this is why vhost net uses queue depth in bytes.
Signature · 1 line
MST
Quoted 4 lines
Bottom halves are run at the very end of the event loop which means that they're guaranteed to be the last thing run. idle bottom halves can be rescheduled without causing an infinite loop and do not affect the select timeout (which normal bottom halves do).
Idle bottom halves (i.e. qemu_bh_schedule_idle) are just bugs waiting to happen, and should never be used for anything.
Paul
Quoted 8 lines · Paul Brook
Quoted 5 lines
Bottom halves are run at the very end of the event loop which means that they're guaranteed to be the last thing run. idle bottom halves can be rescheduled without causing an infinite loop and do not affect the select timeout (which normal bottom halves do).
Idle bottom halves (i.e.
qemu_bh_schedule_idle) are just bugs waiting to happen, and should never be used for anything.
Idle bottom halves make considerable more sense than the normal bottom halves.
The fact that rescheduling a bottom half within a bottom half results in an infinite loop is absurd. It is equally absurd that bottoms halves alter the select timeout. The result of that is that if a bottom half schedules another bottom half, and that bottom half schedules the previous, you get a tight infinite loop. Since bottom halves are used often times deep within functions, the result is very subtle infinite loops (that we've absolutely encountered in the past).
A main loop should have only a few characteristics. It should enable timeouts (based on select), it should enable fd event dispatch, and it should allow for idle functions to be registered. There should be no guarantees on when idle functions are executed other than they'll eventually be executed.
The way we use "bottom halves" today should be implemented in terms of a relative timeout of 0 or an absolute timeout of now. The fact that we can't do that in our main loop is due to the very strange dependencies deep within various devices on io dispatch ordering. I would love to eliminate this but I've not been able to spend any time on this recently.
Regards,
Anthony Liguori
Quoted 2 lines
Paul
Quoted 6 lines
Quoted 2 lines
Idle bottom halves (i.e.
qemu_bh_schedule_idle) are just bugs waiting to happen, and should never be used for anything.Idle bottom halves make considerable more sense than the normal bottom halves.
Quoted 7 lines
The fact that rescheduling a bottom half within a bottom half results in an infinite loop is absurd. It is equally absurd that bottoms halves alter the select timeout. The result of that is that if a bottom half schedules another bottom half, and that bottom half schedules the previous, you get a tight infinite loop. Since bottom halves are used often times deep within functions, the result is very subtle infinite loops (that we've absolutely encountered in the past).
I disagree. The "select timeout" is a completely irrelevant implementation detail. Anything that relies on it is just plain wrong. If you require a delay then you should be using a timer. If scheduling a BH directly then you should expect it to be processed without delay.
If a BH reschedules itself (indirectly or indirectly) without useful work occuring then you absolutely should expect an infinite loop. Rescheduling itself after doing useful work should never cause an infinite loop. The only way it can loop inifinitely is if we have infinite amount of work to do, in which case you loose either way. Looping over work via recursive BHs is probably not the most efficient way to do things, but I guess it may sometimes be the simplest in practice.
Interaction between multiple BH is slightly trickier. By my reading BH are processed in the order they are created. It may be reasonable to guarantee that BH are processed in the order they are scheduled. However I'm reluctant to even go that far without a good use-case. You could probably come up with arguments for processing them in most-recently-scheduled order.
Quoted 5 lines
A main loop should have only a few characteristics. It should enable timeouts (based on select), it should enable fd event dispatch, and it should allow for idle functions to be registered. There should be no guarantees on when idle functions are executed other than they'll eventually be executed.
If you don't provide any guarantees, then surely processing them immediately must be an acceptable implementation. I don't believe there is a useful definition of "idle".
All existing uses of qemu_bh_schedule_idle are in fact poorly implemented periodic polling. Furthermore these should not be using periodic polling, they can and should be event driven. They only exist because noone has bothered to fix the old code properly. Having arbitrary 10ms latency on DMA transfers is just plain wrong.
Quoted 5 lines
The way we use "bottom halves" today should be implemented in terms of a relative timeout of 0 or an absolute timeout of now. The fact that we can't do that in our main loop is due to the very strange dependencies deep within various devices on io dispatch ordering. I would love to eliminate this but I've not been able to spend any time on this recently.
I don't see how this helps. A self-triggering event with a timeout of "now" is still an infinite loop. Any delay is a bugs in the dispatch loop. "idle" BHs are relying on this bug.
Paul
Quoted 14 lines · Paul Brook
Quoted 6 lines
Quoted 3 lines
Idle bottom halves (i.e.
qemu_bh_schedule_idle) are just bugs waiting to happen, and should never be used for anything.Idle bottom halves make considerable more sense than the normal bottom halves.
Quoted 8 lines
The fact that rescheduling a bottom half within a bottom half results in an infinite loop is absurd. It is equally absurd that bottoms halves alter the select timeout. The result of that is that if a bottom half schedules another bottom half, and that bottom half schedules the previous, you get a tight infinite loop. Since bottom halves are used often times deep within functions, the result is very subtle infinite loops (that we've absolutely encountered in the past).
Quoted 2 lines
I disagree. The "select timeout" is a completely irrelevant implementation detail. Anything that relies on it is just plain wrong.
No, it's an extremely important detail because its the difference between an infinite loop in an idle function.
Very simply, without idle bottom halves, there's no way to implement polling with the main loop. If we dropped idle bottom halves, we would have to add explicit polling back to the main loop.
How would you implement polling?
Quoted 4 lines
If you require a delay
then you should be using a timer. If scheduling a BH directly then you should expect it to be processed without delay.
If you need something that doesn't require a delay, schedule a timer with no delay. What's the point of BHs?
It's because there's very subtle differences between BHs and timers and because we don't adjust the select timeout for the next timer deadline, there's a race between when we check for timer expiration and when we invoke select.
Actually, that's probably fixed now because we've changed the SIGALRM handler to write to a file descriptor to eliminate the signal/select race. I'll give it a try, it might actually work now.
Quoted 8 lines
If a BH reschedules itself (indirectly or indirectly) without useful work occuring then you absolutely should expect an infinite loop. Rescheduling itself after doing useful work should never cause an infinite loop. The only way it can loop inifinitely is if we have infinite amount of work to do, in which case you loose either way. Looping over work via recursive BHs is probably not the most efficient way to do things, but I guess it may sometimes be the simplest in practice.
I think the point is getting lost. My contention is that a main loop needs three things 1) an idle callback 2) timers 3) io notification. Bottom halves act both today as a no-delay timer and an idle callback. I agree, that's unfortunate. What we should do is remove idle bottom halves, add proper idle callbacks, and make bottom halves implemented in terms of no-delay timers.
Quoted 6 lines
Interaction between multiple BH is slightly trickier. By my reading BH are processed in the order they are created. It may be reasonable to guarantee that BH are processed in the order they are scheduled. However I'm reluctant to even go that far without a good use-case. You could probably come up with arguments for processing them in most-recently-scheduled order.
It's no different than two timers that happen to fire at the same deadline. You can process in terms of when the timers were created initially or when they were last scheduled. Ultimately, it shouldn't matter to any code that uses them.
Quoted 10 lines
Quoted 6 lines
A main loop should have only a few characteristics. It should enable timeouts (based on select), it should enable fd event dispatch, and it should allow for idle functions to be registered. There should be no guarantees on when idle functions are executed other than they'll eventually be executed.
If you don't provide any guarantees, then surely processing them immediately must be an acceptable implementation. I don't believe there is a useful definition of "idle".
idle is necessary to implement polling. You can argue that polling should never be necessary but practically speaking, it almost always is.
Quoted 6 lines
All existing uses of
qemu_bh_schedule_idleare in fact poorly implemented periodic polling. Furthermore these should not be using periodic polling, they can and should be event driven. They only exist because noone has bothered to fix the old code properly. Having arbitrary 10ms latency on DMA transfers is just plain wrong.
I agree with you here :-)
But there are more legitimate uses of polling. For instance, with virtio-net, we use a timer to implement tx mitigation. It's a huge boost for throughput but it tends to add latency on packet transmission because our notification comes at a longer time. We really just need the notification as a means to re-enable interrupts, not necessarily to slow down packet transmission.
So using an idle callback to transmit packets would certainly decrease latency in many cases while still getting the benefits of throughput improvement.
Quoted 10 lines
Quoted 6 lines
The way we use "bottom halves" today should be implemented in terms of a relative timeout of 0 or an absolute timeout of now. The fact that we can't do that in our main loop is due to the very strange dependencies deep within various devices on io dispatch ordering. I would love to eliminate this but I've not been able to spend any time on this recently.
I don't see how this helps. A self-triggering event with a timeout of "now" is still an infinite loop. Any delay is a bugs in the dispatch loop. "idle" BHs are relying on this bug.
The main point is that BHs should not be implemented in the actual main loop and that "idle" BHs are really the only type of BHs that should exist as far as the main loop is concerned. s/"idle" BHs/idle callbacks/g and I think we're at a more agreeable place.
Regards,
Anthony Liguori
Quoted 2 lines
Paul
Quoted 5 lines
Very simply, without idle bottom halves, there's no way to implement polling with the main loop. If we dropped idle bottom halves, we would have to add explicit polling back to the main loop.
How would you implement polling?
AFAICS any sort of polling is by definition time based so use a timer. Forcing the user to explicitly decide how often to poll is a feature. If they don't know this then they probably shouldn't be using polling.
Quoted 8 lines
Quoted 3 lines
I don't see how this helps. A self-triggering event with a timeout of "now" is still an infinite loop. Any delay is a bugs in the dispatch loop. "idle" BHs are relying on this bug.
The main point is that BHs should not be implemented in the actual main loop and that "idle" BHs are really the only type of BHs that should exist as far as the main loop is concerned. s/"idle" BHs/idle callbacks/g and I think we're at a more agreeable place.
Part of my difficulty is that I don't have a clear idea what "idle" means. It certainly isn't what qemu_bh_schedule_idle implements.
The only vaguely sane definition I can come up with is once the main loop has run out of useful things to do and is about to suspend itself. Typically no significant guest code will be executed between requesting the idle callback and the callback occurring. In an SMP host environment it may be possible for guest CPUs to trigger or observe intermediate events, but this can not be relied upon. Given this definition I'm unclear how useful this would be.
A BH is a deferred callback that is used to allow events to be processed. IMO the important feature is that it is a deferred until after the current event has been processed, so avoid a whole set of reentrancy problems. Of course if you misuse them you can cause infinite loops, in the same way that misusing a regular callback will lead to infinite recursion.
I'm not sure that replacing BHs with zero interval timers actually gains us anything. From a user(device) perspective I'd be more inclined to make timers trigger a BH when they expire, like the ptimer code. Idle events can then be handled in exactly the same way: the user provides a BH which is triggered the next time the idle event occurs.
The exact source of a call to a BH routine from is an implementation detail. The important thing is that they will never be invoked from within or concurrent with any other device callback.
Paul
Quoted 14 lines · Paul Brook
Quoted 6 lines
Quoted 3 lines
Idle bottom halves (i.e.
qemu_bh_schedule_idle) are just bugs waiting to happen, and should never be used for anything.Idle bottom halves make considerable more sense than the normal bottom halves.
Quoted 8 lines
The fact that rescheduling a bottom half within a bottom half results in an infinite loop is absurd. It is equally absurd that bottoms halves alter the select timeout. The result of that is that if a bottom half schedules another bottom half, and that bottom half schedules the previous, you get a tight infinite loop. Since bottom halves are used often times deep within functions, the result is very subtle infinite loops (that we've absolutely encountered in the past).
Quoted 5 lines
I disagree. The "select timeout" is a completely irrelevant implementation detail. Anything that relies on it is just plain wrong. If you require a delay then you should be using a timer. If scheduling a BH directly then you should expect it to be processed without delay.
I agree. Further, once we fine-grain device threading, the iothread essentially disappears and is replaced by device-specific threads. There's no "idle" anymore.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Quoted 13 lines · Avi Kivity
Quoted 12 lines · Paul Brook
Quoted 12 lines
Quoted 2 lines
Idle bottom halves (i.e.
qemu_bh_schedule_idle) are just bugs waiting to happen, and should never be used for anything.Idle bottom halves make considerable more sense than the normal bottom halves.
The fact that rescheduling a bottom half within a bottom half results in an infinite loop is absurd. It is equally absurd that bottoms halves alter the select timeout. The result of that is that if a bottom half schedules another bottom half, and that bottom half schedules the previous, you get a tight infinite loop. Since bottom halves are used often times deep within functions, the result is very subtle infinite loops (that we've absolutely encountered in the past).
Quoted 6 lines
Quoted 4 lines
I disagree. The "select timeout" is a completely irrelevant implementation detail. Anything that relies on it is just plain wrong. If you require a delay then you should be using a timer. If scheduling a BH directly then you should expect it to be processed without delay.
I agree. Further, once we fine-grain device threading, the iothread essentially disappears and is replaced by device-specific threads. There's no "idle" anymore.
That's a nice idea, but how is io dispatch handled? Is everything synchronous or do we continue to program asynchronously?
It's very difficult to mix concepts. I personally don't anticipate per-device threading but rather anticipate re-entrant device models. I would expect all I/O to be dispatched within the I/O thread and the VCPU threads to be able to execute device models simultaneously with the I/O thread.
Regards,
Anthony Liguori
Quoted 4 lines · Anthony Liguori
Quoted 1 line
I agree. Further, once we fine-grain device threading, the iothread essentially disappears and is replaced by device-specific threads. There's no "idle" anymore.
That's a nice idea, but how is io dispatch handled? Is everything synchronous or do we continue to program asynchronously?
Simple stuff can be kept asynchronous, complex stuff (like qcow2) ought to be made synchronous (it uses threads anyway, so we don't lose anything). Stuff like vnc can go either way.
Quoted 2 lines
It's very difficult to mix concepts.
We're complicated enough to have conflicting requirements and a large code base with its own inertia, so no choice really.
Quoted 1 line
I personally don't anticipate per-device threading but rather anticipate re-entrant device models. I would expect all I/O to be dispatched within the I/O thread and the VCPU threads to be able to execute device models simultaneously with the I/O thread.
That means long-running operations on the iothread can lock out other completions.
Candidates for own threads are:
- live migration
- block format drivers (except linux-aio, perhaps have a thread for the aio completion handler)
- vnc
- sdl
- sound?
- hotplug, esp. memory
Each such thread could run the same loop as the iothread. Any pollable fd or timer would be associated with a thread, so things continue as normal more or less. Unassociated objects continue with the main iothread.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Quoted 27 lines · Avi Kivity
Quoted 7 lines · Anthony Liguori
Quoted 3 lines
I agree. Further, once we fine-grain device threading, the iothread essentially disappears and is replaced by device-specific threads. There's no "idle" anymore.
That's a nice idea, but how is io dispatch handled? Is everything synchronous or do we continue to program asynchronously?
Simple stuff can be kept asynchronous, complex stuff (like qcow2) ought to be made synchronous (it uses threads anyway, so we don't lose anything). Stuff like vnc can go either way.
Quoted 2 lines
It's very difficult to mix concepts.
We're complicated enough to have conflicting requirements and a large code base with its own inertia, so no choice really.
Quoted 4 lines
I personally don't anticipate per-device threading but rather anticipate re-entrant device models. I would expect all I/O to be dispatched within the I/O thread and the VCPU threads to be able to execute device models simultaneously with the I/O thread.
That means long-running operations on the iothread can lock out other completions.
Quoted 7 lines
Candidates for own threads are:
- live migration
- block format drivers (except linux-aio, perhaps have a thread for the aio completion handler)
- vnc
- sdl
- sound?
Had(ve?) that on a private branch, there's no point in that complication.
Quoted 7 lines
- hotplug, esp. memory
Each such thread could run the same loop as the iothread. Any pollable fd or timer would be associated with a thread, so things continue as normal more or less. Unassociated objects continue with the main iothread.
Signature · 1 line
mailto:[email protected]
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Anthony Liguori
Quoted 1 line
I agree. Further, once we fine-grain device threading, the iothread essentially disappears and is replaced by device-specific threads. There's no "idle" anymore.
That's a nice idea, but how is io dispatch handled? Is everything synchronous or do we continue to program asynchronously?
Simple stuff can be kept asynchronous, complex stuff (like qcow2) ought to be made synchronous (it uses threads anyway, so we don't lose anything). Stuff like vnc can go either way.
We've discussed this before and I still contend that threads do not make qcow2 any simpler.
Quoted 18 lines
Quoted 2 lines
It's very difficult to mix concepts.
We're complicated enough to have conflicting requirements and a large code base with its own inertia, so no choice really.
Quoted 1 line
I personally don't anticipate per-device threading but rather anticipate re-entrant device models. I would expect all I/O to be dispatched within the I/O thread and the VCPU threads to be able to execute device models simultaneously with the I/O thread.
That means long-running operations on the iothread can lock out other completions.
Candidates for own threads are:
- live migration
- block format drivers (except linux-aio, perhaps have a thread for the aio completion handler)
- vnc
- sdl
- sound?
- hotplug, esp. memory
Quoted 1 line
Each such thread could run the same loop as the iothread. Any pollable fd or timer would be associated with a thread, so things continue as normal more or less. Unassociated objects continue with the main iothread.
Is the point latency or increasing available CPU resources? If the device models are re-entrant, that reduces a ton of the demand on the qemu_mutex which means that IO thread can run uncontended. While we have evidence that the VCPU threads and IO threads are competing with each other today, I don't think we have any evidence to suggest that the IO thread is self-starving itself with long running events.
With the device model, I'd like to see us move toward a very well defined API for each device to use. Part of the reason for this is to limit the scope of the devices in such a way that we can enforce this at compile time. Then we can introduce locking within devices with some level of guarantee that we've covered the API devices are actually consuming.
For host services though, it's much more difficult to isolate them like this. I'm not necessarily claiming that this will never be the right thing to do, but I don't think we really have the evidence today to suggest that we should focus on this in the short term.
Regards,
Anthony Liguori
Quoted 10 lines · Anthony Liguori
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Anthony Liguori
Quoted 1 line
I agree. Further, once we fine-grain device threading, the iothread essentially disappears and is replaced by device-specific threads. There's no "idle" anymore.
That's a nice idea, but how is io dispatch handled? Is everything synchronous or do we continue to program asynchronously?
Simple stuff can be kept asynchronous, complex stuff (like qcow2) ought to be made synchronous (it uses threads anyway, so we don't lose anything). Stuff like vnc can go either way.
We've discussed this before and I still contend that threads do not make qcow2 any simpler.
qcow2 is still not fully asynchronous. All the other format drivers (except raw) are fully synchronous. If we had a threaded infrastructure, we could convert them all in a day. As it is, you can only use the other block format drivers in 'qemu-img convert'.
Quoted 4 lines
Quoted 2 lines
Each such thread could run the same loop as the iothread. Any pollable fd or timer would be associated with a thread, so things continue as normal more or less. Unassociated objects continue with the main iothread.
Is the point latency or increasing available CPU resources?
Yes.
Quoted 1 line
If the device models are re-entrant, that reduces a ton of the demand on the
qemu_mutexwhich means that IO thread can run uncontended. While we have evidence that the VCPU threads and IO threads are competing with each other today, I don't think we have any evidence to suggest that the IO thread is self-starving itself with long running events.
I agree we have no evidence and that this is all speculation. But consider a 64-vcpu guest, it has a 1:64 ratio of vcpu time (initiations) to iothread time (completions). If each vcpu generates 5000 initiations per second, the iothread needs to handle 320,000 completions per second. At that rate you will see some internal competition. That thread will also have a hard time shuffling data since every completion's data will reside in the wrong cpu cache.
Note, an alternative to multiple iothreads is to move completion handling back to vcpus, provided we can steer the handler close to the guest completion handler.
Quoted 2 lines
With the device model, I'd like to see us move toward a very well defined API for each device to use. Part of the reason for this is to limit the scope of the devices in such a way that we can enforce this at compile time. Then we can introduce locking within devices with some level of guarantee that we've covered the API devices are actually consuming.
Yes. On the other hand, the shape of the API will be influenced by the locking model, so we'll have to take iterative steps, unless someone comes out with a brilliant design.
Quoted 2 lines
For host services though, it's much more difficult to isolate them like this.
What do you mean by host services?
Quoted 1 line
I'm not necessarily claiming that this will never be the right thing to do, but I don't think we really have the evidence today to suggest that we should focus on this in the short term.
Agreed. We will start to see evidence (one way or the other) as fully loaded 64-vcpu guests are benchmarked. Another driver may be real-time guests; if a timer can be deferred by some block device initiation or completion, then we can say goodbye to any realtime guarantees we want to make.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
Quoted 1 line · Avi Kivity
qcow2 is still not fully asynchronous. All the other format drivers (except raw) are fully synchronous. If we had a threaded infrastructure, we could convert them all in a day. As it is, you can only use the other block format drivers in 'qemu-img convert'.
I've got a healthy amount of scepticism that it's that easy. But I'm happy to consider patches :-)
Quoted 11 lines
Quoted 5 lines
Quoted 3 lines
Each such thread could run the same loop as the iothread. Any pollable fd or timer would be associated with a thread, so things continue as normal more or less. Unassociated objects continue with the main iothread.
Is the point latency or increasing available CPU resources?
Yes.
Quoted 1 line
If the device models are re-entrant, that reduces a ton of the demand on the
qemu_mutexwhich means that IO thread can run uncontended. While we have evidence that the VCPU threads and IO threads are competing with each other today, I don't think we have any evidence to suggest that the IO thread is self-starving itself with long running events.I agree we have no evidence and that this is all speculation. But consider a 64-vcpu guest, it has a 1:64 ratio of vcpu time (initiations) to iothread time (completions). If each vcpu generates 5000 initiations per second, the iothread needs to handle 320,000 completions per second. At that rate you will see some internal competition. That thread will also have a hard time shuffling data since every completion's data will reside in the wrong cpu cache.
Ultimately, it depends on what you're optimizing for. If you've got a 64-vcpu guest on a 128-way box, then sure, we want to have 64 IO threads because that will absolutely increase throughput.
But realistically, it's more likely that if you've got a 64-vcpu guest, you're on a 1024-way box and you've got 64 guests running at once. Having 64 IO threads per VM means you've got 4k threads floating. It's still just as likely that one completion will get delayed by something less important. Now with all of these threads on a box like this, you get nasty NUMA interactions too.
The difference between the two models is that with threads, we rely on pre-emption to enforce fairness and the Linux scheduler to perform scheduling. With a single IO thread, we're determining execution order and priority.
A lot of main loops have a notion of priority for timer and idle callbacks. For something that is latency sensitive, you absolutely could introduce the concept of priority for bottom halves. It would ensure that a +1 priority bottom half would get scheduled before handling any lower priority I/O/BHs.
Quoted 1 line
Note, an alternative to multiple iothreads is to move completion handling back to vcpus, provided we can steer the handler close to the guest completion handler.
Looking at something like linux-aio, I think we might actually want to do that. We can submit the request from the VCPU thread and we can certainly program the signal to get delivered to that VCPU thread. Maintaining affinity for the request is likely a benefit.
Quoted 4 lines
Quoted 2 lines
For host services though, it's much more difficult to isolate them like this.
What do you mean by host services?
Things like VNC and live migration. Things that aren't directly related to a guest's activity. One model I can imagine is to continue to relegate these things to a single IO thread, but then move device driven callbacks either back to the originating thread or to a dedicated device callback thread. Host services generally have a much lower priority.
Quoted 3 lines
Quoted 1 line
I'm not necessarily claiming that this will never be the right thing to do, but I don't think we really have the evidence today to suggest that we should focus on this in the short term.
Agreed. We will start to see evidence (one way or the other) as fully loaded 64-vcpu guests are benchmarked. Another driver may be real-time guests; if a timer can be deferred by some block device initiation or completion, then we can say goodbye to any realtime guarantees we want to make.
I'm wary of making decisions based on performance of a 64-vcpu guest. It's an important workload to characterize because it's an extreme case but I think 64 1-vcpu guests will continue to be significantly more important than 1 64-vcpu guest.
Regards,
Anthony Liguori
Quoted 4 lines · Anthony Liguori
Quoted 1 line · Avi Kivity
qcow2 is still not fully asynchronous. All the other format drivers (except raw) are fully synchronous. If we had a threaded infrastructure, we could convert them all in a day. As it is, you can only use the other block format drivers in 'qemu-img convert'.
I've got a healthy amount of scepticism that it's that easy. But I'm happy to consider patches :-)
I'd be happy to have time to write them.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
If the device models are re-entrant, that reduces a ton of the demand on the
qemu_mutexwhich means that IO thread can run uncontended. While we have evidence that the VCPU threads and IO threads are competing with each other today, I don't think we have any evidence to suggest that the IO thread is self-starving itself with long running events.I agree we have no evidence and that this is all speculation. But consider a 64-vcpu guest, it has a 1:64 ratio of vcpu time (initiations) to iothread time (completions). If each vcpu generates 5000 initiations per second, the iothread needs to handle 320,000 completions per second. At that rate you will see some internal competition. That thread will also have a hard time shuffling data since every completion's data will reside in the wrong cpu cache.
Ultimately, it depends on what you're optimizing for. If you've got a 64-vcpu guest on a 128-way box, then sure, we want to have 64 IO threads because that will absolutely increase throughput.
But realistically, it's more likely that if you've got a 64-vcpu guest, you're on a 1024-way box and you've got 64 guests running at once. Having 64 IO threads per VM means you've got 4k threads floating. It's still just as likely that one completion will get delayed by something less important. Now with all of these threads on a box like this, you get nasty NUMA interactions too.
I'm not suggesting to scale out - the number of vcpus (across all guests) will usually be higher than the number of cpus. But if you have multiple device threads, the scheduler has flexibility in placing them around and filling bubbles. A single heavily loaded iothread is more difficult.
Quoted 1 line
The difference between the two models is that with threads, we rely on pre-emption to enforce fairness and the Linux scheduler to perform scheduling. With a single IO thread, we're determining execution order and priority.
We could define priorities with multiple threads as well (using thread priorities), and we'd never have a short task delayed behind a long task, unless the host is out of resources.
Quoted 2 lines
A lot of main loops have a notion of priority for timer and idle callbacks. For something that is latency sensitive, you absolutely could introduce the concept of priority for bottom halves. It would ensure that a +1 priority bottom half would get scheduled before handling any lower priority I/O/BHs.
What if it becomes available after the low prio task has started to run?
Quoted 4 lines
Quoted 2 lines
Note, an alternative to multiple iothreads is to move completion handling back to vcpus, provided we can steer the handler close to the guest completion handler.
Looking at something like linux-aio, I think we might actually want to do that. We can submit the request from the VCPU thread and we can certainly program the signal to get delivered to that VCPU thread. Maintaining affinity for the request is likely a benefit.
Likely to benefit when we have multiqueue virtio.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
For host services though, it's much more difficult to isolate them like this.
What do you mean by host services?
Things like VNC and live migration. Things that aren't directly related to a guest's activity. One model I can imagine is to continue to relegate these things to a single IO thread, but then move device driven callbacks either back to the originating thread or to a dedicated device callback thread. Host services generally have a much lower priority.
Or just 'a thread'. Nothing prevents vnc or live migration from running in a thread, using the current code.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I'm not necessarily claiming that this will never be the right thing to do, but I don't think we really have the evidence today to suggest that we should focus on this in the short term.
Agreed. We will start to see evidence (one way or the other) as fully loaded 64-vcpu guests are benchmarked. Another driver may be real-time guests; if a timer can be deferred by some block device initiation or completion, then we can say goodbye to any realtime guarantees we want to make.
I'm wary of making decisions based on performance of a 64-vcpu guest. It's an important workload to characterize because it's an extreme case but I think 64 1-vcpu guests will continue to be significantly more important than 1 64-vcpu guest.
Agreed. 64-vcpu guests will make the headlines and marketing checklists, though.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.