Skip to contentSkip to conversation

*** A Method for evaluating dirty page rate ***

PATCH Superseded Chuan Zheng 4 participants 40 messages Aug 24, 2020 – Aug 27, 2020 Arm/Amlogic Meson SoC support

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover *** A Method for evaluating dirty page rate ***

v4 -> v5:
fix git apply failed due to meson-build
add review-by for patches in v3

v3 -> v4:
use crc32 to get hash result instead of md5 add DirtyRateStatus to denote calculation status add some trace_calls to make it easier to debug fix some comments accroding to review

v2 -> v3:
fix size_t compile warning
fix codestyle checked by checkpatch.pl

v1 -> v2:
use g_rand_new() to generate rand_buf
move RAMBLOCK_FOREACH_MIGRATABLE into migration/ram.h add skip_sample_ramblock to filter sampled ramblock fix multi-numa vm coredump when query dirtyrate rename qapi interface and rename some structures and functions succeed to compile by appling each patch
add test for migrating vm

Sometimes it is neccessary to evaluate dirty page rate before migration. Users could decide whether to proceed migration based on the evaluation in case of vm performance loss due to heavy workload.
Unlikey simulating dirtylog sync which could do harm on runnning vm, we provide a sample-hash method to compare hash results for samping page. In this way, it would have hardly no impact on vm performance.

Evaluate the dirtypage rate both on running and migration vm. The VM specifications for migration are as follows:
- VM use 4-K page;
- the number of VCPU is 32;
- the total memory is 32Gigabit;
- use 'mempress' tool to pressurize VM(mempress 4096 1024);
- migration bandwidth is 1GB/s

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| | running | migrating |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| no mempress | 4MB/s | 8MB/s (migrated success) |
-------------------------------------------------------------------------------------------
| mempress 4096 1024 | 1060MB/s | 456MB/s ~ 1142MB/s (cpu throttle triggered) |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| mempress 4096 4096 | 4114MB/s | 688MB/s ~ 4132MB/s (cpu throttle triggered) |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Test dirtyrate by qmp command like this:

1.  virsh qemu-monitor-command [vmname] '{"execute":"calc-dirty-rate", "arguments": {"calc-time": [sleep-time]}}'; 
2.  sleep specific time which is a bit larger than sleep-time
3.  virsh qemu-monitor-command [vmname] '{"execute":"query-dirty-rate"}'

Further test dirtyrate by libvirt api like this:
virsh getdirtyrate [vmname] [sleep-time]

Chuan Zheng (12):
migration/dirtyrate: setup up query-dirtyrate framwork migration/dirtyrate: add DirtyRateStatus to denote calculation status migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info migration/dirtyrate: Add dirtyrate statistics series functions migration/dirtyrate: move RAMBLOCK_FOREACH_MIGRATABLE into ram.h migration/dirtyrate: Record hash results for each sampled page migration/dirtyrate: Compare page hash results for recorded sampled
page
migration/dirtyrate: skip sampling ramblock with size below
MIN_RAMBLOCK_SIZE
migration/dirtyrate: Implement get_sample_page_period() and
block_sample_page_period()
migration/dirtyrate: Implement calculate_dirtyrate() function migration/dirtyrate: Implement
qmp_cal_dirty_rate()/qmp_get_dirty_rate() function
migration/dirtyrate: Add trace_calls to make it easier to debug

 migration/dirtyrate.c  | 432 +++++++++++++++++++++++++++++++++++++++++++++++++
 migration/dirtyrate.h  |  87 ++++++++++
 migration/meson.build  |   1 +
 migration/ram.c        |  11 +-
 migration/ram.h        |  10 ++
 migration/trace-events |   8 +
 qapi/migration.json    |  61 +++++++
 7 files changed, 600 insertions(+), 10 deletions(-)
 create mode 100644 migration/dirtyrate.c
 create mode 100644 migration/dirtyrate.h
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v5 1/12 migration/dirtyrate: setup up query-dirtyrate framwork

Add get_dirtyrate_thread() functions to setup query-dirtyrate framework.

---
 migration/dirtyrate.c | 39 +++++++++++++++++++++++++++++++++++++++
 migration/dirtyrate.h | 32 ++++++++++++++++++++++++++++++++
 migration/meson.build |  1 +
 3 files changed, 72 insertions(+)
 create mode 100644 migration/dirtyrate.c
 create mode 100644 migration/dirtyrate.h
migration/dirtyrate.c new file +39 -0
new file mode 100644
index 0000000..366f4e9
@@ -0,0 +1,39 @@
+/*
+ * Dirtyrate implement code
+ *
+ * Copyright (c) 2017-2020 HUAWEI TECHNOLOGIES CO.,LTD.
+ *
+ * Authors:
+ *  Chuan Zheng <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "crypto/hash.h"
+#include "crypto/random.h"
+#include "qemu/config-file.h"
+#include "exec/memory.h"
+#include "exec/ramblock.h"
+#include "exec/target_page.h"
+#include "qemu/rcu_queue.h"
+#include "qapi/qapi-commands-migration.h"
+#include "migration.h"
+#include "dirtyrate.h"
+
+static void calculate_dirtyrate(struct DirtyRateConfig config)
+{
+    /* todo */
+    return;
+}
+
+void *get_dirtyrate_thread(void *arg)
+{
+    struct DirtyRateConfig config = *(struct DirtyRateConfig *)arg;
+
+    calculate_dirtyrate(config);
+
+    return NULL;
+}
migration/dirtyrate.h new file +32 -0
new file mode 100644
index 0000000..33669b7
@@ -0,0 +1,32 @@
+/*
+ *  Dirtyrate common functions
+ *
+ *  Copyright (c) 2020 HUAWEI TECHNOLOGIES CO., LTD.
+ *
+ *  Authors:
+ *  Chuan Zheng <[email protected]>
+ *
+ *  This work is licensed under the terms of the GNU GPL, version 2 or later.
+ *  See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_DIRTYRATE_H
+#define QEMU_MIGRATION_DIRTYRATE_H
+
+/*
+ * Sample 512 pages per GB as default.
+ * TODO: Make it configurable.
+ */
+#define DIRTYRATE_DEFAULT_SAMPLE_PAGES            512
+
+/* Take 1s as default for calculation duration */
+#define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
+
+struct DirtyRateConfig {
+    uint64_t sample_pages_per_gigabytes; /* sample pages per GB */
+    int64_t sample_period_seconds; /* time duration between two sampling */
+};
+
+void *get_dirtyrate_thread(void *arg);
+#endif
+
migration/meson.build +1 -0
index ac8ff14..30cc6c3 100644
@@ -21,6 +21,7 @@ softmmu_ss.add(files(
   'channel.c',
   'colo-failover.c',
   'colo.c',
+  'dirtyrate.c',
   'exec.c',
   'fd.c',
   'global_state.c',
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v5 2/12 migration/dirtyrate: add DirtyRateStatus to denote calculation status

In reply to Chuan Zheng

add DirtyRateStatus to denote calculating status.

---
 migration/dirtyrate.c | 22 ++++++++++++++++++++++
 qapi/migration.json   | 17 +++++++++++++++++
 2 files changed, 39 insertions(+)
migration/dirtyrate.c +22 -0
index 366f4e9..91987c5 100644
@@ -23,6 +23,19 @@
 #include "migration.h"
 #include "dirtyrate.h"
 
+static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
+
+static int dirtyrate_set_state(int *state, int old_state, int new_state)
+{
+    assert(new_state < DIRTY_RATE_STATUS__MAX);
+    if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
+        return 0;
+    } else {
+        return -1;
+    }
+}
+
+
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
     /* todo */
@@ -32,8 +45,17 @@ static void calculate_dirtyrate(struct DirtyRateConfig config)
 void *get_dirtyrate_thread(void *arg)
 {
     struct DirtyRateConfig config = *(struct DirtyRateConfig *)arg;
+    int ret;
+
+    ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_UNSTARTED,
+                              DIRTY_RATE_STATUS_MEASURING);
+    if (ret == -1) {
+        return NULL;
+    }
 
     calculate_dirtyrate(config);
 
+    ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_MEASURING,
+                              DIRTY_RATE_STATUS_MEASURED);
     return NULL;
 }
qapi/migration.json +17 -0
index 5f6b061..d640165 100644
@@ -1720,3 +1720,20 @@
 ##
 { 'event': 'UNPLUG_PRIMARY',
   'data': { 'device-id': 'str' } }
+
+##
+# @DirtyRateStatus:
+#
+# An enumeration of dirtyrate status.
+#
+# @unstarted: query-dirtyrate thread is not initial.
+#
+# @measuring: query-dirtyrate thread is created and start to measure.
+#
+# @measured:  query-dirtyrate thread is end, we can get result.
+#
+# Since: 5.2
+#
+##
+{ 'enum': 'DirtyRateStatus',
+  'data': [ 'unstarted', 'measuring', 'measured'] }
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH v5 02/12] migration/dirtyrate: add DirtyRateStatus to denote calculation status

Minor wordsmithing...

Quoted 55 lines · Chuan Zheng

add DirtyRateStatus to denote calculating status.

---
 migration/dirtyrate.c | 22 ++++++++++++++++++++++
 qapi/migration.json   | 17 +++++++++++++++++
 2 files changed, 39 insertions(+)
migration/dirtyrate.c +22 -0
index 366f4e9..91987c5 100644
@@ -23,6 +23,19 @@
 #include "migration.h"
 #include "dirtyrate.h"
 
+static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
+
+static int dirtyrate_set_state(int *state, int old_state, int new_state)
+{
+    assert(new_state < DIRTY_RATE_STATUS__MAX);
+    if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
+        return 0;
+    } else {
+        return -1;
+    }
+}
+
+
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
     /* todo */
@@ -32,8 +45,17 @@ static void calculate_dirtyrate(struct DirtyRateConfig config)
 void *get_dirtyrate_thread(void *arg)
 {
     struct DirtyRateConfig config = *(struct DirtyRateConfig *)arg;
+    int ret;
+
+    ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_UNSTARTED,
+                              DIRTY_RATE_STATUS_MEASURING);
+    if (ret == -1) {
+        return NULL;
+    }
 
     calculate_dirtyrate(config);
 
+    ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_MEASURING,
+                              DIRTY_RATE_STATUS_MEASURED);
     return NULL;
 }
qapi/migration.json +0 -0
index 5f6b061..d640165 100644

qapi/migration.json line 1729

Quoted 10 lines
qapi/migration.json +7 -0
@@ -1720,3 +1720,20 @@
 ##
 { 'event': 'UNPLUG_PRIMARY',
   'data': { 'device-id': 'str' } }
+
+##
+# @DirtyRateStatus:
+#
+# An enumeration of dirtyrate status.
+#
+# @unstarted: query-dirtyrate thread is not initial.

@unstarted: the dirtyrate thread has not been started.

qapi/migration.json line 1731

Quoted 2 lines
qapi/migration.json +2 -0
+#
+# @measuring: query-dirtyrate thread is created and start to measure.

@measuring: the dirtyrate thread is measuring.

qapi/migration.json line 1733

Quoted 2 lines
qapi/migration.json +2 -0
+#
+# @measured:  query-dirtyrate thread is end, we can get result.

@measured: the dirtyrate thread has measured and results are available.

Quoted 8 lines
qapi/migration.json +6 -0
+#
+# Since: 5.2
+#
+##
+{ 'enum': 'DirtyRateStatus',
+  'data': [ 'unstarted', 'measuring', 'measured'] }
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
Tonight I'm gonna bury that horse in the ground.
#4 lore raw

PATCH v5 3/12 migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info

In reply to Chuan Zheng

Add RamlockDirtyInfo to store sampled page info of each ramblock.

---
 migration/dirtyrate.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
migration/dirtyrate.h +18 -0
index 33669b7..70000da 100644
@@ -19,6 +19,11 @@
  */
 #define DIRTYRATE_DEFAULT_SAMPLE_PAGES            512
 
+/*
+ * Record ramblock idstr
+ */
+#define RAMBLOCK_INFO_MAX_LEN                     256
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
@@ -27,6 +32,19 @@ struct DirtyRateConfig {
     int64_t sample_period_seconds; /* time duration between two sampling */
 };
 
+/*
+ * Store dirtypage info for each ramblock.
+ */
+struct RamblockDirtyInfo {
+    char idstr[RAMBLOCK_INFO_MAX_LEN]; /* idstr for each ramblock */
+    uint8_t *ramblock_addr; /* base address of ramblock we measure */
+    uint64_t ramblock_pages; /* ramblock size in 4K-page */
+    uint64_t *sample_page_vfn; /* relative offset address for sampled page */
+    uint64_t sample_pages_count; /* count of sampled pages */
+    uint64_t sample_dirty_count; /* cout of dirty pages we measure */
+    uint32_t *hash_result; /* array of hash result for sampled pages */
+};
+
 void *get_dirtyrate_thread(void *arg);
 #endif
 
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [PATCH v5 03/12] migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info

Quoted 24 lines · Chuan Zheng

Add RamlockDirtyInfo to store sampled page info of each ramblock.

---
 migration/dirtyrate.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
migration/dirtyrate.h +5 -0
index 33669b7..70000da 100644
@@ -19,6 +19,11 @@
  */
 #define DIRTYRATE_DEFAULT_SAMPLE_PAGES            512
 
+/*
+ * Record ramblock idstr
+ */
+#define RAMBLOCK_INFO_MAX_LEN                     256
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 

