/vm/hugepage/thp/thp05: cleanup should not use safe macros
PATCH 1/1 /vm/hugepage/thp/thp05: cleanup should not use safe macros
NOTE: It's wise NOT to use safe macros in test cleanup(). This is because
all safe macros call tst_brkm(), which exits the test immediately, making the cleanup() exit prematurely. (Actually, this is hacked around in the test library at the moment so that the cleanup() will finish, but the hack will be removed in the future).
--- testcases/kernel/mem/thp/thp05.c | 69 ++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 13 deletions(-)
testcases/kernel/mem/thp/thp05.c +56 -13
@@ -66,6 +66,7 @@ option_t thp_options[] = {static int pre_thp_scan_sleep_millisecs;static int pre_thp_alloc_sleep_millisecs;static char pre_thp_enabled[BUFSIZ ]; +static char buf[BUFSIZ ], path[BUFSIZ ];int main (int argc,char *argv[]) {
@@ -129,19 +130,61 @@ void setup(void)void cleanup (void ) { -SAFE_FILE_PRINTF (NULL ,PATH_KHPD "scan_sleep_millisecs" , -"%d" , pre_thp_scan_sleep_millisecs); - -SAFE_FILE_PRINTF (NULL ,PATH_KHPD "alloc_sleep_millisecs" , -"%d" , pre_thp_alloc_sleep_millisecs); - -if (strcmp (pre_thp_enabled,"[always] madvise never" ) ==0 ) -SAFE_FILE_PRINTF (NULL ,PATH_THP "enabled" ,"always" ); -else if (strcmp (pre_thp_enabled,"always [madvise] never" ) ==0 ) -SAFE_FILE_PRINTF (NULL ,PATH_THP "enabled" ,"madvise" ); -else -SAFE_FILE_PRINTF (NULL ,PATH_THP "enabled" ,"never" ); - +int fd; + +/* restore the scan_sleep_millisecs to original value */ +snprintf (buf,BUFSIZ ,"%d" , pre_thp_scan_sleep_millisecs); + fd =open (PATH_KHPD "scan_sleep_millisecs" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore scan_sleep_millisecs to %d" , + pre_thp_scan_sleep_millisecs); +if (write (fd, buf,strlen (buf)) !=strlen (buf)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + +/* restore the alloc_sleep_millisecs to original value */ +snprintf (buf,BUFSIZ ,"%d" , pre_thp_alloc_sleep_millisecs); + fd =open (PATH_KHPD "alloc_sleep_millisecs" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore alloc_sleep_millisecs to %d" , + pre_thp_alloc_sleep_millisecs); +if (write (fd, buf,strlen (buf)) !=strlen (buf)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + +/* restore the transparent_hugepage options to original state */ +if (strcmp (pre_thp_enabled,"[always] madvise never" ) ==0 ){ +snprintf (path,BUFSIZ ,"always" ); + fd =open (PATH_THP "enabled" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore transparent_hugepage to %s" , path); +if (write (fd, path,strlen (path)) !=strlen (path)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + } +else if (strcmp (pre_thp_enabled,"always [madvise] never" ) ==0 ){ +snprintf (path,BUFSIZ ,"madvise" ); + fd =open (PATH_THP "enabled" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore transparent_hugepage to %s" , path); +if (write (fd, path,strlen (path)) !=strlen (path)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + } +else { +snprintf (path,BUFSIZ ,"never" ); + fd =open (PATH_THP "enabled" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore transparent_hugepage to %s" , path); +if (write (fd, path,strlen (path)) !=strlen (path)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + }TEST_CLEANUP ; }
Signature · 8 lines
1.9.3 ------------------------------------------------------------------------------ _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
Hi!
Quoted 85 lines
NOTE: It's wise NOT to use safe macros in test
cleanup(). This is because
all safe macros calltst_brkm(), which exits the test immediately, making thecleanup()exit prematurely. (Actually, this is hacked around in the test library at the moment so that thecleanup()will finish, but the hack will be removed in the future).Signed-off-by Li Wang <[email protected]>--- testcases/kernel/mem/thp/thp05.c | 69 ++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 13 deletions(-)testcases/kernel/mem/thp/thp05.c +46
-13@@ -66,6 +66,7 @@ option_t thp_options[] = {static int pre_thp_scan_sleep_millisecs;static int pre_thp_alloc_sleep_millisecs;static char pre_thp_enabled[BUFSIZ ]; +static char buf[BUFSIZ ], path[BUFSIZ ];int main (int argc,char *argv[]) {@@ -129,19 +130,61 @@ void setup(void)void cleanup (void ) { -SAFE_FILE_PRINTF (NULL ,PATH_KHPD "scan_sleep_millisecs" , -"%d" , pre_thp_scan_sleep_millisecs); - -SAFE_FILE_PRINTF (NULL ,PATH_KHPD "alloc_sleep_millisecs" , -"%d" , pre_thp_alloc_sleep_millisecs); - -if (strcmp (pre_thp_enabled,"[always] madvise never" ) ==0 ) -SAFE_FILE_PRINTF (NULL ,PATH_THP "enabled" ,"always" ); -else if (strcmp (pre_thp_enabled,"always [madvise] never" ) ==0 ) -SAFE_FILE_PRINTF (NULL ,PATH_THP "enabled" ,"madvise" ); -else -SAFE_FILE_PRINTF (NULL ,PATH_THP "enabled" ,"never" ); - +int fd; + +/* restore the scan_sleep_millisecs to original value */ +snprintf (buf,BUFSIZ ,"%d" , pre_thp_scan_sleep_millisecs); + fd =open (PATH_KHPD "scan_sleep_millisecs" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore scan_sleep_millisecs to %d" , + pre_thp_scan_sleep_millisecs); +if (write (fd, buf,strlen (buf)) !=strlen (buf)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + +/* restore the alloc_sleep_millisecs to original value */ +snprintf (buf,BUFSIZ ,"%d" , pre_thp_alloc_sleep_millisecs); + fd =open (PATH_KHPD "alloc_sleep_millisecs" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore alloc_sleep_millisecs to %d" , + pre_thp_alloc_sleep_millisecs); +if (write (fd, buf,strlen (buf)) !=strlen (buf)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + +/* restore the transparent_hugepage options to original state */ +if (strcmp (pre_thp_enabled,"[always] madvise never" ) ==0 ){ +snprintf (path,BUFSIZ ,"always" ); + fd =open (PATH_THP "enabled" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore transparent_hugepage to %s" , path); +if (write (fd, path,strlen (path)) !=strlen (path)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + } +else if (strcmp (pre_thp_enabled,"always [madvise] never" ) ==0 ){ +snprintf (path,BUFSIZ ,"madvise" ); + fd =open (PATH_THP "enabled" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore transparent_hugepage to %s" , path); +if (write (fd, path,strlen (path)) !=strlen (path)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + }
testcases/kernel/mem/thp/thp05.c void setup(void) · line 187
Quoted 10 lines
testcases/kernel/mem/thp/thp05.c +10
-0+else { +snprintf (path,BUFSIZ ,"never" ); + fd =open (PATH_THP "enabled" ,O_WRONLY ); +if (fd ==-1 ) +tst_resm (TWARN |TERRNO ,"open" ); +tst_resm (TINFO ,"restore transparent_hugepage to %s" , path); +if (write (fd, path,strlen (path)) !=strlen (path)) +tst_resm (TWARN |TERRNO ,"write" ); +close (fd); + }
While generally avoiding the safe macros in cleanup is fine, in this case you have replaced about ten lines of code with about hundred lines of copy & pasted code and even the coding style is wrong.
What should be done in this case is to create an equivalent to the SAFE_FILE_PRINTF() function that is not calling the tst_brkm() and issues only a warning. Or modify SAFE_FILE_PRINTF() to have additional parameter that selects if on failure tst_brkm() or tst_resm(TWARN, ...) is called.
Signature · 8 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list
Hi!
Quoted 11 lines
Quoted 2 lines
What should be done in this case is to create an equivalent to the
SAFE_FILE_PRINTF()function that is not calling thetst_brkm()andwell, if create a new safe function/macro to replace use the
SAFE_FILE_PRINTFbut not callingtst_brkm(), that's will be break the safe rule. if create a unsafe function/macro, I dont know where is to place the new function and how to rename.Additional, i found so much testcases use safe macro in there
cleanup(), that's really a hard work if maintain all.
The ideal solution would be to create a base functions file_scanf() and file_printf() that would look like safe_file_scanf() and safe_file_printf() but without the file, lineno and cleanup parameters.
These would return non-zero return value on failure and then we can base the safe_file_scanf() on the top of these functions.
I.e. safe_file_scanf() would call file_scanf() and call tst_brkm() if file_scanf() has reported a failure. Then we can use file_scanf() in test cleanup. But as these functions use variable number of arguments this needs one more indirection with functions that takes va_list.
int file_vscanf(const char *file, const char *fmt, va_list va)
{
// do here what safe_file_scanf() does but return with error
// instead of tst_brkm()
}int file_scanf(const char *file, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
ret = file_vscanf(file, fmt, va);
va_end(va);
return ret;
}void safe_file_scanf(const char *file, const int lineno,
void (*cleanup_fn) (void), const char *path, const char *fmt, ...)
{
// call file_vscanf() here and switch on it's exit value
// i.e. one for failed to open file
// one for EOF
// one for unexpected number of conversion
// and exit cleanly on zero
}Signature · 13 lines
Cyril Hrubis [email protected] ------------------------------------------------------------------------------ Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server from Actuate! Instantly Supercharge Your Business Reports and Dashboards with Interactivity, Sharing, Native Excel Exports, App Integration & more Get technology previously reserved for billion-dollar corporations, FREE http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk _______________________________________________ Ltp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ltp-list