Skip to contentSkip to conversation

several migrations related patches

PATCH Superseded Peter Xu 3 participants 17 messages Mar 27, 2017 – Apr 4, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

cover several migrations related patches

Mostly small touch-ups, the last one looks like a cpu throttle bug on time slice calculation (or I missed anything?). Anyway, please review, thanks.

Peter Xu (5):
migration: set current_active_state once
migration: rename max_size to threshold_size
hmp: info migrate_capability format tunes
hmp: info migrate_parameters format tunes
cpu: throttle: fix throttle time slice

 cpus.c                      |  6 ++----
 hmp.c                       | 26 +++++++++++---------------
 include/migration/vmstate.h |  3 ++-
 migration/migration.c       | 18 +++++++++---------
 migration/savevm.c          |  4 ++--
 5 files changed, 26 insertions(+), 31 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/5 migration: set current_active_state once

We set it right above this one. No need to set it twice.

CC Juan Quintela <[email protected]>
CC "Dr. David Alan Gilbert" <[email protected]>
---
 migration/migration.c | 1 -
 1 file changed, 1 deletion(-)
migration/migration.c +0 -1
index 54060f7..f9f4d98 100644
@@ -1938,7 +1938,6 @@ static void *migration_thread(void *opaque)
     qemu_savevm_state_begin(s->to_dst_file, &s->params);
 
     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
-    current_active_state = MIGRATION_STATUS_ACTIVE;
     migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
                       MIGRATION_STATUS_ACTIVE);
 
Signature · 1 line
2.7.4
author
#3 lore raw

PATCH 2/5 migration: rename max_size to threshold_size

In reply to Peter Xu

In migration codes (especially in migration_thread()), max_size is used in many place for the threshold value that we will start to do the final flush and jump to the next stage to dump the whole rest things to destination. However its name is confusing to first readers. Let's rename it to "threshold_size" when proper and add a comment for it. No functional change is made.

CC Juan Quintela <[email protected]>
CC "Dr. David Alan Gilbert" <[email protected]>
---
 include/migration/vmstate.h |  3 ++-
 migration/migration.c       | 17 +++++++++--------
 migration/savevm.c          |  4 ++--
 3 files changed, 13 insertions(+), 11 deletions(-)
