*
*/
+#define inline inline __attribute__((always_inline))
+
#define _GNU_SOURCE
+#define LTT_TRACE
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/time.h>
+#include <errno.h>
#include <asm/atomic.h>
#include <asm/timex.h> //for get_cycles()
-#include "ltt-usertrace-fast.h"
+_syscall0(pid_t,gettid)
+
+#include <ltt/ltt-usertrace-fast.h>
+#ifdef LTT_SHOW_DEBUG
+#define dbg_printf(...) dbg_printf(__VA_ARGS__)
+#else
+#define dbg_printf(...)
+#endif //LTT_SHOW_DEBUG
+
+
enum force_switch_mode { FORCE_ACTIVE, FORCE_FLUSH };
/* Writer (the traced application) */
/* Reader (the disk dumper daemon) */
static pid_t traced_pid = 0;
-static pthread_t traced_thread = 0;
+static pid_t traced_tid = 0;
static int parent_exited = 0;
/* signal handling */
static void handler_sigusr1(int signo)
{
- printf("LTT Signal %d received : parent buffer switch.\n", signo);
+ dbg_printf("LTT Signal %d received : parent buffer switch.\n", signo);
}
static void handler_sigusr2(int signo)
{
- printf("LTT Signal %d received : parent exited.\n", signo);
+ dbg_printf("LTT Signal %d received : parent exited.\n", signo);
parent_exited = 1;
}
static void handler_sigalarm(int signo)
{
- printf("LTT Signal %d received\n", signo);
+ dbg_printf("LTT Signal %d received\n", signo);
if(getppid() != traced_pid) {
/* Parent died */
- printf("LTT Parent %lu died, cleaning up\n", traced_pid);
+ dbg_printf("LTT Parent %lu died, cleaning up\n", traced_pid);
traced_pid = 0;
}
alarm(3);
* It can also happen if this is a buffer we never got. */
return -EIO;
} else {
- if(atomic_read(<t_buf->full) == 1) {
- /* tell the client that buffer is now unfull */
- ret = futex((unsigned long)<t_buf->full,
- FUTEX_WAKE, 1, 0, 0, 0);
- if(ret != 1) {
- printf("LTT warning : race condition : writer not waiting or too many writers\n");
- }
- atomic_set(<t_buf->full, 0);
+ ret = sem_post(<t_buf->writer_sem);
+ if(ret < 0) {
+ printf("error in sem_post");
}
}
}
-/* In the writer :
- *
- * if(buffer full condition) {
- * put myself in the wait queue
- * ltt_buf->full = 1;
- * schedule
- * }
- *{
- if(buffer_is_full) {
- atomic_set(<t_buf->full, 1);
- ret = do_futex((unsigned long)<t_buf->full, 1, 0, 0, 0);
- }
-}
-
- */
-
static int read_subbuffer(struct ltt_buf *ltt_buf, int fd)
{
unsigned int consumed_old;
int err;
- printf("LTT read buffer\n");
+ dbg_printf("LTT read buffer\n");
err = ltt_buffer_get(ltt_buf, &consumed_old);
- if(err != -EAGAIN && err != 0) {
- printf("LTT Reserving sub buffer failed\n");
+ if(err != 0) {
+ if(err != -EAGAIN) dbg_printf("LTT Reserving sub buffer failed\n");
goto get_error;
}
if(err != 0) {
if(err == -EIO) {
- printf("Reader has been pushed by the writer, last subbuffer corrupted.\n");
+ dbg_printf("Reader has been pushed by the writer, last subbuffer corrupted.\n");
/* FIXME : we may delete the last written buffer if we wish. */
}
goto get_error;
/* 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, pid_t l_traced_pid, pthread_t l_traced_thread)
+ sigset_t oldset, pid_t l_traced_pid, pthread_t l_traced_tid)
{
+ sigset_t set;
struct sigaction act;
int ret;
- int fd_fac;
- int fd_cpu;
+ int fd_process;
char outfile_name[PATH_MAX];
char identifier_name[PATH_MAX];
traced_pid = l_traced_pid;
- traced_thread = l_traced_thread;
+ traced_tid = l_traced_tid;
- printf("LTT ltt_usertrace_fast_daemon : init is %d, pid is %lu, traced_pid is %lu\n",
- shared_trace_info->init, getpid(), traced_pid);
+ dbg_printf("LTT ltt_usertrace_fast_daemon : init is %d, pid is %lu, traced_pid is %lu, traced_tid is %lu\n",
+ shared_trace_info->init, getpid(), traced_pid, traced_tid);
act.sa_handler = handler_sigusr1;
act.sa_flags = 0;
sigaddset(&(act.sa_mask), SIGALRM);
sigaction(SIGALRM, &act, NULL);
- /* Enable signals */
- ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
- if(ret) {
- printf("LTT Error in pthread_sigmask\n");
- }
-
alarm(3);
/* Open output files */
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);
-
+ traced_tid, traced_pid, get_cycles());
+ snprintf(outfile_name, PATH_MAX-1, "process-%s", identifier_name);
+#ifndef LTT_NULL_OUTPUT_TEST
+ fd_process = creat(outfile_name, 0644);
+#else
+ /* NULL test */
+ ret = symlink("/dev/null", outfile_name);
+ if(ret < 0) {
+ perror("error in symlink");
+ }
+ fd_process = open(outfile_name, O_WRONLY);
+ if(fd_process < 0) {
+ perror("Error in open");
+ }
+#endif //LTT_NULL_OUTPUT_TEST
while(1) {
- pause();
+ ret = sigsuspend(&oldset);
+ if(ret != -1) {
+ perror("LTT Error in sigsuspend\n");
+ }
+
if(traced_pid == 0) break; /* parent died */
if(parent_exited) break;
- printf("LTT Doing a buffer switch read. pid is : %lu\n", getpid());
-
- do {
- ret = read_subbuffer(&shared_trace_info->channel.cpu, fd_cpu);
- } while(ret == 0);
+ dbg_printf("LTT Doing a buffer switch read. pid is : %lu\n", getpid());
do {
- ret = read_subbuffer(&shared_trace_info->channel.facilities, fd_fac);
+ ret = read_subbuffer(&shared_trace_info->channel.process, fd_process);
} while(ret == 0);
}
/* Buffer force switch (flush). Using FLUSH instead of ACTIVE because we know
* there is no writer. */
- flush_buffer(&shared_trace_info->channel.cpu, FORCE_FLUSH);
+ flush_buffer(&shared_trace_info->channel.process, FORCE_FLUSH);
do {
- ret = read_subbuffer(&shared_trace_info->channel.cpu, fd_cpu);
+ ret = read_subbuffer(&shared_trace_info->channel.process, fd_process);
} while(ret == 0);
- flush_buffer(&shared_trace_info->channel.facilities, FORCE_FLUSH);
- do {
- ret = read_subbuffer(&shared_trace_info->channel.facilities, fd_fac);
- } while(ret == 0);
-
- close(fd_fac);
- close(fd_cpu);
+ close(fd_process);
+ ret = sem_destroy(&shared_trace_info->channel.process.writer_sem);
+ if(ret < 0) {
+ perror("error in sem_destroy");
+ }
munmap(shared_trace_info, sizeof(*shared_trace_info));
exit(0);
int ret;
sigset_t set, oldset;
pid_t l_traced_pid = getpid();
- pthread_t l_traced_thread = pthread_self();
+ pid_t l_traced_tid = gettid();
/* parent : create the shared memory map */
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(*shared_trace_info));
- /* Tricky semaphore : is in a shared memory space, so it's ok for a fast
- * mutex (futex). */
- atomic_set(&shared_trace_info->channel.facilities.full, 0);
- shared_trace_info->channel.facilities.alloc_size = LTT_BUF_SIZE_FACILITIES;
- shared_trace_info->channel.facilities.subbuf_size = LTT_SUBBUF_SIZE_FACILITIES;
- shared_trace_info->channel.facilities.start =
- shared_trace_info->channel.facilities_buf;
-
- atomic_set(&shared_trace_info->channel.cpu.full, 0);
- shared_trace_info->channel.cpu.alloc_size = LTT_BUF_SIZE_CPU;
- shared_trace_info->channel.cpu.subbuf_size = LTT_SUBBUF_SIZE_CPU;
- shared_trace_info->channel.cpu.start = shared_trace_info->channel.cpu_buf;
+ shared_trace_info->init=0;
+ shared_trace_info->filter=0;
+ shared_trace_info->daemon_id=0;
+ shared_trace_info->nesting=0;
+ memset(&shared_trace_info->channel.process, 0,
+ sizeof(shared_trace_info->channel.process));
+ //Need NPTL!
+ ret = sem_init(&shared_trace_info->channel.process.writer_sem, 1,
+ LTT_N_SUBBUFS);
+ if(ret < 0) {
+ perror("error in sem_init");
+ }
+ shared_trace_info->channel.process.alloc_size = LTT_BUF_SIZE_PROCESS;
+ shared_trace_info->channel.process.subbuf_size = LTT_SUBBUF_SIZE_PROCESS;
+ shared_trace_info->channel.process.start =
+ shared_trace_info->channel.process_buf;
+ ltt_buffer_begin_callback(&shared_trace_info->channel.process,
+ ltt_get_timestamp(), 0);
+
shared_trace_info->init = 1;
/* Disable signals */
ret = sigfillset(&set);
if(ret) {
- printf("LTT Error in sigfillset\n");
+ dbg_printf("LTT Error in sigfillset\n");
}
ret = pthread_sigmask(SIG_BLOCK, &set, &oldset);
if(ret) {
- printf("LTT Error in pthread_sigmask\n");
+ dbg_printf("LTT Error in pthread_sigmask\n");
}
pid = fork();
/* Enable signals */
ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
if(ret) {
- printf("LTT Error in pthread_sigmask\n");
+ dbg_printf("LTT Error in pthread_sigmask\n");
}
} else if(pid == 0) {
+ pid_t sid;
/* Child */
role = LTT_ROLE_READER;
+ sid = setsid();
+ //Not a good idea to renice, unless futex wait eventually implement
+ //priority inheritence.
+ //ret = nice(1);
+ //if(ret < 0) {
+ // perror("Error in nice");
+ //}
+ if(sid < 0) {
+ perror("Error setting sid");
+ }
ltt_usertrace_fast_daemon(shared_trace_info, oldset, l_traced_pid,
- l_traced_thread);
+ l_traced_tid);
/* Should never return */
exit(-1);
} else if(pid < 0) {
void __attribute__((constructor)) __ltt_usertrace_fast_init(void)
{
- printf("LTT usertrace-fast init\n");
+ dbg_printf("LTT usertrace-fast init\n");
ltt_rw_init();
}
void __attribute__((destructor)) __ltt_usertrace_fast_fini(void)
{
if(role == LTT_ROLE_WRITER) {
- printf("LTT usertrace-fast fini\n");
+ dbg_printf("LTT usertrace-fast fini\n");
ltt_usertrace_fast_cleanup(NULL);
}
}