From patchwork Fri Apr 23 17:04:14 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 94720 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o3NH8okK014082 for ; Fri, 23 Apr 2010 17:09:18 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758408Ab0DWRIy (ORCPT ); Fri, 23 Apr 2010 13:08:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:11352 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758365Ab0DWRIr (ORCPT ); Fri, 23 Apr 2010 13:08:47 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o3NH8kEW011960 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 23 Apr 2010 13:08:46 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o3NH8j6V009967; Fri, 23 Apr 2010 13:08:45 -0400 Received: from amt.cnet (vpn-11-16.rdu.redhat.com [10.11.11.16]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o3NH8i1w010365; Fri, 23 Apr 2010 13:08:44 -0400 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 5F28868A9C4; Fri, 23 Apr 2010 14:07:55 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id o3NH7tD1000871; Fri, 23 Apr 2010 14:07:55 -0300 Message-Id: <20100423170646.131945715@amt.cnet> User-Agent: quilt/0.47-1 Date: Fri, 23 Apr 2010 14:04:14 -0300 From: Marcelo Tosatti To: kvm@vger.kernel.org Cc: qemu-devel@nongnu.org, Marcelo Tosatti Subject: [uq/master patch 4/5] kvm: port qemu-kvm's bitmap scanning References: <20100423170410.914857113@amt.cnet> Content-Disposition: inline; filename=use-qemukvm-bitmap-scan X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 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.3 (demeter.kernel.org [140.211.167.41]); Fri, 23 Apr 2010 17:09:18 +0000 (UTC) Index: qemu-kvm/kvm-all.c =================================================================== --- qemu-kvm.orig/kvm-all.c +++ qemu-kvm/kvm-all.c @@ -26,6 +26,7 @@ #include "hw/hw.h" #include "gdbstub.h" #include "kvm.h" +#include "bswap.h" /* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */ #define PAGE_SIZE TARGET_PAGE_SIZE @@ -305,11 +306,41 @@ static int kvm_set_migration_log(int ena return 0; } -static int test_le_bit(unsigned long nr, unsigned char *addr) -{ - return (addr[nr >> 3] >> (nr & 7)) & 1; +/* get kvm's dirty pages bitmap and update qemu's */ +static int kvm_get_dirty_pages_log_range(unsigned long start_addr, + unsigned long *bitmap, + unsigned long offset, + unsigned long mem_size) +{ + unsigned int i, j; + unsigned long page_number, addr, addr1, c; + ram_addr_t ram_addr; + unsigned int len = ((mem_size / TARGET_PAGE_SIZE) + HOST_LONG_BITS - 1) / + HOST_LONG_BITS; + + /* + * bitmap-traveling is faster than memory-traveling (for addr...) + * especially when most of the memory is not dirty. + */ + for (i = 0; i < len; i++) { + if (bitmap[i] != 0) { + c = leul_to_cpu(bitmap[i]); + do { + j = ffsl(c) - 1; + c &= ~(1ul << j); + page_number = i * HOST_LONG_BITS + j; + addr1 = page_number * TARGET_PAGE_SIZE; + addr = offset + addr1; + ram_addr = cpu_get_physical_page_desc(addr); + cpu_physical_memory_set_dirty(ram_addr); + } while (c != 0); + } + } + return 0; } +#define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1)) + /** * kvm_physical_sync_dirty_bitmap - Grab dirty bitmap from kernel space * This function updates qemu's dirty bitmap using cpu_physical_memory_set_dirty(). @@ -323,8 +354,6 @@ static int kvm_physical_sync_dirty_bitma { KVMState *s = kvm_state; unsigned long size, allocated_size = 0; - target_phys_addr_t phys_addr; - ram_addr_t addr; KVMDirtyLog d; KVMSlot *mem; int ret = 0; @@ -336,7 +365,7 @@ static int kvm_physical_sync_dirty_bitma break; } - size = ((mem->memory_size >> TARGET_PAGE_BITS) + 7) / 8; + size = ALIGN(((mem->memory_size) >> TARGET_PAGE_BITS), HOST_LONG_BITS) / 8; if (!d.dirty_bitmap) { d.dirty_bitmap = qemu_malloc(size); } else if (size > allocated_size) { @@ -353,17 +382,9 @@ static int kvm_physical_sync_dirty_bitma break; } - for (phys_addr = mem->start_addr, addr = mem->phys_offset; - phys_addr < mem->start_addr + mem->memory_size; - phys_addr += TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { - unsigned char *bitmap = (unsigned char *)d.dirty_bitmap; - unsigned nr = (phys_addr - mem->start_addr) >> TARGET_PAGE_BITS; - - if (test_le_bit(nr, bitmap)) { - cpu_physical_memory_set_dirty(addr); - } - } - start_addr = phys_addr; + kvm_get_dirty_pages_log_range(mem->start_addr, d.dirty_bitmap, + mem->start_addr, mem->memory_size); + start_addr = mem->start_addr + mem->memory_size; } qemu_free(d.dirty_bitmap);