include/migration/vmstate.h +2 -1
index f2dbf84..dad3984 100644
@@ -56,7 +56,8 @@ typedef struct SaveVMHandlers {
 
     /* This runs outside the iothread lock!  */
     int (*save_live_setup)(QEMUFile *f, void *opaque);
-    void (*save_live_pending)(QEMUFile *f, void *opaque, uint64_t max_size,
+    void (*save_live_pending)(QEMUFile *f, void *opaque,
+                              uint64_t threshold_size,
                               uint64_t *non_postcopiable_pending,
                               uint64_t *postcopiable_pending);
     LoadStateHandler *load_state;
migration/migration.c +9 -8
index f9f4d98..b065fe4 100644
@@ -1907,7 +1907,8 @@ static void *migration_thread(void *opaque)
     int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
     int64_t initial_bytes = 0;
-    int64_t max_size = 0;
+    /* We'll do the final flush when reachs threshold_size */
+    int64_t threshold_size = 0;
     int64_t start_time = initial_time;
     int64_t end_time;
     bool old_vm_running = false;
@@ -1951,17 +1952,17 @@ static void *migration_thread(void *opaque)
         if (!qemu_file_rate_limit(s->to_dst_file)) {
             uint64_t pend_post, pend_nonpost;
 
-            qemu_savevm_state_pending(s->to_dst_file, max_size, &pend_nonpost,
-                                      &pend_post);
+            qemu_savevm_state_pending(s->to_dst_file, threshold_size,
+                                      &pend_nonpost, &pend_post);
             pending_size = pend_nonpost + pend_post;
-            trace_migrate_pending(pending_size, max_size,
+            trace_migrate_pending(pending_size, threshold_size,
                                   pend_post, pend_nonpost);
-            if (pending_size && pending_size >= max_size) {
+            if (pending_size && pending_size >= threshold_size) {
                 /* Still a significant amount to transfer */
 
                 if (migrate_postcopy_ram() &&
                     s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
-                    pend_nonpost <= max_size &&
+                    pend_nonpost <= threshold_size &&
                     atomic_read(&s->start_postcopy)) {
 
                     if (!postcopy_start(s, &old_vm_running)) {
@@ -1993,13 +1994,13 @@ static void *migration_thread(void *opaque)
                                          initial_bytes;
             uint64_t time_spent = current_time - initial_time;
             double bandwidth = (double)transferred_bytes / time_spent;
-            max_size = bandwidth * s->parameters.downtime_limit;
+            threshold_size = bandwidth * s->parameters.downtime_limit;
 
             s->mbps = (((double) transferred_bytes * 8.0) /
                     ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
 
             trace_migrate_transferred(transferred_bytes, time_spent,
-                                      bandwidth, max_size);
+                                      bandwidth, threshold_size);
             /* if we haven't sent anything, we don't want to recalculate
                10000 is a small enough number for our purposes */
             if (s->dirty_bytes_rate && transferred_bytes > 10000) {
migration/savevm.c +2 -2
index 3b19a4a..59c04eb 100644
@@ -1197,7 +1197,7 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
  * the result is split into the amount for units that can and
  * for units that can't do postcopy.
  */
-void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
+void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
                                uint64_t *res_non_postcopiable,
                                uint64_t *res_postcopiable)
 {
@@ -1216,7 +1216,7 @@ void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
                 continue;
             }
         }
-        se->ops->save_live_pending(f, se->opaque, max_size,
+        se->ops->save_live_pending(f, se->opaque, threshold_size,
                                    res_non_postcopiable, res_postcopiable);
     }
 }
Signature · 1 line
2.7.4
author
#6 lore raw

PATCH 3/5 hmp: info migrate_capability format tunes

In reply to Peter Xu

Dump the info in a single line is hard to read. Do it one per line. Also, the first "capabilities:" didn't help much. Let's remove it.

CC "Dr. David Alan Gilbert" <[email protected]>
---
 hmp.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hmp.c +1 -3
index edb8970..95eef8c 100644
@@ -265,13 +265,11 @@ void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
     caps = qmp_query_migrate_capabilities(NULL);
 
     if (caps) {
-        monitor_printf(mon, "capabilities: ");
         for (cap = caps; cap; cap = cap->next) {
-            monitor_printf(mon, "%s: %s ",
+            monitor_printf(mon, "%s: %s\n",
                            MigrationCapability_lookup[cap->value->capability],
                            cap->value->state ? "on" : "off");
         }
-        monitor_printf(mon, "\n");
     }
 
     qapi_free_MigrationCapabilityStatusList(caps);
Signature · 1 line
2.7.4
author
#8 lore raw

PATCH 4/5 hmp: info migrate_parameters format tunes

In reply to Peter Xu

Do the same (one per line) to the parameter list.

CC "Dr. David Alan Gilbert" <[email protected]>
---
 hmp.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
hmp.c +10 -12
index 95eef8c..b33e39e 100644
@@ -282,46 +282,44 @@ void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
     params = qmp_query_migrate_parameters(NULL);
 
     if (params) {
-        monitor_printf(mon, "parameters:");
         assert(params->has_compress_level);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_LEVEL],
             params->compress_level);
         assert(params->has_compress_threads);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_THREADS],
             params->compress_threads);
         assert(params->has_decompress_threads);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_DECOMPRESS_THREADS],
             params->decompress_threads);
         assert(params->has_cpu_throttle_initial);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL],
             params->cpu_throttle_initial);
         assert(params->has_cpu_throttle_increment);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT],
             params->cpu_throttle_increment);
-        monitor_printf(mon, " %s: '%s'",
+        monitor_printf(mon, "%s: '%s'\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_CREDS],
             params->has_tls_creds ? params->tls_creds : "");
-        monitor_printf(mon, " %s: '%s'",
+        monitor_printf(mon, "%s: '%s'\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_HOSTNAME],
             params->has_tls_hostname ? params->tls_hostname : "");
         assert(params->has_max_bandwidth);
-        monitor_printf(mon, " %s: %" PRId64 " bytes/second",
+        monitor_printf(mon, "%s: %" PRId64 " bytes/second\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_MAX_BANDWIDTH],
             params->max_bandwidth);
         assert(params->has_downtime_limit);
-        monitor_printf(mon, " %s: %" PRId64 " milliseconds",
+        monitor_printf(mon, "%s: %" PRId64 " milliseconds\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_DOWNTIME_LIMIT],
             params->downtime_limit);
         assert(params->has_x_checkpoint_delay);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_X_CHECKPOINT_DELAY],
             params->x_checkpoint_delay);
-        monitor_printf(mon, "\n");
     }
 
     qapi_free_MigrationParameters(params);
Signature · 1 line
2.7.4
author
#10 lore raw

PATCH 5/5 cpu: throttle: fix throttle time slice

In reply to Peter Xu

IIUC the throttle idea is that: we split a CPU_THROTTLE_TIMESLICE_NS time slice into two parts - one for vcpu, one for throttle thread (which will suspend the thread by a sleep). However current algorithm on calculating the working piece and sleeping piece is strange.

Assume a 99% throttle, what we want is to merely stop vcpu from running, but the old logic will just first let the vcpu run for a very long time (which is "CPU_THROTTLE_TIMESLICE_NS / (1-pct)" = 1 second) before doing anything else.

Fixing it up to the simplest but imho accurate logic.

CC Paolo Bonzini <[email protected]>
CC Richard Henderson <[email protected]>
CC Jason J. Herne <[email protected]>
---
 cpus.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
cpus.c +2 -4
index 167d961..7976ce4 100644
@@ -633,7 +633,6 @@ static const VMStateDescription vmstate_timers = {
 static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
 {
     double pct;
-    double throttle_ratio;
     long sleeptime_ns;
 
     if (!cpu_throttle_get_percentage()) {
@@ -641,8 +640,7 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
     }
 
     pct = (double)cpu_throttle_get_percentage()/100;
-    throttle_ratio = pct / (1 - pct);
-    sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
+    sleeptime_ns = (long)((1 - pct) * CPU_THROTTLE_TIMESLICE_NS);
 
     qemu_mutex_unlock_iothread();
     atomic_set(&cpu->throttle_thread_scheduled, 0);
@@ -668,7 +666,7 @@ static void cpu_throttle_timer_tick(void *opaque)
 
     pct = (double)cpu_throttle_get_percentage()/100;
     timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
-                                   CPU_THROTTLE_TIMESLICE_NS / (1-pct));
+                                   CPU_THROTTLE_TIMESLICE_NS * pct);
 }
 
 void cpu_throttle_set(int new_throttle_pct)
