Skip to contentSkip to conversation

QEMU file cleanups

PATCH In review Juan Quintela 3 participants 32 messages May 4, 2023 – May 5, 2023

lore
Share & tools
lore
#0 lore raw

cover QEMU file cleanups

Hi

While I am trying to put order in the atomic counters, I made in this series:

- convince and review code to see that everything is uint64_t.

- f->shutdown is not needed. When we shutdown the file we put an error there if there is none. So remove it.
- Make more clear how we use rate_limit.

Please review.

It is based on my previous series to the list:
Subject: [PATCH 0/2] More migration stats
Based-on: Message-Id: <[email protected]>

Juan Quintela (9):
migration: max_postcopy_bandwidth is a size parameter
migration: qemu_file_total_transferred() function is monotonic
qemu-file: make qemu_file_[sg]et_rate_limit() use an uint64_t
qemu-file: Make rate_limit_used an uint64_t
qemu-file: No need to check for shutdown in qemu_file_rate_limit
qemu-file: remove shutdown member
qemu-file: Make total_transferred an uint64_t
qemu-file: Make ram_control_save_page() use accessors for rate_limit
qemu-file: Account for rate_limit usage on qemu_fflush()

 migration/block.c     | 13 +++----------
 migration/migration.c | 10 +++++-----
 migration/options.c   |  2 +-
 migration/options.h   |  2 +-
 migration/qemu-file.c | 42 +++++++++++++++++-------------------------
 migration/qemu-file.h | 10 +++++-----
 migration/savevm.c    |  6 ++----
 migration/vmstate.c   |  2 +-
 8 files changed, 35 insertions(+), 52 deletions(-)
Signature · 1 line
2.40.0
#1 lore raw

PATCH 1/9 migration: max_postcopy_bandwidth is a size parameter

So make everything that uses it uint64_t no int64_t.

---
 migration/migration.c | 4 ++--
 migration/options.c   | 2 +-
 migration/options.h   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
migration/migration.c +2 -2
index feb5ab7493..232e387109 100644
@@ -2056,7 +2056,7 @@ static int postcopy_start(MigrationState *ms)
     QIOChannelBuffer *bioc;
     QEMUFile *fb;
     int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
-    int64_t bandwidth = migrate_max_postcopy_bandwidth();
+    uint64_t bandwidth = migrate_max_postcopy_bandwidth();
     bool restart_block = false;
     int cur_state = MIGRATION_STATUS_ACTIVE;
 
@@ -3176,7 +3176,7 @@ fail:
 void migrate_fd_connect(MigrationState *s, Error *error_in)
 {
     Error *local_err = NULL;
-    int64_t rate_limit;
+    uint64_t rate_limit;
     bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
 
     /*
migration/options.c +1 -1
index 7395787960..2e759cc306 100644
@@ -717,7 +717,7 @@ uint64_t migrate_max_bandwidth(void)
     return s->parameters.max_bandwidth;
 }
 
-int64_t migrate_max_postcopy_bandwidth(void)
+uint64_t migrate_max_postcopy_bandwidth(void)
 {
     MigrationState *s = migrate_get_current();
 
migration/options.h +1 -1
index 09841d6a63..5cca3326d6 100644
@@ -85,7 +85,7 @@ int migrate_decompress_threads(void);
 uint64_t migrate_downtime_limit(void);
 uint8_t migrate_max_cpu_throttle(void);
 uint64_t migrate_max_bandwidth(void);
-int64_t migrate_max_postcopy_bandwidth(void);
+uint64_t migrate_max_postcopy_bandwidth(void);
 int migrate_multifd_channels(void);
 MultiFDCompression migrate_multifd_compression(void);
 int migrate_multifd_zlib_level(void);
Signature · 1 line
2.40.0
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] migration: max_postcopy_bandwidth is a size parameter

Quoted 8 lines · Juan Quintela

So make everything that uses it uint64_t no int64_t.

---
 migration/migration.c | 4 ++--
 migration/options.c   | 2 +-
 migration/options.h   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

PATCH 2/9 migration: qemu_file_total_transferred() function is monotonic

In reply to Juan Quintela

So delta_bytes can only be greater or equal to zero. Never negative.

---
 migration/block.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
migration/block.c +1 -7
index 6d532ac7a2..3499f75e37 100644
@@ -801,13 +801,7 @@ static int block_save_iterate(QEMUFile *f, void *opaque)
 
     qemu_put_be64(f, BLK_MIG_FLAG_EOS);
     delta_bytes = qemu_file_total_transferred(f) - last_bytes;
-    if (delta_bytes > 0) {
-        return 1;
-    } else if (delta_bytes < 0) {
-        return -1;
-    } else {
-        return 0;
-    }
+    return (delta_bytes > 0);
 }
 
 /* Called with iothread lock taken.  */
Signature · 1 line
2.40.0
Reviewed-by on v1 2/9
#4 lore raw

Re: [PATCH 2/9] migration: qemu_file_total_transferred() function is monotonic

Quoted 6 lines · Juan Quintela

So delta_bytes can only be greater or equal to zero. Never negative.

---
 migration/block.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw

PATCH 3/9 qemu-file: make qemu_file_[sg]et_rate_limit() use an uint64_t

In reply to Juan Quintela

It is really size_t. Everything else uses uint64_t, so move this to uint64_t as well. A size can't be negative anyways.

---
 migration/migration.c | 6 +++---
 migration/qemu-file.c | 8 ++++----
 migration/qemu-file.h | 4 ++--
 3 files changed, 9 insertions(+), 9 deletions(-)
migration/migration.c +3 -3
index 232e387109..ee75c6cfbd 100644
@@ -2119,7 +2119,7 @@ static int postcopy_start(MigrationState *ms)
      */
     /* 0 max-postcopy-bandwidth means unlimited */
     if (!bandwidth) {
-        qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
+        qemu_file_set_rate_limit(ms->to_dst_file, UINT64_MAX);
     } else {
         qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
     }
@@ -2301,7 +2301,7 @@ static void migration_completion(MigrationState *s)
             }
             if (ret >= 0) {
                 s->block_inactive = !migrate_colo();
-                qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
+                qemu_file_set_rate_limit(s->to_dst_file, UINT64_MAX);
                 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
                                                          s->block_inactive);
             }
