move everything out of trunk
[lttv.git] / ltt-control / lttd / lttd.c
index 1d170904c7113dd8e8b1664527027d13e78580c0..ceae377bd75f48cde76d8c0361ae30569d2c35f1 100644 (file)
@@ -5,6 +5,7 @@
  * This is a simple daemon that reads a few relay+debugfs channels and save
  * them in a trace.
  *
+ * CPU hot-plugging is supported using inotify.
  *
  * Copyright 2005 -
  *     Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
 #include <asm/types.h>
 
 /* Get the next sub buffer that can be read. */
-#define RELAY_GET_SUBBUF        _IOR(0xF4, 0x00,__u32)
+#define RELAY_GET_SUBBUF        _IOR(0xF5, 0x00,__u32)
 /* Release the oldest reserved (by "get") sub buffer. */
-#define RELAY_PUT_SUBBUF        _IOW(0xF4, 0x01,__u32)
+#define RELAY_PUT_SUBBUF        _IOW(0xF5, 0x01,__u32)
 /* returns the number of sub buffers in the per cpu channel. */
-#define RELAY_GET_N_SUBBUFS     _IOR(0xF4, 0x02,__u32)
+#define RELAY_GET_N_SUBBUFS     _IOR(0xF5, 0x02,__u32)
 /* returns the size of the sub buffers. */
-#define RELAY_GET_SUBBUF_SIZE   _IOR(0xF4, 0x03,__u32)
+#define RELAY_GET_SUBBUF_SIZE   _IOR(0xF5, 0x03,__u32)
 
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
-#include <linux/inotify.h>
+#include <sys/inotify.h>
+#if 0  /* should now be provided by libc. */
 /* From the inotify-tools 2.6 package */
 static inline int inotify_init (void)
 {
@@ -66,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)
@@ -117,6 +120,16 @@ struct inotify_watch_array {
        int num;
 };
 
+static __thread int thread_pipe[2];
+
+struct channel_trace_fd fd_pairs = { NULL, 0 };
+int inotify_fd = -1;
+struct inotify_watch_array inotify_watch_array = { NULL, 0 };
+
+/* protects fd_pairs and inotify_watch_array */
+pthread_rwlock_t fd_pairs_lock = PTHREAD_RWLOCK_INITIALIZER;
+
+
 static char            *trace_name = NULL;
 static char            *channel_name = NULL;
 static int             daemon_mode = 0;
@@ -125,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)
 {
@@ -146,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");
 }
 
@@ -203,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");
@@ -260,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));
@@ -286,7 +312,8 @@ 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,
@@ -336,7 +363,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) {
@@ -362,11 +389,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
 
@@ -383,11 +411,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;
@@ -408,19 +436,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)),
@@ -431,6 +461,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) {
@@ -440,6 +493,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;
@@ -457,7 +511,6 @@ get_error:
 }
 
 
-
 int map_channels(struct channel_trace_fd *fd_pairs,
        int idx_begin, int idx_end)
 {
@@ -493,6 +546,7 @@ int map_channels(struct channel_trace_fd *fd_pairs,
                }
        }
 
+#if 0
        /* Mmap each FD */
        for(i=idx_begin;i<idx_end;i++) {
                struct fd_pair *pair = &fd_pairs->pair[i];
@@ -522,13 +576,11 @@ munmap:
                ret |= err_ret;
        }
 
+#endif //0
 end:
        return ret;
-
-
 }
 
