Skip to contentSkip to conversation

migration/postcopy: use static PostcopyDiscardState instead of allocating it for each block

PATCH In review Wei Yang 3 participants 6 messages Jul 10, 2019 – Jul 24, 2019

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 migration/postcopy: use static PostcopyDiscardState instead of allocating it for each block

Even we need to do discard for each RAMBlock, we still can leverage the same memory space to store the information.

By doing so, we avoid memory allocation and deallocation to the system and also avoid potential failure of memory allocation which breaks the migration.

---
 migration/postcopy-ram.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
migration/postcopy-ram.c +7 -9
index 9faacacc9e..2e6b076bb7 100644
@@ -1377,8 +1377,7 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
  *   asking to discard individual ranges.
  *
  * @ms: The current migration state.
- * @offset: the bitmap offset of the named RAMBlock in the migration
- *   bitmap.
+ * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
  * @name: RAMBlock that discards will operate on.
  *
  * returns: a new PDS.
@@ -1386,13 +1385,14 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
 PostcopyDiscardState *postcopy_discard_send_init(MigrationState *ms,
                                                  const char *name)
 {
-    PostcopyDiscardState *res = g_malloc0(sizeof(PostcopyDiscardState));
+    static PostcopyDiscardState res = {0};
 
-    if (res) {
-        res->ramblock_name = name;
-    }
+    res.ramblock_name = name;
+    res.cur_entry = 0;
+    res.nsentwords = 0;
+    res.nsentcmds = 0;
 
-    return res;
+    return &res;
 }
 
 /**
@@ -1449,8 +1449,6 @@ void postcopy_discard_send_finish(MigrationState *ms, PostcopyDiscardState *pds)
 
     trace_postcopy_discard_send_finish(pds->ramblock_name, pds->nsentwords,
                                        pds->nsentcmds);
-
-    g_free(pds);
 }
 
 /*
Signature · 1 line
2.17.1
#1 lore raw
Quoted 27 lines · * Wei Yang ([email protected]) wrote:

Even we need to do discard for each RAMBlock, we still can leverage the same memory space to store the information.

By doing so, we avoid memory allocation and deallocation to the system and also avoid potential failure of memory allocation which breaks the migration.

---
 migration/postcopy-ram.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
migration/postcopy-ram.c +1 -2
index 9faacacc9e..2e6b076bb7 100644
@@ -1377,8 +1377,7 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
  *   asking to discard individual ranges.
  *
  * @ms: The current migration state.
- * @offset: the bitmap offset of the named RAMBlock in the migration
- *   bitmap.
+ * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
  * @name: RAMBlock that discards will operate on.
  *
  * returns: a new PDS.

migration/postcopy-ram.c void postcopy_fault_thread_notify(MigrationIncomingState *mis) · line 1388

Quoted 5 lines
migration/postcopy-ram.c +1 -1
@@ -1386,13 +1385,14 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
 PostcopyDiscardState *postcopy_discard_send_init(MigrationState *ms,
                                                  const char *name)
 {
-    PostcopyDiscardState *res = g_malloc0(sizeof(PostcopyDiscardState));
+    static PostcopyDiscardState res = {0};

Do you think it would be better to make this a static at the top of migration/postcopy-ram.c and then we could remove the pds parameters from postcopy_discard_send_range and friends?
If there's only one pds then we don't need to pass the pointer around.

Dave

Quoted 25 lines
migration/postcopy-ram.c +5 -6
-    if (res) {
-        res->ramblock_name = name;
-    }
+    res.ramblock_name = name;
+    res.cur_entry = 0;
+    res.nsentwords = 0;
+    res.nsentcmds = 0;
 
-    return res;
+    return &res;
 }
 
 /**
@@ -1449,8 +1449,6 @@ void postcopy_discard_send_finish(MigrationState *ms, PostcopyDiscardState *pds)
 
     trace_postcopy_discard_send_finish(pds->ramblock_name, pds->nsentwords,
                                        pds->nsentcmds);
-
-    g_free(pds);
 }
 
 /*
Signature · 1 line
2.17.1

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

#2 lore raw
Quoted 30 lines · Dr. David Alan Gilbert
Quoted 29 lines · * Wei Yang ([email protected]) wrote:

Even we need to do discard for each RAMBlock, we still can leverage the same memory space to store the information.

By doing so, we avoid memory allocation and deallocation to the system and also avoid potential failure of memory allocation which breaks the migration.

---
 migration/postcopy-ram.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
migration/postcopy-ram.c +1 -2
index 9faacacc9e..2e6b076bb7 100644
@@ -1377,8 +1377,7 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
  *   asking to discard individual ranges.
  *
  * @ms: The current migration state.
- * @offset: the bitmap offset of the named RAMBlock in the migration
- *   bitmap.
+ * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
  * @name: RAMBlock that discards will operate on.
  *
  * returns: a new PDS.
@@ -1386,13 +1385,14 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
 PostcopyDiscardState *postcopy_discard_send_init(MigrationState *ms,
                                                  const char *name)
Quoted 9 lines

migration/postcopy-ram.c void postcopy_fault_thread_notify(MigrationIncomingState *mis) · line 1388

Quoted 3 lines
migration/postcopy-ram.c +1 -1
 {
-    PostcopyDiscardState *res = g_malloc0(sizeof(PostcopyDiscardState));
+    static PostcopyDiscardState res = {0};

Do you think it would be better to make this a static at the top of migration/postcopy-ram.c and then we could remove the pds parameters from postcopy_discard_send_range and friends?
If there's only one pds then we don't need to pass the pointer around.

It sounds better to me, let me prepare v2.

Thanks

Quoted 29 lines

Dave

Quoted 25 lines
migration/postcopy-ram.c +5 -6
-    if (res) {
-        res->ramblock_name = name;
-    }
+    res.ramblock_name = name;
+    res.cur_entry = 0;
+    res.nsentwords = 0;
+    res.nsentcmds = 0;
 
-    return res;
+    return &res;
 }
 
 /**
@@ -1449,8 +1449,6 @@ void postcopy_discard_send_finish(MigrationState *ms, PostcopyDiscardState *pds)
 
     trace_postcopy_discard_send_finish(pds->ramblock_name, pds->nsentwords,
                                        pds->nsentcmds);
-
-    g_free(pds);
 }
 
 /*
Signature · 1 line
2.17.1

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

Signature · 2 lines
Wei Yang
Help you, Help me
author
#3 lore raw
In reply to Dr. David Alan Gilbert
Quoted 29 lines · Dr. David Alan Gilbert
Quoted 28 lines · * Wei Yang ([email protected]) wrote:

Even we need to do discard for each RAMBlock, we still can leverage the same memory space to store the information.

By doing so, we avoid memory allocation and deallocation to the system and also avoid potential failure of memory allocation which breaks the migration.

---
 migration/postcopy-ram.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
migration/postcopy-ram.c +1 -2
index 9faacacc9e..2e6b076bb7 100644
@@ -1377,8 +1377,7 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
  *   asking to discard individual ranges.
  *
  * @ms: The current migration state.
- * @offset: the bitmap offset of the named RAMBlock in the migration
- *   bitmap.
+ * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
  * @name: RAMBlock that discards will operate on.
  *
  * returns: a new PDS.
@@ -1386,13 +1385,14 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
 PostcopyDiscardState *postcopy_discard_send_init(MigrationState *ms,
Quoted 8 lines

migration/postcopy-ram.c void postcopy_fault_thread_notify(MigrationIncomingState *mis) · line 1388

Quoted 4 lines
migration/postcopy-ram.c +1 -1
                                                  const char *name)
 {
-    PostcopyDiscardState *res = g_malloc0(sizeof(PostcopyDiscardState));
+    static PostcopyDiscardState res = {0};

Do you think it would be better to make this a static at the top of migration/postcopy-ram.c and then we could remove the pds parameters from postcopy_discard_send_range and friends?

Just took a look into this one. One problem is not all its friends are in migration/postcopy-ram.c

For example, postcopy_chunk_hostpages_pass() is in migration/ram.c.

Which way do you prefer?

Quoted 31 lines

If there's only one pds then we don't need to pass the pointer around.

Dave

Quoted 25 lines
migration/postcopy-ram.c +5 -6
-    if (res) {
-        res->ramblock_name = name;
-    }
+    res.ramblock_name = name;
+    res.cur_entry = 0;
+    res.nsentwords = 0;
+    res.nsentcmds = 0;
 
-    return res;
+    return &res;
 }
 
 /**
@@ -1449,8 +1449,6 @@ void postcopy_discard_send_finish(MigrationState *ms, PostcopyDiscardState *pds)
 
     trace_postcopy_discard_send_finish(pds->ramblock_name, pds->nsentwords,
                                        pds->nsentcmds);
-
-    g_free(pds);
 }
 
 /*
Signature · 1 line
2.17.1

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

Signature · 2 lines
Wei Yang
Help you, Help me
#4 lore raw
Quoted 39 lines · * Wei Yang ([email protected]) wrote:
Quoted 29 lines · Dr. David Alan Gilbert
Quoted 28 lines · * Wei Yang ([email protected]) wrote:

Even we need to do discard for each RAMBlock, we still can leverage the same memory space to store the information.

By doing so, we avoid memory allocation and deallocation to the system and also avoid potential failure of memory allocation which breaks the migration.

---
 migration/postcopy-ram.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
migration/postcopy-ram.c +1 -2
index 9faacacc9e..2e6b076bb7 100644
@@ -1377,8 +1377,7 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
  *   asking to discard individual ranges.
  *
  * @ms: The current migration state.
- * @offset: the bitmap offset of the named RAMBlock in the migration
- *   bitmap.
+ * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
  * @name: RAMBlock that discards will operate on.
  *
  * returns: a new PDS.
@@ -1386,13 +1385,14 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
 PostcopyDiscardState *postcopy_discard_send_init(MigrationState *ms,
Quoted 8 lines

migration/postcopy-ram.c void postcopy_fault_thread_notify(MigrationIncomingState *mis) · line 1388

Quoted 4 lines
migration/postcopy-ram.c +1 -1
                                                  const char *name)
 {
-    PostcopyDiscardState *res = g_malloc0(sizeof(PostcopyDiscardState));
+    static PostcopyDiscardState res = {0};

Do you think it would be better to make this a static at the top of migration/postcopy-ram.c and then we could remove the pds parameters from postcopy_discard_send_range and friends?

Quoted 4 lines

Just took a look into this one. One problem is not all its friends are in migration/postcopy-ram.c

For example, postcopy_chunk_hostpages_pass() is in migration/ram.c.

But doesn't that just pass teh pds back to postcopy_discard_send_range which is in postcopy-ram ?

Dave

Quoted 27 lines

Which way do you prefer?

Quoted 25 lines

If there's only one pds then we don't need to pass the pointer around.

Dave

Quoted 21 lines
migration/postcopy-ram.c +5 -6
-    if (res) {
-        res->ramblock_name = name;
-    }
+    res.ramblock_name = name;
+    res.cur_entry = 0;
+    res.nsentwords = 0;
+    res.nsentcmds = 0;
 
-    return res;
+    return &res;
 }
 
 /**
@@ -1449,8 +1449,6 @@ void postcopy_discard_send_finish(MigrationState *ms, PostcopyDiscardState *pds)
 
     trace_postcopy_discard_send_finish(pds->ramblock_name, pds->nsentwords,
                                        pds->nsentcmds);
-
-    g_free(pds);
 }
 
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
migration/postcopy-ram.c +0 -0
 /*
Signature · 1 line
2.17.1

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

Signature · 2 lines
Wei Yang
Help you, Help me

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

author
#5 lore raw
Quoted 40 lines · Dr. David Alan Gilbert
Quoted 39 lines · * Wei Yang ([email protected]) wrote:
Quoted 37 lines · Dr. David Alan Gilbert
Quoted 27 lines · * Wei Yang ([email protected]) wrote:

Even we need to do discard for each RAMBlock, we still can leverage the same memory space to store the information.

By doing so, we avoid memory allocation and deallocation to the system and also avoid potential failure of memory allocation which breaks the migration.

---
 migration/postcopy-ram.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
migration/postcopy-ram.c +1 -2
index 9faacacc9e..2e6b076bb7 100644
@@ -1377,8 +1377,7 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
  *   asking to discard individual ranges.
  *
  * @ms: The current migration state.
- * @offset: the bitmap offset of the named RAMBlock in the migration
- *   bitmap.
+ * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
  * @name: RAMBlock that discards will operate on.
  *
  * returns: a new PDS.

migration/postcopy-ram.c void postcopy_fault_thread_notify(MigrationIncomingState *mis) · line 1388

Quoted 5 lines
migration/postcopy-ram.c +1 -1
@@ -1386,13 +1385,14 @@ void postcopy_fault_thread_notify(MigrationIncomingState *mis)
 PostcopyDiscardState *postcopy_discard_send_init(MigrationState *ms,
                                                  const char *name)
 {
-    PostcopyDiscardState *res = g_malloc0(sizeof(PostcopyDiscardState));
+    static PostcopyDiscardState res = {0};

Do you think it would be better to make this a static at the top of migration/postcopy-ram.c and then we could remove the pds parameters from postcopy_discard_send_range and friends?

Quoted 7 lines
Quoted 4 lines

Just took a look into this one. One problem is not all its friends are in migration/postcopy-ram.c

For example, postcopy_chunk_hostpages_pass() is in migration/ram.c.

But doesn't that just pass teh pds back to postcopy_discard_send_range which is in postcopy-ram ?

You are right, I didn't notice this.

Quoted 3 lines

Dave