Signature · 1 line
2.7.4
author
#11 lore raw

Re: [Qemu-devel] [PATCH 5/5] cpu: throttle: fix throttle time slice

Quoted 11 lines · Peter Xu

IIUC the throttle idea is that: we split a CPU_THROTTLE_TIMESLICE_NS time slice into two parts - one for vcpu, one for throttle thread (which will suspend the thread by a sleep). However current algorithm on calculating the working piece and sleeping piece is strange.

Assume a 99% throttle, what we want is to merely stop vcpu from running, but the old logic will just first let the vcpu run for a very long time (which is "CPU_THROTTLE_TIMESLICE_NS / (1-pct)" = 1 second) before doing anything else.

Fixing it up to the simplest but imho accurate logic.

Oh, looks like I need to switch the two pct below... :)

Quoted 22 lines
CC Paolo Bonzini <[email protected]>
CC Richard Henderson <[email protected]>
CC Jason J. Herne <[email protected]>
---
 cpus.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
cpus.c +0 -1
index 167d961..7976ce4 100644
@@ -633,7 +633,6 @@ static const VMStateDescription vmstate_timers = {
 static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
 {
     double pct;
-    double throttle_ratio;
     long sleeptime_ns;
 
     if (!cpu_throttle_get_percentage()) {

cpus.c static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) · line 643

Quoted 6 lines
cpus.c +1 -2
@@ -641,8 +640,7 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
     }
 
     pct = (double)cpu_throttle_get_percentage()/100;
-    throttle_ratio = pct / (1 - pct);
-    sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
+    sleeptime_ns = (long)((1 - pct) * CPU_THROTTLE_TIMESLICE_NS);

^^^^^^^^^
here it should be "pct", while...

cpus.c static void cpu_throttle_timer_tick(void *opaque) · line 669

Quoted 9 lines
cpus.c +1 -1
     qemu_mutex_unlock_iothread();
     atomic_set(&cpu->throttle_thread_scheduled, 0);
@@ -668,7 +666,7 @@ static void cpu_throttle_timer_tick(void *opaque)
 
     pct = (double)cpu_throttle_get_percentage()/100;
     timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
-                                   CPU_THROTTLE_TIMESLICE_NS / (1-pct));
+                                   CPU_THROTTLE_TIMESLICE_NS * pct);

^^^
here it should be (1 - pct)

I'll wait for review comment on the raw idea, to see whether I will need a repost. Sorry for the misunderstanding.

-- peterx

#12 lore raw

Re: [Qemu-devel] [PATCH 5/5] cpu: throttle: fix throttle time slice

In reply to Peter Xu

cpus.c static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) · line 645

Quoted 6 lines · Peter Xu
cpus.c +0 -2
@@ -641,8 +640,7 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
     }
 
     pct = (double)cpu_throttle_get_percentage()/100;
-    throttle_ratio = pct / (1 - pct);
-    sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);

Say pct = 0.25, then throttle_ratio = 0.25/0.75 = 1/3.

cpus.c static void cpu_throttle_timer_tick(void *opaque) · line 671

