diff options
-rw-r--r-- | src/rt-app.c | 36 | ||||
-rw-r--r-- | src/rt-app_utils.c | 19 | ||||
-rw-r--r-- | src/rt-app_utils.h | 7 |
3 files changed, 33 insertions, 29 deletions
diff --git a/src/rt-app.c b/src/rt-app.c index 1b694dc..e7693a2 100644 --- a/src/rt-app.c +++ b/src/rt-app.c @@ -25,6 +25,7 @@ static int errno; static int continue_running; static pthread_t *threads; static int nthreads; +rtapp_options_t opts; static ftrace_data_t ft_data = { .debugfs = "/debug", .trace_fd = -1, @@ -94,6 +95,13 @@ shutdown(int sig) { pthread_join(threads[i], NULL); } + if (opts.ftrace) { + log_notice("stopping ftrace"); + log_ftrace(ft_data.marker_fd, "main ends\n"); + log_ftrace(ft_data.trace_fd, "0"); + close(ft_data.trace_fd); + close(ft_data.marker_fd); + } exit(EXIT_SUCCESS); } @@ -355,7 +363,6 @@ int main(int argc, char* argv[]) thread_data_t *tdata; char tmp[PATH_LENGTH]; - rtapp_options_t opts; parse_command_line(argc, argv, &opts); nthreads = opts.nthreads; @@ -386,16 +393,8 @@ int main(int argc, char* argv[]) exit(EXIT_FAILURE); } - res = ftrace_write(ft_data.trace_fd, "1"); - if (res < 0) { - log_error("Cannot write to trace_fd"); - exit(EXIT_FAILURE); - } - res = ftrace_write(ft_data.marker_fd, "main creates threads\n"); - if (res < 0) { - log_error("Cannot write to marker_fd"); - exit(EXIT_FAILURE); - } + log_ftrace(ft_data.trace_fd, "1"); + log_ftrace(ft_data.marker_fd, "main creates threads\n"); } continue_running = 1; @@ -527,16 +526,11 @@ int main(int argc, char* argv[]) } if (opts.ftrace) { - res = ftrace_write(ft_data.trace_fd, "0"); - if (res < 0) { - log_error("Cannot write to trace_fd"); - exit(EXIT_FAILURE); - } - res = ftrace_write(ft_data.marker_fd, "main ends\n"); - if (res < 0) { - log_error("Cannot write to marker_fd"); - exit(EXIT_FAILURE); - } + log_notice("stopping ftrace"); + log_ftrace(ft_data.marker_fd, "main ends\n"); + log_ftrace(ft_data.trace_fd, "0"); + close(ft_data.trace_fd); + close(ft_data.marker_fd); } exit(EXIT_SUCCESS); diff --git a/src/rt-app_utils.c b/src/rt-app_utils.c index 31e418c..174a984 100644 --- a/src/rt-app_utils.c +++ b/src/rt-app_utils.c @@ -202,17 +202,21 @@ policy_to_string(policy_t policy, char *policy_name) #endif -int ftrace_write(int mark_fd, const char *fmt, ...) +void ftrace_write(int mark_fd, const char *fmt, ...) { va_list ap; int n, size = BUF_SIZE; char *tmp, *ntmp; - if (mark_fd < 0) - return -1; + if (mark_fd < 0) { + log_error("invalid mark_fd"); + exit(EXIT_FAILURE); + } - if ((tmp = malloc(size)) == NULL) - return -1; + if ((tmp = malloc(size)) == NULL) { + log_error("Cannot allocate ftrace buffer"); + exit(EXIT_FAILURE); + } while(1) { /* Try to print in the allocated space */ @@ -223,7 +227,7 @@ int ftrace_write(int mark_fd, const char *fmt, ...) if (n > -1 && n < size) { write(mark_fd, tmp, n); free(tmp); - return 1; + return; } /* Else try again with more space */ if (n > -1) /* glibc 2.1 */ @@ -232,7 +236,8 @@ int ftrace_write(int mark_fd, const char *fmt, ...) size *= 2; if ((ntmp = realloc(tmp, size)) == NULL) { free(tmp); - return -1; + log_error("Cannot reallocate ftrace buffer"); + exit(EXIT_FAILURE); } else { tmp = ntmp; } diff --git a/src/rt-app_utils.h b/src/rt-app_utils.h index 8e9caa4..eb1ab2e 100644 --- a/src/rt-app_utils.h +++ b/src/rt-app_utils.h @@ -51,6 +51,11 @@ do { \ } \ } while (0); +#define log_ftrace(mark_fd, msg, args...) \ +do { \ + ftrace_write(mark_fd, msg, ##args); \ +} while (0); + #define log_notice(msg, args...) \ do { \ rtapp_log_to(stderr, LOG_LEVEL_NOTICE, "<notice> ", msg, ##args); \ @@ -120,7 +125,7 @@ string_to_policy(const char *policy_name, policy_t *policy); int policy_to_string(policy_t policy, char *policy_name); -int +void ftrace_write(int mark_fd, const char *fmt, ...); #endif // _TIMESPEC_UTILS_H_ |