qxl: don't assert() if device isn't yet initialized
[qemu/kevin.git] / migration / dirtyrate.c
blobbccb3515e3c4801d23d565ac6b9e64856125c179
1 /*
2 * Dirtyrate implement code
4 * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO.,LTD.
6 * Authors:
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"
15 #include <zlib.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"
23 #include "ram.h"
24 #include "trace.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 {
42 uint64_t start_pages;
43 uint64_t end_pages;
44 } 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)
53 int64_t current_time;
55 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
56 if ((current_time - initial_time) >= msec) {
57 msec = current_time - initial_time;
58 } else {
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;
64 return msec;
67 static inline void record_dirtypages(DirtyPageRecord *dirty_pages,
68 CPUState *cpu, bool start)
70 if (start) {
71 dirty_pages[cpu->cpu_index].start_pages = cpu->dirty_pages;
72 } else {
73 dirty_pages[cpu->cpu_index].end_pages = cpu->dirty_pages;
77 static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages,
78 int64_t calc_time_ms)
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) /
88 calc_time_ms;
91 void global_dirty_log_change(unsigned int flag, bool start)
93 qemu_mutex_lock_iothread();
94 if (start) {
95 memory_global_dirty_log_start(flag);
96 } else {
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);
111 if (one_shot) {
112 memory_global_dirty_log_stop(flag);
114 qemu_mutex_unlock_iothread();
117 static DirtyPageRecord *vcpu_dirty_stat_alloc(VcpuStat *stat)
119 CPUState *cpu;
120 int nvcpu = 0;
122 CPU_FOREACH(cpu) {
123 nvcpu++;
126 stat->nvcpu = nvcpu;
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,
134 bool start)
136 CPUState *cpu;
138 CPU_FOREACH(cpu) {
139 record_dirtypages(records, cpu, start);
143 int64_t vcpu_calculate_dirtyrate(int64_t calc_time_ms,
144 VcpuStat *stat,
145 unsigned int flag,
146 bool one_shot)
148 DirtyPageRecord *records;
149 int64_t init_time_ms;
150 int64_t duration;
151 int64_t dirtyrate;
152 int i = 0;
153 unsigned int gen_id;
155 retry:
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()) {
170 g_free(records);
171 g_free(stat->rates);
172 cpu_list_unlock();
173 goto retry;
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);
187 g_free(records);
189 return duration;
192 static bool is_sample_period_valid(int64_t sec)
194 if (sec < MIN_FETCH_DIRTYRATE_TIME_SEC ||
195 sec > MAX_FETCH_DIRTYRATE_TIME_SEC) {
196 return false;
199 return true;
202 static bool is_sample_pages_valid(int64_t pages)
204 return pages >= MIN_SAMPLE_PAGE_COUNT &&
205 pages <= MAX_SAMPLE_PAGE_COUNT;
208 static int dirtyrate_set_state(int *state, int old_state, int new_state)
210 assert(new_state < DIRTY_RATE_STATUS__MAX);
211 trace_dirtyrate_set_state(DirtyRateStatus_str(new_state));
212 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
213 return 0;
214 } else {
215 return -1;
219 static struct DirtyRateInfo *query_dirty_rate_info(void)
221 int i;
222 int64_t dirty_rate = DirtyStat.dirty_rate;
223 struct DirtyRateInfo *info = g_new0(DirtyRateInfo, 1);
224 DirtyRateVcpuList *head = NULL, **tail = &head;
226 info->status = CalculatingState;
227 info->start_time = DirtyStat.start_time;
228 info->calc_time = DirtyStat.calc_time;
229 info->sample_pages = DirtyStat.sample_pages;
230 info->mode = dirtyrate_mode;
232 if (qatomic_read(&CalculatingState) == DIRTY_RATE_STATUS_MEASURED) {
233 info->has_dirty_rate = true;
234 info->dirty_rate = dirty_rate;
236 if (dirtyrate_mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) {
238 * set sample_pages with 0 to indicate page sampling
239 * isn't enabled
241 info->sample_pages = 0;
242 info->has_vcpu_dirty_rate = true;
243 for (i = 0; i < DirtyStat.dirty_ring.nvcpu; i++) {
244 DirtyRateVcpu *rate = g_new0(DirtyRateVcpu, 1);
245 rate->id = DirtyStat.dirty_ring.rates[i].id;
246 rate->dirty_rate = DirtyStat.dirty_ring.rates[i].dirty_rate;
247 QAPI_LIST_APPEND(tail, rate);
249 info->vcpu_dirty_rate = head;
252 if (dirtyrate_mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) {
253 info->sample_pages = 0;
257 trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState));
259 return info;
262 static void init_dirtyrate_stat(int64_t start_time,
263 struct DirtyRateConfig config)
265 DirtyStat.dirty_rate = -1;
266 DirtyStat.start_time = start_time;
267 DirtyStat.calc_time = config.sample_period_seconds;
268 DirtyStat.sample_pages = config.sample_pages_per_gigabytes;
270 switch (config.mode) {
271 case DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING:
272 DirtyStat.page_sampling.total_dirty_samples = 0;
273 DirtyStat.page_sampling.total_sample_count = 0;
274 DirtyStat.page_sampling.total_block_mem_MB = 0;
275 break;
276 case DIRTY_RATE_MEASURE_MODE_DIRTY_RING:
277 DirtyStat.dirty_ring.nvcpu = -1;
278 DirtyStat.dirty_ring.rates = NULL;
279 break;
280 default:
281 break;
285 static void cleanup_dirtyrate_stat(struct DirtyRateConfig config)
287 /* last calc-dirty-rate qmp use dirty ring mode */
288 if (dirtyrate_mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) {
289 free(DirtyStat.dirty_ring.rates);
290 DirtyStat.dirty_ring.rates = NULL;
294 static void update_dirtyrate_stat(struct RamblockDirtyInfo *info)
296 DirtyStat.page_sampling.total_dirty_samples += info->sample_dirty_count;
297 DirtyStat.page_sampling.total_sample_count += info->sample_pages_count;
298 /* size of total pages in MB */
299 DirtyStat.page_sampling.total_block_mem_MB +=
300 qemu_target_pages_to_MiB(info->ramblock_pages);
303 static void update_dirtyrate(uint64_t msec)
305 uint64_t dirtyrate;
306 uint64_t total_dirty_samples = DirtyStat.page_sampling.total_dirty_samples;
307 uint64_t total_sample_count = DirtyStat.page_sampling.total_sample_count;
308 uint64_t total_block_mem_MB = DirtyStat.page_sampling.total_block_mem_MB;
310 dirtyrate = total_dirty_samples * total_block_mem_MB *
311 1000 / (total_sample_count * msec);
313 DirtyStat.dirty_rate = dirtyrate;
317 * Compute hash of a single page of size TARGET_PAGE_SIZE.
319 static uint32_t compute_page_hash(void *ptr)
321 size_t page_size = qemu_target_page_size();
322 uint32_t i;
323 uint64_t v1, v2, v3, v4;
324 uint64_t res;
325 const uint64_t *p = ptr;
327 v1 = QEMU_XXHASH_SEED + XXH_PRIME64_1 + XXH_PRIME64_2;
328 v2 = QEMU_XXHASH_SEED + XXH_PRIME64_2;
329 v3 = QEMU_XXHASH_SEED + 0;
330 v4 = QEMU_XXHASH_SEED - XXH_PRIME64_1;
331 for (i = 0; i < page_size / 8; i += 4) {
332 v1 = XXH64_round(v1, p[i + 0]);
333 v2 = XXH64_round(v2, p[i + 1]);
334 v3 = XXH64_round(v3, p[i + 2]);
335 v4 = XXH64_round(v4, p[i + 3]);
337 res = XXH64_mergerounds(v1, v2, v3, v4);
338 res += page_size;
339 res = XXH64_avalanche(res);
340 return (uint32_t)(res & UINT32_MAX);
345 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
346 * in ramblock, which starts from ramblock base address.
348 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
349 uint64_t vfn)
351 uint32_t hash;
353 hash = compute_page_hash(info->ramblock_addr +
354 vfn * qemu_target_page_size());
356 trace_get_ramblock_vfn_hash(info->idstr, vfn, hash);
357 return hash;
360 static bool save_ramblock_hash(struct RamblockDirtyInfo *info)
362 unsigned int sample_pages_count;
363 int i;
364 GRand *rand;
366 sample_pages_count = info->sample_pages_count;
368 /* ramblock size less than one page, return success to skip this ramblock */
369 if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
370 return true;
373 info->hash_result = g_try_malloc0_n(sample_pages_count,
374 sizeof(uint32_t));
375 if (!info->hash_result) {
376 return false;
379 info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
380 sizeof(uint64_t));
381 if (!info->sample_page_vfn) {
382 g_free(info->hash_result);
383 return false;
386 rand = g_rand_new();
387 for (i = 0; i < sample_pages_count; i++) {
388 info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
389 info->ramblock_pages - 1);
390 info->hash_result[i] = get_ramblock_vfn_hash(info,
391 info->sample_page_vfn[i]);
393 g_rand_free(rand);
395 return true;
398 static void get_ramblock_dirty_info(RAMBlock *block,
399 struct RamblockDirtyInfo *info,
400 struct DirtyRateConfig *config)
402 uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
404 /* Right shift 30 bits to calc ramblock size in GB */
405 info->sample_pages_count = (qemu_ram_get_used_length(block) *
406 sample_pages_per_gigabytes) >> 30;
407 /* Right shift TARGET_PAGE_BITS to calc page count */
408 info->ramblock_pages = qemu_ram_get_used_length(block) >>
409 qemu_target_page_bits();
410 info->ramblock_addr = qemu_ram_get_host_addr(block);
411 strcpy(info->idstr, qemu_ram_get_idstr(block));
414 static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
416 int i;
418 if (!infos) {
419 return;
422 for (i = 0; i < count; i++) {
423 g_free(infos[i].sample_page_vfn);
424 g_free(infos[i].hash_result);
426 g_free(infos);
429 static bool skip_sample_ramblock(RAMBlock *block)
432 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
434 if (qemu_ram_get_used_length(block) < (MIN_RAMBLOCK_SIZE << 10)) {
435 trace_skip_sample_ramblock(block->idstr,
436 qemu_ram_get_used_length(block));
437 return true;
440 return false;
443 static bool record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
444 struct DirtyRateConfig config,
445 int *block_count)
447 struct RamblockDirtyInfo *info = NULL;
448 struct RamblockDirtyInfo *dinfo = NULL;
449 RAMBlock *block = NULL;
450 int total_count = 0;
451 int index = 0;
452 bool ret = false;
454 RAMBLOCK_FOREACH_MIGRATABLE(block) {
455 if (skip_sample_ramblock(block)) {
456 continue;
458 total_count++;
461 dinfo = g_try_malloc0_n(total_count, sizeof(struct RamblockDirtyInfo));
462 if (dinfo == NULL) {
463 goto out;
466 RAMBLOCK_FOREACH_MIGRATABLE(block) {
467 if (skip_sample_ramblock(block)) {
468 continue;
470 if (index >= total_count) {
471 break;
473 info = &dinfo[index];
474 get_ramblock_dirty_info(block, info, &config);
475 if (!save_ramblock_hash(info)) {
476 goto out;
478 index++;
480 ret = true;
482 out:
483 *block_count = index;
484 *block_dinfo = dinfo;
485 return ret;
488 static void calc_page_dirty_rate(struct RamblockDirtyInfo *info)
490 uint32_t hash;
491 int i;
493 for (i = 0; i < info->sample_pages_count; i++) {
494 hash = get_ramblock_vfn_hash(info, info->sample_page_vfn[i]);
495 if (hash != info->hash_result[i]) {
496 trace_calc_page_dirty_rate(info->idstr, hash, info->hash_result[i]);
497 info->sample_dirty_count++;
502 static struct RamblockDirtyInfo *
503 find_block_matched(RAMBlock *block, int count,
504 struct RamblockDirtyInfo *infos)
506 int i;
508 for (i = 0; i < count; i++) {
509 if (!strcmp(infos[i].idstr, qemu_ram_get_idstr(block))) {
510 break;
514 if (i == count) {
515 return NULL;
518 if (infos[i].ramblock_addr != qemu_ram_get_host_addr(block) ||
519 infos[i].ramblock_pages !=
520 (qemu_ram_get_used_length(block) >> qemu_target_page_bits())) {
521 trace_find_page_matched(block->idstr);
522 return NULL;
525 return &infos[i];
528 static bool compare_page_hash_info(struct RamblockDirtyInfo *info,
529 int block_count)
531 struct RamblockDirtyInfo *block_dinfo = NULL;
532 RAMBlock *block = NULL;
534 RAMBLOCK_FOREACH_MIGRATABLE(block) {
535 if (skip_sample_ramblock(block)) {
536 continue;
538 block_dinfo = find_block_matched(block, block_count, info);
539 if (block_dinfo == NULL) {
540 continue;
542 calc_page_dirty_rate(block_dinfo);
543 update_dirtyrate_stat(block_dinfo);
546 if (DirtyStat.page_sampling.total_sample_count == 0) {
547 return false;
550 return true;
553 static inline void record_dirtypages_bitmap(DirtyPageRecord *dirty_pages,
554 bool start)
556 if (start) {
557 dirty_pages->start_pages = total_dirty_pages;
558 } else {
559 dirty_pages->end_pages = total_dirty_pages;
563 static inline void dirtyrate_manual_reset_protect(void)
565 RAMBlock *block = NULL;
567 WITH_RCU_READ_LOCK_GUARD() {
568 RAMBLOCK_FOREACH_MIGRATABLE(block) {
569 memory_region_clear_dirty_bitmap(block->mr, 0,
570 block->used_length);
575 static void calculate_dirtyrate_dirty_bitmap(struct DirtyRateConfig config)
577 int64_t msec = 0;
578 int64_t start_time;
579 DirtyPageRecord dirty_pages;
581 qemu_mutex_lock_iothread();
582 memory_global_dirty_log_start(GLOBAL_DIRTY_DIRTY_RATE);
585 * 1'round of log sync may return all 1 bits with
586 * KVM_DIRTY_LOG_INITIALLY_SET enable
587 * skip it unconditionally and start dirty tracking
588 * from 2'round of log sync
590 memory_global_dirty_log_sync(false);
593 * reset page protect manually and unconditionally.
594 * this make sure kvm dirty log be cleared if
595 * KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE cap is enabled.
597 dirtyrate_manual_reset_protect();
598 qemu_mutex_unlock_iothread();
600 record_dirtypages_bitmap(&dirty_pages, true);
602 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
603 DirtyStat.start_time = start_time / 1000;
605 msec = config.sample_period_seconds * 1000;
606 msec = dirty_stat_wait(msec, start_time);
607 DirtyStat.calc_time = msec / 1000;
610 * do two things.
611 * 1. fetch dirty bitmap from kvm
612 * 2. stop dirty tracking
614 global_dirty_log_sync(GLOBAL_DIRTY_DIRTY_RATE, true);
616 record_dirtypages_bitmap(&dirty_pages, false);
618 DirtyStat.dirty_rate = do_calculate_dirtyrate(dirty_pages, msec);
621 static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config)
623 int64_t duration;
624 uint64_t dirtyrate = 0;
625 uint64_t dirtyrate_sum = 0;
626 int i = 0;
628 /* start log sync */
629 global_dirty_log_change(GLOBAL_DIRTY_DIRTY_RATE, true);
631 DirtyStat.start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
633 /* calculate vcpu dirtyrate */
634 duration = vcpu_calculate_dirtyrate(config.sample_period_seconds * 1000,
635 &DirtyStat.dirty_ring,
636 GLOBAL_DIRTY_DIRTY_RATE,
637 true);
639 DirtyStat.calc_time = duration / 1000;
641 /* calculate vm dirtyrate */
642 for (i = 0; i < DirtyStat.dirty_ring.nvcpu; i++) {
643 dirtyrate = DirtyStat.dirty_ring.rates[i].dirty_rate;
644 DirtyStat.dirty_ring.rates[i].dirty_rate = dirtyrate;
645 dirtyrate_sum += dirtyrate;
648 DirtyStat.dirty_rate = dirtyrate_sum;
651 static void calculate_dirtyrate_sample_vm(struct DirtyRateConfig config)
653 struct RamblockDirtyInfo *block_dinfo = NULL;
654 int block_count = 0;
655 int64_t msec = 0;
656 int64_t initial_time;
658 rcu_read_lock();
659 initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
660 if (!record_ramblock_hash_info(&block_dinfo, config, &block_count)) {
661 goto out;
663 rcu_read_unlock();
665 msec = config.sample_period_seconds * 1000;
666 msec = dirty_stat_wait(msec, initial_time);
667 DirtyStat.start_time = initial_time / 1000;
668 DirtyStat.calc_time = msec / 1000;
670 rcu_read_lock();
671 if (!compare_page_hash_info(block_dinfo, block_count)) {
672 goto out;
675 update_dirtyrate(msec);
677 out:
678 rcu_read_unlock();
679 free_ramblock_dirty_info(block_dinfo, block_count);
682 static void calculate_dirtyrate(struct DirtyRateConfig config)
684 if (config.mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) {
685 calculate_dirtyrate_dirty_bitmap(config);
686 } else if (config.mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) {
687 calculate_dirtyrate_dirty_ring(config);
688 } else {
689 calculate_dirtyrate_sample_vm(config);
692 trace_dirtyrate_calculate(DirtyStat.dirty_rate);
695 void *get_dirtyrate_thread(void *arg)
697 struct DirtyRateConfig config = *(struct DirtyRateConfig *)arg;
698 int ret;
699 rcu_register_thread();
701 ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_UNSTARTED,
702 DIRTY_RATE_STATUS_MEASURING);
703 if (ret == -1) {
704 error_report("change dirtyrate state failed.");
705 return NULL;
708 calculate_dirtyrate(config);
710 ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_MEASURING,
711 DIRTY_RATE_STATUS_MEASURED);
712 if (ret == -1) {
713 error_report("change dirtyrate state failed.");
716 rcu_unregister_thread();
717 return NULL;
720 void qmp_calc_dirty_rate(int64_t calc_time,
721 bool has_sample_pages,
722 int64_t sample_pages,
723 bool has_mode,
724 DirtyRateMeasureMode mode,
725 Error **errp)
727 static struct DirtyRateConfig config;
728 QemuThread thread;
729 int ret;
730 int64_t start_time;
733 * If the dirty rate is already being measured, don't attempt to start.
735 if (qatomic_read(&CalculatingState) == DIRTY_RATE_STATUS_MEASURING) {
736 error_setg(errp, "the dirty rate is already being measured.");
737 return;
740 if (!is_sample_period_valid(calc_time)) {
741 error_setg(errp, "calc-time is out of range[%d, %d].",
742 MIN_FETCH_DIRTYRATE_TIME_SEC,
743 MAX_FETCH_DIRTYRATE_TIME_SEC);
744 return;
747 if (!has_mode) {
748 mode = DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
751 if (has_sample_pages && mode != DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING) {
752 error_setg(errp, "sample-pages is used only in page-sampling mode");
753 return;
756 if (has_sample_pages) {
757 if (!is_sample_pages_valid(sample_pages)) {
758 error_setg(errp, "sample-pages is out of range[%d, %d].",
759 MIN_SAMPLE_PAGE_COUNT,
760 MAX_SAMPLE_PAGE_COUNT);
761 return;
763 } else {
764 sample_pages = DIRTYRATE_DEFAULT_SAMPLE_PAGES;
768 * dirty ring mode only works when kvm dirty ring is enabled.
769 * on the contrary, dirty bitmap mode is not.
771 if (((mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) &&
772 !kvm_dirty_ring_enabled()) ||
773 ((mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) &&
774 kvm_dirty_ring_enabled())) {
775 error_setg(errp, "mode %s is not enabled, use other method instead.",
776 DirtyRateMeasureMode_str(mode));
777 return;
781 * Init calculation state as unstarted.
783 ret = dirtyrate_set_state(&CalculatingState, CalculatingState,
784 DIRTY_RATE_STATUS_UNSTARTED);
785 if (ret == -1) {
786 error_setg(errp, "init dirty rate calculation state failed.");
787 return;
790 config.sample_period_seconds = calc_time;
791 config.sample_pages_per_gigabytes = sample_pages;
792 config.mode = mode;
794 cleanup_dirtyrate_stat(config);
797 * update dirty rate mode so that we can figure out what mode has
798 * been used in last calculation
800 dirtyrate_mode = mode;
802 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
803 init_dirtyrate_stat(start_time, config);
805 qemu_thread_create(&thread, "get_dirtyrate", get_dirtyrate_thread,
806 (void *)&config, QEMU_THREAD_DETACHED);
809 struct DirtyRateInfo *qmp_query_dirty_rate(Error **errp)
811 return query_dirty_rate_info();
814 void hmp_info_dirty_rate(Monitor *mon, const QDict *qdict)
816 DirtyRateInfo *info = query_dirty_rate_info();
818 monitor_printf(mon, "Status: %s\n",
819 DirtyRateStatus_str(info->status));
820 monitor_printf(mon, "Start Time: %"PRIi64" (ms)\n",
821 info->start_time);
822 if (info->mode == DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING) {
823 monitor_printf(mon, "Sample Pages: %"PRIu64" (per GB)\n",
824 info->sample_pages);
826 monitor_printf(mon, "Period: %"PRIi64" (sec)\n",
827 info->calc_time);
828 monitor_printf(mon, "Mode: %s\n",
829 DirtyRateMeasureMode_str(info->mode));
830 monitor_printf(mon, "Dirty rate: ");
831 if (info->has_dirty_rate) {
832 monitor_printf(mon, "%"PRIi64" (MB/s)\n", info->dirty_rate);
833 if (info->has_vcpu_dirty_rate) {
834 DirtyRateVcpuList *rate, *head = info->vcpu_dirty_rate;
835 for (rate = head; rate != NULL; rate = rate->next) {
836 monitor_printf(mon, "vcpu[%"PRIi64"], Dirty rate: %"PRIi64
837 " (MB/s)\n", rate->value->id,
838 rate->value->dirty_rate);
841 } else {
842 monitor_printf(mon, "(not ready)\n");
845 qapi_free_DirtyRateVcpuList(info->vcpu_dirty_rate);
846 g_free(info);
849 void hmp_calc_dirty_rate(Monitor *mon, const QDict *qdict)
851 int64_t sec = qdict_get_try_int(qdict, "second", 0);
852 int64_t sample_pages = qdict_get_try_int(qdict, "sample_pages_per_GB", -1);
853 bool has_sample_pages = (sample_pages != -1);
854 bool dirty_ring = qdict_get_try_bool(qdict, "dirty_ring", false);
855 bool dirty_bitmap = qdict_get_try_bool(qdict, "dirty_bitmap", false);
856 DirtyRateMeasureMode mode = DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
857 Error *err = NULL;
859 if (!sec) {
860 monitor_printf(mon, "Incorrect period length specified!\n");
861 return;
864 if (dirty_ring && dirty_bitmap) {
865 monitor_printf(mon, "Either dirty ring or dirty bitmap "
866 "can be specified!\n");
867 return;
870 if (dirty_bitmap) {
871 mode = DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP;
872 } else if (dirty_ring) {
873 mode = DIRTY_RATE_MEASURE_MODE_DIRTY_RING;
876 qmp_calc_dirty_rate(sec, has_sample_pages, sample_pages, true,
877 mode, &err);
878 if (err) {
879 hmp_handle_error(mon, err);
880 return;
883 monitor_printf(mon, "Starting dirty rate measurement with period %"PRIi64
884 " seconds\n", sec);
885 monitor_printf(mon, "[Please use 'info dirty_rate' to check results]\n");