Skip to contentSkip to conversation

multifd: cleanup some source code

PATCH In review Li Zhang 2 participants 7 messages Dec 22, 2021 – Feb 8, 2023

lore
Share & tools
lore

Li Zhang

author
#0 lore raw

cover multifd: cleanup some source code

When looking into the multifd source code, some of them can be improved. This patchset is to cleanup the functions multifd_channel_connect and multfd_send_thead.

The functions are tested without any errors.

Li Zhang (2):
multifd: cleanup the function multifd_channel_connect
multifd: cleanup the function multifd_send_thread

 migration/multifd.c | 131 ++++++++++++++++++++++----------------------
 1 file changed, 65 insertions(+), 66 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/2 multifd: cleanup the function multifd_channel_connect

Cleanup multifd_channel_connect

---
 migration/multifd.c | 49 ++++++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 25 deletions(-)
migration/multifd.c +24 -25
index 212be1ed04..4ec40739e0 100644
@@ -801,33 +801,32 @@ static bool multifd_channel_connect(MultiFDSendParams *p,
     trace_multifd_set_outgoing_channel(
         ioc, object_get_typename(OBJECT(ioc)), p->tls_hostname, error);
 
-    if (!error) {
-        if (s->parameters.tls_creds &&
-            *s->parameters.tls_creds &&
-            !object_dynamic_cast(OBJECT(ioc),
-                                 TYPE_QIO_CHANNEL_TLS)) {
-            multifd_tls_channel_connect(p, ioc, &error);
-            if (!error) {
-                /*
-                 * tls_channel_connect will call back to this
-                 * function after the TLS handshake,
-                 * so we mustn't call multifd_send_thread until then
-                 */
-                return true;
-            } else {
-                return false;
-            }
-        } else {
-            migration_ioc_register_yank(ioc);
-            p->registered_yank = true;
-            p->c = ioc;
-            qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
-                                   QEMU_THREAD_JOINABLE);
-       }
-       return true;
+    if (error) {
+        return false;
     }
 
-    return false;
+    if (s->parameters.tls_creds &&
+        *s->parameters.tls_creds &&
+        !object_dynamic_cast(OBJECT(ioc),
+                             TYPE_QIO_CHANNEL_TLS)) {
+        multifd_tls_channel_connect(p, ioc, &error);
+        if (error) {
+            return false;
+        }
+        /*
+         * tls_channel_connect will call back to this
+         * function after the TLS handshake,
+         * so we mustn't call multifd_send_thread until then
+         */
+        return true;
+    } else {
+        migration_ioc_register_yank(ioc);
+        p->registered_yank = true;
+        p->c = ioc;
+        qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
+                               QEMU_THREAD_JOINABLE);
+    }
+    return true;
 }
 
 static void multifd_new_send_channel_cleanup(MultiFDSendParams *p,
Signature · 1 line
2.31.1
author
#2 lore raw

Re: [PATCH 1/2] multifd: cleanup the function multifd_channel_connect

ping

Quoted 70 lines · Li Zhang

Cleanup multifd_channel_connect

---
  migration/multifd.c | 49 ++++++++++++++++++++++-----------------------
  1 file changed, 24 insertions(+), 25 deletions(-)
migration/multifd.c +24 -25
index 212be1ed04..4ec40739e0 100644
@@ -801,33 +801,32 @@ static bool multifd_channel_connect(MultiFDSendParams *p,
      trace_multifd_set_outgoing_channel(
          ioc, object_get_typename(OBJECT(ioc)), p->tls_hostname, error);
 
-    if (!error) {
-        if (s->parameters.tls_creds &&
-            *s->parameters.tls_creds &&
-            !object_dynamic_cast(OBJECT(ioc),
-                                 TYPE_QIO_CHANNEL_TLS)) {
-            multifd_tls_channel_connect(p, ioc, &error);
-            if (!error) {
-                /*
-                 * tls_channel_connect will call back to this
-                 * function after the TLS handshake,
-                 * so we mustn't call multifd_send_thread until then
-                 */
-                return true;
-            } else {
-                return false;
-            }
-        } else {
-            migration_ioc_register_yank(ioc);
-            p->registered_yank = true;
-            p->c = ioc;
-            qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
-                                   QEMU_THREAD_JOINABLE);
-       }
-       return true;
+    if (error) {
+        return false;
      }
 
-    return false;
+    if (s->parameters.tls_creds &&
+        *s->parameters.tls_creds &&
+        !object_dynamic_cast(OBJECT(ioc),
+                             TYPE_QIO_CHANNEL_TLS)) {
+        multifd_tls_channel_connect(p, ioc, &error);
+        if (error) {
+            return false;
+        }
+        /*
+         * tls_channel_connect will call back to this
+         * function after the TLS handshake,
+         * so we mustn't call multifd_send_thread until then
+         */
+        return true;
+    } else {
+        migration_ioc_register_yank(ioc);
+        p->registered_yank = true;
+        p->c = ioc;
+        qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
+                               QEMU_THREAD_JOINABLE);
+    }
+    return true;
  }
 
  static void multifd_new_send_channel_cleanup(MultiFDSendParams *p,
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] multifd: cleanup the function multifd_channel_connect

