Skip to content

Commit ae6c097

Browse files
author
Roman Rashchupkin
committed
Add libcare-stresstest logging to files
1 parent 26b0c6e commit ae6c097

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
lines changed

src/kpatch_log.c

+24
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88

99
int log_level = LOG_INFO;
1010
int log_indent;
11+
FILE *log_file;
1112

1213
static void __valog(int level, const char *prefix, const char *fmt, va_list va)
1314
{
1415
FILE *f = level <= LOG_WARN ? stderr : stdout;
1516
if (prefix)
1617
fprintf(f, "%s", prefix);
1718

19+
if (log_file) {
20+
va_list vaf;
21+
va_copy(vaf, va);
22+
vfprintf(log_file, fmt, vaf);
23+
fflush(log_file);
24+
}
1825
vfprintf(f, fmt, va);
1926
}
2027

@@ -100,3 +107,20 @@ void _kpfatalerror(const char *file, int line, const char *fmt, ...)
100107

101108
exit(EXIT_FAILURE);
102109
}
110+
111+
int log_file_init(char *fname)
112+
{
113+
if (!fname)
114+
return -1;
115+
log_file = fopen(fname, "a");
116+
if (!log_file)
117+
return -1;
118+
return 0;
119+
}
120+
121+
void log_file_free()
122+
{
123+
if (log_file)
124+
fclose(log_file);
125+
log_file = NULL;
126+
}

src/kpatch_log.h

+3
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ void _kplogerror(const char *filename, int line, const char *fmt, ...)
3333
#define LOG_DEBUG 3
3434
#define LOG_TRACE 5
3535

36+
int log_file_init(char *fname);
37+
void log_file_free();
38+
3639
#endif

src/kpatch_user.c

+55-11
Original file line numberDiff line numberDiff line change
@@ -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+
569573
struct 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

591595
static 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+
630649
static 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

641669
static 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

Comments
 (0)