All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker.
@ 2010-04-20  3:40 Yoshiaki Tamura
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 1/4] Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of bit-based phys_ram_dirty Yoshiaki Tamura
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Yoshiaki Tamura @ 2010-04-20  3:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, ohmura.kei, mtosatti, Yoshiaki Tamura, avi

The dirty and non-dirty pages are checked one by one.  When most of the memory
is not dirty, checking the dirty and non-dirty pages by multiple page size
should be much faster than checking them one by one.  We introduced bit-based
phys_ram_dirty for VGA, CODE, MIGRATION, MASTER, and
cpu_physical_memory_get_dirty_range() for this purpose.
                                                                                
Changes from v3 to v4 are:

- Merged {1,2,3}/6 to compile correctly.
- Fix setting bits after phys_ram_dirty allocation.
- renamed DIRTY_FLAG and DIRTY_IDX converter function.

Changes from v2 to v3 are:

- Change FLAGS value to (1,2,4,8), and add IDX (0,1,2,3)
- Use ffs to convert FLAGS to IDX.
- Add a helper function which takes IDX.
- Change the behavior of MASTER as a buffer.
- Change dirty bitmap access to a loop.
- Add brace after if ()

Yoshiaki Tamura (4):
  Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of
    bit-based phys_ram_dirty.
  Introduce cpu_physical_memory_get_dirty_range().
  Use cpu_physical_memory_set_dirty_range() to update phys_ram_dirty.
  Use cpu_physical_memory_get_dirty_range() to check multiple dirty
    pages.

 arch_init.c   |   54 ++++++++++++++---------
 bswap.h       |    2 +
 cpu-all.h     |  131 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 exec.c        |   82 +++++++++++++++++++++++++++++++++--
 kvm-all.c     |   33 +++++++--------
 qemu-common.h |    3 +
 6 files changed, 242 insertions(+), 63 deletions(-)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Qemu-devel] [PATCH v4 1/4] Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of bit-based phys_ram_dirty.
  2010-04-20  3:40 [Qemu-devel] [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Yoshiaki Tamura
@ 2010-04-20  3:40 ` Yoshiaki Tamura
  2010-05-03 20:03   ` Anthony Liguori
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 2/4] Introduce cpu_physical_memory_get_dirty_range() Yoshiaki Tamura
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Yoshiaki Tamura @ 2010-04-20  3:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, ohmura.kei, mtosatti, Yoshiaki Tamura, avi

Replaces byte-based phys_ram_dirty bitmap with four (MASTER, VGA, CODE,
MIGRATION) bit-based phys_ram_dirty bitmap.  On allocation, it sets all bits in
the bitmap.  It uses ffs() to convert DIRTY_FLAG to DIRTY_IDX.

Modifies wrapper functions for byte-based phys_ram_dirty bitmap to bit-based
phys_ram_dirty bitmap.  MASTER works as a buffer, and upon get_diry() or
get_dirty_flags(), it calls cpu_physical_memory_sync_master() to update VGA and
MIGRATION.

Signed-off-by: Yoshiaki Tamura <tamura.yoshiaki@lab.ntt.co.jp>
---
 cpu-all.h     |  127 ++++++++++++++++++++++++++++++++++++++++++++++++---------
 exec.c        |   15 +++++--
 qemu-common.h |    3 +
 3 files changed, 121 insertions(+), 24 deletions(-)

diff --git a/cpu-all.h b/cpu-all.h
index f8bfa66..b6a2d91 100644
--- a/cpu-all.h
+++ b/cpu-all.h
@@ -37,6 +37,9 @@
 
 #include "softfloat.h"
 
+/* to use ffs in flag_to_idx() */
+#include <strings.h>
+
 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
 #define BSWAP_NEEDED
 #endif
@@ -853,7 +856,6 @@ target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr);
 /* memory API */
 
 extern int phys_ram_fd;
-extern uint8_t *phys_ram_dirty;
 extern ram_addr_t ram_size;
 extern ram_addr_t last_ram_offset;
 
@@ -878,50 +880,137 @@ extern int mem_prealloc;
 /* Set if TLB entry is an IO callback.  */
 #define TLB_MMIO        (1 << 5)
 
