3202
MIG_ITERATE_BREAK, /* Break the loop */
3203
} MigIterateState;
3204
3205
+/* Are we ready to move to the next iteration phase? */
3206
+static bool migration_iteration_next_ready(MigrationState *s,
3207
+ MigPendingData *pending)
3208
+{
3209
+ /*
3210
+ * If the estimated values already suggest us to switchover, mark this
3211
+ * iteration finished, time to do a slow sync.
3212
+ */
3213
+ if (pending->total_bytes <= s->threshold_size) {
3214
+ return true;
3215
+ }
3216
+
3217
+ /*
3218
+ * Since we may have modules reporting stop-only data, we also want to
3219
+ * re-query with slow mode if all precopy data is moved over. This
3220
+ * will also mark the current iteration done.
3221
+ *
3222
+ * This could happen when e.g. a module (like, VFIO) reports stopcopy
3223
+ * size too large so it will never yet satisfy the downtime with the
3224
+ * current setup (above check). Here, slow version of re-query helps
3225
+ * because we keep trying the best to move whatever we have.
3226
+ */
3227
+ if (pending->precopy_bytes == 0) {
3228
+ return true;
3229
+ }
3230
+
3231
+ return false;
3232
+}
3233
+
3234
+static void migration_iteration_go_next(MigPendingData *pending)
3235
+{
3236
+ /*
3237
+ * Do a slow sync will achieve this. TODO: move RAM iteration code
3238
+ * into the core layer.
3239
+ */
3240
+ qemu_savevm_query_pending(pending, true);
3241
+}
3242
+
3243
+static bool postcopy_should_start(MigrationState *s, MigPendingData *pending)
3244
+{
3245
+ /* If postcopy's switchver will violate user specified downtime, stop */
3246
+ if (pending->precopy_bytes + pending->stopcopy_bytes > s->threshold_size) {
3247
+ return false;
3248
+ }
3249
+
3250
+ return qatomic_read(&s->start_postcopy);
3251
+}
3252
+
3253
/*
3254
* Return true if continue to the next iteration directly, false
3255
* otherwise.
3261
s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
3262
bool can_switchover = migration_can_switchover(s);
3263
MigPendingData pending = { };
3216
- uint64_t pending_size;
3264
bool complete_ready;
3265
3266
/* Fast path - get the estimated amount of pending data */
3267
qemu_savevm_query_pending(&pending, false);
3221
- pending_size = pending.precopy_bytes + pending.postcopy_bytes;
3268
3269
if (in_postcopy) {
3270
/*
3272
* postcopy completion doesn't rely on can_switchover, because when
3273
* POSTCOPY_ACTIVE it means switchover already happened.
3274
*/
3229
- complete_ready = !pending_size;
3275
+ complete_ready = !pending.total_bytes;
3276
if (s->state == MIGRATION_STATUS_POSTCOPY_DEVICE &&
3277
(s->postcopy_package_loaded || complete_ready)) {
3278
/*
3304
* postcopy started, so ESTIMATE should always match with EXACT
3305
* during postcopy phase.
3306
*/
3261
- if (pending_size <= s->threshold_size) {
3262
- qemu_savevm_query_pending(&pending, true);
3263
- pending_size = pending.precopy_bytes + pending.postcopy_bytes;
3307
+ if (migration_iteration_next_ready(s, &pending)) {
3308
+ migration_iteration_go_next(&pending);
3309
}
3310
3311
/* Should we switch to postcopy now? */
3267
- if (pending.precopy_bytes <= s->threshold_size &&
3268
- can_switchover && qatomic_read(&s->start_postcopy)) {
3312
+ if (can_switchover && postcopy_should_start(s, &pending)) {
3313
if (postcopy_start(s, &local_err)) {
3314
migrate_error_propagate(s, error_copy(local_err));
3315
error_report_err(local_err);
3324
* (2) Pending size is no more than the threshold specified
3325
* (which was calculated from expected downtime)
3326
*/
3283
- complete_ready = can_switchover && (pending_size <= s->threshold_size);
3327
+ complete_ready = can_switchover &&
3328
+ (pending.total_bytes <= s->threshold_size);
3329
}
3330
3331
if (complete_ready) {
3287
- trace_migration_thread_low_pending(pending_size);
3332
+ trace_migration_thread_low_pending(pending.total_bytes);
3333
migration_completion(s);
3334
return MIG_ITERATE_BREAK;
3335
}