-int unmap_channels(struct channel_trace_fd *fd_pairs)
-{
- int j;
- int ret=0;
-
- /* Munmap each FD */
- for(j=0;j<fd_pairs->num_pairs;j++) {
- 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");
- }
- ret |= err_ret;
- }
-
- return ret;
-}
-
-#ifdef HAS_INOTIFY
-/* Inotify event arrived.
- *
- * Only support add file for now.
- */
-
-int read_inotify(int inotify_fd,
- struct channel_trace_fd *fd_pairs,
- struct inotify_watch_array *iwatch_array)
-{
- char buf[sizeof(struct inotify_event) + PATH_MAX];
- char path_channel[PATH_MAX];
- char path_trace[PATH_MAX];
- ssize_t len;
- struct inotify_event *ievent;
- size_t offset;
- unsigned int i;
- int ret;
- int old_num;
-
- 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;
- }
- while(offset < len) {
- ievent = (struct inotify_event *)&(buf[offset]);
- for(i=0; i<iwatch_array->num; i++) {
- if(iwatch_array->elem[i].wd == ievent->wd &&
- ievent->mask == IN_CREATE) {
- printf_verbose(
- "inotify wd %u event mask : %u for %s%s\n",
- ievent->wd, ievent->mask,
- 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);
- strcpy(path_trace, iwatch_array->elem[i].path_trace);
- strcat(path_trace, ievent->name);
- if(ret = open_buffer_file(ievent->name, path_channel,
- path_trace, fd_pairs)) {
- printf("Error opening buffer file\n");
- return -1;
- }
- if(ret = map_channels(fd_pairs, old_num, fd_pairs->num_pairs)) {
- printf("Error mapping channel\n");
- return -1;
- }
-
- }
- }
- offset += sizeof(*ievent) + ievent->len;
- }
-}
-#endif //HAS_INOTIFY
-
-/* read_channels
- *
- * Thread worker.
- *
- * Read the debugfs 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(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;
- int ret = 0;
- int inotify_fds;
- unsigned int old_num;
-
-#ifdef HAS_INOTIFY
- inotify_fds = 1;
-#else
- 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));
-
-#ifdef HAS_INOTIFY
- pollfd[0].fd = inotify_fd;
- pollfd[0].events = POLLIN|POLLPRI;
-#endif
-
- for(i=0;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 = inotify_fds + fd_pairs->num_pairs;
-
-
- pthread_rwlock_unlock(&fd_pairs_lock);
-
- while(1) {
- high_prio = 0;
- num_hup = 0;
-#ifdef DEBUG
- printf("Press a key for next poll...\n");
- char buf[1];
- read(STDIN_FILENO, &buf, 1);
- printf("Next poll (polling %d fd) :\n", num_pollfd);
-#endif //DEBUG
-
- /* Have we received a signal ? */
- if(quit_program) break;
-
- num_rdy = poll(pollfd, num_pollfd, -1);
-
- if(num_rdy == -1) {
- perror("Poll error");
- goto free_fd;
- }
-
- printf_verbose("Data received\n");
-#ifdef HAS_INOTIFY
- switch(pollfd[0].revents) {
- case POLLERR:
- printf_verbose(
- "Error returned in polling inotify fd %d.\n",
- pollfd[0].fd);
- break;
- case POLLHUP:
- printf_verbose(
- "Polling inotify fd %d tells it has hung up.\n",
- pollfd[0].fd);
- break;
- case POLLNVAL:
- printf_verbose(
- "Polling inotify fd %d tells fd is not open.\n",
- pollfd[0].fd);
- break;
- case POLLPRI:
- case POLLIN:
- 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);
- pthread_rwlock_unlock(&fd_pairs_lock);
-
- break;
- }
-#endif
-
- for(i=inotify_fds;i<num_pollfd;i++) {
- switch(pollfd[i].revents) {
- case POLLERR:
- printf_verbose(
- "Error returned in polling fd %d.\n",
- pollfd[i].fd);
- num_hup++;
- break;
- case POLLHUP:
- printf_verbose(
- "Polling fd %d tells it has hung up.\n",
- pollfd[i].fd);
- num_hup++;
- break;
- case POLLNVAL:
- 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_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 sub-buffer */
- ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]);
- if(ret == EAGAIN) ret = 0;
-
- ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex);
- 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 == num_pollfd - inotify_fds) break;
-
- if(!high_prio) {
- 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_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;
-
- ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex);
- 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:
- free(pollfd);
-
-end:
- return ret;
-}
-
-
-void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs, int inotify_fd,
- struct inotify_watch_array *iwatch_array)
-{
- int i;