X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=trunk%2Fltt-control%2Flttd%2Flttd.c;h=f21c411eb8940de479fb62980917c6880fae7e99;hb=42e99028d61b9ebf5d2a250fc99db0ec4941204a;hp=b00142639098c859b17e22d1c8f94c2db8f39312;hpb=f64efc67a081bb9013f5939ec9e4ca514cd94d0f;p=ltt-control.git diff --git a/trunk/ltt-control/lttd/lttd.c b/trunk/ltt-control/lttd/lttd.c index b001426..f21c411 100644 --- a/trunk/ltt-control/lttd/lttd.c +++ b/trunk/ltt-control/lttd/lttd.c @@ -51,7 +51,8 @@ #define RELAY_GET_SUBBUF_SIZE _IOR(0xF5, 0x03,__u32) #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14) -#include +#include +#if 0 /* should now be provided by libc. */ /* From the inotify-tools 2.6 package */ static inline int inotify_init (void) { @@ -67,6 +68,7 @@ static inline int inotify_rm_watch (int fd, __u32 wd) { return syscall (__NR_inotify_rm_watch, fd, wd); } +#endif //0 #define HAS_INOTIFY #else static inline int inotify_init (void) @@ -118,7 +120,7 @@ struct inotify_watch_array { int num; }; - +static __thread int thread_pipe[2]; struct channel_trace_fd fd_pairs = { NULL, 0 }; int inotify_fd = -1; @@ -136,14 +138,21 @@ static unsigned long num_threads = 1; volatile static int quit_program = 0; /* For signal handler */ static int dump_flight_only = 0; static int dump_normal_only = 0; +static int verbose_mode = 0; + +#define printf_verbose(fmt, args...) \ + do { \ + if (verbose_mode) \ + printf(fmt, ##args); \ + } while (0) /* Args : * * -t directory Directory name of the trace to write to. Will be created. * -c directory Root directory of the debugfs trace channels. * -d Run in background (daemon). - * -a Trace append mode. - * -s Send SIGUSR1 to parent when ready for IO. + * -a Trace append mode. + * -s Send SIGUSR1 to parent when ready for IO. */ void show_arguments(void) { @@ -157,6 +166,7 @@ void show_arguments(void) printf("-N Number of threads to start.\n"); printf("-f Dump only flight recorder channels.\n"); printf("-n Dump only normal channels.\n"); + printf("-v Verbose mode.\n"); printf("\n"); } @@ -214,6 +224,9 @@ int parse_arguments(int argc, char **argv) case 'n': dump_normal_only = 1; break; + case 'v': + verbose_mode = 1; + break; default: printf("Invalid argument '%s'.\n", argv[argn]); printf("\n"); @@ -271,16 +284,18 @@ int open_buffer_file(char *filename, char *path_channel, char *path_trace, if(strncmp(filename, "flight-", sizeof("flight-")-1) != 0) { if(dump_flight_only) { - printf("Skipping normal channel %s\n", path_channel); + printf_verbose("Skipping normal channel %s\n", + path_channel); return 0; } } else { if(dump_normal_only) { - printf("Skipping flight channel %s\n", path_channel); + printf_verbose("Skipping flight channel %s\n", + path_channel); return 0; } } - printf("Opening file.\n"); + printf_verbose("Opening file.\n"); fd_pairs->pair = realloc(fd_pairs->pair, ++fd_pairs->num_pairs * sizeof(struct fd_pair)); @@ -297,15 +312,20 @@ int open_buffer_file(char *filename, char *path_channel, char *path_trace, ret = stat(path_trace, &stat_buf); if(ret == 0) { if(append_mode) { - printf("Appending to file %s as requested\n", path_trace); + printf_verbose("Appending to file %s as requested\n", + path_trace); fd_pairs->pair[fd_pairs->num_pairs-1].trace = - open(path_trace, O_WRONLY|O_APPEND, + open(path_trace, O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO); - if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { perror(path_trace); } + ret = lseek(fd_pairs->pair[fd_pairs->num_pairs-1].trace, + 0, SEEK_END); + if (ret < 0) { + perror(path_trace); + } } else { printf("File %s exists, cannot open. Try append mode.\n", path_trace); open_ret = -1; @@ -347,7 +367,7 @@ int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, goto end; } - printf("Creating trace subdirectory %s\n", subtrace_name); + printf_verbose("Creating trace subdirectory %s\n", subtrace_name); ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO); if(ret == -1) { if(errno != EEXIST) { @@ -373,11 +393,12 @@ int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, iwatch_array->elem = realloc(iwatch_array->elem, ++iwatch_array->num * sizeof(struct inotify_watch)); - printf("Adding inotify for channel %s\n", path_channel); + printf_verbose("Adding inotify for channel %s\n", path_channel); iwatch_array->elem[iwatch_array->num-1].wd = inotify_add_watch(*inotify_fd, path_channel, IN_CREATE); strcpy(iwatch_array->elem[iwatch_array->num-1].path_channel, path_channel); strcpy(iwatch_array->elem[iwatch_array->num-1].path_trace, path_trace); - printf("Added inotify for channel %s, wd %u\n", iwatch_array->elem[iwatch_array->num-1].path_channel, + printf_verbose("Added inotify for channel %s, wd %u\n", + iwatch_array->elem[iwatch_array->num-1].path_channel, iwatch_array->elem[iwatch_array->num-1].wd); #endif @@ -394,11 +415,11 @@ int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, continue; } - printf("Channel file : %s\n", path_channel); + printf_verbose("Channel file : %s\n", path_channel); if(S_ISDIR(stat_buf.st_mode)) { - printf("Entering channel subdirectory...\n"); + printf_verbose("Entering channel subdirectory...\n"); ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs, inotify_fd, iwatch_array); if(ret < 0) continue; @@ -419,19 +440,21 @@ end: int read_subbuffer(struct fd_pair *pair) { - unsigned int consumed_old; - int err, ret=0; + unsigned int consumed_old; + int err; + long ret; + unsigned long len; + off_t offset; - err = ioctl(pair->channel, RELAY_GET_SUBBUF, - &consumed_old); - printf("cookie : %u\n", consumed_old); + err = ioctl(pair->channel, RELAY_GET_SUBBUF, &consumed_old); + printf_verbose("cookie : %u\n", consumed_old); if(err != 0) { ret = errno; perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)"); goto get_error; } - +#if 0 err = TEMP_FAILURE_RETRY(write(pair->trace, pair->mmap + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)), @@ -442,6 +465,29 @@ int read_subbuffer(struct fd_pair *pair) perror("Error in writing to file"); goto write_error; } +#endif //0 + len = pair->subbuf_size; + offset = 0; + while (len > 0) { + printf_verbose("splice chan to pipe offset %lu\n", + (unsigned long)offset); + ret = splice(pair->channel, &offset, thread_pipe[1], NULL, + len, SPLICE_F_MOVE); + printf_verbose("splice chan to pipe ret %ld\n", ret); + if (ret < 0) { + perror("Error in relay splice"); + goto write_error; + } + ret = splice(thread_pipe[0], NULL, pair->trace, NULL, + ret, SPLICE_F_MOVE); + printf_verbose("splice pipe to file %ld\n", ret); + if (ret < 0) { + perror("Error in file splice"); + goto write_error; + } + len -= ret; + } + #if 0 err = fsync(pair->trace); if(err < 0) { @@ -451,6 +497,7 @@ int read_subbuffer(struct fd_pair *pair) } #endif //0 write_error: + ret = 0; err = ioctl(pair->channel, RELAY_PUT_SUBBUF, &consumed_old); if(err != 0) { ret = errno; @@ -468,7 +515,6 @@ get_error: } - int map_channels(struct channel_trace_fd *fd_pairs, int idx_begin, int idx_end) { @@ -504,6 +550,7 @@ int map_channels(struct channel_trace_fd *fd_pairs, } } +#if 0 /* Mmap each FD */ for(i=idx_begin;ipair[i]; @@ -533,13 +580,11 @@ munmap: ret |= err_ret; } +#endif //0 end: return ret; - - } - int unmap_channels(struct channel_trace_fd *fd_pairs) { int j; @@ -550,11 +595,13 @@ int unmap_channels(struct channel_trace_fd *fd_pairs) struct fd_pair *pair = &fd_pairs->pair[j]; int err_ret; +#if 0 err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); if(err_ret != 0) { perror("Error in munmap"); } ret |= err_ret; +#endif //0 err_ret = pthread_mutex_destroy(&pair->mutex); if(err_ret != 0) { perror("Error in mutex destroy"); @@ -600,9 +647,11 @@ int read_inotify(int inotify_fd, for(i=0; inum; i++) { if(iwatch_array->elem[i].wd == ievent->wd && ievent->mask == IN_CREATE) { - printf("inotify wd %u event mask : %u for %s%s\n", + printf_verbose( + "inotify wd %u event mask : %u for %s%s\n", ievent->wd, ievent->mask, - iwatch_array->elem[i].path_channel, ievent->name); + iwatch_array->elem[i].path_channel, + ievent->name); old_num = fd_pairs->num_pairs; strcpy(path_channel, iwatch_array->elem[i].path_channel); strcat(path_channel, ievent->name); @@ -700,22 +749,29 @@ int read_channels(unsigned long thread_num, struct channel_trace_fd *fd_pairs, goto free_fd; } - printf("Data received\n"); + printf_verbose("Data received\n"); #ifdef HAS_INOTIFY switch(pollfd[0].revents) { case POLLERR: - printf("Error returned in polling inotify fd %d.\n", pollfd[0].fd); + printf_verbose( + "Error returned in polling inotify fd %d.\n", + pollfd[0].fd); break; case POLLHUP: - printf("Polling inotify fd %d tells it has hung up.\n", pollfd[0].fd); + printf_verbose( + "Polling inotify fd %d tells it has hung up.\n", + pollfd[0].fd); break; case POLLNVAL: - printf("Polling inotify fd %d tells fd is not open.\n", pollfd[0].fd); + printf_verbose( + "Polling inotify fd %d tells fd is not open.\n", + pollfd[0].fd); break; case POLLPRI: case POLLIN: - - printf("Polling inotify fd %d : data ready.\n", pollfd[0].fd); + printf_verbose( + "Polling inotify fd %d : data ready.\n", + pollfd[0].fd); pthread_rwlock_wrlock(&fd_pairs_lock); read_inotify(inotify_fd, fd_pairs, iwatch_array); @@ -728,21 +784,29 @@ int read_channels(unsigned long thread_num, struct channel_trace_fd *fd_pairs, for(i=inotify_fds;ipair[i-inotify_fds].mutex) == 0) { - printf("Urgent read on fd %d\n", pollfd[i].fd); + printf_verbose( + "Urgent read on fd %d\n", + pollfd[i].fd); /* Take care of high priority channels first. */ high_prio = 1; /* it's ok to have an unavailable subbuffer */ @@ -767,7 +831,9 @@ int read_channels(unsigned long thread_num, struct channel_trace_fd *fd_pairs, pthread_rwlock_rdlock(&fd_pairs_lock); if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) { /* Take care of low priority channels. */ - printf("Normal read on fd %d\n", pollfd[i].fd); + printf_verbose( + "Normal read on fd %d\n", + pollfd[i].fd); /* it's ok to have an unavailable subbuffer */ ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]); if(ret == EAGAIN) ret = 0; @@ -836,8 +902,14 @@ void * thread_main(void *arg) long ret; unsigned long thread_num = (unsigned long)arg; + ret = pipe(thread_pipe); + if (ret < 0) { + perror("Error creating pipe"); + return (void*)ret; + } ret = read_channels(thread_num, &fd_pairs, inotify_fd, &inotify_watch_array); - + close(thread_pipe[0]); /* close read end */ + close(thread_pipe[1]); /* close write end */ return (void*)ret; } @@ -852,10 +924,13 @@ int channels_init() if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs, &inotify_fd, &inotify_watch_array)) goto close_channel; - + if (fd_pairs.num_pairs == 0) { + printf("No channel available for reading, exiting\n"); + ret = -ENOENT; + goto close_channel; + } if(ret = map_channels(&fd_pairs, 0, fd_pairs.num_pairs)) goto close_channel; - return 0; close_channel: @@ -882,15 +957,6 @@ int main(int argc, char ** argv) show_info(); - if(daemon_mode) { - ret = daemon(0, 0); - - if(ret == -1) { - perror("An error occured while daemonizing."); - exit(-1); - } - } - /* Connect the signal handlers */ act.sa_handler = handler; act.sa_flags = 0; @@ -905,6 +971,15 @@ int main(int argc, char ** argv) if(ret = channels_init()) return ret; + if(daemon_mode) { + ret = daemon(0, 0); + + if(ret == -1) { + perror("An error occured while daemonizing."); + exit(-1); + } + } + tids = malloc(sizeof(pthread_t) * num_threads); for(i=0; i= 0)