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 "hw/core/cpu.h"
17 #include "qapi/error.h"
18 #include "exec/ramblock.h"
19 #include "exec/target_page.h"
20 #include "qemu/rcu_queue.h"
21 #include "qemu/main-loop.h"
22 #include "qapi/qapi-commands-migration.h"
25 #include "dirtyrate.h"
26 #include "monitor/hmp.h"
27 #include "monitor/monitor.h"
28 #include "qapi/qmp/qdict.h"
29 #include "sysemu/kvm.h"
30 #include "sysemu/runstate.h"
31 #include "exec/memory.h"
32 #include "qemu/xxhash.h"
35 * total_dirty_pages is procted by BQL and is used
36 * to stat dirty pages during the period of two
37 * memory_global_dirty_log_sync
39 uint64_t total_dirty_pages
;
41 typedef struct DirtyPageRecord
{
46 static int CalculatingState
= DIRTY_RATE_STATUS_UNSTARTED
;
47 static struct DirtyRateStat DirtyStat
;
48 static DirtyRateMeasureMode dirtyrate_mode
=
49 DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
;
51 static int64_t dirty_stat_wait(int64_t msec
, int64_t initial_time
)
55 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
56 if ((current_time
- initial_time
) >= msec
) {
57 msec
= current_time
- initial_time
;
59 g_usleep((msec
+ initial_time
- current_time
) * 1000);
60 /* g_usleep may overshoot */
61 msec
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) - initial_time
;
67 static inline void record_dirtypages(DirtyPageRecord
*dirty_pages
,
68 CPUState
*cpu
, bool start
)
71 dirty_pages
[cpu
->cpu_index
].start_pages
= cpu
->dirty_pages
;
73 dirty_pages
[cpu
->cpu_index
].end_pages
= cpu
->dirty_pages
;
77 static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages
,
80 uint64_t increased_dirty_pages
=
81 dirty_pages
.end_pages
- dirty_pages
.start_pages
;
84 * multiply by 1000ms/s _before_ converting down to megabytes
85 * to avoid losing precision
87 return qemu_target_pages_to_MiB(increased_dirty_pages
* 1000) /
91 void global_dirty_log_change(unsigned int flag
, bool start
)
93 qemu_mutex_lock_iothread();
95 memory_global_dirty_log_start(flag
);
97 memory_global_dirty_log_stop(flag
);
99 qemu_mutex_unlock_iothread();
103 * global_dirty_log_sync
104 * 1. sync dirty log from kvm
105 * 2. stop dirty tracking if needed.
107 static void global_dirty_log_sync(unsigned int flag
, bool one_shot
)
109 qemu_mutex_lock_iothread();
110 memory_global_dirty_log_sync(false);
112 memory_global_dirty_log_stop(flag
);
114 qemu_mutex_unlock_iothread();
117 static DirtyPageRecord
*vcpu_dirty_stat_alloc(VcpuStat
*stat
)
127 stat
->rates
= g_new0(DirtyRateVcpu
, nvcpu
);
129 return g_new0(DirtyPageRecord
, nvcpu
);
132 static void vcpu_dirty_stat_collect(VcpuStat
*stat
,
133 DirtyPageRecord
*records
,
139 record_dirtypages(records
, cpu
, start
);
143 int64_t vcpu_calculate_dirtyrate(int64_t calc_time_ms
,
148 DirtyPageRecord
*records
;
149 int64_t init_time_ms
;
156 init_time_ms
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
158 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock
) {
159 gen_id
= cpu_list_generation_id_get();
160 records
= vcpu_dirty_stat_alloc(stat
);
161 vcpu_dirty_stat_collect(stat
, records
, true);
164 duration
= dirty_stat_wait(calc_time_ms
, init_time_ms
);
166 global_dirty_log_sync(flag
, one_shot
);
168 WITH_QEMU_LOCK_GUARD(&qemu_cpu_list_lock
) {
169 if (gen_id
!= cpu_list_generation_id_get()) {
175 vcpu_dirty_stat_collect(stat
, records
, false);
178 for (i
= 0; i
< stat
->nvcpu
; i
++) {
179 dirtyrate
= do_calculate_dirtyrate(records
[i
], duration
);
181 stat
->rates
[i
].id
= i
;
182 stat
->rates
[i
].dirty_rate
= dirtyrate
;
184 trace_dirtyrate_do_calculate_vcpu(i
, dirtyrate
);
192 static bool is_calc_time_valid(int64_t msec
)
194 if ((msec
< MIN_CALC_TIME_MS
) || (msec
> MAX_CALC_TIME_MS
)) {
201 static bool is_sample_pages_valid(int64_t pages
)
203 return pages
>= MIN_SAMPLE_PAGE_COUNT
&&
204 pages
<= MAX_SAMPLE_PAGE_COUNT
;
207 static int dirtyrate_set_state(int *state
, int old_state
, int new_state
)
209 assert(new_state
< DIRTY_RATE_STATUS__MAX
);
210 trace_dirtyrate_set_state(DirtyRateStatus_str(new_state
));
211 if (qatomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
218 /* Decimal power of given time unit relative to one second */
219 static int time_unit_to_power(TimeUnit time_unit
)
222 case TIME_UNIT_SECOND
:
224 case TIME_UNIT_MILLISECOND
:
227 assert(false); /* unreachable */
232 static int64_t convert_time_unit(int64_t value
, TimeUnit unit_from
,
235 int power
= time_unit_to_power(unit_from
) -
236 time_unit_to_power(unit_to
);
249 static struct DirtyRateInfo
*
250 query_dirty_rate_info(TimeUnit calc_time_unit
)
253 int64_t dirty_rate
= DirtyStat
.dirty_rate
;
254 struct DirtyRateInfo
*info
= g_new0(DirtyRateInfo
, 1);
255 DirtyRateVcpuList
*head
= NULL
, **tail
= &head
;
257 info
->status
= CalculatingState
;
258 info
->start_time
= DirtyStat
.start_time
;
259 info
->calc_time
= convert_time_unit(DirtyStat
.calc_time_ms
,
260 TIME_UNIT_MILLISECOND
,
262 info
->calc_time_unit
= calc_time_unit
;
263 info
->sample_pages
= DirtyStat
.sample_pages
;
264 info
->mode
= dirtyrate_mode
;
266 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURED
) {
267 info
->has_dirty_rate
= true;
268 info
->dirty_rate
= dirty_rate
;
270 if (dirtyrate_mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_RING
) {
272 * set sample_pages with 0 to indicate page sampling
275 info
->sample_pages
= 0;
276 info
->has_vcpu_dirty_rate
= true;
277 for (i
= 0; i
< DirtyStat
.dirty_ring
.nvcpu
; i
++) {
278 DirtyRateVcpu
*rate
= g_new0(DirtyRateVcpu
, 1);
279 rate
->id
= DirtyStat
.dirty_ring
.rates
[i
].id
;
280 rate
->dirty_rate
= DirtyStat
.dirty_ring
.rates
[i
].dirty_rate
;
281 QAPI_LIST_APPEND(tail
, rate
);
283 info
->vcpu_dirty_rate
= head
;
286 if (dirtyrate_mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP
) {
287 info
->sample_pages
= 0;
291 trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState
));
296 static void init_dirtyrate_stat(int64_t start_time
,
297 struct DirtyRateConfig config
)
299 DirtyStat
.dirty_rate
= -1;
300 DirtyStat
.start_time
= start_time
;
301 DirtyStat
.calc_time_ms
= config
.calc_time_ms
;
302 DirtyStat
.sample_pages
= config
.sample_pages_per_gigabytes
;
304 switch (config
.mode
) {
305 case DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
:
306 DirtyStat
.page_sampling
.total_dirty_samples
= 0;
307 DirtyStat
.page_sampling
.total_sample_count
= 0;
308 DirtyStat
.page_sampling
.total_block_mem_MB
= 0;
310 case DIRTY_RATE_MEASURE_MODE_DIRTY_RING
:
311 DirtyStat
.dirty_ring
.nvcpu
= -1;
312 DirtyStat
.dirty_ring
.rates
= NULL
;
319 static void cleanup_dirtyrate_stat(struct DirtyRateConfig config
)
321 /* last calc-dirty-rate qmp use dirty ring mode */
322 if (dirtyrate_mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_RING
) {
323 free(DirtyStat
.dirty_ring
.rates
);
324 DirtyStat
.dirty_ring
.rates
= NULL
;
328 static void update_dirtyrate_stat(struct RamblockDirtyInfo
*info
)
330 DirtyStat
.page_sampling
.total_dirty_samples
+= info
->sample_dirty_count
;
331 DirtyStat
.page_sampling
.total_sample_count
+= info
->sample_pages_count
;
332 /* size of total pages in MB */
333 DirtyStat
.page_sampling
.total_block_mem_MB
+=
334 qemu_target_pages_to_MiB(info
->ramblock_pages
);
337 static void update_dirtyrate(uint64_t msec
)
340 uint64_t total_dirty_samples
= DirtyStat
.page_sampling
.total_dirty_samples
;
341 uint64_t total_sample_count
= DirtyStat
.page_sampling
.total_sample_count
;
342 uint64_t total_block_mem_MB
= DirtyStat
.page_sampling
.total_block_mem_MB
;
344 dirtyrate
= total_dirty_samples
* total_block_mem_MB
*
345 1000 / (total_sample_count
* msec
);
347 DirtyStat
.dirty_rate
= dirtyrate
;
351 * Compute hash of a single page of size TARGET_PAGE_SIZE.
353 static uint32_t compute_page_hash(void *ptr
)
355 size_t page_size
= qemu_target_page_size();
357 uint64_t v1
, v2
, v3
, v4
;
359 const uint64_t *p
= ptr
;
361 v1
= QEMU_XXHASH_SEED
+ XXH_PRIME64_1
+ XXH_PRIME64_2
;
362 v2
= QEMU_XXHASH_SEED
+ XXH_PRIME64_2
;
363 v3
= QEMU_XXHASH_SEED
+ 0;
364 v4
= QEMU_XXHASH_SEED
- XXH_PRIME64_1
;
365 for (i
= 0; i
< page_size
/ 8; i
+= 4) {
366 v1
= XXH64_round(v1
, p
[i
+ 0]);
367 v2
= XXH64_round(v2
, p
[i
+ 1]);
368 v3
= XXH64_round(v3
, p
[i
+ 2]);
369 v4
= XXH64_round(v4
, p
[i
+ 3]);
371 res
= XXH64_mergerounds(v1
, v2
, v3
, v4
);
373 res
= XXH64_avalanche(res
);
374 return (uint32_t)(res
& UINT32_MAX
);
379 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
380 * in ramblock, which starts from ramblock base address.
382 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo
*info
,
387 hash
= compute_page_hash(info
->ramblock_addr
+
388 vfn
* qemu_target_page_size());
390 trace_get_ramblock_vfn_hash(info
->idstr
, vfn
, hash
);
394 static bool save_ramblock_hash(struct RamblockDirtyInfo
*info
)
396 unsigned int sample_pages_count
;
400 sample_pages_count
= info
->sample_pages_count
;
402 /* ramblock size less than one page, return success to skip this ramblock */
403 if (unlikely(info
->ramblock_pages
== 0 || sample_pages_count
== 0)) {
407 info
->hash_result
= g_try_malloc0_n(sample_pages_count
,
409 if (!info
->hash_result
) {
413 info
->sample_page_vfn
= g_try_malloc0_n(sample_pages_count
,
415 if (!info
->sample_page_vfn
) {
416 g_free(info
->hash_result
);
421 for (i
= 0; i
< sample_pages_count
; i
++) {
422 info
->sample_page_vfn
[i
] = g_rand_int_range(rand
, 0,
423 info
->ramblock_pages
- 1);
424 info
->hash_result
[i
] = get_ramblock_vfn_hash(info
,
425 info
->sample_page_vfn
[i
]);
432 static void get_ramblock_dirty_info(RAMBlock
*block
,
433 struct RamblockDirtyInfo
*info
,
434 struct DirtyRateConfig
*config
)
436 uint64_t sample_pages_per_gigabytes
= config
->sample_pages_per_gigabytes
;
438 /* Right shift 30 bits to calc ramblock size in GB */
439 info
->sample_pages_count
= (qemu_ram_get_used_length(block
) *
440 sample_pages_per_gigabytes
) >> 30;
441 /* Right shift TARGET_PAGE_BITS to calc page count */
442 info
->ramblock_pages
= qemu_ram_get_used_length(block
) >>
443 qemu_target_page_bits();
444 info
->ramblock_addr
= qemu_ram_get_host_addr(block
);
445 strcpy(info
->idstr
, qemu_ram_get_idstr(block
));
448 static void free_ramblock_dirty_info(struct RamblockDirtyInfo
*infos
, int count
)
456 for (i
= 0; i
< count
; i
++) {
457 g_free(infos
[i
].sample_page_vfn
);
458 g_free(infos
[i
].hash_result
);
463 static bool skip_sample_ramblock(RAMBlock
*block
)
466 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
468 if (qemu_ram_get_used_length(block
) < (MIN_RAMBLOCK_SIZE
<< 10)) {
469 trace_skip_sample_ramblock(block
->idstr
,
470 qemu_ram_get_used_length(block
));
477 static bool record_ramblock_hash_info(struct RamblockDirtyInfo
**block_dinfo
,
478 struct DirtyRateConfig config
,
481 struct RamblockDirtyInfo
*info
= NULL
;
482 struct RamblockDirtyInfo
*dinfo
= NULL
;
483 RAMBlock
*block
= NULL
;
488 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
489 if (skip_sample_ramblock(block
)) {
495 dinfo
= g_try_malloc0_n(total_count
, sizeof(struct RamblockDirtyInfo
));
500 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
501 if (skip_sample_ramblock(block
)) {
504 if (index
>= total_count
) {
507 info
= &dinfo
[index
];
508 get_ramblock_dirty_info(block
, info
, &config
);
509 if (!save_ramblock_hash(info
)) {
517 *block_count
= index
;
518 *block_dinfo
= dinfo
;
522 static void calc_page_dirty_rate(struct RamblockDirtyInfo
*info
)
527 for (i
= 0; i
< info
->sample_pages_count
; i
++) {
528 hash
= get_ramblock_vfn_hash(info
, info
->sample_page_vfn
[i
]);
529 if (hash
!= info
->hash_result
[i
]) {
530 trace_calc_page_dirty_rate(info
->idstr
, hash
, info
->hash_result
[i
]);
531 info
->sample_dirty_count
++;
536 static struct RamblockDirtyInfo
*
537 find_block_matched(RAMBlock
*block
, int count
,
538 struct RamblockDirtyInfo
*infos
)
542 for (i
= 0; i
< count
; i
++) {
543 if (!strcmp(infos
[i
].idstr
, qemu_ram_get_idstr(block
))) {
552 if (infos
[i
].ramblock_addr
!= qemu_ram_get_host_addr(block
) ||
553 infos
[i
].ramblock_pages
!=
554 (qemu_ram_get_used_length(block
) >> qemu_target_page_bits())) {
555 trace_find_page_matched(block
->idstr
);
562 static bool compare_page_hash_info(struct RamblockDirtyInfo
*info
,
565 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
566 RAMBlock
*block
= NULL
;
568 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
569 if (skip_sample_ramblock(block
)) {
572 block_dinfo
= find_block_matched(block
, block_count
, info
);
573 if (block_dinfo
== NULL
) {
576 calc_page_dirty_rate(block_dinfo
);
577 update_dirtyrate_stat(block_dinfo
);
580 if (DirtyStat
.page_sampling
.total_sample_count
== 0) {
587 static inline void record_dirtypages_bitmap(DirtyPageRecord
*dirty_pages
,
591 dirty_pages
->start_pages
= total_dirty_pages
;
593 dirty_pages
->end_pages
= total_dirty_pages
;
597 static inline void dirtyrate_manual_reset_protect(void)
599 RAMBlock
*block
= NULL
;
601 WITH_RCU_READ_LOCK_GUARD() {
602 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
603 memory_region_clear_dirty_bitmap(block
->mr
, 0,
609 static void calculate_dirtyrate_dirty_bitmap(struct DirtyRateConfig config
)
612 DirtyPageRecord dirty_pages
;
614 qemu_mutex_lock_iothread();
615 memory_global_dirty_log_start(GLOBAL_DIRTY_DIRTY_RATE
);
618 * 1'round of log sync may return all 1 bits with
619 * KVM_DIRTY_LOG_INITIALLY_SET enable
620 * skip it unconditionally and start dirty tracking
621 * from 2'round of log sync
623 memory_global_dirty_log_sync(false);
626 * reset page protect manually and unconditionally.
627 * this make sure kvm dirty log be cleared if
628 * KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE cap is enabled.
630 dirtyrate_manual_reset_protect();
631 qemu_mutex_unlock_iothread();
633 record_dirtypages_bitmap(&dirty_pages
, true);
635 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
636 DirtyStat
.start_time
= start_time
/ 1000;
638 DirtyStat
.calc_time_ms
= dirty_stat_wait(config
.calc_time_ms
, start_time
);
642 * 1. fetch dirty bitmap from kvm
643 * 2. stop dirty tracking
645 global_dirty_log_sync(GLOBAL_DIRTY_DIRTY_RATE
, true);
647 record_dirtypages_bitmap(&dirty_pages
, false);
649 DirtyStat
.dirty_rate
= do_calculate_dirtyrate(dirty_pages
,
650 DirtyStat
.calc_time_ms
);
653 static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config
)
655 uint64_t dirtyrate
= 0;
656 uint64_t dirtyrate_sum
= 0;
660 global_dirty_log_change(GLOBAL_DIRTY_DIRTY_RATE
, true);
662 DirtyStat
.start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
664 /* calculate vcpu dirtyrate */
665 DirtyStat
.calc_time_ms
= vcpu_calculate_dirtyrate(config
.calc_time_ms
,
666 &DirtyStat
.dirty_ring
,
667 GLOBAL_DIRTY_DIRTY_RATE
,
670 /* calculate vm dirtyrate */
671 for (i
= 0; i
< DirtyStat
.dirty_ring
.nvcpu
; i
++) {
672 dirtyrate
= DirtyStat
.dirty_ring
.rates
[i
].dirty_rate
;
673 DirtyStat
.dirty_ring
.rates
[i
].dirty_rate
= dirtyrate
;
674 dirtyrate_sum
+= dirtyrate
;
677 DirtyStat
.dirty_rate
= dirtyrate_sum
;
680 static void calculate_dirtyrate_sample_vm(struct DirtyRateConfig config
)
682 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
684 int64_t initial_time
;
687 initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
688 if (!record_ramblock_hash_info(&block_dinfo
, config
, &block_count
)) {
693 DirtyStat
.calc_time_ms
= dirty_stat_wait(config
.calc_time_ms
,
695 DirtyStat
.start_time
= initial_time
/ 1000;
698 if (!compare_page_hash_info(block_dinfo
, block_count
)) {
702 update_dirtyrate(DirtyStat
.calc_time_ms
);
706 free_ramblock_dirty_info(block_dinfo
, block_count
);
709 static void calculate_dirtyrate(struct DirtyRateConfig config
)
711 if (config
.mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP
) {
712 calculate_dirtyrate_dirty_bitmap(config
);
713 } else if (config
.mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_RING
) {
714 calculate_dirtyrate_dirty_ring(config
);
716 calculate_dirtyrate_sample_vm(config
);
719 trace_dirtyrate_calculate(DirtyStat
.dirty_rate
);
722 void *get_dirtyrate_thread(void *arg
)
724 struct DirtyRateConfig config
= *(struct DirtyRateConfig
*)arg
;
726 rcu_register_thread();
728 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_UNSTARTED
,
729 DIRTY_RATE_STATUS_MEASURING
);
731 error_report("change dirtyrate state failed.");
735 calculate_dirtyrate(config
);
737 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_MEASURING
,
738 DIRTY_RATE_STATUS_MEASURED
);
740 error_report("change dirtyrate state failed.");
743 rcu_unregister_thread();
747 void qmp_calc_dirty_rate(int64_t calc_time
,
748 bool has_calc_time_unit
,
749 TimeUnit calc_time_unit
,
750 bool has_sample_pages
,
751 int64_t sample_pages
,
753 DirtyRateMeasureMode mode
,
756 static struct DirtyRateConfig config
;
762 * If the dirty rate is already being measured, don't attempt to start.
764 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURING
) {
765 error_setg(errp
, "the dirty rate is already being measured.");
769 int64_t calc_time_ms
= convert_time_unit(
771 has_calc_time_unit
? calc_time_unit
: TIME_UNIT_SECOND
,
772 TIME_UNIT_MILLISECOND
775 if (!is_calc_time_valid(calc_time_ms
)) {
776 error_setg(errp
, "Calculation time is out of range [%dms, %dms].",
777 MIN_CALC_TIME_MS
, MAX_CALC_TIME_MS
);
782 mode
= DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
;
785 if (has_sample_pages
&& mode
!= DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
) {
786 error_setg(errp
, "sample-pages is used only in page-sampling mode");
790 if (has_sample_pages
) {
791 if (!is_sample_pages_valid(sample_pages
)) {
792 error_setg(errp
, "sample-pages is out of range[%d, %d].",
793 MIN_SAMPLE_PAGE_COUNT
,
794 MAX_SAMPLE_PAGE_COUNT
);
798 sample_pages
= DIRTYRATE_DEFAULT_SAMPLE_PAGES
;
802 * dirty ring mode only works when kvm dirty ring is enabled.
803 * on the contrary, dirty bitmap mode is not.
805 if (((mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_RING
) &&
806 !kvm_dirty_ring_enabled()) ||
807 ((mode
== DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP
) &&
808 kvm_dirty_ring_enabled())) {
809 error_setg(errp
, "mode %s is not enabled, use other method instead.",
810 DirtyRateMeasureMode_str(mode
));
815 * Init calculation state as unstarted.
817 ret
= dirtyrate_set_state(&CalculatingState
, CalculatingState
,
818 DIRTY_RATE_STATUS_UNSTARTED
);
820 error_setg(errp
, "init dirty rate calculation state failed.");
824 config
.calc_time_ms
= calc_time_ms
;
825 config
.sample_pages_per_gigabytes
= sample_pages
;
828 cleanup_dirtyrate_stat(config
);
831 * update dirty rate mode so that we can figure out what mode has
832 * been used in last calculation
834 dirtyrate_mode
= mode
;
836 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
837 init_dirtyrate_stat(start_time
, config
);
839 qemu_thread_create(&thread
, "get_dirtyrate", get_dirtyrate_thread
,
840 (void *)&config
, QEMU_THREAD_DETACHED
);
844 struct DirtyRateInfo
*qmp_query_dirty_rate(bool has_calc_time_unit
,
845 TimeUnit calc_time_unit
,
848 return query_dirty_rate_info(
849 has_calc_time_unit
? calc_time_unit
: TIME_UNIT_SECOND
);
852 void hmp_info_dirty_rate(Monitor
*mon
, const QDict
*qdict
)
854 DirtyRateInfo
*info
= query_dirty_rate_info(TIME_UNIT_SECOND
);
856 monitor_printf(mon
, "Status: %s\n",
857 DirtyRateStatus_str(info
->status
));
858 monitor_printf(mon
, "Start Time: %"PRIi64
" (ms)\n",
860 if (info
->mode
== DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
) {
861 monitor_printf(mon
, "Sample Pages: %"PRIu64
" (per GB)\n",
864 monitor_printf(mon
, "Period: %"PRIi64
" (sec)\n",
866 monitor_printf(mon
, "Mode: %s\n",
867 DirtyRateMeasureMode_str(info
->mode
));
868 monitor_printf(mon
, "Dirty rate: ");
869 if (info
->has_dirty_rate
) {
870 monitor_printf(mon
, "%"PRIi64
" (MB/s)\n", info
->dirty_rate
);
871 if (info
->has_vcpu_dirty_rate
) {
872 DirtyRateVcpuList
*rate
, *head
= info
->vcpu_dirty_rate
;
873 for (rate
= head
; rate
!= NULL
; rate
= rate
->next
) {
874 monitor_printf(mon
, "vcpu[%"PRIi64
"], Dirty rate: %"PRIi64
875 " (MB/s)\n", rate
->value
->id
,
876 rate
->value
->dirty_rate
);
880 monitor_printf(mon
, "(not ready)\n");
883 qapi_free_DirtyRateVcpuList(info
->vcpu_dirty_rate
);
887 void hmp_calc_dirty_rate(Monitor
*mon
, const QDict
*qdict
)
889 int64_t sec
= qdict_get_try_int(qdict
, "second", 0);
890 int64_t sample_pages
= qdict_get_try_int(qdict
, "sample_pages_per_GB", -1);
891 bool has_sample_pages
= (sample_pages
!= -1);
892 bool dirty_ring
= qdict_get_try_bool(qdict
, "dirty_ring", false);
893 bool dirty_bitmap
= qdict_get_try_bool(qdict
, "dirty_bitmap", false);
894 DirtyRateMeasureMode mode
= DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
;
898 monitor_printf(mon
, "Incorrect period length specified!\n");
902 if (dirty_ring
&& dirty_bitmap
) {
903 monitor_printf(mon
, "Either dirty ring or dirty bitmap "
904 "can be specified!\n");
909 mode
= DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP
;
910 } else if (dirty_ring
) {
911 mode
= DIRTY_RATE_MEASURE_MODE_DIRTY_RING
;
914 qmp_calc_dirty_rate(sec
, /* calc-time */
915 false, TIME_UNIT_SECOND
, /* calc-time-unit */
916 has_sample_pages
, sample_pages
,
920 hmp_handle_error(mon
, err
);
924 monitor_printf(mon
, "Starting dirty rate measurement with period %"PRIi64
926 monitor_printf(mon
, "[Please use 'info dirty_rate' to check results]\n");