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"
24 static int CalculatingState
= DIRTY_RATE_STATUS_UNSTARTED
;
25 static struct DirtyRateStat DirtyStat
;
27 static int64_t set_sample_page_period(int64_t msec
, int64_t initial_time
)
31 current_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
32 if ((current_time
- initial_time
) >= msec
) {
33 msec
= current_time
- initial_time
;
35 g_usleep((msec
+ initial_time
- current_time
) * 1000);
41 static bool is_sample_period_valid(int64_t sec
)
43 if (sec
< MIN_FETCH_DIRTYRATE_TIME_SEC
||
44 sec
> MAX_FETCH_DIRTYRATE_TIME_SEC
) {
51 static int dirtyrate_set_state(int *state
, int old_state
, int new_state
)
53 assert(new_state
< DIRTY_RATE_STATUS__MAX
);
54 trace_dirtyrate_set_state(DirtyRateStatus_str(new_state
));
55 if (qatomic_cmpxchg(state
, old_state
, new_state
) == old_state
) {
62 static struct DirtyRateInfo
*query_dirty_rate_info(void)
64 int64_t dirty_rate
= DirtyStat
.dirty_rate
;
65 struct DirtyRateInfo
*info
= g_malloc0(sizeof(DirtyRateInfo
));
67 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURED
) {
68 info
->has_dirty_rate
= true;
69 info
->dirty_rate
= dirty_rate
;
72 info
->status
= CalculatingState
;
73 info
->start_time
= DirtyStat
.start_time
;
74 info
->calc_time
= DirtyStat
.calc_time
;
76 trace_query_dirty_rate_info(DirtyRateStatus_str(CalculatingState
));
81 static void init_dirtyrate_stat(int64_t start_time
, int64_t calc_time
)
83 DirtyStat
.total_dirty_samples
= 0;
84 DirtyStat
.total_sample_count
= 0;
85 DirtyStat
.total_block_mem_MB
= 0;
86 DirtyStat
.dirty_rate
= -1;
87 DirtyStat
.start_time
= start_time
;
88 DirtyStat
.calc_time
= calc_time
;
91 static void update_dirtyrate_stat(struct RamblockDirtyInfo
*info
)
93 DirtyStat
.total_dirty_samples
+= info
->sample_dirty_count
;
94 DirtyStat
.total_sample_count
+= info
->sample_pages_count
;
95 /* size of total pages in MB */
96 DirtyStat
.total_block_mem_MB
+= (info
->ramblock_pages
*
97 TARGET_PAGE_SIZE
) >> 20;
100 static void update_dirtyrate(uint64_t msec
)
103 uint64_t total_dirty_samples
= DirtyStat
.total_dirty_samples
;
104 uint64_t total_sample_count
= DirtyStat
.total_sample_count
;
105 uint64_t total_block_mem_MB
= DirtyStat
.total_block_mem_MB
;
107 dirtyrate
= total_dirty_samples
* total_block_mem_MB
*
108 1000 / (total_sample_count
* msec
);
110 DirtyStat
.dirty_rate
= dirtyrate
;
114 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
115 * in ramblock, which starts from ramblock base address.
117 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo
*info
,
122 crc
= crc32(0, (info
->ramblock_addr
+
123 vfn
* TARGET_PAGE_SIZE
), TARGET_PAGE_SIZE
);
125 trace_get_ramblock_vfn_hash(info
->idstr
, vfn
, crc
);
129 static bool save_ramblock_hash(struct RamblockDirtyInfo
*info
)
131 unsigned int sample_pages_count
;
135 sample_pages_count
= info
->sample_pages_count
;
137 /* ramblock size less than one page, return success to skip this ramblock */
138 if (unlikely(info
->ramblock_pages
== 0 || sample_pages_count
== 0)) {
142 info
->hash_result
= g_try_malloc0_n(sample_pages_count
,
144 if (!info
->hash_result
) {
148 info
->sample_page_vfn
= g_try_malloc0_n(sample_pages_count
,
150 if (!info
->sample_page_vfn
) {
151 g_free(info
->hash_result
);
156 for (i
= 0; i
< sample_pages_count
; i
++) {
157 info
->sample_page_vfn
[i
] = g_rand_int_range(rand
, 0,
158 info
->ramblock_pages
- 1);
159 info
->hash_result
[i
] = get_ramblock_vfn_hash(info
,
160 info
->sample_page_vfn
[i
]);
167 static void get_ramblock_dirty_info(RAMBlock
*block
,
168 struct RamblockDirtyInfo
*info
,
169 struct DirtyRateConfig
*config
)
171 uint64_t sample_pages_per_gigabytes
= config
->sample_pages_per_gigabytes
;
173 /* Right shift 30 bits to calc ramblock size in GB */
174 info
->sample_pages_count
= (qemu_ram_get_used_length(block
) *
175 sample_pages_per_gigabytes
) >> 30;
176 /* Right shift TARGET_PAGE_BITS to calc page count */
177 info
->ramblock_pages
= qemu_ram_get_used_length(block
) >>
179 info
->ramblock_addr
= qemu_ram_get_host_addr(block
);
180 strcpy(info
->idstr
, qemu_ram_get_idstr(block
));
183 static void free_ramblock_dirty_info(struct RamblockDirtyInfo
*infos
, int count
)
191 for (i
= 0; i
< count
; i
++) {
192 g_free(infos
[i
].sample_page_vfn
);
193 g_free(infos
[i
].hash_result
);
198 static bool skip_sample_ramblock(RAMBlock
*block
)
201 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
203 if (qemu_ram_get_used_length(block
) < (MIN_RAMBLOCK_SIZE
<< 10)) {
204 trace_skip_sample_ramblock(block
->idstr
,
205 qemu_ram_get_used_length(block
));
212 static bool record_ramblock_hash_info(struct RamblockDirtyInfo
**block_dinfo
,
213 struct DirtyRateConfig config
,
216 struct RamblockDirtyInfo
*info
= NULL
;
217 struct RamblockDirtyInfo
*dinfo
= NULL
;
218 RAMBlock
*block
= NULL
;
223 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
224 if (skip_sample_ramblock(block
)) {
230 dinfo
= g_try_malloc0_n(total_count
, sizeof(struct RamblockDirtyInfo
));
235 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
236 if (skip_sample_ramblock(block
)) {
239 if (index
>= total_count
) {
242 info
= &dinfo
[index
];
243 get_ramblock_dirty_info(block
, info
, &config
);
244 if (!save_ramblock_hash(info
)) {
252 *block_count
= index
;
253 *block_dinfo
= dinfo
;
257 static void calc_page_dirty_rate(struct RamblockDirtyInfo
*info
)
262 for (i
= 0; i
< info
->sample_pages_count
; i
++) {
263 crc
= get_ramblock_vfn_hash(info
, info
->sample_page_vfn
[i
]);
264 if (crc
!= info
->hash_result
[i
]) {
265 trace_calc_page_dirty_rate(info
->idstr
, crc
, info
->hash_result
[i
]);
266 info
->sample_dirty_count
++;
271 static struct RamblockDirtyInfo
*
272 find_block_matched(RAMBlock
*block
, int count
,
273 struct RamblockDirtyInfo
*infos
)
276 struct RamblockDirtyInfo
*matched
;
278 for (i
= 0; i
< count
; i
++) {
279 if (!strcmp(infos
[i
].idstr
, qemu_ram_get_idstr(block
))) {
288 if (infos
[i
].ramblock_addr
!= qemu_ram_get_host_addr(block
) ||
289 infos
[i
].ramblock_pages
!=
290 (qemu_ram_get_used_length(block
) >> TARGET_PAGE_BITS
)) {
291 trace_find_page_matched(block
->idstr
);
300 static bool compare_page_hash_info(struct RamblockDirtyInfo
*info
,
303 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
304 RAMBlock
*block
= NULL
;
306 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
307 if (skip_sample_ramblock(block
)) {
310 block_dinfo
= find_block_matched(block
, block_count
, info
);
311 if (block_dinfo
== NULL
) {
314 calc_page_dirty_rate(block_dinfo
);
315 update_dirtyrate_stat(block_dinfo
);
318 if (DirtyStat
.total_sample_count
== 0) {
325 static void calculate_dirtyrate(struct DirtyRateConfig config
)
327 struct RamblockDirtyInfo
*block_dinfo
= NULL
;
330 int64_t initial_time
;
332 rcu_register_thread();
334 initial_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
335 if (!record_ramblock_hash_info(&block_dinfo
, config
, &block_count
)) {
340 msec
= config
.sample_period_seconds
* 1000;
341 msec
= set_sample_page_period(msec
, initial_time
);
342 DirtyStat
.start_time
= initial_time
/ 1000;
343 DirtyStat
.calc_time
= msec
/ 1000;
346 if (!compare_page_hash_info(block_dinfo
, block_count
)) {
350 update_dirtyrate(msec
);
354 free_ramblock_dirty_info(block_dinfo
, block_count
);
355 rcu_unregister_thread();
358 void *get_dirtyrate_thread(void *arg
)
360 struct DirtyRateConfig config
= *(struct DirtyRateConfig
*)arg
;
365 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_UNSTARTED
,
366 DIRTY_RATE_STATUS_MEASURING
);
368 error_report("change dirtyrate state failed.");
372 start_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
373 calc_time
= config
.sample_period_seconds
;
374 init_dirtyrate_stat(start_time
, calc_time
);
376 calculate_dirtyrate(config
);
378 ret
= dirtyrate_set_state(&CalculatingState
, DIRTY_RATE_STATUS_MEASURING
,
379 DIRTY_RATE_STATUS_MEASURED
);
381 error_report("change dirtyrate state failed.");
386 void qmp_calc_dirty_rate(int64_t calc_time
, Error
**errp
)
388 static struct DirtyRateConfig config
;
393 * If the dirty rate is already being measured, don't attempt to start.
395 if (qatomic_read(&CalculatingState
) == DIRTY_RATE_STATUS_MEASURING
) {
396 error_setg(errp
, "the dirty rate is already being measured.");
400 if (!is_sample_period_valid(calc_time
)) {
401 error_setg(errp
, "calc-time is out of range[%d, %d].",
402 MIN_FETCH_DIRTYRATE_TIME_SEC
,
403 MAX_FETCH_DIRTYRATE_TIME_SEC
);
408 * Init calculation state as unstarted.
410 ret
= dirtyrate_set_state(&CalculatingState
, CalculatingState
,
411 DIRTY_RATE_STATUS_UNSTARTED
);
413 error_setg(errp
, "init dirty rate calculation state failed.");
417 config
.sample_period_seconds
= calc_time
;
418 config
.sample_pages_per_gigabytes
= DIRTYRATE_DEFAULT_SAMPLE_PAGES
;
419 qemu_thread_create(&thread
, "get_dirtyrate", get_dirtyrate_thread
,
420 (void *)&config
, QEMU_THREAD_DETACHED
);
423 struct DirtyRateInfo
*qmp_query_dirty_rate(Error
**errp
)
425 return query_dirty_rate_info();