migration/dirtyrate.h struct DirtyRateConfig { · line 41

Quoted 10 lines
migration/dirtyrate.h +7 -0
@@ -27,6 +32,19 @@ struct DirtyRateConfig {
     int64_t sample_period_seconds; /* time duration between two sampling */
 };
 
+/*
+ * Store dirtypage info for each ramblock.
+ */
+struct RamblockDirtyInfo {
+    char idstr[RAMBLOCK_INFO_MAX_LEN]; /* idstr for each ramblock */
+    uint8_t *ramblock_addr; /* base address of ramblock we measure */
+    uint64_t ramblock_pages; /* ramblock size in 4K-page */

It's probably a stupid question, but why not store a pointer to the RAMBlock rather than copying some of the details?

migration/dirtyrate.h struct DirtyRateConfig { · line 44

Quoted 3 lines
migration/dirtyrate.h +3 -0
+    uint64_t *sample_page_vfn; /* relative offset address for sampled page */
+    uint64_t sample_pages_count; /* count of sampled pages */
+    uint64_t sample_dirty_count; /* cout of dirty pages we measure */

"cout" -> "count"

Quoted 8 lines
migration/dirtyrate.h +3 -0
+    uint32_t *hash_result; /* array of hash result for sampled pages */
+};
+
 void *get_dirtyrate_thread(void *arg);
 #endif
 
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
Please forgive me if I act a little strange, for I know not what I do.
#6 lore raw

Re: [PATCH v5 03/12] migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info

Quoted 40 lines · David Edmondson
Quoted 24 lines · Chuan Zheng

Add RamlockDirtyInfo to store sampled page info of each ramblock.

---
 migration/dirtyrate.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
migration/dirtyrate.h +5 -0
index 33669b7..70000da 100644
@@ -19,6 +19,11 @@
  */
 #define DIRTYRATE_DEFAULT_SAMPLE_PAGES            512
 
+/*
+ * Record ramblock idstr
+ */
+#define RAMBLOCK_INFO_MAX_LEN                     256
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 

migration/dirtyrate.h struct DirtyRateConfig { · line 41

Quoted 10 lines
migration/dirtyrate.h +7 -0
@@ -27,6 +32,19 @@ struct DirtyRateConfig {
     int64_t sample_period_seconds; /* time duration between two sampling */
 };
 
+/*
+ * Store dirtypage info for each ramblock.
+ */
+struct RamblockDirtyInfo {
+    char idstr[RAMBLOCK_INFO_MAX_LEN]; /* idstr for each ramblock */
+    uint8_t *ramblock_addr; /* base address of ramblock we measure */
+    uint64_t ramblock_pages; /* ramblock size in 4K-page */

It's probably a stupid question, but why not store a pointer to the RAMBlock rather than copying some of the details?

Quoted 6 lines

migration/dirtyrate.h struct DirtyRateConfig { · line 44

Quoted 3 lines
migration/dirtyrate.h +3 -0
+    uint64_t *sample_page_vfn; /* relative offset address for sampled page */
+    uint64_t sample_pages_count; /* count of sampled pages */
+    uint64_t sample_dirty_count; /* cout of dirty pages we measure */

"cout" -> "count"

Hi, David.
Thank you for review.
Actually I have resent [PATCH V5], but it's my fault to forget to add resent tag which may lead to confusing. In new patch series, this spell mistake is fixed.
Please refer to the newer patch series:)

Quoted 11 lines
Quoted 8 lines
migration/dirtyrate.h +3 -0
+    uint32_t *hash_result; /* array of hash result for sampled pages */
+};
+
 void *get_dirtyrate_thread(void *arg);
 #endif
 
Signature · 1 line
1.8.3.1

dme.

#7 lore raw

Re: [PATCH v5 03/12] migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info

Quoted 32 lines · Zheng Chuan
Quoted 29 lines · David Edmondson
Quoted 27 lines · Chuan Zheng

Add RamlockDirtyInfo to store sampled page info of each ramblock.

---
 migration/dirtyrate.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
migration/dirtyrate.h +5 -0
index 33669b7..70000da 100644
@@ -19,6 +19,11 @@
  */
 #define DIRTYRATE_DEFAULT_SAMPLE_PAGES            512
 
+/*
+ * Record ramblock idstr
+ */
+#define RAMBLOCK_INFO_MAX_LEN                     256
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
@@ -27,6 +32,19 @@ struct DirtyRateConfig {
     int64_t sample_period_seconds; /* time duration between two sampling */
 };
 
Quoted 11 lines
Quoted 11 lines

migration/dirtyrate.h struct DirtyRateConfig { · line 41

Quoted 7 lines
migration/dirtyrate.h +7 -0
+/*
+ * Store dirtypage info for each ramblock.
+ */
+struct RamblockDirtyInfo {
+    char idstr[RAMBLOCK_INFO_MAX_LEN]; /* idstr for each ramblock */
+    uint8_t *ramblock_addr; /* base address of ramblock we measure */
+    uint64_t ramblock_pages; /* ramblock size in 4K-page */

It's probably a stupid question, but why not store a pointer to the RAMBlock rather than copying some of the details?

Missing this question:)
Since I only hold the RCU around each part separately, the RAMBlock could disappear between the initial hash, and the later compare; so using the name makes it safe.
Again, Please refer to the newer patch series:)

Quoted 23 lines
Quoted 6 lines

migration/dirtyrate.h struct DirtyRateConfig { · line 44

Quoted 3 lines
migration/dirtyrate.h +3 -0
+    uint64_t *sample_page_vfn; /* relative offset address for sampled page */
+    uint64_t sample_pages_count; /* count of sampled pages */
+    uint64_t sample_dirty_count; /* cout of dirty pages we measure */

"cout" -> "count"

Hi, David.
Thank you for review.
Actually I have resent [PATCH V5], but it's my fault to forget to add resent tag which may lead to confusing. In new patch series, this spell mistake is fixed.
Please refer to the newer patch series:)

Quoted 11 lines
Quoted 8 lines
migration/dirtyrate.h +3 -0
+    uint32_t *hash_result; /* array of hash result for sampled pages */
+};
+
 void *get_dirtyrate_thread(void *arg);
 #endif
 
Signature · 1 line
1.8.3.1

dme.

#8 lore raw

Re: [PATCH v5 03/12] migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info

In reply to David Edmondson
Quoted 29 lines · * David Edmondson ([email protected]) wrote:
Quoted 27 lines · Chuan Zheng

Add RamlockDirtyInfo to store sampled page info of each ramblock.

---
 migration/dirtyrate.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
migration/dirtyrate.h +5 -0
index 33669b7..70000da 100644
@@ -19,6 +19,11 @@
  */
 #define DIRTYRATE_DEFAULT_SAMPLE_PAGES            512
 
+/*
+ * Record ramblock idstr
+ */
+#define RAMBLOCK_INFO_MAX_LEN                     256
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
@@ -27,6 +32,19 @@ struct DirtyRateConfig {
     int64_t sample_period_seconds; /* time duration between two sampling */
 };
 
Quoted 10 lines

migration/dirtyrate.h struct DirtyRateConfig { · line 41

Quoted 7 lines
migration/dirtyrate.h +7 -0
+/*
+ * Store dirtypage info for each ramblock.
+ */
+struct RamblockDirtyInfo {
+    char idstr[RAMBLOCK_INFO_MAX_LEN]; /* idstr for each ramblock */
+    uint8_t *ramblock_addr; /* base address of ramblock we measure */
+    uint64_t ramblock_pages; /* ramblock size in 4K-page */

It's probably a stupid question, but why not store a pointer to the RAMBlock rather than copying some of the details?

I think I figured that out in the last round; this code runs as:

    rcu lock {
       calculate initial CRCs
    }

    <sleep 1 second ish>
    rcu lock {
       calculate new CRCs
    }

A RAMBlock might get deleted between the two.

Dave

Quoted 19 lines

migration/dirtyrate.h struct DirtyRateConfig { · line 44

Quoted 3 lines
migration/dirtyrate.h +3 -0
+    uint64_t *sample_page_vfn; /* relative offset address for sampled page */
+    uint64_t sample_pages_count; /* count of sampled pages */
+    uint64_t sample_dirty_count; /* cout of dirty pages we measure */

"cout" -> "count"

Quoted 8 lines
migration/dirtyrate.h +3 -0
+    uint32_t *hash_result; /* array of hash result for sampled pages */
+};
+
 void *get_dirtyrate_thread(void *arg);
 #endif
 
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
Please forgive me if I act a little strange, for I know not what I do.
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#9 lore raw

Re: [PATCH v5 03/12] migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info

Quoted 52 lines · Dr. David Alan Gilbert
Quoted 29 lines · * David Edmondson ([email protected]) wrote:
Quoted 27 lines · Chuan Zheng

Add RamlockDirtyInfo to store sampled page info of each ramblock.

---
 migration/dirtyrate.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
migration/dirtyrate.h +5 -0
index 33669b7..70000da 100644
@@ -19,6 +19,11 @@
  */
 #define DIRTYRATE_DEFAULT_SAMPLE_PAGES            512
 
+/*
+ * Record ramblock idstr
+ */
+#define RAMBLOCK_INFO_MAX_LEN                     256
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
@@ -27,6 +32,19 @@ struct DirtyRateConfig {
     int64_t sample_period_seconds; /* time duration between two sampling */
 };
 
Quoted 10 lines

migration/dirtyrate.h struct DirtyRateConfig { · line 41

Quoted 7 lines
migration/dirtyrate.h +7 -0
+/*
+ * Store dirtypage info for each ramblock.
+ */
+struct RamblockDirtyInfo {
+    char idstr[RAMBLOCK_INFO_MAX_LEN]; /* idstr for each ramblock */
+    uint8_t *ramblock_addr; /* base address of ramblock we measure */
+    uint64_t ramblock_pages; /* ramblock size in 4K-page */

It's probably a stupid question, but why not store a pointer to the RAMBlock rather than copying some of the details?

I think I figured that out in the last round; this code runs as:

    rcu lock {
       calculate initial CRCs
    }

    <sleep 1 second ish>
    rcu lock {
       calculate new CRCs
    }
Quoted 1 line

A RAMBlock might get deleted between the two.

Makes sense, thanks.

dme.

Signature · 1 line
Why does it have to be like this? I can never tell.
#10 lore raw

PATCH v5 4/12 migration/dirtyrate: Add dirtyrate statistics series functions

In reply to Chuan Zheng

Add dirtyrate statistics to record/update dirtyrate info.

---
 migration/dirtyrate.c | 29 +++++++++++++++++++++++++++++
 migration/dirtyrate.h | 10 ++++++++++
 2 files changed, 39 insertions(+)
migration/dirtyrate.c +29 -0
index 91987c5..0d7163f 100644
@@ -24,6 +24,7 @@
 #include "dirtyrate.h"
 
 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
+static struct DirtyRateStat DirtyStat;
 
 static int dirtyrate_set_state(int *state, int old_state, int new_state)
 {
@@ -35,6 +36,34 @@ static int dirtyrate_set_state(int *state, int old_state, int new_state)
     }
 }
 
+static void reset_dirtyrate_stat(void)
+{
+    DirtyStat.total_dirty_samples = 0;
+    DirtyStat.total_sample_count = 0;
+    DirtyStat.total_block_mem_MB = 0;
+    DirtyStat.dirty_rate = 0;
+}
+
+static void update_dirtyrate_stat(struct RamblockDirtyInfo *info)
+{
+    DirtyStat.total_dirty_samples += info->sample_dirty_count;
+    DirtyStat.total_sample_count += info->sample_pages_count;
+    /* size of 4K pages in MB */
+    DirtyStat.total_block_mem_MB += info->ramblock_pages / 256;
+}
+
+static void update_dirtyrate(uint64_t msec)
+{
+    uint64_t dirtyrate;
+    uint64_t total_dirty_samples = DirtyStat.total_dirty_samples;
+    uint64_t total_sample_count = DirtyStat.total_sample_count;
+    uint64_t total_block_mem_MB = DirtyStat.total_block_mem_MB;
+
+    dirtyrate = total_dirty_samples * total_block_mem_MB *
+                 1000 / (total_sample_count * msec);
+
+    DirtyStat.dirty_rate = dirtyrate;
+}
 
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
migration/dirtyrate.h +10 -0
index 70000da..9db269d 100644
@@ -45,6 +45,16 @@ struct RamblockDirtyInfo {
     uint32_t *hash_result; /* array of hash result for sampled pages */
 };
 
+/*
+ * Store calculation statistics for each measure.
+ */
+struct DirtyRateStat {
+    uint64_t total_dirty_samples; /* total dirty sampled page */
+    uint64_t total_sample_count; /* total sampled pages */
+    uint64_t total_block_mem_MB; /* size of total sampled pages in MB */
+    int64_t dirty_rate; /* dirty rate in MB/s */
+};
+
 void *get_dirtyrate_thread(void *arg);
 #endif
 
Signature · 1 line
1.8.3.1
#11 lore raw

PATCH v5 5/12 migration/dirtyrate: move RAMBLOCK_FOREACH_MIGRATABLE into ram.h

In reply to Chuan Zheng

RAMBLOCK_FOREACH_MIGRATABLE is need in dirtyrate measure, move the existing definition up into migration/ram.h

---
 migration/dirtyrate.c |  1 +
 migration/ram.c       | 11 +----------
 migration/ram.h       | 10 ++++++++++
 3 files changed, 12 insertions(+), 10 deletions(-)
migration/dirtyrate.c +1 -0
index 0d7163f..f6a94d8 100644
@@ -21,6 +21,7 @@
 #include "qemu/rcu_queue.h"
 #include "qapi/qapi-commands-migration.h"
 #include "migration.h"
+#include "ram.h"
 #include "dirtyrate.h"
 
 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
migration/ram.c +1 -10
index 76d4fee..37ef0da 100644
@@ -158,21 +158,12 @@ out:
     return ret;
 }
 
-static bool ramblock_is_ignored(RAMBlock *block)
+bool ramblock_is_ignored(RAMBlock *block)
 {
     return !qemu_ram_is_migratable(block) ||
            (migrate_ignore_shared() && qemu_ram_is_shared(block));
 }
 
-/* Should be holding either ram_list.mutex, or the RCU lock. */
-#define RAMBLOCK_FOREACH_NOT_IGNORED(block)            \
-    INTERNAL_RAMBLOCK_FOREACH(block)                   \
-        if (ramblock_is_ignored(block)) {} else
-
-#define RAMBLOCK_FOREACH_MIGRATABLE(block)             \
-    INTERNAL_RAMBLOCK_FOREACH(block)                   \
-        if (!qemu_ram_is_migratable(block)) {} else
-
 #undef RAMBLOCK_FOREACH
 
 int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque)
migration/ram.h +10 -0
index 2eeaacf..011e854 100644
@@ -37,6 +37,16 @@ extern MigrationStats ram_counters;
 extern XBZRLECacheStats xbzrle_counters;
 extern CompressionStats compression_counters;
 
+bool ramblock_is_ignored(RAMBlock *block);
+/* Should be holding either ram_list.mutex, or the RCU lock. */
+#define RAMBLOCK_FOREACH_NOT_IGNORED(block)            \
+    INTERNAL_RAMBLOCK_FOREACH(block)                   \
+        if (ramblock_is_ignored(block)) {} else
+
+#define RAMBLOCK_FOREACH_MIGRATABLE(block)             \
+    INTERNAL_RAMBLOCK_FOREACH(block)                   \
+        if (!qemu_ram_is_migratable(block)) {} else
+
 int xbzrle_cache_resize(int64_t new_size, Error **errp);
 uint64_t ram_bytes_remaining(void);
 uint64_t ram_bytes_total(void);
Signature · 1 line
1.8.3.1
#12 lore raw

PATCH v5 6/12 migration/dirtyrate: Record hash results for each sampled page

In reply to Chuan Zheng

Record hash results for each sampled page, crc32 is taken to calculate hash results for each sampled 4K-page.

---
 migration/dirtyrate.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++
 migration/dirtyrate.h |  15 ++++++
 2 files changed, 151 insertions(+)
migration/dirtyrate.c +136 -0
index f6a94d8..66de426 100644
@@ -10,6 +10,7 @@
  * See the COPYING file in the top-level directory.
  */
 
+#include <zlib.h>
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "crypto/hash.h"
@@ -66,6 +67,141 @@ static void update_dirtyrate(uint64_t msec)
     DirtyStat.dirty_rate = dirtyrate;
 }
 
+/*
+ * get hash result for the sampled memory with length of 4K byte in ramblock,
+ * which starts from ramblock base address.
+ */
+static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
+                                      uint64_t vfn)
+{
+    struct iovec iov_array;
+    uint32_t crc;
+
+    iov_array.iov_base = info->ramblock_addr +
+                         vfn * DIRTYRATE_SAMPLE_PAGE_SIZE;
+    iov_array.iov_len = DIRTYRATE_SAMPLE_PAGE_SIZE;
+
+    crc = crc32(0, iov_array.iov_base, iov_array.iov_len);
+
+    return crc;
+}
+
+static int save_ramblock_hash(struct RamblockDirtyInfo *info)
+{
+    unsigned int sample_pages_count;
+    int i;
+    int ret = -1;
+    GRand *rand = g_rand_new();
+
+    sample_pages_count = info->sample_pages_count;
+
+    /* ramblock size less than one page, return success to skip this ramblock */
+    if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
+        ret = 0;
+        goto out;
+    }
+
+    info->hash_result = g_try_malloc0_n(sample_pages_count,
+                                        sizeof(uint32_t));
+    if (!info->hash_result) {
+        ret = -1;
+        goto out;
+    }
+
+    info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
+                                            sizeof(uint64_t));
+    if (!info->sample_page_vfn) {
+        g_free(info->hash_result);
+        ret = -1;
+        goto out;
+    }
+
+    for (i = 0; i < sample_pages_count; i++) {
+        info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
+                                                    info->ramblock_pages - 1);
+        info->hash_result[i] = get_ramblock_vfn_hash(info,
+                                                     info->sample_page_vfn[i]);
+    }
+    ret = 0;
+
+out:
+    g_rand_free(rand);
+    return ret;
+}
+
+static void get_ramblock_dirty_info(RAMBlock *block,
+                                    struct RamblockDirtyInfo *info,
+                                    struct DirtyRateConfig *config)
+{
+    uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
+
+    /* Right shift 30 bits to calc block size in GB */
+    info->sample_pages_count = (qemu_ram_get_used_length(block) *
+                                sample_pages_per_gigabytes) >>
+                                DIRTYRATE_PAGE_SHIFT_GB;
+
+    /* Right shift 12 bits to calc page count in 4KB */
+    info->ramblock_pages = qemu_ram_get_used_length(block) >>
+                           DIRTYRATE_PAGE_SHIFT_KB;
+    info->ramblock_addr = qemu_ram_get_host_addr(block);
+    strcpy(info->idstr, qemu_ram_get_idstr(block));
+}
+
+static struct RamblockDirtyInfo *
+alloc_ramblock_dirty_info(int *block_index,
+                          struct RamblockDirtyInfo *block_dinfo)
+{
+    struct RamblockDirtyInfo *info = NULL;
+    int index = *block_index;
+
+    if (!block_dinfo) {
+        index = 0;
+        block_dinfo = g_try_new(struct RamblockDirtyInfo, 1);
+    } else {
+        index++;
+        block_dinfo = g_try_realloc(block_dinfo, (index + 1) *
+                                    sizeof(struct RamblockDirtyInfo));
+    }
+    if (!block_dinfo) {
+        return NULL;
+    }
+
+    info = &block_dinfo[index];
+    *block_index = index;
+    memset(info, 0, sizeof(struct RamblockDirtyInfo));
+
+    return block_dinfo;
+}
+
+static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
+                                     struct DirtyRateConfig config,
+                                     int *block_index)
+{
+    struct RamblockDirtyInfo *info = NULL;
+    struct RamblockDirtyInfo *dinfo = NULL;
+    RAMBlock *block = NULL;
+    int index = 0;
+
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        dinfo = alloc_ramblock_dirty_info(&index, dinfo);
+        if (dinfo == NULL) {
+            return -1;
+        }
+        info = &dinfo[index];
+        get_ramblock_dirty_info(block, info, &config);
+        if (save_ramblock_hash(info) < 0) {
+            *block_dinfo = dinfo;
+            *block_index = index;
+            return -1;
+        }
+    }
+
+    *block_dinfo = dinfo;
+    *block_index = index;
+
+    return 0;
+}
+
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
     /* todo */
