Skip to contentSkip to conversation

some fix in tests/migration

PATCH Superseded Mao Zhongyi 3 participants 7 messages Oct 4, 2019 – Oct 7, 2019

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover some fix in tests/migration

This patchset mainly fixes memory leak, typo and return value of stress function in stress test.

v3->v2:
p1:
- replace malloc with g_malloc [Laurent Vivier]
p3:
- change stressone type to void and stree return value to -1 to make the path of 'if (stress(ramsizeGB, ncpus) < 0)'
can be reached. [Laurent Vivier]
- update the commit message.

v2->v1:
- use g_autofree to release memory automatically instead
of free(). [Alex Bennée]

Mao Zhongyi (3):
tests/migration: mem leak fix tests/migration: fix a typo in comment tests/migration:fix unreachable path in stress test

 tests/migration/stress.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/3 tests/migration: mem leak fix

‘data’ has the possibility of memory leaks, so use the glib macros g_autofree recommended by CODING_STYLE.rst to automatically release the memory that returned from g_malloc().

---
 tests/migration/stress.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
tests/migration/stress.c +2 -8
index d9aa4afe92..9e128eef50 100644
@@ -170,10 +170,10 @@ static unsigned long long now(void)
 static int stressone(unsigned long long ramsizeMB)
 {
     size_t pagesPerMB = 1024 * 1024 / PAGE_SIZE;
-    char *ram = malloc(ramsizeMB * 1024 * 1024);
+    g_autofree char *ram = g_malloc(ramsizeMB * 1024 * 1024);
     char *ramptr;
     size_t i, j, k;
-    char *data = malloc(PAGE_SIZE);
+    g_autofree char *data = g_malloc(PAGE_SIZE);
     char *dataptr;
     size_t nMB = 0;
     unsigned long long before, after;
@@ -186,7 +186,6 @@ static int stressone(unsigned long long ramsizeMB)
     if (!data) {
         fprintf(stderr, "%s (%d): ERROR: cannot allocate %d bytes of RAM: %s\n",
                 argv0, gettid(), PAGE_SIZE, strerror(errno));
-        free(ram);
         return -1;
     }
 
@@ -198,8 +197,6 @@ static int stressone(unsigned long long ramsizeMB)
     memset(ram, 0xfe, ramsizeMB * 1024 * 1024);
 
     if (random_bytes(data, PAGE_SIZE) < 0) {
-        free(ram);
-        free(data);
         return -1;
     }
 
@@ -227,9 +224,6 @@ static int stressone(unsigned long long ramsizeMB)
             }
         }
     }
-
-    free(data);
-    free(ram);
 }
 
 
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v3 1/3] tests/migration: mem leak fix

Le 04/10/2019 à 05:43, Mao Zhongyi a écrit :

Quoted 29 lines

‘data’ has the possibility of memory leaks, so use the glib macros g_autofree recommended by CODING_STYLE.rst to automatically release the memory that returned from g_malloc().

---
 tests/migration/stress.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
tests/migration/stress.c +2 -2
index d9aa4afe92..9e128eef50 100644
@@ -170,10 +170,10 @@ static unsigned long long now(void)
 static int stressone(unsigned long long ramsizeMB)
 {
     size_t pagesPerMB = 1024 * 1024 / PAGE_SIZE;
-    char *ram = malloc(ramsizeMB * 1024 * 1024);
+    g_autofree char *ram = g_malloc(ramsizeMB * 1024 * 1024);
     char *ramptr;
     size_t i, j, k;
-    char *data = malloc(PAGE_SIZE);
+    g_autofree char *data = g_malloc(PAGE_SIZE);
     char *dataptr;
     size_t nMB = 0;
     unsigned long long before, after;

tests/migration/stress.c static int stressone(unsigned long long ramsizeMB) · line 186

Quoted 1 line
tests/migration/stress.c +0 -0
@@ -186,7 +186,6 @@ static int stressone(unsigned long long ramsizeMB)
     if (!data) {

As g_malloc() aborts on error, data is never NULL. There is the same thing for ram.

Thanks,
Laurent

#3 lore raw

PATCH v3 2/3 tests/migration: fix a typo in comment

In reply to Mao Zhongyi
---
 tests/migration/stress.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/migration/stress.c +1 -1
index 9e128eef50..debf34359f 100644
@@ -191,7 +191,7 @@ static int stressone(unsigned long long ramsizeMB)
 
     /* We don't care about initial state, but we do want
      * to fault it all into RAM, otherwise the first iter
-     * of the loop below will be quite slow. We cna't use
+     * of the loop below will be quite slow. We can't use
      * 0x0 as the byte as gcc optimizes that away into a
      * calloc instead :-) */
     memset(ram, 0xfe, ramsizeMB * 1024 * 1024);
Signature · 1 line
2.17.1
Reviewed-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] tests/migration: fix a typo in comment

Quoted 13 lines · Mao Zhongyi
---
 tests/migration/stress.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/migration/stress.c +0 -0
index 9e128eef50..debf34359f 100644

tests/migration/stress.c static int stressone(unsigned long long ramsizeMB) · line 197

Quoted 8 lines
tests/migration/stress.c +1 -1
@@ -191,7 +191,7 @@ static int stressone(unsigned long long ramsizeMB)
 
     /* We don't care about initial state, but we do want
      * to fault it all into RAM, otherwise the first iter
-     * of the loop below will be quite slow. We cna't use
+     * of the loop below will be quite slow. We can't use
      * 0x0 as the byte as gcc optimizes that away into a
      * calloc instead :-) */
     memset(ram, 0xfe, ramsizeMB * 1024 * 1024);
#5 lore raw

PATCH v3 3/3 tests/migration:fix unreachable path in stress test

In reply to Mao Zhongyi

If stressone() or stress() exits it's because of a failure because the test runs forever otherwise, so change stressone type to void and stress should always return -1 to make the path of 'if (stress(ramsizeGB, ncpus) < 0)' can be reached.

---
 tests/migration/stress.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
tests/migration/stress.c +5 -7
index debf34359f..b0df1323bb 100644
@@ -167,7 +167,7 @@ static unsigned long long now(void)
     return (tv.tv_sec * 1000ull) + (tv.tv_usec / 1000ull);
 }
 