Quoted 9 lines
cpus.c +1 -1
+    sleeptime_ns = (long)((1 - pct) * CPU_THROTTLE_TIMESLICE_NS);
 
     qemu_mutex_unlock_iothread();
     atomic_set(&cpu->throttle_thread_scheduled, 0);
@@ -668,7 +666,7 @@ static void cpu_throttle_timer_tick(void *opaque)
 
     pct = (double)cpu_throttle_get_percentage()/100;
     timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
-                                   CPU_THROTTLE_TIMESLICE_NS / (1-pct));

And the timer is running every 1/0.75 = 4/3 * CPU_THROTTLE_TIMESLICE_NS.

Of these, 1/3 is spent sleeping (3.33 ms), while 1 (10 ms) is spent not sleeping.

When pct = 0.75, throttle_ratio = 3 and the timer is running every 4 * CPU_THROTTLE_TIMESLICE_NS (40 ms). Of these, 3 slices (30 ms) are spent sleeping, while 10 ms are spent not sleeping.

The rationale _could_ be (I don't remember) that a CPU with a very high throttling frequency leaves little time for the migration thread to do any work. So QEMU keeps the "on" phase always the same and lengthens the "off" phase, which as you found out can be unsatisfactory.

However, I think your patch has the opposite problem: the frequency is constant, but with high throttling all time reserved for the CPU will be lost in overhead. For example, at 99% throttling you only have 100 microseconds to wake up, do work and go back to sleep.

So I'm inclined _not_ to take your patch. One possibility could be to do the following:

- for throttling between 0% and 80%, use the current algorithm. At 66%, the CPU will work for 10 ms and sleep for 40 ms.

- for throttling above 80% adapt your algorithm to have a variable timeslice, going from 50 ms at 66% to 100 ms at 100%. This way, the CPU time will shrink below 10 ms and the sleep time will grow.

It looks like this: http://i.imgur.com/lyFie04.png

So at 99% the timeslice will be 97.5 ms; the CPU will work for 975 us and sleep for the rest (10x more than with just your patch). But I'm not sure it's really worth it.

Paolo

Quoted 2 lines
cpus.c +1 -0
+                                   CPU_THROTTLE_TIMESLICE_NS * pct);
 }
Reviewed-by on v1 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH 1/5] migration: set current_active_state once

In reply to Peter Xu
Quoted 1 line · * Peter Xu ([email protected]) wrote:

We set it right above this one. No need to set it twice.

Ah good spot, yes set in the declaration of current_active_state.

Quoted 12 lines
CC Juan Quintela <[email protected]>
CC "Dr. David Alan Gilbert" <[email protected]>
---
 migration/migration.c | 1 -
 1 file changed, 1 deletion(-)
migration/migration.c +0 -0
index 54060f7..f9f4d98 100644

migration/migration.c static void *migration_thread(void *opaque) · line 1942

Quoted 6 lines
migration/migration.c +0 -1
@@ -1938,7 +1938,6 @@ static void *migration_thread(void *opaque)
     qemu_savevm_state_begin(s->to_dst_file, &s->params);
 
     s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
-    current_active_state = MIGRATION_STATUS_ACTIVE;
     migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
                       MIGRATION_STATUS_ACTIVE);
Quoted 4 lines
Signature · 2 lines
-- 
2.7.4

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Reviewed-by on v1 2/5
#4 lore raw

Re: [Qemu-devel] [PATCH 2/5] migration: rename max_size to threshold_size

In reply to Peter Xu
Quoted 35 lines · * Peter Xu ([email protected]) wrote:

In migration codes (especially in migration_thread()), max_size is used in many place for the threshold value that we will start to do the final flush and jump to the next stage to dump the whole rest things to destination. However its name is confusing to first readers. Let's rename it to "threshold_size" when proper and add a comment for it. No functional change is made.

CC Juan Quintela <[email protected]>
CC "Dr. David Alan Gilbert" <[email protected]>
---
 include/migration/vmstate.h |  3 ++-
 migration/migration.c       | 17 +++++++++--------
 migration/savevm.c          |  4 ++--
 3 files changed, 13 insertions(+), 11 deletions(-)