migration/dirtyrate.h +15 -0
index 9db269d..5050add 100644
@@ -24,6 +24,21 @@
  */
 #define RAMBLOCK_INFO_MAX_LEN                     256
 
+/*
+ * Sample page size 4K as default.
+ */
+#define DIRTYRATE_SAMPLE_PAGE_SIZE                4096
+
+/*
+ * Sample page size 4K shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_KB                   12
+
+/*
+ * Sample page size 1G shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_GB                   30
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
Signature · 1 line
1.8.3.1
#13 lore raw

Re: [PATCH v5 06/12] migration/dirtyrate: Record hash results for each sampled page

Quoted 24 lines · Chuan Zheng

Record hash results for each sampled page, crc32 is taken to calculate hash results for each sampled 4K-page.

---
 migration/dirtyrate.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++
 migration/dirtyrate.h |  15 ++++++
 2 files changed, 151 insertions(+)
migration/dirtyrate.c +1 -0
index f6a94d8..66de426 100644
@@ -10,6 +10,7 @@
  * See the COPYING file in the top-level directory.
  */
 
+#include <zlib.h>
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "crypto/hash.h"
@@ -66,6 +67,141 @@ static void update_dirtyrate(uint64_t msec)
     DirtyStat.dirty_rate = dirtyrate;

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 77

Quoted 10 lines
migration/dirtyrate.c +8 -0
 }
 
+/*
+ * get hash result for the sampled memory with length of 4K byte in ramblock,
+ * which starts from ramblock base address.
+ */
+static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
+                                      uint64_t vfn)
+{
+    struct iovec iov_array;

There's no need for an iovec now that crc32() is being used.

Quoted 6 lines
migration/dirtyrate.c +6 -0
+    uint32_t crc;
+
+    iov_array.iov_base = info->ramblock_addr +
+                         vfn * DIRTYRATE_SAMPLE_PAGE_SIZE;
+    iov_array.iov_len = DIRTYRATE_SAMPLE_PAGE_SIZE;
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 93

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    crc = crc32(0, iov_array.iov_base, iov_array.iov_len);
+
+    return crc;
+}
+
+static int save_ramblock_hash(struct RamblockDirtyInfo *info)
+{
+    unsigned int sample_pages_count;
+    int i;
+    int ret = -1;

There's no need to initialise "ret".

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 94

Quoted 1 line
migration/dirtyrate.c +1 -0
+    GRand *rand = g_rand_new();

Calling g_rand_new() when the result may not be used (because of the various conditions immediately below) seems as though it might waste entropy. Could this be pushed down just above the loop? It would even get rid of the gotos ;-)

Quoted 36 lines
migration/dirtyrate.c +36 -0
+    sample_pages_count = info->sample_pages_count;
+
+    /* ramblock size less than one page, return success to skip this ramblock */
+    if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
+        ret = 0;
+        goto out;
+    }
+
+    info->hash_result = g_try_malloc0_n(sample_pages_count,
+                                        sizeof(uint32_t));
+    if (!info->hash_result) {
+        ret = -1;
+        goto out;
+    }
+
+    info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
+                                            sizeof(uint64_t));
+    if (!info->sample_page_vfn) {
+        g_free(info->hash_result);
+        ret = -1;
+        goto out;
+    }
+
+    for (i = 0; i < sample_pages_count; i++) {
+        info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
+                                                    info->ramblock_pages - 1);
+        info->hash_result[i] = get_ramblock_vfn_hash(info,
+                                                     info->sample_page_vfn[i]);
+    }
+    ret = 0;
+
+out:
+    g_rand_free(rand);
+    return ret;
+}
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 141

Quoted 10 lines
migration/dirtyrate.c +10 -0
+static void get_ramblock_dirty_info(RAMBlock *block,
+                                    struct RamblockDirtyInfo *info,
+                                    struct DirtyRateConfig *config)
+{
+    uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
+
+    /* Right shift 30 bits to calc block size in GB */
+    info->sample_pages_count = (qemu_ram_get_used_length(block) *
+                                sample_pages_per_gigabytes) >>
+                                DIRTYRATE_PAGE_SHIFT_GB;

Doing the calculation this way around seems odd. Why was it not:

    info->sample_pages_count = (qemu_ram_get_used_length(block) >>
                                DIRTYRATE_PAGE_SHIFT_GB) *
                                sample_pages_per_gigabytes;

?

Quoted 12 lines
migration/dirtyrate.c +12 -0
+
+    /* Right shift 12 bits to calc page count in 4KB */
+    info->ramblock_pages = qemu_ram_get_used_length(block) >>
+                           DIRTYRATE_PAGE_SHIFT_KB;
+    info->ramblock_addr = qemu_ram_get_host_addr(block);
+    strcpy(info->idstr, qemu_ram_get_idstr(block));
+}
+
+static struct RamblockDirtyInfo *
+alloc_ramblock_dirty_info(int *block_index,
+                          struct RamblockDirtyInfo *block_dinfo)
+{

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 163

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    struct RamblockDirtyInfo *info = NULL;
+    int index = *block_index;
+
+    if (!block_dinfo) {
+        index = 0;
+        block_dinfo = g_try_new(struct RamblockDirtyInfo, 1);
+    } else {
+        index++;
+        block_dinfo = g_try_realloc(block_dinfo, (index + 1) *
+                                    sizeof(struct RamblockDirtyInfo));

g_try_renew() instead of g_try_realloc()?

Quoted 9 lines
migration/dirtyrate.c +9 -0
+    }
+    if (!block_dinfo) {
+        return NULL;
+    }
+
+    info = &block_dinfo[index];
+    *block_index = index;
+    memset(info, 0, sizeof(struct RamblockDirtyInfo));
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 182

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    return block_dinfo;
+}
+
+static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
+                                     struct DirtyRateConfig config,
+                                     int *block_index)
+{
+    struct RamblockDirtyInfo *info = NULL;
+    struct RamblockDirtyInfo *dinfo = NULL;
+    RAMBlock *block = NULL;

There's no need to initialise "info" or "block".

The declaration of "info" could be pushed into the loop.

Quoted 53 lines
migration/dirtyrate.c +22 -0
+    int index = 0;
+
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        dinfo = alloc_ramblock_dirty_info(&index, dinfo);
+        if (dinfo == NULL) {
+            return -1;
+        }
+        info = &dinfo[index];
+        get_ramblock_dirty_info(block, info, &config);
+        if (save_ramblock_hash(info) < 0) {
+            *block_dinfo = dinfo;
+            *block_index = index;
+            return -1;
+        }
+    }
+
+    *block_dinfo = dinfo;
+    *block_index = index;
+
+    return 0;
+}
+
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
     /* todo */
migration/dirtyrate.h +15 -0
index 9db269d..5050add 100644
@@ -24,6 +24,21 @@
  */
 #define RAMBLOCK_INFO_MAX_LEN                     256
 
+/*
+ * Sample page size 4K as default.
+ */
+#define DIRTYRATE_SAMPLE_PAGE_SIZE                4096
+
+/*
+ * Sample page size 4K shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_KB                   12
+
+/*
+ * Sample page size 1G shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_GB                   30
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
You bring light in.
#14 lore raw

Re: [PATCH v5 06/12] migration/dirtyrate: Record hash results for each sampled page

Quoted 114 lines · * David Edmondson ([email protected]) wrote:
Quoted 24 lines · Chuan Zheng

Record hash results for each sampled page, crc32 is taken to calculate hash results for each sampled 4K-page.

---
 migration/dirtyrate.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++
 migration/dirtyrate.h |  15 ++++++
 2 files changed, 151 insertions(+)
migration/dirtyrate.c +1 -0
index f6a94d8..66de426 100644
@@ -10,6 +10,7 @@
  * See the COPYING file in the top-level directory.
  */
 
+#include <zlib.h>
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "crypto/hash.h"
@@ -66,6 +67,141 @@ static void update_dirtyrate(uint64_t msec)
     DirtyStat.dirty_rate = dirtyrate;

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 77

Quoted 10 lines
migration/dirtyrate.c +8 -0
 }
 
+/*
+ * get hash result for the sampled memory with length of 4K byte in ramblock,
+ * which starts from ramblock base address.
+ */
+static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
+                                      uint64_t vfn)
+{
+    struct iovec iov_array;

There's no need for an iovec now that crc32() is being used.

Quoted 6 lines
migration/dirtyrate.c +6 -0
+    uint32_t crc;
+
+    iov_array.iov_base = info->ramblock_addr +
+                         vfn * DIRTYRATE_SAMPLE_PAGE_SIZE;
+    iov_array.iov_len = DIRTYRATE_SAMPLE_PAGE_SIZE;
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 93

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    crc = crc32(0, iov_array.iov_base, iov_array.iov_len);
+
+    return crc;
+}
+
+static int save_ramblock_hash(struct RamblockDirtyInfo *info)
+{
+    unsigned int sample_pages_count;
+    int i;
+    int ret = -1;

There's no need to initialise "ret".

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 94

Quoted 1 line
migration/dirtyrate.c +1 -0
+    GRand *rand = g_rand_new();

Calling g_rand_new() when the result may not be used (because of the various conditions immediately below) seems as though it might waste entropy. Could this be pushed down just above the loop? It would even get rid of the gotos ;-)

Quoted 36 lines
migration/dirtyrate.c +36 -0
+    sample_pages_count = info->sample_pages_count;
+
+    /* ramblock size less than one page, return success to skip this ramblock */
+    if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
+        ret = 0;
+        goto out;
+    }
+
+    info->hash_result = g_try_malloc0_n(sample_pages_count,
+                                        sizeof(uint32_t));
+    if (!info->hash_result) {
+        ret = -1;
+        goto out;
+    }
+
+    info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
+                                            sizeof(uint64_t));
+    if (!info->sample_page_vfn) {
+        g_free(info->hash_result);
+        ret = -1;
+        goto out;
+    }
+
+    for (i = 0; i < sample_pages_count; i++) {
+        info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
+                                                    info->ramblock_pages - 1);
+        info->hash_result[i] = get_ramblock_vfn_hash(info,
+                                                     info->sample_page_vfn[i]);
+    }
+    ret = 0;
+
+out:
+    g_rand_free(rand);
+    return ret;
+}
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 141

Quoted 10 lines
migration/dirtyrate.c +10 -0
+static void get_ramblock_dirty_info(RAMBlock *block,
+                                    struct RamblockDirtyInfo *info,
+                                    struct DirtyRateConfig *config)
+{
+    uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
+
+    /* Right shift 30 bits to calc block size in GB */
+    info->sample_pages_count = (qemu_ram_get_used_length(block) *
+                                sample_pages_per_gigabytes) >>
+                                DIRTYRATE_PAGE_SHIFT_GB;

Doing the calculation this way around seems odd. Why was it not:

Quoted 5 lines
    info->sample_pages_count = (qemu_ram_get_used_length(block) >>
                                DIRTYRATE_PAGE_SHIFT_GB) *
                                sample_pages_per_gigabytes;

?

Because that would give 0 for a 0.5GB block

Dave

Quoted 107 lines
Quoted 12 lines
migration/dirtyrate.c +12 -0
+
+    /* Right shift 12 bits to calc page count in 4KB */
+    info->ramblock_pages = qemu_ram_get_used_length(block) >>
+                           DIRTYRATE_PAGE_SHIFT_KB;
+    info->ramblock_addr = qemu_ram_get_host_addr(block);
+    strcpy(info->idstr, qemu_ram_get_idstr(block));
+}
+
+static struct RamblockDirtyInfo *
+alloc_ramblock_dirty_info(int *block_index,
+                          struct RamblockDirtyInfo *block_dinfo)
+{

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 163

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    struct RamblockDirtyInfo *info = NULL;
+    int index = *block_index;
+
+    if (!block_dinfo) {
+        index = 0;
+        block_dinfo = g_try_new(struct RamblockDirtyInfo, 1);
+    } else {
+        index++;
+        block_dinfo = g_try_realloc(block_dinfo, (index + 1) *
+                                    sizeof(struct RamblockDirtyInfo));

g_try_renew() instead of g_try_realloc()?

Quoted 9 lines
migration/dirtyrate.c +9 -0
+    }
+    if (!block_dinfo) {
+        return NULL;
+    }
+
+    info = &block_dinfo[index];
+    *block_index = index;
+    memset(info, 0, sizeof(struct RamblockDirtyInfo));
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 182

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    return block_dinfo;
+}
+
+static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
+                                     struct DirtyRateConfig config,
+                                     int *block_index)
+{
+    struct RamblockDirtyInfo *info = NULL;
+    struct RamblockDirtyInfo *dinfo = NULL;
+    RAMBlock *block = NULL;

There's no need to initialise "info" or "block".

The declaration of "info" could be pushed into the loop.

Quoted 53 lines
migration/dirtyrate.c +22 -0
+    int index = 0;
+
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        dinfo = alloc_ramblock_dirty_info(&index, dinfo);
+        if (dinfo == NULL) {
+            return -1;
+        }
+        info = &dinfo[index];
+        get_ramblock_dirty_info(block, info, &config);
+        if (save_ramblock_hash(info) < 0) {
+            *block_dinfo = dinfo;
+            *block_index = index;
+            return -1;
+        }
+    }
+
+    *block_dinfo = dinfo;
+    *block_index = index;
+
+    return 0;
+}
+
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
     /* todo */
