@@ -566,6 +566,10 @@ cmd_update(int argc, char *argv[])
566
566
567
567
#ifdef STRESS_TEST
568
568
569
+ #define LOG_NAME_LEN 1024
570
+ char test_log_base [LOG_NAME_LEN ];
571
+ char test_log_name [LOG_NAME_LEN ];
572
+
569
573
struct test_data {
570
574
int option_period ;
571
575
int stat_cycle_num ;
@@ -581,26 +585,20 @@ server_wait(int pid, int period)
581
585
for (i = 0 ; i < period ; i ++ ) {
582
586
nanosleep (& req , & rem );
583
587
if (kill (pid , 0 ) != 0 ) {
584
- fprintf ( stderr , "Process %d terminated.\n" , pid );
588
+ kpinfo ( "Process %d terminated.\n" , pid );
585
589
return -1 ;
586
590
}
587
591
}
588
592
return 0 ;
589
593
}
590
594
591
595
static int
592
- server_stress_test (int fd , int argc , char * argv [] )
596
+ server_stress_test (int fd , int pid )
593
597
{
594
- int pid ;
595
598
int delay ;
596
599
test_info .stat_cycle_num = 0 ;
597
600
srand (time (NULL ));
598
601
599
- if (sscanf (argv [1 ], "%d" , & pid ) != 1 ) {
600
- kperr ("Can't parse pid from %s\n" , argv [1 ]);
601
- return -1 ;
602
- }
603
-
604
602
while (1 ) {
605
603
while (patch_user (storage_dir , pid , 0 , fd ) < 0 )
606
604
if (server_wait (pid , 1 ) < 0 )
@@ -627,11 +625,41 @@ server_stress_test(int fd, int argc, char *argv[])
627
625
return 0 ;
628
626
}
629
627
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
+
630
649
static int cmd_stress_test (int fd , int argc , char * argv [])
631
650
{
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
+
632
658
int child = fork ();
633
659
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 ();
635
663
exit (rv );
636
664
}
637
665
close (fd );
@@ -640,7 +668,11 @@ static int cmd_stress_test(int fd, int argc, char *argv[])
640
668
641
669
static int usage_stresstest ()
642
670
{
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" );
644
676
return -1 ;
645
677
}
646
678
@@ -828,6 +860,8 @@ cmd_server(int argc, char *argv[])
828
860
}
829
861
830
862
#ifdef STRESS_TEST
863
+ if (stress_test_log_init (-1 ))
864
+ exit (1 );
831
865
if (sscanf (argv [0 ], "%d" , & test_info .option_period ) != 1 ) {
832
866
kplogerror ("Can't parse period from %s\n" , argv [0 ]);
833
867
}
@@ -954,13 +988,23 @@ int main(int argc, char *argv[])
954
988
{
955
989
int opt ;
956
990
957
- while ((opt = getopt (argc , argv , "+vh " )) != EOF ) {
991
+ while ((opt = getopt (argc , argv , "+vl:h " )) != EOF ) {
958
992
switch (opt ) {
959
993
case 'v' :
960
994
log_level += 1 ;
961
995
break ;
962
996
case 'h' :
963
997
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
964
1008
default :
965
1009
return usage ("unknown option" );
966
1010
}
0 commit comments