2 * Dirtyrate implement code
4 * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO.,LTD.
7 * Chuan Zheng <zhengchuan@huawei.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "qapi/error.h"
17 #include "exec/ramblock.h"
18 #include "qemu/rcu_queue.h"
19 #include "qapi/qapi-commands-migration.h"
22 #include "dirtyrate.h"
23 #include "monitor/hmp.h"
24 #include "monitor/monitor.h"
25 #include "qapi/qmp/qdict.h"
27 static int CalculatingState
= DIRTY_RATE_STATUS_UNSTARTED
;
28 static struct DirtyRateStat DirtyStat
;
30 static int64_t set_sample_page_period(int64_t msec
, int64_t initial_time
)
34 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
35 if ((current_time
- initial_time
) >= msec
) {
36 msec
= current_time
- initial_time
;
38 g_usleep((msec
+ initial_time
- current_time
) * 1000);
44 static bool is_sample_period_valid(int64_t sec
)
46 if (sec
< MIN_FETCH_DIRTYRATE_TIME_SEC
||
47 sec
> MAX_FETCH_DIRTYRATE_TIME_SEC
) {
54 static bool is_sample_pages_valid(int64_t pages
)
56 return pages
>= MIN_SAMPLE_PAGE_COUNT
&&
57 pages
<= MAX_SAMPLE_PAGE_COUNT
;
60 static int dirtyrate_set_state(int *state
, int old_state
, int new_state
)
62 assert(new_state
< DIRTY_RATE_STATUS__MAX
);
63 trace_dirtyrate_set_state(DirtyRateStatus_str(new_state
));
64 if (qatomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
71 static struct DirtyRateInfo
*query_dirty_rate_info(void)
73 int64_t dirty_rate
= DirtyStat
.dirty_rate
;
74 struct DirtyRateInfo
*info
= g_malloc0(sizeof(DirtyRateInfo
));
76 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURED
) {
77 info
->has_dirty_rate
= true;
78 info
->dirty_rate
= dirty_rate
;
81 info
->status
= CalculatingState
;
82 info
->start_time
= DirtyStat
.start_time
;
83 info
->calc_time
= DirtyStat
.calc_time
;
84 info
->sample_pages
= DirtyStat
.sample_pages
;
86 trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState
));
91 static void init_dirtyrate_stat(int64_t start_time
, int64_t calc_time
,
92 uint64_t sample_pages
)
94 DirtyStat
.total_dirty_samples
= 0;
95 DirtyStat
.total_sample_count
= 0;
96 DirtyStat
.total_block_mem_MB
= 0;
97 DirtyStat
.dirty_rate
= -1;
98 DirtyStat
.start_time
= start_time
;
99 DirtyStat
.calc_time
= calc_time
;
100 DirtyStat
.sample_pages
= sample_pages
;
103 static void update_dirtyrate_stat(struct RamblockDirtyInfo
*info
)
105 DirtyStat
.total_dirty_samples
+= info
->sample_dirty_count
;
106 DirtyStat
.total_sample_count
+= info
->sample_pages_count
;
107 /* size of total pages in MB */
108 DirtyStat
.total_block_mem_MB
+= (info
->ramblock_pages
*
109 TARGET_PAGE_SIZE
) >> 20;
112 static void update_dirtyrate(uint64_t msec
)
115 uint64_t total_dirty_samples
= DirtyStat
.total_dirty_samples
;
116 uint64_t total_sample_count
= DirtyStat
.total_sample_count
;
117 uint64_t total_block_mem_MB
= DirtyStat
.total_block_mem_MB
;
119 dirtyrate
= total_dirty_samples
* total_block_mem_MB
*
120 1000 / (total_sample_count
* msec
);
122 DirtyStat
.dirty_rate
= dirtyrate
;
126 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
127 * in ramblock, which starts from ramblock base address.
129 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo
*info
,
134 crc
= crc32(0, (info
->ramblock_addr
+
135 vfn
* TARGET_PAGE_SIZE
), TARGET_PAGE_SIZE
);
137 trace_get_ramblock_vfn_hash(info
->idstr
, vfn
, crc
);
141 static bool save_ramblock_hash(struct RamblockDirtyInfo
*info
)
143 unsigned int sample_pages_count
;
147 sample_pages_count
= info
->sample_pages_count
;
149 /* ramblock size less than one page, return success to skip this ramblock */
150 if (unlikely(info
->ramblock_pages
== 0 || sample_pages_count
== 0)) {
154 info
->hash_result
= g_try_malloc0_n(sample_pages_count
,
156 if (!info
->hash_result
) {
160 info
->sample_page_vfn
= g_try_malloc0_n(sample_pages_count
,
162 if (!info
->sample_page_vfn
) {
163 g_free(info
->hash_result
);
168 for (i
= 0; i
< sample_pages_count
; i
++) {
169 info
->sample_page_vfn
[i
] = g_rand_int_range(rand
, 0,
170 info
->ramblock_pages
- 1);
171 info
->hash_result
[i
] = get_ramblock_vfn_hash(info
,
172 info
->sample_page_vfn
[i
]);
179 static void get_ramblock_dirty_info(RAMBlock
*block
,
180 struct RamblockDirtyInfo
*info
,
181 struct DirtyRateConfig
*config
)
183 uint64_t sample_pages_per_gigabytes
= config
->sample_pages_per_gigabytes
;
185 /* Right shift 30 bits to calc ramblock size in GB */
186 info
->sample_pages_count
= (qemu_ram_get_used_length(block
) *
187 sample_pages_per_gigabytes
) >> 30;
188 /* Right shift TARGET_PAGE_BITS to calc page count */
189 info
->ramblock_pages
= qemu_ram_get_used_length(block
) >>
191 info
->ramblock_addr
= qemu_ram_get_host_addr(block
);
192 strcpy(info
->idstr
, qemu_ram_get_idstr(block
));
195 static void free_ramblock_dirty_info(struct RamblockDirtyInfo
*infos
, int count
)
203 for (i
= 0; i
< count
; i
++) {
204 g_free(infos
[i
].sample_page_vfn
);
205 g_free(infos
[i
].hash_result
);
210 static bool skip_sample_ramblock(RAMBlock
*block
)
213 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
215 if (qemu_ram_get_used_length(block
) < (MIN_RAMBLOCK_SIZE
<< 10)) {
216 trace_skip_sample_ramblock(block
->idstr
,
217 qemu_ram_get_used_length(block
));
224 static bool record_ramblock_hash_info(struct RamblockDirtyInfo
**block_dinfo
,
225 struct DirtyRateConfig config
,
228 struct RamblockDirtyInfo
*info
= NULL
;
229 struct RamblockDirtyInfo
*dinfo
= NULL
;
230 RAMBlock
*block
= NULL
;
235 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
236 if (skip_sample_ramblock(block
)) {
242 dinfo
= g_try_malloc0_n(total_count
, sizeof(struct RamblockDirtyInfo
));
247 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
248 if (skip_sample_ramblock(block
)) {
251 if (index
>= total_count
) {
254 info
= &dinfo
[index
];
255 get_ramblock_dirty_info(block
, info
, &config
);
256 if (!save_ramblock_hash(info
)) {
264 *block_count
= index
;
265 *block_dinfo
= dinfo
;
269 static void calc_page_dirty_rate(struct RamblockDirtyInfo
*info
)
274 for (i
= 0; i
< info
->sample_pages_count
; i
++) {
275 crc
= get_ramblock_vfn_hash(info
, info
->sample_page_vfn
[i
]);
276 if (crc
!= info
->hash_result
[i
]) {
277 trace_calc_page_dirty_rate(info
->idstr
, crc
, info
->hash_result
[i
]);
278 info
->sample_dirty_count
++;
283 static struct RamblockDirtyInfo
*
284 find_block_matched(RAMBlock
*block
, int count
,
285 struct RamblockDirtyInfo
*infos
)
288 struct RamblockDirtyInfo
*matched
;
290 for (i
= 0; i
< count
; i
++) {
291 if (!strcmp(infos
[i
].idstr
, qemu_ram_get_idstr(block
))) {
300 if (infos
[i
].ramblock_addr
!= qemu_ram_get_host_addr(block
) ||
301 infos
[i
].ramblock_pages
!=
302 (qemu_ram_get_used_length(block
) >> TARGET_PAGE_BITS
)) {
303 trace_find_page_matched(block
->idstr
);
312 static bool compare_page_hash_info(struct RamblockDirtyInfo
*info
,
315 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
316 RAMBlock
*block
= NULL
;
318 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
319 if (skip_sample_ramblock(block
)) {
322 block_dinfo
= find_block_matched(block
, block_count
, info
);
323 if (block_dinfo
== NULL
) {
326 calc_page_dirty_rate(block_dinfo
);
327 update_dirtyrate_stat(block_dinfo
);
330 if (DirtyStat
.total_sample_count
== 0) {
337 static void calculate_dirtyrate(struct DirtyRateConfig config
)
339 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
342 int64_t initial_time
;
344 rcu_register_thread();
346 initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
347 if (!record_ramblock_hash_info(&block_dinfo
, config
, &block_count
)) {
352 msec
= config
.sample_period_seconds
* 1000;
353 msec
= set_sample_page_period(msec
, initial_time
);
354 DirtyStat
.start_time
= initial_time
/ 1000;
355 DirtyStat
.calc_time
= msec
/ 1000;
358 if (!compare_page_hash_info(block_dinfo
, block_count
)) {
362 update_dirtyrate(msec
);
366 free_ramblock_dirty_info(block_dinfo
, block_count
);
367 rcu_unregister_thread();
370 void *get_dirtyrate_thread(void *arg
)
372 struct DirtyRateConfig config
= *(struct DirtyRateConfig
*)arg
;
376 uint64_t sample_pages
;
378 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_UNSTARTED
,
379 DIRTY_RATE_STATUS_MEASURING
);
381 error_report("change dirtyrate state failed.");
385 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
386 calc_time
= config
.sample_period_seconds
;
387 sample_pages
= config
.sample_pages_per_gigabytes
;
388 init_dirtyrate_stat(start_time
, calc_time
, sample_pages
);
390 calculate_dirtyrate(config
);
392 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_MEASURING
,
393 DIRTY_RATE_STATUS_MEASURED
);
395 error_report("change dirtyrate state failed.");
400 void qmp_calc_dirty_rate(int64_t calc_time
, bool has_sample_pages
,
401 int64_t sample_pages
, Error
**errp
)
403 static struct DirtyRateConfig config
;
408 * If the dirty rate is already being measured, don't attempt to start.
410 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURING
) {
411 error_setg(errp
, "the dirty rate is already being measured.");
415 if (!is_sample_period_valid(calc_time
)) {
416 error_setg(errp
, "calc-time is out of range[%d, %d].",
417 MIN_FETCH_DIRTYRATE_TIME_SEC
,
418 MAX_FETCH_DIRTYRATE_TIME_SEC
);
422 if (has_sample_pages
) {
423 if (!is_sample_pages_valid(sample_pages
)) {
424 error_setg(errp
, "sample-pages is out of range[%d, %d].",
425 MIN_SAMPLE_PAGE_COUNT
,
426 MAX_SAMPLE_PAGE_COUNT
);
430 sample_pages
= DIRTYRATE_DEFAULT_SAMPLE_PAGES
;
434 * Init calculation state as unstarted.
436 ret
= dirtyrate_set_state(&CalculatingState
, CalculatingState
,
437 DIRTY_RATE_STATUS_UNSTARTED
);
439 error_setg(errp
, "init dirty rate calculation state failed.");
443 config
.sample_period_seconds
= calc_time
;
444 config
.sample_pages_per_gigabytes
= sample_pages
;
445 qemu_thread_create(&thread
, "get_dirtyrate", get_dirtyrate_thread
,
446 (void *)&config
, QEMU_THREAD_DETACHED
);
449 struct DirtyRateInfo
*qmp_query_dirty_rate(Error
**errp
)
451 return query_dirty_rate_info();
454 void hmp_info_dirty_rate(Monitor
*mon
, const QDict
*qdict
)
456 DirtyRateInfo
*info
= query_dirty_rate_info();
458 monitor_printf(mon
, "Status: %s\n",
459 DirtyRateStatus_str(info
->status
));
460 monitor_printf(mon
, "Start Time: %"PRIi64
" (ms)\n",
462 monitor_printf(mon
, "Sample Pages: %"PRIu64
" (per GB)\n",
464 monitor_printf(mon
, "Period: %"PRIi64
" (sec)\n",
466 monitor_printf(mon
, "Dirty rate: ");
467 if (info
->has_dirty_rate
) {
468 monitor_printf(mon
, "%"PRIi64
" (MB/s)\n", info
->dirty_rate
);
470 monitor_printf(mon
, "(not ready)\n");
475 void hmp_calc_dirty_rate(Monitor
*mon
, const QDict
*qdict
)
477 int64_t sec
= qdict_get_try_int(qdict
, "second", 0);
478 int64_t sample_pages
= qdict_get_try_int(qdict
, "sample_pages_per_GB", -1);
479 bool has_sample_pages
= (sample_pages
!= -1);
483 monitor_printf(mon
, "Incorrect period length specified!\n");
487 qmp_calc_dirty_rate(sec
, has_sample_pages
, sample_pages
, &err
);
489 hmp_handle_error(mon
, err
);
493 monitor_printf(mon
, "Starting dirty rate measurement with period %"PRIi64
495 monitor_printf(mon
, "[Please use 'info dirty_rate' to check results]\n");