migration/dirtyrate.h +15 -0
index 9db269d..5050add 100644
@@ -24,6 +24,21 @@
  */
 #define RAMBLOCK_INFO_MAX_LEN                     256
 
+/*
+ * Sample page size 4K as default.
+ */
+#define DIRTYRATE_SAMPLE_PAGE_SIZE                4096
+
+/*
+ * Sample page size 4K shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_KB                   12
+
+/*
+ * Sample page size 1G shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_GB                   30
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
You bring light in.
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#15 lore raw

Re: [PATCH v5 06/12] migration/dirtyrate: Record hash results for each sampled page

Quoted 115 lines · Dr. David Alan Gilbert
Quoted 114 lines · * David Edmondson ([email protected]) wrote:
Quoted 24 lines · Chuan Zheng

Record hash results for each sampled page, crc32 is taken to calculate hash results for each sampled 4K-page.

---
 migration/dirtyrate.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++
 migration/dirtyrate.h |  15 ++++++
 2 files changed, 151 insertions(+)
migration/dirtyrate.c +1 -0
index f6a94d8..66de426 100644
@@ -10,6 +10,7 @@
  * See the COPYING file in the top-level directory.
  */
 
+#include <zlib.h>
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "crypto/hash.h"
@@ -66,6 +67,141 @@ static void update_dirtyrate(uint64_t msec)
     DirtyStat.dirty_rate = dirtyrate;

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 77

Quoted 10 lines
migration/dirtyrate.c +8 -0
 }
 
+/*
+ * get hash result for the sampled memory with length of 4K byte in ramblock,
+ * which starts from ramblock base address.
+ */
+static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
+                                      uint64_t vfn)
+{
+    struct iovec iov_array;

There's no need for an iovec now that crc32() is being used.

Quoted 6 lines
migration/dirtyrate.c +6 -0
+    uint32_t crc;
+
+    iov_array.iov_base = info->ramblock_addr +
+                         vfn * DIRTYRATE_SAMPLE_PAGE_SIZE;
+    iov_array.iov_len = DIRTYRATE_SAMPLE_PAGE_SIZE;
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 93

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    crc = crc32(0, iov_array.iov_base, iov_array.iov_len);
+
+    return crc;
+}
+
+static int save_ramblock_hash(struct RamblockDirtyInfo *info)
+{
+    unsigned int sample_pages_count;
+    int i;
+    int ret = -1;

There's no need to initialise "ret".

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 94

Quoted 1 line
migration/dirtyrate.c +1 -0
+    GRand *rand = g_rand_new();

Calling g_rand_new() when the result may not be used (because of the various conditions immediately below) seems as though it might waste entropy. Could this be pushed down just above the loop? It would even get rid of the gotos ;-)

Quoted 36 lines
migration/dirtyrate.c +36 -0
+    sample_pages_count = info->sample_pages_count;
+
+    /* ramblock size less than one page, return success to skip this ramblock */
+    if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
+        ret = 0;
+        goto out;
+    }
+
+    info->hash_result = g_try_malloc0_n(sample_pages_count,
+                                        sizeof(uint32_t));
+    if (!info->hash_result) {
+        ret = -1;
+        goto out;
+    }
+
+    info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
+                                            sizeof(uint64_t));
+    if (!info->sample_page_vfn) {
+        g_free(info->hash_result);
+        ret = -1;
+        goto out;
+    }
+
+    for (i = 0; i < sample_pages_count; i++) {
+        info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
+                                                    info->ramblock_pages - 1);
+        info->hash_result[i] = get_ramblock_vfn_hash(info,
+                                                     info->sample_page_vfn[i]);
+    }
+    ret = 0;
+
+out:
+    g_rand_free(rand);
+    return ret;
+}
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 141

Quoted 10 lines
migration/dirtyrate.c +10 -0
+static void get_ramblock_dirty_info(RAMBlock *block,
+                                    struct RamblockDirtyInfo *info,
+                                    struct DirtyRateConfig *config)
+{
+    uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
+
+    /* Right shift 30 bits to calc block size in GB */
+    info->sample_pages_count = (qemu_ram_get_used_length(block) *
+                                sample_pages_per_gigabytes) >>
+                                DIRTYRATE_PAGE_SHIFT_GB;

Doing the calculation this way around seems odd. Why was it not:

Quoted 7 lines
Quoted 5 lines
    info->sample_pages_count = (qemu_ram_get_used_length(block) >>
                                DIRTYRATE_PAGE_SHIFT_GB) *
                                sample_pages_per_gigabytes;

?

Because that would give 0 for a 0.5GB block

Ouch, obviously :-) Thanks.

dme.

Signature · 1 line
And removed his hat, in respect of her presence.
#16 lore raw

Re: [PATCH v5 06/12] migration/dirtyrate: Record hash results for each sampled page

In reply to Dr. David Alan Gilbert
Quoted 29 lines · Dr. David Alan Gilbert
Quoted 28 lines · * David Edmondson ([email protected]) wrote:
Quoted 26 lines · Chuan Zheng

Record hash results for each sampled page, crc32 is taken to calculate hash results for each sampled 4K-page.

---
 migration/dirtyrate.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++
 migration/dirtyrate.h |  15 ++++++
 2 files changed, 151 insertions(+)
migration/dirtyrate.c +1 -0
index f6a94d8..66de426 100644
@@ -10,6 +10,7 @@
  * See the COPYING file in the top-level directory.
  */
 
+#include <zlib.h>
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "crypto/hash.h"
@@ -66,6 +67,141 @@ static void update_dirtyrate(uint64_t msec)
     DirtyStat.dirty_rate = dirtyrate;
 }
 
Quoted 11 lines
Quoted 11 lines

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 77

Quoted 8 lines
migration/dirtyrate.c +8 -0
+/*
+ * get hash result for the sampled memory with length of 4K byte in ramblock,
+ * which starts from ramblock base address.
+ */
+static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
+                                      uint64_t vfn)
+{
+    struct iovec iov_array;

There's no need for an iovec now that crc32() is being used.

OK, will take two variables instead to represent base address and length.

Quoted 19 lines
Quoted 19 lines
Quoted 6 lines
migration/dirtyrate.c +6 -0
+    uint32_t crc;
+
+    iov_array.iov_base = info->ramblock_addr +
+                         vfn * DIRTYRATE_SAMPLE_PAGE_SIZE;
+    iov_array.iov_len = DIRTYRATE_SAMPLE_PAGE_SIZE;
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 93

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    crc = crc32(0, iov_array.iov_base, iov_array.iov_len);
+
+    return crc;
+}
+
+static int save_ramblock_hash(struct RamblockDirtyInfo *info)
+{
+    unsigned int sample_pages_count;
+    int i;
+    int ret = -1;

There's no need to initialise "ret".

Quoted 7 lines
Quoted 7 lines

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 94

Quoted 1 line
migration/dirtyrate.c +1 -0
+    GRand *rand = g_rand_new();

Calling g_rand_new() when the result may not be used (because of the various conditions immediately below) seems as though it might waste entropy. Could this be pushed down just above the loop? It would even get rid of the gotos ;-)

OK, i'll try it.

Quoted 53 lines
Quoted 53 lines
Quoted 36 lines
migration/dirtyrate.c +36 -0
+    sample_pages_count = info->sample_pages_count;
+
+    /* ramblock size less than one page, return success to skip this ramblock */
+    if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
+        ret = 0;
+        goto out;
+    }
+
+    info->hash_result = g_try_malloc0_n(sample_pages_count,
+                                        sizeof(uint32_t));
+    if (!info->hash_result) {
+        ret = -1;
+        goto out;
+    }
+
+    info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
+                                            sizeof(uint64_t));
+    if (!info->sample_page_vfn) {
+        g_free(info->hash_result);
+        ret = -1;
+        goto out;
+    }
+
+    for (i = 0; i < sample_pages_count; i++) {
+        info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
+                                                    info->ramblock_pages - 1);
+        info->hash_result[i] = get_ramblock_vfn_hash(info,
+                                                     info->sample_page_vfn[i]);
+    }
+    ret = 0;
+
+out:
+    g_rand_free(rand);
+    return ret;
+}
+

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 141

Quoted 10 lines
migration/dirtyrate.c +10 -0
+static void get_ramblock_dirty_info(RAMBlock *block,
+                                    struct RamblockDirtyInfo *info,
+                                    struct DirtyRateConfig *config)
+{
+    uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
+
+    /* Right shift 30 bits to calc block size in GB */
+    info->sample_pages_count = (qemu_ram_get_used_length(block) *
+                                sample_pages_per_gigabytes) >>
+                                DIRTYRATE_PAGE_SHIFT_GB;

Doing the calculation this way around seems odd. Why was it not:

    info->sample_pages_count = (qemu_ram_get_used_length(block) >>
                                DIRTYRATE_PAGE_SHIFT_GB) *
                                sample_pages_per_gigabytes;
Quoted 31 lines
Quoted 1 line

?

Because that would give 0 for a 0.5GB block

Dave

Quoted 25 lines
Quoted 12 lines
migration/dirtyrate.c +12 -0
+
+    /* Right shift 12 bits to calc page count in 4KB */
+    info->ramblock_pages = qemu_ram_get_used_length(block) >>
+                           DIRTYRATE_PAGE_SHIFT_KB;
+    info->ramblock_addr = qemu_ram_get_host_addr(block);
+    strcpy(info->idstr, qemu_ram_get_idstr(block));
+}
+
+static struct RamblockDirtyInfo *
+alloc_ramblock_dirty_info(int *block_index,
+                          struct RamblockDirtyInfo *block_dinfo)
+{

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 163

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    struct RamblockDirtyInfo *info = NULL;
+    int index = *block_index;
+
+    if (!block_dinfo) {
+        index = 0;
+        block_dinfo = g_try_new(struct RamblockDirtyInfo, 1);
+    } else {
+        index++;
+        block_dinfo = g_try_realloc(block_dinfo, (index + 1) *
+                                    sizeof(struct RamblockDirtyInfo));

g_try_renew() instead of g_try_realloc()?

Hi,
I am not sure that because there only one place in qemu to use g_try_renew. Could you tell me why, because i think g_try_realloc will also return NULL when error happen:)

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
migration/dirtyrate.c +14 -0
+    }
+    if (!block_dinfo) {
+        return NULL;
+    }
+
+    info = &block_dinfo[index];
+    *block_index = index;
+    memset(info, 0, sizeof(struct RamblockDirtyInfo));
+
+    return block_dinfo;
+}
+
+static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
+                                     struct DirtyRateConfig config,
Quoted 8 lines
Quoted 8 lines

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 182

Quoted 5 lines
migration/dirtyrate.c +5 -0
+                                     int *block_index)
+{
+    struct RamblockDirtyInfo *info = NULL;
+    struct RamblockDirtyInfo *dinfo = NULL;
+    RAMBlock *block = NULL;

There's no need to initialise "info" or "block".

OK, will remove unused initialization in V6.

Quoted 60 lines
Quoted 60 lines

The declaration of "info" could be pushed into the loop.

Quoted 53 lines
migration/dirtyrate.c +22 -0
+    int index = 0;
+
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        dinfo = alloc_ramblock_dirty_info(&index, dinfo);
+        if (dinfo == NULL) {
+            return -1;
+        }
+        info = &dinfo[index];
+        get_ramblock_dirty_info(block, info, &config);
+        if (save_ramblock_hash(info) < 0) {
+            *block_dinfo = dinfo;
+            *block_index = index;
+            return -1;
+        }
+    }
+
+    *block_dinfo = dinfo;
+    *block_index = index;
+
+    return 0;
+}
+
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
     /* todo */
migration/dirtyrate.h +15 -0
index 9db269d..5050add 100644
@@ -24,6 +24,21 @@
  */
 #define RAMBLOCK_INFO_MAX_LEN                     256
 
+/*
+ * Sample page size 4K as default.
+ */
+#define DIRTYRATE_SAMPLE_PAGE_SIZE                4096
+
+/*
+ * Sample page size 4K shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_KB                   12
+
+/*
+ * Sample page size 1G shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_GB                   30
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
You bring light in.
#17 lore raw

Re: [PATCH v5 06/12] migration/dirtyrate: Record hash results for each sampled page

Quoted 17 lines · Zheng Chuan
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
migration/dirtyrate.c +9 -0
+static struct RamblockDirtyInfo *
+alloc_ramblock_dirty_info(int *block_index,
+                          struct RamblockDirtyInfo *block_dinfo)
+{
+    struct RamblockDirtyInfo *info = NULL;
+    int index = *block_index;
+
+    if (!block_dinfo) {
+        index = 0;
Quoted 8 lines
Quoted 8 lines

migration/dirtyrate.c static void update_dirtyrate(uint64_t msec) · line 163

Quoted 5 lines
migration/dirtyrate.c +5 -0
+        block_dinfo = g_try_new(struct RamblockDirtyInfo, 1);
+    } else {
+        index++;
+        block_dinfo = g_try_realloc(block_dinfo, (index + 1) *
+                                    sizeof(struct RamblockDirtyInfo));

g_try_renew() instead of g_try_realloc()?

Quoted 3 lines

Hi,
I am not sure that because there only one place in qemu to use g_try_renew. Could you tell me why, because i think g_try_realloc will also return NULL when error happen:)

Only suggested because it would make the two branches of the code more similar.

dme.

Signature · 1 line
And you can't hold me down, 'cause I belong to the hurricane.
#18 lore raw

PATCH v5 7/12 migration/dirtyrate: Compare page hash results for recorded sampled page

In reply to Chuan Zheng

Compare page hash results for recorded sampled page.

---
 migration/dirtyrate.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
migration/dirtyrate.c +64 -0
index 66de426..050270d 100644
@@ -202,6 +202,70 @@ static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
     return 0;
 }
 
+static int calc_page_dirty_rate(struct RamblockDirtyInfo *info)
+{
+    uint32_t crc;
+    int i;
+
+    for (i = 0; i < info->sample_pages_count; i++) {
+        crc = get_ramblock_vfn_hash(info, info->sample_page_vfn[i]);
+        if (crc != info->hash_result[i]) {
+            info->sample_dirty_count++;
+        }
+    }
+
+    return 0;
+}
+
+static bool find_page_matched(RAMBlock *block, struct RamblockDirtyInfo *infos,
+                              int count, struct RamblockDirtyInfo **matched)
+{
+    int i;
+
+    for (i = 0; i < count; i++) {
+        if (!strcmp(infos[i].idstr, qemu_ram_get_idstr(block))) {
+            break;
+        }
+    }
+
+    if (i == count) {
+        return false;
+    }
+
+    if (infos[i].ramblock_addr != qemu_ram_get_host_addr(block) ||
+        infos[i].ramblock_pages !=
+            (qemu_ram_get_used_length(block) >> DIRTYRATE_PAGE_SHIFT_KB)) {
+        return false;
+    }
+
+    *matched = &infos[i];
+    return true;
+}
+
+static int compare_page_hash_info(struct RamblockDirtyInfo *info,
+                                  int block_index)
+{
+    struct RamblockDirtyInfo *block_dinfo = NULL;
+    RAMBlock *block = NULL;
+
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        block_dinfo = NULL;
+        if (!find_page_matched(block, info, block_index + 1, &block_dinfo)) {
+            continue;
+        }
+        if (calc_page_dirty_rate(block_dinfo) < 0) {
+            return -1;
+        }
+        update_dirtyrate_stat(block_dinfo);
+    }
+
+    if (!DirtyStat.total_sample_count) {
+        return -1;
+    }
+
+    return 0;
+}
+
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
     /* todo */