include/migration/vmstate.h +2 -1
index f2dbf84..dad3984 100644
@@ -56,7 +56,8 @@ typedef struct SaveVMHandlers {
 
     /* This runs outside the iothread lock!  */
     int (*save_live_setup)(QEMUFile *f, void *opaque);
-    void (*save_live_pending)(QEMUFile *f, void *opaque, uint64_t max_size,
+    void (*save_live_pending)(QEMUFile *f, void *opaque,
+                              uint64_t threshold_size,
                               uint64_t *non_postcopiable_pending,
                               uint64_t *postcopiable_pending);
     LoadStateHandler *load_state;
migration/migration.c +0 -0
index f9f4d98..b065fe4 100644

migration/migration.c static void *migration_thread(void *opaque) · line 1910

Quoted 5 lines
migration/migration.c +1 -1
@@ -1907,7 +1907,8 @@ static void *migration_thread(void *opaque)
     int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
     int64_t initial_bytes = 0;
-    int64_t max_size = 0;
+    /* We'll do the final flush when reachs threshold_size */

I think that's 'reaches' - however perhaps we should make a more explicit comment:
'The final stage happens when the remaining data is smaller than
this threshold; it's calculated from the requested downtime and measured bandwidth'

other than that:

Dave

Quoted 68 lines
migration/migration.c +8 -7
+    int64_t threshold_size = 0;
     int64_t start_time = initial_time;
     int64_t end_time;
     bool old_vm_running = false;
@@ -1951,17 +1952,17 @@ static void *migration_thread(void *opaque)
         if (!qemu_file_rate_limit(s->to_dst_file)) {
             uint64_t pend_post, pend_nonpost;
 
-            qemu_savevm_state_pending(s->to_dst_file, max_size, &pend_nonpost,
-                                      &pend_post);
+            qemu_savevm_state_pending(s->to_dst_file, threshold_size,
+                                      &pend_nonpost, &pend_post);
             pending_size = pend_nonpost + pend_post;
-            trace_migrate_pending(pending_size, max_size,
+            trace_migrate_pending(pending_size, threshold_size,
                                   pend_post, pend_nonpost);
-            if (pending_size && pending_size >= max_size) {
+            if (pending_size && pending_size >= threshold_size) {
                 /* Still a significant amount to transfer */
 
                 if (migrate_postcopy_ram() &&
                     s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE &&
-                    pend_nonpost <= max_size &&
+                    pend_nonpost <= threshold_size &&
                     atomic_read(&s->start_postcopy)) {
 
                     if (!postcopy_start(s, &old_vm_running)) {
@@ -1993,13 +1994,13 @@ static void *migration_thread(void *opaque)
                                          initial_bytes;
             uint64_t time_spent = current_time - initial_time;
             double bandwidth = (double)transferred_bytes / time_spent;
-            max_size = bandwidth * s->parameters.downtime_limit;
+            threshold_size = bandwidth * s->parameters.downtime_limit;
 
             s->mbps = (((double) transferred_bytes * 8.0) /
                     ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
 
             trace_migrate_transferred(transferred_bytes, time_spent,
-                                      bandwidth, max_size);
+                                      bandwidth, threshold_size);
             /* if we haven't sent anything, we don't want to recalculate
                10000 is a small enough number for our purposes */
             if (s->dirty_bytes_rate && transferred_bytes > 10000) {
migration/savevm.c +2 -2
index 3b19a4a..59c04eb 100644
@@ -1197,7 +1197,7 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
  * the result is split into the amount for units that can and
  * for units that can't do postcopy.
  */
-void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
+void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
                                uint64_t *res_non_postcopiable,
                                uint64_t *res_postcopiable)
 {
@@ -1216,7 +1216,7 @@ void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
                 continue;
             }
         }
-        se->ops->save_live_pending(f, se->opaque, max_size,
+        se->ops->save_live_pending(f, se->opaque, threshold_size,
                                    res_non_postcopiable, res_postcopiable);
     }
 }
Signature · 1 line
2.7.4

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Reviewed-by on v1 3/5
#7 lore raw

Re: [Qemu-devel] [PATCH 3/5] hmp: info migrate_capability format tunes

In reply to Peter Xu
Quoted 5 lines · * Peter Xu ([email protected]) wrote:

Dump the info in a single line is hard to read. Do it one per line. Also, the first "capabilities:" didn't help much. Let's remove it.

CC "Dr. David Alan Gilbert" <[email protected]>

agreed, although you may find it breaks some peoples scripts, but we're allowed to do that in HMP and it should be more readable, so:

Quoted 26 lines
---
 hmp.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hmp.c +1 -3
index edb8970..95eef8c 100644
@@ -265,13 +265,11 @@ void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
     caps = qmp_query_migrate_capabilities(NULL);
 
     if (caps) {
-        monitor_printf(mon, "capabilities: ");
         for (cap = caps; cap; cap = cap->next) {
-            monitor_printf(mon, "%s: %s ",
+            monitor_printf(mon, "%s: %s\n",
                            MigrationCapability_lookup[cap->value->capability],
                            cap->value->state ? "on" : "off");
         }
-        monitor_printf(mon, "\n");
     }
 
     qapi_free_MigrationCapabilityStatusList(caps);
Signature · 1 line
2.7.4

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

Reviewed-by on v1 4/5
#9 lore raw

Re: [Qemu-devel] [PATCH 4/5] hmp: info migrate_parameters format tunes

In reply to Peter Xu
Quoted 4 lines · * Peter Xu ([email protected]) wrote:

Do the same (one per line) to the parameter list.

CC "Dr. David Alan Gilbert" <[email protected]>
Quoted 68 lines
---
 hmp.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
hmp.c +10 -12
index 95eef8c..b33e39e 100644
@@ -282,46 +282,44 @@ void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
     params = qmp_query_migrate_parameters(NULL);
 
     if (params) {
-        monitor_printf(mon, "parameters:");
         assert(params->has_compress_level);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_LEVEL],
             params->compress_level);
         assert(params->has_compress_threads);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_THREADS],
             params->compress_threads);
         assert(params->has_decompress_threads);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_DECOMPRESS_THREADS],
             params->decompress_threads);
         assert(params->has_cpu_throttle_initial);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL],
             params->cpu_throttle_initial);
         assert(params->has_cpu_throttle_increment);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT],
             params->cpu_throttle_increment);