In reply to Li Zhang
Quoted 3 lines · Li Zhang

Cleanup multifd_channel_connect

queued.

Quoted 66 lines
---
 migration/multifd.c | 49 ++++++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 25 deletions(-)
migration/multifd.c +24 -25
index 212be1ed04..4ec40739e0 100644
@@ -801,33 +801,32 @@ static bool multifd_channel_connect(MultiFDSendParams *p,
     trace_multifd_set_outgoing_channel(
         ioc, object_get_typename(OBJECT(ioc)), p->tls_hostname, error);
 
-    if (!error) {
-        if (s->parameters.tls_creds &&
-            *s->parameters.tls_creds &&
-            !object_dynamic_cast(OBJECT(ioc),
-                                 TYPE_QIO_CHANNEL_TLS)) {
-            multifd_tls_channel_connect(p, ioc, &error);
-            if (!error) {
-                /*
-                 * tls_channel_connect will call back to this
-                 * function after the TLS handshake,
-                 * so we mustn't call multifd_send_thread until then
-                 */
-                return true;
-            } else {
-                return false;
-            }
-        } else {
-            migration_ioc_register_yank(ioc);
-            p->registered_yank = true;
-            p->c = ioc;
-            qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
-                                   QEMU_THREAD_JOINABLE);
-       }
-       return true;
+    if (error) {
+        return false;
     }
 
-    return false;
+    if (s->parameters.tls_creds &&
+        *s->parameters.tls_creds &&
+        !object_dynamic_cast(OBJECT(ioc),
+                             TYPE_QIO_CHANNEL_TLS)) {
+        multifd_tls_channel_connect(p, ioc, &error);
+        if (error) {
+            return false;
+        }
+        /*
+         * tls_channel_connect will call back to this
+         * function after the TLS handshake,
+         * so we mustn't call multifd_send_thread until then
+         */
+        return true;
+    } else {
+        migration_ioc_register_yank(ioc);
+        p->registered_yank = true;
+        p->c = ioc;
+        qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
+                               QEMU_THREAD_JOINABLE);
+    }
+    return true;
 }
 
 static void multifd_new_send_channel_cleanup(MultiFDSendParams *p,
author
#4 lore raw

PATCH 2/2 multifd: cleanup the function multifd_send_thread

In reply to Li Zhang

Cleanup multifd_send_thread

---
 migration/multifd.c | 82 ++++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 41 deletions(-)
migration/multifd.c +41 -41
index 4ec40739e0..7888d71bfe 100644
@@ -649,58 +649,58 @@ static void *multifd_send_thread(void *opaque)
             break;
         }
         qemu_mutex_lock(&p->mutex);
