kvm tools: Add scatter-gather variants of IO functions
PATCH v2 1/2 kvm tools: Add scatter-gather variants of IO functions
Added scatter-gather variants of [x,xp][read,write]() and [p]read_in_full().
--- tools/kvm/include/kvm/read-write.h | 13 +++ tools/kvm/read-write.c | 172 ++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 0 deletions(-)
tools/kvm/include/kvm/read-write.h +13 -0
@@ -2,6 +2,7 @@#define KVM_READ_WRITE_H #include <sys/types.h> +#include <sys/uio.h> #include <unistd.h> ssize_t xread (int fd,void *buf,size_t count);
@@ -16,4 +17,16 @@ ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset);ssize_t pread_in_full (int fd,void *buf,size_t count,off_t offset);ssize_t pwrite_in_full (int fd,const void *buf,size_t count,off_t offset); +ssize_t xreadv (int fd,const struct iovec *iov,int iovcnt); +ssize_t xwritev (int fd,const struct iovec *iov,int iovcnt); + +ssize_t readv_in_full (int fd,const struct iovec *iov,int iovcnt); +ssize_t writev_in_full (int fd,const struct iovec *iov,int iovcnt); + +ssize_t xpreadv (int fd,const struct iovec *iov,int iovcnt,off_t offset); +ssize_t xpwritev (int fd,const struct iovec *iov,int iovcnt,off_t offset); + +ssize_t preadv_in_full (int fd,const struct iovec *iov,int iovcnt,off_t offset); +ssize_t pwritev_in_full (int fd,const struct iovec *iov,int iovcnt,off_t offset); +#endif /* KVM_READ_WRITE_H */
tools/kvm/read-write.c +172 -0
175 lines not on this page · raw message
Signature · 1 line
1.7.5.rc1
PATCH v2 2/2 kvm tools: Add scatter-gather support for disk images
Add optional support for scatter-gather to disk_image.
Formats that can't take advantage of scatter-gather fallback to simple IO.
--- tools/kvm/disk-image.c | 16 ++++++++++++ tools/kvm/include/kvm/disk-image.h | 31 +++++++++++++++++++++++ tools/kvm/virtio-blk.c | 47 +++++++++++------------------------ 3 files changed, 62 insertions(+), 32 deletions(-)
tools/kvm/disk-image.c +16 -0
@@ -72,6 +72,20 @@ static int raw_image__write_sector(struct disk_image *self, uint64_t sector, voireturn 0 ; } +static ssize_t raw_image__read_sector_sg (struct disk_image *self,uint64_t sector,const struct iovec *iov,int iovcount) +{ +uint64_t offset = sector <<SECTOR_SHIFT ; + +return preadv_in_full (self->fd, iov, iovcount, offset); +} + +static ssize_t raw_image__write_sector_sg (struct disk_image *self,uint64_t sector,const struct iovec *iov,int iovcount) +{ +uint64_t offset = sector <<SECTOR_SHIFT ; + +return pwritev_in_full (self->fd, iov, iovcount, offset); +} +static int raw_image__read_sector_ro_mmap (struct disk_image *self,uint64_t sector,void *dst,uint32_t dst_len) {uint64_t offset = sector <<SECTOR_SHIFT ;
@@ -105,6 +119,8 @@ static void raw_image__close_ro_mmap(struct disk_image *self)static struct disk_image_operations raw_image_ops = { .read_sector = raw_image__read_sector, .write_sector = raw_image__write_sector, + .read_sector_sg = raw_image__read_sector_sg, + .write_sector_sg = raw_image__write_sector_sg };static struct disk_image_operations raw_image_ro_mmap_ops = {
tools/kvm/include/kvm/disk-image.h +31 -0
@@ -3,6 +3,7 @@#include <stdint.h> #include <stdbool.h> +#include <sys/uio.h> #define SECTOR_SHIFT 9#define SECTOR_SIZE (1UL << SECTOR_SHIFT)
@@ -12,6 +13,8 @@ struct disk_image;struct disk_image_operations {int (*read_sector)(struct disk_image *self,uint64_t sector,void *dst,uint32_t dst_len);int (*write_sector)(struct disk_image *self,uint64_t sector,void *src,uint32_t src_len); +ssize_t (*read_sector_sg)(struct disk_image *self,uint64_t sector,const struct iovec *iov,int iovcount); +ssize_t (*write_sector_sg)(struct disk_image *self,uint64_t sector,const struct iovec *iov,int iovcount);void (*close)(struct disk_image *self); };
@@ -37,4 +40,32 @@ static inline int disk_image__write_sector(struct disk_image *self, uint64_t secreturn self->ops->write_sector (self, sector, src, src_len); } +static inline ssize_t disk_image__read_sector_sg (struct disk_image *self,uint64_t sector,const struct iovec *iov,int iovcount) +{ +if (self->ops->read_sector_sg) +return self->ops->read_sector_sg (self, sector, iov, iovcount); + +while (iovcount--) { + self->ops->read_sector (self, sector, iov->iov_base, iov->iov_len); + sector += iov->iov_len >>SECTOR_SHIFT ; + iov++; + } + +return sector <<SECTOR_SHIFT ; +} + +static inline ssize_t disk_image__write_sector_sg (struct disk_image *self,uint64_t sector,const struct iovec *iov,int iovcount) +{ +if (self->ops->write_sector_sg) +return self->ops->write_sector_sg (self, sector, iov, iovcount); + +while (iovcount--) { + self->ops->write_sector (self, sector, iov->iov_base, iov->iov_len); + sector += iov->iov_len >>SECTOR_SHIFT ; + iov++; + } + +return sector <<SECTOR_SHIFT ; +} +#endif /* KVM__DISK_IMAGE_H */
tools/kvm/virtio-blk.c +15 -32
65 lines not on this page · raw message
Signature · 1 line
1.7.5.rc1
one note below
Quoted 202 lines · Sasha Levin
Added scatter-gather variants of [x,xp][read,write]() and [p]read_in_full().
Signed-off-by Sasha Levin<[email protected]>--- tools/kvm/include/kvm/read-write.h | 13 +++ tools/kvm/read-write.c | 172 ++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 0 deletions(-)tools/kvm/include/kvm/read-write.h +13
-0@@ -2,6 +2,7 @@#define KVM_READ_WRITE_H #include <sys/types.h> +#include <sys/uio.h> #include <unistd.h> ssize_t xread (int fd,void *buf,size_t count);@@ -16,4 +17,16 @@ ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset);ssize_t pread_in_full (int fd,void *buf,size_t count,off_t offset);ssize_t pwrite_in_full (int fd,const void *buf,size_t count,off_t offset); +ssize_t xreadv (int fd,const struct iovec *iov,int iovcnt); +ssize_t xwritev (int fd,const struct iovec *iov,int iovcnt); + +ssize_t readv_in_full (int fd,const struct iovec *iov,int iovcnt); +ssize_t writev_in_full (int fd,const struct iovec *iov,int iovcnt); + +ssize_t xpreadv (int fd,const struct iovec *iov,int iovcnt,off_t offset); +ssize_t xpwritev (int fd,const struct iovec *iov,int iovcnt,off_t offset); + +ssize_t preadv_in_full (int fd,const struct iovec *iov,int iovcnt,off_t offset); +ssize_t pwritev_in_full (int fd,const struct iovec *iov,int iovcnt,off_t offset); +#endif /* KVM_READ_WRITE_H */ tools/kvm/read-write.c +157
-0@@ -152,3 +152,175 @@ ssize_t pwrite_in_full(int fd, const void *buf, size_t count, off_t offset)return total; } + +/* Same as readv(2) except that this function never returns EAGAIN or EINTR. */ +ssize_t xreadv (int fd,const struct iovec *iov,int iovcnt) +{ +ssize_t nr; + +restart: + nr =readv (fd, iov, iovcnt); +if ((nr<0 )&& ((errno ==EAGAIN ) || (errno ==EINTR ))) + goto restart; + +return nr; +} + +/* Same as writev(2) except that this function never returns EAGAIN or EINTR. */ +ssize_t xwritev (int fd,const struct iovec *iov,int iovcnt) +{ +ssize_t nr; + +restart: + nr =write (fd, iov, iovcnt); +if ((nr<0 )&& ((errno ==EAGAIN ) || (errno ==EINTR ))) + goto restart; + +return nr; +} + +static inline ssize_t get_iov_size (const struct iovec *iov,int iovcnt) +{ +size_t size =0 ; +while (iovcnt--) + size += (iov++)->iov_len; + +return size; +} + +ssize_t readv_in_full (int fd,const struct iovec *iov,int iovcnt) +{ +ssize_t total =0 ; +ssize_t count =get_iov_size (iov, iovcnt); + +while (count>0 ) { +ssize_t nr; + + nr =xreadv (fd, iov, iovcnt); +if (nr<=0 ) { +if (total>0 ) +return total; + +return -1 ; + } + +while ((size_t )nr>= iov->iov_len) { + nr -= iov->iov_len; + total += iov->iov_len; + count -= iov->iov_len; + iovcnt--; + iov++; + } + } + +return total; +} + +ssize_t writev_in_full (int fd,const struct iovec *iov,int iovcnt) +{ +ssize_t total =0 ; +size_t count =get_iov_size (iov, iovcnt); + +while (count>0 ) { +ssize_t nr; + + nr =xwritev (fd, iov, iovcnt); +if (nr<0 ) +return -1 ; +if (nr ==0 ) { + errno =ENOSPC ; +return -1 ; + } + +while ((size_t )nr>= iov->iov_len) { + nr -= iov->iov_len; + total += iov->iov_len; + count -= iov->iov_len; + iovcnt--; + iov++; + } + } + +return total; +} + +/* Same as preadv(2) except that this function never returns EAGAIN or EINTR. */ +ssize_t xpreadv (int fd,const struct iovec *iov,int iovcnt,off_t offset) +{ +ssize_t nr; + +restart: + nr =preadv (fd, iov, iovcnt, offset); +if ((nr<0 )&& ((errno ==EAGAIN ) || (errno ==EINTR ))) + goto restart; + +return nr; +} + +/* Same as pwritev(2) except that this function never returns EAGAIN or EINTR. */ +ssize_t xpwritev (int fd,const struct iovec *iov,int iovcnt,off_t offset) +{ +ssize_t nr; + +restart: + nr =pwritev (fd, iov, iovcnt, offset); +if ((nr<0 )&& ((errno ==EAGAIN ) || (errno ==EINTR ))) + goto restart; + +return nr; +} + +ssize_t preadv_in_full (int fd,const struct iovec *iov,int iovcnt,off_t offset) +{ +ssize_t total =0 ; +size_t count =get_iov_size (iov, iovcnt); + +while (count>0 ) { +ssize_t nr; + + nr =xpreadv (fd, iov, iovcnt, offset); +if (nr<=0 ) { +if (total>0 ) +return total; + +return -1 ; + } + +while ((size_t )nr>= iov->iov_len) { + nr -= iov->iov_len; + total += iov->iov_len; + count -= iov->iov_len; + iovcnt--; + iov++; + } + } + +return total; +} + +ssize_t pwritev_in_full (int fd,const struct iovec *iov,int iovcnt,off_t offset) +{ +ssize_t total =0 ; +size_t count =get_iov_size (iov, iovcnt); + +while (count>0 ) { +ssize_t nr; + + nr =xpwritev (fd, iov, iovcnt, offset); +if (nr<0 ) +return -1 ;
tools/kvm/read-write.c ssize_t pwrite_in_full(int fd, const void *buf, size_t count, off_t offset) · line 321
Quoted 10 lines
tools/kvm/read-write.c +10
-0+if (nr ==0 ) { + errno =ENOSPC ; +return -1 ; + } +while ((size_t )nr>= iov->iov_len) { + nr -= iov->iov_len; + total += iov->iov_len; + count -= iov->iov_len; + iovcnt--; + iov++;
You forget here to change offset.
tools/kvm/read-write.c ssize_t pwrite_in_full(int fd, const void *buf, size_t count, off_t offset) · line 205
Quoted 5 lines
tools/kvm/read-write.c +5
-0+ } + } + +return total; +}
It might be better to add an helper function, something like this:
static inline void shift_iovec(const struct iovec **iov, int *iovcnt,
size_t nr, ssize_t *total, off_t *offset)
{
while (nr >= (*iov)->iov_len) {
nr -= (*iov)->iov_len;
*total += (*iov)->iov_len;
*offset += (*iov)->iov_len;
*iovcnt--;
*iov++;
}
}ssize_t pwritev_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offset)
{
ssize_t nr, total = 0;
while (iovcnt) {
nr = xpwritev(fd, iov, iovcnt, offset);
if (nr < 0)
return -1;
if (nr == 0) {
errno = ENOSPC;
return -1;
}
shift_iovec(&iov, &iovcnt, nr, &total, &offset);
}
return total;
}Quoted 15 lines · Konstantin Khlebnikov
It might be better to add an helper function, something like this:
static inline void shift_iovec(const struct iovec **iov, int *iovcnt,
size_tnr,ssize_t*total,off_t*offset){ while (nr >= (*iov)->iov_len) { nr -= (*iov)->iov_len; *total += (*iov)->iov_len; *offset += (*iov)->iov_len; *iovcnt--; *iov++; } }
ssize_tpwritev_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Quoted 16 lines
{
ssize_t nr, total = 0;
while (iovcnt) {
nr = xpwritev(fd, iov, iovcnt, offset);
if (nr < 0)
return -1;
if (nr == 0) {
errno = ENOSPC;
return -1;
}
shift_iovec(&iov, &iovcnt, nr, &total, &offset);
}
return total;
}Yup, Good idea - Let's do that.
Thanks!
Signature · 1 line
Sasha.
Quoted 5 lines · Konstantin Khlebnikov
It might be better to add an helper function, something like this:
static inline void shift_iovec(const struct iovec **iov, int *iovcnt,
size_tnr,ssize_t*total,off_t*offset)
Quoted 10 lines
{
while (nr >= (*iov)->iov_len) {
nr -= (*iov)->iov_len;
*total += (*iov)->iov_len;
*offset += (*iov)->iov_len;
*iovcnt--;
*iov++;
}
}What if nr is nonzero when the loop terminates? You need to update the first iovec entry so you don't redo that segment.
Quoted 21 lines
ssize_tpwritev_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offset){ ssize_t nr, total = 0; while (iovcnt) { nr = xpwritev(fd, iov, iovcnt, offset); if (nr < 0) return -1; if (nr == 0) { errno = ENOSPC; return -1; } shift_iovec(&iov, &iovcnt, nr, &total, &offset); } return total; }Signature · 3 lines
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 4 lines · Avi Kivity
Quoted 3 lines · Konstantin Khlebnikov
It might be better to add an helper function, something like this:
Quoted 10 lines
Quoted 10 lines
static inline void shift_iovec(const struct iovec **iov, int *iovcnt,
size_tnr,ssize_t*total,off_t*offset){ while (nr>= (*iov)->iov_len) { nr -= (*iov)->iov_len; *total += (*iov)->iov_len; *offset += (*iov)->iov_len; *iovcnt--; *iov++; }
if (nr) {
(*iov)->iov_base += nr;
(*iov)->iov_len -= nr;
}Quoted 5 lines
Quoted 2 lines
}
What if nr is nonzero when the loop terminates? You need to update the first iovec entry so you don't redo that segment.
Oh yes, it is simple (hunk above), so "const" from prototype must be removed, while original syscalls declare iov argument as const.
Quoted 25 lines
Quoted 23 lines
ssize_tpwritev_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offset){ ssize_t nr, total = 0; while (iovcnt) { nr = xpwritev(fd, iov, iovcnt, offset); if (nr< 0) return -1; if (nr == 0) { errno = ENOSPC; return -1; } shift_iovec(&iov,&iovcnt, nr,&total,&offset); } return total; }--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 6 lines · Konstantin Khlebnikov
Quoted 2 lines · Avi Kivity
What if nr is nonzero when the loop terminates? You need to update the first iovec entry so you don't redo that segment.
Oh yes, it is simple (hunk above), so "const" from prototype must be removed, while original syscalls declare iov argument as const.
The problem here is that if we start changing the internals of the iovec struct it forces us to copy it before calling the I/O functions since if we change it within the functions it's unusable to the caller anymore.
Currently it's passed as a const because we only move pointers outside the struct.
Basically the trade-off here is copying iovec before every read/write vs having to sometimes read data again (< 1 block), so I've went with the latter.
Do you think it's better to go with the first alternative?
Signature · 1 line
Sasha.
Quoted 12 lines · Sasha Levin
Quoted 6 lines · Konstantin Khlebnikov
Quoted 2 lines · Avi Kivity
What if nr is nonzero when the loop terminates? You need to update the first iovec entry so you don't redo that segment.
Oh yes, it is simple (hunk above), so "const" from prototype must be removed, while original syscalls declare iov argument as const.
The problem here is that if we start changing the internals of the iovec struct it forces us to copy it before calling the I/O functions since if we change it within the functions it's unusable to the caller anymore.
Quoted 10 lines
Currently it's passed as a const because we only move pointers outside the struct.
Basically the trade-off here is copying iovec before every read/write vs having to sometimes read data again (< 1 block), so I've went with the latter.
Do you think it's better to go with the first alternative?
Or we can call one non-vectored operation for the rest of last io-verctor.
Quoted 20 lines · Konstantin Khlebnikov
Quoted 19 lines · Sasha Levin
Quoted 6 lines · Konstantin Khlebnikov
Quoted 2 lines · Avi Kivity
What if nr is nonzero when the loop terminates? You need to update the first iovec entry so you don't redo that segment.
Oh yes, it is simple (hunk above), so "const" from prototype must be removed, while original syscalls declare iov argument as const.
The problem here is that if we start changing the internals of the iovec struct it forces us to copy it before calling the I/O functions since if we change it within the functions it's unusable to the caller anymore.
Currently it's passed as a const because we only move pointers outside the struct.
Basically the trade-off here is copying iovec before every read/write vs having to sometimes read data again (< 1 block), so I've went with the latter.
Quoted 5 lines
Quoted 3 lines
Do you think it's better to go with the first alternative?
Or we can call one non-vectored operation for the rest of last io-verctor.
I don't think it's worth delaying the next readv() to run through read_in_full, and whats more, the part we're going to read again is probably sitting in the file cache anyway.
Signature · 1 line
Sasha.
Quoted 3 lines · Sasha Levin
I don't think it's worth delaying the next
readv()to run throughread_in_full, and whats more, the part we're going to read again is probably sitting in the file cache anyway.
If you have a file cache. High performance often means bypassing the cache, and in those cases submitting guest requests exactly as the guest passed them is important.
Signature · 1 line
error compiling committee.c: too many arguments to function