-        monitor_printf(mon, " %s: '%s'",
+        monitor_printf(mon, "%s: '%s'\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_CREDS],
             params->has_tls_creds ? params->tls_creds : "");
-        monitor_printf(mon, " %s: '%s'",
+        monitor_printf(mon, "%s: '%s'\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_TLS_HOSTNAME],
             params->has_tls_hostname ? params->tls_hostname : "");
         assert(params->has_max_bandwidth);
-        monitor_printf(mon, " %s: %" PRId64 " bytes/second",
+        monitor_printf(mon, "%s: %" PRId64 " bytes/second\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_MAX_BANDWIDTH],
             params->max_bandwidth);
         assert(params->has_downtime_limit);
-        monitor_printf(mon, " %s: %" PRId64 " milliseconds",
+        monitor_printf(mon, "%s: %" PRId64 " milliseconds\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_DOWNTIME_LIMIT],
             params->downtime_limit);
         assert(params->has_x_checkpoint_delay);
-        monitor_printf(mon, " %s: %" PRId64,
+        monitor_printf(mon, "%s: %" PRId64 "\n",
             MigrationParameter_lookup[MIGRATION_PARAMETER_X_CHECKPOINT_DELAY],
             params->x_checkpoint_delay);
-        monitor_printf(mon, "\n");
     }
 
     qapi_free_MigrationParameters(params);
Signature · 1 line
2.7.4

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#13 lore raw

Re: [Qemu-devel] [PATCH 5/5] cpu: throttle: fix throttle time slice

In reply to Paolo Bonzini

Ignoring the details below for a minute, this patch belongs in a separate series; all the rest of the patches in this set are nice simple ones.

Quoted 36 lines · * Paolo Bonzini ([email protected]) wrote:

cpus.c static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) · line 645

Quoted 6 lines · Peter Xu
cpus.c +0 -2
@@ -641,8 +640,7 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
     }
 
     pct = (double)cpu_throttle_get_percentage()/100;
-    throttle_ratio = pct / (1 - pct);
-    sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);

Say pct = 0.25, then throttle_ratio = 0.25/0.75 = 1/3.

cpus.c static void cpu_throttle_timer_tick(void *opaque) · line 671

Quoted 9 lines
cpus.c +1 -1
+    sleeptime_ns = (long)((1 - pct) * CPU_THROTTLE_TIMESLICE_NS);
 
     qemu_mutex_unlock_iothread();
     atomic_set(&cpu->throttle_thread_scheduled, 0);
@@ -668,7 +666,7 @@ static void cpu_throttle_timer_tick(void *opaque)
 
     pct = (double)cpu_throttle_get_percentage()/100;
     timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
-                                   CPU_THROTTLE_TIMESLICE_NS / (1-pct));

And the timer is running every 1/0.75 = 4/3 * CPU_THROTTLE_TIMESLICE_NS.

Of these, 1/3 is spent sleeping (3.33 ms), while 1 (10 ms) is spent not sleeping.

When pct = 0.75, throttle_ratio = 3 and the timer is running every 4 * CPU_THROTTLE_TIMESLICE_NS (40 ms). Of these, 3 slices (30 ms) are spent sleeping, while 10 ms are spent not sleeping.