Signature · 1 line
1.8.3.1
#19 lore raw

Re: [PATCH v5 07/12] migration/dirtyrate: Compare page hash results for recorded sampled page

Quoted 13 lines · Chuan Zheng

Compare page hash results for recorded sampled page.

---
 migration/dirtyrate.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
migration/dirtyrate.c +0 -0
index 66de426..050270d 100644

migration/dirtyrate.c static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo, · line 205

Quoted 4 lines
migration/dirtyrate.c +1 -0
@@ -202,6 +202,70 @@ static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
     return 0;
 }
 
+static int calc_page_dirty_rate(struct RamblockDirtyInfo *info)

This never fails - could be void?

Quoted 6 lines
migration/dirtyrate.c +6 -0
+{
+    uint32_t crc;
+    int i;
+
+    for (i = 0; i < info->sample_pages_count; i++) {
+        crc = get_ramblock_vfn_hash(info, info->sample_page_vfn[i]);

migration/dirtyrate.c static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo, · line 221

Quoted 10 lines
migration/dirtyrate.c +10 -0
+        if (crc != info->hash_result[i]) {
+            info->sample_dirty_count++;
+        }
+    }
+
+    return 0;
+}
+
+static bool find_page_matched(RAMBlock *block, struct RamblockDirtyInfo *infos,
+                              int count, struct RamblockDirtyInfo **matched)

This might as well just return a struct RamblockDirtyInfo pointer (or NULL).

Quoted 52 lines
migration/dirtyrate.c +47 -0
+{
+    int i;
+
+    for (i = 0; i < count; i++) {
+        if (!strcmp(infos[i].idstr, qemu_ram_get_idstr(block))) {
+            break;
+        }
+    }
+
+    if (i == count) {
+        return false;
+    }
+
+    if (infos[i].ramblock_addr != qemu_ram_get_host_addr(block) ||
+        infos[i].ramblock_pages !=
+            (qemu_ram_get_used_length(block) >> DIRTYRATE_PAGE_SHIFT_KB)) {
+        return false;
+    }
+
+    *matched = &infos[i];
+    return true;
+}
+
+static int compare_page_hash_info(struct RamblockDirtyInfo *info,
+                                  int block_index)
+{
+    struct RamblockDirtyInfo *block_dinfo = NULL;
+    RAMBlock *block = NULL;
+
+    RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        block_dinfo = NULL;
+        if (!find_page_matched(block, info, block_index + 1, &block_dinfo)) {
+            continue;
+        }
+        if (calc_page_dirty_rate(block_dinfo) < 0) {
+            return -1;
+        }
+        update_dirtyrate_stat(block_dinfo);
+    }
+
+    if (!DirtyStat.total_sample_count) {
+        return -1;
+    }
+
+    return 0;
+}
+
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
     /* todo */
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
Sometimes these eyes, forget the face they're peering from.
#20 lore raw

PATCH v5 8/12 migration/dirtyrate: skip sampling ramblock with size below MIN_RAMBLOCK_SIZE

In reply to Chuan Zheng

In order to sample real RAM, skip ramblock with size below MIN_RAMBLOCK_SIZE which is set as 128M.

---
 migration/dirtyrate.c | 24 ++++++++++++++++++++++++
 migration/dirtyrate.h | 10 ++++++++++
 2 files changed, 34 insertions(+)
migration/dirtyrate.c +24 -0
index 050270d..bd398b7 100644
@@ -173,6 +173,24 @@ alloc_ramblock_dirty_info(int *block_index,
     return block_dinfo;
 }
 
+static int skip_sample_ramblock(RAMBlock *block)
+{
+    int64_t ramblock_size;
+
+    /* ramblock size in MB */
+    ramblock_size = qemu_ram_get_used_length(block) >> DIRTYRATE_PAGE_SHIFT_MB;
+
+    /*
+     * Consider ramblock with size larger than 128M is what we
+     * want to sample.
+     */
+    if (ramblock_size < MIN_RAMBLOCK_SIZE) {
+        return -1;
+    }
+
+    return 0;
+}
+
 static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
                                      struct DirtyRateConfig config,
                                      int *block_index)