@@ -3049,7 +3049,7 @@ static void *bg_migration_thread(void *opaque)
     rcu_register_thread();
     object_ref(OBJECT(s));
 
-    qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
+    qemu_file_set_rate_limit(s->to_dst_file, UINT64_MAX);
 
     setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
     /*
migration/qemu-file.c +4 -4
index ee04240a21..b322b363cf 100644
@@ -43,7 +43,7 @@ struct QEMUFile {
      * Maximum amount of data in bytes to transfer during one
      * rate limiting time window
      */
-    int64_t rate_limit_max;
+    uint64_t rate_limit_max;
     /*
      * Total amount of data in bytes queued for transfer
      * during this rate limiting time window
@@ -748,18 +748,18 @@ int qemu_file_rate_limit(QEMUFile *f)
     if (qemu_file_get_error(f)) {
         return 1;
     }
-    if (f->rate_limit_max > 0 && f->rate_limit_used > f->rate_limit_max) {
+    if (f->rate_limit_used > f->rate_limit_max) {
         return 1;
     }
     return 0;
 }
 
-int64_t qemu_file_get_rate_limit(QEMUFile *f)
+uint64_t qemu_file_get_rate_limit(QEMUFile *f)
 {
     return f->rate_limit_max;
 }
 
-void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
+void qemu_file_set_rate_limit(QEMUFile *f, uint64_t limit)
 {
     f->rate_limit_max = limit;
 }
migration/qemu-file.h +2 -2
index d16cd50448..9e158c00f6 100644
@@ -138,8 +138,8 @@ void qemu_file_reset_rate_limit(QEMUFile *f);
  * need to be applied to the rate limiting calcuations
  */
 void qemu_file_acct_rate_limit(QEMUFile *f, int64_t len);
-void qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate);
-int64_t qemu_file_get_rate_limit(QEMUFile *f);
+void qemu_file_set_rate_limit(QEMUFile *f, uint64_t new_rate);
+uint64_t qemu_file_get_rate_limit(QEMUFile *f);
 int qemu_file_get_error_obj(QEMUFile *f, Error **errp);
 int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp);
 void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err);
Signature · 1 line
2.40.0
Reviewed-by on v1 3/9
#6 lore raw

Re: [PATCH 3/9] qemu-file: make qemu_file_[sg]et_rate_limit() use an uint64_t

Quoted 9 lines · Juan Quintela

It is really size_t. Everything else uses uint64_t, so move this to uint64_t as well. A size can't be negative anyways.

---
 migration/migration.c | 6 +++---
 migration/qemu-file.c | 8 ++++----
 migration/qemu-file.h | 4 ++--
 3 files changed, 9 insertions(+), 9 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw

Re: [PATCH 3/9] qemu-file: make qemu_file_[sg]et_rate_limit() use an uint64_t

In reply to Juan Quintela
Quoted 4 lines · Juan Quintela

It is really size_t. Everything else uses uint64_t, so move this to uint64_t as well. A size can't be negative anyways.

Self-nack.

migration/migration.c static int postcopy_start(MigrationState *ms) · line 2122

Quoted 2 lines
migration/migration.c +1 -1
-        qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
+        qemu_file_set_rate_limit(ms->to_dst_file, UINT64_MAX);

1st: this should be zero.

migration/migration.c static void migration_completion(MigrationState *s) · line 2304

Quoted 9 lines
     } else {
         qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
     }
migration/migration.c +1 -1
@@ -2301,7 +2301,7 @@ static void migration_completion(MigrationState *s)
             }
             if (ret >= 0) {
                 s->block_inactive = !migrate_colo();
-                qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
+                qemu_file_set_rate_limit(s->to_dst_file, UINT64_MAX);

Same here

migration/migration.c static void *bg_migration_thread(void *opaque) · line 3052

Quoted 11 lines
migration/migration.c +1 -1
                 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
                                                          s->block_inactive);
             }