The rationale _could_ be (I don't remember) that a CPU with a very high throttling frequency leaves little time for the migration thread to do any work. So QEMU keeps the "on" phase always the same and lengthens the "off" phase, which as you found out can be unsatisfactory.

Quoted 4 lines

However, I think your patch has the opposite problem: the frequency is constant, but with high throttling all time reserved for the CPU will be lost in overhead. For example, at 99% throttling you only have 100 microseconds to wake up, do work and go back to sleep.

Yes, and I'm worried that with the 10ms timeslice it is a lot of overhead, especially if your timer that wakes you have that much higher resolution than that.

Quoted 9 lines

So I'm inclined _not_ to take your patch. One possibility could be to do the following:

- for throttling between 0% and 80%, use the current algorithm. At 66%, the CPU will work for 10 ms and sleep for 40 ms.

- for throttling above 80% adapt your algorithm to have a variable timeslice, going from 50 ms at 66% to 100 ms at 100%. This way, the CPU time will shrink below 10 ms and the sleep time will grow.

It seems odd to have a threshold like that on something that's supposedly a linear scale.

Quoted 5 lines

It looks like this: http://i.imgur.com/lyFie04.png

So at 99% the timeslice will be 97.5 ms; the CPU will work for 975 us and sleep for the rest (10x more than with just your patch). But I'm not sure it's really worth it.

Can you really run a CPU for 975us ?

Dave

Quoted 4 lines

Paolo

Quoted 2 lines
cpus.c +1 -0
+                                   CPU_THROTTLE_TIMESLICE_NS * pct);
 }

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#14 lore raw

Re: [Qemu-devel] [PATCH 5/5] cpu: throttle: fix throttle time slice

Quoted 11 lines
Quoted 11 lines

So I'm inclined _not_ to take your patch. One possibility could be to do the following:

- for throttling between 0% and 80%, use the current algorithm. At 66%, the CPU will work for 10 ms and sleep for 40 ms.

- for throttling above 80% adapt your algorithm to have a variable timeslice, going from 50 ms at 66% to 100 ms at 100%. This way, the CPU time will shrink below 10 ms and the sleep time will grow.

Oops, all the 66% should be 80%.

Quoted 2 lines

It seems odd to have a threshold like that on something that's supposedly a linear scale.

I futzed a bit with the threshold until the first derivative of the CPU time was zero at the threshold, and the result was 80%. That is, if you switch before 80%, the CPU time slice can grow to more than 10 ms right after the threshold, and then start shrinking.

Quoted 7 lines
Quoted 5 lines

It looks like this: http://i.imgur.com/lyFie04.png

So at 99% the timeslice will be 97.5 ms; the CPU will work for 975 us and sleep for the rest (10x more than with just your patch). But I'm not sure it's really worth it.

Can you really run a CPU for 975us ?

It's 2-3 million clock cycles, should be doable.

Paolo

author
#5 lore raw

Re: [Qemu-devel] [PATCH 2/5] migration: rename max_size to threshold_size

In reply to Dr. David Alan Gilbert
Quoted 39 lines · Dr. David Alan Gilbert
Quoted 38 lines · * Peter Xu ([email protected]) wrote:

In migration codes (especially in migration_thread()), max_size is used in many place for the threshold value that we will start to do the final flush and jump to the next stage to dump the whole rest things to destination. However its name is confusing to first readers. Let's rename it to "threshold_size" when proper and add a comment for it. No functional change is made.

CC Juan Quintela <[email protected]>
CC "Dr. David Alan Gilbert" <[email protected]>
---
 include/migration/vmstate.h |  3 ++-
 migration/migration.c       | 17 +++++++++--------
 migration/savevm.c          |  4 ++--
 3 files changed, 13 insertions(+), 11 deletions(-)
include/migration/vmstate.h +2 -1
index f2dbf84..dad3984 100644
@@ -56,7 +56,8 @@ typedef struct SaveVMHandlers {
 
     /* This runs outside the iothread lock!  */
     int (*save_live_setup)(QEMUFile *f, void *opaque);
-    void (*save_live_pending)(QEMUFile *f, void *opaque, uint64_t max_size,
+    void (*save_live_pending)(QEMUFile *f, void *opaque,
+                              uint64_t threshold_size,
                               uint64_t *non_postcopiable_pending,
                               uint64_t *postcopiable_pending);
     LoadStateHandler *load_state;
migration/migration.c +0 -0
index f9f4d98..b065fe4 100644
@@ -1907,7 +1907,8 @@ static void *migration_thread(void *opaque)
     int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
     int64_t initial_bytes = 0;
Quoted 8 lines

migration/migration.c static void *migration_thread(void *opaque) · line 1910

Quoted 2 lines
migration/migration.c +1 -1
-    int64_t max_size = 0;
+    /* We'll do the final flush when reachs threshold_size */

I think that's 'reaches' - however perhaps we should make a more explicit comment:
'The final stage happens when the remaining data is smaller than
this threshold; it's calculated from the requested downtime and measured bandwidth'

Yes it looks better. Will "steal" that. :)

Quoted 3 lines

other than that:

Thanks!

-- peterx

author
#16 lore raw

Re: [Qemu-devel] [PATCH 5/5] cpu: throttle: fix throttle time slice

In reply to Paolo Bonzini
Quoted 27 lines · Paolo Bonzini

cpus.c static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) · line 645