-
 int unmap_channels(struct channel_trace_fd *fd_pairs)
 {
        int j;
@@ -539,11 +591,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");
@@ -577,6 +631,10 @@ int read_inotify(int inotify_fd,
        offset = 0;
        len = read(inotify_fd, buf, sizeof(struct inotify_event) + PATH_MAX);
        if(len < 0) {
+
+               if(errno == EAGAIN)
+                       return 0;  /* another thread got the data before us */
+
                printf("Error in read from inotify FD %s.\n", strerror(len));
                return -1;
        }
@@ -585,9 +643,11 @@ int read_inotify(int inotify_fd,
                for(i=0; i<iwatch_array->num; 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);
@@ -628,10 +688,11 @@ int read_inotify(int inotify_fd,
  * full.
  */
 
-int read_channels(unsigned int thread_num, struct channel_trace_fd *fd_pairs,
+int read_channels(unsigned long thread_num, struct channel_trace_fd *fd_pairs,
        int inotify_fd, struct inotify_watch_array *iwatch_array)
 {
        struct pollfd *pollfd = NULL;
+       int num_pollfd;
        int i,j;
        int num_rdy, num_hup;
        int high_prio;
@@ -645,6 +706,8 @@ int read_channels(unsigned int thread_num, struct channel_trace_fd *fd_pairs,
        inotify_fds = 0;
 #endif
 
+       pthread_rwlock_rdlock(&fd_pairs_lock);
+
        /* Start polling the FD. Keep one fd for inotify */
        pollfd = malloc((inotify_fds + fd_pairs->num_pairs) * sizeof(struct pollfd));
 
@@ -657,7 +720,11 @@ int read_channels(unsigned int thread_num, struct channel_trace_fd *fd_pairs,
                pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel;
                pollfd[inotify_fds+i].events = POLLIN|POLLPRI;
        }
-       
+       num_pollfd = inotify_fds + fd_pairs->num_pairs;
+
+
+       pthread_rwlock_unlock(&fd_pairs_lock);
+
        while(1) {
                high_prio = 0;
                num_hup = 0; 
@@ -665,62 +732,77 @@ int read_channels(unsigned int thread_num, 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 (polling %d fd) :\n", 1+fd_pairs->num_pairs);
+               printf("Next poll (polling %d fd) :\n", num_pollfd);
 #endif //DEBUG
 
                /* Have we received a signal ? */
                if(quit_program) break;
                
-               num_rdy = poll(pollfd, inotify_fds+fd_pairs->num_pairs, -1);
+               num_rdy = poll(pollfd, num_pollfd, -1);
+
                if(num_rdy == -1) {
                        perror("Poll error");
                        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);
-                               old_num = fd_pairs->num_pairs;
+                               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);
-                               pollfd = realloc(pollfd,
-                                               (inotify_fds + fd_pairs->num_pairs) * sizeof(struct pollfd));
-                               for(i=old_num;i<fd_pairs->num_pairs;i++) {
-                                       pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel;
-                                       pollfd[inotify_fds+i].events = POLLIN|POLLPRI;
-                               }
+                               pthread_rwlock_unlock(&fd_pairs_lock);
+
                        break;
                }
 #endif
 
-               for(i=inotify_fds;i<inotify_fds+fd_pairs->num_pairs;i++) {
+               for(i=inotify_fds;i<num_pollfd;i++) {
                        switch(pollfd[i].revents) {
                                case POLLERR:
-                                       printf("Error returned in polling fd %d.\n", pollfd[i].fd);
+                                       printf_verbose(
+                                               "Error returned in polling fd %d.\n",
+                                               pollfd[i].fd);
                                        num_hup++;
                                        break;
                                case POLLHUP:
-                                       printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
+                                       printf_verbose(
+                                               "Polling fd %d tells it has hung up.\n",
+                                               pollfd[i].fd);
                                        num_hup++;
                                        break;
                                case POLLNVAL:
-                                       printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
+                                       printf_verbose(
+                                               "Polling fd %d tells fd is not open.\n",
+                                               pollfd[i].fd);
                                        num_hup++;
                                        break;
                                case POLLPRI:
+                                       pthread_rwlock_rdlock(&fd_pairs_lock);
                                        if(pthread_mutex_trylock(&fd_pairs->pair[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 */
@@ -731,19 +813,23 @@ int read_channels(unsigned int thread_num, struct channel_trace_fd *fd_pairs,
                                                if(ret)
                                                        printf("Error in mutex unlock : %s\n", strerror(ret));
                                        }
+                                       pthread_rwlock_unlock(&fd_pairs_lock);
                                        break;
                        }
                }
                /* If every buffer FD has hung up, we end the read loop here */
-               if(num_hup == fd_pairs->num_pairs) break;
+               if(num_hup == num_pollfd - inotify_fds) break;
 
                if(!high_prio) {
-                       for(i=inotify_fds;i<inotify_fds+fd_pairs->num_pairs;i++) {
+                       for(i=inotify_fds;i<num_pollfd;i++) {
                                switch(pollfd[i].revents) {
                                        case POLLIN:
+                                               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;
@@ -752,11 +838,34 @@ int read_channels(unsigned int thread_num, struct channel_trace_fd *fd_pairs,
                                                        if(ret)
                                                                printf("Error in mutex unlock : %s\n", strerror(ret));
                                                }
+                                               pthread_rwlock_unlock(&fd_pairs_lock);
                                                break;
                                }
                        }
                }
 
+               /* Update pollfd array if an entry was added to fd_pairs */
+               pthread_rwlock_rdlock(&fd_pairs_lock);
+               if((inotify_fds + fd_pairs->num_pairs) != num_pollfd) {
+                       pollfd = realloc(pollfd,
+                                       (inotify_fds + fd_pairs->num_pairs) * sizeof(struct pollfd));
+                       for(i=num_pollfd-inotify_fds;i<fd_pairs->num_pairs;i++) {
+                               pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel;
+                               pollfd[inotify_fds+i].events = POLLIN|POLLPRI;
+                       }
+                       num_pollfd = fd_pairs->num_pairs + inotify_fds;
+               }
+               pthread_rwlock_unlock(&fd_pairs_lock);
+
+               /* NB: If the fd_pairs structure is updated by another thread from this
+                *     point forward, the current thread will wait in the poll without
+                *     monitoring the new channel. However, this thread will add the
+                *     new channel on next poll (and this should not take too much time
+                *     on a loaded system).
+                *
+                *     This event is quite unlikely and can only occur if a CPU is
+                *     hot-plugged while multple lttd threads are running.
+                */
        }
 
 free_fd:
@@ -786,31 +895,45 @@ void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs, int inotify_fd
 /* Thread worker */
 void * thread_main(void *arg)
 {
-       struct channel_trace_fd fd_pairs = { NULL, 0 };
-       int inotify_fd = -1;
-       struct inotify_watch_array inotify_watch_array = { NULL, 0 };
+       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;
+}
+
+
+int channels_init()
+{
        int ret = 0;
-       unsigned int thread_num = (unsigned int)arg;
 
        inotify_fd = inotify_init();
+       fcntl(inotify_fd, F_SETFL, O_NONBLOCK);
 
        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;
-
-       ret = read_channels(thread_num, &fd_pairs, inotify_fd, &inotify_watch_array);
-
-       ret |= unmap_channels(&fd_pairs);
+       return 0;
 
 close_channel:
        close_channel_trace_pairs(&fd_pairs, inotify_fd, &inotify_watch_array);
        if(inotify_fd >= 0)
                close(inotify_fd);
-
-       return (void*)ret;
+       return ret;
 }
 
 
@@ -819,7 +942,7 @@ int main(int argc, char ** argv)
        int ret = 0;
        struct sigaction act;
        pthread_t *tids;
-       unsigned int i;
+       unsigned long i;
        void *tret;
        
        ret = parse_arguments(argc, argv);
@@ -830,15 +953,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;
@@ -850,9 +964,21 @@ int main(int argc, char ** argv)
        sigaction(SIGQUIT, &act, NULL);
        sigaction(SIGINT, &act, NULL);
 
+       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<num_threads; i++) {
+
                ret = pthread_create(&tids[i], NULL, thread_main, (void*)i);
                if(ret) {
                        perror("Error creating thread");
@@ -866,12 +992,17 @@ int main(int argc, char ** argv)
                        perror("Error joining thread");
                        break;
                }
-               if((int)tret != 0) {
-                       printf("Error %s occured in thread %u\n", strerror((int)tret), i);
+               if((long)tret != 0) {
+                       printf("Error %s occured in thread %u\n",
+                               strerror((long)tret), i);
                }
        }
 
        free(tids);
+       ret = unmap_channels(&fd_pairs);
+       close_channel_trace_pairs(&fd_pairs, inotify_fd, &inotify_watch_array);
+       if(inotify_fd >= 0)
+               close(inotify_fd);
                        
        return ret;
 }
This page took 0.046246 seconds and 4 git commands to generate.