tests/qtest: arm-cpu-features: Match tests to required accelerators
[qemu/kevin.git] / migration / dirtyrate.c
blob575d48c3970ad59f447c80c8d53e8d8bc1ca2238
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 <zlib.h>
15 #include "qapi/error.h"
16 #include "cpu.h"
17 #include "exec/ramblock.h"
18 #include "exec/ram_addr.h"
19 #include "qemu/rcu_queue.h"
20 #include "qemu/main-loop.h"
21 #include "qapi/qapi-commands-migration.h"
22 #include "ram.h"
23 #include "trace.h"
24 #include "dirtyrate.h"
25 #include "monitor/hmp.h"
26 #include "monitor/monitor.h"
27 #include "qapi/qmp/qdict.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/runstate.h"
30 #include "exec/memory.h"
33 * total_dirty_pages is procted by BQL and is used
34 * to stat dirty pages during the period of two
35 * memory_global_dirty_log_sync
37 uint64_t total_dirty_pages;
39 typedef struct DirtyPageRecord {
40 uint64_t start_pages;
41 uint64_t end_pages;
42 } DirtyPageRecord;
44 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
45 static struct DirtyRateStat DirtyStat;
46 static DirtyRateMeasureMode dirtyrate_mode =
47 DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
49 static int64_t dirty_stat_wait(int64_t msec, int64_t initial_time)
51 int64_t current_time;
53 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
54 if ((current_time - initial_time) >= msec) {
55 msec = current_time - initial_time;
56 } else {
57 g_usleep((msec + initial_time - current_time) * 1000);
60 return msec;
63 static inline void record_dirtypages(DirtyPageRecord *dirty_pages,
64 CPUState *cpu, bool start)
66 if (start) {
67 dirty_pages[cpu->cpu_index].start_pages = cpu->dirty_pages;
68 } else {
69 dirty_pages[cpu->cpu_index].end_pages = cpu->dirty_pages;
73 static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages,
74 int64_t calc_time_ms)
76 uint64_t memory_size_MB;
77 uint64_t increased_dirty_pages =
78 dirty_pages.end_pages - dirty_pages.start_pages;
80 memory_size_MB = (increased_dirty_pages * TARGET_PAGE_SIZE) >> 20;
82 return memory_size_MB * 1000 / calc_time_ms;
85 void global_dirty_log_change(unsigned int flag, bool start)
87 qemu_mutex_lock_iothread();
88 if (start) {
89 memory_global_dirty_log_start(flag);
90 } else {
91 memory_global_dirty_log_stop(flag);
93 qemu_mutex_unlock_iothread();
97 * global_dirty_log_sync
98 * 1. sync dirty log from kvm
99 * 2. stop dirty tracking if needed.
101 static void global_dirty_log_sync(unsigned int flag, bool one_shot)
103 qemu_mutex_lock_iothread();
104 memory_global_dirty_log_sync();
105 if (one_shot) {
106 memory_global_dirty_log_stop(flag);
108 qemu_mutex_unlock_iothread();
111 static DirtyPageRecord *vcpu_dirty_stat_alloc(VcpuStat *stat)
113 CPUState *cpu;
114 int nvcpu = 0;
116 CPU_FOREACH(cpu) {
117 nvcpu++;
120 stat->nvcpu = nvcpu;
121 stat->rates = g_new0(DirtyRateVcpu, nvcpu);
123 return g_new0(DirtyPageRecord, nvcpu);
126 static void vcpu_dirty_stat_collect(VcpuStat *stat,
127 DirtyPageRecord *records,
128 bool start)
130 CPUState *cpu;
132 CPU_FOREACH(cpu) {
133 record_dirtypages(records, cpu, start);
137 int64_t vcpu_calculate_dirtyrate(int64_t calc_time_ms,
138 VcpuStat *stat,
139 unsigned int flag,
140 bool one_shot)
142 DirtyPageRecord *records;
143 int64_t init_time_ms;
144 int64_t duration;
145 int64_t dirtyrate;
146 int i = 0;
147 unsigned int gen_id;
149 retry:
150 init_time_ms = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
152 cpu_list_lock();
153 gen_id = cpu_list_generation_id_get();
154 records = vcpu_dirty_stat_alloc(stat);
155 vcpu_dirty_stat_collect(stat, records, true);
156 cpu_list_unlock();
158 duration = dirty_stat_wait(calc_time_ms, init_time_ms);
160 global_dirty_log_sync(flag, one_shot);
162 cpu_list_lock();
163 if (gen_id != cpu_list_generation_id_get()) {
164 g_free(records);
165 g_free(stat->rates);
166 cpu_list_unlock();
167 goto retry;
169 vcpu_dirty_stat_collect(stat, records, false);
170 cpu_list_unlock();
172 for (i = 0; i < stat->nvcpu; i++) {
173 dirtyrate = do_calculate_dirtyrate(records[i], duration);
175 stat->rates[i].id = i;
176 stat->rates[i].dirty_rate = dirtyrate;
178 trace_dirtyrate_do_calculate_vcpu(i, dirtyrate);
181 g_free(records);
183 return duration;
186 static bool is_sample_period_valid(int64_t sec)
188 if (sec < MIN_FETCH_DIRTYRATE_TIME_SEC ||
189 sec > MAX_FETCH_DIRTYRATE_TIME_SEC) {
190 return false;
193 return true;
196 static bool is_sample_pages_valid(int64_t pages)
198 return pages >= MIN_SAMPLE_PAGE_COUNT &&
199 pages <= MAX_SAMPLE_PAGE_COUNT;
202 static int dirtyrate_set_state(int *state, int old_state, int new_state)
204 assert(new_state < DIRTY_RATE_STATUS__MAX);
205 trace_dirtyrate_set_state(DirtyRateStatus_str(new_state));
206 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
207 return 0;
208 } else {
209 return -1;
213 static struct DirtyRateInfo *query_dirty_rate_info(void)
215 int i;
216 int64_t dirty_rate = DirtyStat.dirty_rate;
217 struct DirtyRateInfo *info = g_new0(DirtyRateInfo, 1);
218 DirtyRateVcpuList *head = NULL, **tail = &head;
220 info->status = CalculatingState;
221 info->start_time = DirtyStat.start_time;
222 info->calc_time = DirtyStat.calc_time;
223 info->sample_pages = DirtyStat.sample_pages;
224 info->mode = dirtyrate_mode;
226 if (qatomic_read(&CalculatingState) == DIRTY_RATE_STATUS_MEASURED) {
227 info->has_dirty_rate = true;
228 info->dirty_rate = dirty_rate;
230 if (dirtyrate_mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) {
232 * set sample_pages with 0 to indicate page sampling
233 * isn't enabled
235 info->sample_pages = 0;
236 info->has_vcpu_dirty_rate = true;
237 for (i = 0; i < DirtyStat.dirty_ring.nvcpu; i++) {
238 DirtyRateVcpu *rate = g_new0(DirtyRateVcpu, 1);
239 rate->id = DirtyStat.dirty_ring.rates[i].id;
240 rate->dirty_rate = DirtyStat.dirty_ring.rates[i].dirty_rate;
241 QAPI_LIST_APPEND(tail, rate);
243 info->vcpu_dirty_rate = head;
246 if (dirtyrate_mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) {
247 info->sample_pages = 0;
251 trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState));
253 return info;
256 static void init_dirtyrate_stat(int64_t start_time,
257 struct DirtyRateConfig config)
259 DirtyStat.dirty_rate = -1;
260 DirtyStat.start_time = start_time;
261 DirtyStat.calc_time = config.sample_period_seconds;
262 DirtyStat.sample_pages = config.sample_pages_per_gigabytes;
264 switch (config.mode) {
265 case DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING:
266 DirtyStat.page_sampling.total_dirty_samples = 0;
267 DirtyStat.page_sampling.total_sample_count = 0;
268 DirtyStat.page_sampling.total_block_mem_MB = 0;
269 break;
270 case DIRTY_RATE_MEASURE_MODE_DIRTY_RING:
271 DirtyStat.dirty_ring.nvcpu = -1;
272 DirtyStat.dirty_ring.rates = NULL;
273 break;
274 default:
275 break;
279 static void cleanup_dirtyrate_stat(struct DirtyRateConfig config)
281 /* last calc-dirty-rate qmp use dirty ring mode */
282 if (dirtyrate_mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) {
283 free(DirtyStat.dirty_ring.rates);
284 DirtyStat.dirty_ring.rates = NULL;
288 static void update_dirtyrate_stat(struct RamblockDirtyInfo *info)
290 DirtyStat.page_sampling.total_dirty_samples += info->sample_dirty_count;
291 DirtyStat.page_sampling.total_sample_count += info->sample_pages_count;
292 /* size of total pages in MB */
293 DirtyStat.page_sampling.total_block_mem_MB += (info->ramblock_pages *
294 TARGET_PAGE_SIZE) >> 20;
297 static void update_dirtyrate(uint64_t msec)
299 uint64_t dirtyrate;
300 uint64_t total_dirty_samples = DirtyStat.page_sampling.total_dirty_samples;
301 uint64_t total_sample_count = DirtyStat.page_sampling.total_sample_count;
302 uint64_t total_block_mem_MB = DirtyStat.page_sampling.total_block_mem_MB;
304 dirtyrate = total_dirty_samples * total_block_mem_MB *
305 1000 / (total_sample_count * msec);
307 DirtyStat.dirty_rate = dirtyrate;
311 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
312 * in ramblock, which starts from ramblock base address.
314 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
315 uint64_t vfn)
317 uint32_t crc;
319 crc = crc32(0, (info->ramblock_addr +
320 vfn * TARGET_PAGE_SIZE), TARGET_PAGE_SIZE);
322 trace_get_ramblock_vfn_hash(info->idstr, vfn, crc);
323 return crc;
326 static bool save_ramblock_hash(struct RamblockDirtyInfo *info)
328 unsigned int sample_pages_count;
329 int i;
330 GRand *rand;
332 sample_pages_count = info->sample_pages_count;
334 /* ramblock size less than one page, return success to skip this ramblock */
335 if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
336 return true;
339 info->hash_result = g_try_malloc0_n(sample_pages_count,
340 sizeof(uint32_t));
341 if (!info->hash_result) {
342 return false;
345 info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
346 sizeof(uint64_t));
347 if (!info->sample_page_vfn) {
348 g_free(info->hash_result);
349 return false;
352 rand = g_rand_new();
353 for (i = 0; i < sample_pages_count; i++) {
354 info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
355 info->ramblock_pages - 1);
356 info->hash_result[i] = get_ramblock_vfn_hash(info,
357 info->sample_page_vfn[i]);
359 g_rand_free(rand);
361 return true;
364 static void get_ramblock_dirty_info(RAMBlock *block,
365 struct RamblockDirtyInfo *info,
366 struct DirtyRateConfig *config)
368 uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
370 /* Right shift 30 bits to calc ramblock size in GB */
371 info->sample_pages_count = (qemu_ram_get_used_length(block) *
372 sample_pages_per_gigabytes) >> 30;
373 /* Right shift TARGET_PAGE_BITS to calc page count */
374 info->ramblock_pages = qemu_ram_get_used_length(block) >>
375 TARGET_PAGE_BITS;
376 info->ramblock_addr = qemu_ram_get_host_addr(block);
377 strcpy(info->idstr, qemu_ram_get_idstr(block));
380 static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
382 int i;
384 if (!infos) {
385 return;
388 for (i = 0; i < count; i++) {
389 g_free(infos[i].sample_page_vfn);
390 g_free(infos[i].hash_result);
392 g_free(infos);
395 static bool skip_sample_ramblock(RAMBlock *block)
398 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
400 if (qemu_ram_get_used_length(block) < (MIN_RAMBLOCK_SIZE << 10)) {
401 trace_skip_sample_ramblock(block->idstr,
402 qemu_ram_get_used_length(block));
403 return true;
406 return false;
409 static bool record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
410 struct DirtyRateConfig config,
411 int *block_count)
413 struct RamblockDirtyInfo *info = NULL;
414 struct RamblockDirtyInfo *dinfo = NULL;
415 RAMBlock *block = NULL;
416 int total_count = 0;
417 int index = 0;
418 bool ret = false;
420 RAMBLOCK_FOREACH_MIGRATABLE(block) {
421 if (skip_sample_ramblock(block)) {
422 continue;
424 total_count++;
427 dinfo = g_try_malloc0_n(total_count, sizeof(struct RamblockDirtyInfo));
428 if (dinfo == NULL) {
429 goto out;
432 RAMBLOCK_FOREACH_MIGRATABLE(block) {
433 if (skip_sample_ramblock(block)) {
434 continue;
436 if (index >= total_count) {
437 break;
439 info = &dinfo[index];
440 get_ramblock_dirty_info(block, info, &config);
441 if (!save_ramblock_hash(info)) {
442 goto out;
444 index++;
446 ret = true;
448 out:
449 *block_count = index;
450 *block_dinfo = dinfo;
451 return ret;
454 static void calc_page_dirty_rate(struct RamblockDirtyInfo *info)
456 uint32_t crc;
457 int i;
459 for (i = 0; i < info->sample_pages_count; i++) {
460 crc = get_ramblock_vfn_hash(info, info->sample_page_vfn[i]);
461 if (crc != info->hash_result[i]) {
462 trace_calc_page_dirty_rate(info->idstr, crc, info->hash_result[i]);
463 info->sample_dirty_count++;
468 static struct RamblockDirtyInfo *
469 find_block_matched(RAMBlock *block, int count,
470 struct RamblockDirtyInfo *infos)
472 int i;
474 for (i = 0; i < count; i++) {
475 if (!strcmp(infos[i].idstr, qemu_ram_get_idstr(block))) {
476 break;
480 if (i == count) {
481 return NULL;
484 if (infos[i].ramblock_addr != qemu_ram_get_host_addr(block) ||
485 infos[i].ramblock_pages !=
486 (qemu_ram_get_used_length(block) >> TARGET_PAGE_BITS)) {
487 trace_find_page_matched(block->idstr);
488 return NULL;
491 return &infos[i];
494 static bool compare_page_hash_info(struct RamblockDirtyInfo *info,
495 int block_count)
497 struct RamblockDirtyInfo *block_dinfo = NULL;
498 RAMBlock *block = NULL;
500 RAMBLOCK_FOREACH_MIGRATABLE(block) {
501 if (skip_sample_ramblock(block)) {
502 continue;
504 block_dinfo = find_block_matched(block, block_count, info);
505 if (block_dinfo == NULL) {
506 continue;
508 calc_page_dirty_rate(block_dinfo);
509 update_dirtyrate_stat(block_dinfo);
512 if (DirtyStat.page_sampling.total_sample_count == 0) {
513 return false;
516 return true;
519 static inline void record_dirtypages_bitmap(DirtyPageRecord *dirty_pages,
520 bool start)
522 if (start) {
523 dirty_pages->start_pages = total_dirty_pages;
524 } else {
525 dirty_pages->end_pages = total_dirty_pages;
529 static inline void dirtyrate_manual_reset_protect(void)
531 RAMBlock *block = NULL;
533 WITH_RCU_READ_LOCK_GUARD() {
534 RAMBLOCK_FOREACH_MIGRATABLE(block) {
535 memory_region_clear_dirty_bitmap(block->mr, 0,
536 block->used_length);
541 static void calculate_dirtyrate_dirty_bitmap(struct DirtyRateConfig config)
543 int64_t msec = 0;
544 int64_t start_time;
545 DirtyPageRecord dirty_pages;
547 qemu_mutex_lock_iothread();
548 memory_global_dirty_log_start(GLOBAL_DIRTY_DIRTY_RATE);
551 * 1'round of log sync may return all 1 bits with
552 * KVM_DIRTY_LOG_INITIALLY_SET enable
553 * skip it unconditionally and start dirty tracking
554 * from 2'round of log sync
556 memory_global_dirty_log_sync();
559 * reset page protect manually and unconditionally.
560 * this make sure kvm dirty log be cleared if
561 * KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE cap is enabled.
563 dirtyrate_manual_reset_protect();
564 qemu_mutex_unlock_iothread();
566 record_dirtypages_bitmap(&dirty_pages, true);
568 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
569 DirtyStat.start_time = start_time / 1000;
571 msec = config.sample_period_seconds * 1000;
572 msec = dirty_stat_wait(msec, start_time);
573 DirtyStat.calc_time = msec / 1000;
576 * do two things.
577 * 1. fetch dirty bitmap from kvm
578 * 2. stop dirty tracking
580 global_dirty_log_sync(GLOBAL_DIRTY_DIRTY_RATE, true);
582 record_dirtypages_bitmap(&dirty_pages, false);
584 DirtyStat.dirty_rate = do_calculate_dirtyrate(dirty_pages, msec);
587 static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config)
589 int64_t duration;
590 uint64_t dirtyrate = 0;
591 uint64_t dirtyrate_sum = 0;
592 int i = 0;
594 /* start log sync */
595 global_dirty_log_change(GLOBAL_DIRTY_DIRTY_RATE, true);
597 DirtyStat.start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
599 /* calculate vcpu dirtyrate */
600 duration = vcpu_calculate_dirtyrate(config.sample_period_seconds * 1000,
601 &DirtyStat.dirty_ring,
602 GLOBAL_DIRTY_DIRTY_RATE,
603 true);
605 DirtyStat.calc_time = duration / 1000;
607 /* calculate vm dirtyrate */
608 for (i = 0; i < DirtyStat.dirty_ring.nvcpu; i++) {
609 dirtyrate = DirtyStat.dirty_ring.rates[i].dirty_rate;
610 DirtyStat.dirty_ring.rates[i].dirty_rate = dirtyrate;
611 dirtyrate_sum += dirtyrate;
614 DirtyStat.dirty_rate = dirtyrate_sum;
617 static void calculate_dirtyrate_sample_vm(struct DirtyRateConfig config)
619 struct RamblockDirtyInfo *block_dinfo = NULL;
620 int block_count = 0;
621 int64_t msec = 0;
622 int64_t initial_time;
624 rcu_read_lock();
625 initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
626 if (!record_ramblock_hash_info(&block_dinfo, config, &block_count)) {
627 goto out;
629 rcu_read_unlock();
631 msec = config.sample_period_seconds * 1000;
632 msec = dirty_stat_wait(msec, initial_time);
633 DirtyStat.start_time = initial_time / 1000;
634 DirtyStat.calc_time = msec / 1000;
636 rcu_read_lock();
637 if (!compare_page_hash_info(block_dinfo, block_count)) {
638 goto out;
641 update_dirtyrate(msec);
643 out:
644 rcu_read_unlock();
645 free_ramblock_dirty_info(block_dinfo, block_count);
648 static void calculate_dirtyrate(struct DirtyRateConfig config)
650 if (config.mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) {
651 calculate_dirtyrate_dirty_bitmap(config);
652 } else if (config.mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) {
653 calculate_dirtyrate_dirty_ring(config);
654 } else {
655 calculate_dirtyrate_sample_vm(config);
658 trace_dirtyrate_calculate(DirtyStat.dirty_rate);
661 void *get_dirtyrate_thread(void *arg)
663 struct DirtyRateConfig config = *(struct DirtyRateConfig *)arg;
664 int ret;
665 rcu_register_thread();
667 ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_UNSTARTED,
668 DIRTY_RATE_STATUS_MEASURING);
669 if (ret == -1) {
670 error_report("change dirtyrate state failed.");
671 return NULL;
674 calculate_dirtyrate(config);
676 ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_MEASURING,
677 DIRTY_RATE_STATUS_MEASURED);
678 if (ret == -1) {
679 error_report("change dirtyrate state failed.");
682 rcu_unregister_thread();
683 return NULL;
686 void qmp_calc_dirty_rate(int64_t calc_time,
687 bool has_sample_pages,
688 int64_t sample_pages,
689 bool has_mode,
690 DirtyRateMeasureMode mode,
691 Error **errp)
693 static struct DirtyRateConfig config;
694 QemuThread thread;
695 int ret;
696 int64_t start_time;
699 * If the dirty rate is already being measured, don't attempt to start.
701 if (qatomic_read(&CalculatingState) == DIRTY_RATE_STATUS_MEASURING) {
702 error_setg(errp, "the dirty rate is already being measured.");
703 return;
706 if (!is_sample_period_valid(calc_time)) {
707 error_setg(errp, "calc-time is out of range[%d, %d].",
708 MIN_FETCH_DIRTYRATE_TIME_SEC,
709 MAX_FETCH_DIRTYRATE_TIME_SEC);
710 return;
713 if (!has_mode) {
714 mode = DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
717 if (has_sample_pages && mode != DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING) {
718 error_setg(errp, "sample-pages is used only in page-sampling mode");
719 return;
722 if (has_sample_pages) {
723 if (!is_sample_pages_valid(sample_pages)) {
724 error_setg(errp, "sample-pages is out of range[%d, %d].",
725 MIN_SAMPLE_PAGE_COUNT,
726 MAX_SAMPLE_PAGE_COUNT);
727 return;
729 } else {
730 sample_pages = DIRTYRATE_DEFAULT_SAMPLE_PAGES;
734 * dirty ring mode only works when kvm dirty ring is enabled.
735 * on the contrary, dirty bitmap mode is not.
737 if (((mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) &&
738 !kvm_dirty_ring_enabled()) ||
739 ((mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) &&
740 kvm_dirty_ring_enabled())) {
741 error_setg(errp, "mode %s is not enabled, use other method instead.",
742 DirtyRateMeasureMode_str(mode));
743 return;
747 * Init calculation state as unstarted.
749 ret = dirtyrate_set_state(&CalculatingState, CalculatingState,
750 DIRTY_RATE_STATUS_UNSTARTED);
751 if (ret == -1) {
752 error_setg(errp, "init dirty rate calculation state failed.");
753 return;
756 config.sample_period_seconds = calc_time;
757 config.sample_pages_per_gigabytes = sample_pages;
758 config.mode = mode;
760 cleanup_dirtyrate_stat(config);
763 * update dirty rate mode so that we can figure out what mode has
764 * been used in last calculation
766 dirtyrate_mode = mode;
768 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
769 init_dirtyrate_stat(start_time, config);
771 qemu_thread_create(&thread, "get_dirtyrate", get_dirtyrate_thread,
772 (void *)&config, QEMU_THREAD_DETACHED);
775 struct DirtyRateInfo *qmp_query_dirty_rate(Error **errp)
777 return query_dirty_rate_info();
780 void hmp_info_dirty_rate(Monitor *mon, const QDict *qdict)
782 DirtyRateInfo *info = query_dirty_rate_info();
784 monitor_printf(mon, "Status: %s\n",
785 DirtyRateStatus_str(info->status));
786 monitor_printf(mon, "Start Time: %"PRIi64" (ms)\n",
787 info->start_time);
788 if (info->mode == DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING) {
789 monitor_printf(mon, "Sample Pages: %"PRIu64" (per GB)\n",
790 info->sample_pages);
792 monitor_printf(mon, "Period: %"PRIi64" (sec)\n",
793 info->calc_time);
794 monitor_printf(mon, "Mode: %s\n",
795 DirtyRateMeasureMode_str(info->mode));
796 monitor_printf(mon, "Dirty rate: ");
797 if (info->has_dirty_rate) {
798 monitor_printf(mon, "%"PRIi64" (MB/s)\n", info->dirty_rate);
799 if (info->has_vcpu_dirty_rate) {
800 DirtyRateVcpuList *rate, *head = info->vcpu_dirty_rate;
801 for (rate = head; rate != NULL; rate = rate->next) {
802 monitor_printf(mon, "vcpu[%"PRIi64"], Dirty rate: %"PRIi64
803 " (MB/s)\n", rate->value->id,
804 rate->value->dirty_rate);
807 } else {
808 monitor_printf(mon, "(not ready)\n");
811 qapi_free_DirtyRateVcpuList(info->vcpu_dirty_rate);
812 g_free(info);
815 void hmp_calc_dirty_rate(Monitor *mon, const QDict *qdict)
817 int64_t sec = qdict_get_try_int(qdict, "second", 0);
818 int64_t sample_pages = qdict_get_try_int(qdict, "sample_pages_per_GB", -1);
819 bool has_sample_pages = (sample_pages != -1);
820 bool dirty_ring = qdict_get_try_bool(qdict, "dirty_ring", false);
821 bool dirty_bitmap = qdict_get_try_bool(qdict, "dirty_bitmap", false);
822 DirtyRateMeasureMode mode = DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
823 Error *err = NULL;
825 if (!sec) {
826 monitor_printf(mon, "Incorrect period length specified!\n");
827 return;
830 if (dirty_ring && dirty_bitmap) {
831 monitor_printf(mon, "Either dirty ring or dirty bitmap "
832 "can be specified!\n");
833 return;
836 if (dirty_bitmap) {
837 mode = DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP;
838 } else if (dirty_ring) {
839 mode = DIRTY_RATE_MEASURE_MODE_DIRTY_RING;
842 qmp_calc_dirty_rate(sec, has_sample_pages, sample_pages, true,
843 mode, &err);
844 if (err) {
845 hmp_handle_error(mon, err);
846 return;
849 monitor_printf(mon, "Starting dirty rate measurement with period %"PRIi64
850 " seconds\n", sec);
851 monitor_printf(mon, "[Please use 'info dirty_rate' to check results]\n");