@@ -183,6 +201,9 @@ static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
     int index = 0;
 
     RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        if (skip_sample_ramblock(block) < 0) {
+            continue;
+        }
         dinfo = alloc_ramblock_dirty_info(&index, dinfo);
         if (dinfo == NULL) {
             return -1;
@@ -249,6 +270,9 @@ static int compare_page_hash_info(struct RamblockDirtyInfo *info,
     RAMBlock *block = NULL;
 
     RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        if (skip_sample_ramblock(block) < 0) {
+            continue;
+        }
         block_dinfo = NULL;
         if (!find_page_matched(block, info, block_index + 1, &block_dinfo)) {
             continue;
migration/dirtyrate.h +10 -0
index 5050add..41bc264 100644
@@ -35,10 +35,20 @@
 #define DIRTYRATE_PAGE_SHIFT_KB                   12
 
 /*
+ * Sample page size MB shift
+ */
+#define DIRTYRATE_PAGE_SHIFT_MB                   20
+
+/*
  * Sample page size 1G shift
  */
 #define DIRTYRATE_PAGE_SHIFT_GB                   30
 
+/*
+ * minimum ramblock size to sampled
+ */
+#define MIN_RAMBLOCK_SIZE                         128
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
Signature · 1 line
1.8.3.1
#21 lore raw

Re: [PATCH v5 08/12] migration/dirtyrate: skip sampling ramblock with size below MIN_RAMBLOCK_SIZE

Quoted 67 lines · Chuan Zheng

In order to sample real RAM, skip ramblock with size below MIN_RAMBLOCK_SIZE which is set as 128M.

---
 migration/dirtyrate.c | 24 ++++++++++++++++++++++++
 migration/dirtyrate.h | 10 ++++++++++
 2 files changed, 34 insertions(+)
migration/dirtyrate.c +24 -0
index 050270d..bd398b7 100644
@@ -173,6 +173,24 @@ alloc_ramblock_dirty_info(int *block_index,
     return block_dinfo;
 }
 
+static int skip_sample_ramblock(RAMBlock *block)
+{
+    int64_t ramblock_size;
+
+    /* ramblock size in MB */
+    ramblock_size = qemu_ram_get_used_length(block) >> DIRTYRATE_PAGE_SHIFT_MB;
+
+    /*
+     * Consider ramblock with size larger than 128M is what we
+     * want to sample.
+     */
+    if (ramblock_size < MIN_RAMBLOCK_SIZE) {
+        return -1;
+    }
+
+    return 0;
+}
+
 static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
                                      struct DirtyRateConfig config,
                                      int *block_index)
@@ -183,6 +201,9 @@ static int record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
     int index = 0;
 
     RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        if (skip_sample_ramblock(block) < 0) {
+            continue;
+        }
         dinfo = alloc_ramblock_dirty_info(&index, dinfo);
         if (dinfo == NULL) {
             return -1;
@@ -249,6 +270,9 @@ static int compare_page_hash_info(struct RamblockDirtyInfo *info,
     RAMBlock *block = NULL;
 
     RAMBLOCK_FOREACH_MIGRATABLE(block) {
+        if (skip_sample_ramblock(block) < 0) {
+            continue;
+        }
         block_dinfo = NULL;
         if (!find_page_matched(block, info, block_index + 1, &block_dinfo)) {
             continue;
migration/dirtyrate.h +1 -0
index 5050add..41bc264 100644
@@ -35,10 +35,20 @@
 #define DIRTYRATE_PAGE_SHIFT_KB                   12
 
 /*
+ * Sample page size MB shift

migration/dirtyrate.h line 48

Quoted 10 lines
migration/dirtyrate.h +6 -0
+ */
+#define DIRTYRATE_PAGE_SHIFT_MB                   20
+
+/*
  * Sample page size 1G shift
  */
 #define DIRTYRATE_PAGE_SHIFT_GB                   30
 
+/*
+ * minimum ramblock size to sampled

"Minimum RAMBlock size to sample, in megabytes."

Quoted 8 lines
migration/dirtyrate.h +3 -0
+ */
+#define MIN_RAMBLOCK_SIZE                         128
+
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
 
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
I went starin' out of my window, been caught doin' it once or twice.
#22 lore raw

PATCH v5 9/12 migration/dirtyrate: Implement get_sample_page_period() and block_sample_page_period()

In reply to Chuan Zheng

Implement get_sample_page_period() and set_sample_page_period() to sleep specific time between sample actions.

---
 migration/dirtyrate.c | 24 ++++++++++++++++++++++++
 migration/dirtyrate.h |  2 ++
 2 files changed, 26 insertions(+)
migration/dirtyrate.c +24 -0
index bd398b7..d1c0a78 100644
@@ -28,6 +28,30 @@
 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
 static struct DirtyRateStat DirtyStat;
 
+static int64_t set_sample_page_period(int64_t msec, int64_t initial_time)
+{
+    int64_t current_time;
+
+    current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    if ((current_time - initial_time) >= msec) {
+        msec = current_time - initial_time;
+    } else {
+        g_usleep((msec + initial_time - current_time) * 1000);
+    }
+
+    return msec;
+}
+
+static int64_t get_sample_page_period(int64_t sec)
+{
+    if (sec <= MIN_FETCH_DIRTYRATE_TIME_SEC ||
+        sec > MAX_FETCH_DIRTYRATE_TIME_SEC) {
+        sec = DEFAULT_FETCH_DIRTYRATE_TIME_SEC;
+    }
+
+    return sec;
+}
+
 static int dirtyrate_set_state(int *state, int old_state, int new_state)
 {
     assert(new_state < DIRTY_RATE_STATUS__MAX);
migration/dirtyrate.h +2 -0
index 41bc264..50a5636 100644
@@ -51,6 +51,8 @@
 
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
+#define MIN_FETCH_DIRTYRATE_TIME_SEC              0
+#define MAX_FETCH_DIRTYRATE_TIME_SEC              60
 
 struct DirtyRateConfig {
     uint64_t sample_pages_per_gigabytes; /* sample pages per GB */
Signature · 1 line
1.8.3.1
#23 lore raw

Re: [PATCH v5 09/12] migration/dirtyrate: Implement get_sample_page_period() and block_sample_page_period()

Quoted 24 lines · Chuan Zheng

Implement get_sample_page_period() and set_sample_page_period() to sleep specific time between sample actions.

---
 migration/dirtyrate.c | 24 ++++++++++++++++++++++++
 migration/dirtyrate.h |  2 ++
 2 files changed, 26 insertions(+)
migration/dirtyrate.c +7 -0
index bd398b7..d1c0a78 100644
@@ -28,6 +28,30 @@
 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
 static struct DirtyRateStat DirtyStat;
 
+static int64_t set_sample_page_period(int64_t msec, int64_t initial_time)
+{
+    int64_t current_time;
+
+    current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    if ((current_time - initial_time) >= msec) {
+        msec = current_time - initial_time;

migration/dirtyrate.c line 47

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    } else {
+        g_usleep((msec + initial_time - current_time) * 1000);
+    }
+
+    return msec;
+}
+
+static int64_t get_sample_page_period(int64_t sec)
+{
+    if (sec <= MIN_FETCH_DIRTYRATE_TIME_SEC ||

Shouldn't the minimum value be allowed?

That is, this test should be "sec < MIN_FETCH_DIRTYRATE_TIME_SEC" and MIN_FETCH_DIRTYRATE_TIME_SEC should be 1.

Quoted 25 lines
migration/dirtyrate.c +7 -0
+        sec > MAX_FETCH_DIRTYRATE_TIME_SEC) {
+        sec = DEFAULT_FETCH_DIRTYRATE_TIME_SEC;
+    }
+
+    return sec;
+}
+
 static int dirtyrate_set_state(int *state, int old_state, int new_state)
 {
     assert(new_state < DIRTY_RATE_STATUS__MAX);
migration/dirtyrate.h +2 -0
index 41bc264..50a5636 100644
@@ -51,6 +51,8 @@
 
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
+#define MIN_FETCH_DIRTYRATE_TIME_SEC              0
+#define MAX_FETCH_DIRTYRATE_TIME_SEC              60
 
 struct DirtyRateConfig {
     uint64_t sample_pages_per_gigabytes; /* sample pages per GB */
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
Facts don't do what I want them to.
#24 lore raw

Re: [PATCH v5 09/12] migration/dirtyrate: Implement get_sample_page_period() and block_sample_page_period()

Quoted 39 lines · David Edmondson
Quoted 24 lines · Chuan Zheng

Implement get_sample_page_period() and set_sample_page_period() to sleep specific time between sample actions.

---
 migration/dirtyrate.c | 24 ++++++++++++++++++++++++
 migration/dirtyrate.h |  2 ++
 2 files changed, 26 insertions(+)
migration/dirtyrate.c +7 -0
index bd398b7..d1c0a78 100644
@@ -28,6 +28,30 @@
 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
 static struct DirtyRateStat DirtyStat;
 
+static int64_t set_sample_page_period(int64_t msec, int64_t initial_time)
+{
+    int64_t current_time;
+
+    current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    if ((current_time - initial_time) >= msec) {
+        msec = current_time - initial_time;

migration/dirtyrate.c line 47

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    } else {
+        g_usleep((msec + initial_time - current_time) * 1000);
+    }
+
+    return msec;
+}
+
+static int64_t get_sample_page_period(int64_t sec)
+{
+    if (sec <= MIN_FETCH_DIRTYRATE_TIME_SEC ||

Shouldn't the minimum value be allowed?

Quoted 3 lines

That is, this test should be "sec < MIN_FETCH_DIRTYRATE_TIME_SEC" and MIN_FETCH_DIRTYRATE_TIME_SEC should be 1.

Well, Actually we could measure dirtyrate within duration below 1s, like 0.5s.
Howerver, I am reconsider that maybe taking 0.5s as MIN_FETCH_DIRTYRATE_TIME_SEC is better in case of someone to do nasty thing like setting a meaningless time duration which is close to 0:)

Quoted 28 lines
Quoted 25 lines
migration/dirtyrate.c +7 -0
+        sec > MAX_FETCH_DIRTYRATE_TIME_SEC) {
+        sec = DEFAULT_FETCH_DIRTYRATE_TIME_SEC;
+    }
+
+    return sec;
+}
+
 static int dirtyrate_set_state(int *state, int old_state, int new_state)
 {
     assert(new_state < DIRTY_RATE_STATUS__MAX);
migration/dirtyrate.h +2 -0
index 41bc264..50a5636 100644
@@ -51,6 +51,8 @@
 
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
+#define MIN_FETCH_DIRTYRATE_TIME_SEC              0
+#define MAX_FETCH_DIRTYRATE_TIME_SEC              60
 
 struct DirtyRateConfig {
     uint64_t sample_pages_per_gigabytes; /* sample pages per GB */
Signature · 1 line
1.8.3.1

dme.

#25 lore raw

Re: [PATCH v5 09/12] migration/dirtyrate: Implement get_sample_page_period() and block_sample_page_period()

Quoted 42 lines · Zheng Chuan
Quoted 39 lines · David Edmondson
Quoted 24 lines · Chuan Zheng

Implement get_sample_page_period() and set_sample_page_period() to sleep specific time between sample actions.

---
 migration/dirtyrate.c | 24 ++++++++++++++++++++++++
 migration/dirtyrate.h |  2 ++
 2 files changed, 26 insertions(+)
migration/dirtyrate.c +7 -0
index bd398b7..d1c0a78 100644
@@ -28,6 +28,30 @@
 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
 static struct DirtyRateStat DirtyStat;
 
+static int64_t set_sample_page_period(int64_t msec, int64_t initial_time)
+{
+    int64_t current_time;
+
+    current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    if ((current_time - initial_time) >= msec) {
+        msec = current_time - initial_time;

migration/dirtyrate.c line 47

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    } else {
+        g_usleep((msec + initial_time - current_time) * 1000);
+    }
+
+    return msec;
+}
+
+static int64_t get_sample_page_period(int64_t sec)
+{
+    if (sec <= MIN_FETCH_DIRTYRATE_TIME_SEC ||

Shouldn't the minimum value be allowed?

Quoted 7 lines
Quoted 3 lines

That is, this test should be "sec < MIN_FETCH_DIRTYRATE_TIME_SEC" and MIN_FETCH_DIRTYRATE_TIME_SEC should be 1.

Well, Actually we could measure dirtyrate within duration below 1s, like 0.5s.
Howerver, I am reconsider that maybe taking 0.5s as MIN_FETCH_DIRTYRATE_TIME_SEC is better in case of someone to do nasty thing like setting a meaningless time duration which is close to 0:)

Ouch, since i define sec as int64_t,it could never be 0.5s:-).
I will take 1s as minimum value because it will take about 100ms for large vm to hash its result. Thanks for your review.

Quoted 28 lines
Quoted 28 lines
Quoted 25 lines
migration/dirtyrate.c +7 -0
+        sec > MAX_FETCH_DIRTYRATE_TIME_SEC) {
+        sec = DEFAULT_FETCH_DIRTYRATE_TIME_SEC;
+    }
+
+    return sec;
+}
+
 static int dirtyrate_set_state(int *state, int old_state, int new_state)
 {
     assert(new_state < DIRTY_RATE_STATUS__MAX);
migration/dirtyrate.h +2 -0
index 41bc264..50a5636 100644
@@ -51,6 +51,8 @@
 
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
+#define MIN_FETCH_DIRTYRATE_TIME_SEC              0
+#define MAX_FETCH_DIRTYRATE_TIME_SEC              60
 
 struct DirtyRateConfig {
     uint64_t sample_pages_per_gigabytes; /* sample pages per GB */
Signature · 1 line
1.8.3.1

dme.

#26 lore raw

Re: [PATCH v5 09/12] migration/dirtyrate: Implement get_sample_page_period() and block_sample_page_period()

In reply to Zheng Chuan
Quoted 40 lines · Zheng Chuan
Quoted 39 lines · David Edmondson
Quoted 24 lines · Chuan Zheng

Implement get_sample_page_period() and set_sample_page_period() to sleep specific time between sample actions.

---
 migration/dirtyrate.c | 24 ++++++++++++++++++++++++
 migration/dirtyrate.h |  2 ++
 2 files changed, 26 insertions(+)
migration/dirtyrate.c +7 -0
index bd398b7..d1c0a78 100644
@@ -28,6 +28,30 @@
 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
 static struct DirtyRateStat DirtyStat;
 
+static int64_t set_sample_page_period(int64_t msec, int64_t initial_time)
+{
+    int64_t current_time;
+
+    current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    if ((current_time - initial_time) >= msec) {
+        msec = current_time - initial_time;

migration/dirtyrate.c line 47

Quoted 10 lines
migration/dirtyrate.c +10 -0
+    } else {
+        g_usleep((msec + initial_time - current_time) * 1000);
+    }
+
+    return msec;
+}
+
+static int64_t get_sample_page_period(int64_t sec)
+{
+    if (sec <= MIN_FETCH_DIRTYRATE_TIME_SEC ||

Shouldn't the minimum value be allowed?

Quoted 6 lines
Quoted 3 lines

That is, this test should be "sec < MIN_FETCH_DIRTYRATE_TIME_SEC" and MIN_FETCH_DIRTYRATE_TIME_SEC should be 1.

Well, Actually we could measure dirtyrate within duration below 1s, like 0.5s.
Howerver, I am reconsider that maybe taking 0.5s as MIN_FETCH_DIRTYRATE_TIME_SEC is better in case of someone to do nasty thing like setting a meaningless time duration which is close to 0:)

I think that a minimum of 1 second is fine. My concern is only that if you say "the minimum is X" but then don't let me choose X, it seems weird.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
migration/dirtyrate.c +7 -0
+        sec > MAX_FETCH_DIRTYRATE_TIME_SEC) {
+        sec = DEFAULT_FETCH_DIRTYRATE_TIME_SEC;
+    }
+
+    return sec;
+}
+
 static int dirtyrate_set_state(int *state, int old_state, int new_state)
 {
     assert(new_state < DIRTY_RATE_STATUS__MAX);
migration/dirtyrate.h +2 -0
index 41bc264..50a5636 100644
@@ -51,6 +51,8 @@
 
 /* Take 1s as default for calculation duration */
 #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC          1
+#define MIN_FETCH_DIRTYRATE_TIME_SEC              0
+#define MAX_FETCH_DIRTYRATE_TIME_SEC              60
 
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
migration/dirtyrate.h +0 -0
 struct DirtyRateConfig {
     uint64_t sample_pages_per_gigabytes; /* sample pages per GB */
Signature · 1 line
1.8.3.1

dme.

dme.

Signature · 1 line
Hello? Is anybody home? Well, you don't know me, but I know you.
#27 lore raw

PATCH v5 10/12 migration/dirtyrate: Implement calculate_dirtyrate() function

In reply to Chuan Zheng

Implement calculate_dirtyrate() function.

---
 migration/dirtyrate.c | 45 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)
migration/dirtyrate.c +43 -2
index d1c0a78..9f52f5f 100644
@@ -171,6 +171,21 @@ static void get_ramblock_dirty_info(RAMBlock *block,
     strcpy(info->idstr, qemu_ram_get_idstr(block));
 }
 
+static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
+{
+    int i;
+
+    if (!infos) {
+        return;
+    }
+
+    for (i = 0; i < count; i++) {
+        g_free(infos[i].sample_page_vfn);
+        g_free(infos[i].hash_result);
+    }
+    g_free(infos);
+}
+
 static struct RamblockDirtyInfo *
 alloc_ramblock_dirty_info(int *block_index,
                           struct RamblockDirtyInfo *block_dinfo)
@@ -316,8 +331,34 @@ static int compare_page_hash_info(struct RamblockDirtyInfo *info,
 
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
-    /* todo */
-    return;
+    struct RamblockDirtyInfo *block_dinfo = NULL;
+    int block_index = 0;
+    int64_t msec = 0;
+    int64_t initial_time;
+
+    rcu_register_thread();
+    reset_dirtyrate_stat();
+    initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    rcu_read_lock();
+    if (record_ramblock_hash_info(&block_dinfo, config, &block_index) < 0) {
+        goto out;
+    }
+    rcu_read_unlock();
+
+    msec = config.sample_period_seconds * 1000;
+    msec = set_sample_page_period(msec, initial_time);
+
+    rcu_read_lock();
+    if (compare_page_hash_info(block_dinfo, block_index) < 0) {
+        goto out;
+    }
+
+    update_dirtyrate(msec);
+
+out:
+    rcu_read_unlock();
+    free_ramblock_dirty_info(block_dinfo, block_index + 1);
+    rcu_unregister_thread();
 }
 
 void *get_dirtyrate_thread(void *arg)
Signature · 1 line
1.8.3.1
#28 lore raw

Re: [PATCH v5 10/12] migration/dirtyrate: Implement calculate_dirtyrate() function

Quoted 39 lines · Chuan Zheng

Implement calculate_dirtyrate() function.

---
 migration/dirtyrate.c | 45 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)
migration/dirtyrate.c +15 -1
index d1c0a78..9f52f5f 100644
@@ -171,6 +171,21 @@ static void get_ramblock_dirty_info(RAMBlock *block,
     strcpy(info->idstr, qemu_ram_get_idstr(block));
 }
 
+static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
+{
+    int i;
+
+    if (!infos) {
+        return;
+    }
+
+    for (i = 0; i < count; i++) {
+        g_free(infos[i].sample_page_vfn);
+        g_free(infos[i].hash_result);
+    }
+    g_free(infos);
+}
+
 static struct RamblockDirtyInfo *
 alloc_ramblock_dirty_info(int *block_index,
                           struct RamblockDirtyInfo *block_dinfo)
@@ -316,8 +331,34 @@ static int compare_page_hash_info(struct RamblockDirtyInfo *info,
 
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
-    /* todo */

migration/dirtyrate.c static int compare_page_hash_info(struct RamblockDirtyInfo *info, · line 342

Quoted 10 lines
migration/dirtyrate.c +9 -1
-    return;
+    struct RamblockDirtyInfo *block_dinfo = NULL;
+    int block_index = 0;
+    int64_t msec = 0;
+    int64_t initial_time;
+
+    rcu_register_thread();
+    reset_dirtyrate_stat();
+    initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    rcu_read_lock();

Page dirtying that happens while acquiring the lock will not be accounted for, but is within the time window. Could we store the time after acquiring the lock?

Quoted 7 lines
migration/dirtyrate.c +7 -0
+    if (record_ramblock_hash_info(&block_dinfo, config, &block_index) < 0) {
+        goto out;
+    }
+    rcu_read_unlock();
+
+    msec = config.sample_period_seconds * 1000;
+    msec = set_sample_page_period(msec, initial_time);

migration/dirtyrate.c static int compare_page_hash_info(struct RamblockDirtyInfo *info, · line 359

Quoted 10 lines
migration/dirtyrate.c +10 -0
+
+    rcu_read_lock();
+    if (compare_page_hash_info(block_dinfo, block_index) < 0) {
+        goto out;
+    }
+
+    update_dirtyrate(msec);
+
+out:
+    rcu_read_unlock();

Is it necessary to hold the lock across update_dirtyrate()?

Quoted 7 lines
migration/dirtyrate.c +2 -0
+    free_ramblock_dirty_info(block_dinfo, block_index + 1);
+    rcu_unregister_thread();
 }
 
 void *get_dirtyrate_thread(void *arg)
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
There's someone in my head but it's not me.
#29 lore raw

Re: [PATCH v5 10/12] migration/dirtyrate: Implement calculate_dirtyrate() function

Quoted 47 lines · David Edmondson
Quoted 45 lines · Chuan Zheng

Implement calculate_dirtyrate() function.

---
 migration/dirtyrate.c | 45 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)
migration/dirtyrate.c +20 -2
index d1c0a78..9f52f5f 100644
@@ -171,6 +171,21 @@ static void get_ramblock_dirty_info(RAMBlock *block,
     strcpy(info->idstr, qemu_ram_get_idstr(block));
 }
 
+static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
+{
+    int i;
+
+    if (!infos) {
+        return;
+    }
+
+    for (i = 0; i < count; i++) {
+        g_free(infos[i].sample_page_vfn);
+        g_free(infos[i].hash_result);
+    }
+    g_free(infos);
+}
+
 static struct RamblockDirtyInfo *
 alloc_ramblock_dirty_info(int *block_index,
                           struct RamblockDirtyInfo *block_dinfo)
@@ -316,8 +331,34 @@ static int compare_page_hash_info(struct RamblockDirtyInfo *info,
 
 static void calculate_dirtyrate(struct DirtyRateConfig config)
 {
-    /* todo */
-    return;
+    struct RamblockDirtyInfo *block_dinfo = NULL;
+    int block_index = 0;
+    int64_t msec = 0;
+    int64_t initial_time;
+
Quoted 9 lines

migration/dirtyrate.c static int compare_page_hash_info(struct RamblockDirtyInfo *info, · line 342

Quoted 4 lines
migration/dirtyrate.c +4 -0
+    rcu_register_thread();
+    reset_dirtyrate_stat();
+    initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
+    rcu_read_lock();

Page dirtying that happens while acquiring the lock will not be accounted for, but is within the time window. Could we store the time after acquiring the lock?

Yes, it would be better.
will fix in V6.

Quoted 11 lines
Quoted 11 lines
migration/dirtyrate.c +11 -0
+    if (record_ramblock_hash_info(&block_dinfo, config, &block_index) < 0) {
+        goto out;
+    }
+    rcu_read_unlock();
+
+    msec = config.sample_period_seconds * 1000;
+    msec = set_sample_page_period(msec, initial_time);
+
+    rcu_read_lock();
+    if (compare_page_hash_info(block_dinfo, block_index) < 0) {
+        goto out;
Quoted 9 lines

migration/dirtyrate.c static int compare_page_hash_info(struct RamblockDirtyInfo *info, · line 359

Quoted 6 lines
migration/dirtyrate.c +6 -0
+    }
+
+    update_dirtyrate(msec);
+
+out:
+    rcu_read_unlock();

Is it necessary to hold the lock across update_dirtyrate()?

There is no need for that.
Will fix it in V6.

Quoted 10 lines
Quoted 7 lines
migration/dirtyrate.c +2 -0
+    free_ramblock_dirty_info(block_dinfo, block_index + 1);
+    rcu_unregister_thread();
 }
 
 void *get_dirtyrate_thread(void *arg)
Signature · 1 line
1.8.3.1

dme.

#30 lore raw

PATCH v5 11/12 migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function

In reply to Chuan Zheng

Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function which could be called

---
 migration/dirtyrate.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 qapi/migration.json   | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
migration/dirtyrate.c +45 -0
index 9f52f5f..08c46d3 100644
@@ -62,6 +62,28 @@ static int dirtyrate_set_state(int *state, int old_state, int new_state)
     }
 }
 
+static struct DirtyRateInfo *query_dirty_rate_info(void)
+{
+    int64_t dirty_rate = DirtyStat.dirty_rate;
+    struct DirtyRateInfo *info = g_malloc0(sizeof(DirtyRateInfo));
+
+    if (CalculatingState == DIRTY_RATE_STATUS_MEASURED) {
+        info->dirty_rate = dirty_rate;
+    } else {
+        info->dirty_rate = -1;
+    }
+
+    info->status = CalculatingState;
+    /*
+     * Only support query once for each calculation,
+     * reset as DIRTY_RATE_STATUS_UNSTARTED after query
+     */
+    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
+                              DIRTY_RATE_STATUS_UNSTARTED);
+
+    return info;
+}
+
 static void reset_dirtyrate_stat(void)
 {
     DirtyStat.total_dirty_samples = 0;
@@ -378,3 +400,26 @@ void *get_dirtyrate_thread(void *arg)
                               DIRTY_RATE_STATUS_MEASURED);
     return NULL;
 }
+
+void qmp_calc_dirty_rate(int64_t calc_time, Error **errp)
+{
+    static struct DirtyRateConfig config;
+    QemuThread thread;
+
+    /*
+     * We don't begin calculating thread only when it's in calculating status.
+     */
+    if (CalculatingState == DIRTY_RATE_STATUS_MEASURING) {
+        return;
+    }
+
+    config.sample_period_seconds = get_sample_page_period(calc_time);
+    config.sample_pages_per_gigabytes = DIRTYRATE_DEFAULT_SAMPLE_PAGES;
+    qemu_thread_create(&thread, "get_dirtyrate", get_dirtyrate_thread,
+                       (void *)&config, QEMU_THREAD_DETACHED);
+}
+
+struct DirtyRateInfo *qmp_query_dirty_rate(Error **errp)
+{
+    return query_dirty_rate_info();
+}
qapi/migration.json +44 -0

47 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#31 lore raw

Re: [PATCH v5 11/12] migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function

Quoted 24 lines · Chuan Zheng

Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function which could be called

---
 migration/dirtyrate.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 qapi/migration.json   | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
migration/dirtyrate.c +8 -0
index 9f52f5f..08c46d3 100644
@@ -62,6 +62,28 @@ static int dirtyrate_set_state(int *state, int old_state, int new_state)
     }
 }
 
+static struct DirtyRateInfo *query_dirty_rate_info(void)
+{
+    int64_t dirty_rate = DirtyStat.dirty_rate;
+    struct DirtyRateInfo *info = g_malloc0(sizeof(DirtyRateInfo));
+
+    if (CalculatingState == DIRTY_RATE_STATUS_MEASURED) {
+        info->dirty_rate = dirty_rate;
+    } else {

migration/dirtyrate.c static int dirtyrate_set_state(int *state, int old_state, int new_state) · line 82

Quoted 10 lines
migration/dirtyrate.c +10 -0
+        info->dirty_rate = -1;
+    }
+
+    info->status = CalculatingState;
+    /*
+     * Only support query once for each calculation,
+     * reset as DIRTY_RATE_STATUS_UNSTARTED after query
+     */
+    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
+                              DIRTY_RATE_STATUS_UNSTARTED);

Is there a reason for this restriction? Removing it would require clarifying the state model, I suppose.

Quoted 9 lines
migration/dirtyrate.c +4 -0
+
+    return info;
+}
+
 static void reset_dirtyrate_stat(void)
 {
     DirtyStat.total_dirty_samples = 0;
@@ -378,3 +400,26 @@ void *get_dirtyrate_thread(void *arg)
                               DIRTY_RATE_STATUS_MEASURED);

migration/dirtyrate.c void *get_dirtyrate_thread(void *arg) · line 410

Quoted 10 lines
migration/dirtyrate.c +8 -0
     return NULL;
 }
+
+void qmp_calc_dirty_rate(int64_t calc_time, Error **errp)
+{
+    static struct DirtyRateConfig config;
+    QemuThread thread;
+
+    /*
+     * We don't begin calculating thread only when it's in calculating status.

"If the dirty rate is already being measured, don't attempt to start."

migration/dirtyrate.c void *get_dirtyrate_thread(void *arg) · line 412

Quoted 2 lines
migration/dirtyrate.c +2 -0
+     */
+    if (CalculatingState == DIRTY_RATE_STATUS_MEASURING) {

Should this return an error to the caller?

migration/dirtyrate.c void *get_dirtyrate_thread(void *arg) · line 416

Quoted 4 lines
migration/dirtyrate.c +4 -0
+        return;
+    }
+
+    config.sample_period_seconds = get_sample_page_period(calc_time);

If the specified sample period is outside the min/max, should an error be returned to the caller?

Quoted 39 lines
migration/dirtyrate.c +9 -0
+    config.sample_pages_per_gigabytes = DIRTYRATE_DEFAULT_SAMPLE_PAGES;
+    qemu_thread_create(&thread, "get_dirtyrate", get_dirtyrate_thread,
+                       (void *)&config, QEMU_THREAD_DETACHED);
+}
+
+struct DirtyRateInfo *qmp_query_dirty_rate(Error **errp)
+{
+    return query_dirty_rate_info();
+}
qapi/migration.json +22 -0
index d640165..826bfd7 100644
@@ -1737,3 +1737,47 @@
 ##
 { 'enum': 'DirtyRateStatus',
   'data': [ 'unstarted', 'measuring', 'measured'] }
+
+##
+# @DirtyRateInfo:
+#
+# Information about current dirty page rate of vm.
+#
+# @dirty-rate: @dirtyrate describing the dirty page rate of vm
+#          in units of MB/s.
+#          If this field return '-1', it means querying is not
+#          start or not complete.
+#
+# @status: status containing dirtyrate query status includes
+#          'unstarted' or 'measuring' or 'measured'
+#
+# Since: 5.2
+#
+##
+{ 'struct': 'DirtyRateInfo',
+  'data': {'dirty-rate': 'int64',
+           'status': 'DirtyRateStatus'} }
+
+##

qapi/migration.json line 1771

Quoted 10 lines
qapi/migration.json +10 -0
+# @calc-dirty-rate:
+#
+# start calculating dirty page rate for vm
+#
+# @calc-time: time in units of second for sample dirty pages
+#
+# Since: 5.2
+#
+# Example:
+#   {"command": "cal-dirty-rate", "data": {"calc-time": 1} }

"cal-dirty-rate" -> "calc-dirty-rate".

Quoted 14 lines
qapi/migration.json +12 -0
+#
+##
+{ 'command': 'calc-dirty-rate', 'data': {'calc-time': 'int64'} }
+
+##
+# @query-dirty-rate:
+#
+# query dirty page rate in units of MB/s for vm
+#
+# Since: 5.2
+##
+{ 'command': 'query-dirty-rate', 'returns': 'DirtyRateInfo' }
Signature · 1 line
1.8.3.1

dme.

Signature · 1 line
I'm going in the out door, I'm doing all right.
#32 lore raw

Re: [PATCH v5 11/12] migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function

Quoted 31 lines · David Edmondson
Quoted 29 lines · Chuan Zheng

Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function which could be called

---
 migration/dirtyrate.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 qapi/migration.json   | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
migration/dirtyrate.c +13 -0
index 9f52f5f..08c46d3 100644
@@ -62,6 +62,28 @@ static int dirtyrate_set_state(int *state, int old_state, int new_state)
     }
 }
 
+static struct DirtyRateInfo *query_dirty_rate_info(void)
+{
+    int64_t dirty_rate = DirtyStat.dirty_rate;
+    struct DirtyRateInfo *info = g_malloc0(sizeof(DirtyRateInfo));
+
+    if (CalculatingState == DIRTY_RATE_STATUS_MEASURED) {
+        info->dirty_rate = dirty_rate;
+    } else {
+        info->dirty_rate = -1;
+    }
+
+    info->status = CalculatingState;
+    /*
Quoted 9 lines

migration/dirtyrate.c static int dirtyrate_set_state(int *state, int old_state, int new_state) · line 82

Quoted 5 lines
migration/dirtyrate.c +5 -0
+     * Only support query once for each calculation,
+     * reset as DIRTY_RATE_STATUS_UNSTARTED after query
+     */
+    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
+                              DIRTY_RATE_STATUS_UNSTARTED);

Is there a reason for this restriction? Removing it would require clarifying the state model, I suppose.

We only support query once for each calculation.
Otherwise, it could always query dirtyrate, but maybe the dirtyrate is calculated long time ago.

Quoted 104 lines
Quoted 9 lines
migration/dirtyrate.c +4 -0
+
+    return info;
+}
+
 static void reset_dirtyrate_stat(void)
 {
     DirtyStat.total_dirty_samples = 0;
@@ -378,3 +400,26 @@ void *get_dirtyrate_thread(void *arg)
                               DIRTY_RATE_STATUS_MEASURED);

migration/dirtyrate.c void *get_dirtyrate_thread(void *arg) · line 410

Quoted 10 lines
migration/dirtyrate.c +8 -0
     return NULL;
 }
+
+void qmp_calc_dirty_rate(int64_t calc_time, Error **errp)
+{
+    static struct DirtyRateConfig config;
+    QemuThread thread;
+
+    /*
+     * We don't begin calculating thread only when it's in calculating status.

"If the dirty rate is already being measured, don't attempt to start."

migration/dirtyrate.c void *get_dirtyrate_thread(void *arg) · line 412

Quoted 2 lines
migration/dirtyrate.c +2 -0
+     */
+    if (CalculatingState == DIRTY_RATE_STATUS_MEASURING) {

Should this return an error to the caller?

migration/dirtyrate.c void *get_dirtyrate_thread(void *arg) · line 416

Quoted 4 lines
migration/dirtyrate.c +4 -0
+        return;
+    }
+
+    config.sample_period_seconds = get_sample_page_period(calc_time);

If the specified sample period is outside the min/max, should an error be returned to the caller?

Quoted 39 lines
migration/dirtyrate.c +9 -0
+    config.sample_pages_per_gigabytes = DIRTYRATE_DEFAULT_SAMPLE_PAGES;
+    qemu_thread_create(&thread, "get_dirtyrate", get_dirtyrate_thread,
+                       (void *)&config, QEMU_THREAD_DETACHED);
+}
+
+struct DirtyRateInfo *qmp_query_dirty_rate(Error **errp)
+{
+    return query_dirty_rate_info();
+}
qapi/migration.json +22 -0
index d640165..826bfd7 100644
@@ -1737,3 +1737,47 @@
 ##
 { 'enum': 'DirtyRateStatus',
   'data': [ 'unstarted', 'measuring', 'measured'] }
+
+##
+# @DirtyRateInfo:
+#
+# Information about current dirty page rate of vm.
+#
+# @dirty-rate: @dirtyrate describing the dirty page rate of vm
+#          in units of MB/s.
+#          If this field return '-1', it means querying is not
+#          start or not complete.
+#
+# @status: status containing dirtyrate query status includes
+#          'unstarted' or 'measuring' or 'measured'
+#
+# Since: 5.2
+#
+##
+{ 'struct': 'DirtyRateInfo',
+  'data': {'dirty-rate': 'int64',
+           'status': 'DirtyRateStatus'} }
+
+##

qapi/migration.json line 1771

Quoted 10 lines
qapi/migration.json +10 -0
+# @calc-dirty-rate:
+#
+# start calculating dirty page rate for vm
+#
+# @calc-time: time in units of second for sample dirty pages
+#
+# Since: 5.2
+#
+# Example:
+#   {"command": "cal-dirty-rate", "data": {"calc-time": 1} }

"cal-dirty-rate" -> "calc-dirty-rate".

Quoted 14 lines
qapi/migration.json +12 -0
+#
+##
+{ 'command': 'calc-dirty-rate', 'data': {'calc-time': 'int64'} }
+
+##
+# @query-dirty-rate:
+#
+# query dirty page rate in units of MB/s for vm
+#
+# Since: 5.2
+##
+{ 'command': 'query-dirty-rate', 'returns': 'DirtyRateInfo' }
Signature · 1 line
1.8.3.1

dme.

#33 lore raw

Re: [PATCH v5 11/12] migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function

Quoted 7 lines · Zheng Chuan
Quoted 7 lines
Quoted 6 lines
migration/dirtyrate.c +6 -0
+    /*
+     * Only support query once for each calculation,
+     * reset as DIRTY_RATE_STATUS_UNSTARTED after query
+     */
+    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
+                              DIRTY_RATE_STATUS_UNSTARTED);
Quoted 6 lines
Quoted 3 lines

Is there a reason for this restriction? Removing it would require clarifying the state model, I suppose.

We only support query once for each calculation.
Otherwise, it could always query dirtyrate, but maybe the dirtyrate is calculated long time ago.

There's nothing in the current interface that prevents this from being the case already - the caller could initiate a 1 second sample, then wait 24 hours to query the result.

Obviously this would generally be regarded as "d'oh - don't do that", but the same argument would apply if the caller is allowed to query the results multiple times.

Perhaps a complete solution would be to include information about the sample period with the result. The caller could then determine whether the sample is of adequate quality (sufficiently recent, taken over a sufficiently long time period) for its' intended use.

dme.

Signature · 1 line
I walk like a building, I never get wet.
#34 lore raw

Re: [PATCH v5 11/12] migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function

Quoted 20 lines · David Edmondson
Quoted 7 lines · Zheng Chuan
Quoted 7 lines
Quoted 6 lines
migration/dirtyrate.c +6 -0
+    /*
+     * Only support query once for each calculation,
+     * reset as DIRTY_RATE_STATUS_UNSTARTED after query
+     */
+    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
+                              DIRTY_RATE_STATUS_UNSTARTED);
Quoted 6 lines
Quoted 3 lines

