CTF: open trace read
[babeltrace.git] / formats / ctf / ctf.c
index 515e478357b3e519d1f4cb5e43fbf8bd5f83fb0d..6488ffc965da2ecb667fee4ea6f08f6c9ddcdc76 100644 (file)
 
 /*
  * We currently simply map a page to read the packet header and packet
- * context to get the packet length and content length.
+ * context to get the packet length and content length. (in bits)
  */
-#define MAX_PACKET_HEADER_LEN  getpagesize()
+#define MAX_PACKET_HEADER_LEN  (getpagesize() * CHAR_BIT)
+#define WRITE_PACKET_LEN       (getpagesize() * 8 * CHAR_BIT)
 #define UUID_LEN 16    /* uuid by value len */
 
 extern int yydebug;
@@ -59,6 +60,8 @@ static struct format ctf_format = {
        .int_write = ctf_int_write,
        .double_read = ctf_double_read,
        .double_write = ctf_double_write,
+       .ldouble_read = ctf_ldouble_read,
+       .ldouble_write = ctf_ldouble_write,
        .float_copy = ctf_float_copy,
        .string_copy = ctf_string_copy,
        .string_read = ctf_string_read,
@@ -78,19 +81,71 @@ static struct format ctf_format = {
        .close_trace = ctf_close_trace,
 };
 
+void init_pos(struct stream_pos *pos, int fd)
+{
+       pos->fd = fd;
+       pos->mmap_offset = 0;
+       pos->packet_size = 0;
+       pos->content_size = 0;
+       pos->content_size_loc = NULL;
+       pos->base = NULL;
+       pos->offset = 0;
+       pos->dummy = false;
+       pos->packet_index = g_array_new(FALSE, TRUE,
+                                       sizeof(struct packet_index));
+       pos->cur_index = 0;
+       if (fd >= 0) {
+               int flags = fcntl(fd, F_GETFL, 0);
+
+               switch (flags & O_ACCMODE) {
+               case O_RDONLY:
+                       pos->prot = PROT_READ;
+                       pos->flags = MAP_PRIVATE;
+                       break;
+               case O_WRONLY:
+               case O_RDWR:
+                       pos->prot = PROT_WRITE; /* Write has priority */
+                       pos->flags = MAP_SHARED;
+                       move_pos_slow(pos, 0);  /* position for write */
+                       break;
+               default:
+                       assert(0);
+               }
+
+       }
+}
+
+void fini_pos(struct stream_pos *pos)
+{
+       int ret;
+
+       if (pos->prot == PROT_WRITE && pos->content_size_loc)
+               *pos->content_size_loc = pos->offset;
+       if (pos->base) {
+               /* unmap old base */
+               ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+               if (ret) {
+                       fprintf(stdout, "Unable to unmap old base: %s.\n",
+                               strerror(errno));
+                       assert(0);
+               }
+       }
+       (void) g_array_free(pos->packet_index, TRUE);
+}
+
 void move_pos_slow(struct stream_pos *pos, size_t offset)
 {
        int ret;
+       off_t off;
+       struct packet_index *index;
 
-       /*
-        * The caller should never ask for move_pos across packets,
-        * except to get exactly at the beginning of the next packet.
-        */
-       assert(pos->offset + offset == pos->content_size);
+
+       if (pos->prot == PROT_WRITE && pos->content_size_loc)
+               *pos->content_size_loc = pos->offset;
 
        if (pos->base) {
                /* unmap old base */
-               ret = munmap(pos->base, pos->packet_size);
+               ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
                if (ret) {
                        fprintf(stdout, "Unable to unmap old base: %s.\n",
                                strerror(errno));
@@ -98,13 +153,46 @@ void move_pos_slow(struct stream_pos *pos, size_t offset)
                }
        }
 
-       pos->mmap_offset += pos->packet_size / CHAR_BIT;
+       /*
+        * The caller should never ask for move_pos across packets,
+        * except to get exactly at the beginning of the next packet.
+        */
+       if (pos->prot == PROT_WRITE) {
+               /* The writer will add padding */
+               assert(pos->offset + offset == pos->packet_size);
+
+               /*
+                * Don't increment for initial stream move (only condition where
+                * pos->offset can be 0.
+                */
+               if (pos->offset)
+                       pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
+               pos->content_size = -1U;        /* Unknown at this point */
+               pos->packet_size = WRITE_PACKET_LEN;
+               off = posix_fallocate(pos->fd, pos->mmap_offset, pos->packet_size / CHAR_BIT);
+               assert(off >= 0);
+       } else {
+               /* The reader will expect us to skip padding */
+               assert(pos->offset + offset == pos->content_size);
+
+               /*
+                * Don't increment for initial stream move (only condition where
+                * pos->offset can be 0).
+                */
+               if (pos->offset)
+                       ++pos->cur_index;
+               index = &g_array_index(pos->packet_index, struct packet_index,
+                                      pos->cur_index);
+               pos->mmap_offset = index->offset;
+
+               /* Lookup context/packet size in index */
+               pos->content_size = index->content_size;
+               pos->packet_size = index->packet_size;
+       }
        /* map new base. Need mapping length from header. */
-       pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN, PROT_READ,
-                        MAP_PRIVATE, pos->fd, pos->mmap_offset);
-       pos->content_size = 0;  /* Unknown at this point */
-       pos->packet_size = 0;   /* Unknown at this point */
-
+       pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
+                        pos->flags, pos->fd, pos->mmap_offset);
+       pos->offset = 0;
 }
 
 /*
@@ -199,20 +287,25 @@ int create_stream_packet_index(struct trace_descriptor *td,
 
                if (pos->base) {
                        /* unmap old base */
