+ if(err < 0) {
+ perror("Error in writing to file");
+ goto write_error;
+ }
+#if 0
+ err = fsync(pair->trace);
+ if(err < 0) {
+ ret = errno;
+ perror("Error in writing to file");
+ goto write_error;
+ }
+#endif //0
+write_error:
+ err = ltt_buffer_put(ltt_buf, consumed_old);
+
+ if(err != 0) {
+ if(err == -EIO) {
+ 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;
+ }
+
+get_error:
+ return err;
+}
+
+/* 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)
+{
+ struct sigaction act;
+ int ret;
+ int fd_fac;
+ int fd_cpu;
+ char outfile_name[PATH_MAX];
+ char identifier_name[PATH_MAX];
+
+
+ traced_pid = l_traced_pid;
+ traced_thread = l_traced_thread;
+
+ 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;
+ sigemptyset(&(act.sa_mask));
+ sigaddset(&(act.sa_mask), SIGUSR1);
+ sigaction(SIGUSR1, &act, NULL);
+
+ act.sa_handler = handler_sigusr2;
+ act.sa_flags = 0;
+ sigemptyset(&(act.sa_mask));
+ sigaddset(&(act.sa_mask), SIGUSR2);
+ sigaction(SIGUSR2, &act, NULL);
+
+ act.sa_handler = handler_sigalarm;
+ act.sa_flags = 0;
+ sigemptyset(&(act.sa_mask));
+ 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 */
+ 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) {
+ pause();
+ 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);
+
+ do {
+ ret = read_subbuffer(&shared_trace_info->channel.facilities, fd_fac);
+ } while(ret == 0);
+ }
+
+ /* The parent thread is dead and we have finished with the buffer */
+
+ /* 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);
+ do {
+ ret = read_subbuffer(&shared_trace_info->channel.cpu, fd_cpu);
+ } 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);
+
+ munmap(shared_trace_info, sizeof(*shared_trace_info));
+
+ exit(0);
+}
+
+
+/* Reader-writer initialization */
+
+static enum ltt_process_role { LTT_ROLE_WRITER, LTT_ROLE_READER }
+ role = LTT_ROLE_WRITER;
+
+
+void ltt_rw_init(void)
+{
+ pid_t pid;
+ 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 = 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;
+ ltt_buffer_begin_callback(&shared_trace_info->channel.facilities,
+ ltt_get_timestamp(), 0);
+
+ 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;
+ ltt_buffer_begin_callback(&shared_trace_info->channel.cpu,
+ ltt_get_timestamp(), 0);
+
+ shared_trace_info->init = 1;
+
+ /* Disable signals */
+ ret = sigfillset(&set);
+ if(ret) {
+ printf("LTT Error in sigfillset\n");
+ }
+
+
+ ret = pthread_sigmask(SIG_BLOCK, &set, &oldset);
+ if(ret) {
+ printf("LTT Error in pthread_sigmask\n");
+ }
+
+ pid = fork();
+ if(pid > 0) {
+ /* Parent */
+ shared_trace_info->daemon_id = pid;
+ thread_trace_info = shared_trace_info;
+
+ /* Enable signals */
+ ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+ if(ret) {
+ printf("LTT Error in pthread_sigmask\n");
+ }
+ } else if(pid == 0) {
+ /* Child */
+ role = LTT_ROLE_READER;
+ 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("LTT Error in forking ltt-usertrace-fast");
+ }
+}
+
+static __thread struct _pthread_cleanup_buffer cleanup_buffer;
+
+void ltt_thread_init(void)
+{
+ _pthread_cleanup_push(&cleanup_buffer, ltt_usertrace_fast_cleanup, NULL);
+ ltt_rw_init();
+}
+
+void __attribute__((constructor)) __ltt_usertrace_fast_init(void)
+{
+ 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");
+ ltt_usertrace_fast_cleanup(NULL);
+ }