11
11
#include <sys/socket.h>
12
12
#include <sys/un.h>
13
13
#include <sys/wait.h>
14
+ #include <sys/types.h>
14
15
15
16
#include <gelf.h>
16
17
#include <libunwind.h>
@@ -566,7 +567,11 @@ cmd_update(int argc, char *argv[])
566
567
567
568
#ifdef STRESS_TEST
568
569
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 {
570
575
int option_period ;
571
576
int stat_cycle_num ;
572
577
} test_info = { .option_period = 0 , .stat_cycle_num = 0 };
@@ -581,26 +586,20 @@ server_wait(int pid, int period)
581
586
for (i = 0 ; i < period ; i ++ ) {
582
587
nanosleep (& req , & rem );
583
588
if (kill (pid , 0 ) != 0 ) {
584
- fprintf ( stderr , "Process %d terminated.\n" , pid );
589
+ kpinfo ( "Process %d terminated.\n" , pid );
585
590
return -1 ;
586
591
}
587
592
}
588
593
return 0 ;
589
594
}
590
595
591
596
static int
592
- server_stress_test (int fd , int argc , char * argv [] )
597
+ server_stress_test (int fd , int pid )
593
598
{
594
- int pid ;
595
599
int delay ;
596
600
test_info .stat_cycle_num = 0 ;
597
601
srand (time (NULL ));
598
602
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
603
while (1 ) {
605
604
while (patch_user (storage_dir , pid , 0 , fd ) < 0 )
606
605
if (server_wait (pid , 1 ) < 0 )
@@ -627,11 +626,44 @@ server_stress_test(int fd, int argc, char *argv[])
627
626
return 0 ;
628
627
}
629
628
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
+
630
650
static int cmd_stress_test (int fd , int argc , char * argv [])
631
651
{
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
+
632
658
int child = fork ();
633
659
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 ();
635
667
exit (rv );
636
668
}
637
669
close (fd );
@@ -640,7 +672,11 @@ static int cmd_stress_test(int fd, int argc, char *argv[])
640
672
641
673
static int usage_stresstest ()
642
674
{
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" );
644
680
return -1 ;
645
681
}
646
682
@@ -828,9 +864,11 @@ cmd_server(int argc, char *argv[])
828
864
}
829
865
830
866
#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 )
832
870
kplogerror ("Can't parse period from %s\n" , argv [0 ]);
833
- }
871
+ kpinfo ( "Starting libcare-stresstest: period=%d\n" , test_info . option_period );
834
872
#endif
835
873
836
874
sfd = server_bind_socket (argv [1 ]);
@@ -954,13 +992,23 @@ int main(int argc, char *argv[])
954
992
{
955
993
int opt ;
956
994
957
- while ((opt = getopt (argc , argv , "+vh " )) != EOF ) {
995
+ while ((opt = getopt (argc , argv , "+vl:h " )) != EOF ) {
958
996
switch (opt ) {
959
997
case 'v' :
960
998
log_level += 1 ;
961
999
break ;
962
1000
case 'h' :
963
1001
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
964
1012
default :
965
1013
return usage ("unknown option" );
966
1014
}
0 commit comments