split read into map/read/unmap : preparation for thread version
[lttv.git] / ltt / branches / poly / lttd / lttd.c
index 9a6b3d68b732bc86267edd9bcaa6c4c0b05c8361..2fa7fe8db4f5214bc6b78880c15fa688e3bb2b99 100644 (file)
@@ -14,7 +14,9 @@
 #include <config.h>
 #endif
 
+#define _REENTRANT
 #define _GNU_SOURCE
+#include <features.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
@@ -27,6 +29,7 @@
 #include <sys/poll.h>
 #include <sys/mman.h>
 #include <signal.h>
+#include <pthread.h>
 
 /* Relayfs IOCTL */
 #include <asm/ioctl.h>
@@ -67,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 :
@@ -86,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");
 }
 
@@ -131,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");
@@ -297,13 +308,13 @@ end:
 
 int read_subbuffer(struct fd_pair *pair)
 {
-       unsigned int    subbuf_index;
+       unsigned int    consumed_old;
        int err, ret;
 
 
        err = ioctl(pair->channel, RELAYFS_GET_SUBBUF, 
-                                                               &subbuf_index);
-       printf("index : %u\n", subbuf_index);
+                                                               &consumed_old);
+       printf("cookie : %u\n", consumed_old);
        if(err != 0) {
                perror("Error in reserving sub buffer");
                ret = -EPERM;
@@ -311,7 +322,8 @@ int read_subbuffer(struct fd_pair *pair)
        }
        
        err = TEMP_FAILURE_RETRY(write(pair->trace,
-                               pair->mmap + (subbuf_index * pair->subbuf_size),
+                               pair->mmap 
+                                       + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)),
                                pair->subbuf_size));
 
        if(err < 0) {
@@ -322,10 +334,16 @@ int read_subbuffer(struct fd_pair *pair)
 
 
 write_error:
-       err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF);
+       err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF, &consumed_old);
        if(err != 0) {
-               perror("Error in unreserving sub buffer");
-               ret = -EPERM;
+               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;
        }
 
@@ -334,29 +352,11 @@ get_error:
 }
 
 
-/* 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");
@@ -395,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;j<i;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;
+       }
+
+end:
+       return ret;
+
+
+}
+
+
+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;
+
+               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));
@@ -468,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;j<i;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;
-       }
-
 end:
        return ret;
 }
@@ -504,7 +556,7 @@ void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
 
 int main(int argc, char ** argv)
 {
-       int ret;
+       int ret = 0;
        struct channel_trace_fd fd_pairs = { NULL, 0 };
        struct sigaction act;
        
@@ -536,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);
 
This page took 0.027765 seconds and 4 git commands to generate.