diff mbox

[1/2] kvm tool: Use mmap for working with disk image

Message ID 1302345496-20669-1-git-send-email-levinsasha928@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Sasha Levin April 9, 2011, 10:38 a.m. UTC
Attempt to use mmap first for working with a disk image, if the attempt is failed (for example, large image on a 32bit system) fallback to using read/write.

Performance (kB/s) test using bonnie++ showed the following improvement:

Sequential write: 14%
Sequential rewrite: 15%
Sequential read: 7%
Random seek latency: 82%

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 tools/kvm/disk-image.c             |   54 +++++++++++++++++++++++++++++------
 tools/kvm/include/kvm/disk-image.h |    3 +-
 2 files changed, 46 insertions(+), 11 deletions(-)

Comments

Pekka Enberg April 9, 2011, 11:29 a.m. UTC | #1
On Sat, 9 Apr 2011, Sasha Levin wrote:

> Attempt to use mmap first for working with a disk image, if the attempt is failed (for example, large image on a 32bit system) fallback to using read/write.
>
> Performance (kB/s) test using bonnie++ showed the following improvement:
>
> Sequential write: 14%
> Sequential rewrite: 15%
> Sequential read: 7%
> Random seek latency: 82%

Can you also please share the before and after raw data?

> -struct disk_image *disk_image__new(int fd, uint64_t size, struct disk_image_operations *ops)
> +static int raw_image__read_sector_mmap(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len)
> {
> -	struct disk_image *self;
> +	uint64_t offset = sector << SECTOR_SHIFT;
>
> -	self		= malloc(sizeof *self);
> -	if (!self)
> -		return NULL;
> +	if (offset + dst_len > self->size)
> +		return -1;
>
> -	self->fd	= fd;
> -	self->size	= size;
> -	self->ops	= ops;
> +	memmove(dst, self->mapping + offset, dst_len);

Why do you want to use memmove() here? The areas cannot overlap.

 			Pekka
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/tools/kvm/disk-image.c b/tools/kvm/disk-image.c
index 9deaf45..ae2fa2a 100644
--- a/tools/kvm/disk-image.c
+++ b/tools/kvm/disk-image.c
@@ -13,21 +13,31 @@ 
 #include <unistd.h>
 #include <fcntl.h>
 
-struct disk_image *disk_image__new(int fd, uint64_t size, struct disk_image_operations *ops)
+static int raw_image__read_sector_mmap(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len)
 {
-	struct disk_image *self;
+	uint64_t offset = sector << SECTOR_SHIFT;
 
-	self		= malloc(sizeof *self);
-	if (!self)
-		return NULL;
+	if (offset + dst_len > self->size)
+		return -1;
 
-	self->fd	= fd;
-	self->size	= size;
-	self->ops	= ops;
+	memmove(dst, self->mapping + offset, dst_len);
 
-	return self;
+	return 0;
+}
+
+static int raw_image__write_sector_mmap(struct disk_image *self, uint64_t sector, void *src, uint32_t src_len)
+{
+	uint64_t offset = sector << SECTOR_SHIFT;
+
+	if (offset + src_len > self->size)
+		return -1;
+
+	memmove(self->mapping + offset, src, src_len);
+
+	return 0;
 }
 
+
 static int raw_image__read_sector(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len)
 {
 	uint64_t offset = sector << SECTOR_SHIFT;
@@ -59,6 +69,27 @@  static struct disk_image_operations raw_image_ops = {
 	.write_sector		= raw_image__write_sector,
 };
 
+static struct disk_image_operations raw_image_mmap_ops = {
+	.read_sector		= raw_image__read_sector_mmap,
+	.write_sector		= raw_image__write_sector_mmap,
+};
+
+struct disk_image *disk_image__new(int fd, uint64_t size)
+{
+	struct disk_image *self;
+
+	self		= malloc(sizeof *self);
+	if (!self)
+		return NULL;
+
+	self->fd	= fd;
+	self->size	= size;
+	self->mapping	= mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
+	self->ops 	= (self->mapping == MAP_FAILED) ? &raw_image_ops : &raw_image_mmap_ops;
+
+	return self;
+}
+
 static struct disk_image *raw_image__probe(int fd)
 {
 	struct stat st;
@@ -66,7 +97,7 @@  static struct disk_image *raw_image__probe(int fd)
 	if (fstat(fd, &st) < 0)
 		return NULL;
 
-	return disk_image__new(fd, st.st_size, &raw_image_ops);
+	return disk_image__new(fd, st.st_size);
 }
 
 struct disk_image *disk_image__open(const char *filename)
@@ -97,6 +128,9 @@  void disk_image__close(struct disk_image *self)
 	if (self->ops->close)
 		self->ops->close(self);
 
+	if (self->mapping != MAP_FAILED)
+		munmap(self->mapping, self->size);
+
 	if (close(self->fd) < 0)
 		warning("close() failed");
 
diff --git a/tools/kvm/include/kvm/disk-image.h b/tools/kvm/include/kvm/disk-image.h
index df0a15d..001a8d5 100644
--- a/tools/kvm/include/kvm/disk-image.h
+++ b/tools/kvm/include/kvm/disk-image.h
@@ -18,11 +18,12 @@  struct disk_image {
 	int				fd;
 	uint64_t			size;
 	struct disk_image_operations	*ops;
+	void				*mapping;
 	void				*priv;
 };
 
 struct disk_image *disk_image__open(const char *filename);
-struct disk_image *disk_image__new(int fd, uint64_t size, struct disk_image_operations *ops);
+struct disk_image *disk_image__new(int fd, uint64_t size);
 void disk_image__close(struct disk_image *self);
 
 static inline int disk_image__read_sector(struct disk_image *self, uint64_t sector, void *dst, uint32_t dst_len)