2 * Dirtyrate implement code
4 * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO.,LTD.
7 * Chuan Zheng <zhengchuan@huawei.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/error-report.h"
16 #include "qapi/error.h"
18 #include "exec/ramblock.h"
19 #include "exec/target_page.h"
20 #include "exec/ram_addr.h"
21 #include "qemu/rcu_queue.h"
22 #include "qemu/main-loop.h"
23 #include "qapi/qapi-commands-migration.h"
26 #include "dirtyrate.h"
27 #include "monitor/hmp.h"
28 #include "monitor/monitor.h"
29 #include "qapi/qmp/qdict.h"
30 #include "sysemu/kvm.h"
31 #include "sysemu/runstate.h"
32 #include "exec/memory.h"
33 #include "qemu/xxhash.h"
36 * total_dirty_pages is procted by BQL and is used
37 * to stat dirty pages during the period of two
38 * memory_global_dirty_log_sync
40 uint64_t total_dirty_pages
;
42 typedef struct DirtyPageRecord
{
47 static int CalculatingState
= DIRTY_RATE_STATUS_UNSTARTED
;
48 static struct DirtyRateStat DirtyStat
;
49 static DirtyRateMeasureMode dirtyrate_mode
=
50 DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
;
52 static int64_t dirty_stat_wait(int64_t msec
, int64_t initial_time
)
56 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
57 if ((current_time
- initial_time
) >= msec
) {
58 msec
= current_time
- initial_time
;
60 g_usleep((msec
+ initial_time
- current_time
) * 1000);
66 static inline void record_dirtypages(DirtyPageRecord
*dirty_pages
,
67 CPUState
*cpu
, bool start
)
70 dirty_pages
[cpu
->cpu_index
].start_pages
= cpu
->dirty_pages
;
72 dirty_pages
[cpu
->cpu_index
].end_pages
= cpu
->dirty_pages
;
76 static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages
,
79 uint64_t increased_dirty_pages
=
80 dirty_pages
.end_pages
- dirty_pages
.start_pages
;
81 uint64_t memory_size_MiB
= qemu_target_pages_to_MiB(increased_dirty_pages
);
83 return memory_size_MiB
* 1000 / calc_time_ms
;
86 void global_dirty_log_change(unsigned int flag
, bool start
)
88 qemu_mutex_lock_iothread();
90 memory_global_dirty_log_start(flag
);
92 memory_global_dirty_log_stop(flag
);
94 qemu_mutex_unlock_iothread();
98 * global_dirty_log_sync
99 * 1. sync dirty log from kvm
100 * 2. stop dirty tracking if needed.
102 static void global_dirty_log_sync(unsigned int flag
, bool one_shot
)
104 qemu_mutex_lock_iothread();
105 memory_global_dirty_log_sync();
107 memory_global_dirty_log_stop(flag
);
109 qemu_mutex_unlock_iothread();
112 static DirtyPageRecord
*vcpu_dirty_stat_alloc(VcpuStat
*stat
)
122 stat
->rates
= g_new0(DirtyRateVcpu
, nvcpu
);
124 return g_new0(DirtyPageRecord
, nvcpu
);
127 static void vcpu_dirty_stat_collect(VcpuStat
*stat
,
128 DirtyPageRecord
*records
,
134 record_dirtypages(records
, cpu
, start
);
138 int64_t vcpu_calculate_dirtyrate(int64_t calc_time_ms
,
143 DirtyPageRecord
*records
;
144 int64_t init_time_ms
;
151 init_time_ms
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
153 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock
) {
154 gen_id
= cpu_list_generation_id_get();
155 records
= vcpu_dirty_stat_alloc(stat
);
156 vcpu_dirty_stat_collect(stat
, records
, true);
159 duration
= dirty_stat_wait(calc_time_ms
, init_time_ms
);
161 global_dirty_log_sync(flag
, one_shot
);
163 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock
) {
164 if (gen_id
!= cpu_list_generation_id_get()) {
170 vcpu_dirty_stat_collect(stat
, records
, false);
173 for (i
= 0; i
< stat
->nvcpu
; i
++) {
174 dirtyrate
= do_calculate_dirtyrate(records
[i
], duration
);
176 stat
->rates
[i
].id
= i
;
177 stat
->rates
[i
].dirty_rate
= dirtyrate
;
179 trace_dirtyrate_do_calculate_vcpu(i
, dirtyrate
);
187 static bool is_sample_period_valid(int64_t sec
)
189 if (sec
< MIN_FETCH_DIRTYRATE_TIME_SEC
||
190 sec
> MAX_FETCH_DIRTYRATE_TIME_SEC
) {
197 static bool is_sample_pages_valid(int64_t pages
)
199 return pages
>= MIN_SAMPLE_PAGE_COUNT
&&
200 pages
<= MAX_SAMPLE_PAGE_COUNT
;
203 static int dirtyrate_set_state(int *state
, int old_state
, int new_state
)
205 assert(new_state
< DIRTY_RATE_STATUS__MAX
);
206 trace_dirtyrate_set_state(DirtyRateStatus_str(new_state
));
207 if (qatomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
214 static struct DirtyRateInfo
*query_dirty_rate_info(void)
217 int64_t dirty_rate
= DirtyStat
.dirty_rate
;
218 struct DirtyRateInfo
*info
= g_new0(DirtyRateInfo
, 1);
219 DirtyRateVcpuList
*head
= NULL
, **tail
= &head
;
221 info
->status
= CalculatingState
;
222 info
->start_time
= DirtyStat
.start_time
;
223 info
->calc_time
= DirtyStat
.calc_time
;
224 info
->sample_pages
= DirtyStat
.sample_pages
;
225 info
->mode
= dirtyrate_mode
;
227 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURED
) {
228 info
->has_dirty_rate
= true;
229 info
->dirty_rate
= dirty_rate
;
231 if (dirtyrate_mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_RING
) {
233 * set sample_pages with 0 to indicate page sampling
236 info
->sample_pages
= 0;
237 info
->has_vcpu_dirty_rate
= true;
238 for (i
= 0; i
< DirtyStat
.dirty_ring
.nvcpu
; i
++) {
239 DirtyRateVcpu
*rate
= g_new0(DirtyRateVcpu
, 1);
240 rate
->id
= DirtyStat
.dirty_ring
.rates
[i
].id
;
241 rate
->dirty_rate
= DirtyStat
.dirty_ring
.rates
[i
].dirty_rate
;
242 QAPI_LIST_APPEND(tail
, rate
);
244 info
->vcpu_dirty_rate
= head
;
247 if (dirtyrate_mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP
) {
248 info
->sample_pages
= 0;
252 trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState
));
257 static void init_dirtyrate_stat(int64_t start_time
,
258 struct DirtyRateConfig config
)
260 DirtyStat
.dirty_rate
= -1;
261 DirtyStat
.start_time
= start_time
;
262 DirtyStat
.calc_time
= config
.sample_period_seconds
;
263 DirtyStat
.sample_pages
= config
.sample_pages_per_gigabytes
;
265 switch (config
.mode
) {
266 case DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
:
267 DirtyStat
.page_sampling
.total_dirty_samples
= 0;
268 DirtyStat
.page_sampling
.total_sample_count
= 0;
269 DirtyStat
.page_sampling
.total_block_mem_MB
= 0;
271 case DIRTY_RATE_MEASURE_MODE_DIRTY_RING
:
272 DirtyStat
.dirty_ring
.nvcpu
= -1;
273 DirtyStat
.dirty_ring
.rates
= NULL
;
280 static void cleanup_dirtyrate_stat(struct DirtyRateConfig config
)
282 /* last calc-dirty-rate qmp use dirty ring mode */
283 if (dirtyrate_mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_RING
) {
284 free(DirtyStat
.dirty_ring
.rates
);
285 DirtyStat
.dirty_ring
.rates
= NULL
;
289 static void update_dirtyrate_stat(struct RamblockDirtyInfo
*info
)
291 DirtyStat
.page_sampling
.total_dirty_samples
+= info
->sample_dirty_count
;
292 DirtyStat
.page_sampling
.total_sample_count
+= info
->sample_pages_count
;
293 /* size of total pages in MB */
294 DirtyStat
.page_sampling
.total_block_mem_MB
+=
295 qemu_target_pages_to_MiB(info
->ramblock_pages
);
298 static void update_dirtyrate(uint64_t msec
)
301 uint64_t total_dirty_samples
= DirtyStat
.page_sampling
.total_dirty_samples
;
302 uint64_t total_sample_count
= DirtyStat
.page_sampling
.total_sample_count
;
303 uint64_t total_block_mem_MB
= DirtyStat
.page_sampling
.total_block_mem_MB
;
305 dirtyrate
= total_dirty_samples
* total_block_mem_MB
*
306 1000 / (total_sample_count
* msec
);
308 DirtyStat
.dirty_rate
= dirtyrate
;
312 * Compute hash of a single page of size TARGET_PAGE_SIZE.
314 static uint32_t compute_page_hash(void *ptr
)
316 size_t page_size
= qemu_target_page_size();
318 uint64_t v1
, v2
, v3
, v4
;
320 const uint64_t *p
= ptr
;
322 v1
= QEMU_XXHASH_SEED
+ XXH_PRIME64_1
+ XXH_PRIME64_2
;
323 v2
= QEMU_XXHASH_SEED
+ XXH_PRIME64_2
;
324 v3
= QEMU_XXHASH_SEED
+ 0;
325 v4
= QEMU_XXHASH_SEED
- XXH_PRIME64_1
;
326 for (i
= 0; i
< page_size
/ 8; i
+= 4) {
327 v1
= XXH64_round(v1
, p
[i
+ 0]);
328 v2
= XXH64_round(v2
, p
[i
+ 1]);
329 v3
= XXH64_round(v3
, p
[i
+ 2]);
330 v4
= XXH64_round(v4
, p
[i
+ 3]);
332 res
= XXH64_mergerounds(v1
, v2
, v3
, v4
);
334 res
= XXH64_avalanche(res
);
335 return (uint32_t)(res
& UINT32_MAX
);
340 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
341 * in ramblock, which starts from ramblock base address.
343 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo
*info
,
348 hash
= compute_page_hash(info
->ramblock_addr
+
349 vfn
* qemu_target_page_size());
351 trace_get_ramblock_vfn_hash(info
->idstr
, vfn
, hash
);
355 static bool save_ramblock_hash(struct RamblockDirtyInfo
*info
)
357 unsigned int sample_pages_count
;
361 sample_pages_count
= info
->sample_pages_count
;
363 /* ramblock size less than one page, return success to skip this ramblock */
364 if (unlikely(info
->ramblock_pages
== 0 || sample_pages_count
== 0)) {
368 info
->hash_result
= g_try_malloc0_n(sample_pages_count
,
370 if (!info
->hash_result
) {
374 info
->sample_page_vfn
= g_try_malloc0_n(sample_pages_count
,
376 if (!info
->sample_page_vfn
) {
377 g_free(info
->hash_result
);
382 for (i
= 0; i
< sample_pages_count
; i
++) {
383 info
->sample_page_vfn
[i
] = g_rand_int_range(rand
, 0,
384 info
->ramblock_pages
- 1);
385 info
->hash_result
[i
] = get_ramblock_vfn_hash(info
,
386 info
->sample_page_vfn
[i
]);
393 static void get_ramblock_dirty_info(RAMBlock
*block
,
394 struct RamblockDirtyInfo
*info
,
395 struct DirtyRateConfig
*config
)
397 uint64_t sample_pages_per_gigabytes
= config
->sample_pages_per_gigabytes
;
399 /* Right shift 30 bits to calc ramblock size in GB */
400 info
->sample_pages_count
= (qemu_ram_get_used_length(block
) *
401 sample_pages_per_gigabytes
) >> 30;
402 /* Right shift TARGET_PAGE_BITS to calc page count */
403 info
->ramblock_pages
= qemu_ram_get_used_length(block
) >>
405 info
->ramblock_addr
= qemu_ram_get_host_addr(block
);
406 strcpy(info
->idstr
, qemu_ram_get_idstr(block
));
409 static void free_ramblock_dirty_info(struct RamblockDirtyInfo
*infos
, int count
)
417 for (i
= 0; i
< count
; i
++) {
418 g_free(infos
[i
].sample_page_vfn
);
419 g_free(infos
[i
].hash_result
);
424 static bool skip_sample_ramblock(RAMBlock
*block
)
427 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
429 if (qemu_ram_get_used_length(block
) < (MIN_RAMBLOCK_SIZE
<< 10)) {
430 trace_skip_sample_ramblock(block
->idstr
,
431 qemu_ram_get_used_length(block
));
438 static bool record_ramblock_hash_info(struct RamblockDirtyInfo
**block_dinfo
,
439 struct DirtyRateConfig config
,
442 struct RamblockDirtyInfo
*info
= NULL
;
443 struct RamblockDirtyInfo
*dinfo
= NULL
;
444 RAMBlock
*block
= NULL
;
449 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
450 if (skip_sample_ramblock(block
)) {
456 dinfo
= g_try_malloc0_n(total_count
, sizeof(struct RamblockDirtyInfo
));
461 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
462 if (skip_sample_ramblock(block
)) {
465 if (index
>= total_count
) {
468 info
= &dinfo
[index
];
469 get_ramblock_dirty_info(block
, info
, &config
);
470 if (!save_ramblock_hash(info
)) {
478 *block_count
= index
;
479 *block_dinfo
= dinfo
;
483 static void calc_page_dirty_rate(struct RamblockDirtyInfo
*info
)
488 for (i
= 0; i
< info
->sample_pages_count
; i
++) {
489 hash
= get_ramblock_vfn_hash(info
, info
->sample_page_vfn
[i
]);
490 if (hash
!= info
->hash_result
[i
]) {
491 trace_calc_page_dirty_rate(info
->idstr
, hash
, info
->hash_result
[i
]);
492 info
->sample_dirty_count
++;
497 static struct RamblockDirtyInfo
*
498 find_block_matched(RAMBlock
*block
, int count
,
499 struct RamblockDirtyInfo
*infos
)
503 for (i
= 0; i
< count
; i
++) {
504 if (!strcmp(infos
[i
].idstr
, qemu_ram_get_idstr(block
))) {
513 if (infos
[i
].ramblock_addr
!= qemu_ram_get_host_addr(block
) ||
514 infos
[i
].ramblock_pages
!=
515 (qemu_ram_get_used_length(block
) >> TARGET_PAGE_BITS
)) {
516 trace_find_page_matched(block
->idstr
);
523 static bool compare_page_hash_info(struct RamblockDirtyInfo
*info
,
526 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
527 RAMBlock
*block
= NULL
;
529 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
530 if (skip_sample_ramblock(block
)) {
533 block_dinfo
= find_block_matched(block
, block_count
, info
);
534 if (block_dinfo
== NULL
) {
537 calc_page_dirty_rate(block_dinfo
);
538 update_dirtyrate_stat(block_dinfo
);
541 if (DirtyStat
.page_sampling
.total_sample_count
== 0) {
548 static inline void record_dirtypages_bitmap(DirtyPageRecord
*dirty_pages
,
552 dirty_pages
->start_pages
= total_dirty_pages
;
554 dirty_pages
->end_pages
= total_dirty_pages
;
558 static inline void dirtyrate_manual_reset_protect(void)
560 RAMBlock
*block
= NULL
;
562 WITH_RCU_READ_LOCK_GUARD() {
563 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
564 memory_region_clear_dirty_bitmap(block
->mr
, 0,
570 static void calculate_dirtyrate_dirty_bitmap(struct DirtyRateConfig config
)
574 DirtyPageRecord dirty_pages
;
576 qemu_mutex_lock_iothread();
577 memory_global_dirty_log_start(GLOBAL_DIRTY_DIRTY_RATE
);
580 * 1'round of log sync may return all 1 bits with
581 * KVM_DIRTY_LOG_INITIALLY_SET enable
582 * skip it unconditionally and start dirty tracking
583 * from 2'round of log sync
585 memory_global_dirty_log_sync();
588 * reset page protect manually and unconditionally.
589 * this make sure kvm dirty log be cleared if
590 * KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE cap is enabled.
592 dirtyrate_manual_reset_protect();
593 qemu_mutex_unlock_iothread();
595 record_dirtypages_bitmap(&dirty_pages
, true);
597 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
598 DirtyStat
.start_time
= start_time
/ 1000;
600 msec
= config
.sample_period_seconds
* 1000;
601 msec
= dirty_stat_wait(msec
, start_time
);
602 DirtyStat
.calc_time
= msec
/ 1000;
606 * 1. fetch dirty bitmap from kvm
607 * 2. stop dirty tracking
609 global_dirty_log_sync(GLOBAL_DIRTY_DIRTY_RATE
, true);
611 record_dirtypages_bitmap(&dirty_pages
, false);
613 DirtyStat
.dirty_rate
= do_calculate_dirtyrate(dirty_pages
, msec
);
616 static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config
)
619 uint64_t dirtyrate
= 0;
620 uint64_t dirtyrate_sum
= 0;
624 global_dirty_log_change(GLOBAL_DIRTY_DIRTY_RATE
, true);
626 DirtyStat
.start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
628 /* calculate vcpu dirtyrate */
629 duration
= vcpu_calculate_dirtyrate(config
.sample_period_seconds
* 1000,
630 &DirtyStat
.dirty_ring
,
631 GLOBAL_DIRTY_DIRTY_RATE
,
634 DirtyStat
.calc_time
= duration
/ 1000;
636 /* calculate vm dirtyrate */
637 for (i
= 0; i
< DirtyStat
.dirty_ring
.nvcpu
; i
++) {
638 dirtyrate
= DirtyStat
.dirty_ring
.rates
[i
].dirty_rate
;
639 DirtyStat
.dirty_ring
.rates
[i
].dirty_rate
= dirtyrate
;
640 dirtyrate_sum
+= dirtyrate
;
643 DirtyStat
.dirty_rate
= dirtyrate_sum
;
646 static void calculate_dirtyrate_sample_vm(struct DirtyRateConfig config
)
648 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
651 int64_t initial_time
;
654 initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
655 if (!record_ramblock_hash_info(&block_dinfo
, config
, &block_count
)) {
660 msec
= config
.sample_period_seconds
* 1000;
661 msec
= dirty_stat_wait(msec
, initial_time
);
662 DirtyStat
.start_time
= initial_time
/ 1000;
663 DirtyStat
.calc_time
= msec
/ 1000;
666 if (!compare_page_hash_info(block_dinfo
, block_count
)) {
670 update_dirtyrate(msec
);
674 free_ramblock_dirty_info(block_dinfo
, block_count
);
677 static void calculate_dirtyrate(struct DirtyRateConfig config
)
679 if (config
.mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP
) {
680 calculate_dirtyrate_dirty_bitmap(config
);
681 } else if (config
.mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_RING
) {
682 calculate_dirtyrate_dirty_ring(config
);
684 calculate_dirtyrate_sample_vm(config
);
687 trace_dirtyrate_calculate(DirtyStat
.dirty_rate
);
690 void *get_dirtyrate_thread(void *arg
)
692 struct DirtyRateConfig config
= *(struct DirtyRateConfig
*)arg
;
694 rcu_register_thread();
696 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_UNSTARTED
,
697 DIRTY_RATE_STATUS_MEASURING
);
699 error_report("change dirtyrate state failed.");
703 calculate_dirtyrate(config
);
705 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_MEASURING
,
706 DIRTY_RATE_STATUS_MEASURED
);
708 error_report("change dirtyrate state failed.");
711 rcu_unregister_thread();
715 void qmp_calc_dirty_rate(int64_t calc_time
,
716 bool has_sample_pages
,
717 int64_t sample_pages
,
719 DirtyRateMeasureMode mode
,
722 static struct DirtyRateConfig config
;
728 * If the dirty rate is already being measured, don't attempt to start.
730 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURING
) {
731 error_setg(errp
, "the dirty rate is already being measured.");
735 if (!is_sample_period_valid(calc_time
)) {
736 error_setg(errp
, "calc-time is out of range[%d, %d].",
737 MIN_FETCH_DIRTYRATE_TIME_SEC
,
738 MAX_FETCH_DIRTYRATE_TIME_SEC
);
743 mode
= DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
;
746 if (has_sample_pages
&& mode
!= DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
) {
747 error_setg(errp
, "sample-pages is used only in page-sampling mode");
751 if (has_sample_pages
) {
752 if (!is_sample_pages_valid(sample_pages
)) {
753 error_setg(errp
, "sample-pages is out of range[%d, %d].",
754 MIN_SAMPLE_PAGE_COUNT
,
755 MAX_SAMPLE_PAGE_COUNT
);
759 sample_pages
= DIRTYRATE_DEFAULT_SAMPLE_PAGES
;
763 * dirty ring mode only works when kvm dirty ring is enabled.
764 * on the contrary, dirty bitmap mode is not.
766 if (((mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_RING
) &&
767 !kvm_dirty_ring_enabled()) ||
768 ((mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP
) &&
769 kvm_dirty_ring_enabled())) {
770 error_setg(errp
, "mode %s is not enabled, use other method instead.",
771 DirtyRateMeasureMode_str(mode
));
776 * Init calculation state as unstarted.
778 ret
= dirtyrate_set_state(&CalculatingState
, CalculatingState
,
779 DIRTY_RATE_STATUS_UNSTARTED
);
781 error_setg(errp
, "init dirty rate calculation state failed.");
785 config
.sample_period_seconds
= calc_time
;
786 config
.sample_pages_per_gigabytes
= sample_pages
;
789 cleanup_dirtyrate_stat(config
);
792 * update dirty rate mode so that we can figure out what mode has
793 * been used in last calculation
795 dirtyrate_mode
= mode
;
797 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
798 init_dirtyrate_stat(start_time
, config
);
800 qemu_thread_create(&thread
, "get_dirtyrate", get_dirtyrate_thread
,
801 (void *)&config
, QEMU_THREAD_DETACHED
);
804 struct DirtyRateInfo
*qmp_query_dirty_rate(Error
**errp
)
806 return query_dirty_rate_info();
809 void hmp_info_dirty_rate(Monitor
*mon
, const QDict
*qdict
)
811 DirtyRateInfo
*info
= query_dirty_rate_info();
813 monitor_printf(mon
, "Status: %s\n",
814 DirtyRateStatus_str(info
->status
));
815 monitor_printf(mon
, "Start Time: %"PRIi64
" (ms)\n",
817 if (info
->mode
== DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
) {
818 monitor_printf(mon
, "Sample Pages: %"PRIu64
" (per GB)\n",
821 monitor_printf(mon
, "Period: %"PRIi64
" (sec)\n",
823 monitor_printf(mon
, "Mode: %s\n",
824 DirtyRateMeasureMode_str(info
->mode
));
825 monitor_printf(mon
, "Dirty rate: ");
826 if (info
->has_dirty_rate
) {
827 monitor_printf(mon
, "%"PRIi64
" (MB/s)\n", info
->dirty_rate
);
828 if (info
->has_vcpu_dirty_rate
) {
829 DirtyRateVcpuList
*rate
, *head
= info
->vcpu_dirty_rate
;
830 for (rate
= head
; rate
!= NULL
; rate
= rate
->next
) {
831 monitor_printf(mon
, "vcpu[%"PRIi64
"], Dirty rate: %"PRIi64
832 " (MB/s)\n", rate
->value
->id
,
833 rate
->value
->dirty_rate
);
837 monitor_printf(mon
, "(not ready)\n");
840 qapi_free_DirtyRateVcpuList(info
->vcpu_dirty_rate
);
844 void hmp_calc_dirty_rate(Monitor
*mon
, const QDict
*qdict
)
846 int64_t sec
= qdict_get_try_int(qdict
, "second", 0);
847 int64_t sample_pages
= qdict_get_try_int(qdict
, "sample_pages_per_GB", -1);
848 bool has_sample_pages
= (sample_pages
!= -1);
849 bool dirty_ring
= qdict_get_try_bool(qdict
, "dirty_ring", false);
850 bool dirty_bitmap
= qdict_get_try_bool(qdict
, "dirty_bitmap", false);
851 DirtyRateMeasureMode mode
= DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
;
855 monitor_printf(mon
, "Incorrect period length specified!\n");
859 if (dirty_ring
&& dirty_bitmap
) {
860 monitor_printf(mon
, "Either dirty ring or dirty bitmap "
861 "can be specified!\n");
866 mode
= DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP
;
867 } else if (dirty_ring
) {
868 mode
= DIRTY_RATE_MEASURE_MODE_DIRTY_RING
;
871 qmp_calc_dirty_rate(sec
, has_sample_pages
, sample_pages
, true,
874 hmp_handle_error(mon
, err
);
878 monitor_printf(mon
, "Starting dirty rate measurement with period %"PRIi64
880 monitor_printf(mon
, "[Please use 'info dirty_rate' to check results]\n");