]> git.proxmox.com Git - mirror_qemu.git/blame - migration/dirtyrate.c
Merge tag 'pull-block-2023-09-01' of https://gitlab.com/hreitz/qemu into staging
[mirror_qemu.git] / migration / dirtyrate.c
CommitLineData
4240dcee
CZ
1/*
2 * Dirtyrate implement code
3 *
4 * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO.,LTD.
5 *
6 * Authors:
7 * Chuan Zheng <zhengchuan@huawei.com>
8 *
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.
11 */
12
13#include "qemu/osdep.h"
cc37d98b 14#include "qemu/error-report.h"
662770af 15#include <zlib.h>
1e05888a 16#include "hw/core/cpu.h"
4240dcee 17#include "qapi/error.h"
4240dcee 18#include "exec/ramblock.h"
beeda9b7 19#include "exec/target_page.h"
4240dcee 20#include "qemu/rcu_queue.h"
0e21bf24 21#include "qemu/main-loop.h"
4240dcee 22#include "qapi/qapi-commands-migration.h"
3ded54b1 23#include "ram.h"
3c0b5dff 24#include "trace.h"
4240dcee 25#include "dirtyrate.h"
a4a571d9
PX
26#include "monitor/hmp.h"
27#include "monitor/monitor.h"
28#include "qapi/qmp/qdict.h"
0e21bf24
HH
29#include "sysemu/kvm.h"
30#include "sysemu/runstate.h"
31#include "exec/memory.h"
00a3f9c6 32#include "qemu/xxhash.h"
0e21bf24 33
4998a37e
HH
34/*
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
38 */
39uint64_t total_dirty_pages;
40
0e21bf24
HH
41typedef struct DirtyPageRecord {
42 uint64_t start_pages;
43 uint64_t end_pages;
44} DirtyPageRecord;
4240dcee 45
7df3aa30 46static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
c9a58d71 47static struct DirtyRateStat DirtyStat;
0e21bf24
HH
48static DirtyRateMeasureMode dirtyrate_mode =
49 DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
7df3aa30 50
8244166d 51static int64_t dirty_stat_wait(int64_t msec, int64_t initial_time)
eca58224
CZ
52{
53 int64_t current_time;
54
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);
3eb82637
AG
60 /* g_usleep may overshoot */
61 msec = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - initial_time;
eca58224
CZ
62 }
63
64 return msec;
65}
66
8244166d
HH
67static inline void record_dirtypages(DirtyPageRecord *dirty_pages,
68 CPUState *cpu, bool start)
69{
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;
74 }
75}
76
77static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages,
78 int64_t calc_time_ms)
79{
8244166d
HH
80 uint64_t increased_dirty_pages =
81 dirty_pages.end_pages - dirty_pages.start_pages;
82
3eb82637
AG
83 /*
84 * multiply by 1000ms/s _before_ converting down to megabytes
85 * to avoid losing precision
86 */
87 return qemu_target_pages_to_MiB(increased_dirty_pages * 1000) /
88 calc_time_ms;
8244166d
HH
89}
90
91void global_dirty_log_change(unsigned int flag, bool start)
92{
93 qemu_mutex_lock_iothread();
94 if (start) {
95 memory_global_dirty_log_start(flag);
96 } else {
97 memory_global_dirty_log_stop(flag);
98 }
99 qemu_mutex_unlock_iothread();
100}
101
102/*
103 * global_dirty_log_sync
104 * 1. sync dirty log from kvm
105 * 2. stop dirty tracking if needed.
106 */
107static void global_dirty_log_sync(unsigned int flag, bool one_shot)
108{
109 qemu_mutex_lock_iothread();
1e493be5 110 memory_global_dirty_log_sync(false);
8244166d
HH
111 if (one_shot) {
112 memory_global_dirty_log_stop(flag);
113 }
114 qemu_mutex_unlock_iothread();
115}
116
117static DirtyPageRecord *vcpu_dirty_stat_alloc(VcpuStat *stat)
118{
119 CPUState *cpu;
8244166d
HH
120 int nvcpu = 0;
121
122 CPU_FOREACH(cpu) {
123 nvcpu++;
124 }
125
126 stat->nvcpu = nvcpu;
c5e8d518 127 stat->rates = g_new0(DirtyRateVcpu, nvcpu);
8244166d 128
66997c42 129 return g_new0(DirtyPageRecord, nvcpu);
8244166d
HH
130}
131
132static void vcpu_dirty_stat_collect(VcpuStat *stat,
133 DirtyPageRecord *records,
134 bool start)
135{
136 CPUState *cpu;
137
138 CPU_FOREACH(cpu) {
139 record_dirtypages(records, cpu, start);
140 }
141}
142
143int64_t vcpu_calculate_dirtyrate(int64_t calc_time_ms,
144 VcpuStat *stat,
145 unsigned int flag,
146 bool one_shot)
147{
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;
154
155retry:
156 init_time_ms = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
157
370ed600
JI
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);
162 }
8244166d
HH
163
164 duration = dirty_stat_wait(calc_time_ms, init_time_ms);
165
166 global_dirty_log_sync(flag, one_shot);
167
370ed600
JI
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;
174 }
175 vcpu_dirty_stat_collect(stat, records, false);
8244166d 176 }
8244166d
HH
177
178 for (i = 0; i < stat->nvcpu; i++) {
179 dirtyrate = do_calculate_dirtyrate(records[i], duration);
180
181 stat->rates[i].id = i;
182 stat->rates[i].dirty_rate = dirtyrate;
183
184 trace_dirtyrate_do_calculate_vcpu(i, dirtyrate);
185 }
186
187 g_free(records);
188
189 return duration;
190}
191
eca58224
CZ
192static bool is_sample_period_valid(int64_t sec)
193{
194 if (sec < MIN_FETCH_DIRTYRATE_TIME_SEC ||
195 sec > MAX_FETCH_DIRTYRATE_TIME_SEC) {
196 return false;
197 }
198
199 return true;
200}
201
7afa08cd
HH
202static bool is_sample_pages_valid(int64_t pages)
203{
204 return pages >= MIN_SAMPLE_PAGE_COUNT &&
205 pages <= MAX_SAMPLE_PAGE_COUNT;
206}
207
7df3aa30
CZ
208static int dirtyrate_set_state(int *state, int old_state, int new_state)
209{
210 assert(new_state < DIRTY_RATE_STATUS__MAX);
3c0b5dff 211 trace_dirtyrate_set_state(DirtyRateStatus_str(new_state));
7df3aa30
CZ
212 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
213 return 0;
214 } else {
215 return -1;
216 }
217}
218
4c437254
CZ
219static struct DirtyRateInfo *query_dirty_rate_info(void)
220{
0e21bf24 221 int i;
4c437254 222 int64_t dirty_rate = DirtyStat.dirty_rate;
b21e2380 223 struct DirtyRateInfo *info = g_new0(DirtyRateInfo, 1);
0e21bf24 224 DirtyRateVcpuList *head = NULL, **tail = &head;
4c437254
CZ
225
226 info->status = CalculatingState;
227 info->start_time = DirtyStat.start_time;
228 info->calc_time = DirtyStat.calc_time;
7afa08cd 229 info->sample_pages = DirtyStat.sample_pages;
0e21bf24
HH
230 info->mode = dirtyrate_mode;
231
232 if (qatomic_read(&CalculatingState) == DIRTY_RATE_STATUS_MEASURED) {
233 info->has_dirty_rate = true;
234 info->dirty_rate = dirty_rate;
235
236 if (dirtyrate_mode == DIRTY_RATE_MEASURE_MODE_DIRTY_RING) {
237 /*
238 * set sample_pages with 0 to indicate page sampling
239 * isn't enabled
240 **/
241 info->sample_pages = 0;
242 info->has_vcpu_dirty_rate = true;
243 for (i = 0; i < DirtyStat.dirty_ring.nvcpu; i++) {
b21e2380 244 DirtyRateVcpu *rate = g_new0(DirtyRateVcpu, 1);
0e21bf24
HH
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);
248 }
249 info->vcpu_dirty_rate = head;
250 }
826b8bc8
HH
251
252 if (dirtyrate_mode == DIRTY_RATE_MEASURE_MODE_DIRTY_BITMAP) {
253 info->sample_pages = 0;
254 }
0e21bf24 255 }
4c437254 256
3c0b5dff
CZ
257 trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState));
258
4c437254
CZ
259 return info;
260}
261
71864ead
HH
262static void init_dirtyrate_stat(int64_t start_time,
263 struct DirtyRateConfig config)
c9a58d71 264{
c9a58d71 265 DirtyStat.dirty_rate = -1;
aa84b506 266 DirtyStat.start_time = start_time;
71864ead
HH
267 DirtyStat.calc_time = config.sample_period_seconds;
268 DirtyStat.sample_pages = config.sample_pages_per_gigabytes;
269
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;
282 }
c9a58d71
CZ
283}
284
0e21bf24
HH
285static void cleanup_dirtyrate_stat(struct DirtyRateConfig config)
286{
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;
291 }
292}
293
c9a58d71
CZ
294static void update_dirtyrate_stat(struct RamblockDirtyInfo *info)
295{
71864ead
HH
296 DirtyStat.page_sampling.total_dirty_samples += info->sample_dirty_count;
297 DirtyStat.page_sampling.total_sample_count += info->sample_pages_count;
c9a58d71 298 /* size of total pages in MB */
beeda9b7
JQ
299 DirtyStat.page_sampling.total_block_mem_MB +=
300 qemu_target_pages_to_MiB(info->ramblock_pages);
c9a58d71
CZ
301}
302
303static void update_dirtyrate(uint64_t msec)
304{
305 uint64_t dirtyrate;
71864ead
HH
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;
c9a58d71
CZ
309
310 dirtyrate = total_dirty_samples * total_block_mem_MB *
311 1000 / (total_sample_count * msec);
312
313 DirtyStat.dirty_rate = dirtyrate;
314}
7df3aa30 315
00a3f9c6
AG
316/*
317 * Compute hash of a single page of size TARGET_PAGE_SIZE.
318 */
319static uint32_t compute_page_hash(void *ptr)
320{
edd83a70 321 size_t page_size = qemu_target_page_size();
00a3f9c6
AG
322 uint32_t i;
323 uint64_t v1, v2, v3, v4;
324 uint64_t res;
325 const uint64_t *p = ptr;
326
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;
edd83a70 331 for (i = 0; i < page_size / 8; i += 4) {
00a3f9c6
AG
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]);
336 }
337 res = XXH64_mergerounds(v1, v2, v3, v4);
edd83a70 338 res += page_size;
00a3f9c6
AG
339 res = XXH64_avalanche(res);
340 return (uint32_t)(res & UINT32_MAX);
341}
342
343
ba0e519f
CZ
344/*
345 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
346 * in ramblock, which starts from ramblock base address.
347 */
348static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
349 uint64_t vfn)
350{
00a3f9c6 351 uint32_t hash;
ba0e519f 352
edd83a70
JQ
353 hash = compute_page_hash(info->ramblock_addr +
354 vfn * qemu_target_page_size());
ba0e519f 355
00a3f9c6
AG
356 trace_get_ramblock_vfn_hash(info->idstr, vfn, hash);
357 return hash;
ba0e519f
CZ
358}
359
360static bool save_ramblock_hash(struct RamblockDirtyInfo *info)
361{
362 unsigned int sample_pages_count;
363 int i;
364 GRand *rand;
365
366 sample_pages_count = info->sample_pages_count;
367
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;
371 }
372
373 info->hash_result = g_try_malloc0_n(sample_pages_count,
374 sizeof(uint32_t));
375 if (!info->hash_result) {
376 return false;
377 }
378
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;
384 }
385
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]);
392 }
393 g_rand_free(rand);
394
395 return true;
396}
397
398static void get_ramblock_dirty_info(RAMBlock *block,
399 struct RamblockDirtyInfo *info,
400 struct DirtyRateConfig *config)
401{
402 uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
403
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) >>
148b1ad8 409 qemu_target_page_bits();
ba0e519f
CZ
410 info->ramblock_addr = qemu_ram_get_host_addr(block);
411 strcpy(info->idstr, qemu_ram_get_idstr(block));
412}
413
cf0bbb49
CZ
414static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
415{
416 int i;
417
418 if (!infos) {
419 return;
420 }
421
422 for (i = 0; i < count; i++) {
423 g_free(infos[i].sample_page_vfn);
424 g_free(infos[i].hash_result);
425 }
426 g_free(infos);
427}
428
f82583cd
CZ
429static bool skip_sample_ramblock(RAMBlock *block)
430{
431 /*
432 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
433 */
434 if (qemu_ram_get_used_length(block) < (MIN_RAMBLOCK_SIZE << 10)) {
3c0b5dff
CZ
435 trace_skip_sample_ramblock(block->idstr,
436 qemu_ram_get_used_length(block));
f82583cd
CZ
437 return true;
438 }
439
440 return false;
441}
442
ba0e519f
CZ
443static bool record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
444 struct DirtyRateConfig config,
445 int *block_count)
446{
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;
453
454 RAMBLOCK_FOREACH_MIGRATABLE(block) {
f82583cd
CZ
455 if (skip_sample_ramblock(block)) {
456 continue;
457 }
ba0e519f
CZ
458 total_count++;
459 }
460
461 dinfo = g_try_malloc0_n(total_count, sizeof(struct RamblockDirtyInfo));
462 if (dinfo == NULL) {
463 goto out;
464 }
465
466 RAMBLOCK_FOREACH_MIGRATABLE(block) {
f82583cd
CZ
467 if (skip_sample_ramblock(block)) {
468 continue;
469 }
ba0e519f
CZ
470 if (index >= total_count) {
471 break;
472 }
473 info = &dinfo[index];
474 get_ramblock_dirty_info(block, info, &config);
475 if (!save_ramblock_hash(info)) {
476 goto out;
477 }
478 index++;
479 }
480 ret = true;
481
482out:
483 *block_count = index;
484 *block_dinfo = dinfo;
485 return ret;
486}
487
9c04387b
CZ
488static void calc_page_dirty_rate(struct RamblockDirtyInfo *info)
489{
00a3f9c6 490 uint32_t hash;
9c04387b
CZ
491 int i;
492
493 for (i = 0; i < info->sample_pages_count; i++) {
00a3f9c6
AG
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]);
9c04387b
CZ
497 info->sample_dirty_count++;
498 }
499 }
500}
501
502static struct RamblockDirtyInfo *
503find_block_matched(RAMBlock *block, int count,
504 struct RamblockDirtyInfo *infos)
505{
506 int i;
9c04387b
CZ
507
508 for (i = 0; i < count; i++) {
509 if (!strcmp(infos[i].idstr, qemu_ram_get_idstr(block))) {
510 break;
511 }
512 }
513
514 if (i == count) {
515 return NULL;
516 }
517
518 if (infos[i].ramblock_addr != qemu_ram_get_host_addr(block) ||
519 infos[i].ramblock_pages !=
148b1ad8 520 (qemu_ram_get_used_length(block) >> qemu_target_page_bits())) {
3c0b5dff 521 trace_find_page_matched(block->idstr);
9c04387b
CZ
522 return NULL;
523 }
524
66997c42 525 return &infos[i];
9c04387b
CZ
526}
527
528static bool compare_page_hash_info(struct RamblockDirtyInfo *info,
529 int block_count)
530{
531 struct RamblockDirtyInfo *block_dinfo = NULL;
532 RAMBlock *block = NULL;
533
534 RAMBLOCK_FOREACH_MIGRATABLE(block) {
f82583cd
CZ
535 if (skip_sample_ramblock(block)) {
536 continue;
537 }
9c04387b
CZ
538 block_dinfo = find_block_matched(block, block_count, info);
539 if (block_dinfo == NULL) {
540 continue;
541 }
542 calc_page_dirty_rate(block_dinfo);
543 update_dirtyrate_stat(block_dinfo);
544 }
545
71864ead 546 if (DirtyStat.page_sampling.total_sample_count == 0) {
9c04387b
CZ
547 return false;
548 }
549
550 return true;
551}
552
826b8bc8
HH
553static inline void record_dirtypages_bitmap(DirtyPageRecord *dirty_pages,
554 bool start)
555{
556 if (start) {
557 dirty_pages->start_pages = total_dirty_pages;
558 } else {
559 dirty_pages->end_pages = total_dirty_pages;
560 }
561}
562
826b8bc8
HH
563static inline void dirtyrate_manual_reset_protect(void)
564{
565 RAMBlock *block = NULL;
566
567 WITH_RCU_READ_LOCK_GUARD() {
568 RAMBLOCK_FOREACH_MIGRATABLE(block) {
569 memory_region_clear_dirty_bitmap(block->mr, 0,
570 block->used_length);
571 }
572 }
573}
574
575static void calculate_dirtyrate_dirty_bitmap(struct DirtyRateConfig config)
576{
577 int64_t msec = 0;
578 int64_t start_time;
579 DirtyPageRecord dirty_pages;
580
581 qemu_mutex_lock_iothread();
582 memory_global_dirty_log_start(GLOBAL_DIRTY_DIRTY_RATE);
583
584 /*
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
589 */
1e493be5 590 memory_global_dirty_log_sync(false);
826b8bc8
HH
591
592 /*
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.
596 */
597 dirtyrate_manual_reset_protect();
598 qemu_mutex_unlock_iothread();
599
600 record_dirtypages_bitmap(&dirty_pages, true);
601
602 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
603 DirtyStat.start_time = start_time / 1000;
604
605 msec = config.sample_period_seconds * 1000;
8244166d 606 msec = dirty_stat_wait(msec, start_time);
826b8bc8
HH
607 DirtyStat.calc_time = msec / 1000;
608
609 /*
8244166d 610 * do two things.
826b8bc8
HH
611 * 1. fetch dirty bitmap from kvm
612 * 2. stop dirty tracking
613 */
8244166d 614 global_dirty_log_sync(GLOBAL_DIRTY_DIRTY_RATE, true);
826b8bc8
HH
615
616 record_dirtypages_bitmap(&dirty_pages, false);
617
8244166d 618 DirtyStat.dirty_rate = do_calculate_dirtyrate(dirty_pages, msec);
826b8bc8
HH
619}
620
0e21bf24
HH
621static void calculate_dirtyrate_dirty_ring(struct DirtyRateConfig config)
622{
8244166d 623 int64_t duration;
0e21bf24
HH
624 uint64_t dirtyrate = 0;
625 uint64_t dirtyrate_sum = 0;
0e21bf24
HH
626 int i = 0;
627
8244166d
HH
628 /* start log sync */
629 global_dirty_log_change(GLOBAL_DIRTY_DIRTY_RATE, true);
0e21bf24 630
8244166d 631 DirtyStat.start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
0e21bf24 632
8244166d
HH
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);
0e21bf24 638
8244166d 639 DirtyStat.calc_time = duration / 1000;
0e21bf24 640
8244166d 641 /* calculate vm dirtyrate */
0e21bf24 642 for (i = 0; i < DirtyStat.dirty_ring.nvcpu; i++) {
8244166d 643 dirtyrate = DirtyStat.dirty_ring.rates[i].dirty_rate;
0e21bf24
HH
644 DirtyStat.dirty_ring.rates[i].dirty_rate = dirtyrate;
645 dirtyrate_sum += dirtyrate;
646 }
647
648 DirtyStat.dirty_rate = dirtyrate_sum;
0e21bf24
HH
649}
650
651static void calculate_dirtyrate_sample_vm(struct DirtyRateConfig config)
4240dcee 652{
cf0bbb49
CZ
653 struct RamblockDirtyInfo *block_dinfo = NULL;
654 int block_count = 0;
655 int64_t msec = 0;
656 int64_t initial_time;
657
cf0bbb49
CZ
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;
662 }
663 rcu_read_unlock();
664
665 msec = config.sample_period_seconds * 1000;
8244166d 666 msec = dirty_stat_wait(msec, initial_time);
4c437254
CZ
667 DirtyStat.start_time = initial_time / 1000;
668 DirtyStat.calc_time = msec / 1000;
cf0bbb49
CZ
669
670 rcu_read_lock();
671 if (!compare_page_hash_info(block_dinfo, block_count)) {
672 goto out;
673 }
674
675 update_dirtyrate(msec);
676
677out:
678 rcu_read_unlock();
679 free_ramblock_dirty_info(block_dinfo, block_count);
4240dcee
CZ
680}
681
0e21bf24
HH
682static void calculate_dirtyrate(struct DirtyRateConfig config)
683{
826b8bc8
HH
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) {
0e21bf24
HH
687 calculate_dirtyrate_dirty_ring(config);
688 } else {
689 calculate_dirtyrate_sample_vm(config);
690 }
691
692 trace_dirtyrate_calculate(DirtyStat.dirty_rate);
693}
694
4240dcee
CZ
695void *get_dirtyrate_thread(void *arg)
696{
697 struct DirtyRateConfig config = *(struct DirtyRateConfig *)arg;
7df3aa30 698 int ret;
15eb2d64 699 rcu_register_thread();
7df3aa30
CZ
700
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;
706 }
4240dcee
CZ
707
708 calculate_dirtyrate(config);
709
7df3aa30
CZ
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.");
714 }
15eb2d64
HH
715
716 rcu_unregister_thread();
4240dcee
CZ
717 return NULL;
718}
4c437254 719
0e21bf24
HH
720void 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)
4c437254
CZ
726{
727 static struct DirtyRateConfig config;
728 QemuThread thread;
729 int ret;
9865d0f6 730 int64_t start_time;
4c437254
CZ
731
732 /*
733 * If the dirty rate is already being measured, don't attempt to start.
734 */
735 if (qatomic_read(&CalculatingState) == DIRTY_RATE_STATUS_MEASURING) {
736 error_setg(errp, "the dirty rate is already being measured.");
737 return;
738 }
739
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;
745 }
746
0e21bf24
HH
747 if (!has_mode) {
748 mode = DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
749 }
750
bd9510d3
ZD
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");
0e21bf24
HH
753 return;
754 }
755
7afa08cd
HH
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;
762 }
763 } else {
764 sample_pages = DIRTYRATE_DEFAULT_SAMPLE_PAGES;
765 }
766
0e21bf24
HH
767 /*
768 * dirty ring mode only works when kvm dirty ring is enabled.
826b8bc8 769 * on the contrary, dirty bitmap mode is not.
0e21bf24 770 */
826b8bc8
HH
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;
0e21bf24
HH
778 }
779
4c437254
CZ
780 /*
781 * Init calculation state as unstarted.
782 */
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;
788 }
789
790 config.sample_period_seconds = calc_time;
7afa08cd 791 config.sample_pages_per_gigabytes = sample_pages;
0e21bf24
HH
792 config.mode = mode;
793
794 cleanup_dirtyrate_stat(config);
795
796 /*
797 * update dirty rate mode so that we can figure out what mode has
798 * been used in last calculation
799 **/
800 dirtyrate_mode = mode;
9865d0f6
HH
801
802 start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
803 init_dirtyrate_stat(start_time, config);
804
4c437254
CZ
805 qemu_thread_create(&thread, "get_dirtyrate", get_dirtyrate_thread,
806 (void *)&config, QEMU_THREAD_DETACHED);
807}
808
809struct DirtyRateInfo *qmp_query_dirty_rate(Error **errp)
810{
811 return query_dirty_rate_info();
812}
a4a571d9
PX
813
814void hmp_info_dirty_rate(Monitor *mon, const QDict *qdict)
815{
816 DirtyRateInfo *info = query_dirty_rate_info();
817
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);
bd9510d3
ZD
822 if (info->mode == DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING) {
823 monitor_printf(mon, "Sample Pages: %"PRIu64" (per GB)\n",
824 info->sample_pages);
825 }
a4a571d9
PX
826 monitor_printf(mon, "Period: %"PRIi64" (sec)\n",
827 info->calc_time);
0e21bf24
HH
828 monitor_printf(mon, "Mode: %s\n",
829 DirtyRateMeasureMode_str(info->mode));
a4a571d9
PX
830 monitor_printf(mon, "Dirty rate: ");
831 if (info->has_dirty_rate) {
832 monitor_printf(mon, "%"PRIi64" (MB/s)\n", info->dirty_rate);
0e21bf24
HH
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);
839 }
840 }
a4a571d9
PX
841 } else {
842 monitor_printf(mon, "(not ready)\n");
843 }
0e21bf24
HH
844
845 qapi_free_DirtyRateVcpuList(info->vcpu_dirty_rate);
a4a571d9
PX
846 g_free(info);
847}
848
849void hmp_calc_dirty_rate(Monitor *mon, const QDict *qdict)
850{
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);
0e21bf24 854 bool dirty_ring = qdict_get_try_bool(qdict, "dirty_ring", false);
826b8bc8
HH
855 bool dirty_bitmap = qdict_get_try_bool(qdict, "dirty_bitmap", false);
856 DirtyRateMeasureMode mode = DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING;
a4a571d9
PX
857 Error *err = NULL;
858
859 if (!sec) {
860 monitor_printf(mon, "Incorrect period length specified!\n");
861 return;
862 }
863
826b8bc8
HH
864 if (dirty_ring && dirty_bitmap) {
865 monitor_printf(mon, "Either dirty ring or dirty bitmap "
866 "can be specified!\n");
867 return;
868 }
869
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;
874 }
875
0e21bf24
HH
876 qmp_calc_dirty_rate(sec, has_sample_pages, sample_pages, true,
877 mode, &err);
a4a571d9
PX
878 if (err) {
879 hmp_handle_error(mon, err);
880 return;
881 }
882
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");
886}