migration: Introduce a helper to return switchover bw estimate
Add a helper migration_get_switchover_bw() to return an estimate of switchover bandwidth. Use it to simplify the current code. This will be used in later to remove expected_downtime. When at it, remove two qatomic_read() to shrink the lines because atomic ops are not needed when it's always the same thread who does the updates. Reviewed-by: Juraj Marcin <jmarcin@redhat.com> Link: https://lore.kernel.org/r/20260421202110.306051-11-peterx@redhat.com Signed-off-by: Peter Xu <peterx@redhat.com>
Peter Xu committed
Apr 21, 2026 at 16:21 UTC
474838440f77ac5b360a182fbadbc5c03a09b4d5
1 file changed
+24
-24
migration/migration.c
+24
-24
@@ -984,6 +984,21 @@ void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
984
migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
985
}
986
987
+/*
988
+ * Returns the estimated switchover bandwidth (unit: bytes / seconds)
989
+ */
990
+static double migration_get_switchover_bw(MigrationState *s)
991
+{
992
+ uint64_t switchover_bw = migrate_avail_switchover_bandwidth();
993
+
994
+ if (switchover_bw) {
995
+ /* If user specified, prioritize this value and don't estimate */
996
+ return (double)switchover_bw;
997
+ }
998
+
999
+ return s->mbps / 8 * 1000 * 1000;
1000
+}
1001
+
1002
bool migration_is_running(void)
1003
{
1004
MigrationState *s = current_migration;
@@ -3130,37 +3145,22 @@ static void migration_update_counters(MigrationState *s,
3145
{
3146
uint64_t transferred, transferred_pages, time_spent;
3147
uint64_t current_bytes; /* bytes transferred since the beginning */
3133
- uint64_t switchover_bw;
3134
- /* Expected bandwidth when switching over to destination QEMU */
3135
- double expected_bw_per_ms;
3136
- double bandwidth;
3148
+ double switchover_bw_per_ms;
3149
3150
if (current_time < s->iteration_start_time + BUFFER_DELAY) {
3151
return;
3152
}
3153
3142
- switchover_bw = migrate_avail_switchover_bandwidth();
3154
current_bytes = migration_transferred_bytes();
3155
transferred = current_bytes - s->iteration_initial_bytes;
3156
time_spent = current_time - s->iteration_start_time;
3146
- bandwidth = (double)transferred / time_spent;
3147
-
3148
- if (switchover_bw) {
3149
- /*
3150
- * If the user specified a switchover bandwidth, let's trust the
3151
- * user so that can be more accurate than what we estimated.
3152
- */
3153
- expected_bw_per_ms = (double)switchover_bw / 1000;
3154
- } else {
3155
- /* If the user doesn't specify bandwidth, we use the estimated */
3156
- expected_bw_per_ms = bandwidth;
3157
- }
3158
-
3159
- s->threshold_size = expected_bw_per_ms * migrate_downtime_limit();
3160
-
3157
s->mbps = (((double) transferred * 8.0) /
3158
((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
3159
3160
+ /* NOTE: only update this after bandwidth (s->mbps) updated */
3161
+ switchover_bw_per_ms = migration_get_switchover_bw(s) / 1000;
3162
+ s->threshold_size = switchover_bw_per_ms * migrate_downtime_limit();
3163
+
3164
transferred_pages = ram_get_total_transferred_pages() -
3165
s->iteration_initial_pages;
3166
s->pages_per_second = (double) transferred_pages /
@@ -3170,10 +3170,9 @@ static void migration_update_counters(MigrationState *s,
3170
* if we haven't sent anything, we don't want to
3171
* recalculate. 10000 is a small enough number for our purposes
3172
*/
3173
- if (qatomic_read(&mig_stats.dirty_pages_rate) &&
3174
- transferred > 10000) {
3173
+ if (mig_stats.dirty_pages_rate && transferred > 10000) {
3174
s->expected_downtime =
3176
- qatomic_read(&mig_stats.dirty_bytes_last_sync) / expected_bw_per_ms;
3175
+ mig_stats.dirty_bytes_last_sync / switchover_bw_per_ms;
3176
}
3177
3178
migration_rate_reset();
@@ -3182,7 +3181,8 @@ static void migration_update_counters(MigrationState *s,
3181
3182
trace_migrate_transferred(transferred, time_spent,
3183
/* Both in unit bytes/ms */
3185
- bandwidth, switchover_bw / 1000,
3184
+ (uint64_t)s->mbps,
3185
+ (uint64_t)switchover_bw_per_ms,
3186
s->threshold_size);
3187
}
3188