-static int stressone(unsigned long long ramsizeMB)
+static void stressone(unsigned long long ramsizeMB)
 {
     size_t pagesPerMB = 1024 * 1024 / PAGE_SIZE;
     g_autofree char *ram = g_malloc(ramsizeMB * 1024 * 1024);
@@ -181,12 +181,12 @@ static int stressone(unsigned long long ramsizeMB)
     if (!ram) {
         fprintf(stderr, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",
                 argv0, gettid(), ramsizeMB, strerror(errno));
-        return -1;
+        return;
     }
     if (!data) {
         fprintf(stderr, "%s (%d): ERROR: cannot allocate %d bytes of RAM: %s\n",
                 argv0, gettid(), PAGE_SIZE, strerror(errno));
-        return -1;
+        return;
     }
 
     /* We don't care about initial state, but we do want
@@ -197,7 +197,7 @@ static int stressone(unsigned long long ramsizeMB)
     memset(ram, 0xfe, ramsizeMB * 1024 * 1024);
 
     if (random_bytes(data, PAGE_SIZE) < 0) {
-        return -1;
+        return;
     }
 
     before = now();
@@ -250,7 +250,7 @@ static int stress(unsigned long long ramsizeGB, int ncpus)
 
     stressone(ramsizeMB);
 
-    return 0;
+    return -1;
 }
 
 
@@ -348,6 +348,4 @@ int main(int argc, char **argv)
 
     if (stress(ramsizeGB, ncpus) < 0)
         exit_failure();
-
-    exit_success();
 }
Signature · 1 line
2.17.1
#6 lore raw

Re: [PATCH v3 3/3] tests/migration:fix unreachable path in stress test

Le 04/10/2019 à 05:43, Mao Zhongyi a écrit :

Quoted 26 lines

If stressone() or stress() exits it's because of a failure because the test runs forever otherwise, so change stressone type to void and stress should always return -1 to make the path of 'if (stress(ramsizeGB, ncpus) < 0)' can be reached.

---
 tests/migration/stress.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
tests/migration/stress.c +1 -1
index debf34359f..b0df1323bb 100644
@@ -167,7 +167,7 @@ static unsigned long long now(void)
     return (tv.tv_sec * 1000ull) + (tv.tv_usec / 1000ull);
 }
 
-static int stressone(unsigned long long ramsizeMB)
+static void stressone(unsigned long long ramsizeMB)
 {
     size_t pagesPerMB = 1024 * 1024 / PAGE_SIZE;
     g_autofree char *ram = g_malloc(ramsizeMB * 1024 * 1024);
@@ -181,12 +181,12 @@ static int stressone(unsigned long long ramsizeMB)
     if (!ram) {
         fprintf(stderr, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",

tests/migration/stress.c static int stressone(unsigned long long ramsizeMB) · line 190

Quoted 10 lines
tests/migration/stress.c +2 -2
                 argv0, gettid(), ramsizeMB, strerror(errno));
-        return -1;
+        return;
     }
     if (!data) {
         fprintf(stderr, "%s (%d): ERROR: cannot allocate %d bytes of RAM: %s\n",
                 argv0, gettid(), PAGE_SIZE, strerror(errno));
-        return -1;
+        return;
     }

Thanks to the g_malloc() you can remove this two "if () { }" blocks.

tests/migration/stress.c static int stress(unsigned long long ramsizeGB, int ncpus)

Quoted 12 lines
tests/migration/stress.c +1 -1
     /* We don't care about initial state, but we do want
@@ -197,7 +197,7 @@ static int stressone(unsigned long long ramsizeMB)
     memset(ram, 0xfe, ramsizeMB * 1024 * 1024);
 
     if (random_bytes(data, PAGE_SIZE) < 0) {
-        return -1;
+        return;
     }
 
     before = now();
@@ -250,7 +250,7 @@ static int stress(unsigned long long ramsizeGB, int ncpus)

you can change it to "void" too

tests/migration/stress.c int main(int argc, char **argv) · line 349

Quoted 11 lines
tests/migration/stress.c +1 -1
     stressone(ramsizeMB);
 
-    return 0;
+    return -1;
 }
 
 
@@ -348,6 +348,4 @@ int main(int argc, char **argv)
 
     if (stress(ramsizeGB, ncpus) < 0)

As it exits only on failure, you can remove the "if"

tests/migration/stress.c int main(int argc, char **argv) · line 352

Quoted 3 lines
tests/migration/stress.c +0 -2
         exit_failure();
-
-    exit_success();

and always use exit_failure().

Quoted 2 lines
tests/migration/stress.c +0 -0
 }