migration: Fix potential race on postcopy_qemufile_src
[qemu/ar7.git] / migration / dirtyrate.c
blob180ba38c7a803f0e37cb5eaa2dd586fc3895822f
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 "qapi/error.h"
17 #include "cpu.h"
18 #include "exec/ramblock.h"
19 #include "exec/ram_addr.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"
34 * total_dirty_pages is procted by BQL and is used
35 * to stat dirty pages during the period of two
36 * memory_global_dirty_log_sync
38 uint64_t total_dirty_pages;
40 typedef struct DirtyPageRecord {
41 uint64_t start_pages;
42 uint64_t end_pages;
43 } DirtyPageRecord;
45 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
46 static struct DirtyRateStat DirtyStat;
47 static DirtyRateMeasureMode dirtyrate_mode =
48 DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
50 static int64_t dirty_stat_wait(int64_t msec, int64_t initial_time)
52 int64_t current_time;
54 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
55 if ((current_time - initial_time) >= msec) {
56 msec = current_time - initial_time;
57 } else {
58 g_usleep((msec + initial_time - current_time) * 1000);
61 return msec;
64 static inline void record_dirtypages(DirtyPageRecord *dirty_pages,
65 CPUState *cpu, bool start)
67 if (start) {
68 dirty_pages[cpu->cpu_index].start_pages = cpu->dirty_pages;
69 } else {
70 dirty_pages[cpu->cpu_index].end_pages = cpu->dirty_pages;
74 static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages,
75 int64_t calc_time_ms)
77 uint64_t memory_size_MB;
78 uint64_t increased_dirty_pages =
79 dirty_pages.end_pages - dirty_pages.start_pages;
81 memory_size_MB = (increased_dirty_pages * TARGET_PAGE_SIZE) >> 20;
83 return memory_size_MB * 1000 / calc_time_ms;
86 void global_dirty_log_change(unsigned int flag, bool start)
88 qemu_mutex_lock_iothread();
89 if (start) {
90 memory_global_dirty_log_start(flag);
91 } else {
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();
106 if (one_shot) {
107 memory_global_dirty_log_stop(flag);
109 qemu_mutex_unlock_iothread();
112 static DirtyPageRecord *vcpu_dirty_stat_alloc(VcpuStat *stat)
114 CPUState *cpu;
115 int nvcpu = 0;
117 CPU_FOREACH(cpu) {
118 nvcpu++;
121 stat->nvcpu = nvcpu;
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,
129 bool start)
131 CPUState *cpu;
133 CPU_FOREACH(cpu) {
134 record_dirtypages(records, cpu, start);
138 int64_t vcpu_calculate_dirtyrate(int64_t calc_time_ms,
139 VcpuStat *stat,
140 unsigned int flag,
141 bool one_shot)
143 DirtyPageRecord *records;
144 int64_t init_time_ms;
145 int64_t duration;
146 int64_t dirtyrate;
147 int i = 0;
148 unsigned int gen_id;
150 retry:
151 init_time_ms = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
153 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);
157 cpu_list_unlock();
159 duration = dirty_stat_wait(calc_time_ms, init_time_ms);
161 global_dirty_log_sync(flag, one_shot);
163 cpu_list_lock();
164 if (gen_id != cpu_list_generation_id_get()) {
165 g_free(records);
166 g_free(stat->rates);
167 cpu_list_unlock();
168 goto retry;
170 vcpu_dirty_stat_collect(stat, records, false);
171 cpu_list_unlock();
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);
182 g_free(records);
184 return duration;
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) {
191 return false;
194 return true;
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) {
208 return 0;
209 } else {
210 return -1;
214 static struct DirtyRateInfo *query_dirty_rate_info(void)
216 int i;
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
234 * isn't enabled
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));
254 return info;
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;
270 break;
271 case DIRTY_RATE_MEASURE_MODE_DIRTY_RING:
272 DirtyStat.dirty_ring.nvcpu = -1;
273 DirtyStat.dirty_ring.rates = NULL;
274 break;
275 default:
276 break;
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 += (info->ramblock_pages *
295 TARGET_PAGE_SIZE) >> 20;
298 static void update_dirtyrate(uint64_t msec)
300 uint64_t dirtyrate;
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 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
313 * in ramblock, which starts from ramblock base address.
315 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
316 uint64_t vfn)
318 uint32_t crc;
320 crc = crc32(0, (info->ramblock_addr +
321 vfn * TARGET_PAGE_SIZE), TARGET_PAGE_SIZE);
323 trace_get_ramblock_vfn_hash(info->idstr, vfn, crc);
324 return crc;
327 static bool save_ramblock_hash(struct RamblockDirtyInfo *info)
329 unsigned int sample_pages_count;
330 int i;
331 GRand *rand;
333 sample_pages_count = info->sample_pages_count;
335 /* ramblock size less than one page, return success to skip this ramblock */
336 if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
337 return true;
340 info->hash_result = g_try_malloc0_n(sample_pages_count,
341 sizeof(uint32_t));
342 if (!info->hash_result) {
343 return false;
346 info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
347 sizeof(uint64_t));
348 if (!info->sample_page_vfn) {
349 g_free(info->hash_result);
350 return false;
353 rand = g_rand_new();
354 for (i = 0; i < sample_pages_count; i++) {
355 info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
356 info->ramblock_pages - 1);
357 info->hash_result[i] = get_ramblock_vfn_hash(info,
358 info->sample_page_vfn[i]);
360 g_rand_free(rand);
362 return true;
365 static void get_ramblock_dirty_info(RAMBlock *block,
366 struct RamblockDirtyInfo *info,
367 struct DirtyRateConfig *config)
369 uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
371 /* Right shift 30 bits to calc ramblock size in GB */
372 info->sample_pages_count = (qemu_ram_get_used_length(block) *
373 sample_pages_per_gigabytes) >> 30;
374 /* Right shift TARGET_PAGE_BITS to calc page count */
375 info->ramblock_pages = qemu_ram_get_used_length(block) >>
376 TARGET_PAGE_BITS;
377 info->ramblock_addr = qemu_ram_get_host_addr(block);
378 strcpy(info->idstr, qemu_ram_get_idstr(block));
381 static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
383 int i;
385 if (!infos) {
386 return;
389 for (i = 0; i < count; i++) {
390 g_free(infos[i].sample_page_vfn);
391 g_free(infos[i].hash_result);
393 g_free(infos);
396 static bool skip_sample_ramblock(RAMBlock *block)
399 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
401 if (qemu_ram_get_used_length(block) < (MIN_RAMBLOCK_SIZE << 10)) {
402 trace_skip_sample_ramblock(block->idstr,
403 qemu_ram_get_used_length(block));
404 return true;
407 return false;
410 static bool record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
411 struct DirtyRateConfig config,
412 int *block_count)
414 struct RamblockDirtyInfo *info = NULL;
415 struct RamblockDirtyInfo *dinfo = NULL;
416 RAMBlock *block = NULL;
417 int total_count = 0;
418 int index = 0;
419 bool ret = false;
421 RAMBLOCK_FOREACH_MIGRATABLE(block) {
422 if (skip_sample_ramblock(block)) {
423 continue;
425 total_count++;
428 dinfo = g_try_malloc0_n(total_count, sizeof(struct RamblockDirtyInfo));
429 if (dinfo == NULL) {
430 goto out;
433 RAMBLOCK_FOREACH_MIGRATABLE(block) {
434 if (skip_sample_ramblock(block)) {
435 continue;
437 if (index >= total_count) {
438 break;
440 info = &dinfo[index];
441 get_ramblock_dirty_info(block, info, &config);
442 if (!save_ramblock_hash(info)) {
443 goto out;
445 index++;
447 ret = true;
449 out:
450 *block_count = index;
451 *block_dinfo = dinfo;
452 return ret;
455 static void calc_page_dirty_rate(struct RamblockDirtyInfo *info)
457 uint32_t crc;
458 int i;
460 for (i = 0; i < info->sample_pages_count; i++) {
461 crc = get_ramblock_vfn_hash(info, info->sample_page_vfn[i]);
462 if (crc != info->hash_result[i]) {
463 trace_calc_page_dirty_rate(info->idstr, crc, info->hash_result[i]);
464 info->sample_dirty_count++;
469 static struct RamblockDirtyInfo *
470 find_block_matched(RAMBlock *block, int count,
471 struct RamblockDirtyInfo *infos)
473 int i;
475 for (i = 0; i < count; i++) {
476 if (!strcmp(infos[i].idstr, qemu_ram_get_idstr(block))) {
477 break;
481 if (i == count) {
482 return NULL;
485 if (infos[i].ramblock_addr != qemu_ram_get_host_addr(block) ||
486 infos[i].ramblock_pages !=
487 (qemu_ram_get_used_length(block) >> TARGET_PAGE_BITS)) {
488 trace_find_page_matched(block->idstr);
489 return NULL;
492 return &infos[i];
495 static bool compare_page_hash_info(struct RamblockDirtyInfo *info,
496 int block_count)
498 struct RamblockDirtyInfo *block_dinfo = NULL;
499 RAMBlock *block = NULL;
501 RAMBLOCK_FOREACH_MIGRATABLE(block) {
502 if (skip_sample_ramblock(block)) {
503 continue;
505 block_dinfo = find_block_matched(block, block_count, info);
506 if (block_dinfo == NULL) {
507 continue;
509 calc_page_dirty_rate(block_dinfo);
510 update_dirtyrate_stat(block_dinfo);
513 if (DirtyStat.page_sampling.total_sample_count == 0) {
514 return false;
517 return true;
520 static inline void record_dirtypages_bitmap(DirtyPageRecord *dirty_pages,
521 bool start)
523 if (start) {
524 dirty_pages->start_pages = total_dirty_pages;
525 } else {
526 dirty_pages->end_pages = total_dirty_pages;
530 static inline void dirtyrate_manual_reset_protect(void)
532 RAMBlock *block = NULL;
534 WITH_RCU_READ_LOCK_GUARD() {
535 RAMBLOCK_FOREACH_MIGRATABLE(block) {
536 memory_region_clear_dirty_bitmap(block->mr, 0,
537 block->used_length);
542 static void calculate_dirtyrate_dirty_bitmap(struct DirtyRateConfig config)
544 int64_t msec = 0;
545 int64_t start_time;
546 DirtyPageRecord dirty_pages;
548 qemu_mutex_lock_iothread();
549 memory_global_dirty_log_start(GLOBAL_DIRTY_DIRTY_RATE);
552 * 1'round of log sync may return all 1 bits with
553 * KVM_DIRTY_LOG_INITIALLY_SET enable
554 * skip it unconditionally and start dirty tracking
555 * from 2'round of log sync
557 memory_global_dirty_log_sync();
560 * reset page protect manually and unconditionally.
561 * this make sure kvm dirty log be cleared if
562 * KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE cap is enabled.
564 dirtyrate_manual_reset_protect();
565 qemu_mutex_unlock_iothread();
567 record_dirtypages_bitmap(&dirty_pages, true);
569 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
570 DirtyStat.start_time = start_time / 1000;
572 msec = config.sample_period_seconds * 1000;
573 msec = dirty_stat_wait(msec, start_time);
574 DirtyStat.calc_time = msec / 1000;
577 * do two things.
578 * 1. fetch dirty bitmap from kvm
579 * 2. stop dirty tracking
581 global_dirty_log_sync(GLOBAL_DIRTY_DIRTY_RATE, true);
583 record_dirtypages_bitmap(&dirty_pages, false);
585 DirtyStat.dirty_rate = do_calculate_dirtyrate(dirty_pages, msec);
588 static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config)
590 int64_t duration;
591 uint64_t dirtyrate = 0;
592 uint64_t dirtyrate_sum = 0;
593 int i = 0;
595 /* start log sync */
596 global_dirty_log_change(GLOBAL_DIRTY_DIRTY_RATE, true);
598 DirtyStat.start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
600 /* calculate vcpu dirtyrate */
601 duration = vcpu_calculate_dirtyrate(config.sample_period_seconds * 1000,
602 &DirtyStat.dirty_ring,
603 GLOBAL_DIRTY_DIRTY_RATE,
604 true);
606 DirtyStat.calc_time = duration / 1000;
608 /* calculate vm dirtyrate */
609 for (i = 0; i < DirtyStat.dirty_ring.nvcpu; i++) {
610 dirtyrate = DirtyStat.dirty_ring.rates[i].dirty_rate;
611 DirtyStat.dirty_ring.rates[i].dirty_rate = dirtyrate;
612 dirtyrate_sum += dirtyrate;
615 DirtyStat.dirty_rate = dirtyrate_sum;
618 static void calculate_dirtyrate_sample_vm(struct DirtyRateConfig config)
620 struct RamblockDirtyInfo *block_dinfo = NULL;
621 int block_count = 0;
622 int64_t msec = 0;
623 int64_t initial_time;
625 rcu_read_lock();
626 initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
627 if (!record_ramblock_hash_info(&block_dinfo, config, &block_count)) {
628 goto out;
630 rcu_read_unlock();
632 msec = config.sample_period_seconds * 1000;
633 msec = dirty_stat_wait(msec, initial_time);
634 DirtyStat.start_time = initial_time / 1000;
635 DirtyStat.calc_time = msec / 1000;
637 rcu_read_lock();
638 if (!compare_page_hash_info(block_dinfo, block_count)) {
639 goto out;
642 update_dirtyrate(msec);
644 out:
645 rcu_read_unlock();
646 free_ramblock_dirty_info(block_dinfo, block_count);
649 static void calculate_dirtyrate(struct DirtyRateConfig config)
651 if (config.mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) {
652 calculate_dirtyrate_dirty_bitmap(config);
653 } else if (config.mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) {
654 calculate_dirtyrate_dirty_ring(config);
655 } else {
656 calculate_dirtyrate_sample_vm(config);
659 trace_dirtyrate_calculate(DirtyStat.dirty_rate);
662 void *get_dirtyrate_thread(void *arg)
664 struct DirtyRateConfig config = *(struct DirtyRateConfig *)arg;
665 int ret;
666 rcu_register_thread();
668 ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_UNSTARTED,
669 DIRTY_RATE_STATUS_MEASURING);
670 if (ret == -1) {
671 error_report("change dirtyrate state failed.");
672 return NULL;
675 calculate_dirtyrate(config);
677 ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_MEASURING,
678 DIRTY_RATE_STATUS_MEASURED);
679 if (ret == -1) {
680 error_report("change dirtyrate state failed.");
683 rcu_unregister_thread();
684 return NULL;
687 void qmp_calc_dirty_rate(int64_t calc_time,
688 bool has_sample_pages,
689 int64_t sample_pages,
690 bool has_mode,
691 DirtyRateMeasureMode mode,
692 Error **errp)
694 static struct DirtyRateConfig config;
695 QemuThread thread;
696 int ret;
697 int64_t start_time;
700 * If the dirty rate is already being measured, don't attempt to start.
702 if (qatomic_read(&CalculatingState) == DIRTY_RATE_STATUS_MEASURING) {
703 error_setg(errp, "the dirty rate is already being measured.");
704 return;
707 if (!is_sample_period_valid(calc_time)) {
708 error_setg(errp, "calc-time is out of range[%d, %d].",
709 MIN_FETCH_DIRTYRATE_TIME_SEC,
710 MAX_FETCH_DIRTYRATE_TIME_SEC);
711 return;
714 if (!has_mode) {
715 mode = DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
718 if (has_sample_pages && mode != DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING) {
719 error_setg(errp, "sample-pages is used only in page-sampling mode");
720 return;
723 if (has_sample_pages) {
724 if (!is_sample_pages_valid(sample_pages)) {
725 error_setg(errp, "sample-pages is out of range[%d, %d].",
726 MIN_SAMPLE_PAGE_COUNT,
727 MAX_SAMPLE_PAGE_COUNT);
728 return;
730 } else {
731 sample_pages = DIRTYRATE_DEFAULT_SAMPLE_PAGES;
735 * dirty ring mode only works when kvm dirty ring is enabled.
736 * on the contrary, dirty bitmap mode is not.
738 if (((mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) &&
739 !kvm_dirty_ring_enabled()) ||
740 ((mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) &&
741 kvm_dirty_ring_enabled())) {
742 error_setg(errp, "mode %s is not enabled, use other method instead.",
743 DirtyRateMeasureMode_str(mode));
744 return;
748 * Init calculation state as unstarted.
750 ret = dirtyrate_set_state(&CalculatingState, CalculatingState,
751 DIRTY_RATE_STATUS_UNSTARTED);
752 if (ret == -1) {
753 error_setg(errp, "init dirty rate calculation state failed.");
754 return;
757 config.sample_period_seconds = calc_time;
758 config.sample_pages_per_gigabytes = sample_pages;
759 config.mode = mode;
761 cleanup_dirtyrate_stat(config);
764 * update dirty rate mode so that we can figure out what mode has
765 * been used in last calculation
767 dirtyrate_mode = mode;
769 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
770 init_dirtyrate_stat(start_time, config);
772 qemu_thread_create(&thread, "get_dirtyrate", get_dirtyrate_thread,
773 (void *)&config, QEMU_THREAD_DETACHED);
776 struct DirtyRateInfo *qmp_query_dirty_rate(Error **errp)
778 return query_dirty_rate_info();
781 void hmp_info_dirty_rate(Monitor *mon, const QDict *qdict)
783 DirtyRateInfo *info = query_dirty_rate_info();
785 monitor_printf(mon, "Status: %s\n",
786 DirtyRateStatus_str(info->status));
787 monitor_printf(mon, "Start Time: %"PRIi64" (ms)\n",
788 info->start_time);
789 if (info->mode == DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING) {
790 monitor_printf(mon, "Sample Pages: %"PRIu64" (per GB)\n",
791 info->sample_pages);
793 monitor_printf(mon, "Period: %"PRIi64" (sec)\n",
794 info->calc_time);
795 monitor_printf(mon, "Mode: %s\n",
796 DirtyRateMeasureMode_str(info->mode));
797 monitor_printf(mon, "Dirty rate: ");
798 if (info->has_dirty_rate) {
799 monitor_printf(mon, "%"PRIi64" (MB/s)\n", info->dirty_rate);
800 if (info->has_vcpu_dirty_rate) {
801 DirtyRateVcpuList *rate, *head = info->vcpu_dirty_rate;
802 for (rate = head; rate != NULL; rate = rate->next) {
803 monitor_printf(mon, "vcpu[%"PRIi64"], Dirty rate: %"PRIi64
804 " (MB/s)\n", rate->value->id,
805 rate->value->dirty_rate);
808 } else {
809 monitor_printf(mon, "(not ready)\n");
812 qapi_free_DirtyRateVcpuList(info->vcpu_dirty_rate);
813 g_free(info);
816 void hmp_calc_dirty_rate(Monitor *mon, const QDict *qdict)
818 int64_t sec = qdict_get_try_int(qdict, "second", 0);
819 int64_t sample_pages = qdict_get_try_int(qdict, "sample_pages_per_GB", -1);
820 bool has_sample_pages = (sample_pages != -1);
821 bool dirty_ring = qdict_get_try_bool(qdict, "dirty_ring", false);
822 bool dirty_bitmap = qdict_get_try_bool(qdict, "dirty_bitmap", false);
823 DirtyRateMeasureMode mode = DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
824 Error *err = NULL;
826 if (!sec) {
827 monitor_printf(mon, "Incorrect period length specified!\n");
828 return;
831 if (dirty_ring && dirty_bitmap) {
832 monitor_printf(mon, "Either dirty ring or dirty bitmap "
833 "can be specified!\n");
834 return;
837 if (dirty_bitmap) {
838 mode = DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP;
839 } else if (dirty_ring) {
840 mode = DIRTY_RATE_MEASURE_MODE_DIRTY_RING;
843 qmp_calc_dirty_rate(sec, has_sample_pages, sample_pages, true,
844 mode, &err);
845 if (err) {
846 hmp_handle_error(mon, err);
847 return;
850 monitor_printf(mon, "Starting dirty rate measurement with period %"PRIi64
851 " seconds\n", sec);
852 monitor_printf(mon, "[Please use 'info dirty_rate' to check results]\n");