-                       ret = munmap(pos->base, pos->packet_size);
+                       ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
                        if (ret) {
                                fprintf(stdout, "Unable to unmap old base: %s.\n",
                                        strerror(errno));
                                return ret;
                        }
+                       pos->base = NULL;
                }
                /* map new base. Need mapping length from header. */
-               pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN, PROT_READ,
+               pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
                                 MAP_PRIVATE, pos->fd, pos->mmap_offset);
-               pos->content_size = 0;  /* Unknown at this point */
-               pos->packet_size = 0;   /* Unknown at this point */
+               pos->content_size = MAX_PACKET_HEADER_LEN;      /* Unknown at this point */
+               pos->packet_size = MAX_PACKET_HEADER_LEN;       /* Unknown at this point */
                pos->offset = 0;        /* Position of the packet header */
 
+               packet_index.offset = pos->mmap_offset;
+               packet_index.content_size = 0;
+               packet_index.packet_size = 0;
+
                /* read and check header, set stream id (and check) */
                if (td->ctf_trace.packet_header) {
                        /* Read packet header */
@@ -229,8 +322,10 @@ int create_stream_packet_index(struct trace_descriptor *td,
                                defint = container_of(field->definition, struct definition_integer, p);
                                assert(defint->declaration->signedness == FALSE);
                                if (defint->value._unsigned != CTF_MAGIC) {
-                                       fprintf(stdout, "[error] Invalid magic number %" PRIX64 ".\n",
-                                                       defint->value._unsigned);
+                                       fprintf(stdout, "[error] Invalid magic number %" PRIX64 " at packet %u (file offset %zd).\n",
+                                                       defint->value._unsigned,
+                                                       file_stream->pos.packet_index->len,
+                                                       (ssize_t) pos->mmap_offset);
                                        return -EINVAL;
                                }
                        }
@@ -243,7 +338,6 @@ int create_stream_packet_index(struct trace_descriptor *td,
                                uint64_t i;
                                uint8_t uuidval[UUID_LEN];
 
-
                                field = struct_definition_get_field_from_index(td->ctf_trace.packet_header, len_index);
                                assert(field->definition->declaration->id == CTF_TYPE_ARRAY);
                                defarray = container_of(field->definition, struct definition_array, p);
@@ -299,49 +393,53 @@ int create_stream_packet_index(struct trace_descriptor *td,
                }
                first_packet = 0;
 
-               /* Read packet context */
-               stream->packet_context->p.declaration->copy(NULL, NULL,
-                               pos, &ctf_format, &stream->packet_context->p);
-
-               /* read content size from header */
-               len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
-               if (len_index >= 0) {
-                       struct definition_integer *defint;
-                       struct field *field;
-
-                       field = struct_definition_get_field_from_index(stream->packet_context, len_index);
-                       assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
-                       defint = container_of(field->definition, struct definition_integer, p);
-                       assert(defint->declaration->signedness == FALSE);
-                       pos->content_size = defint->value._unsigned;
-               } else {
-                       /* Use file size for packet size */
-                       pos->content_size = filestats.st_size * CHAR_BIT;
-               }
+               if (stream->packet_context) {
+                       /* Read packet context */
+                       stream->packet_context->p.declaration->copy(NULL, NULL,
+                                       pos, &ctf_format, &stream->packet_context->p);
 
-               /* read packet size from header */
-               len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
-               if (len_index >= 0) {
-                       struct definition_integer *defint;
-                       struct field *field;
-
-                       field = struct_definition_get_field_from_index(stream->packet_context, len_index);
-                       assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
-                       defint = container_of(field->definition, struct definition_integer, p);
-                       assert(defint->declaration->signedness == FALSE);
-                       pos->packet_size = defint->value._unsigned;
+                       /* read content size from header */
+                       len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
+                       if (len_index >= 0) {
+                               struct definition_integer *defint;
+                               struct field *field;
+
+                               field = struct_definition_get_field_from_index(stream->packet_context, len_index);
+                               assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
+                               defint = container_of(field->definition, struct definition_integer, p);
+                               assert(defint->declaration->signedness == FALSE);
+                               packet_index.content_size = defint->value._unsigned;
+                       } else {
+                               /* Use file size for packet size */
+                               packet_index.content_size = filestats.st_size * CHAR_BIT;
+                       }
+
+                       /* read packet size from header */
+                       len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
+                       if (len_index >= 0) {
+                               struct definition_integer *defint;
+                               struct field *field;
+
+                               field = struct_definition_get_field_from_index(stream->packet_context, len_index);
+                               assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
+                               defint = container_of(field->definition, struct definition_integer, p);
+                               assert(defint->declaration->signedness == FALSE);
+                               packet_index.packet_size = defint->value._unsigned;
+                       } else {
+                               /* Use content size if non-zero, else file size */
+                               packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
+                       }
                } else {
+                       /* Use file size for packet size */
+                       packet_index.content_size = filestats.st_size * CHAR_BIT;
                        /* Use content size if non-zero, else file size */
-                       pos->packet_size = pos->content_size ? : filestats.st_size * CHAR_BIT;
+                       packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
                }
 
-               packet_index.offset = pos->mmap_offset;
-               packet_index.content_size = pos->content_size;
-               packet_index.packet_size = pos->packet_size;
                /* add index to packet array */
                g_array_append_val(file_stream->pos.packet_index, packet_index);
 
-               pos->mmap_offset += pos->packet_size / CHAR_BIT;
+               pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
        }
 
        return 0;
@@ -361,9 +459,7 @@ int ctf_open_file_stream_read(struct trace_descriptor *td, const char *path, int
        if (ret < 0)
                goto error;
        file_stream = g_new0(struct ctf_file_stream, 1);
-       file_stream->pos.fd = ret;
-       file_stream->pos.packet_index = g_array_new(FALSE, TRUE,
-                                               sizeof(struct packet_index));
+       init_pos(&file_stream->pos, ret);
        ret = create_stream_packet_index(td, file_stream);
        if (ret)
                goto error_index;
@@ -372,7 +468,7 @@ int ctf_open_file_stream_read(struct trace_descriptor *td, const char *path, int
        return 0;
 
 error_index:
-       (void) g_array_free(file_stream->pos.packet_index, TRUE);
+       fini_pos(&file_stream->pos);
        close(file_stream->pos.fd);
        g_free(file_stream);
 error:
@@ -392,14 +488,14 @@ int ctf_open_trace_read(struct trace_descriptor *td, const char *path, int flags
        /* Open trace directory */
        td->ctf_trace.dir = opendir(path);
        if (!td->ctf_trace.dir) {
-               fprintf(stdout, "Unable to open trace directory.\n");
+               fprintf(stdout, "[error] Unable to open trace directory.\n");
                ret = -ENOENT;
                goto error;
        }
 
        td->ctf_trace.dirfd = open(path, 0);
        if (td->ctf_trace.dirfd < 0) {
-               fprintf(stdout, "Unable to open trace directory file descriptor.\n");
+               fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
                ret = -ENOENT;
                goto error_dirfd;
        }
@@ -429,7 +525,7 @@ int ctf_open_trace_read(struct trace_descriptor *td, const char *path, int flags
        for (;;) {
                ret = readdir_r(td->ctf_trace.dir, dirent, &diriter);
                if (ret) {
-                       fprintf(stdout, "Readdir error.\n");
+                       fprintf(stdout, "[error] Readdir error.\n");
                        goto readdir_error;
                }
                if (!diriter)
@@ -438,7 +534,11 @@ int ctf_open_trace_read(struct trace_descriptor *td, const char *path, int flags
                                || !strcmp(diriter->d_name, "..")
                                || !strcmp(diriter->d_name, "metadata"))
                        continue;
-               /* TODO: open file stream */
+               ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
+               if (ret) {
+                       fprintf(stdout, "[error] Open file stream error.\n");
+                       goto readdir_error;
+               }
        }
 
        free(dirent);
@@ -486,7 +586,7 @@ struct trace_descriptor *ctf_open_trace(const char *path, int flags)
 
        td = g_new0(struct trace_descriptor, 1);
 
-       switch (flags) {
+       switch (flags & O_ACCMODE) {
        case O_RDONLY:
                ret = ctf_open_trace_read(td, path, flags);
                if (ret)
@@ -511,7 +611,7 @@ error:
 static
 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
 {
-       (void) g_array_free(file_stream->pos.packet_index, TRUE);
+       fini_pos(&file_stream->pos);
        close(file_stream->pos.fd);
 }
 
This page took 0.027955 seconds and 4 git commands to generate.