Quoted 6 lines · Peter Xu
cpus.c +0 -2
@@ -641,8 +640,7 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
     }
 
     pct = (double)cpu_throttle_get_percentage()/100;
-    throttle_ratio = pct / (1 - pct);
-    sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);

Say pct = 0.25, then throttle_ratio = 0.25/0.75 = 1/3.

cpus.c static void cpu_throttle_timer_tick(void *opaque) · line 671

Quoted 9 lines
cpus.c +1 -1
+    sleeptime_ns = (long)((1 - pct) * CPU_THROTTLE_TIMESLICE_NS);
 
     qemu_mutex_unlock_iothread();
     atomic_set(&cpu->throttle_thread_scheduled, 0);
@@ -668,7 +666,7 @@ static void cpu_throttle_timer_tick(void *opaque)
 
     pct = (double)cpu_throttle_get_percentage()/100;
     timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
-                                   CPU_THROTTLE_TIMESLICE_NS / (1-pct));

And the timer is running every 1/0.75 = 4/3 * CPU_THROTTLE_TIMESLICE_NS.

Of these, 1/3 is spent sleeping (3.33 ms), while 1 (10 ms) is spent not sleeping.

Quoted 8 lines

When pct = 0.75, throttle_ratio = 3 and the timer is running every 4 * CPU_THROTTLE_TIMESLICE_NS (40 ms). Of these, 3 slices (30 ms) are spent sleeping, while 10 ms are spent not sleeping.

The rationale _could_ be (I don't remember) that a CPU with a very high throttling frequency leaves little time for the migration thread to do any work. So QEMU keeps the "on" phase always the same and lengthens the "off" phase, which as you found out can be unsatisfactory.

Yes. Sorry I must have done an incorrect math that day. Current algorithm is correct, it just assumes that the running time is constant, which is CPU_THROTTLE_TIMESLICE_NS (10ms). I just didn't realize it at the first glance.

Quoted 16 lines

However, I think your patch has the opposite problem: the frequency is constant, but with high throttling all time reserved for the CPU will be lost in overhead. For example, at 99% throttling you only have 100 microseconds to wake up, do work and go back to sleep.

So I'm inclined _not_ to take your patch. One possibility could be to do the following:

- for throttling between 0% and 80%, use the current algorithm. At 66%, the CPU will work for 10 ms and sleep for 40 ms.

- for throttling above 80% adapt your algorithm to have a variable timeslice, going from 50 ms at 66% to 100 ms at 100%. This way, the CPU time will shrink below 10 ms and the sleep time will grow.

Quoted 5 lines

It looks like this: http://i.imgur.com/lyFie04.png

So at 99% the timeslice will be 97.5 ms; the CPU will work for 975 us and sleep for the rest (10x more than with just your patch). But I'm not sure it's really worth it.

Yeah. It may not that worth it. Thanks for the analysis. :-)

Will drop this patch in next post.

Thanks,

-- peterx

#15 lore raw

Re: [Qemu-devel] [PATCH 5/5] cpu: throttle: fix throttle time slice

In reply to Paolo Bonzini
Quoted 24 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 11 lines
Quoted 11 lines

So I'm inclined _not_ to take your patch. One possibility could be to do the following:

- for throttling between 0% and 80%, use the current algorithm. At 66%, the CPU will work for 10 ms and sleep for 40 ms.

- for throttling above 80% adapt your algorithm to have a variable timeslice, going from 50 ms at 66% to 100 ms at 100%. This way, the CPU time will shrink below 10 ms and the sleep time will grow.

Oops, all the 66% should be 80%.

Quoted 2 lines

It seems odd to have a threshold like that on something that's supposedly a linear scale.

I futzed a bit with the threshold until the first derivative of the CPU time was zero at the threshold, and the result was 80%. That is, if you switch before 80%, the CPU time slice can grow to more than 10 ms right after the threshold, and then start shrinking.

Quoted 2 lines
Quoted 2 lines

It looks like this: http://i.imgur.com/lyFie04.png

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

So at 99% the timeslice will be 97.5 ms; the CPU will work for 975 us and sleep for the rest (10x more than with just your patch). But I'm not sure it's really worth it.

Can you really run a CPU for 975us ?

It's 2-3 million clock cycles, should be doable.

I wasn't really worrying about the CPU running, I was more worried about timer resolution in stopping it. If you're timer isn't that accurate in starting/stopping the CPU then the errors might be so large as to make that a bit odd.

Dave

Quoted 1 line

Paolo

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK