fs/openfile: refactor code based on ltp rules
PATCH v2 1/1 fs/openfile: refactor code based on ltp rules
From: George Wang <[email protected]>
--- testcases/kernel/fs/openfile/openfile.c | 149 +++++++++++--------------------- 1 file changed, 49 insertions(+), 100 deletions(-)
testcases/kernel/fs/openfile/openfile.c +49 -100
@@ -38,7 +38,7 @@#include "test.h" #include "usctest.h" -char *TCID ="openfile01" ;/* Test program identifier. */ +char *TCID ="openfile01" ;int TST_TOTAL =1 ;#define MAXFILES 32768
@@ -52,33 +52,38 @@ struct cb {int thr_sleeping; } c; -/* Global Variables */ int numthreads =10 , numfiles =10 ;int debug =0 ;char *filename ="FILETOOPEN" ; +FILE *fd =NULL ; -void setup (void ) +void cleanup (void ) { -tst_tmpdir (); +if (fd) +fclose (fd); +tst_rmdir (); + } -void cleanup (void ) +void setup (void ) { -tst_rmdir (); +tst_tmpdir (); + +if ((fd =fopen (filename,"w" )) ==NULL ) +tst_brkm (TBROK ,NULL ,"Could not create file" ); +pthread_mutex_init (&c.m,NULL ); +pthread_cond_init (&c.init_cv,NULL ); +pthread_cond_init (&c.thr_cv,NULL ); + c.thr_sleeping =0 ; } -/* Procedures */ -void *threads (void *thread_id); -/* ************************************************************************** -* MAIN PROCEDURE * -************************************************************************** */ +void *threads (void *thread_id);int main (int argc,char *argv[]) {int i, opt, badopts =0 ; -FILE *fd;pthread_t th_id;char msg[80 ] ="" ;extern char *optarg;
@@ -101,100 +106,57 @@ int main(int argc, char *argv[])case 'h' :default :printf ("Usage: openfile [-d] -f FILES -t THREADS\n" ); -_exit (1 ); +tst_brkm (TBROK ,NULL ,"PARSE OPTION" ); } } -if (badopts) { -printf ("Usage: openfile [-d] -f FILES -t THREADS\n" ); -_exit (1 ); - } - -setup (); +if (badopts) +tst_brkm (TBROK ,NULL ,"Usage: openfile [-d] -f FILES -t THREADS" ); -/* Check if numthreads is less than MAXFILES */ if (numfiles >MAXFILES ) {sprintf (msg,"%s\nCannot use %d files" , msg, numfiles);sprintf (msg,"%s, used %d files instead\n" , msg,MAXFILES ); numfiles =MAXFILES ; } -/* Check if numthreads is less than MAXTHREADS */ if (numthreads >MAXTHREADS ) {sprintf (msg,"%s\nCannot use %d threads" , msg, numthreads);sprintf (msg,"%s, used %d threads instead\n" , msg,MAXTHREADS ); numthreads =MAXTHREADS ; } -/* Create files */ -if ((fd =fopen (filename,"w" )) ==NULL ) { -tst_resm (TFAIL ,"Could not create file" ); -cleanup (); - } - -/* Initialize thread control variables, lock & condition */ -pthread_mutex_init (&c.m,NULL ); -pthread_cond_init (&c.init_cv,NULL ); -pthread_cond_init (&c.thr_cv,NULL ); - c.thr_sleeping =0 ; +setup (); -/* Grab mutex lock */ -if (pthread_mutex_lock (&c.m)) { -tst_resm (TFAIL ,"failed to grab mutex lock" ); -fclose (fd); -unlink (filename); -cleanup (); - } +if (pthread_mutex_lock (&c.m)) +tst_brkm (TBROK , cleanup,"failed to grab mutex lock" );printf ("Creating Reading Threads\n" );/* Create threads */ -for (i =0 ; i < numthreads; i++) -if (pthread_create (&th_id,NULL , threads, - (void *)(uintptr_t ) i)) { -tst_resm (TFAIL , -"failed creating a pthread; increase limits" ); -fclose (fd); -unlink (filename); -cleanup (); - } +for (i =0 ; i < numthreads; i++) { +if (pthread_create (&th_id,NULL , threads, (void *)(uintptr_t ) i)) +tst_brkm (TFAIL , cleanup,"failed creating a pthread; increase limits" ); + }/* Sleep until all threads are created */ -while (c.thr_sleeping != numthreads) -if (pthread_cond_wait (&c.init_cv, &c.m)) { -tst_resm (TFAIL , -"error while waiting for reading threads" ); -fclose (fd); -unlink (filename); -cleanup (); - } +while (c.thr_sleeping != numthreads) { +if (pthread_cond_wait (&c.init_cv, &c.m)) +tst_brkm (TFAIL , cleanup,"error while waiting for reading threads" ); + }/* Wake up all threads */ -if (pthread_cond_broadcast (&c.thr_cv)) { -tst_resm (TFAIL ,"failed trying to wake up reading threads" ); -fclose (fd); -unlink (filename); -cleanup (); - } +if (pthread_cond_broadcast (&c.thr_cv)) +tst_brkm (TFAIL , cleanup,"failed trying to wake up reading threads" ); +/* Release mutex lock */ -if (pthread_mutex_unlock (&c.m)) { -tst_resm (TFAIL ,"failed to release mutex lock" ); -fclose (fd); -unlink (filename); -cleanup (); - } +if (pthread_mutex_unlock (&c.m)) +tst_brkm (TFAIL , cleanup,"failed to release mutex lock" );tst_resm (TPASS ,"Threads are done reading" ); -fclose (fd); -unlink (filename);cleanup (); -_exit (0 ); +tst_exit (); } -/* ************************************************************************** -* OTHER PROCEDURES * -************************************************************************** */ -void close_files (FILE * fd_list[],int len) {int i;
@@ -210,8 +172,8 @@ void *threads(void *thread_id_)char errmsg[80 ];FILE *fd_list[MAXFILES ];int i; +int rs =1 ; -/* Open files */ for (i =0 ; i < numfiles; i++) {if (debug) printf("Thread %d : Opening file number %d \n",
@@ -219,49 +181,36 @@ void *threads(void *thread_id_)if ((fd_list[i] =fopen (filename,"rw" )) ==NULL ) {sprintf (errmsg,"FAIL - Couldn't open file #%d" , i);perror (errmsg); -if (i >0 ) { -close_files (fd_list, i -1 ); - } -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; } } -/* Grab mutex lock */ if (pthread_mutex_lock (&c.m)) {perror ("FAIL - failed to grab mutex lock" ); -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; }/* Check if you should wake up main thread */ if (++c.thr_sleeping == numthreads)if (pthread_cond_signal (&c.init_cv)) {perror ("FAIL - failed to signal main thread" ); -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; } -/* Sleep until woken up */ if (pthread_cond_wait (&c.thr_cv, &c.m)) {perror ("FAIL - failed to wake up correctly" ); -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; } -/* Release mutex lock */ if (pthread_mutex_unlock (&c.m)) {perror ("FAIL - failed to release mutex lock" ); -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; } + + rs =0 ; + +ret: -/* Close file handles and exit */ -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit (NULL ); +close_files (fd_list, i); +pthread_exit ((void *)rs); }
Signature · 13 lines
1.8.4.2 ------------------------------------------------------------------------------ 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
----- Original Message -----
Quoted 8 lines
From: [email protected]
To: [email protected]
Sent: Monday, 17 November, 2014 5:06:13 PM
Subject: [LTP] [PATCH V2] fs/openfile: refactor code based on ltp rulesFrom: George Wang <[email protected]>
Signed-off-by George Wang <[email protected]>
Hi,
few comments inline - mostly cosmetic stuff.
I noticed that main is not waiting for threads to finish, do you know if this is on purpose? Looking at messages in main(), it doesn't seem so.
Regards,
Jan
Quoted 72 lines
--- testcases/kernel/fs/openfile/openfile.c | 149 +++++++++++--------------------- 1 file changed, 49 insertions(+), 100 deletions(-)testcases/kernel/fs/openfile/openfile.c +17
-12@@ -38,7 +38,7 @@#include "test.h" #include "usctest.h" -char *TCID ="openfile01" ;/* Test program identifier. */ +char *TCID ="openfile01" ;int TST_TOTAL =1 ;#define MAXFILES 32768@@ -52,33 +52,38 @@ struct cb {int thr_sleeping; } c; -/* Global Variables */ int numthreads = 10, numfiles = 10; int debug = 0; char *filename = "FILETOOPEN"; +FILE *fd = NULL; -void setup (void ) +void cleanup(void) { -tst_tmpdir (); +if (fd) +fclose(fd); +tst_rmdir(); +} -void cleanup (void ) +void setup(void) { -tst_rmdir (); +tst_tmpdir(); + +if ((fd = fopen(filename, "w")) == NULL) +tst_brkm(TBROK, NULL, "Could not create file"); +pthread_mutex_init(&c.m, NULL); +pthread_cond_init(&c.init_cv, NULL); +pthread_cond_init(&c.thr_cv, NULL); +c.thr_sleeping = 0; } -/* Procedures */ -void *threads (void *thread_id); -/* ************************************************************************** -* MAIN PROCEDURE * -************************************************************************** */ +void *threads (void *thread_id);int main (int argc,char *argv[]) {int i, opt, badopts =0 ; -FILE *fd;pthread_t th_id;testcases/kernel/fs/openfile/openfile.c +0
-0char msg[80 ] ="" ;extern char *optarg;
testcases/kernel/fs/openfile/openfile.c int main(int argc, char *argv[]) · line 109
Quoted 5 lines
testcases/kernel/fs/openfile/openfile.c +1
-1@@ -101,100 +106,57 @@ int main(int argc, char *argv[])case 'h' :default :printf ("Usage: openfile [-d] -f FILES -t THREADS\n" ); -_exit (1 ); +tst_brkm (TBROK ,NULL ,"PARSE OPTION" );
This now gives TBROK messages for "-h" parameter.
testcases/kernel/fs/openfile/openfile.c int main(int argc, char *argv[]) · line 108
Quoted 4 lines
testcases/kernel/fs/openfile/openfile.c +0
-2} } -if (badopts) { -printf ("Usage: openfile [-d] -f FILES -t THREADS\n" );
This line is too long.
Quoted 33 lines
testcases/kernel/fs/openfile/openfile.c +3
-17-_exit (1 ); - } - -setup (); +if (badopts) +tst_brkm (TBROK ,NULL ,"Usage: openfile [-d] -f FILES -t THREADS" ); -/* Check if numthreads is less than MAXFILES */ if (numfiles >MAXFILES ) {sprintf (msg,"%s\nCannot use %d files" , msg, numfiles);sprintf (msg,"%s, used %d files instead\n" , msg,MAXFILES ); numfiles =MAXFILES ; } -/* Check if numthreads is less than MAXTHREADS */ if (numthreads >MAXTHREADS ) {sprintf (msg,"%s\nCannot use %d threads" , msg, numthreads);sprintf (msg,"%s, used %d threads instead\n" , msg,MAXTHREADS ); numthreads =MAXTHREADS ; } -/* Create files */ -if ((fd =fopen (filename,"w" )) ==NULL ) { -tst_resm (TFAIL ,"Could not create file" ); -cleanup (); - } - -/* Initialize thread control variables, lock & condition */ -pthread_mutex_init (&c.m,NULL ); -pthread_cond_init (&c.init_cv,NULL ); -pthread_cond_init (&c.thr_cv,NULL ); - c.thr_sleeping =0 ; +setup ();
testcases/kernel/fs/openfile/openfile.c int main(int argc, char *argv[]) · line 130
Quoted 10 lines
testcases/kernel/fs/openfile/openfile.c +2
-7-/* Grab mutex lock */ -if (pthread_mutex_lock (&c.m)) { -tst_resm (TFAIL ,"failed to grab mutex lock" ); -fclose (fd); -unlink (filename); -cleanup (); - } +if (pthread_mutex_lock (&c.m)) +tst_brkm (TBROK , cleanup,"failed to grab mutex lock" );
If you wanted to print also reason for failure, we have TRERRNO, which will treat TEST_RESULT as errno:
TEST(pthread_function);
tst_brkm(TBROK|TRERRNO, ...);
Quoted 6 lines
testcases/kernel/fs/openfile/openfile.c +0
-2printf ("Creating Reading Threads\n" );/* Create threads */ -for (i =0 ; i < numthreads; i++) - if (pthread_create(&th_id, NULL, threads,
testcases/kernel/fs/openfile/openfile.c int main(int argc, char *argv[]) · line 137
Quoted 10 lines
testcases/kernel/fs/openfile/openfile.c +3
-7- (void *)(uintptr_t) i)) { -tst_resm (TFAIL , -"failed creating a pthread; increase limits" ); -fclose (fd); -unlink (filename); -cleanup (); - } +for (i =0 ; i < numthreads; i++) { +if (pthread_create (&th_id,NULL , threads, (void *)(uintptr_t ) i)) +tst_brkm (TFAIL , cleanup,"failed creating a pthread; increase limits" );
I think TBROK would fit more here, and also in other places where pthread_ functions fail. Both lines above are too long.
Quoted 4 lines
testcases/kernel/fs/openfile/openfile.c +1
-1+ }/* Sleep until all threads are created */ -while (c.thr_sleeping != numthreads)
testcases/kernel/fs/openfile/openfile.c int main(int argc, char *argv[]) · line 143
Quoted 10 lines
testcases/kernel/fs/openfile/openfile.c +3
-7-if (pthread_cond_wait (&c.init_cv, &c.m)) { -tst_resm (TFAIL , -"error while waiting for reading threads" ); -fclose (fd); -unlink (filename); -cleanup (); - } +while (c.thr_sleeping != numthreads) { +if (pthread_cond_wait (&c.init_cv, &c.m)) +tst_brkm (TFAIL , cleanup,"error while waiting for reading threads" );
Line too long.
testcases/kernel/fs/openfile/openfile.c int main(int argc, char *argv[]) · line 148
Quoted 11 lines
testcases/kernel/fs/openfile/openfile.c +3
-6+ }/* Wake up all threads */ -if (pthread_cond_broadcast (&c.thr_cv)) { -tst_resm (TFAIL ,"failed trying to wake up reading threads" ); -fclose (fd); -unlink (filename); -cleanup (); - } +if (pthread_cond_broadcast (&c.thr_cv)) +tst_brkm (TFAIL , cleanup,"failed trying to wake up reading threads" );
Line too long.
testcases/kernel/fs/openfile/openfile.c int main(int argc, char *argv[]) · line 154
Quoted 12 lines
testcases/kernel/fs/openfile/openfile.c +3
-6+/* Release mutex lock */ -if (pthread_mutex_unlock (&c.m)) { -tst_resm (TFAIL ,"failed to release mutex lock" ); -fclose (fd); -unlink (filename); -cleanup (); - } +if (pthread_mutex_unlock (&c.m)) +tst_brkm (TFAIL , cleanup,"failed to release mutex lock" );tst_resm (TPASS ,"Threads are done reading" );
Shouldn't main join the reading threads? It doesn't seem to wait until they are finished.
Quoted 28 lines
testcases/kernel/fs/openfile/openfile.c +2
-8-fclose (fd); -unlink (filename);cleanup(); -_exit (0 ); +tst_exit(); } -/* ************************************************************************** -* OTHER PROCEDURES * -************************************************************************** */ -void close_files (FILE * fd_list[],int len) {int i;@@ -210,8 +172,8 @@ void *threads(void *thread_id_)char errmsg[80 ];FILE *fd_list[MAXFILES ];int i; +int rs =1 ; -/* Open files */ for (i =0 ; i < numfiles; i++) {if (debug) printf("Thread %d : Opening file number %d \n",
testcases/kernel/fs/openfile/openfile.c void *threads(void *thread_id_) · line 183
Quoted 3 lines
testcases/kernel/fs/openfile/openfile.c +0
-0@@ -219,49 +181,36 @@ void *threads(void *thread_id_)if ((fd_list[i] =fopen (filename,"rw" )) ==NULL ) {sprintf (errmsg,"FAIL - Couldn't open file #%d" , i);perror (errmsg);
We have TFAIL|TERRNO
testcases/kernel/fs/openfile/openfile.c void *threads(void *thread_id_) · line 189
Quoted 12 lines
testcases/kernel/fs/openfile/openfile.c +1
-6-if (i >0 ) { -close_files (fd_list, i -1 ); - } -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; } } -/* Grab mutex lock */ if (pthread_mutex_lock (&c.m)) {perror ("FAIL - failed to grab mutex lock" );
perror() uses errno, and pthread_mutex returns failure directly. As mentioned above, you can do:
if (TEST(pthread_mutex_lock(&c.m))) {
tst_brkm(TBROK|TRERRNO, cleanup, "failed to grab mutex lock");}
Quoted 49 lines
testcases/kernel/fs/openfile/openfile.c +10
-19-close_files (fd_list, numfiles); -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; }/* Check if you should wake up main thread */ if (++c.thr_sleeping == numthreads)if (pthread_cond_signal (&c.init_cv)) {perror ("FAIL - failed to signal main thread" ); -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; } -/* Sleep until woken up */ if (pthread_cond_wait (&c.thr_cv, &c.m)) {perror ("FAIL - failed to wake up correctly" ); -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; } -/* Release mutex lock */ if (pthread_mutex_unlock (&c.m)) {perror ("FAIL - failed to release mutex lock" ); -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit ((void *)1 ); + goto ret; } + + rs =0 ; + +ret: -/* Close file handles and exit */ -close_files (fd_list, numfiles); -unlink (filename); -pthread_exit (NULL ); +close_files (fd_list, i); +pthread_exit ((void *)rs); } --1.8.4.2
Quoted 11 lines
------------------------------------------------------------------------------
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
------------------------------------------------------------------------------
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