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
,
92 struct DirtyRateConfig config
)
94 DirtyStat
.dirty_rate
= -1;
95 DirtyStat
.start_time
= start_time
;
96 DirtyStat
.calc_time
= config
.sample_period_seconds
;
97 DirtyStat
.sample_pages
= config
.sample_pages_per_gigabytes
;
99 switch (config
.mode
) {
100 case DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
:
101 DirtyStat
.page_sampling
.total_dirty_samples
= 0;
102 DirtyStat
.page_sampling
.total_sample_count
= 0;
103 DirtyStat
.page_sampling
.total_block_mem_MB
= 0;
105 case DIRTY_RATE_MEASURE_MODE_DIRTY_RING
:
106 DirtyStat
.dirty_ring
.nvcpu
= -1;
107 DirtyStat
.dirty_ring
.rates
= NULL
;
114 static void update_dirtyrate_stat(struct RamblockDirtyInfo
*info
)
116 DirtyStat
.page_sampling
.total_dirty_samples
+= info
->sample_dirty_count
;
117 DirtyStat
.page_sampling
.total_sample_count
+= info
->sample_pages_count
;
118 /* size of total pages in MB */
119 DirtyStat
.page_sampling
.total_block_mem_MB
+= (info
->ramblock_pages
*
120 TARGET_PAGE_SIZE
) >> 20;
123 static void update_dirtyrate(uint64_t msec
)
126 uint64_t total_dirty_samples
= DirtyStat
.page_sampling
.total_dirty_samples
;
127 uint64_t total_sample_count
= DirtyStat
.page_sampling
.total_sample_count
;
128 uint64_t total_block_mem_MB
= DirtyStat
.page_sampling
.total_block_mem_MB
;
130 dirtyrate
= total_dirty_samples
* total_block_mem_MB
*
131 1000 / (total_sample_count
* msec
);
133 DirtyStat
.dirty_rate
= dirtyrate
;
137 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
138 * in ramblock, which starts from ramblock base address.
140 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo
*info
,
145 crc
= crc32(0, (info
->ramblock_addr
+
146 vfn
* TARGET_PAGE_SIZE
), TARGET_PAGE_SIZE
);
148 trace_get_ramblock_vfn_hash(info
->idstr
, vfn
, crc
);
152 static bool save_ramblock_hash(struct RamblockDirtyInfo
*info
)
154 unsigned int sample_pages_count
;
158 sample_pages_count
= info
->sample_pages_count
;
160 /* ramblock size less than one page, return success to skip this ramblock */
161 if (unlikely(info
->ramblock_pages
== 0 || sample_pages_count
== 0)) {
165 info
->hash_result
= g_try_malloc0_n(sample_pages_count
,
167 if (!info
->hash_result
) {
171 info
->sample_page_vfn
= g_try_malloc0_n(sample_pages_count
,
173 if (!info
->sample_page_vfn
) {
174 g_free(info
->hash_result
);
179 for (i
= 0; i
< sample_pages_count
; i
++) {
180 info
->sample_page_vfn
[i
] = g_rand_int_range(rand
, 0,
181 info
->ramblock_pages
- 1);
182 info
->hash_result
[i
] = get_ramblock_vfn_hash(info
,
183 info
->sample_page_vfn
[i
]);
190 static void get_ramblock_dirty_info(RAMBlock
*block
,
191 struct RamblockDirtyInfo
*info
,
192 struct DirtyRateConfig
*config
)
194 uint64_t sample_pages_per_gigabytes
= config
->sample_pages_per_gigabytes
;
196 /* Right shift 30 bits to calc ramblock size in GB */
197 info
->sample_pages_count
= (qemu_ram_get_used_length(block
) *
198 sample_pages_per_gigabytes
) >> 30;
199 /* Right shift TARGET_PAGE_BITS to calc page count */
200 info
->ramblock_pages
= qemu_ram_get_used_length(block
) >>
202 info
->ramblock_addr
= qemu_ram_get_host_addr(block
);
203 strcpy(info
->idstr
, qemu_ram_get_idstr(block
));
206 static void free_ramblock_dirty_info(struct RamblockDirtyInfo
*infos
, int count
)
214 for (i
= 0; i
< count
; i
++) {
215 g_free(infos
[i
].sample_page_vfn
);
216 g_free(infos
[i
].hash_result
);
221 static bool skip_sample_ramblock(RAMBlock
*block
)
224 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
226 if (qemu_ram_get_used_length(block
) < (MIN_RAMBLOCK_SIZE
<< 10)) {
227 trace_skip_sample_ramblock(block
->idstr
,
228 qemu_ram_get_used_length(block
));
235 static bool record_ramblock_hash_info(struct RamblockDirtyInfo
**block_dinfo
,
236 struct DirtyRateConfig config
,
239 struct RamblockDirtyInfo
*info
= NULL
;
240 struct RamblockDirtyInfo
*dinfo
= NULL
;
241 RAMBlock
*block
= NULL
;
246 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
247 if (skip_sample_ramblock(block
)) {
253 dinfo
= g_try_malloc0_n(total_count
, sizeof(struct RamblockDirtyInfo
));
258 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
259 if (skip_sample_ramblock(block
)) {
262 if (index
>= total_count
) {
265 info
= &dinfo
[index
];
266 get_ramblock_dirty_info(block
, info
, &config
);
267 if (!save_ramblock_hash(info
)) {
275 *block_count
= index
;
276 *block_dinfo
= dinfo
;
280 static void calc_page_dirty_rate(struct RamblockDirtyInfo
*info
)
285 for (i
= 0; i
< info
->sample_pages_count
; i
++) {
286 crc
= get_ramblock_vfn_hash(info
, info
->sample_page_vfn
[i
]);
287 if (crc
!= info
->hash_result
[i
]) {
288 trace_calc_page_dirty_rate(info
->idstr
, crc
, info
->hash_result
[i
]);
289 info
->sample_dirty_count
++;
294 static struct RamblockDirtyInfo
*
295 find_block_matched(RAMBlock
*block
, int count
,
296 struct RamblockDirtyInfo
*infos
)
299 struct RamblockDirtyInfo
*matched
;
301 for (i
= 0; i
< count
; i
++) {
302 if (!strcmp(infos
[i
].idstr
, qemu_ram_get_idstr(block
))) {
311 if (infos
[i
].ramblock_addr
!= qemu_ram_get_host_addr(block
) ||
312 infos
[i
].ramblock_pages
!=
313 (qemu_ram_get_used_length(block
) >> TARGET_PAGE_BITS
)) {
314 trace_find_page_matched(block
->idstr
);
323 static bool compare_page_hash_info(struct RamblockDirtyInfo
*info
,
326 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
327 RAMBlock
*block
= NULL
;
329 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
330 if (skip_sample_ramblock(block
)) {
333 block_dinfo
= find_block_matched(block
, block_count
, info
);
334 if (block_dinfo
== NULL
) {
337 calc_page_dirty_rate(block_dinfo
);
338 update_dirtyrate_stat(block_dinfo
);
341 if (DirtyStat
.page_sampling
.total_sample_count
== 0) {
348 static void calculate_dirtyrate(struct DirtyRateConfig config
)
350 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
353 int64_t initial_time
;
356 initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
357 if (!record_ramblock_hash_info(&block_dinfo
, config
, &block_count
)) {
362 msec
= config
.sample_period_seconds
* 1000;
363 msec
= set_sample_page_period(msec
, initial_time
);
364 DirtyStat
.start_time
= initial_time
/ 1000;
365 DirtyStat
.calc_time
= msec
/ 1000;
368 if (!compare_page_hash_info(block_dinfo
, block_count
)) {
372 update_dirtyrate(msec
);
376 free_ramblock_dirty_info(block_dinfo
, block_count
);
379 void *get_dirtyrate_thread(void *arg
)
381 struct DirtyRateConfig config
= *(struct DirtyRateConfig
*)arg
;
384 rcu_register_thread();
386 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_UNSTARTED
,
387 DIRTY_RATE_STATUS_MEASURING
);
389 error_report("change dirtyrate state failed.");
393 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
394 init_dirtyrate_stat(start_time
, config
);
396 calculate_dirtyrate(config
);
398 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_MEASURING
,
399 DIRTY_RATE_STATUS_MEASURED
);
401 error_report("change dirtyrate state failed.");
404 rcu_unregister_thread();
408 void qmp_calc_dirty_rate(int64_t calc_time
, bool has_sample_pages
,
409 int64_t sample_pages
, Error
**errp
)
411 static struct DirtyRateConfig config
;
416 * If the dirty rate is already being measured, don't attempt to start.
418 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURING
) {
419 error_setg(errp
, "the dirty rate is already being measured.");
423 if (!is_sample_period_valid(calc_time
)) {
424 error_setg(errp
, "calc-time is out of range[%d, %d].",
425 MIN_FETCH_DIRTYRATE_TIME_SEC
,
426 MAX_FETCH_DIRTYRATE_TIME_SEC
);
430 if (has_sample_pages
) {
431 if (!is_sample_pages_valid(sample_pages
)) {
432 error_setg(errp
, "sample-pages is out of range[%d, %d].",
433 MIN_SAMPLE_PAGE_COUNT
,
434 MAX_SAMPLE_PAGE_COUNT
);
438 sample_pages
= DIRTYRATE_DEFAULT_SAMPLE_PAGES
;
442 * Init calculation state as unstarted.
444 ret
= dirtyrate_set_state(&CalculatingState
, CalculatingState
,
445 DIRTY_RATE_STATUS_UNSTARTED
);
447 error_setg(errp
, "init dirty rate calculation state failed.");
451 config
.sample_period_seconds
= calc_time
;
452 config
.sample_pages_per_gigabytes
= sample_pages
;
453 config
.mode
= DIRTY_RATE_MEASURE_MODE_PAGE_SAMPLING
;
454 qemu_thread_create(&thread
, "get_dirtyrate", get_dirtyrate_thread
,
455 (void *)&config
, QEMU_THREAD_DETACHED
);
458 struct DirtyRateInfo
*qmp_query_dirty_rate(Error
**errp
)
460 return query_dirty_rate_info();
463 void hmp_info_dirty_rate(Monitor
*mon
, const QDict
*qdict
)
465 DirtyRateInfo
*info
= query_dirty_rate_info();
467 monitor_printf(mon
, "Status: %s\n",
468 DirtyRateStatus_str(info
->status
));
469 monitor_printf(mon
, "Start Time: %"PRIi64
" (ms)\n",
471 monitor_printf(mon
, "Sample Pages: %"PRIu64
" (per GB)\n",
473 monitor_printf(mon
, "Period: %"PRIi64
" (sec)\n",
475 monitor_printf(mon
, "Dirty rate: ");
476 if (info
->has_dirty_rate
) {
477 monitor_printf(mon
, "%"PRIi64
" (MB/s)\n", info
->dirty_rate
);
479 monitor_printf(mon
, "(not ready)\n");
484 void hmp_calc_dirty_rate(Monitor
*mon
, const QDict
*qdict
)
486 int64_t sec
= qdict_get_try_int(qdict
, "second", 0);
487 int64_t sample_pages
= qdict_get_try_int(qdict
, "sample_pages_per_GB", -1);
488 bool has_sample_pages
= (sample_pages
!= -1);
492 monitor_printf(mon
, "Incorrect period length specified!\n");
496 qmp_calc_dirty_rate(sec
, has_sample_pages
, sample_pages
, &err
);
498 hmp_handle_error(mon
, err
);
502 monitor_printf(mon
, "Starting dirty rate measurement with period %"PRIi64
504 monitor_printf(mon
, "[Please use 'info dirty_rate' to check results]\n");