migration/dirtyrate: skip sampling ramblock with size below MIN_RAMBLOCK_SIZE
[qemu.git] / migration / dirtyrate.c
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 <zlib.h>
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "cpu.h"
17 #include "qemu/config-file.h"
18 #include "exec/memory.h"
19 #include "exec/ramblock.h"
20 #include "exec/target_page.h"
21 #include "qemu/rcu_queue.h"
22 #include "qapi/qapi-commands-migration.h"
23 #include "migration.h"
24 #include "ram.h"
25 #include "dirtyrate.h"
26
27 static int CalculatingState = DIRTY_RATE_STATUS_UNSTARTED;
28 static struct DirtyRateStat DirtyStat;
29
30 static int dirtyrate_set_state(int *state, int old_state, int new_state)
31 {
32 assert(new_state < DIRTY_RATE_STATUS__MAX);
33 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) {
34 return 0;
35 } else {
36 return -1;
37 }
38 }
39
40 static void reset_dirtyrate_stat(void)
41 {
42 DirtyStat.total_dirty_samples = 0;
43 DirtyStat.total_sample_count = 0;
44 DirtyStat.total_block_mem_MB = 0;
45 DirtyStat.dirty_rate = -1;
46 DirtyStat.start_time = 0;
47 DirtyStat.calc_time = 0;
48 }
49
50 static void update_dirtyrate_stat(struct RamblockDirtyInfo *info)
51 {
52 DirtyStat.total_dirty_samples += info->sample_dirty_count;
53 DirtyStat.total_sample_count += info->sample_pages_count;
54 /* size of total pages in MB */
55 DirtyStat.total_block_mem_MB += (info->ramblock_pages *
56 TARGET_PAGE_SIZE) >> 20;
57 }
58
59 static void update_dirtyrate(uint64_t msec)
60 {
61 uint64_t dirtyrate;
62 uint64_t total_dirty_samples = DirtyStat.total_dirty_samples;
63 uint64_t total_sample_count = DirtyStat.total_sample_count;
64 uint64_t total_block_mem_MB = DirtyStat.total_block_mem_MB;
65
66 dirtyrate = total_dirty_samples * total_block_mem_MB *
67 1000 / (total_sample_count * msec);
68
69 DirtyStat.dirty_rate = dirtyrate;
70 }
71
72 /*
73 * get hash result for the sampled memory with length of TARGET_PAGE_SIZE
74 * in ramblock, which starts from ramblock base address.
75 */
76 static uint32_t get_ramblock_vfn_hash(struct RamblockDirtyInfo *info,
77 uint64_t vfn)
78 {
79 uint32_t crc;
80
81 crc = crc32(0, (info->ramblock_addr +
82 vfn * TARGET_PAGE_SIZE), TARGET_PAGE_SIZE);
83
84 return crc;
85 }
86
87 static bool save_ramblock_hash(struct RamblockDirtyInfo *info)
88 {
89 unsigned int sample_pages_count;
90 int i;
91 GRand *rand;
92
93 sample_pages_count = info->sample_pages_count;
94
95 /* ramblock size less than one page, return success to skip this ramblock */
96 if (unlikely(info->ramblock_pages == 0 || sample_pages_count == 0)) {
97 return true;
98 }
99
100 info->hash_result = g_try_malloc0_n(sample_pages_count,
101 sizeof(uint32_t));
102 if (!info->hash_result) {
103 return false;
104 }
105
106 info->sample_page_vfn = g_try_malloc0_n(sample_pages_count,
107 sizeof(uint64_t));
108 if (!info->sample_page_vfn) {
109 g_free(info->hash_result);
110 return false;
111 }
112
113 rand = g_rand_new();
114 for (i = 0; i < sample_pages_count; i++) {
115 info->sample_page_vfn[i] = g_rand_int_range(rand, 0,
116 info->ramblock_pages - 1);
117 info->hash_result[i] = get_ramblock_vfn_hash(info,
118 info->sample_page_vfn[i]);
119 }
120 g_rand_free(rand);
121
122 return true;
123 }
124
125 static void get_ramblock_dirty_info(RAMBlock *block,
126 struct RamblockDirtyInfo *info,
127 struct DirtyRateConfig *config)
128 {
129 uint64_t sample_pages_per_gigabytes = config->sample_pages_per_gigabytes;
130
131 /* Right shift 30 bits to calc ramblock size in GB */
132 info->sample_pages_count = (qemu_ram_get_used_length(block) *
133 sample_pages_per_gigabytes) >> 30;
134 /* Right shift TARGET_PAGE_BITS to calc page count */
135 info->ramblock_pages = qemu_ram_get_used_length(block) >>
136 TARGET_PAGE_BITS;
137 info->ramblock_addr = qemu_ram_get_host_addr(block);
138 strcpy(info->idstr, qemu_ram_get_idstr(block));
139 }
140
141 static bool skip_sample_ramblock(RAMBlock *block)
142 {
143 /*
144 * Sample only blocks larger than MIN_RAMBLOCK_SIZE.
145 */
146 if (qemu_ram_get_used_length(block) < (MIN_RAMBLOCK_SIZE << 10)) {
147 return true;
148 }
149
150 return false;
151 }
152
153 static bool record_ramblock_hash_info(struct RamblockDirtyInfo **block_dinfo,
154 struct DirtyRateConfig config,
155 int *block_count)
156 {
157 struct RamblockDirtyInfo *info = NULL;
158 struct RamblockDirtyInfo *dinfo = NULL;
159 RAMBlock *block = NULL;
160 int total_count = 0;
161 int index = 0;
162 bool ret = false;
163
164 RAMBLOCK_FOREACH_MIGRATABLE(block) {
165 if (skip_sample_ramblock(block)) {
166 continue;
167 }
168 total_count++;
169 }
170
171 dinfo = g_try_malloc0_n(total_count, sizeof(struct RamblockDirtyInfo));
172 if (dinfo == NULL) {
173 goto out;
174 }
175
176 RAMBLOCK_FOREACH_MIGRATABLE(block) {
177 if (skip_sample_ramblock(block)) {
178 continue;
179 }
180 if (index >= total_count) {
181 break;
182 }
183 info = &dinfo[index];
184 get_ramblock_dirty_info(block, info, &config);
185 if (!save_ramblock_hash(info)) {
186 goto out;
187 }
188 index++;
189 }
190 ret = true;
191
192 out:
193 *block_count = index;
194 *block_dinfo = dinfo;
195 return ret;
196 }
197
198 static void calc_page_dirty_rate(struct RamblockDirtyInfo *info)
199 {
200 uint32_t crc;
201 int i;
202
203 for (i = 0; i < info->sample_pages_count; i++) {
204 crc = get_ramblock_vfn_hash(info, info->sample_page_vfn[i]);
205 if (crc != info->hash_result[i]) {
206 info->sample_dirty_count++;
207 }
208 }
209 }
210
211 static struct RamblockDirtyInfo *
212 find_block_matched(RAMBlock *block, int count,
213 struct RamblockDirtyInfo *infos)
214 {
215 int i;
216 struct RamblockDirtyInfo *matched;
217
218 for (i = 0; i < count; i++) {
219 if (!strcmp(infos[i].idstr, qemu_ram_get_idstr(block))) {
220 break;
221 }
222 }
223
224 if (i == count) {
225 return NULL;
226 }
227
228 if (infos[i].ramblock_addr != qemu_ram_get_host_addr(block) ||
229 infos[i].ramblock_pages !=
230 (qemu_ram_get_used_length(block) >> TARGET_PAGE_BITS)) {
231 return NULL;
232 }
233
234 matched = &infos[i];
235
236 return matched;
237 }
238
239 static bool compare_page_hash_info(struct RamblockDirtyInfo *info,
240 int block_count)
241 {
242 struct RamblockDirtyInfo *block_dinfo = NULL;
243 RAMBlock *block = NULL;
244
245 RAMBLOCK_FOREACH_MIGRATABLE(block) {
246 if (skip_sample_ramblock(block)) {
247 continue;
248 }
249 block_dinfo = find_block_matched(block, block_count, info);
250 if (block_dinfo == NULL) {
251 continue;
252 }
253 calc_page_dirty_rate(block_dinfo);
254 update_dirtyrate_stat(block_dinfo);
255 }
256
257 if (DirtyStat.total_sample_count == 0) {
258 return false;
259 }
260
261 return true;
262 }
263
264 static void calculate_dirtyrate(struct DirtyRateConfig config)
265 {
266 /* todo */
267 return;
268 }
269
270 void *get_dirtyrate_thread(void *arg)
271 {
272 struct DirtyRateConfig config = *(struct DirtyRateConfig *)arg;
273 int ret;
274
275 ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_UNSTARTED,
276 DIRTY_RATE_STATUS_MEASURING);
277 if (ret == -1) {
278 error_report("change dirtyrate state failed.");
279 return NULL;
280 }
281
282 calculate_dirtyrate(config);
283
284 ret = dirtyrate_set_state(&CalculatingState, DIRTY_RATE_STATUS_MEASURING,
285 DIRTY_RATE_STATUS_MEASURED);
286 if (ret == -1) {
287 error_report("change dirtyrate state failed.");
288 }
289 return NULL;
290 }