From patchwork Sat Apr 9 10:38:15 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 695791 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p39Acmi0028421 for ; Sat, 9 Apr 2011 10:38:48 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754156Ab1DIKij (ORCPT ); Sat, 9 Apr 2011 06:38:39 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:52504 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754030Ab1DIKii (ORCPT ); Sat, 9 Apr 2011 06:38:38 -0400 Received: by wya21 with SMTP id 21so3695295wya.19 for ; Sat, 09 Apr 2011 03:38:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer; bh=IZ+F8jI2PykZLJtQESVlvMFDJDDJpxE6y8TxlZzsvvQ=; b=VUQxNMk2ccUx7Db0z6lBBjlhQZfNcYP5qEfe8w8MHWd+wJVqtppQJFT4Y8R8bfXr0l IBX5rNcIdYms/v2YK0d8L5h3PcwqP8fITgpKEs18tpftAo6VnFnRDgIQYhT7Yr+xYxvF PQ5ZypP9Q6q5MY5AWze6AEmbydqpZ977svgxc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=NRKCbo73rktCaoWG/ChbwnvRmUxSm1Bu4gXYX66M/9P95qw+7R4bsZuhXLjZIeq/8O n+6TTxR39Bq8508Ci1KxT1WEMp+aZJaRfDjheITz3jnHRoa6Q6u9KT8iIHTRjmOzrHWV 0shJrLkRqhUJZmRBTHl3VnPnvJ3vObH0JvT8Y= Received: by 10.216.60.193 with SMTP id u43mr675270wec.103.1302345517369; Sat, 09 Apr 2011 03:38:37 -0700 (PDT) Received: from localhost.localdomain ([94.230.80.142]) by mx.google.com with ESMTPS id g32sm1731531wej.27.2011.04.09.03.38.35 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 09 Apr 2011 03:38:36 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: kvm@vger.kernel.org, Sasha Levin Subject: [PATCH 1/2] kvm tool: Use mmap for working with disk image Date: Sat, 9 Apr 2011 13:38:15 +0300 Message-Id: <1302345496-20669-1-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.5.rc1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Sat, 09 Apr 2011 10:38:48 +0000 (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 --- tools/kvm/disk-image.c | 54 +++++++++++++++++++++++++++++------ tools/kvm/include/kvm/disk-image.h | 3 +- 2 files changed, 46 insertions(+), 11 deletions(-) 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 #include -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)