From nobody Sat May 18 22:54:14 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624772461201726.9892538639416; Sat, 26 Jun 2021 22:41:01 -0700 (PDT) Received: from localhost ([::1]:53608 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lxNXP-0000XJ-3p for importer@patchew.org; Sun, 27 Jun 2021 01:40:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53266) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lxNVA-0007df-PC for qemu-devel@nongnu.org; Sun, 27 Jun 2021 01:38:41 -0400 Received: from prt-mail.chinatelecom.cn ([42.123.76.227]:44580 helo=chinatelecom.cn) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lxNV6-0001I8-6y for qemu-devel@nongnu.org; Sun, 27 Jun 2021 01:38:40 -0400 Received: from clientip-171.223.99.176?logid-0210caab79734a8b87be68778a878dff (unknown [172.18.0.218]) by chinatelecom.cn (HERMES) with SMTP id 44C94280098; Sun, 27 Jun 2021 13:38:29 +0800 (CST) Received: from ([172.18.0.218]) by app0025 with ESMTP id bfc85ca1d7fa4d4da61ae2fb94d56ac3 for qemu-devel@nongnu.org; Sun Jun 27 13:38:29 2021 HMM_SOURCE_IP: 172.18.0.218:60506.95674552 HMM_ATTACHE_NUM: 0000 HMM_SOURCE_TYPE: SMTP X-189-SAVE-TO-SEND: +huangy81@chinatelecom.cn X-Transaction-ID: bfc85ca1d7fa4d4da61ae2fb94d56ac3 X-filter-score: X-Real-From: huangy81@chinatelecom.cn X-Receive-IP: 172.18.0.218 X-MEDUSA-Status: 0 From: huangy81@chinatelecom.cn To: qemu-devel@nongnu.org Subject: [PATCH 1/4] memory: introduce DIRTY_MEMORY_DIRTY_RATE dirty bits Date: Sun, 27 Jun 2021 13:38:14 +0800 Message-Id: X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=42.123.76.227; envelope-from=huangy81@chinatelecom.cn; helo=chinatelecom.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , Juan Quintela , Hyman , "Dr. David Alan Gilbert" , Peter Xu , Chuan Zheng , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" From: Hyman Huang(=E9=BB=84=E5=8B=87) intrduce DIRTY_MEMORY_DIRTY_RATE dirty bits to tracking vm dirty page for calculating dirty rate since dirtyrate and migration may be trigger concurrently, reusing the exsiting DIRTY_MEMORY_MIGRATION dirty bits seems not a good choice. introduce a fresh new dirty bits for dirtyrate to ensure both dirtyrate and migration work fine. Signed-off-by: Hyman Huang(=E9=BB=84=E5=8B=87) --- include/exec/ram_addr.h | 15 ++++++++++++++- include/exec/ramlist.h | 9 +++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 45c9132..6070a52 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -308,6 +308,10 @@ static inline void cpu_physical_memory_set_dirty_range= (ram_addr_t start, while (page < end) { unsigned long next =3D MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE= ); =20 + if (unlikely(mask & (1 << DIRTY_MEMORY_DIRTY_RATE))) { + bitmap_set_atomic(blocks[DIRTY_MEMORY_DIRTY_RATE]->blocks[= idx], + offset, next - page); + } if (likely(mask & (1 << DIRTY_MEMORY_MIGRATION))) { bitmap_set_atomic(blocks[DIRTY_MEMORY_MIGRATION]->blocks[i= dx], offset, next - page); @@ -370,9 +374,17 @@ static inline void cpu_physical_memory_set_dirty_lebit= map(unsigned long *bitmap, qatomic_or(&blocks[DIRTY_MEMORY_VGA][idx][offset], tem= p); =20 if (global_dirty_tracking) { - qatomic_or( + if (global_dirty_tracking & GLOBAL_DIRTY_MIGRATION= ) { + qatomic_or( &blocks[DIRTY_MEMORY_MIGRATION][idx][offse= t], temp); + } + + if (global_dirty_tracking & GLOBAL_DIRTY_DIRTY_RAT= E) { + qatomic_or( + &blocks[DIRTY_MEMORY_DIRTY_RATE][idx][offs= et], + temp); + } } =20 if (tcg_enabled()) { @@ -394,6 +406,7 @@ static inline void cpu_physical_memory_set_dirty_lebitm= ap(unsigned long *bitmap, =20 if (!global_dirty_tracking) { clients &=3D ~(1 << DIRTY_MEMORY_MIGRATION); + clients &=3D ~(1 << DIRTY_MEMORY_DIRTY_RATE); } =20 /* diff --git a/include/exec/ramlist.h b/include/exec/ramlist.h index ece6497..8585f00 100644 --- a/include/exec/ramlist.h +++ b/include/exec/ramlist.h @@ -8,10 +8,11 @@ =20 typedef struct RAMBlockNotifier RAMBlockNotifier; =20 -#define DIRTY_MEMORY_VGA 0 -#define DIRTY_MEMORY_CODE 1 -#define DIRTY_MEMORY_MIGRATION 2 -#define DIRTY_MEMORY_NUM 3 /* num of dirty bits */ +#define DIRTY_MEMORY_VGA 0 +#define DIRTY_MEMORY_CODE 1 +#define DIRTY_MEMORY_MIGRATION 2 +#define DIRTY_MEMORY_DIRTY_RATE 3 +#define DIRTY_MEMORY_NUM 4 /* num of dirty bits */ =20 /* The dirty memory bitmap is split into fixed-size blocks to allow growth * under RCU. The bitmap for a block can be accessed as follows: --=20 1.8.3.1 From nobody Sat May 18 22:54:14 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624772637859469.61940685477316; Sat, 26 Jun 2021 22:43:57 -0700 (PDT) Received: from localhost ([::1]:57880 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lxNaG-0003aY-Rd for importer@patchew.org; Sun, 27 Jun 2021 01:43:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53278) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lxNVF-0007dm-54 for qemu-devel@nongnu.org; Sun, 27 Jun 2021 01:38:45 -0400 Received: from prt-mail.chinatelecom.cn ([42.123.76.227]:44587 helo=chinatelecom.cn) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lxNV6-0001LL-BZ for qemu-devel@nongnu.org; Sun, 27 Jun 2021 01:38:44 -0400 Received: from clientip-171.223.99.176?logid-0210caab79734a8b87be68778a878dff (unknown [172.18.0.218]) by chinatelecom.cn (HERMES) with SMTP id 31EF6280099; Sun, 27 Jun 2021 13:38:34 +0800 (CST) Received: from ([172.18.0.218]) by app0025 with ESMTP id 1a488dc223314a2ca9e423d97856d07a for qemu-devel@nongnu.org; Sun Jun 27 13:38:34 2021 HMM_SOURCE_IP: 172.18.0.218:60506.95674552 HMM_ATTACHE_NUM: 0000 HMM_SOURCE_TYPE: SMTP X-189-SAVE-TO-SEND: +huangy81@chinatelecom.cn X-Transaction-ID: 1a488dc223314a2ca9e423d97856d07a X-filter-score: X-Real-From: huangy81@chinatelecom.cn X-Receive-IP: 172.18.0.218 X-MEDUSA-Status: 0 From: huangy81@chinatelecom.cn To: qemu-devel@nongnu.org Subject: [PATCH 2/4] KVM: introduce kvm_get_manual_dirty_log_protect Date: Sun, 27 Jun 2021 13:38:15 +0800 Message-Id: <93edbfecdaf4a67a8316c33a1183e2e5f97a0b22.1624771216.git.huangy81@chinatelecom.cn> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=42.123.76.227; envelope-from=huangy81@chinatelecom.cn; helo=chinatelecom.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , Juan Quintela , Hyman , "Dr. David Alan Gilbert" , Peter Xu , Chuan Zheng , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" From: Hyman Huang(=E9=BB=84=E5=8B=87) introduce kvm_get_manual_dirty_log_protect for measureing dirtyrate via dirty bitmap. calculation of dirtyrate need to sync dirty log and depends on the features of dirty log. Signed-off-by: Hyman Huang(=E9=BB=84=E5=8B=87) --- accel/kvm/kvm-all.c | 6 ++++++ include/sysemu/kvm.h | 1 + 2 files changed, 7 insertions(+) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index e0e88a2..f7d9ae0 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -245,6 +245,12 @@ int kvm_get_max_memslots(void) return s->nr_slots; } =20 +uint64_t kvm_get_manual_dirty_log_protect(void) +{ + KVMState *s =3D KVM_STATE(current_accel()); + return s->manual_dirty_log_protect; +} + /* Called with KVMMemoryListener.slots_lock held */ static KVMSlot *kvm_get_free_slot(KVMMemoryListener *kml) { diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index 7b22aeb..b668d49 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -533,6 +533,7 @@ int kvm_set_one_reg(CPUState *cs, uint64_t id, void *so= urce); int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target); struct ppc_radix_page_info *kvm_get_radix_page_info(void); int kvm_get_max_memslots(void); +uint64_t kvm_get_manual_dirty_log_protect(void); =20 /* Notify resamplefd for EOI of specific interrupts. */ void kvm_resample_fd_notify(int gsi); --=20 1.8.3.1 From nobody Sat May 18 22:54:14 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624772468928419.2976041437372; Sat, 26 Jun 2021 22:41:08 -0700 (PDT) Received: from localhost ([::1]:53732 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lxNXX-0000cR-Pq for importer@patchew.org; Sun, 27 Jun 2021 01:41:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53292) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lxNVH-0007dw-3e for qemu-devel@nongnu.org; Sun, 27 Jun 2021 01:38:47 -0400 Received: from prt-mail.chinatelecom.cn ([42.123.76.227]:44593 helo=chinatelecom.cn) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lxNVB-0001QG-Pm for qemu-devel@nongnu.org; Sun, 27 Jun 2021 01:38:46 -0400 Received: from clientip-171.223.99.176?logid-0210caab79734a8b87be68778a878dff (unknown [172.18.0.218]) by chinatelecom.cn (HERMES) with SMTP id 18BDB28009A; Sun, 27 Jun 2021 13:38:39 +0800 (CST) Received: from ([172.18.0.218]) by app0025 with ESMTP id 29f13c49ed394465816ab1302c0509a2 for qemu-devel@nongnu.org; Sun Jun 27 13:38:39 2021 HMM_SOURCE_IP: 172.18.0.218:60506.95674552 HMM_ATTACHE_NUM: 0000 HMM_SOURCE_TYPE: SMTP X-189-SAVE-TO-SEND: +huangy81@chinatelecom.cn X-Transaction-ID: 29f13c49ed394465816ab1302c0509a2 X-filter-score: X-Real-From: huangy81@chinatelecom.cn X-Receive-IP: 172.18.0.218 X-MEDUSA-Status: 0 From: huangy81@chinatelecom.cn To: qemu-devel@nongnu.org Subject: [PATCH 3/4] memory: introduce DIRTY_MEMORY_DIRTY_RATE dirty bits functions Date: Sun, 27 Jun 2021 13:38:16 +0800 Message-Id: X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=42.123.76.227; envelope-from=huangy81@chinatelecom.cn; helo=chinatelecom.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , Juan Quintela , Hyman , "Dr. David Alan Gilbert" , Peter Xu , Chuan Zheng , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" From: Hyman Huang(=E9=BB=84=E5=8B=87) introduce util functions to setup the DIRTY_MEMORY_DIRTY_RATE dirty bits for the convenience of tracking dirty bitmap when calculating dirtyrate. Signed-off-by: Hyman Huang(=E9=BB=84=E5=8B=87) --- include/exec/ram_addr.h | 121 ++++++++++++++++++++++++++++++++++++++++++++= ++++ softmmu/physmem.c | 61 ++++++++++++++++++++++++ 2 files changed, 182 insertions(+) diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 6070a52..57dc96b 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -435,6 +435,12 @@ bool cpu_physical_memory_test_and_clear_dirty(ram_addr= _t start, ram_addr_t length, unsigned client); =20 +void cpu_physical_memory_dirtyrate_clear_bit(ram_addr_t start, + ram_addr_t length); + +void cpu_physical_memory_dirtyrate_reprotect_bit(ram_addr_t start, + ram_addr_t length); + DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty (MemoryRegion *mr, hwaddr offset, hwaddr length, unsigned client); =20 @@ -523,5 +529,120 @@ uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlo= ck *rb, =20 return num_dirty; } + +/* Called with RCU critical section */ +static inline +void cpu_physical_memory_dirtyrate_clear_dirty_bits(RAMBlock *rb) +{ + ram_addr_t addr; + ram_addr_t length =3D rb->used_length; + unsigned long word =3D BIT_WORD(rb->offset >> TARGET_PAGE_BITS); + + /* start address and length is aligned at the start of a word? */ + if (((word * BITS_PER_LONG) << TARGET_PAGE_BITS) =3D=3D rb->offset && + !(length & ((BITS_PER_LONG << TARGET_PAGE_BITS) - 1))) { + int k; + int nr =3D BITS_TO_LONGS(length >> TARGET_PAGE_BITS); + unsigned long * const *src; + unsigned long idx =3D (word * BITS_PER_LONG) / DIRTY_MEMORY_BLOCK_= SIZE; + unsigned long offset =3D BIT_WORD((word * BITS_PER_LONG) % + DIRTY_MEMORY_BLOCK_SIZE); + + src =3D qatomic_rcu_read( + &ram_list.dirty_memory[DIRTY_MEMORY_DIRTY_RATE])->blocks; + + for (k =3D 0; k < nr; k++) { + if (src[idx][offset]) { + qatomic_set(&src[idx][offset], 0); + } + if (++offset >=3D BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE)) { + offset =3D 0; + idx++; + } + } + } else { + ram_addr_t offset =3D rb->offset; + + for (addr =3D 0; addr < length; addr +=3D TARGET_PAGE_SIZE) { + cpu_physical_memory_dirtyrate_clear_bit(addr + offset, + TARGET_PAGE_SIZE); + } + } + + return; +} + +/* Called with RCU critical section */ +static inline +uint64_t cpu_physical_memory_dirtyrate_stat_dirty_bits(RAMBlock *rb) +{ + uint64_t dirty_pages =3D 0; + ram_addr_t addr; + ram_addr_t length =3D rb->used_length; + unsigned long word =3D BIT_WORD(rb->offset >> TARGET_PAGE_BITS); + unsigned long bits; + + /* start address and length is aligned at the start of a word? */ + if (((word * BITS_PER_LONG) << TARGET_PAGE_BITS) =3D=3D rb->offset && + !(length & ((BITS_PER_LONG << TARGET_PAGE_BITS) - 1))) { + int k; + int nr =3D BITS_TO_LONGS(length >> TARGET_PAGE_BITS); + unsigned long * const *src; + unsigned long idx =3D (word * BITS_PER_LONG) / DIRTY_MEMORY_BLOCK_= SIZE; + unsigned long offset =3D BIT_WORD((word * BITS_PER_LONG) % + DIRTY_MEMORY_BLOCK_SIZE); + + src =3D qatomic_rcu_read( + &ram_list.dirty_memory[DIRTY_MEMORY_DIRTY_RATE])->blocks; + + for (k =3D 0; k < nr; k++) { + if (src[idx][offset]) { + bits =3D qatomic_read(&src[idx][offset]); + dirty_pages +=3D ctpopl(bits); + } + + if (++offset >=3D BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE)) { + offset =3D 0; + idx++; + } + } + } else { + ram_addr_t offset =3D rb->offset; + + for (addr =3D 0; addr < length; addr +=3D TARGET_PAGE_SIZE) { + if (cpu_physical_memory_get_dirty(offset + addr, + TARGET_PAGE_SIZE, + DIRTY_MEMORY_DIRTY_RATE)) { + dirty_pages++; + } + } + } + + return dirty_pages; +} + +static inline +void cpu_physical_memory_dirtyrate_reset_protect(RAMBlock *rb) +{ + ram_addr_t addr; + ram_addr_t length =3D rb->used_length; + unsigned long word =3D BIT_WORD(rb->offset >> TARGET_PAGE_BITS); + + /* start address and length is aligned at the start of a word? */ + if (((word * BITS_PER_LONG) << TARGET_PAGE_BITS) =3D=3D rb->offset && + !(length & ((BITS_PER_LONG << TARGET_PAGE_BITS) - 1))) { + memory_region_clear_dirty_bitmap(rb->mr, 0, length); + } else { + ram_addr_t offset =3D rb->offset; + + for (addr =3D 0; addr < length; addr +=3D TARGET_PAGE_SIZE) { + cpu_physical_memory_dirtyrate_reprotect_bit(offset + addr, + TARGET_PAGE_SIZE); + } + } + + return; +} + #endif #endif diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 9b171c9..d68649a 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1068,6 +1068,67 @@ bool cpu_physical_memory_test_and_clear_dirty(ram_ad= dr_t start, return dirty; } =20 +void cpu_physical_memory_dirtyrate_clear_bit(ram_addr_t start, + ram_addr_t length) +{ + DirtyMemoryBlocks *blocks; + unsigned long end, page; + RAMBlock *ramblock; + + if (length =3D=3D 0) { + return; + } + + end =3D TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS; + page =3D start >> TARGET_PAGE_BITS; + + WITH_RCU_READ_LOCK_GUARD() { + blocks =3D + qatomic_rcu_read(&ram_list.dirty_memory[DIRTY_MEMORY_DIRTY_RAT= E]); + ramblock =3D qemu_get_ram_block(start); + /* Range sanity check on the ramblock */ + assert(start >=3D ramblock->offset && + start + length <=3D ramblock->offset + ramblock->used_lengt= h); + while (page < end) { + unsigned long idx =3D page / DIRTY_MEMORY_BLOCK_SIZE; + unsigned long offset =3D page % DIRTY_MEMORY_BLOCK_SIZE; + unsigned long num =3D MIN(end - page, + DIRTY_MEMORY_BLOCK_SIZE - offset); + + clear_bit(num, blocks->blocks[idx]); + page +=3D num; + } + } + + return; +} + +void cpu_physical_memory_dirtyrate_reprotect_bit(ram_addr_t start, + ram_addr_t length) +{ + unsigned long end, start_page; + RAMBlock *ramblock; + uint64_t mr_offset, mr_size; + + if (length =3D=3D 0) { + return; + } + + end =3D TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS; + start_page =3D start >> TARGET_PAGE_BITS; + + ramblock =3D qemu_get_ram_block(start); + /* Range sanity check on the ramblock */ + assert(start >=3D ramblock->offset && + start + length <=3D ramblock->offset + ramblock->used_length); + + mr_offset =3D (ram_addr_t)(start_page << TARGET_PAGE_BITS) - ramblock-= >offset; + mr_size =3D (end - start_page) << TARGET_PAGE_BITS; + memory_region_clear_dirty_bitmap(ramblock->mr, mr_offset, mr_size); + + return; +} + DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty (MemoryRegion *mr, hwaddr offset, hwaddr length, unsigned client) { --=20 1.8.3.1 From nobody Sat May 18 22:54:14 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624772643115726.8076042208161; Sat, 26 Jun 2021 22:44:03 -0700 (PDT) Received: from localhost ([::1]:58134 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lxNaM-0003kg-32 for importer@patchew.org; Sun, 27 Jun 2021 01:44:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53308) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lxNVI-0007fo-Ub for qemu-devel@nongnu.org; Sun, 27 Jun 2021 01:38:48 -0400 Received: from prt-mail.chinatelecom.cn ([42.123.76.227]:44597 helo=chinatelecom.cn) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lxNVF-0001SH-UN for qemu-devel@nongnu.org; Sun, 27 Jun 2021 01:38:48 -0400 Received: from clientip-171.223.99.176?logid-0210caab79734a8b87be68778a878dff (unknown [172.18.0.218]) by chinatelecom.cn (HERMES) with SMTP id D5308280098; Sun, 27 Jun 2021 13:38:44 +0800 (CST) Received: from ([172.18.0.218]) by app0025 with ESMTP id 9faa8b32bda048a993e73bdd46f4c4c7 for qemu-devel@nongnu.org; Sun Jun 27 13:38:44 2021 HMM_SOURCE_IP: 172.18.0.218:60506.95674552 HMM_ATTACHE_NUM: 0000 HMM_SOURCE_TYPE: SMTP X-189-SAVE-TO-SEND: +huangy81@chinatelecom.cn X-Transaction-ID: 9faa8b32bda048a993e73bdd46f4c4c7 X-filter-score: X-Real-From: huangy81@chinatelecom.cn X-Receive-IP: 172.18.0.218 X-MEDUSA-Status: 0 From: huangy81@chinatelecom.cn To: qemu-devel@nongnu.org Subject: [PATCH 4/4] migration/dirtyrate: implement dirty-bitmap dirtyrate calculation Date: Sun, 27 Jun 2021 13:38:17 +0800 Message-Id: X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=42.123.76.227; envelope-from=huangy81@chinatelecom.cn; helo=chinatelecom.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , Juan Quintela , Hyman , "Dr. David Alan Gilbert" , Peter Xu , Chuan Zheng , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" From: Hyman Huang(=E9=BB=84=E5=8B=87) introduce dirty-bitmap mode as the third method of calc-dirty-rate. implement dirty-bitmap dirtyrate calculation, which can be used to measuring dirtyrate in the absence of dirty-ring. introduce "dirty_bitmap:-b" option in hmp calc_dirty_rate to indicate dirty bitmap method should be used for calculation. Signed-off-by: Hyman Huang(=E9=BB=84=E5=8B=87) --- hmp-commands.hx | 9 +-- migration/dirtyrate.c | 147 +++++++++++++++++++++++++++++++++++++++++++++= +--- migration/trace-events | 2 + qapi/migration.json | 6 +- 4 files changed, 150 insertions(+), 14 deletions(-) diff --git a/hmp-commands.hx b/hmp-commands.hx index f7fc9d7..605973c 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -1738,9 +1738,10 @@ ERST =20 { .name =3D "calc_dirty_rate", - .args_type =3D "dirty_ring:-r,second:l,sample_pages_per_GB:l?", - .params =3D "[-r] second [sample_pages_per_GB]", - .help =3D "start a round of guest dirty rate measurement (us= ing -d to" - "\n\t\t\t specify dirty ring as the method of calcul= ation)", + .args_type =3D "dirty_ring:-r,dirty_bitmap:-b,second:l,sample_pag= es_per_GB:l?", + .params =3D "[-r] [-b] second [sample_pages_per_GB]", + .help =3D "start a round of guest dirty rate measurement (us= ing -r to" + "\n\t\t\t specify dirty ring as the method of calcul= ation and" + "\n\t\t\t -b to specify dirty bitmap as method of ca= lculation)", .cmd =3D hmp_calc_dirty_rate, }, diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c index 3c8c5e2..bf465ce 100644 --- a/migration/dirtyrate.c +++ b/migration/dirtyrate.c @@ -15,6 +15,7 @@ #include "qapi/error.h" #include "cpu.h" #include "exec/ramblock.h" +#include "exec/ram_addr.h" #include "qemu/rcu_queue.h" #include "qemu/main-loop.h" #include "qapi/qapi-commands-migration.h" @@ -111,6 +112,10 @@ static struct DirtyRateInfo *query_dirty_rate_info(voi= d) } info->vcpu_dirty_rate =3D head; } + + if (dirtyrate_mode =3D=3D DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) { + info->sample_pages =3D 0; + } } =20 trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState)); @@ -421,6 +426,114 @@ static int64_t do_calculate_dirtyrate_vcpu(DirtyPageR= ecord dirty_pages) return memory_size_MB / time_s; } =20 +static inline void dirtyrate_dirty_bits_clear(void) +{ + RAMBlock *block =3D NULL; + + WITH_RCU_READ_LOCK_GUARD() { + RAMBLOCK_FOREACH_MIGRATABLE(block) { + cpu_physical_memory_dirtyrate_clear_dirty_bits(block); + } + } +} + +static inline uint64_t dirtyrate_dirty_bits_reap(void) +{ + RAMBlock *block =3D NULL; + uint64_t dirty_pages =3D 0; + uint64_t total_pages =3D 0; + + WITH_RCU_READ_LOCK_GUARD() { + RAMBLOCK_FOREACH_MIGRATABLE(block) { + dirty_pages =3D cpu_physical_memory_dirtyrate_stat_dirty_bits(= block); + total_pages +=3D dirty_pages; + + trace_dirtyrate_bitmap_reap(block->idstr, dirty_pages); + } + } + + return total_pages; +} + +static inline void dirtyrate_manual_reset_protect(void) +{ + RAMBlock *block =3D NULL; + + WITH_RCU_READ_LOCK_GUARD() { + RAMBLOCK_FOREACH_MIGRATABLE(block) { + cpu_physical_memory_dirtyrate_reset_protect(block); + } + } +} + +static void do_calculate_dirtyrate_bitmap(void) +{ + uint64_t memory_size_MB; + int64_t time_s; + uint64_t increased_dirty_pages =3D 0; + uint64_t dirtyrate =3D 0; + + increased_dirty_pages =3D dirtyrate_dirty_bits_reap(); + + memory_size_MB =3D (increased_dirty_pages * TARGET_PAGE_SIZE) >> 20; + time_s =3D DirtyStat.calc_time; + + dirtyrate =3D memory_size_MB / time_s; + DirtyStat.dirty_rate =3D dirtyrate; + + trace_dirtyrate_do_calculate_bitmap(increased_dirty_pages, + time_s, dirtyrate); +} + +static void calculate_dirtyrate_dirty_bitmap(struct DirtyRateConfig config) +{ + int64_t msec =3D 0; + int64_t start_time; + uint64_t protect_flags =3D 0; + uint64_t initially_set =3D 0; + uint64_t protect_only =3D 0; + + protect_flags =3D kvm_get_manual_dirty_log_protect(); + protect_only =3D KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE; + initially_set =3D + (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | KVM_DIRTY_LOG_INITIALLY_SET= ); + + dirtyrate_global_dirty_log_start(); + + /* absense of migration */ + if (!(global_dirty_tracking & GLOBAL_DIRTY_MIGRATION)) { + if ((protect_flags & initially_set) =3D=3D initially_set) { + /* skip the 1'round which return all 1 bits */ + memory_global_dirty_log_sync(); + /* + * reset page protect manually and + * start dirty tracking from now on + **/ + dirtyrate_manual_reset_protect(); + } + } + + dirtyrate_dirty_bits_clear(); + + start_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); + DirtyStat.start_time =3D start_time / 1000; + + msec =3D config.sample_period_seconds * 1000; + msec =3D set_sample_page_period(msec, start_time); + DirtyStat.calc_time =3D msec / 1000; + + /* fetch dirty bitmap from kvm */ + memory_global_dirty_log_sync(); + + do_calculate_dirtyrate_bitmap(); + + if (protect_flags & protect_only) { + dirtyrate_manual_reset_protect(); + } + + dirtyrate_global_dirty_log_stop(); +} + static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config) { CPUState *cpu; @@ -506,7 +619,9 @@ out: =20 static void calculate_dirtyrate(struct DirtyRateConfig config) { - if (config.mode =3D=3D DIRTY_RATE_MEASURE_MODE_DIRTY_RING) { + if (config.mode =3D=3D DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) { + calculate_dirtyrate_dirty_bitmap(config); + } else if (config.mode =3D=3D DIRTY_RATE_MEASURE_MODE_DIRTY_RING) { calculate_dirtyrate_dirty_ring(config); } else { calculate_dirtyrate_sample_vm(config); @@ -589,11 +704,14 @@ void qmp_calc_dirty_rate(int64_t calc_time, =20 /* * dirty ring mode only works when kvm dirty ring is enabled. + * on the contrary, dirty bitmap mode is not. */ - if ((mode =3D=3D DIRTY_RATE_MEASURE_MODE_DIRTY_RING) && - !kvm_dirty_ring_enabled()) { - error_setg(errp, "dirty ring is disabled, use sample-pages method " - "or remeasure later."); + if (((mode =3D=3D DIRTY_RATE_MEASURE_MODE_DIRTY_RING) && + !kvm_dirty_ring_enabled()) || + ((mode =3D=3D DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) && + kvm_dirty_ring_enabled())) { + error_setg(errp, "mode %s is not enabled, use other method instead= .", + DirtyRateMeasureMode_str(mode)); return; } =20 @@ -670,9 +788,8 @@ void hmp_calc_dirty_rate(Monitor *mon, const QDict *qdi= ct) int64_t sample_pages =3D qdict_get_try_int(qdict, "sample_pages_per_GB= ", -1); bool has_sample_pages =3D (sample_pages !=3D -1); bool dirty_ring =3D qdict_get_try_bool(qdict, "dirty_ring", false); - DirtyRateMeasureMode mode =3D - (dirty_ring ? DIRTY_RATE_MEASURE_MODE_DIRTY_RING : - DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING); + bool dirty_bitmap =3D qdict_get_try_bool(qdict, "dirty_bitmap", false); + DirtyRateMeasureMode mode =3D DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING; Error *err =3D NULL; =20 if (!sec) { @@ -680,6 +797,20 @@ void hmp_calc_dirty_rate(Monitor *mon, const QDict *qd= ict) return; } =20 + if (dirty_ring && dirty_bitmap) { + monitor_printf(mon, "Either dirty ring or dirty bitmap " + "can be specified!\n"); + return; + } + + if (dirty_bitmap) { + mode =3D DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP; + } + + if (dirty_ring) { + mode =3D DIRTY_RATE_MEASURE_MODE_DIRTY_RING; + } + qmp_calc_dirty_rate(sec, has_sample_pages, sample_pages, true, mode, &err); if (err) { diff --git a/migration/trace-events b/migration/trace-events index ce0b5e6..e19e3e1 100644 --- a/migration/trace-events +++ b/migration/trace-events @@ -333,6 +333,8 @@ skip_sample_ramblock(const char *idstr, uint64_t ramblo= ck_size) "ramblock name: find_page_matched(const char *idstr) "ramblock %s addr or size changed" dirtyrate_calculate(int64_t dirtyrate) "dirty rate: %" PRIi64 " MB/s" dirtyrate_do_calculate_vcpu(int idx, uint64_t rate) "vcpu[%d]: %"PRIu64 " = MB/s" +dirtyrate_do_calculate_bitmap(uint64_t pages, int64_t time, uint64_t rate)= "%"PRIu64 " pages has increased in %"PRIi64 " s, rate %"PRIu64 "MB/s" +dirtyrate_bitmap_reap(const char *idstr, uint64_t pages) "ramblock name: %= s, has %"PRIu64 " dirty pages" =20 # block.c migration_block_init_shared(const char *blk_device_name) "Start migration = for %s with shared base image" diff --git a/qapi/migration.json b/qapi/migration.json index de35528..0b00976 100644 --- a/qapi/migration.json +++ b/qapi/migration.json @@ -1747,13 +1747,15 @@ # # @page-sampling: calculate dirtyrate by sampling pages. # -# @dirty-ring: calculate dirtyrate by via dirty ring. +# @dirty-ring: calculate dirtyrate by dirty ring. +# +# @dirty-bitmap: calculate dirtyrate by dirty bitmap. # # Since: 6.1 # ## { 'enum': 'DirtyRateMeasureMode', - 'data': ['page-sampling', 'dirty-ring'] } + 'data': ['page-sampling', 'dirty-ring', 'dirty-bitmap'] } =20 ## # @DirtyRateInfo: --=20 1.8.3.1