some fix in tests/migration
v2 cover some fix in tests/migration
This patchset mainly fixes memory leak, typo and return value of stress function in stress test.
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 | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/3 tests/migration: mem leak fix
‘data’ has the possibility of memory leaks, so use the glic 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
@@ -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 = malloc(ramsizeMB * 1024 * 1024);char *ramptr;size_t i, j, k; -char *data =malloc (PAGE_SIZE ); + g_autofree char *data = 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
Re: [Qemu-devel] [PATCH v2 1/3] tests/migration: mem leak fix
Quoted 2 lines · Mao Zhongyi
‘data’ has the possibility of memory leaks, so use the glic macros
g_autofreerecommended byCODING_STYLE.rst
nit: glib
Quoted 9 lines
to automatically release the memory that returned from
g_malloc().Signed-off-by Mao Zhongyi <[email protected]>
Quoted 39 lines
--- tests/migration/stress.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)tests/migration/stress.c +2
-5@@ -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 = malloc(ramsizeMB * 1024 * 1024);char *ramptr;size_t i, j, k; -char *data =malloc (PAGE_SIZE ); + g_autofree char *data = 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 ; }
tests/migration/stress.c static int stressone(unsigned long long ramsizeMB) · line 227
Quoted 7 lines
tests/migration/stress.c +0
-3@@ -227,9 +224,6 @@ static int stressone(unsigned long long ramsizeMB) } } } - -free (data); -free (ram); }
--
Alex Bennée
Re: [PATCH v2 1/3] tests/migration: mem leak fix
In reply to Mao ZhongyiLe 11/09/2019 à 05:31, Mao Zhongyi a écrit :
Quoted 20 lines
‘data’ has the possibility of memory leaks, so use the glic macros
g_autofreerecommended byCODING_STYLE.rstto automatically release the memory that returned fromg_malloc().Signed-off-by Mao Zhongyi <[email protected]>--- tests/migration/stress.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)tests/migration/stress.c +0
-0
tests/migration/stress.c static unsigned long long now(void) · line 176
Quoted 9 lines
tests/migration/stress.c +2
-2@@ -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 = malloc(ramsizeMB * 1024 * 1024);char *ramptr;size_t i, j, k; -char *data =malloc (PAGE_SIZE ); + g_autofree char *data = malloc(PAGE_SIZE);
So perhaps g_malloc() could be a better choice as it will exit on allocation failure?
Thanks,
Laurent
Re: [PATCH v2 1/3] tests/migration: mem leak fix
Quoted 16 lines · Laurent Vivier
Le 11/09/2019 à 05:31, Mao Zhongyi a écrit :
Quoted 15 lines
‘data’ has the possibility of memory leaks, so use the glic macros
g_autofreerecommended byCODING_STYLE.rstto automatically release the memory that returned fromg_malloc().Signed-off-by Mao Zhongyi <[email protected]>--- tests/migration/stress.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
Quoted 16 lines
Quoted 5 lines
tests/migration/stress.c +0
-0tests/migration/stress.c static unsigned long long now(void) · line 176
Quoted 9 lines
tests/migration/stress.c +2
-2@@ -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 = malloc(ramsizeMB * 1024 * 1024);char *ramptr;size_t i, j, k; -char *data =malloc (PAGE_SIZE ); + g_autofree char *data = malloc(PAGE_SIZE);So perhaps
g_malloc()could be a better choice as it will exit on allocation failure?
right, will fix it in next.
Thanks,
Mao
Quoted 5 lines
Thanks,
Laurent
PATCH v2 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
@@ -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
Re: [PATCH v2 2/3] tests/migration: fix a typo in comment
Le 11/09/2019 à 05:31, Mao Zhongyi a écrit :
Quoted 16 lines
Signed-off-by Mao Zhongyi <[email protected]>Reviewed-by Alex Bennée <[email protected]>--- tests/migration/stress.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tests/migration/stress.c +0
-0
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 );
Re: [Qemu-devel] [PATCH v2 2/3] tests/migration: fix a typo in comment
In reply to Mao ZhongyiQuoted 16 lines · Mao Zhongyi
Signed-off-by Mao Zhongyi <[email protected]>Reviewed-by Alex Bennée <[email protected]>--- tests/migration/stress.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tests/migration/stress.c +0
-0
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 );
PATCH v2 3/3 tests/migration:fix unreachable path in stress test
In reply to Mao Zhongyiif stress function always return 0, the path
'if (stress(ramsizeGB, ncpus) < 0)' is nerver unreachable, so fix it to allow the test failed.
--- tests/migration/stress.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
tests/migration/stress.c +2 -3
@@ -224,6 +224,7 @@ static int stressone(unsigned long long ramsizeMB) } } } +return 0 ; }
@@ -248,9 +249,7 @@ static int stress(unsigned long long ramsizeGB, int ncpus) stressthread, &ramsizeMB); } -stressone (ramsizeMB); - -return 0 ; +return stressone (ramsizeMB); }
Signature · 1 line
2.17.1
Re: [PATCH v2 3/3] tests/migration:fix unreachable path in stress test
ping...
Quoted 36 lines · Mao Zhongyi
if stress function always return 0, the path
'if (stress(ramsizeGB, ncpus)< 0)' is nerver unreachable, so fix it to allow the test failed.Signed-off-by Mao Zhongyi <[email protected]>--- tests/migration/stress.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)tests/migration/stress.c +2
-3@@ -224,6 +224,7 @@ static int stressone(unsigned long long ramsizeMB) } } } +return 0 ; }@@ -248,9 +249,7 @@ static int stress(unsigned long long ramsizeGB, int ncpus) stressthread, &ramsizeMB); } -stressone (ramsizeMB); - -return 0 ; +return stressone (ramsizeMB); }
Re: [PATCH v2 3/3] tests/migration:fix unreachable path in stress test
In reply to Mao ZhongyiLe 11/09/2019 à 05:31, Mao Zhongyi a écrit :
Quoted 18 lines
if stress function always return 0, the path
'if (stress(ramsizeGB, ncpus)< 0)' is nerver unreachable, so fix it to allow the test failed.Signed-off-by Mao Zhongyi <[email protected]>--- tests/migration/stress.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)tests/migration/stress.c +0
-0
tests/migration/stress.c static int stressone(unsigned long long ramsizeMB) · line 228
Quoted 5 lines
tests/migration/stress.c +1
-0@@ -224,6 +224,7 @@ static int stressone(unsigned long long ramsizeMB) } } } +return 0 ; }
before the return, we have an infinite loop "while(1) { }".
So this part is dead code.
In fact, if the function exits, it's because it fails, otherwise it loops infinitely, so I think we should change its type to void and stress should always return -1.
Thanks,
Laurent
Re: _[PATCH_v2_3/3]_tests/migration:fix_unreachable_path_in_stress_test
Hi, Laurent
Quoted 26 lines · Laurent Vivier
Le 11/09/2019 à 05:31, Mao Zhongyi a écrit :
Quoted 18 lines
if stress function always return 0, the path
'if (stress(ramsizeGB, ncpus)< 0)' is nerver unreachable, so fix it to allow the test failed.Signed-off-by Mao Zhongyi <[email protected]>--- tests/migration/stress.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)tests/migration/stress.c +0
-0tests/migration/stress.c static int stressone(unsigned long long ramsizeMB) · line 228
Quoted 5 lines
tests/migration/stress.c +1
-0@@ -224,6 +224,7 @@ static int stressone(unsigned long long ramsizeMB) } } } +return 0 ; }before the return, we have an infinite loop "while(1) { }".
Quoted 5 lines
So this part is dead code.
In fact, if the function exits, it's because it fails, otherwise it loops infinitely, so I think we should change its type to void and stress should always return -1.
Yes, I think it's ok to change stressone typo to void because no one cares about its return value, but if make stress always return -1, main will always exited in exit_failure, like this:
...
if (stress(ramsizeGB, ncpus) < 0)
exit_failure();
exit_success();
}
so, perhaps also change stress typo to void may be good. then:
...
stress(ramsizeGB, ncpus);
exit_success();
}
Anther way , make stressone return 0 when infinite loop fails to exit, then main can handle both success and failure case.
what do you think?
Thanks,
Mao
Quoted 3 lines
Thanks,
Laurent
Re: _[PATCH_v2_3/3]_tests/migration:fix_unreachable_path_in_stress_test
Le 03/10/2019 à 09:17, maozy a écrit :
Quoted 50 lines
Hi, Laurent
Quoted 26 lines · Laurent Vivier
Le 11/09/2019 à 05:31, Mao Zhongyi a écrit :
Quoted 18 lines
if stress function always return 0, the path
'if (stress(ramsizeGB, ncpus)< 0)' is nerver unreachable, so fix it to allow the test failed.Signed-off-by Mao Zhongyi <[email protected]>---
tests/migration/stress.c| 5 ++---1 file changed, 2 insertions(+), 3 deletions(-)tests/migration/stress.c +0
-0tests/migration/stress.c static int stressone(unsigned long long ramsizeMB) · line 228
Quoted 5 lines
tests/migration/stress.c +1
-0@@ -224,6 +224,7 @@ static int stressone(unsigned long long ramsizeMB) } } } + return 0; }before the return, we have an infinite loop "while(1) { }".
Quoted 5 lines
So this part is dead code.
In fact, if the function exits, it's because it fails, otherwise it loops infinitely, so I think we should change its type to void and stress should always return -1.
Yes, I think it's ok to change stressone typo to void because no one cares about its return value, but if make stress always return -1, main will always exited in
exit_failure, like this:...
if (stress(ramsizeGB, ncpus)< 0)
exit_failure();
exit_success();
}so, perhaps also change stress typo to void may be good. then:
...
stress(ramsizeGB, ncpus);
Quoted 8 lines
exit_success();
}Anther way , make stressone return 0 when infinite loop fails to exit, then main can handle both success and failure case.
what do you think?
If stressone() or stress() exits it's because of a failure because the test runs forever otherwise.
So I think there is no problem to use exit_failure() as the exit function of main(): it should never be reached if the test runs without error.
Thanks,
Laurent
Re: _[PATCH_v2_3/3]_tests/migration:fix_unreachable_path_in_stress_test
Quoted 60 lines · Laurent Vivier
Le 03/10/2019 à 09:17, maozy a écrit :
Quoted 50 lines
Hi, Laurent
Quoted 26 lines · Laurent Vivier
Le 11/09/2019 à 05:31, Mao Zhongyi a écrit :
Quoted 18 lines
if stress function always return 0, the path
'if (stress(ramsizeGB, ncpus)< 0)' is nerver unreachable, so fix it to allow the test failed.Signed-off-by Mao Zhongyi <[email protected]>--- tests/migration/stress.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)tests/migration/stress.c +0
-0tests/migration/stress.c static int stressone(unsigned long long ramsizeMB) · line 228
Quoted 5 lines
tests/migration/stress.c +1
-0@@ -224,6 +224,7 @@ static int stressone(unsigned long long ramsizeMB) } } } + return 0; }before the return, we have an infinite loop "while(1) { }".
Quoted 5 lines
So this part is dead code.
In fact, if the function exits, it's because it fails, otherwise it loops infinitely, so I think we should change its type to void and stress should always return -1.
Yes, I think it's ok to change stressone typo to void because no one cares about its return value, but if make stress always return -1, main will always exited in
exit_failure, like this:...
if (stress(ramsizeGB, ncpus)< 0)
exit_failure();
exit_success();
}so, perhaps also change stress typo to void may be good. then:
...
stress(ramsizeGB, ncpus);Quoted 8 lines
exit_success();
}Anther way , make stressone return 0 when infinite loop fails to exit, then main can handle both success and failure case.
what do you think?
Quoted 6 lines
If
stressone()orstress()exits it's because of a failure because the test runs forever otherwise.So I think there is no problem to use
exit_failure()as the exit function ofmain(): it should never be reached if the test runs without error.
OK, I see, thanks a lot.
Quoted 4 lines
Thanks,
Laurent