@@ -3049,7 +3049,7 @@ static void *bg_migration_thread(void *opaque)
     rcu_register_thread();
     object_ref(OBJECT(s));
 
-    qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
+    qemu_file_set_rate_limit(s->to_dst_file, UINT64_MAX);

And here.

migration/qemu-file.c int qemu_file_rate_limit(QEMUFile *f) · line 751

Quoted 6 lines
migration/qemu-file.c +1 -1
@@ -748,18 +748,18 @@ int qemu_file_rate_limit(QEMUFile *f)
     if (qemu_file_get_error(f)) {
         return 1;
     }
-    if (f->rate_limit_max > 0 && f->rate_limit_used > f->rate_limit_max) {
+    if (f->rate_limit_used > f->rate_limit_max) {

And this is wrong. f->rate_limit_max == 0 means that we don't do rate_limit.

Will resend this one later.

Sorry, Juan.

PD. No, I have no clue how I have had this patch applied the whole day
and no failures and now I get failures in migration-test. The number of times that I run this test on the last two days have been in the hundreds.

#8 lore raw

PATCH 4/9 qemu-file: Make rate_limit_used an uint64_t

In reply to Juan Quintela

Change all the functions that use it. It was already passed as uint64_t.

---
 migration/qemu-file.c | 4 ++--
 migration/qemu-file.h | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
migration/qemu-file.c +2 -2
index b322b363cf..5aa990b82a 100644
@@ -48,7 +48,7 @@ struct QEMUFile {
      * Total amount of data in bytes queued for transfer
      * during this rate limiting time window
      */
-    int64_t rate_limit_used;
+    uint64_t rate_limit_used;
 
     /* The sum of bytes transferred on the wire */
     int64_t total_transferred;
@@ -769,7 +769,7 @@ void qemu_file_reset_rate_limit(QEMUFile *f)
     f->rate_limit_used = 0;
 }
 
-void qemu_file_acct_rate_limit(QEMUFile *f, int64_t len)
+void qemu_file_acct_rate_limit(QEMUFile *f, uint64_t len)
 {
     f->rate_limit_used += len;
 }
migration/qemu-file.h +1 -1
index 9e158c00f6..55ef5a2ac7 100644
@@ -137,7 +137,7 @@ void qemu_file_reset_rate_limit(QEMUFile *f);
  * out of band from the main file object I/O methods, and
  * need to be applied to the rate limiting calcuations
  */
-void qemu_file_acct_rate_limit(QEMUFile *f, int64_t len);
+void qemu_file_acct_rate_limit(QEMUFile *f, uint64_t len);
 void qemu_file_set_rate_limit(QEMUFile *f, uint64_t new_rate);
 uint64_t qemu_file_get_rate_limit(QEMUFile *f);
 int qemu_file_get_error_obj(QEMUFile *f, Error **errp);
Signature · 1 line
2.40.0
Reviewed-by on v1 4/9
#9 lore raw

Re: [PATCH 4/9] qemu-file: Make rate_limit_used an uint64_t

Quoted 8 lines · Juan Quintela

Change all the functions that use it. It was already passed as uint64_t.

---
 migration/qemu-file.c | 4 ++--
 migration/qemu-file.h | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#10 lore raw

PATCH 5/9 qemu-file: No need to check for shutdown in qemu_file_rate_limit

In reply to Juan Quintela

After calling qemu_file_shutdown() we set the error as -EIO if there is no another previous error, so no need to check it here.

---
 migration/qemu-file.c | 3 ---
 1 file changed, 3 deletions(-)
migration/qemu-file.c +0 -3
index 5aa990b82a..771aba7369 100644
@@ -742,9 +742,6 @@ int64_t qemu_file_total_transferred(QEMUFile *f)
 
 int qemu_file_rate_limit(QEMUFile *f)
 {
-    if (f->shutdown) {
-        return 1;
-    }
     if (qemu_file_get_error(f)) {
         return 1;
     }
Signature · 1 line
2.40.0
Reviewed-by on v1 5/9
#11 lore raw

Re: [PATCH 5/9] qemu-file: No need to check for shutdown in qemu_file_rate_limit

Quoted 4 lines · Juan Quintela

After calling qemu_file_shutdown() we set the error as -EIO if there is no another previous error, so no need to check it here.

Signature · 1 line
Peter Xu
Reviewed-by on v1 5/9
#12 lore raw

Re: [PATCH 5/9] qemu-file: No need to check for shutdown in qemu_file_rate_limit

In reply to Juan Quintela
Quoted 7 lines · Juan Quintela

After calling qemu_file_shutdown() we set the error as -EIO if there is no another previous error, so no need to check it here.

---
 migration/qemu-file.c | 3 ---
 1 file changed, 3 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#13 lore raw

PATCH 6/9 qemu-file: remove shutdown member

In reply to Juan Quintela

The first thing that we do after setting the shutdown value is set the error as -EIO if there is not a previous error.

So this value is reduntant. Just remove it and use qemu_file_get_error() in the places that it was tested.

---
 migration/qemu-file.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
migration/qemu-file.c +3 -7
index 771aba7369..7b32ef45a9 100644
@@ -63,8 +63,6 @@ struct QEMUFile {
 
     int last_error;
     Error *last_error_obj;
-    /* has the file has been shutdown */
-    bool shutdown;
 };
 
 /*
@@ -78,8 +76,6 @@ int qemu_file_shutdown(QEMUFile *f)
 {
     int ret = 0;
 
-    f->shutdown = true;
-
     /*
      * We must set qemufile error before the real shutdown(), otherwise
      * there can be a race window where we thought IO all went though
@@ -294,7 +290,7 @@ void qemu_fflush(QEMUFile *f)
         return;
     }
 
-    if (f->shutdown) {
+    if (qemu_file_get_error(f)) {
         return;
     }
     if (f->iovcnt > 0) {
@@ -407,7 +403,7 @@ static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f)
     f->buf_index = 0;
     f->buf_size = pending;
 
-    if (f->shutdown) {
+    if (qemu_file_get_error(f)) {
         return 0;
     }
 
@@ -496,7 +492,7 @@ static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
     } else {
         if (f->iovcnt >= MAX_IOV_SIZE) {
             /* Should only happen if a previous fflush failed */
-            assert(f->shutdown || !qemu_file_is_writable(f));
+            assert(qemu_file_get_error(f) || !qemu_file_is_writable(f));
             return 1;
         }
         if (may_free) {
Signature · 1 line
2.40.0
Reviewed-by on v1 6/9
#14 lore raw

Re: [PATCH 6/9] qemu-file: remove shutdown member

Quoted 4 lines · Juan Quintela

The first thing that we do after setting the shutdown value is set the error as -EIO if there is not a previous error.

So this value is reduntant. Just remove it and use

s/reduntant/redundant/

Quoted 6 lines

qemu_file_get_error() in the places that it was tested.

---
 migration/qemu-file.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v1 6/9
#15 lore raw

Re: [PATCH 6/9] qemu-file: remove shutdown member

In reply to Juan Quintela
Quoted 7 lines · Juan Quintela

The first thing that we do after setting the shutdown value is set the error as -EIO if there is not a previous error.

So this value is reduntant. Just remove it and use qemu_file_get_error() in the places that it was tested.

Nit: I'd squash this with previous.

Signature · 1 line
Peter Xu
#16 lore raw

PATCH 7/9 qemu-file: Make total_transferred an uint64_t

In reply to Juan Quintela

Change all the functions that use it. It was already passed as uint64_t.

---
 migration/block.c     | 5 ++---
 migration/qemu-file.c | 8 ++++----
 migration/qemu-file.h | 4 ++--
 migration/savevm.c    | 6 ++----
 migration/vmstate.c   | 2 +-
 5 files changed, 11 insertions(+), 14 deletions(-)
migration/block.c +2 -3
index 3499f75e37..a37678ce95 100644
@@ -747,8 +747,7 @@ static int block_save_setup(QEMUFile *f, void *opaque)
 static int block_save_iterate(QEMUFile *f, void *opaque)
 {
     int ret;
-    int64_t last_bytes = qemu_file_total_transferred(f);
-    int64_t delta_bytes;
+    uint64_t last_bytes = qemu_file_total_transferred(f);
 
     trace_migration_block_save("iterate", block_mig_state.submitted,
                                block_mig_state.transferred);
@@ -800,7 +799,7 @@ static int block_save_iterate(QEMUFile *f, void *opaque)
     }
 
     qemu_put_be64(f, BLK_MIG_FLAG_EOS);
-    delta_bytes = qemu_file_total_transferred(f) - last_bytes;
+    uint64_t delta_bytes = qemu_file_total_transferred(f) - last_bytes;
     return (delta_bytes > 0);
 }
 
migration/qemu-file.c +4 -4
index 7b32ef45a9..e97d180f17 100644
@@ -51,7 +51,7 @@ struct QEMUFile {
     uint64_t rate_limit_used;
 
     /* The sum of bytes transferred on the wire */
-    int64_t total_transferred;
+    uint64_t total_transferred;
 
     int buf_index;
     int buf_size; /* 0 when writing */
@@ -718,9 +718,9 @@ int coroutine_mixed_fn qemu_get_byte(QEMUFile *f)
     return result;
 }
 
-int64_t qemu_file_total_transferred_fast(QEMUFile *f)
+uint64_t qemu_file_total_transferred_fast(QEMUFile *f)
 {
-    int64_t ret = f->total_transferred;
+    uint64_t ret = f->total_transferred;
     int i;
 
     for (i = 0; i < f->iovcnt; i++) {
@@ -730,7 +730,7 @@ int64_t qemu_file_total_transferred_fast(QEMUFile *f)
     return ret;
 }
 
-int64_t qemu_file_total_transferred(QEMUFile *f)
+uint64_t qemu_file_total_transferred(QEMUFile *f)
 {
     qemu_fflush(f);
     return f->total_transferred;
migration/qemu-file.h +2 -2
index 55ef5a2ac7..d758e7f10b 100644
@@ -83,7 +83,7 @@ int qemu_fclose(QEMUFile *f);
  *
  * Returns: the total bytes transferred
  */
-int64_t qemu_file_total_transferred(QEMUFile *f);
+uint64_t qemu_file_total_transferred(QEMUFile *f);
 
 /*
  * qemu_file_total_transferred_fast:
@@ -95,7 +95,7 @@ int64_t qemu_file_total_transferred(QEMUFile *f);
  *
  * Returns: the total bytes transferred and queued
  */
-int64_t qemu_file_total_transferred_fast(QEMUFile *f);
+uint64_t qemu_file_total_transferred_fast(QEMUFile *f);
 
 /*
  * put_buffer without copying the buffer.
migration/savevm.c +2 -4
index a9d0a88e62..032044b1d5 100644
@@ -927,11 +927,9 @@ static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se,
                                    JSONWriter *vmdesc)
 {
-    int64_t old_offset, size;
-
-    old_offset = qemu_file_total_transferred_fast(f);
+    uint64_t old_offset = qemu_file_total_transferred_fast(f);
     se->ops->save_state(f, se->opaque);
-    size = qemu_file_total_transferred_fast(f) - old_offset;
+    uint64_t size = qemu_file_total_transferred_fast(f) - old_offset;
 
     if (vmdesc) {
         json_writer_int64(vmdesc, "size", size);
migration/vmstate.c +1 -1
index 83ca4c7d3e..351f56104e 100644
@@ -349,7 +349,7 @@ int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
             void *first_elem = opaque + field->offset;
             int i, n_elems = vmstate_n_elems(opaque, field);
             int size = vmstate_size(opaque, field);
-            int64_t old_offset, written_bytes;
+            uint64_t old_offset, written_bytes;
             JSONWriter *vmdesc_loop = vmdesc;
 
             trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
Signature · 1 line
2.40.0
Reviewed-by on v1 7/9
#17 lore raw

Re: [PATCH 7/9] qemu-file: Make total_transferred an uint64_t

Quoted 11 lines · Juan Quintela

Change all the functions that use it. It was already passed as uint64_t.

---
 migration/block.c     | 5 ++---
 migration/qemu-file.c | 8 ++++----
 migration/qemu-file.h | 4 ++--
 migration/savevm.c    | 6 ++----
 migration/vmstate.c   | 2 +-
 5 files changed, 11 insertions(+), 14 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#18 lore raw

PATCH 8/9 qemu-file: Make ram_control_save_page() use accessors for rate_limit

In reply to Juan Quintela
---
 migration/qemu-file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/qemu-file.c +1 -1
index e97d180f17..ddebfac847 100644
@@ -362,7 +362,7 @@ size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
         int ret = f->hooks->save_page(f, block_offset,
                                       offset, size, bytes_sent);
         if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
-            f->rate_limit_used += size;
+            qemu_file_acct_rate_limit(f, size);
         }
 
         if (ret != RAM_SAVE_CONTROL_DELAYED &&
Signature · 1 line
2.40.0
Reviewed-by on v1 8/9
#20 lore raw

Re: [PATCH 8/9] qemu-file: Make ram_control_save_page() use accessors for rate_limit

In reply to Juan Quintela
Quoted 4 lines · Juan Quintela
---
 migration/qemu-file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#21 lore raw

PATCH 9/9 qemu-file: Account for rate_limit usage on qemu_fflush()

In reply to Juan Quintela

That is the moment we know we have transferred something.

---
 migration/qemu-file.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
migration/qemu-file.c +3 -4
index ddebfac847..309b4c56f4 100644
@@ -300,7 +300,9 @@ void qemu_fflush(QEMUFile *f)
                                    &local_error) < 0) {
             qemu_file_set_error_obj(f, -EIO, local_error);
         } else {
-            f->total_transferred += iov_size(f->iov, f->iovcnt);
+            uint64_t size = iov_size(f->iov, f->iovcnt);
+            qemu_file_acct_rate_limit(f, size);
+            f->total_transferred += size;
         }
 
         qemu_iovec_release_ram(f);
@@ -527,7 +529,6 @@ void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
         return;
     }
 
-    f->rate_limit_used += size;
     add_to_iovec(f, buf, size, may_free);
 }
 
@@ -545,7 +546,6 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
             l = size;
         }
         memcpy(f->buf + f->buf_index, buf, l);
-        f->rate_limit_used += l;
         add_buf_to_iovec(f, l);
         if (qemu_file_get_error(f)) {
             break;
@@ -562,7 +562,6 @@ void qemu_put_byte(QEMUFile *f, int v)
     }
 
     f->buf[f->buf_index] = v;
-    f->rate_limit_used++;
     add_buf_to_iovec(f, 1);
 }
 
Signature · 1 line
2.40.0
#22 lore raw

Re: [PATCH 9/9] qemu-file: Account for rate_limit usage on qemu_fflush()

Quoted 39 lines · Juan Quintela

That is the moment we know we have transferred something.

---
 migration/qemu-file.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
migration/qemu-file.c +3 -3
index ddebfac847..309b4c56f4 100644
@@ -300,7 +300,9 @@ void qemu_fflush(QEMUFile *f)
                                    &local_error) < 0) {
             qemu_file_set_error_obj(f, -EIO, local_error);
         } else {
-            f->total_transferred += iov_size(f->iov, f->iovcnt);
+            uint64_t size = iov_size(f->iov, f->iovcnt);
+            qemu_file_acct_rate_limit(f, size);
+            f->total_transferred += size;
         }
 
         qemu_iovec_release_ram(f);
@@ -527,7 +529,6 @@ void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
         return;
     }
 
-    f->rate_limit_used += size;
     add_to_iovec(f, buf, size, may_free);
 }
 
@@ -545,7 +546,6 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
             l = size;
         }
         memcpy(f->buf + f->buf_index, buf, l);
-        f->rate_limit_used += l;
         add_buf_to_iovec(f, l);
         if (qemu_file_get_error(f)) {
             break;

migration/qemu-file.c void qemu_put_byte(QEMUFile *f, int v) · line 566

Quoted 6 lines
migration/qemu-file.c +0 -1
@@ -562,7 +562,6 @@ void qemu_put_byte(QEMUFile *f, int v)
     }
 
     f->buf[f->buf_index] = v;
-    f->rate_limit_used++;
     add_buf_to_iovec(f, 1);
 }

This has a slight semantic behavioural change.

By accounting for rate limit in the qemu_put functions, we ensure that we stop growing the iovec when rate limiting activates.

If we only apply rate limit in the the flush function, that will let the f->iov continue to accumulate buffers, while we have rate limited the actual transfer. This makes me uneasy - it feels like a bad idea to continue to accumulate buffers if we're not ready to send them

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#23 lore raw

Re: [PATCH 9/9] qemu-file: Account for rate_limit usage on qemu_fflush()

Quoted 42 lines · Daniel P. Berrangé
Quoted 41 lines · Juan Quintela

That is the moment we know we have transferred something.

---
 migration/qemu-file.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
migration/qemu-file.c +3 -3
index ddebfac847..309b4c56f4 100644
@@ -300,7 +300,9 @@ void qemu_fflush(QEMUFile *f)
                                    &local_error) < 0) {
             qemu_file_set_error_obj(f, -EIO, local_error);
         } else {
-            f->total_transferred += iov_size(f->iov, f->iovcnt);
+            uint64_t size = iov_size(f->iov, f->iovcnt);
+            qemu_file_acct_rate_limit(f, size);
+            f->total_transferred += size;
         }
 
         qemu_iovec_release_ram(f);
@@ -527,7 +529,6 @@ void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
         return;
     }
 
-    f->rate_limit_used += size;
     add_to_iovec(f, buf, size, may_free);
 }
 
@@ -545,7 +546,6 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
             l = size;
         }
         memcpy(f->buf + f->buf_index, buf, l);
-        f->rate_limit_used += l;
         add_buf_to_iovec(f, l);
         if (qemu_file_get_error(f)) {
             break;
@@ -562,7 +562,6 @@ void qemu_put_byte(QEMUFile *f, int v)
     }
 
Quoted 6 lines

migration/qemu-file.c void qemu_put_byte(QEMUFile *f, int v) · line 566

Quoted 4 lines
migration/qemu-file.c +0 -1
     f->buf[f->buf_index] = v;
-    f->rate_limit_used++;
     add_buf_to_iovec(f, 1);
 }

This has a slight semantic behavioural change.

Yeap.

See the answer to Peter. But three things came to mind:

a - the size of the buffer is small (between 32KB and 256KB depending
how you count it). So we are going to call qemu_fflush() really soon.

b - We are using this value to calculate how much we can send through
the wire. Here we are saything how much we have accepted to send.

c - When using multifd the number of bytes that we send through the qemu
file is even smaller. migration-test multifd test send 300MB of data through multifd channels and around 300KB on the qemu_file channel.

Quoted 7 lines

By accounting for rate limit in the qemu_put functions, we ensure that we stop growing the iovec when rate limiting activates.

If we only apply rate limit in the the flush function, that will let the f->iov continue to accumulate buffers, while we have rate limited the actual transfer.

256KB maximum. Our accounting has bigger errors than that.

Quoted 2 lines

This makes me uneasy - it feels like a bad idea to continue to accumulate buffers if we're not ready to send them

I still think that the change is correct. But as you and Peter have concerns about it, I will think a bit more about it.

Thanks, Juan.

#24 lore raw

Re: [PATCH 9/9] qemu-file: Account for rate_limit usage on qemu_fflush()

Quoted 74 lines · Juan Quintela
Quoted 42 lines · Daniel P. Berrangé
Quoted 41 lines · Juan Quintela

That is the moment we know we have transferred something.

---
 migration/qemu-file.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
migration/qemu-file.c +3 -3
index ddebfac847..309b4c56f4 100644
@@ -300,7 +300,9 @@ void qemu_fflush(QEMUFile *f)
                                    &local_error) < 0) {
             qemu_file_set_error_obj(f, -EIO, local_error);
         } else {
-            f->total_transferred += iov_size(f->iov, f->iovcnt);
+            uint64_t size = iov_size(f->iov, f->iovcnt);
+            qemu_file_acct_rate_limit(f, size);
+            f->total_transferred += size;
         }
 
         qemu_iovec_release_ram(f);
@@ -527,7 +529,6 @@ void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
         return;
     }
 
-    f->rate_limit_used += size;
     add_to_iovec(f, buf, size, may_free);
 }
 
@@ -545,7 +546,6 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
             l = size;
         }
         memcpy(f->buf + f->buf_index, buf, l);
-        f->rate_limit_used += l;
         add_buf_to_iovec(f, l);
         if (qemu_file_get_error(f)) {
             break;
@@ -562,7 +562,6 @@ void qemu_put_byte(QEMUFile *f, int v)
     }
 
Quoted 6 lines

migration/qemu-file.c void qemu_put_byte(QEMUFile *f, int v) · line 566

Quoted 4 lines
migration/qemu-file.c +0 -1
     f->buf[f->buf_index] = v;
-    f->rate_limit_used++;
     add_buf_to_iovec(f, 1);
 }

This has a slight semantic behavioural change.

Yeap.

See the answer to Peter. But three things came to mind:

a - the size of the buffer is small (between 32KB and 256KB depending
how you count it). So we are going to call qemu_fflush() really soon.

b - We are using this value to calculate how much we can send through
the wire. Here we are saything how much we have accepted to send.

c - When using multifd the number of bytes that we send through the qemu
file is even smaller. migration-test multifd test send 300MB of data through multifd channels and around 300KB on the qemu_file channel.

Quoted 7 lines

By accounting for rate limit in the qemu_put functions, we ensure that we stop growing the iovec when rate limiting activates.

If we only apply rate limit in the the flush function, that will let the f->iov continue to accumulate buffers, while we have rate limited the actual transfer.

Quoted 8 lines

256KB maximum. Our accounting has bigger errors than that.

Quoted 2 lines

This makes me uneasy - it feels like a bad idea to continue to accumulate buffers if we're not ready to send them

I still think that the change is correct. But as you and Peter have concerns about it, I will think a bit more about it.

If Peter's calculations are correct, then I don't have any objection, as that's a small overhead.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#25 lore raw

Re: [PATCH 9/9] qemu-file: Account for rate_limit usage on qemu_fflush()

Quoted 30 lines · Daniel P. Berrangé
Quoted 30 lines
Quoted 2 lines

This has a slight semantic behavioural change.

Yeap.

See the answer to Peter. But three things came to mind:

a - the size of the buffer is small (between 32KB and 256KB depending
how you count it). So we are going to call qemu_fflush() really soon.

b - We are using this value to calculate how much we can send through
the wire. Here we are saything how much we have accepted to send.

c - When using multifd the number of bytes that we send through the qemu
file is even smaller. migration-test multifd test send 300MB of data through multifd channels and around 300KB on the qemu_file channel.

Quoted 7 lines

By accounting for rate limit in the qemu_put functions, we ensure that we stop growing the iovec when rate limiting activates.

If we only apply rate limit in the the flush function, that will let the f->iov continue to accumulate buffers, while we have rate limited the actual transfer.

256KB maximum. Our accounting has bigger errors than that.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This makes me uneasy - it feels like a bad idea to continue to accumulate buffers if we're not ready to send them

I still think that the change is correct. But as you and Peter have concerns about it, I will think a bit more about it.

If Peter's calculations are correct, then I don't have any objection, as that's a small overhead.

#define IOV_MAX 1024
....
#define IO_BUF_SIZE 32768
#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)

struct QEMUFile {
...

    uint8_t buf[IO_BUF_SIZE];

    struct iovec iov[MAX_IOV_SIZE];
    ....
}

Later, Juan.

Reviewed-by on v1 9/9
#26 lore raw

Re: [PATCH 9/9] qemu-file: Account for rate_limit usage on qemu_fflush()

In reply to Juan Quintela
Quoted 3 lines · Juan Quintela

That is the moment we know we have transferred something.

There'll be a slight side effect that qemu_file_rate_limit() can be triggered later than before because data cached in the qemufile won't be accounted until flushed.

Two limits here:

- IO_BUF_SIZE==32K, the real buffer
- MAX_IOV_SIZE==64 (I think), the async buffer to put guest page ptrs directly, on x86 it's 64*4K=256K

So the impact should be no more than 288KB on x86. Looks still fine..

Signature · 1 line
Peter Xu
#27 lore raw

Re: [PATCH 9/9] qemu-file: Account for rate_limit usage on qemu_fflush()

Quoted 9 lines · Peter Xu
Quoted 3 lines · Juan Quintela

That is the moment we know we have transferred something.

There'll be a slight side effect that qemu_file_rate_limit() can be triggered later than before because data cached in the qemufile won't be accounted until flushed.

Quoted 7 lines

Two limits here:

- IO_BUF_SIZE==32K, the real buffer
- MAX_IOV_SIZE==64 (I think), the async buffer to put guest page ptrs directly, on x86 it's 64*4K=256K

So the impact should be no more than 288KB on x86. Looks still fine..

This was on purpose. We are counting data as sent when it has not been sent yet.

With this change, we account for the data written when we do the real write.

And yes, I fully agree that with the size that we have it shouldn't make much of a difference in the speed calculation.

The difference here is that I will move that counter somewhere else, and the less places that I have to use it the better O:-)

Quoted 1 line

Thanks.

#28 lore raw
In reply to Juan Quintela
Quoted 1 line · Juan Quintela

- convince and review code to see that everything is uint64_t.

One general question to patches regarding this - what's the major benefit of using uint64_t?

It doubles the possible numbers to hold, but it's already 64bits so I don't think it matters a lot. The thing is we're removing some code trying to detect negative which seems to be still helpful to detect e.g. overflows (even though I don't think it'll happen). I just still think it's good to know when overflow happens, and not sure what I missed on benefits of using unsigned here.

I've reviewed all the rest patches and all look good here.

Thanks,

Signature · 1 line
Peter Xu
#29 lore raw
Quoted 8 lines · Peter Xu
Quoted 1 line · Juan Quintela

- convince and review code to see that everything is uint64_t.

One general question to patches regarding this - what's the major benefit of using uint64_t?

It doubles the possible numbers to hold, but it's already 64bits so I don't think it matters a lot.

We were checking for negatives even when that can't be. And we are doing this dance of

int64_t x, y;
uint64_t a, b;

x = a;
b = y;

This is always confusing and not always right.

Quoted 5 lines

The thing is we're removing some code trying to
detect negative which seems to be still helpful to detect e.g. overflows (even though I don't think it'll happen). I just still think it's good to know when overflow happens, and not sure what I missed on benefits of using unsigned here.

If you grep through the code, you see that half of the things are int64_t and the other half is uint64_t. I find it always confusing.

Quoted 1 line

I've reviewed all the rest patches and all look good here.

Thanks very much.

#30 lore raw
Quoted 13 lines · Juan Quintela
Quoted 8 lines · Peter Xu
Quoted 1 line · Juan Quintela

- convince and review code to see that everything is uint64_t.

One general question to patches regarding this - what's the major benefit of using uint64_t?

It doubles the possible numbers to hold, but it's already 64bits so I don't think it matters a lot.

We were checking for negatives even when that can't be. And we are doing this dance of

Quoted 7 lines

int64_t x, y;
uint64_t a, b;

x = a;
b = y;

This is always confusing and not always right.

Yeah this is confusing, but if anything can go wrong with this I assume we could have some bigger problem anyway..

Quoted 9 lines
Quoted 6 lines

The thing is we're removing some code trying to
detect negative which seems to be still helpful to detect e.g. overflows (even though I don't think it'll happen). I just still think it's good to know when overflow happens, and not sure what I missed on benefits of using unsigned here.

If you grep through the code, you see that half of the things are int64_t and the other half is uint64_t. I find it always confusing.

Right, I'm personally curious whether we should just use int64_t always unless necessary. :) Another good thing with int64_t is it's also suitable for error report when used in retvals.

But no strong opinion here, I don't think that's a huge deal for now. Having such an alignment on types makes sense to me.

Thanks,

Signature · 1 line
Peter Xu
#31 lore raw
Quoted 32 lines · Peter Xu
Quoted 13 lines · Juan Quintela
Quoted 8 lines · Peter Xu
Quoted 1 line · Juan Quintela

- convince and review code to see that everything is uint64_t.

One general question to patches regarding this - what's the major benefit of using uint64_t?

It doubles the possible numbers to hold, but it's already 64bits so I don't think it matters a lot.

We were checking for negatives even when that can't be. And we are doing this dance of

Quoted 7 lines

int64_t x, y;
uint64_t a, b;

x = a;
b = y;

This is always confusing and not always right.

Yeah this is confusing, but if anything can go wrong with this I assume we could have some bigger problem anyway..

Quoted 7 lines
Quoted 6 lines

The thing is we're removing some code trying to
detect negative which seems to be still helpful to detect e.g. overflows (even though I don't think it'll happen). I just still think it's good to know when overflow happens, and not sure what I missed on benefits of using unsigned here.

Quoted 6 lines
Quoted 2 lines

If you grep through the code, you see that half of the things are int64_t and the other half is uint64_t. I find it always confusing.

Right, I'm personally curious whether we should just use int64_t always unless necessary. :) Another good thing with int64_t is it's also suitable for error report when used in retvals.

It is used by sizes. We don't return errors right now. And if it is negative, we need to check that if it is negative, even when no code uses the negative functionality.

Quoted 2 lines

But no strong opinion here, I don't think that's a huge deal for now. Having such an alignment on types makes sense to me.

Thanks, Juan.