1111#include  <sys/socket.h> 
1212#include  <sys/un.h> 
1313#include  <sys/wait.h> 
14+ #include  <sys/types.h> 
1415
1516#include  <gelf.h> 
1617#include  <libunwind.h> 
@@ -566,7 +567,11 @@ cmd_update(int argc, char *argv[])
566567
567568#ifdef  STRESS_TEST 
568569
569- struct  test_data  {
570+ static  int  process_pid  =  -1 ;
571+ static  char  test_log_base [PATH_MAX - sizeof ("-4194304" )];
572+ static  char  test_log_name [PATH_MAX ];
573+ 
574+ static  struct  test_data  {
570575	int  option_period ;
571576	int  stat_cycle_num ;
572577} test_info  =  { .option_period  =  0 , .stat_cycle_num  =  0  };
@@ -581,26 +586,20 @@ server_wait(int pid, int period)
581586	for  (i = 0 ; i < period ; i ++ ) {
582587		nanosleep (& req , & rem );
583588		if  (kill (pid , 0 ) !=  0 ) {
584- 			fprintf ( stderr ,  "Process %d terminated.\n" , pid );
589+ 			kpinfo ( "Process %d terminated.\n" , pid );
585590			return  -1 ;
586591		}
587592	}
588593	return  0 ;
589594}
590595
591596static  int 
592- server_stress_test (int  fd , int  argc ,  char   * argv [] )
597+ server_stress_test (int  fd , int  pid )
593598{
594- 	int  pid ;
595599	int  delay ;
596600	test_info .stat_cycle_num  =  0 ;
597601	srand (time (NULL ));
598602
599- 	if  (sscanf (argv [1 ], "%d" , & pid ) !=  1 ) {
600- 		kperr ("Can't parse pid from %s\n" , argv [1 ]);
601- 		return  -1 ;
602- 	}
603- 
604603	while  (1 ) {
605604		while  (patch_user (storage_dir , pid , 0 , fd ) <  0 )
606605			if  (server_wait (pid , 1 ) <  0 )
@@ -627,11 +626,44 @@ server_stress_test(int fd, int argc, char *argv[])
627626	return  0 ;
628627}
629628
629+ static  int  stress_test_log_init (int  is_base_process )
630+ {
631+ 	if  (!strlen (test_log_base ))
632+ 		return  0 ;
633+ 	if  (is_base_process ) {
634+ 		if  (snprintf (test_log_name , PATH_MAX , "%s-base" , test_log_base ) >= PATH_MAX ) {
635+ 			kplogerror ("Can't initialize log \'%s\'-<PID>\n" , test_log_base );
636+ 			return  -1 ;
637+ 		}
638+ 	} else 
639+ 		if  (snprintf (test_log_name , PATH_MAX , "%s-%d" , test_log_base , getpid ()) >= PATH_MAX ) {
640+ 			kplogerror ("Can't initialize log \'%s\'-<PID>\n" , test_log_base );
641+ 			return  -1 ;
642+ 		}
643+ 	if  (log_file_init (test_log_name )) {
644+ 		kplogerror ("Can't open log file \'%s\'\n" , test_log_name );
645+ 		return  -1 ;
646+ 	}
647+ 	return  0 ;
648+ }
649+ 
630650static  int  cmd_stress_test (int  fd , int  argc , char  * argv [])
631651{
652+ 	if  (sscanf (argv [1 ], "%d" , & process_pid ) !=  1 ) {
653+ 		kplogerror ("Can't parse pid from %s\n" , argv [1 ]);
654+ 		return  -1 ;
655+ 	}
656+ 	kpinfo ("Spawning child to patch pid %d\n" , process_pid );
657+ 
632658	int  child  =  fork ();
633659	if  (child  ==  0 ) {
634- 		int  rv  =  server_stress_test (fd , argc , argv );
660+ 		log_file_free ();
661+ 		if  (stress_test_log_init (0 ))
662+ 			kpfatal ("Can't initialize log.\n" );
663+ 
664+ 		int  rv  =  server_stress_test (fd , process_pid );
665+ 
666+ 		log_file_free ();
635667		exit (rv );
636668	}
637669	close (fd );
@@ -640,7 +672,11 @@ static int cmd_stress_test(int fd, int argc, char *argv[])
640672
641673static  int  usage_stresstest ()
642674{
643- 	fprintf (stderr , "usage: libcare-stresstest PERIOD(ms, 0 - only patch) <UNIX socket> [STORAGE ROOT]\n" );
675+ 	fprintf (stderr , "usage: libcare-stresstest [args] PERIOD(ms, 0 - only patch) <UNIX socket> [STORAGE ROOT]\n" );
676+ 	fprintf (stderr , "\nOptions:\n" );
677+ 	fprintf (stderr , "  -v          - verbose mode\n" );
678+ 	fprintf (stderr , "  -l <BASE>   - log file name <BASE>-PID\n" );
679+ 	fprintf (stderr , "  -h          - this message\n" );
644680	return  -1 ;
645681}
646682
@@ -828,9 +864,11 @@ cmd_server(int argc, char *argv[])
828864	}
829865
830866#ifdef  STRESS_TEST 
831- 	if  (sscanf (argv [0 ], "%d" , & test_info .option_period ) !=  1 ) {
867+ 	if  (stress_test_log_init (1 ))
868+ 		kpfatal ("Can't initialize log.\n" );
869+ 	if  (sscanf (argv [0 ], "%d" , & test_info .option_period ) !=  1 )
832870		kplogerror ("Can't parse period from %s\n" , argv [0 ]);
833- 	} 
871+ 	kpinfo ( "Starting libcare-stresstest: period=%d\n" ,  test_info . option_period ); 
834872#endif 
835873
836874	sfd  =  server_bind_socket (argv [1 ]);
@@ -954,13 +992,23 @@ int main(int argc, char *argv[])
954992{
955993	int  opt ;
956994
957- 	while  ((opt  =  getopt (argc , argv , "+vh " )) !=  EOF ) {
995+ 	while  ((opt  =  getopt (argc , argv , "+vl:h " )) !=  EOF ) {
958996		switch  (opt ) {
959997			case  'v' :
960998				log_level  +=  1 ;
961999				break ;
9621000			case  'h' :
9631001				return  usage (NULL );
1002+ 			case  'l' :
1003+ #ifdef  STRESS_TEST 
1004+ 				strncpy (test_log_base , optarg , sizeof (test_log_base ));
1005+ 				if  (test_log_base [sizeof (test_log_base )- 1 ]) {
1006+ 					usage_stresstest ();
1007+ 					kpfatal ("Can't initialize log\n" );
1008+ 					break ;
1009+ 				}
1010+ 				break ;
1011+ #endif 
9641012			default :
9651013				return  usage ("unknown option" );
9661014		}
0 commit comments