Skip to content

Commit

Permalink
Add libcare-stresstest logging to files
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Rashchupkin committed Feb 8, 2018
1 parent 26b0c6e commit ae6c097
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 11 deletions.
24 changes: 24 additions & 0 deletions src/kpatch_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

int log_level = LOG_INFO;
int log_indent;
FILE *log_file;

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

if (log_file) {
va_list vaf;
va_copy(vaf, va);
vfprintf(log_file, fmt, vaf);
fflush(log_file);
}
vfprintf(f, fmt, va);
}

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

exit(EXIT_FAILURE);
}

int log_file_init(char *fname)
{
if (!fname)
return -1;
log_file = fopen(fname, "a");
if (!log_file)
return -1;
return 0;
}

void log_file_free()
{
if (log_file)
fclose(log_file);
log_file = NULL;
}
3 changes: 3 additions & 0 deletions src/kpatch_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ void _kplogerror(const char *filename, int line, const char *fmt, ...)
#define LOG_DEBUG 3
#define LOG_TRACE 5

int log_file_init(char *fname);
void log_file_free();

#endif
66 changes: 55 additions & 11 deletions src/kpatch_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,10 @@ cmd_update(int argc, char *argv[])

#ifdef STRESS_TEST

#define LOG_NAME_LEN 1024
char test_log_base[LOG_NAME_LEN];
char test_log_name[LOG_NAME_LEN];

struct test_data {
int option_period;
int stat_cycle_num;
Expand All @@ -581,26 +585,20 @@ server_wait(int pid, int period)
for (i=0; i<period; i++) {
nanosleep(&req, &rem);
if (kill(pid, 0) != 0) {
fprintf(stderr, "Process %d terminated.\n", pid);
kpinfo("Process %d terminated.\n", pid);
return -1;
}
}
return 0;
}

static int
server_stress_test(int fd, int argc, char *argv[])
server_stress_test(int fd, int pid)
{
int pid;
int delay;
test_info.stat_cycle_num = 0;
srand(time(NULL));

if (sscanf(argv[1], "%d", &pid) != 1) {
kperr("Can't parse pid from %s\n", argv[1]);
return -1;
}

while (1) {
while (patch_user(storage_dir, pid, 0, fd) < 0)
if (server_wait(pid, 1) < 0)
Expand All @@ -627,11 +625,41 @@ server_stress_test(int fd, int argc, char *argv[])
return 0;
}

static int stress_test_log_init(int pid)
{
if (!strlen(test_log_base))
return 0;
if (pid < 0) {
if (snprintf(test_log_name, LOG_NAME_LEN, "%s-base", test_log_base) >= LOG_NAME_LEN) {
kperr("Can't initialize log \'%s\'-<PID>\n", test_log_base);
return -1;
}
} else
if (snprintf(test_log_name, LOG_NAME_LEN, "%s-%d", test_log_base, pid) >= LOG_NAME_LEN) {
kperr("Can't initialize log \'%s\'-<PID>\n", test_log_base);
return -1;
}
if (log_file_init(test_log_name)) {
kperr("Can't open log file \'%s\'\n", test_log_name);
return -1;
}
return 0;
}

static int cmd_stress_test(int fd, int argc, char *argv[])
{
int pid;
if (sscanf(argv[1], "%d", &pid) != 1) {
kperr("Can't parse pid from %s\n", argv[1]);
return -1;
} else
kpinfo("Fork for pid=%d\n", pid);

int child = fork();
if (child == 0) {
int rv = server_stress_test(fd, argc, argv);
stress_test_log_init(pid);
int rv = server_stress_test(fd, pid);
log_file_free();
exit(rv);
}
close(fd);
Expand All @@ -640,7 +668,11 @@ static int cmd_stress_test(int fd, int argc, char *argv[])

static int usage_stresstest()
{
fprintf(stderr, "usage: libcare-stresstest PERIOD(ms, 0 - only patch) <UNIX socket> [STORAGE ROOT]\n");
fprintf(stderr, "usage: libcare-stresstest [args] PERIOD(ms, 0 - only patch) <UNIX socket> [STORAGE ROOT]\n");
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, " -v - verbose mode\n");
fprintf(stderr, " -l <BASE> - log file name <BASE>-PID\n");
fprintf(stderr, " -h - this message\n");
return -1;
}

Expand Down Expand Up @@ -828,6 +860,8 @@ cmd_server(int argc, char *argv[])
}

#ifdef STRESS_TEST
if (stress_test_log_init(-1))
exit(1);
if (sscanf(argv[0], "%d", &test_info.option_period) != 1) {
kplogerror("Can't parse period from %s\n", argv[0]);
}
Expand Down Expand Up @@ -954,13 +988,23 @@ int main(int argc, char *argv[])
{
int opt;

while ((opt = getopt(argc, argv, "+vh")) != EOF) {
while ((opt = getopt(argc, argv, "+vl:h")) != EOF) {
switch (opt) {
case 'v':
log_level += 1;
break;
case 'h':
return usage(NULL);
case 'l':
#ifdef STRESS_TEST
if (strlen(optarg) + 9 >= LOG_NAME_LEN) { // 9 - max suffix length
usage_stresstest();
kpfatal("Can't initialize log\n");
break;
}
strncpy(test_log_base, optarg, LOG_NAME_LEN);
break;
#endif
default:
return usage("unknown option");
}
Expand Down

0 comments on commit ae6c097

Please sign in to comment.