@@ -566,6 +566,10 @@ cmd_update(int argc, char *argv[])
566566
567567#ifdef STRESS_TEST
568568
569+ #define LOG_NAME_LEN 1024
570+ char test_log_base [LOG_NAME_LEN ];
571+ char test_log_name [LOG_NAME_LEN ];
572+
569573struct test_data {
570574 int option_period ;
571575 int stat_cycle_num ;
@@ -581,26 +585,20 @@ server_wait(int pid, int period)
581585 for (i = 0 ; i < period ; i ++ ) {
582586 nanosleep (& req , & rem );
583587 if (kill (pid , 0 ) != 0 ) {
584- fprintf ( stderr , "Process %d terminated.\n" , pid );
588+ kpinfo ( "Process %d terminated.\n" , pid );
585589 return -1 ;
586590 }
587591 }
588592 return 0 ;
589593}
590594
591595static int
592- server_stress_test (int fd , int argc , char * argv [] )
596+ server_stress_test (int fd , int pid )
593597{
594- int pid ;
595598 int delay ;
596599 test_info .stat_cycle_num = 0 ;
597600 srand (time (NULL ));
598601
599- if (sscanf (argv [1 ], "%d" , & pid ) != 1 ) {
600- kperr ("Can't parse pid from %s\n" , argv [1 ]);
601- return -1 ;
602- }
603-
604602 while (1 ) {
605603 while (patch_user (storage_dir , pid , 0 , fd ) < 0 )
606604 if (server_wait (pid , 1 ) < 0 )
@@ -627,11 +625,41 @@ server_stress_test(int fd, int argc, char *argv[])
627625 return 0 ;
628626}
629627
628+ static int stress_test_log_init (int pid )
629+ {
630+ if (!strlen (test_log_base ))
631+ return 0 ;
632+ if (pid < 0 ) {
633+ if (snprintf (test_log_name , LOG_NAME_LEN , "%s-base" , test_log_base ) >= LOG_NAME_LEN ) {
634+ kperr ("Can't initialize log \'%s\'-<PID>\n" , test_log_base );
635+ return -1 ;
636+ }
637+ } else
638+ if (snprintf (test_log_name , LOG_NAME_LEN , "%s-%d" , test_log_base , pid ) >= LOG_NAME_LEN ) {
639+ kperr ("Can't initialize log \'%s\'-<PID>\n" , test_log_base );
640+ return -1 ;
641+ }
642+ if (log_file_init (test_log_name )) {
643+ kperr ("Can't open log file \'%s\'\n" , test_log_name );
644+ return -1 ;
645+ }
646+ return 0 ;
647+ }
648+
630649static int cmd_stress_test (int fd , int argc , char * argv [])
631650{
651+ int pid ;
652+ if (sscanf (argv [1 ], "%d" , & pid ) != 1 ) {
653+ kperr ("Can't parse pid from %s\n" , argv [1 ]);
654+ return -1 ;
655+ } else
656+ kpinfo ("Fork for pid=%d\n" , pid );
657+
632658 int child = fork ();
633659 if (child == 0 ) {
634- int rv = server_stress_test (fd , argc , argv );
660+ stress_test_log_init (pid );
661+ int rv = server_stress_test (fd , pid );
662+ log_file_free ();
635663 exit (rv );
636664 }
637665 close (fd );
@@ -640,7 +668,11 @@ static int cmd_stress_test(int fd, int argc, char *argv[])
640668
641669static int usage_stresstest ()
642670{
643- fprintf (stderr , "usage: libcare-stresstest PERIOD(ms, 0 - only patch) <UNIX socket> [STORAGE ROOT]\n" );
671+ fprintf (stderr , "usage: libcare-stresstest [args] PERIOD(ms, 0 - only patch) <UNIX socket> [STORAGE ROOT]\n" );
672+ fprintf (stderr , "\nOptions:\n" );
673+ fprintf (stderr , " -v - verbose mode\n" );
674+ fprintf (stderr , " -l <BASE> - log file name <BASE>-PID\n" );
675+ fprintf (stderr , " -h - this message\n" );
644676 return -1 ;
645677}
646678
@@ -828,6 +860,8 @@ cmd_server(int argc, char *argv[])
828860 }
829861
830862#ifdef STRESS_TEST
863+ if (stress_test_log_init (-1 ))
864+ exit (1 );
831865 if (sscanf (argv [0 ], "%d" , & test_info .option_period ) != 1 ) {
832866 kplogerror ("Can't parse period from %s\n" , argv [0 ]);
833867 }
@@ -954,13 +988,23 @@ int main(int argc, char *argv[])
954988{
955989 int opt ;
956990
957- while ((opt = getopt (argc , argv , "+vh " )) != EOF ) {
991+ while ((opt = getopt (argc , argv , "+vl:h " )) != EOF ) {
958992 switch (opt ) {
959993 case 'v' :
960994 log_level += 1 ;
961995 break ;
962996 case 'h' :
963997 return usage (NULL );
998+ case 'l' :
999+ #ifdef STRESS_TEST
1000+ if (strlen (optarg ) + 9 >= LOG_NAME_LEN ) { // 9 - max suffix length
1001+ usage_stresstest ();
1002+ kpfatal ("Can't initialize log\n" );
1003+ break ;
1004+ }
1005+ strncpy (test_log_base , optarg , LOG_NAME_LEN );
1006+ break ;
1007+ #endif
9641008 default :
9651009 return usage ("unknown option" );
9661010 }
0 commit comments