From patchwork Sat Apr 9 12:14:08 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 695901 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 p39CEVHh009925 for ; Sat, 9 Apr 2011 12:14:31 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754602Ab1DIMO2 (ORCPT ); Sat, 9 Apr 2011 08:14:28 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:57354 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754415Ab1DIMO1 (ORCPT ); Sat, 9 Apr 2011 08:14:27 -0400 Received: by wya21 with SMTP id 21so3728461wya.19 for ; Sat, 09 Apr 2011 05:14:26 -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=wNItKJz/LvZMAiOqNB/8a2cVTLu8gpSgFH4tUGSUOZI=; b=Xth9a1r0uVPB+JyRdkkIRlLYz1m8R6cYmqaI32kZ+NJ22UmZU+uh6xKcOHU0WeFmlL j1jaq6wcg/mJwbyJkZ4kUKhBPu7kILiPlE+d5t5OqqETE0I4ctPLGVgDKhW1WrT5IYzh 2CrVRISJSSwAMAyBu3C/hrhXJ3M04d3Ih5Y9E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=JVIUCKUgH9X0gp+MpL4A1hFc5ZElPanBZGabxSNJzRqcjJOpddhQq+4pZCpFauXszW E23YzlANOY9rfvtu4x+H+9xNqPHiPcPw7e6k2H2lhn+7KsAv7IzNmfUBd+WruhkHrlP6 lJlbFM4iWtFry7iGUwa6y2haBnQvzDzZOgJzk= Received: by 10.227.20.13 with SMTP id d13mr3297687wbb.133.1302351266336; Sat, 09 Apr 2011 05:14:26 -0700 (PDT) Received: from localhost.localdomain ([94.230.80.142]) by mx.google.com with ESMTPS id x1sm2243078wbh.19.2011.04.09.05.14.25 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 09 Apr 2011 05:14:26 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: kvm@vger.kernel.org, Sasha Levin Subject: [PATCH] kvm tools: Use mmap for working with disk image V2 Date: Sat, 9 Apr 2011 15:14:08 +0300 Message-Id: <1302351248-4853-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 12:14:31 +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: kvm cmdline: ./kvm run --mem=256 --image=./work/vms/gentoo.img --kernel=/boot/bzImage-git bonnie++ cmdline: bonnie++ -u 0 Before: Version 1.96 ------Sequential Output----- --Sequential Input- -Random- Concurrency 1 -Per Chr- --Block-- -Rewrite- -Per Chr- --Block-- --Seeks-- Machine Size K/sec %CP K/sec %CP K/sec %CP K/sec %CP K/sec %CP /sec %CP tux 480M 673 100 308017 61 288156 49 3286 99 892186 76 +++++ +++ Latency 12998us 50992us 35993us 3000us 1999us 201ms Version 1.96 ------Sequential Create------ --------Random Create-------- tux -Create-- --Read--- -Delete-- -Create-- --Read--- -Delete-- files /sec %CP /sec %CP /sec %CP /sec %CP /sec %CP /sec %CP 16 +++++ +++ +++++ +++ +++++ +++ +++++ +++ +++++ +++ +++++ +++ Latency 3000us 1000us 1000us 1000us 1998us 1000us After: Version 1.96 ------Sequential Output------ --Sequential Input- --Random- Concurrency 1 -Per Chr- --Block-- -Rewrite- -Per Chr- --Block-- --Seeks-- Machine Size K/sec %CP K/sec %CP K/sec %CP K/sec %CP K/sec %CP /sec %CP tux 480M 672 99 415549 45 310350 52 3495 99 910360 83 +++++ +++ Latency 12998us 7998us 23996us 3998us 1000us 116ms Version 1.96 ------Sequential Create------ --------Random Create-------- tux -Create-- --Read--- -Delete-- -Create-- --Read--- -Delete-- files /sec %CP /sec %CP /sec %CP /sec %CP /sec %CP /sec %CP 16 +++++ +++ +++++ +++ +++++ +++ +++++ +++ +++++ +++ +++++ +++ Latency 1000us 1000us 1000us 2000us 1000us 1999us Signed-off-by: Sasha Levin --- tools/kvm/disk-image.c | 55 +++++++++++++++++++++++++++++------ tools/kvm/include/kvm/disk-image.h | 2 +- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/tools/kvm/disk-image.c b/tools/kvm/disk-image.c index 9deaf45..8c27392 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; + memcpy(dst, self->priv + 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; + + memcpy(self->priv + 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,28 @@ 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; + void*k=&raw_image_mmap_ops; +k=NULL; + self = malloc(sizeof *self); + if (!self) + return NULL; + + self->fd = fd; + self->size = size; + self->priv = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_NORESERVE, fd, 0); + self->ops = (self->priv == MAP_FAILED) ? &raw_image_ops : &raw_image_ops; + + return self; +} + static struct disk_image *raw_image__probe(int fd) { struct stat st; @@ -66,7 +98,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 +129,9 @@ void disk_image__close(struct disk_image *self) if (self->ops->close) self->ops->close(self); + if (self->priv != MAP_FAILED) + munmap(self->priv, 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..8b78657 100644 --- a/tools/kvm/include/kvm/disk-image.h +++ b/tools/kvm/include/kvm/disk-image.h @@ -22,7 +22,7 @@ struct disk_image { }; 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)