[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 10/40] virtio: combine the read of a descriptor
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PATCH 10/40] virtio: combine the read of a descriptor |
Date: |
Tue, 24 Nov 2015 19:01:01 +0100 |
Compared to vring, virtio has a performance penalty of 10%. Fix it
by combining all the reads for a descriptor in a single address_space_read
call. This also simplifies the code nicely.
Signed-off-by: Paolo Bonzini <address@hidden>
---
hw/virtio/virtio.c | 86 ++++++++++++++++++++++--------------------------------
1 file changed, 35 insertions(+), 51 deletions(-)
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 0163d0f..0e9fff6 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -107,35 +107,15 @@ void virtio_queue_update_rings(VirtIODevice *vdev, int n)
vring->align);
}
-static inline uint64_t vring_desc_addr(VirtIODevice *vdev, hwaddr desc_pa,
- int i)
+static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc,
+ hwaddr desc_pa, int i)
{
- hwaddr pa;
- pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr);
- return virtio_ldq_phys(vdev, pa);
-}
-
-static inline uint32_t vring_desc_len(VirtIODevice *vdev, hwaddr desc_pa, int
i)
-{
- hwaddr pa;
- pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len);
- return virtio_ldl_phys(vdev, pa);
-}
-
-static inline uint16_t vring_desc_flags(VirtIODevice *vdev, hwaddr desc_pa,
- int i)
-{
- hwaddr pa;
- pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags);
- return virtio_lduw_phys(vdev, pa);
-}
-
-static inline uint16_t vring_desc_next(VirtIODevice *vdev, hwaddr desc_pa,
- int i)
-{
- hwaddr pa;
- pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next);
- return virtio_lduw_phys(vdev, pa);
+ address_space_read(&address_space_memory, desc_pa + i * sizeof(VRingDesc),
+ MEMTXATTRS_UNSPECIFIED, (void *)desc,
sizeof(VRingDesc));
+ virtio_tswap64s(vdev, &desc->addr);
+ virtio_tswap32s(vdev, &desc->len);
+ virtio_tswap16s(vdev, &desc->flags);
+ virtio_tswap16s(vdev, &desc->next);
}
static inline uint16_t vring_avail_flags(VirtQueue *vq)
@@ -345,18 +325,18 @@ static unsigned int virtqueue_get_head(VirtQueue *vq,
unsigned int idx)
return head;
}
-static unsigned virtqueue_next_desc(VirtIODevice *vdev, hwaddr desc_pa,
- unsigned int i, unsigned int max)
+static unsigned virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
+ hwaddr desc_pa, unsigned int max)
{
unsigned int next;
/* If this descriptor says it doesn't chain, we're done. */
- if (!(vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_NEXT)) {
+ if (!(desc->flags & VRING_DESC_F_NEXT)) {
return max;
}
/* Check they're not leading us off end of descriptors. */
- next = vring_desc_next(vdev, desc_pa, i);
+ next = desc->next;
/* Make sure compiler knows to grab that: we don't want it changing! */
smp_wmb();
@@ -365,6 +345,7 @@ static unsigned virtqueue_next_desc(VirtIODevice *vdev,
hwaddr desc_pa,
exit(1);
}
+ vring_desc_read(vdev, desc, desc_pa, next);
return next;
}
@@ -381,6 +362,7 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int
*in_bytes,
while (virtqueue_num_heads(vq, idx)) {
VirtIODevice *vdev = vq->vdev;
unsigned int max, num_bufs, indirect = 0;
+ VRingDesc desc;
hwaddr desc_pa;
int i;
@@ -388,9 +370,10 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int
*in_bytes,
num_bufs = total_bufs;
i = virtqueue_get_head(vq, idx++);
desc_pa = vq->vring.desc;
+ vring_desc_read(vdev, &desc, desc_pa, i);
- if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_INDIRECT) {
- if (vring_desc_len(vdev, desc_pa, i) % sizeof(VRingDesc)) {
+ if (desc.flags & VRING_DESC_F_INDIRECT) {
+ if (desc.len % sizeof(VRingDesc)) {
error_report("Invalid size for indirect buffer table");
exit(1);
}
@@ -403,9 +386,10 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int
*in_bytes,
/* loop over the indirect descriptor table */
indirect = 1;
- max = vring_desc_len(vdev, desc_pa, i) / sizeof(VRingDesc);
- desc_pa = vring_desc_addr(vdev, desc_pa, i);
+ max = desc.len / sizeof(VRingDesc);
+ desc_pa = desc.addr;
num_bufs = i = 0;
+ vring_desc_read(vdev, &desc, desc_pa, i);
}
do {
@@ -415,15 +399,15 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned
int *in_bytes,
exit(1);
}
- if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_WRITE) {
- in_total += vring_desc_len(vdev, desc_pa, i);
+ if (desc.flags & VRING_DESC_F_WRITE) {
+ in_total += desc.flags;
} else {
- out_total += vring_desc_len(vdev, desc_pa, i);
+ out_total += desc.flags;
}
if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
goto done;
}
- } while ((i = virtqueue_next_desc(vdev, desc_pa, i, max)) != max);
+ } while ((i = virtqueue_read_next_desc(vdev, &desc, desc_pa, max)) !=
max);
if (!indirect)
total_bufs = num_bufs;
@@ -544,6 +528,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
unsigned out_num, in_num;
hwaddr addr[VIRTQUEUE_MAX_SIZE];
struct iovec iov[VIRTQUEUE_MAX_SIZE];
+ VRingDesc desc;
if (!virtqueue_num_heads(vq, vq->last_avail_idx)) {
return NULL;
@@ -559,33 +544,32 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
vring_set_avail_event(vq, vq->last_avail_idx);
}
- if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_INDIRECT) {
- if (vring_desc_len(vdev, desc_pa, i) % sizeof(VRingDesc)) {
+ vring_desc_read(vdev, &desc, desc_pa, i);
+ if (desc.flags & VRING_DESC_F_INDIRECT) {
+ if (desc.len % sizeof(VRingDesc)) {
error_report("Invalid size for indirect buffer table");
exit(1);
}
/* loop over the indirect descriptor table */
- max = vring_desc_len(vdev, desc_pa, i) / sizeof(VRingDesc);
- desc_pa = vring_desc_addr(vdev, desc_pa, i);
+ max = desc.len / sizeof(VRingDesc);
+ desc_pa = desc.addr;
i = 0;
+ vring_desc_read(vdev, &desc, desc_pa, i);
}
/* Collect all the descriptors */
do {
- hwaddr pa = vring_desc_addr(vdev, desc_pa, i);
- size_t len = vring_desc_len(vdev, desc_pa, i);
-
- if (vring_desc_flags(vdev, desc_pa, i) & VRING_DESC_F_WRITE) {
+ if (desc.flags & VRING_DESC_F_WRITE) {
virtqueue_map_desc(&in_num, addr + out_num, iov + out_num,
- VIRTQUEUE_MAX_SIZE - out_num, 1, pa, len);
+ VIRTQUEUE_MAX_SIZE - out_num, 1, desc.addr,
desc.len);
} else {
if (in_num) {
error_report("Incorrect order for descriptors");
exit(1);
}
virtqueue_map_desc(&out_num, addr, iov,
- VIRTQUEUE_MAX_SIZE, 0, pa, len);
+ VIRTQUEUE_MAX_SIZE, 0, desc.addr, desc.len);
}
/* If we've got too many, that implies a descriptor loop. */
@@ -593,7 +577,7 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz)
error_report("Looped descriptor");
exit(1);
}
- } while ((i = virtqueue_next_desc(vdev, desc_pa, i, max)) != max);
+ } while ((i = virtqueue_read_next_desc(vdev, &desc, desc_pa, max)) != max);
/* Now copy what we have collected and mapped */
elem = virtqueue_alloc_element(sz, out_num, in_num);
--
1.8.3.1
- [Qemu-devel] [PATCH 02/40] virtio: move VirtQueueElement at the beginning of the structs, (continued)
- [Qemu-devel] [PATCH 02/40] virtio: move VirtQueueElement at the beginning of the structs, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 06/40] virtio: introduce virtqueue_alloc_element, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 03/40] virtio: move allocation to virtqueue_pop/vring_pop, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 04/40] virtio: introduce qemu_get/put_virtqueue_element, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 07/40] virtio: slim down allocation of VirtQueueElements, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 08/40] vring: slim down allocation of VirtQueueElements, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 09/40] vring: make vring_enable_notification return void, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 13/40] virtio-blk: fix "disabled data plane" mode, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 05/40] virtio: read/write the VirtQueueElement a field at a time, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 12/40] virtio: export vring_notify as virtio_should_notify, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 10/40] virtio: combine the read of a descriptor,
Paolo Bonzini <=
- [Qemu-devel] [PATCH 11/40] virtio: add AioContext-specific function for host notifiers, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 14/40] virtio-blk: do not use vring in dataplane, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 15/40] virtio-scsi: do not use vring in dataplane, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 16/40] vring: remove, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 20/40] aio: rename bh_lock to list_lock, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 22/40] aio: make ctx->list_lock a QemuLockCnt, subsuming ctx->walking_bh, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 19/40] aio: convert from RFifoLock to QemuRecMutex, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 17/40] iothread: release AioContext around aio_poll, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 18/40] qemu-thread: introduce QemuRecMutex, Paolo Bonzini, 2015/11/24
- [Qemu-devel] [PATCH 24/40] aio: tweak walking in dispatch phase, Paolo Bonzini, 2015/11/24