Is there a reason for this restriction? Removing it would require clarifying the state model, I suppose.

We only support query once for each calculation.
Otherwise, it could always query dirtyrate, but maybe the dirtyrate is calculated long time ago.

There's nothing in the current interface that prevents this from being the case already - the caller could initiate a 1 second sample, then wait 24 hours to query the result.

Quoted 9 lines

Obviously this would generally be regarded as "d'oh - don't do that", but the same argument would apply if the caller is allowed to query the results multiple times.

Perhaps a complete solution would be to include information about the sample period with the result. The caller could then determine whether the sample is of adequate quality (sufficiently recent, taken over a sufficiently long time period) for its' intended use.

You mean add timestamp when i calculate?
Actually, I do not want make it complicate for qemu code, maybe it could be left for user to implement both two qmp commands like in libvirt-api.

On the other hand, it really bother me that we need to reset calculating state to make sure the state model could be restart in next calculation.

For now, i put it after query_dirty_rate_info is finished as you see, it should not be a good idea:(

Maybe it is better to initialize at the beginning of qmp_calc_dirty_rate().

Quoted 2 lines

dme.

#35 lore raw

Re: [PATCH v5 11/12] migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function

Quoted 25 lines · Zheng Chuan
Quoted 24 lines · David Edmondson
Quoted 7 lines · Zheng Chuan
Quoted 7 lines
Quoted 6 lines
migration/dirtyrate.c +6 -0
+    /*
+     * Only support query once for each calculation,
+     * reset as DIRTY_RATE_STATUS_UNSTARTED after query
+     */
+    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
+                              DIRTY_RATE_STATUS_UNSTARTED);
Quoted 6 lines
Quoted 3 lines

Is there a reason for this restriction? Removing it would require clarifying the state model, I suppose.

We only support query once for each calculation.
Otherwise, it could always query dirtyrate, but maybe the dirtyrate is calculated long time ago.

There's nothing in the current interface that prevents this from being the case already - the caller could initiate a 1 second sample, then wait 24 hours to query the result.

Obviously this would generally be regarded as "d'oh - don't do that", but the same argument would apply if the caller is allowed to query the results multiple times.

Quoted 6 lines
Quoted 5 lines

Perhaps a complete solution would be to include information about the sample period with the result. The caller could then determine whether the sample is of adequate quality (sufficiently recent, taken over a sufficiently long time period) for its' intended use.

You mean add timestamp when i calculate?

You already have a timestamp, though I'm not sure if it is one that is appropriate to report to a user.

I was thinking that you would include both the start time and duration of the sample in the output of the query-dirty-rate QMP command, as well as the dirty rate itself. That way the caller can make a decision about whether the data is useful.

Quoted 3 lines

Actually, I do not want make it complicate for qemu code, maybe it could be left for user to implement both two qmp commands like in libvirt-api.

Sorry, I didn't understand this comment.

Quoted 9 lines

On the other hand, it really bother me that we need to reset calculating state to make sure the state model could be restart in next calculation.

