some fix in tests/migration
PATCH v3 1/3 tests/migration: mem leak fix
In reply to Mao Zhongyi‘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
@@ -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
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
PATCH v3 2/3 tests/migration: fix a typo in comment
--- 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
PATCH v3 3/3 tests/migration:fix unreachable path in stress test
In reply to Mao ZhongyiIf 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
@@ -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
Re: [PATCH v3 1/3] tests/migration: mem leak fix
In reply to Mao ZhongyiLe 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_autofreerecommended byCODING_STYLE.rstto automatically release the memory that returned fromg_malloc().Signed-off-by Mao Zhongyi <[email protected]>Reviewed-by Alex Bennée <[email protected]>--- tests/migration/stress.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)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 = 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
Re: [PATCH v3 3/3] tests/migration:fix unreachable path in stress test
In reply to Mao ZhongyiLe 04/10/2019 à 05:43, Mao Zhongyi a écrit :
Quoted 26 lines
If
stressone()orstress()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.Signed-off-by Mao Zhongyi <[email protected]>--- tests/migration/stress.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)tests/migration/stress.c +1
-1@@ -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
-2argv0, 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
-1stressone (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
-2exit_failure (); - -exit_success ();
and always use exit_failure().
Quoted 2 lines
tests/migration/stress.c +0
-0}
Re: [PATCH v3 2/3] tests/migration: fix a typo in comment
In reply to Mao ZhongyiQuoted 13 lines · Mao Zhongyi
Signed-off-by Mao Zhongyi <[email protected]>Reviewed-by Alex Bennée <[email protected]>Reviewed-by Laurent Vivier <[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 );