X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=ltt%2Fbranches%2Fpoly%2Flttd%2Flttd.c;h=2fa7fe8db4f5214bc6b78880c15fa688e3bb2b99;hb=e54e1d5d5e29aab92dd8356eff84407c6b0e0073;hp=e090729c369aa0feb24a19b96d76c536f96f18e0;hpb=1d483eea84bbb9463a64384817690109fdf81942;p=ltt-control.git diff --git a/ltt/branches/poly/lttd/lttd.c b/ltt/branches/poly/lttd/lttd.c index e090729..2fa7fe8 100644 --- a/ltt/branches/poly/lttd/lttd.c +++ b/ltt/branches/poly/lttd/lttd.c @@ -10,7 +10,13 @@ * Mathieu Desnoyers */ +#ifdef HAVE_CONFIG_H +#include +#endif + +#define _REENTRANT #define _GNU_SOURCE +#include #include #include #include @@ -23,6 +29,7 @@ #include #include #include +#include /* Relayfs IOCTL */ #include @@ -31,7 +38,7 @@ /* Get the next sub buffer that can be read. */ #define RELAYFS_GET_SUBBUF _IOR(0xF4, 0x00,__u32) /* Release the oldest reserved (by "get") sub buffer. */ -#define RELAYFS_PUT_SUBBUF _IO(0xF4, 0x01) +#define RELAYFS_PUT_SUBBUF _IOW(0xF4, 0x01,__u32) /* returns the number of sub buffers in the per cpu channel. */ #define RELAYFS_GET_N_SUBBUFS _IOR(0xF4, 0x02,__u32) /* returns the size of the sub buffers. */ @@ -63,6 +70,7 @@ static char *trace_name = NULL; static char *channel_name = NULL; static int daemon_mode = 0; static int append_mode = 0; +static unsigned long num_threads = 1; volatile static int quit_program = 0; /* For signal handler */ /* Args : @@ -71,6 +79,7 @@ volatile static int quit_program = 0; /* For signal handler */ * -c directory Root directory of the relayfs trace channels. * -d Run in background (daemon). * -a Trace append mode. + * -s Send SIGUSR1 to parent when ready for IO. */ void show_arguments(void) { @@ -81,6 +90,7 @@ void show_arguments(void) printf("-c directory Root directory of the relayfs trace channels.\n"); printf("-d Run in background (daemon).\n"); printf("-a Append to an possibly existing trace.\n"); + printf("-n Number of threads to start.\n"); printf("\n"); } @@ -126,6 +136,12 @@ int parse_arguments(int argc, char **argv) case 'a': append_mode = 1; break; + case 'n': + if(argn+1 < argc) { + num_threads = strtoul(argv[argn+1], NULL, 0); + argn++; + } + break; default: printf("Invalid argument '%s'.\n", argv[argn]); printf("\n"); @@ -188,22 +204,21 @@ int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, char path_trace[PATH_MAX]; int path_trace_len; char *path_trace_ptr; + int open_ret = 0; if(channel_dir == NULL) { perror(subchannel_name); - return ENOENT; + open_ret = ENOENT; + goto end; } - //FIXME : check if the directory already exist, and ask the user if he wants - //to append to the traces. printf("Creating trace subdirectory %s\n", subtrace_name); ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO); if(ret == -1) { - if(errno == EEXIST && append_mode) { - printf("Appending to directory %s as resquested\n", subtrace_name); - } else { + if(errno != EEXIST) { perror(subtrace_name); - return -1; + open_ret = -1; + goto end; } } @@ -250,12 +265,14 @@ int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, open(path_channel, O_RDONLY | O_NONBLOCK); if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) { perror(path_channel); + fd_pairs->num_pairs--; + continue; } /* Open the trace in write mode, only append if append_mode */ ret = stat(path_trace, &stat_buf); if(ret == 0) { if(append_mode) { - printf("Appending to file %s as resquested\n", path_trace); + printf("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, @@ -266,7 +283,8 @@ int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, } } else { printf("File %s exists, cannot open. Try append mode.\n", path_trace); - return -1; + open_ret = -1; + goto end; } } else { if(errno == ENOENT) { @@ -281,71 +299,70 @@ int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, } } +end: closedir(channel_dir); - return 0; + return open_ret; } int read_subbuffer(struct fd_pair *pair) { - unsigned int subbuf_index; - int ret; + unsigned int consumed_old; + int err, ret; - ret = ioctl(pair->channel, RELAYFS_GET_SUBBUF, - &subbuf_index); - if(ret != 0) { + err = ioctl(pair->channel, RELAYFS_GET_SUBBUF, + &consumed_old); + printf("cookie : %u\n", consumed_old); + if(err != 0) { perror("Error in reserving sub buffer"); - goto error; + ret = -EPERM; + goto get_error; } - - ret = TEMP_FAILURE_RETRY(write(pair->trace, - pair->mmap + (subbuf_index * pair->subbuf_size), - pair->subbuf_size)); - if(ret != 0) { + err = TEMP_FAILURE_RETRY(write(pair->trace, + pair->mmap + + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)), + pair->subbuf_size)); + + if(err < 0) { perror("Error in writing to file"); - goto error; + ret = err; + goto write_error; } - ret = ioctl(pair->channel, RELAYFS_PUT_SUBBUF); - if(ret != 0) { - perror("Error in unreserving sub buffer"); - goto error; +write_error: + err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF, &consumed_old); + if(err != 0) { + if(errno == -EFAULT) { + perror("Error in unreserving sub buffer"); + ret = -EFAULT; + } else if(errno == -EIO) { + perror("Reader has been pushed by the writer, last subbuffer corrupted."); + /* FIXME : we may delete the last written buffer if we wish. */ + ret = -EIO; + } + goto get_error; } - return 0; -error: - return -1; +get_error: + return ret; } -/* read_channels - * - * Read the realyfs channels and write them in the paired tracefiles. - * - * @fd_pairs : paired channels and trace files. - * - * returns 0 on success, -1 on error. - * - * Note that the high priority polled channels are consumed first. We then poll - * again to see if these channels are still in priority. Only when no - * high priority channel is left, we start reading low priority channels. - * - * Note that a channel is considered high priority when the buffer is almost - * full. - */ -int read_channels(struct channel_trace_fd *fd_pairs) +int map_channels(struct channel_trace_fd *fd_pairs) { - struct pollfd *pollfd; int i,j; - int num_rdy, num_hup; - int high_prio; - int ret; + int ret=0; + if(fd_pairs->num_pairs <= 0) { + printf("No channel to read\n"); + goto end; + } + /* Get the subbuf sizes and number */ for(i=0;inum_pairs;i++) { @@ -378,6 +395,73 @@ int read_channels(struct channel_trace_fd *fd_pairs) } + /* Error handling */ + /* munmap only the successfully mmapped indexes */ +munmap: + /* Munmap each FD */ + for(j=0;jpair[j]; + int err_ret; + + err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); + if(err_ret != 0) { + perror("Error in munmap"); + } + ret |= err_ret; + } + +end: + return ret; + + +} + + +int unmap_channels(struct channel_trace_fd *fd_pairs) +{ + int j; + int ret=0; + + /* Munmap each FD */ + for(j=0;jnum_pairs;j++) { + struct fd_pair *pair = &fd_pairs->pair[j]; + int err_ret; + + err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); + if(err_ret != 0) { + perror("Error in munmap"); + } + ret |= err_ret; + } + + return ret; +} + + +/* read_channels + * + * Read the relayfs channels and write them in the paired tracefiles. + * + * @fd_pairs : paired channels and trace files. + * + * returns 0 on success, -1 on error. + * + * Note that the high priority polled channels are consumed first. We then poll + * again to see if these channels are still in priority. Only when no + * high priority channel is left, we start reading low priority channels. + * + * Note that a channel is considered high priority when the buffer is almost + * full. + */ + +int read_channels(struct channel_trace_fd *fd_pairs) +{ + struct pollfd *pollfd; + int i,j; + int num_rdy, num_hup; + int high_prio; + int ret; + /* Start polling the FD */ pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd)); @@ -395,7 +479,7 @@ int read_channels(struct channel_trace_fd *fd_pairs) printf("Press a key for next poll...\n"); char buf[1]; read(STDIN_FILENO, &buf, 1); - printf("Next poll :\n"); + printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs); #endif //DEBUG /* Have we received a signal ? */ @@ -451,21 +535,6 @@ int read_channels(struct channel_trace_fd *fd_pairs) free_fd: free(pollfd); - /* munmap only the successfully mmapped indexes */ - i = fd_pairs->num_pairs; -munmap: - /* Munmap each FD */ - for(j=0;jpair[j]; - int err_ret; - - err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); - if(err_ret != 0) { - perror("Error in munmap"); - } - ret |= err_ret; - } - end: return ret; } @@ -487,8 +556,7 @@ void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs) int main(int argc, char ** argv) { - int ret; - pid_t pid; + int ret = 0; struct channel_trace_fd fd_pairs = { NULL, 0 }; struct sigaction act; @@ -501,18 +569,13 @@ int main(int argc, char ** argv) show_info(); if(daemon_mode) { - pid = fork(); - - if(pid > 0) { - /* parent */ - return 0; - } else if(pid < 0) { - /* error */ - printf("An error occured while forking.\n"); - return -1; - } - /* else, we are the child, continue... */ - } + ret = daemon(0, 0); + + if(ret == -1) { + perror("An error occured while daemonizing."); + exit(-1); + } + } /* Connect the signal handlers */ act.sa_handler = handler; @@ -525,12 +588,17 @@ int main(int argc, char ** argv) sigaction(SIGQUIT, &act, NULL); sigaction(SIGINT, &act, NULL); - //return 0; + if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs)) goto close_channel; + if(ret = map_channels(&fd_pairs)) + goto close_channel; + ret = read_channels(&fd_pairs); + ret |= unmap_channels(&fd_pairs); + close_channel: close_channel_trace_pairs(&fd_pairs);