For now, i put it after query_dirty_rate_info is finished as you see, it should not be a good idea:(

Maybe it is better to initialize at the beginning of qmp_calc_dirty_rate().

Quoted 2 lines

dme.

dme.

Signature · 1 line
Another lonely day, no one here but me-o.
#36 lore raw

Re: [PATCH v5 11/12] migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function

Quoted 34 lines · David Edmondson
Quoted 25 lines · Zheng Chuan
Quoted 24 lines · David Edmondson
Quoted 7 lines · Zheng Chuan
Quoted 7 lines
Quoted 6 lines
migration/dirtyrate.c +6 -0
+    /*
+     * Only support query once for each calculation,
+     * reset as DIRTY_RATE_STATUS_UNSTARTED after query
+     */
+    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
+                              DIRTY_RATE_STATUS_UNSTARTED);
Quoted 6 lines
Quoted 3 lines

Is there a reason for this restriction? Removing it would require clarifying the state model, I suppose.

We only support query once for each calculation.
Otherwise, it could always query dirtyrate, but maybe the dirtyrate is calculated long time ago.

There's nothing in the current interface that prevents this from being the case already - the caller could initiate a 1 second sample, then wait 24 hours to query the result.

Obviously this would generally be regarded as "d'oh - don't do that", but the same argument would apply if the caller is allowed to query the results multiple times.

Quoted 6 lines
Quoted 5 lines

Perhaps a complete solution would be to include information about the sample period with the result. The caller could then determine whether the sample is of adequate quality (sufficiently recent, taken over a sufficiently long time period) for its' intended use.

You mean add timestamp when i calculate?

Quoted 8 lines

You already have a timestamp, though I'm not sure if it is one that is appropriate to report to a user.

I was thinking that you would include both the start time and duration of the sample in the output of the query-dirty-rate QMP command, as well as the dirty rate itself. That way the caller can make a decision about whether the data is useful.

OK, i understand.
I may add it like this:
+##
+{ 'struct': 'DirtyRateInfo',
+ 'data': {'dirty-rate': 'int64',

+           'status': 'DirtyRateStatus',
+           'start-timestamp': 'int64',
+           'calc-time': 'int64'} }

+
+##
the stat-timestamp would be initial_time which gets from qemu_clock_get_ms(QEMU_CLOCK_REALTIME) at the beginning of calculation while calc_time is time-duration in microsecond.

But i reconsider that, it maybe still need to reset the CalculatingState as DIRTY_RATE_STATUS_UNSTARTED here?

Initialization like:
void qmp_calc_dirty_rate(int64_t calc_time, Error **errp) {
XXXX

    if (CalculatingState == DIRTY_RATE_STATUS_MEASURING) {
        return;
    }


    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
                              DIRTY_RATE_STATUS_UNSTARTED);
    XXXX
}

It could not prevent concurrent scene which may lead to disorder state:(

Quoted 18 lines
Quoted 3 lines

Actually, I do not want make it complicate for qemu code, maybe it could be left for user to implement both two qmp commands like in libvirt-api.

Sorry, I didn't understand this comment.

Quoted 9 lines

On the other hand, it really bother me that we need to reset calculating state to make sure the state model could be restart in next calculation.

For now, i put it after query_dirty_rate_info is finished as you see, it should not be a good idea:(

Maybe it is better to initialize at the beginning of qmp_calc_dirty_rate().

Quoted 2 lines

dme.

dme.

#37 lore raw

Re: [PATCH v5 11/12] migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function

Quoted 53 lines · Zheng Chuan
Quoted 34 lines · David Edmondson
Quoted 25 lines · Zheng Chuan
Quoted 24 lines · David Edmondson
Quoted 7 lines · Zheng Chuan
Quoted 7 lines
Quoted 6 lines
(quoted diff) +6 -0
+    /*
+     * Only support query once for each calculation,
+     * reset as DIRTY_RATE_STATUS_UNSTARTED after query
+     */
+    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
+                              DIRTY_RATE_STATUS_UNSTARTED);
Quoted 6 lines
Quoted 3 lines

Is there a reason for this restriction? Removing it would require clarifying the state model, I suppose.

We only support query once for each calculation.
Otherwise, it could always query dirtyrate, but maybe the dirtyrate is calculated long time ago.

There's nothing in the current interface that prevents this from being the case already - the caller could initiate a 1 second sample, then wait 24 hours to query the result.

Obviously this would generally be regarded as "d'oh - don't do that", but the same argument would apply if the caller is allowed to query the results multiple times.

Quoted 6 lines
Quoted 5 lines

Perhaps a complete solution would be to include information about the sample period with the result. The caller could then determine whether the sample is of adequate quality (sufficiently recent, taken over a sufficiently long time period) for its' intended use.

You mean add timestamp when i calculate?

Quoted 8 lines

You already have a timestamp, though I'm not sure if it is one that is appropriate to report to a user.

I was thinking that you would include both the start time and duration of the sample in the output of the query-dirty-rate QMP command, as well as the dirty rate itself. That way the caller can make a decision about whether the data is useful.

OK, i understand.
I may add it like this:

(quoted diff) +8 -0
+##
+{ 'struct': 'DirtyRateInfo',
+  'data': {'dirty-rate': 'int64',
+           'status': 'DirtyRateStatus',
+           'start-timestamp': 'int64',
+           'calc-time': 'int64'} }
+
+##
Quoted 2 lines

the stat-timestamp would be initial_time which gets from qemu_clock_get_ms(QEMU_CLOCK_REALTIME) at the beginning of calculation while calc_time is time-duration in microsecond.

The calc-time reported here should be in the same units as when it is specified in calc-dirty-rate (seconds for both seems fine).

I suspect that providing the start-timestamp in seconds would also be fine - it's not obvious that knowing the value in milliseconds adds much value.

Quoted 18 lines

But i reconsider that, it maybe still need to reset the CalculatingState as DIRTY_RATE_STATUS_UNSTARTED here?

Initialization like:
void qmp_calc_dirty_rate(int64_t calc_time, Error **errp)

{
   XXXX

    if (CalculatingState == DIRTY_RATE_STATUS_MEASURING) {
        return;
    }


    (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
                              DIRTY_RATE_STATUS_UNSTARTED);
    XXXX
}
Quoted 1 line

It could not prevent concurrent scene which may lead to disorder state:(

It should be possible to initiate measurement when the state is either UNSTARTED or MEASURED - only MEASURING should rule it out.

Quoted 13 lines
Quoted 13 lines
Quoted 5 lines

Actually, I do not want make it complicate for qemu code, maybe it could be left for user to implement both two qmp commands like in libvirt-api.

Sorry, I didn't understand this comment.

Quoted 5 lines

On the other hand, it really bother me that we need to reset calculating state to make sure the state model could be restart in next calculation.

For now, i put it after query_dirty_rate_info is finished as you see, it should not be a good idea:(

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

Maybe it is better to initialize at the beginning of qmp_calc_dirty_rate().

Quoted 2 lines

dme.

dme.

dme.

Signature · 1 line
Everyone I know, goes away in the end.
#38 lore raw

PATCH v5 12/12 migration/dirtyrate: Add trace_calls to make it easier to debug

In reply to Chuan Zheng

Add trace_calls to make it easier to debug

---
 migration/dirtyrate.c  | 7 +++++++
 migration/trace-events | 8 ++++++++
 2 files changed, 15 insertions(+)
migration/dirtyrate.c +7 -0
index 08c46d3..3513ef3 100644
@@ -23,6 +23,7 @@
 #include "qapi/qapi-commands-migration.h"
 #include "migration.h"
 #include "ram.h"
+#include "trace.h"
 #include "dirtyrate.h"
 
 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
@@ -55,6 +56,7 @@ static int64_t get_sample_page_period(int64_t sec)
 static int dirtyrate_set_state(int *state, int old_state, int new_state)
 {
     assert(new_state < DIRTY_RATE_STATUS__MAX);
+    trace_dirtyrate_set_state(DirtyRateStatus_str(new_state));
     if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
         return 0;
     } else {
@@ -78,6 +80,7 @@ static struct DirtyRateInfo *query_dirty_rate_info(void)
      * Only support query once for each calculation,
      * reset as DIRTY_RATE_STATUS_UNSTARTED after query
      */
+    trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState));
     (void)dirtyrate_set_state(&CalculatingState, CalculatingState,
                               DIRTY_RATE_STATUS_UNSTARTED);
 
@@ -129,6 +132,7 @@ static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
 
     crc = crc32(0, iov_array.iov_base, iov_array.iov_len);
 
+    trace_get_ramblock_vfn_hash(info->idstr, vfn, crc);
     return crc;
 }
 
@@ -246,6 +250,7 @@ static int skip_sample_ramblock(RAMBlock *block)
      * want to sample.
      */
     if (ramblock_size < MIN_RAMBLOCK_SIZE) {
+        trace_skip_sample_ramblock(block->idstr, ramblock_size);
         return -1;
     }
 
@@ -292,6 +297,7 @@ static int calc_page_dirty_rate(struct RamblockDirtyInfo *info)
     for (i = 0; i < info->sample_pages_count; i++) {
         crc = get_ramblock_vfn_hash(info, info->sample_page_vfn[i]);
         if (crc != info->hash_result[i]) {
+            trace_calc_page_dirty_rate(info->idstr, crc, info->hash_result[i]);
             info->sample_dirty_count++;
         }
     }
@@ -317,6 +323,7 @@ static bool find_page_matched(RAMBlock *block, struct RamblockDirtyInfo *infos,
     if (infos[i].ramblock_addr != qemu_ram_get_host_addr(block) ||
         infos[i].ramblock_pages !=
             (qemu_ram_get_used_length(block) >> DIRTYRATE_PAGE_SHIFT_KB)) {
+        trace_find_page_matched(block->idstr);
         return false;
     }
 
migration/trace-events +8 -0
index 4ab0a50..34569b9 100644
@@ -312,3 +312,11 @@ dirty_bitmap_load_bits_zeroes(void) ""
 dirty_bitmap_load_header(uint32_t flags) "flags 0x%x"
 dirty_bitmap_load_enter(void) ""
 dirty_bitmap_load_success(void) ""
+
+# dirtyrate.c
+dirtyrate_set_state(const char *new_state) "new state %s"
+query_dirty_rate_info(const char *new_state) "current state %s"
+get_ramblock_vfn_hash(const char *idstr, uint64_t vfn, uint32_t crc) "ramblock name: %s, vfn: %"PRIu64 ", crc: %" PRIu32
+calc_page_dirty_rate(const char *idstr, uint32_t new_crc, uint32_t old_crc) "ramblock name: %s, new crc: %" PRIu32 ", old crc: %" PRIu32
+skip_sample_ramblock(const char *idstr, int64_t ramblock_size) "ramblock name: %s, ramblock size: %" PRIu64
+find_page_matched(const char *idstr) "ramblock %s addr or size changed"
Signature · 1 line
1.8.3.1
#39 lore raw
In reply to Chuan Zheng
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [PATCH v5 00/12] *** A Method for evaluating dirty page rate ***

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu

   dd8014e..df82aa7  master     -> master
 - [tag update]      patchew/[email protected] -> patchew/[email protected]
 - [tag update]      patchew/[email protected] -> patchew/[email protected]
 - [tag update]      patchew/[email protected] -> patchew/[email protected]
 - [tag update]      patchew/[email protected] -> patchew/[email protected]
 - [tag update]      patchew/[email protected] -> patchew/[email protected]
 - [tag update]      patchew/[email protected] -> patchew/[email protected]
 - [tag update]      patchew/[email protected] -> patchew/[email protected]
 - [tag update]      patchew/[email protected] -> patchew/[email protected]
 * [new tag]         patchew/[email protected] -> patchew/[email protected]
 * [new tag]         patchew/[email protected] -> patchew/[email protected]
 * [new tag]         patchew/[email protected] -> patchew/[email protected]
 * [new tag]         patchew/[email protected] -> patchew/[email protected]
 * [new tag]         patchew/[email protected] -> patchew/[email protected]
 * [new tag]         patchew/[email protected] -> patchew/[email protected]
 * [new tag]         patchew/[email protected] -> patchew/[email protected]

Switched to a new branch 'test'
e422c48 migration/dirtyrate: Add trace_calls to make it easier to debug
d544358 migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function b245e3e migration/dirtyrate: Implement calculate_dirtyrate() function
f634d60 migration/dirtyrate: Implement get_sample_page_period() and block_sample_page_period() 039884c migration/dirtyrate: skip sampling ramblock with size below MIN_RAMBLOCK_SIZE b62b0f3 migration/dirtyrate: Compare page hash results for recorded sampled page a0c77f6 migration/dirtyrate: Record hash results for each sampled page
7e4c941 migration/dirtyrate: move RAMBLOCK_FOREACH_MIGRATABLE into ram.h 5bd531b migration/dirtyrate: Add dirtyrate statistics series functions
af4f569 migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info 9d2abe8 migration/dirtyrate: add DirtyRateStatus to denote calculation status 9da875e migration/dirtyrate: setup up query-dirtyrate framwork

=== OUTPUT BEGIN ===
1/12 Checking commit 9da875ef425e (migration/dirtyrate: setup up query-dirtyrate framwork)
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #15:
new file mode 100644

total: 0 errors, 1 warnings, 78 lines checked

Patch 1/12 has style problems, please review. If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
2/12 Checking commit 9d2abe8b480a (migration/dirtyrate: add DirtyRateStatus to denote calculation status) 3/12 Checking commit af4f56975987 (migration/dirtyrate: Add RamlockDirtyInfo to store sampled page info) 4/12 Checking commit 5bd531bb00ce (migration/dirtyrate: Add dirtyrate statistics series functions) 5/12 Checking commit 7e4c941b32bc (migration/dirtyrate: move RAMBLOCK_FOREACH_MIGRATABLE into ram.h)
ERROR: Macros with multiple statements should be enclosed in a do - while loop
#62: FILE: migration/ram.h:42:
+#define RAMBLOCK_FOREACH_NOT_IGNORED(block) \
+ INTERNAL_RAMBLOCK_FOREACH(block) \
+ if (ramblock_is_ignored(block)) {} else

ERROR: trailing statements should be on next line
#64: FILE: migration/ram.h:44:
+ if (ramblock_is_ignored(block)) {} else

ERROR: Macros with multiple statements should be enclosed in a do - while loop #66: FILE: migration/ram.h:46:
+#define RAMBLOCK_FOREACH_MIGRATABLE(block) \
+ INTERNAL_RAMBLOCK_FOREACH(block) \
+ if (!qemu_ram_is_migratable(block)) {} else

ERROR: trailing statements should be on next line
#68: FILE: migration/ram.h:48:
+ if (!qemu_ram_is_migratable(block)) {} else

ERROR: braces {} are necessary for all arms of this statement #68: FILE: migration/ram.h:48:
+ if (!qemu_ram_is_migratable(block)) {} else
[...]
+ if (!qemu_ram_is_migratable(block)) {} else
[...]

total: 5 errors, 0 warnings, 45 lines checked

Patch 5/12 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

6/12 Checking commit a0c77f6034fc (migration/dirtyrate: Record hash results for each sampled page) 7/12 Checking commit b62b0f3c7033 (migration/dirtyrate: Compare page hash results for recorded sampled page) 8/12 Checking commit 039884cf410e (migration/dirtyrate: skip sampling ramblock with size below MIN_RAMBLOCK_SIZE) 9/12 Checking commit f634d60df875 (migration/dirtyrate: Implement get_sample_page_period() and block_sample_page_period()) 10/12 Checking commit b245e3e9cd0d (migration/dirtyrate: Implement calculate_dirtyrate() function) 11/12 Checking commit d544358ff2b0 (migration/dirtyrate: Implement qmp_cal_dirty_rate()/qmp_get_dirty_rate() function) 12/12 Checking commit e422c48502c8 (migration/dirtyrate: Add trace_calls to make it easier to debug) === OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]