-
-        if (p->pending_job) {
-            uint32_t used = p->pages->num;
-            uint64_t packet_num = p->packet_num;
-            uint32_t flags = p->flags;
-
-            if (used) {
-                ret = multifd_send_state->ops->send_prepare(p, &local_err);
-                if (ret != 0) {
-                    qemu_mutex_unlock(&p->mutex);
-                    break;
-                }
-            }
-            multifd_send_fill_packet(p);
-            p->flags = 0;
-            p->num_packets++;
-            p->num_pages += used;
-            p->pages->num = 0;
-            p->pages->block = NULL;
+        if (!p->quit && !p->pending_job) {
+            /* sometimes there are spurious wakeups */
+            qemu_mutex_unlock(&p->mutex);
+            continue;
+        } else if (!p->pending_job) {
             qemu_mutex_unlock(&p->mutex);
+            break;
+        }
 
-            trace_multifd_send(p->id, packet_num, used, flags,
-                               p->next_packet_size);
+        uint32_t used = p->pages->num;
+        uint64_t packet_num = p->packet_num;
+        uint32_t flags = p->flags;
 
-            ret = qio_channel_write_all(p->c, (void *)p->packet,
-                                        p->packet_len, &local_err);
+        if (used) {
+            ret = multifd_send_state->ops->send_prepare(p, &local_err);
             if (ret != 0) {
+                qemu_mutex_unlock(&p->mutex);
                 break;
             }
+        }
+        multifd_send_fill_packet(p);
+        p->flags = 0;
+        p->num_packets++;
+        p->num_pages += used;
+        p->pages->num = 0;
+        p->pages->block = NULL;
+        qemu_mutex_unlock(&p->mutex);
 
-            if (used) {
-                ret = multifd_send_state->ops->send_write(p, used, &local_err);
-                if (ret != 0) {
-                    break;
-                }
-            }
+        trace_multifd_send(p->id, packet_num, used, flags,
+                           p->next_packet_size);
 
-            qemu_mutex_lock(&p->mutex);
-            p->pending_job--;
-            qemu_mutex_unlock(&p->mutex);
+        ret = qio_channel_write_all(p->c, (void *)p->packet,
+                                    p->packet_len, &local_err);
+        if (ret != 0) {
+            break;
+        }
 
-            if (flags & MULTIFD_FLAG_SYNC) {
-                qemu_sem_post(&p->sem_sync);
+        if (used) {
+            ret = multifd_send_state->ops->send_write(p, used, &local_err);
+            if (ret != 0) {
+                break;
             }
-            qemu_sem_post(&multifd_send_state->channels_ready);
-        } else if (p->quit) {
-            qemu_mutex_unlock(&p->mutex);
-            break;
-        } else {
-            qemu_mutex_unlock(&p->mutex);
-            /* sometimes there are spurious wakeups */
         }
+
+        qemu_mutex_lock(&p->mutex);
+        p->pending_job--;
+        qemu_mutex_unlock(&p->mutex);
+
+        if (flags & MULTIFD_FLAG_SYNC) {
+            qemu_sem_post(&p->sem_sync);
+        }
+        qemu_sem_post(&multifd_send_state->channels_ready);
     }
 
 out:
Signature · 1 line
2.31.1
author
#5 lore raw

Re: [PATCH 2/2] multifd: cleanup the function multifd_send_thread

ping

Quoted 112 lines · Li Zhang

Cleanup multifd_send_thread

---
  migration/multifd.c | 82 ++++++++++++++++++++++-----------------------
  1 file changed, 41 insertions(+), 41 deletions(-)
migration/multifd.c +41 -41
index 4ec40739e0..7888d71bfe 100644
@@ -649,58 +649,58 @@ static void *multifd_send_thread(void *opaque)
              break;
          }
          qemu_mutex_lock(&p->mutex);
