X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=usertrace-fast%2Fltt-usertrace-fast.c;h=c809aff096f0fb359bdaf141c2b45d3477b103af;hb=5efa73ea0fa6562c46a966f56665022ccb018963;hp=2a4f039e710a48fb6e81e3b79993a804db225099;hpb=e8efa18da8bfbc203c0ed0eaf557dc280ff23daf;p=lttv.git diff --git a/usertrace-fast/ltt-usertrace-fast.c b/usertrace-fast/ltt-usertrace-fast.c index 2a4f039e..c809aff0 100644 --- a/usertrace-fast/ltt-usertrace-fast.c +++ b/usertrace-fast/ltt-usertrace-fast.c @@ -30,6 +30,13 @@ * dies, the data is not lost. * * Fast thread spawn : a pthread_create() is done by the application for each * new thread. + * + * We use a timer to check periodically if the parent died. I think it is less + * intrusive than a ptrace() on the parent, which would get every signal. The + * side effect of this is that we won't be notified if the parent does an + * exec(). In this case, we will just sit there until the parent exits. + * + * * Copyright 2006 Mathieu Desnoyers * */ @@ -47,6 +54,12 @@ #include #include #include +#include +#include +#include +#include + +#include //for get_cycles() #include "ltt-usertrace-fast.h" @@ -57,55 +70,70 @@ __thread struct ltt_trace_info *thread_trace_info = NULL; void ltt_usertrace_fast_buffer_switch(void) { - kill(thread_trace_info->daemon_id, SIGUSR1); + struct ltt_trace_info *tmp = thread_trace_info; + if(tmp) + kill(tmp->daemon_id, SIGUSR1); } +/* The cleanup should never be called from a signal handler */ static void ltt_usertrace_fast_cleanup(void *arg) { - kill(thread_trace_info->daemon_id, SIGUSR2); + struct ltt_trace_info *tmp = thread_trace_info; + if(tmp) { + thread_trace_info = NULL; + kill(tmp->daemon_id, SIGUSR2); + munmap(tmp, sizeof(*tmp)); + } } /* Reader (the disk dumper daemon) */ -static pid_t ppid = 0; +static pid_t traced_pid = 0; +static pthread_t traced_thread = 0; static int parent_exited = 0; /* signal handling */ static void handler_sigusr1(int signo) { - printf("Signal %d received : parent buffer switch.\n", signo); + printf("LTT Signal %d received : parent buffer switch.\n", signo); } static void handler_sigusr2(int signo) { - printf("Signal %d received : parent exited.\n", signo); + printf("LTT Signal %d received : parent exited.\n", signo); parent_exited = 1; } static void handler_sigalarm(int signo) { - printf("Signal %d received\n", signo); + printf("LTT Signal %d received\n", signo); - if(getppid() != ppid) { + if(getppid() != traced_pid) { /* Parent died */ - printf("Parent %lu died, cleaning up\n", ppid); - ppid = 0; + printf("LTT Parent %lu died, cleaning up\n", traced_pid); + traced_pid = 0; } alarm(3); } -/* This function is called by ltt_thread_init which has signals blocked */ +/* This function is called by ltt_rw_init which has signals blocked */ static void ltt_usertrace_fast_daemon(struct ltt_trace_info *shared_trace_info, - sigset_t oldset) + sigset_t oldset, pid_t l_traced_pid, pthread_t l_traced_thread) { struct sigaction act; int ret; + int fd_fac; + int fd_cpu; + char outfile_name[PATH_MAX]; + char identifier_name[PATH_MAX]; + - ppid = getppid(); + traced_pid = l_traced_pid; + traced_thread = l_traced_thread; - printf("ltt_usertrace_fast_daemon : init is %d, pid is %lu\n", - shared_trace_info->init, getpid()); + printf("LTT ltt_usertrace_fast_daemon : init is %d, pid is %lu, traced_pid is %lu\n", + shared_trace_info->init, getpid(), traced_pid); act.sa_handler = handler_sigusr1; act.sa_flags = 0; @@ -128,23 +156,46 @@ static void ltt_usertrace_fast_daemon(struct ltt_trace_info *shared_trace_info, /* Enable signals */ ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL); if(ret) { - printf("Error in pthread_sigmask\n"); + printf("LTT Error in pthread_sigmask\n"); } alarm(3); + /* Open output files */ + umask(00000); + ret = mkdir(LTT_USERTRACE_ROOT, 0777); + if(ret < 0 && errno != EEXIST) { + perror("LTT Error in creating output (mkdir)"); + exit(-1); + } + ret = chdir(LTT_USERTRACE_ROOT); + if(ret < 0) { + perror("LTT Error in creating output (chdir)"); + exit(-1); + } + snprintf(identifier_name, PATH_MAX-1, "%lu.%lu.%llu", + traced_pid, traced_thread, get_cycles()); + snprintf(outfile_name, PATH_MAX-1, "facilities-%s", identifier_name); + fd_fac = creat(outfile_name, 0644); + + snprintf(outfile_name, PATH_MAX-1, "cpu-%s", identifier_name); + fd_cpu = creat(outfile_name, 0644); + + while(1) { - sleep(1); pause(); - if(ppid == 0) break; /* parent died */ + if(traced_pid == 0) break; /* parent died */ if(parent_exited) break; - printf("Doing a buffer switch read. pid is : %lu\n", getpid()); + printf("LTT Doing a buffer switch read. pid is : %lu\n", getpid()); //printf("Test parent. pid is : %lu, ppid is %lu\n", getpid(), getppid()); } /* Buffer force switch (flush) */ //TODO + close(fd_fac); + close(fd_cpu); + /* The parent thread is dead and we have finished with the buffer */ munmap(shared_trace_info, sizeof(*shared_trace_info)); @@ -164,44 +215,48 @@ void ltt_rw_init(void) struct ltt_trace_info *shared_trace_info; int ret; sigset_t set, oldset; + pid_t l_traced_pid = getpid(); + pthread_t l_traced_thread = pthread_self(); /* parent : create the shared memory map */ - shared_trace_info = thread_trace_info = mmap(0, sizeof(*thread_trace_info), + shared_trace_info = mmap(0, sizeof(*thread_trace_info), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0); - memset(shared_trace_info, 0, sizeof(*thread_trace_info)); - thread_trace_info->init = 1; + memset(shared_trace_info, 0, sizeof(*shared_trace_info)); + shared_trace_info->init = 1; /* Disable signals */ ret = sigfillset(&set); if(ret) { - printf("Error in sigfillset\n"); + printf("LTT Error in sigfillset\n"); } ret = pthread_sigmask(SIG_BLOCK, &set, &oldset); if(ret) { - printf("Error in pthread_sigmask\n"); + printf("LTT Error in pthread_sigmask\n"); } - + pid = fork(); if(pid > 0) { /* Parent */ - thread_trace_info->daemon_id = pid; + shared_trace_info->daemon_id = pid; + thread_trace_info = shared_trace_info; /* Enable signals */ ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL); if(ret) { - printf("Error in pthread_sigmask\n"); + printf("LTT Error in pthread_sigmask\n"); } } else if(pid == 0) { /* Child */ role = LTT_ROLE_READER; - ltt_usertrace_fast_daemon(shared_trace_info, oldset); + ltt_usertrace_fast_daemon(shared_trace_info, oldset, l_traced_pid, + l_traced_thread); /* Should never return */ exit(-1); } else if(pid < 0) { /* fork error */ - perror("Error in forking ltt-usertrace-fast"); + perror("LTT Error in forking ltt-usertrace-fast"); } }