-#define VGA_DIRTY_FLAG       0x01
-#define CODE_DIRTY_FLAG      0x02
-#define MIGRATION_DIRTY_FLAG 0x08
+/* Use DIRTY_IDX as indexes of bit-based phys_ram_dirty. */
+#define MASTER_DIRTY_IDX    0
+#define VGA_DIRTY_IDX       1
+#define CODE_DIRTY_IDX      2
+#define MIGRATION_DIRTY_IDX 3
+#define NUM_DIRTY_IDX       4
+
+#define MASTER_DIRTY_FLAG    (1 << MASTER_DIRTY_IDX)
+#define VGA_DIRTY_FLAG       (1 << VGA_DIRTY_IDX)
+#define CODE_DIRTY_FLAG      (1 << CODE_DIRTY_IDX)
+#define MIGRATION_DIRTY_FLAG (1 << MIGRATION_DIRTY_IDX)
+
+extern unsigned long *phys_ram_dirty[NUM_DIRTY_IDX];
+
+static inline int dirty_flag_to_idx(int flag)
+{
+    return ffs(flag) - 1;
+}
+
+static inline int dirty_idx_to_flag(int idx)
+{
+    return 1 << idx;
+}
 
 /* read dirty bit (return 0 or 1) */
 static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
 {
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] == 0xff;
+    unsigned long mask;
+    ram_addr_t index = (addr >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+    int offset = (addr >> TARGET_PAGE_BITS) & (HOST_LONG_BITS - 1);
+ 
+    mask = 1UL << offset;
+    return (phys_ram_dirty[MASTER_DIRTY_IDX][index] & mask) == mask;
+}
+
+static inline void cpu_physical_memory_sync_master(ram_addr_t index)
+{
+    if (phys_ram_dirty[MASTER_DIRTY_IDX][index]) {
+        phys_ram_dirty[VGA_DIRTY_IDX][index]
+            |=  phys_ram_dirty[MASTER_DIRTY_IDX][index];
+        phys_ram_dirty[MIGRATION_DIRTY_IDX][index]
+            |=  phys_ram_dirty[MASTER_DIRTY_IDX][index];
+        phys_ram_dirty[MASTER_DIRTY_IDX][index] = 0UL;
+    }
 }
 
 static inline int cpu_physical_memory_get_dirty_flags(ram_addr_t addr)
 {
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS];
+     unsigned long mask;
+     ram_addr_t index = (addr >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+     int offset = (addr >> TARGET_PAGE_BITS) & (HOST_LONG_BITS - 1);
+     int ret = 0, i;
+ 
+     mask = 1UL << offset;
+     cpu_physical_memory_sync_master(index);
+
+     for (i = VGA_DIRTY_IDX; i <= MIGRATION_DIRTY_IDX; i++) {
+         if (phys_ram_dirty[i][index] & mask) {
+             ret |= dirty_idx_to_flag(i);
+         }
+     }
+ 
+     return ret;
+}
+
+static inline int cpu_physical_memory_get_dirty_idx(ram_addr_t addr,
+                                                    int dirty_idx)
+{
+    unsigned long mask;
+    ram_addr_t index = (addr >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+    int offset = (addr >> TARGET_PAGE_BITS) & (HOST_LONG_BITS - 1);
+
+    mask = 1UL << offset;
+    cpu_physical_memory_sync_master(index);
+    return (phys_ram_dirty[dirty_idx][index] & mask) == mask;
 }
 
 static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
                                                 int dirty_flags)
 {
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] & dirty_flags;
+    return cpu_physical_memory_get_dirty_idx(addr,
+                                             dirty_flag_to_idx(dirty_flags));
 }
 
 static inline void cpu_physical_memory_set_dirty(ram_addr_t addr)
 {
-    phys_ram_dirty[addr >> TARGET_PAGE_BITS] = 0xff;
+    unsigned long mask;
+    ram_addr_t index = (addr >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+    int offset = (addr >> TARGET_PAGE_BITS) & (HOST_LONG_BITS - 1);
+
+    mask = 1UL << offset;
+    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
+}
+
+static inline void cpu_physical_memory_set_dirty_range(ram_addr_t addr,
+                                                       unsigned long mask)
+{
+    ram_addr_t index = (addr >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+
+    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
 }
 
-static inline int cpu_physical_memory_set_dirty_flags(ram_addr_t addr,
-                                                      int dirty_flags)
+static inline void cpu_physical_memory_set_dirty_flags(ram_addr_t addr,
+                                                       int dirty_flags)
 {
-    return phys_ram_dirty[addr >> TARGET_PAGE_BITS] |= dirty_flags;
+    unsigned long mask;
+    ram_addr_t index = (addr >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+    int offset = (addr >> TARGET_PAGE_BITS) & (HOST_LONG_BITS - 1);
+
+    mask = 1UL << offset;
+    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
+
+    if (dirty_flags & CODE_DIRTY_FLAG) {
+        phys_ram_dirty[CODE_DIRTY_IDX][index] |= mask;
+    }
 }
 
 static inline void cpu_physical_memory_mask_dirty_range(ram_addr_t start,
-                                                        int length,
+                                                        unsigned long length,
                                                         int dirty_flags)
 {
-    int i, mask, len;
-    uint8_t *p;
+    ram_addr_t addr = start, index;
+    unsigned long mask;
+    int offset, i;
 
-    len = length >> TARGET_PAGE_BITS;
-    mask = ~dirty_flags;
-    p = phys_ram_dirty + (start >> TARGET_PAGE_BITS);
-    for (i = 0; i < len; i++)
-        p[i] &= mask;
+    for (i = 0;  i < length; i += TARGET_PAGE_SIZE) {
+        index = ((addr + i) >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+        offset = ((addr + i) >> TARGET_PAGE_BITS) & (HOST_LONG_BITS - 1);
+        mask = ~(1UL << offset);
+        phys_ram_dirty[dirty_flag_to_idx(dirty_flags)][index] &= mask;
+     }
 }
 
 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
diff --git a/exec.c b/exec.c
index c74b0a4..82b7c32 100644
--- a/exec.c
+++ b/exec.c
@@ -110,7 +110,7 @@ uint8_t *code_gen_ptr;
 
 #if !defined(CONFIG_USER_ONLY)
 int phys_ram_fd;
-uint8_t *phys_ram_dirty;
+unsigned long *phys_ram_dirty[NUM_DIRTY_IDX];
 static int in_migration;
 
 typedef struct RAMBlock {
@@ -2793,6 +2793,7 @@ static void *file_ram_alloc(ram_addr_t memory, const char *path)
 ram_addr_t qemu_ram_alloc(ram_addr_t size)
 {
     RAMBlock *new_block;
+    int i;
 
     size = TARGET_PAGE_ALIGN(size);
     new_block = qemu_malloc(sizeof(*new_block));
@@ -2825,10 +2826,14 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size)
     new_block->next = ram_blocks;
     ram_blocks = new_block;
 
-    phys_ram_dirty = qemu_realloc(phys_ram_dirty,
-        (last_ram_offset + size) >> TARGET_PAGE_BITS);
-    memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS),
-           0xff, size >> TARGET_PAGE_BITS);
+    for (i = MASTER_DIRTY_IDX; i < NUM_DIRTY_IDX; i++) {
+        phys_ram_dirty[i] 
+            = qemu_realloc(phys_ram_dirty[i],
+                           BITMAP_SIZE(last_ram_offset + size));
+        memset((uint8_t *)phys_ram_dirty[i] + BITMAP_SIZE(last_ram_offset),
+               0xff, BITMAP_SIZE(last_ram_offset + size)
+               - BITMAP_SIZE(last_ram_offset));
+    }
 
     last_ram_offset += size;
 
diff --git a/qemu-common.h b/qemu-common.h
index 4ba0cda..efe5b1f 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -285,6 +285,9 @@ static inline uint8_t from_bcd(uint8_t val)
     return ((val >> 4) * 10) + (val & 0x0f);
 }
 
+#define ALIGN(x, y)  (((x)+(y)-1) & ~((y)-1))
+#define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
+
 #include "module.h"
 
 #endif /* dyngen-exec.h hack */
-- 
1.7.0.31.g1df487

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [Qemu-devel] [PATCH v4 2/4] Introduce cpu_physical_memory_get_dirty_range().
  2010-04-20  3:40 [Qemu-devel] [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Yoshiaki Tamura
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 1/4] Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of bit-based phys_ram_dirty Yoshiaki Tamura
@ 2010-04-20  3:40 ` Yoshiaki Tamura
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 3/4] Use cpu_physical_memory_set_dirty_range() to update phys_ram_dirty Yoshiaki Tamura
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Yoshiaki Tamura @ 2010-04-20  3:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, ohmura.kei, mtosatti, Yoshiaki Tamura, avi

It checks the first row and puts dirty addr in the array.  If the first row is
empty, it skips to the first non-dirty row or the end addr, and put the length
in the first entry of the array.

Signed-off-by: Yoshiaki Tamura <tamura.yoshiaki@lab.ntt.co.jp>
Signed-off-by: OHMURA Kei <ohmura.kei@lab.ntt.co.jp>
---
 cpu-all.h |    4 +++
 exec.c    |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/cpu-all.h b/cpu-all.h
index b6a2d91..8b214b0 100644
--- a/cpu-all.h
+++ b/cpu-all.h
@@ -1013,6 +1013,10 @@ static inline void cpu_physical_memory_mask_dirty_range(ram_addr_t start,
      }
 }
 
+int cpu_physical_memory_get_dirty_range(ram_addr_t start, ram_addr_t end, 
+                                        ram_addr_t *dirty_rams, int length,
+                                        int dirty_flags);
+
 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
                                      int dirty_flags);
 void cpu_tlb_update_dirty(CPUState *env);
diff --git a/exec.c b/exec.c
index 82b7c32..a56f9bb 100644
--- a/exec.c
+++ b/exec.c
@@ -2045,6 +2045,73 @@ static inline void tlb_reset_dirty_range(CPUTLBEntry *tlb_entry,
     }
 }
 
+/* It checks the first row and puts dirty addrs in the array.
+   If the first row is empty, it skips to the first non-dirty row
+   or the end addr, and put the length in the first entry of the array. */
+int cpu_physical_memory_get_dirty_range(ram_addr_t start, ram_addr_t end, 
+                                        ram_addr_t *dirty_rams, int length,
+                                        int dirty_flag)
+{
+    unsigned long p = 0, page_number;
+    ram_addr_t addr;
+    ram_addr_t s_idx = (start >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+    ram_addr_t e_idx = (end >> TARGET_PAGE_BITS) / HOST_LONG_BITS;
+    int i, j, offset, dirty_idx = dirty_flag_to_idx(dirty_flag);
+
+    /* mask bits before the start addr */
+    offset = (start >> TARGET_PAGE_BITS) & (HOST_LONG_BITS - 1);
+    cpu_physical_memory_sync_master(s_idx);
+    p |= phys_ram_dirty[dirty_idx][s_idx] & ~((1UL << offset) - 1);
+
+    if (s_idx == e_idx) {
+        /* mask bits after the end addr */
+        offset = (end >> TARGET_PAGE_BITS) & (HOST_LONG_BITS - 1);
+        p &= (1UL << offset) - 1;
+    }
+
+    if (p == 0) {
+        /* when the row is empty */
+        ram_addr_t skip;
+        if (s_idx == e_idx) {
+            skip = end;
+	} else {
+            /* skip empty rows */
+            while (s_idx < e_idx) {
+                s_idx++;
+                cpu_physical_memory_sync_master(s_idx);
+
+                if (phys_ram_dirty[dirty_idx][s_idx] != 0) {
+                    break;
+                }
+            }
+            skip = (s_idx * HOST_LONG_BITS * TARGET_PAGE_SIZE);
+        }
+        dirty_rams[0] = skip - start;
+        i = 0;
+
+    } else if (p == ~0UL) {
+        /* when the row is fully dirtied */
+        addr = start;
+        for (i = 0; i < length; i++) {
+            dirty_rams[i] = addr;
+            addr += TARGET_PAGE_SIZE;
+        }
+    } else {
+        /* when the row is partially dirtied */
+        i = 0;
+        do {
+            j = ffsl(p) - 1;
+            p &= ~(1UL << j);
+            page_number = s_idx * HOST_LONG_BITS + j;
+            addr = page_number * TARGET_PAGE_SIZE;
+            dirty_rams[i] = addr;
+            i++;
+        } while (p != 0 && i < length);
+    }
+
+    return i;
+}
+
 /* Note: start and end must be within the same ram block.  */
 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
                                      int dirty_flags)
-- 
1.7.0.31.g1df487

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [Qemu-devel] [PATCH v4 3/4] Use cpu_physical_memory_set_dirty_range() to update phys_ram_dirty.
  2010-04-20  3:40 [Qemu-devel] [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Yoshiaki Tamura
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 1/4] Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of bit-based phys_ram_dirty Yoshiaki Tamura
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 2/4] Introduce cpu_physical_memory_get_dirty_range() Yoshiaki Tamura
@ 2010-04-20  3:40 ` Yoshiaki Tamura
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 4/4] Use cpu_physical_memory_get_dirty_range() to check multiple dirty pages Yoshiaki Tamura
  2010-04-21 12:15 ` [Qemu-devel] Re: [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Avi Kivity
  4 siblings, 0 replies; 9+ messages in thread
From: Yoshiaki Tamura @ 2010-04-20  3:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, ohmura.kei, mtosatti, Yoshiaki Tamura, avi

Modifies kvm_physical_sync_dirty_bitmap to use
cpu_physical_memory_set_dirty_range() to update the row of the bit-based
phys_ram_dirty bitmap at once.

Signed-off-by: OHMURA Kei <ohmura.kei@lab.ntt.co.jp>
Signed-off-by: Yoshiaki Tamura <tamura.yoshiaki@lab.ntt.co.jp>
---
 bswap.h   |    2 ++
 kvm-all.c |   33 +++++++++++++++------------------
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/bswap.h b/bswap.h
index aace9b7..956f3fa 100644
--- a/bswap.h
+++ b/bswap.h
@@ -205,8 +205,10 @@ static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
 
 #ifdef HOST_WORDS_BIGENDIAN
 #define cpu_to_32wu cpu_to_be32wu
+#define leul_to_cpu(v) le ## HOST_LONG_BITS ## _to_cpu(v)
 #else
 #define cpu_to_32wu cpu_to_le32wu
+#define leul_to_cpu(v) (v)
 #endif
 
 #undef le_bswap
diff --git a/kvm-all.c b/kvm-all.c
index 7aa5e57..db762ff 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -282,11 +282,6 @@ static int kvm_set_migration_log(int enable)
     return 0;
 }
 
-static int test_le_bit(unsigned long nr, unsigned char *addr)
-{
-    return (addr[nr >> 3] >> (nr & 7)) & 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().
@@ -299,9 +294,9 @@ static int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
 					  target_phys_addr_t end_addr)
 {
     KVMState *s = kvm_state;
-    unsigned long size, allocated_size = 0;
-    target_phys_addr_t phys_addr;
-    ram_addr_t addr;
+    unsigned long size, page_number, addr, addr1, *bitmap,
+        allocated_size = 0;
+    unsigned int i, len;
     KVMDirtyLog d;
     KVMSlot *mem;
     int ret = 0;
@@ -313,7 +308,8 @@ static int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
             break;
         }
 
-        size = ((mem->memory_size >> TARGET_PAGE_BITS) + 7) / 8;
+        size = ((mem->memory_size >> TARGET_PAGE_BITS) + HOST_LONG_BITS - 1) /
+            HOST_LONG_BITS * HOST_LONG_SIZE;
         if (!d.dirty_bitmap) {
             d.dirty_bitmap = qemu_malloc(size);
         } else if (size > allocated_size) {
@@ -330,17 +326,18 @@ static int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
             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);
+        bitmap = (unsigned long *)d.dirty_bitmap;
+        len = size / HOST_LONG_SIZE;
+        for (i = 0; i < len; i++) {
+            if (bitmap[i] != 0) {
+                page_number = i * HOST_LONG_BITS;
+                addr1 = page_number * TARGET_PAGE_SIZE;
+                addr = mem->phys_offset + addr1;
+                cpu_physical_memory_set_dirty_range(addr, 
+                    leul_to_cpu(bitmap[i]));
             }
         }
-        start_addr = phys_addr;
+        start_addr = mem->start_addr + mem->memory_size;
     }
     qemu_free(d.dirty_bitmap);
 
-- 
1.7.0.31.g1df487

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [Qemu-devel] [PATCH v4 4/4] Use cpu_physical_memory_get_dirty_range() to check multiple dirty pages.
  2010-04-20  3:40 [Qemu-devel] [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Yoshiaki Tamura
                   ` (2 preceding siblings ...)
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 3/4] Use cpu_physical_memory_set_dirty_range() to update phys_ram_dirty Yoshiaki Tamura
@ 2010-04-20  3:40 ` Yoshiaki Tamura
  2010-04-21 12:15 ` [Qemu-devel] Re: [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Avi Kivity
  4 siblings, 0 replies; 9+ messages in thread
From: Yoshiaki Tamura @ 2010-04-20  3:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: aliguori, ohmura.kei, mtosatti, Yoshiaki Tamura, avi

Modifies ram_save_block() and ram_save_remaining() to use
cpu_physical_memory_get_dirty_range() to check multiple dirty and non-dirty
pages at once.

Signed-off-by: Yoshiaki Tamura <tamura.yoshiaki@lab.ntt.co.jp>
Signed-off-by: OHMURA Kei <ohmura.kei@lab.ntt.co.jp>
---
 arch_init.c |   54 +++++++++++++++++++++++++++++++++---------------------
 1 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/arch_init.c b/arch_init.c
index cfc03ea..245a082 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -108,31 +108,37 @@ static int ram_save_block(QEMUFile *f)
     static ram_addr_t current_addr = 0;
     ram_addr_t saved_addr = current_addr;
     ram_addr_t addr = 0;
-    int found = 0;
+    ram_addr_t dirty_rams[HOST_LONG_BITS];
+    int i, found = 0;
 
     while (addr < last_ram_offset) {
-        if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
+        if ((found = cpu_physical_memory_get_dirty_range(
+                 current_addr, last_ram_offset, dirty_rams, HOST_LONG_BITS,
+                 MIGRATION_DIRTY_FLAG))) {
             uint8_t *p;
 
-            cpu_physical_memory_reset_dirty(current_addr,
-                                            current_addr + TARGET_PAGE_SIZE,
-                                            MIGRATION_DIRTY_FLAG);
+            for (i = 0; i < found; i++) {
+                ram_addr_t page_addr = dirty_rams[i];
+                cpu_physical_memory_reset_dirty(page_addr,
+                                                page_addr + TARGET_PAGE_SIZE,
+                                                MIGRATION_DIRTY_FLAG);
 
-            p = qemu_get_ram_ptr(current_addr);
+                p = qemu_get_ram_ptr(page_addr);
 
-            if (is_dup_page(p, *p)) {
-                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
-                qemu_put_byte(f, *p);
-            } else {
-                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
-                qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
+                if (is_dup_page(p, *p)) {
+                    qemu_put_be64(f, page_addr | RAM_SAVE_FLAG_COMPRESS);
+                    qemu_put_byte(f, *p);
+                } else {
+                    qemu_put_be64(f, page_addr | RAM_SAVE_FLAG_PAGE);
+                    qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
+                }
             }
 
-            found = 1;
             break;
+        } else {
+            addr += dirty_rams[0];
+            current_addr = (saved_addr + addr) % last_ram_offset;
         }
-        addr += TARGET_PAGE_SIZE;
-        current_addr = (saved_addr + addr) % last_ram_offset;
     }
 
     return found;
@@ -142,12 +148,18 @@ static uint64_t bytes_transferred;
 
 static ram_addr_t ram_save_remaining(void)
 {
-    ram_addr_t addr;
+    ram_addr_t addr = 0;
     ram_addr_t count = 0;
+    ram_addr_t dirty_rams[HOST_LONG_BITS];
+    int found = 0;
 
-    for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
-        if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) {
-            count++;
+    while (addr < last_ram_offset) {
+        if ((found = cpu_physical_memory_get_dirty_range(addr, last_ram_offset,
+            dirty_rams, HOST_LONG_BITS, MIGRATION_DIRTY_FLAG))) {
+            count += found;
+            addr = dirty_rams[found - 1] + TARGET_PAGE_SIZE;
+        } else {
+            addr += dirty_rams[0];
         }
     }
 
-- 
1.7.0.31.g1df487

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [Qemu-devel] Re: [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker.
  2010-04-20  3:40 [Qemu-devel] [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Yoshiaki Tamura
                   ` (3 preceding siblings ...)
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 4/4] Use cpu_physical_memory_get_dirty_range() to check multiple dirty pages Yoshiaki Tamura
@ 2010-04-21 12:15 ` Avi Kivity
  2010-04-26 10:43   ` Yoshiaki Tamura
  4 siblings, 1 reply; 9+ messages in thread
From: Avi Kivity @ 2010-04-21 12:15 UTC (permalink / raw)
  To: Yoshiaki Tamura; +Cc: aliguori, mtosatti, qemu-devel, ohmura.kei

On 04/20/2010 06:40 AM, Yoshiaki Tamura wrote:
> The dirty and non-dirty pages are checked one by one.  When most of the memory
> is not dirty, checking the dirty and non-dirty pages by multiple page size
> should be much faster than checking them one by one.  We introduced bit-based
> phys_ram_dirty for VGA, CODE, MIGRATION, MASTER, and
> cpu_physical_memory_get_dirty_range() for this purpose.
>    

Looks good.

-- 
error compiling committee.c: too many arguments to function

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Qemu-devel] Re: [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker.
  2010-04-21 12:15 ` [Qemu-devel] Re: [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Avi Kivity
@ 2010-04-26 10:43   ` Yoshiaki Tamura
  0 siblings, 0 replies; 9+ messages in thread
From: Yoshiaki Tamura @ 2010-04-26 10:43 UTC (permalink / raw)
  To: Avi Kivity; +Cc: aliguori, mtosatti, qemu-devel, ohmura.kei

Hi,

Here are some numbers on bit-based phys_ram_dirty.

Test Environment:
CPU: 4x Intel Xeon Quad Core 2.66GHz
Mem size: 96GB

Host OS: CentOS (kernel 2.6.33)
Guest OS: Debian/GNU Linux lenny (kernel 2.6.26)
Guest Mem size: 512MB

Conditions of experiments are as follows:
Cond1: Guest OS periodically makes the 256MB continuous dirty pages.
Cond2: Guest OS periodically makes the 256MB dirty pages and non-dirty pages
in turn.
Cond3: Guest OS read 1GB file, which is bigger than memory.
Cond4: Guest OS write 1GB file, which is bigger than memory.

Experimental results:
Cond1: 5 ~ 83 times speed up
Cond2: 5 ~ 52 times speed up
Cond3: 5 ~ 132 times speed up
Cond4: 5 ~ 57 times speed up

The speed up grows when the number of rows, whose contents are 0, gets larger.

Thanks,

Yoshi

Avi Kivity wrote:
> On 04/20/2010 06:40 AM, Yoshiaki Tamura wrote:
>> The dirty and non-dirty pages are checked one by one. When most of the
>> memory
>> is not dirty, checking the dirty and non-dirty pages by multiple page
>> size
>> should be much faster than checking them one by one. We introduced
>> bit-based
>> phys_ram_dirty for VGA, CODE, MIGRATION, MASTER, and
>> cpu_physical_memory_get_dirty_range() for this purpose.
>
> Looks good.
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH v4 1/4] Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of bit-based phys_ram_dirty.
  2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 1/4] Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of bit-based phys_ram_dirty Yoshiaki Tamura
@ 2010-05-03 20:03   ` Anthony Liguori
  2010-05-04  8:31     ` Yoshiaki Tamura
  0 siblings, 1 reply; 9+ messages in thread
From: Anthony Liguori @ 2010-05-03 20:03 UTC (permalink / raw)
  To: Yoshiaki Tamura; +Cc: ohmura.kei, Marcelo Tosatti, qemu-devel, avi

Hi Yoshi,

Could you rebase this series and resubmit?  It conflicts with the latest 
HEAD.

Regards,

Anthony Liguori

On 04/19/2010 10:40 PM, Yoshiaki Tamura wrote:
> Replaces byte-based phys_ram_dirty bitmap with four (MASTER, VGA, CODE,
> MIGRATION) bit-based phys_ram_dirty bitmap.  On allocation, it sets all bits in
> the bitmap.  It uses ffs() to convert DIRTY_FLAG to DIRTY_IDX.
>
> Modifies wrapper functions for byte-based phys_ram_dirty bitmap to bit-based
> phys_ram_dirty bitmap.  MASTER works as a buffer, and upon get_diry() or
> get_dirty_flags(), it calls cpu_physical_memory_sync_master() to update VGA and
> MIGRATION.
>
> Signed-off-by: Yoshiaki Tamura<tamura.yoshiaki@lab.ntt.co.jp>
> ---
>   cpu-all.h     |  127 ++++++++++++++++++++++++++++++++++++++++++++++++---------
>   exec.c        |   15 +++++--
>   qemu-common.h |    3 +
>   3 files changed, 121 insertions(+), 24 deletions(-)
>
> diff --git a/cpu-all.h b/cpu-all.h
> index f8bfa66..b6a2d91 100644
> --- a/cpu-all.h
> +++ b/cpu-all.h
> @@ -37,6 +37,9 @@
>
>   #include "softfloat.h"
>
> +/* to use ffs in flag_to_idx() */
> +#include<strings.h>
> +
>   #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
>   #define BSWAP_NEEDED
>   #endif
> @@ -853,7 +856,6 @@ target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr);
>   /* memory API */
>
>   extern int phys_ram_fd;
> -extern uint8_t *phys_ram_dirty;
>   extern ram_addr_t ram_size;
>   extern ram_addr_t last_ram_offset;
>
> @@ -878,50 +880,137 @@ extern int mem_prealloc;
>   /* Set if TLB entry is an IO callback.  */
>   #define TLB_MMIO        (1<<  5)
>
> -#define VGA_DIRTY_FLAG       0x01
> -#define CODE_DIRTY_FLAG      0x02
> -#define MIGRATION_DIRTY_FLAG 0x08
> +/* Use DIRTY_IDX as indexes of bit-based phys_ram_dirty. */
> +#define MASTER_DIRTY_IDX    0
> +#define VGA_DIRTY_IDX       1
> +#define CODE_DIRTY_IDX      2
> +#define MIGRATION_DIRTY_IDX 3
> +#define NUM_DIRTY_IDX       4
> +
> +#define MASTER_DIRTY_FLAG    (1<<  MASTER_DIRTY_IDX)
> +#define VGA_DIRTY_FLAG       (1<<  VGA_DIRTY_IDX)
> +#define CODE_DIRTY_FLAG      (1<<  CODE_DIRTY_IDX)
> +#define MIGRATION_DIRTY_FLAG (1<<  MIGRATION_DIRTY_IDX)
> +
> +extern unsigned long *phys_ram_dirty[NUM_DIRTY_IDX];
> +
> +static inline int dirty_flag_to_idx(int flag)
> +{
> +    return ffs(flag) - 1;
> +}
> +
> +static inline int dirty_idx_to_flag(int idx)
> +{
> +    return 1<<  idx;
> +}
>
>   /* read dirty bit (return 0 or 1) */
>   static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
>   {
> -    return phys_ram_dirty[addr>>  TARGET_PAGE_BITS] == 0xff;
> +    unsigned long mask;
> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
> +    int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
> +
> +    mask = 1UL<<  offset;
> +    return (phys_ram_dirty[MASTER_DIRTY_IDX][index]&  mask) == mask;
> +}
> +
> +static inline void cpu_physical_memory_sync_master(ram_addr_t index)
> +{
> +    if (phys_ram_dirty[MASTER_DIRTY_IDX][index]) {
> +        phys_ram_dirty[VGA_DIRTY_IDX][index]
> +            |=  phys_ram_dirty[MASTER_DIRTY_IDX][index];
> +        phys_ram_dirty[MIGRATION_DIRTY_IDX][index]
> +            |=  phys_ram_dirty[MASTER_DIRTY_IDX][index];
> +        phys_ram_dirty[MASTER_DIRTY_IDX][index] = 0UL;
> +    }
>   }
>
>   static inline int cpu_physical_memory_get_dirty_flags(ram_addr_t addr)
>   {
> -    return phys_ram_dirty[addr>>  TARGET_PAGE_BITS];
> +     unsigned long mask;
> +     ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
> +     int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
> +     int ret = 0, i;
> +
> +     mask = 1UL<<  offset;
> +     cpu_physical_memory_sync_master(index);
> +
> +     for (i = VGA_DIRTY_IDX; i<= MIGRATION_DIRTY_IDX; i++) {
> +         if (phys_ram_dirty[i][index]&  mask) {
> +             ret |= dirty_idx_to_flag(i);
> +         }
> +     }
> +
> +     return ret;
> +}
> +
> +static inline int cpu_physical_memory_get_dirty_idx(ram_addr_t addr,
> +                                                    int dirty_idx)
> +{
> +    unsigned long mask;
> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
> +    int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
> +
> +    mask = 1UL<<  offset;
> +    cpu_physical_memory_sync_master(index);
> +    return (phys_ram_dirty[dirty_idx][index]&  mask) == mask;
>   }
>
>   static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
>                                                   int dirty_flags)
>   {
> -    return phys_ram_dirty[addr>>  TARGET_PAGE_BITS]&  dirty_flags;
> +    return cpu_physical_memory_get_dirty_idx(addr,
> +                                             dirty_flag_to_idx(dirty_flags));
>   }
>
>   static inline void cpu_physical_memory_set_dirty(ram_addr_t addr)
>   {
> -    phys_ram_dirty[addr>>  TARGET_PAGE_BITS] = 0xff;
> +    unsigned long mask;
> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
> +    int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
> +
> +    mask = 1UL<<  offset;
> +    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
> +}
> +
> +static inline void cpu_physical_memory_set_dirty_range(ram_addr_t addr,
> +                                                       unsigned long mask)
> +{
> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
> +
> +    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
>   }
>
> -static inline int cpu_physical_memory_set_dirty_flags(ram_addr_t addr,
> -                                                      int dirty_flags)
> +static inline void cpu_physical_memory_set_dirty_flags(ram_addr_t addr,
> +                                                       int dirty_flags)
>   {
> -    return phys_ram_dirty[addr>>  TARGET_PAGE_BITS] |= dirty_flags;
> +    unsigned long mask;
> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
> +    int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
> +
> +    mask = 1UL<<  offset;
> +    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
> +
> +    if (dirty_flags&  CODE_DIRTY_FLAG) {
> +        phys_ram_dirty[CODE_DIRTY_IDX][index] |= mask;
> +    }
>   }
>
>   static inline void cpu_physical_memory_mask_dirty_range(ram_addr_t start,
> -                                                        int length,
> +                                                        unsigned long length,
>                                                           int dirty_flags)
>   {
> -    int i, mask, len;
> -    uint8_t *p;
> +    ram_addr_t addr = start, index;
> +    unsigned long mask;
> +    int offset, i;
>
> -    len = length>>  TARGET_PAGE_BITS;
> -    mask = ~dirty_flags;
> -    p = phys_ram_dirty + (start>>  TARGET_PAGE_BITS);
> -    for (i = 0; i<  len; i++)
> -        p[i]&= mask;
> +    for (i = 0;  i<  length; i += TARGET_PAGE_SIZE) {
> +        index = ((addr + i)>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
> +        offset = ((addr + i)>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
> +        mask = ~(1UL<<  offset);
> +        phys_ram_dirty[dirty_flag_to_idx(dirty_flags)][index]&= mask;
> +     }
>   }
>
>   void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
> diff --git a/exec.c b/exec.c
> index c74b0a4..82b7c32 100644
> --- a/exec.c
> +++ b/exec.c
> @@ -110,7 +110,7 @@ uint8_t *code_gen_ptr;
>
>   #if !defined(CONFIG_USER_ONLY)
>   int phys_ram_fd;
> -uint8_t *phys_ram_dirty;
> +unsigned long *phys_ram_dirty[NUM_DIRTY_IDX];
>   static int in_migration;
>
>   typedef struct RAMBlock {
> @@ -2793,6 +2793,7 @@ static void *file_ram_alloc(ram_addr_t memory, const char *path)
>   ram_addr_t qemu_ram_alloc(ram_addr_t size)
>   {
>       RAMBlock *new_block;
> +    int i;
>
>       size = TARGET_PAGE_ALIGN(size);
>       new_block = qemu_malloc(sizeof(*new_block));
> @@ -2825,10 +2826,14 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size)
>       new_block->next = ram_blocks;
>       ram_blocks = new_block;
>
> -    phys_ram_dirty = qemu_realloc(phys_ram_dirty,
> -        (last_ram_offset + size)>>  TARGET_PAGE_BITS);
> -    memset(phys_ram_dirty + (last_ram_offset>>  TARGET_PAGE_BITS),
> -           0xff, size>>  TARGET_PAGE_BITS);
> +    for (i = MASTER_DIRTY_IDX; i<  NUM_DIRTY_IDX; i++) {
> +        phys_ram_dirty[i]
> +            = qemu_realloc(phys_ram_dirty[i],
> +                           BITMAP_SIZE(last_ram_offset + size));
> +        memset((uint8_t *)phys_ram_dirty[i] + BITMAP_SIZE(last_ram_offset),
> +               0xff, BITMAP_SIZE(last_ram_offset + size)
> +               - BITMAP_SIZE(last_ram_offset));
> +    }
>
>       last_ram_offset += size;
>
> diff --git a/qemu-common.h b/qemu-common.h
> index 4ba0cda..efe5b1f 100644
> --- a/qemu-common.h
> +++ b/qemu-common.h
> @@ -285,6 +285,9 @@ static inline uint8_t from_bcd(uint8_t val)
>       return ((val>>  4) * 10) + (val&  0x0f);
>   }
>
> +#define ALIGN(x, y)  (((x)+(y)-1)&  ~((y)-1))
> +#define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
> +
>   #include "module.h"
>
>   #endif /* dyngen-exec.h hack */
>    

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH v4 1/4] Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of bit-based phys_ram_dirty.
  2010-05-03 20:03   ` Anthony Liguori
@ 2010-05-04  8:31     ` Yoshiaki Tamura
  0 siblings, 0 replies; 9+ messages in thread
From: Yoshiaki Tamura @ 2010-05-04  8:31 UTC (permalink / raw)
  To: Anthony Liguori; +Cc: ohmura.kei, Marcelo Tosatti, qemu-devel, avi

Sure.  Submitted as v5.

Thanks,

Yoshi

2010/5/4 Anthony Liguori <anthony@codemonkey.ws>:
> Hi Yoshi,
>
> Could you rebase this series and resubmit?  It conflicts with the latest
> HEAD.
>
> Regards,
>
> Anthony Liguori
>
> On 04/19/2010 10:40 PM, Yoshiaki Tamura wrote:
>>
>> Replaces byte-based phys_ram_dirty bitmap with four (MASTER, VGA, CODE,
>> MIGRATION) bit-based phys_ram_dirty bitmap.  On allocation, it sets all
>> bits in
>> the bitmap.  It uses ffs() to convert DIRTY_FLAG to DIRTY_IDX.
>>
>> Modifies wrapper functions for byte-based phys_ram_dirty bitmap to
>> bit-based
>> phys_ram_dirty bitmap.  MASTER works as a buffer, and upon get_diry() or
>> get_dirty_flags(), it calls cpu_physical_memory_sync_master() to update
>> VGA and
>> MIGRATION.
>>
>> Signed-off-by: Yoshiaki Tamura<tamura.yoshiaki@lab.ntt.co.jp>
>> ---
>>  cpu-all.h     |  127
>> ++++++++++++++++++++++++++++++++++++++++++++++++---------
>>  exec.c        |   15 +++++--
>>  qemu-common.h |    3 +
>>  3 files changed, 121 insertions(+), 24 deletions(-)
>>
>> diff --git a/cpu-all.h b/cpu-all.h
>> index f8bfa66..b6a2d91 100644
>> --- a/cpu-all.h
>> +++ b/cpu-all.h
>> @@ -37,6 +37,9 @@
>>
>>  #include "softfloat.h"
>>
>> +/* to use ffs in flag_to_idx() */
>> +#include<strings.h>
>> +
>>  #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
>>  #define BSWAP_NEEDED
>>  #endif
>> @@ -853,7 +856,6 @@ target_phys_addr_t cpu_get_phys_page_debug(CPUState
>> *env, target_ulong addr);
>>  /* memory API */
>>
>>  extern int phys_ram_fd;
>> -extern uint8_t *phys_ram_dirty;
>>  extern ram_addr_t ram_size;
>>  extern ram_addr_t last_ram_offset;
>>
>> @@ -878,50 +880,137 @@ extern int mem_prealloc;
>>  /* Set if TLB entry is an IO callback.  */
>>  #define TLB_MMIO        (1<<  5)
>>
>> -#define VGA_DIRTY_FLAG       0x01
>> -#define CODE_DIRTY_FLAG      0x02
>> -#define MIGRATION_DIRTY_FLAG 0x08
>> +/* Use DIRTY_IDX as indexes of bit-based phys_ram_dirty. */
>> +#define MASTER_DIRTY_IDX    0
>> +#define VGA_DIRTY_IDX       1
>> +#define CODE_DIRTY_IDX      2
>> +#define MIGRATION_DIRTY_IDX 3
>> +#define NUM_DIRTY_IDX       4
>> +
>> +#define MASTER_DIRTY_FLAG    (1<<  MASTER_DIRTY_IDX)
>> +#define VGA_DIRTY_FLAG       (1<<  VGA_DIRTY_IDX)
>> +#define CODE_DIRTY_FLAG      (1<<  CODE_DIRTY_IDX)
>> +#define MIGRATION_DIRTY_FLAG (1<<  MIGRATION_DIRTY_IDX)
>> +
>> +extern unsigned long *phys_ram_dirty[NUM_DIRTY_IDX];
>> +
>> +static inline int dirty_flag_to_idx(int flag)
>> +{
>> +    return ffs(flag) - 1;
>> +}
>> +
>> +static inline int dirty_idx_to_flag(int idx)
>> +{
>> +    return 1<<  idx;
>> +}
>>
>>  /* read dirty bit (return 0 or 1) */
>>  static inline int cpu_physical_memory_is_dirty(ram_addr_t addr)
>>  {
>> -    return phys_ram_dirty[addr>>  TARGET_PAGE_BITS] == 0xff;
>> +    unsigned long mask;
>> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
>> +    int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
>> +
>> +    mask = 1UL<<  offset;
>> +    return (phys_ram_dirty[MASTER_DIRTY_IDX][index]&  mask) == mask;
>> +}
>> +
>> +static inline void cpu_physical_memory_sync_master(ram_addr_t index)
>> +{
>> +    if (phys_ram_dirty[MASTER_DIRTY_IDX][index]) {
>> +        phys_ram_dirty[VGA_DIRTY_IDX][index]
>> +            |=  phys_ram_dirty[MASTER_DIRTY_IDX][index];
>> +        phys_ram_dirty[MIGRATION_DIRTY_IDX][index]
>> +            |=  phys_ram_dirty[MASTER_DIRTY_IDX][index];
>> +        phys_ram_dirty[MASTER_DIRTY_IDX][index] = 0UL;
>> +    }
>>  }
>>
>>  static inline int cpu_physical_memory_get_dirty_flags(ram_addr_t addr)
>>  {
>> -    return phys_ram_dirty[addr>>  TARGET_PAGE_BITS];
>> +     unsigned long mask;
>> +     ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
>> +     int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
>> +     int ret = 0, i;
>> +
>> +     mask = 1UL<<  offset;
>> +     cpu_physical_memory_sync_master(index);
>> +
>> +     for (i = VGA_DIRTY_IDX; i<= MIGRATION_DIRTY_IDX; i++) {
>> +         if (phys_ram_dirty[i][index]&  mask) {
>> +             ret |= dirty_idx_to_flag(i);
>> +         }
>> +     }
>> +
>> +     return ret;
>> +}
>> +
>> +static inline int cpu_physical_memory_get_dirty_idx(ram_addr_t addr,
>> +                                                    int dirty_idx)
>> +{
>> +    unsigned long mask;
>> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
>> +    int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
>> +
>> +    mask = 1UL<<  offset;
>> +    cpu_physical_memory_sync_master(index);
>> +    return (phys_ram_dirty[dirty_idx][index]&  mask) == mask;
>>  }
>>
>>  static inline int cpu_physical_memory_get_dirty(ram_addr_t addr,
>>                                                  int dirty_flags)
>>  {
>> -    return phys_ram_dirty[addr>>  TARGET_PAGE_BITS]&  dirty_flags;
>> +    return cpu_physical_memory_get_dirty_idx(addr,
>> +
>> dirty_flag_to_idx(dirty_flags));
>>  }
>>
>>  static inline void cpu_physical_memory_set_dirty(ram_addr_t addr)
>>  {
>> -    phys_ram_dirty[addr>>  TARGET_PAGE_BITS] = 0xff;
>> +    unsigned long mask;
>> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
>> +    int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
>> +
>> +    mask = 1UL<<  offset;
>> +    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
>> +}
>> +
>> +static inline void cpu_physical_memory_set_dirty_range(ram_addr_t addr,
>> +                                                       unsigned long
>> mask)
>> +{
>> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
>> +
>> +    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
>>  }
>>
>> -static inline int cpu_physical_memory_set_dirty_flags(ram_addr_t addr,
>> -                                                      int dirty_flags)
>> +static inline void cpu_physical_memory_set_dirty_flags(ram_addr_t addr,
>> +                                                       int dirty_flags)
>>  {
>> -    return phys_ram_dirty[addr>>  TARGET_PAGE_BITS] |= dirty_flags;
>> +    unsigned long mask;
>> +    ram_addr_t index = (addr>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
>> +    int offset = (addr>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
>> +
>> +    mask = 1UL<<  offset;
>> +    phys_ram_dirty[MASTER_DIRTY_IDX][index] |= mask;
>> +
>> +    if (dirty_flags&  CODE_DIRTY_FLAG) {
>> +        phys_ram_dirty[CODE_DIRTY_IDX][index] |= mask;
>> +    }
>>  }
>>
>>  static inline void cpu_physical_memory_mask_dirty_range(ram_addr_t start,
>> -                                                        int length,
>> +                                                        unsigned long
>> length,
>>                                                          int dirty_flags)
>>  {
>> -    int i, mask, len;
>> -    uint8_t *p;
>> +    ram_addr_t addr = start, index;
>> +    unsigned long mask;
>> +    int offset, i;
>>
>> -    len = length>>  TARGET_PAGE_BITS;
>> -    mask = ~dirty_flags;
>> -    p = phys_ram_dirty + (start>>  TARGET_PAGE_BITS);
>> -    for (i = 0; i<  len; i++)
>> -        p[i]&= mask;
>> +    for (i = 0;  i<  length; i += TARGET_PAGE_SIZE) {
>> +        index = ((addr + i)>>  TARGET_PAGE_BITS) / HOST_LONG_BITS;
>> +        offset = ((addr + i)>>  TARGET_PAGE_BITS)&  (HOST_LONG_BITS - 1);
>> +        mask = ~(1UL<<  offset);
>> +        phys_ram_dirty[dirty_flag_to_idx(dirty_flags)][index]&= mask;
>> +     }
>>  }
>>
>>  void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
>> diff --git a/exec.c b/exec.c
>> index c74b0a4..82b7c32 100644
>> --- a/exec.c
>> +++ b/exec.c
>> @@ -110,7 +110,7 @@ uint8_t *code_gen_ptr;
>>
>>  #if !defined(CONFIG_USER_ONLY)
>>  int phys_ram_fd;
>> -uint8_t *phys_ram_dirty;
>> +unsigned long *phys_ram_dirty[NUM_DIRTY_IDX];
>>  static int in_migration;
>>
>>  typedef struct RAMBlock {
>> @@ -2793,6 +2793,7 @@ static void *file_ram_alloc(ram_addr_t memory, const
>> char *path)
>>  ram_addr_t qemu_ram_alloc(ram_addr_t size)
>>  {
>>      RAMBlock *new_block;
>> +    int i;
>>
>>      size = TARGET_PAGE_ALIGN(size);
>>      new_block = qemu_malloc(sizeof(*new_block));
>> @@ -2825,10 +2826,14 @@ ram_addr_t qemu_ram_alloc(ram_addr_t size)
>>      new_block->next = ram_blocks;
>>      ram_blocks = new_block;
>>
>> -    phys_ram_dirty = qemu_realloc(phys_ram_dirty,
>> -        (last_ram_offset + size)>>  TARGET_PAGE_BITS);
>> -    memset(phys_ram_dirty + (last_ram_offset>>  TARGET_PAGE_BITS),
>> -           0xff, size>>  TARGET_PAGE_BITS);
>> +    for (i = MASTER_DIRTY_IDX; i<  NUM_DIRTY_IDX; i++) {
>> +        phys_ram_dirty[i]
>> +            = qemu_realloc(phys_ram_dirty[i],
>> +                           BITMAP_SIZE(last_ram_offset + size));
>> +        memset((uint8_t *)phys_ram_dirty[i] +
>> BITMAP_SIZE(last_ram_offset),
>> +               0xff, BITMAP_SIZE(last_ram_offset + size)
>> +               - BITMAP_SIZE(last_ram_offset));
>> +    }
>>
>>      last_ram_offset += size;
>>
>> diff --git a/qemu-common.h b/qemu-common.h
>> index 4ba0cda..efe5b1f 100644
>> --- a/qemu-common.h
>> +++ b/qemu-common.h
>> @@ -285,6 +285,9 @@ static inline uint8_t from_bcd(uint8_t val)
>>      return ((val>>  4) * 10) + (val&  0x0f);
>>  }
>>
>> +#define ALIGN(x, y)  (((x)+(y)-1)&  ~((y)-1))
>> +#define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) /
>> 8)
>> +
>>  #include "module.h"
>>
>>  #endif /* dyngen-exec.h hack */
>>
>
>
>
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-05-04  8:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-20  3:40 [Qemu-devel] [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Yoshiaki Tamura
2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 1/4] Modify DIRTY_FLAG value and introduce DIRTY_IDX to use as indexes of bit-based phys_ram_dirty Yoshiaki Tamura
2010-05-03 20:03   ` Anthony Liguori
2010-05-04  8:31     ` Yoshiaki Tamura
2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 2/4] Introduce cpu_physical_memory_get_dirty_range() Yoshiaki Tamura
2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 3/4] Use cpu_physical_memory_set_dirty_range() to update phys_ram_dirty Yoshiaki Tamura
2010-04-20  3:40 ` [Qemu-devel] [PATCH v4 4/4] Use cpu_physical_memory_get_dirty_range() to check multiple dirty pages Yoshiaki Tamura
2010-04-21 12:15 ` [Qemu-devel] Re: [PATCH v4 0/4] Introduce bit-based phys_ram_dirty, and bit-based dirty page checker Avi Kivity
2010-04-26 10:43   ` Yoshiaki Tamura

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.