-
-        if (p->pending_job) {
-            uint32_t used = p->pages->num;
-            uint64_t packet_num = p->packet_num;
-            uint32_t flags = p->flags;
-
-            if (used) {
-                ret = multifd_send_state->ops->send_prepare(p, &local_err);
-                if (ret != 0) {
-                    qemu_mutex_unlock(&p->mutex);
-                    break;
-                }
-            }
-            multifd_send_fill_packet(p);
-            p->flags = 0;
-            p->num_packets++;
-            p->num_pages += used;
-            p->pages->num = 0;
-            p->pages->block = NULL;
+        if (!p->quit && !p->pending_job) {
+            /* sometimes there are spurious wakeups */
+            qemu_mutex_unlock(&p->mutex);
+            continue;
+        } else if (!p->pending_job) {
              qemu_mutex_unlock(&p->mutex);
+            break;
+        }
 
-            trace_multifd_send(p->id, packet_num, used, flags,
-                               p->next_packet_size);
+        uint32_t used = p->pages->num;
+        uint64_t packet_num = p->packet_num;
+        uint32_t flags = p->flags;
 
-            ret = qio_channel_write_all(p->c, (void *)p->packet,
-                                        p->packet_len, &local_err);
+        if (used) {
+            ret = multifd_send_state->ops->send_prepare(p, &local_err);
              if (ret != 0) {
+                qemu_mutex_unlock(&p->mutex);
                  break;
              }
+        }
+        multifd_send_fill_packet(p);
+        p->flags = 0;
+        p->num_packets++;
+        p->num_pages += used;
+        p->pages->num = 0;
+        p->pages->block = NULL;
+        qemu_mutex_unlock(&p->mutex);
 
-            if (used) {
-                ret = multifd_send_state->ops->send_write(p, used, &local_err);
-                if (ret != 0) {
-                    break;
-                }
-            }
+        trace_multifd_send(p->id, packet_num, used, flags,
+                           p->next_packet_size);
 
-            qemu_mutex_lock(&p->mutex);
-            p->pending_job--;
-            qemu_mutex_unlock(&p->mutex);
+        ret = qio_channel_write_all(p->c, (void *)p->packet,
+                                    p->packet_len, &local_err);
+        if (ret != 0) {
+            break;
+        }
 
-            if (flags & MULTIFD_FLAG_SYNC) {
-                qemu_sem_post(&p->sem_sync);
+        if (used) {
+            ret = multifd_send_state->ops->send_write(p, used, &local_err);
+            if (ret != 0) {
+                break;
              }
-            qemu_sem_post(&multifd_send_state->channels_ready);
-        } else if (p->quit) {
-            qemu_mutex_unlock(&p->mutex);
-            break;
-        } else {
-            qemu_mutex_unlock(&p->mutex);
-            /* sometimes there are spurious wakeups */
          }
+
+        qemu_mutex_lock(&p->mutex);
+        p->pending_job--;
+        qemu_mutex_unlock(&p->mutex);
+
+        if (flags & MULTIFD_FLAG_SYNC) {
+            qemu_sem_post(&p->sem_sync);
+        }
+        qemu_sem_post(&multifd_send_state->channels_ready);
      }
 
  out:
#6 lore raw

Re: [PATCH 2/2] multifd: cleanup the function multifd_send_thread

In reply to Li Zhang
Quoted 3 lines · Li Zhang

Cleanup multifd_send_thread

Hi Zhang

First of all, sorry for the late review.

This other patch is wrong.

Quoted 26 lines
---
 migration/multifd.c | 82 ++++++++++++++++++++++-----------------------
 1 file changed, 41 insertions(+), 41 deletions(-)
migration/multifd.c +0 -14
index 4ec40739e0..7888d71bfe 100644
@@ -649,58 +649,58 @@ static void *multifd_send_thread(void *opaque)
             break;
         }
         qemu_mutex_lock(&p->mutex);
-
-        if (p->pending_job) {
-            uint32_t used = p->pages->num;
-            uint64_t packet_num = p->packet_num;
-            uint32_t flags = p->flags;
-
-            if (used) {
-                ret = multifd_send_state->ops->send_prepare(p, &local_err);
-                if (ret != 0) {
-                    qemu_mutex_unlock(&p->mutex);
-                    break;
-                }
-            }
-            multifd_send_fill_packet(p);

migration/multifd.c static void *multifd_send_thread(void *opaque) · line 656

Quoted 10 lines
migration/multifd.c +5 -5
-            p->flags = 0;
-            p->num_packets++;
-            p->num_pages += used;
-            p->pages->num = 0;
-            p->pages->block = NULL;
+        if (!p->quit && !p->pending_job) {
+            /* sometimes there are spurious wakeups */
+            qemu_mutex_unlock(&p->mutex);
+            continue;
+        } else if (!p->pending_job) {

Here it should be

        } else if (p->quit) {

And in this case, I preffer the previous code, as the first case is the common one.

I still have to see if we ever enter through the spurious case anymore.

Thanks, Juan.