Use mmap_align
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 29 May 2012 22:54:43 +0000 (18:54 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 29 May 2012 22:54:43 +0000 (18:54 -0400)
Allow reading traces with packet size different from the machine page
size.

Fixes #237

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
formats/ctf/ctf.c
formats/ctf/types/float.c
formats/ctf/types/integer.c
include/babeltrace/ctf/types.h

index 1ba7e949d8fabfcf3263e041f2608cca5bc6337d..e3a487d6ef969f0ad2cd97c2ba360bb3a76c0937 100644 (file)
@@ -491,7 +491,7 @@ void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
        pos->packet_size = 0;
        pos->content_size = 0;
        pos->content_size_loc = NULL;
-       pos->base = NULL;
+       pos->base_mma = NULL;
        pos->offset = 0;
        pos->dummy = false;
        pos->cur_index = 0;
@@ -526,9 +526,9 @@ void ctf_fini_pos(struct ctf_stream_pos *pos)
 
        if (pos->prot == PROT_WRITE && pos->content_size_loc)
                *pos->content_size_loc = pos->offset;
-       if (pos->base) {
+       if (pos->base_mma) {
                /* unmap old base */
-               ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+               ret = munmap_align(pos->base_mma);
                if (ret) {
                        fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                strerror(errno));
@@ -555,15 +555,15 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
        if (pos->prot == PROT_WRITE && pos->content_size_loc)
                *pos->content_size_loc = pos->offset;
 
-       if (pos->base) {
+       if (pos->base_mma) {
                /* unmap old base */
-               ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+               ret = munmap_align(pos->base_mma);
                if (ret) {
                        fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                strerror(errno));
                        assert(0);
                }
-               pos->base = NULL;
+               pos->base_mma = NULL;
        }
 
        /*
@@ -686,9 +686,9 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
                }
        }
        /* map new base. Need mapping length from header. */
-       pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
-                        pos->flags, pos->fd, pos->mmap_offset);
-       if (pos->base == MAP_FAILED) {
+       pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
+                       pos->flags, pos->fd, pos->mmap_offset);
+       if (pos->base_mma == MAP_FAILED) {
                fprintf(stderr, "[error] mmap error %s.\n",
                        strerror(errno));
                assert(0);
@@ -1145,19 +1145,20 @@ int create_stream_packet_index(struct ctf_trace *td,
        for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
                uint64_t stream_id = 0;
 
-               if (pos->base) {
+               if (pos->base_mma) {
                        /* unmap old base */
-                       ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+                       ret = munmap_align(pos->base_mma);
                        if (ret) {
                                fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
                                        strerror(errno));
                                return ret;
                        }
-                       pos->base = NULL;
+                       pos->base_mma = NULL;
                }
                /* map new base. Need mapping length from header. */
-               pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
+               pos->base_mma = mmap_align(MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
                                 MAP_PRIVATE, pos->fd, pos->mmap_offset);
+               assert(pos->base_mma != MAP_FAILED);
                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 */
@@ -1553,7 +1554,7 @@ void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
        pos->content_size = 0;
        pos->content_size_loc = NULL;
        pos->fd = mmap_info->fd;
-       pos->base = 0;
+       pos->base_mma = NULL;
        pos->offset = 0;
        pos->dummy = false;
        pos->cur_index = 0;
index b2433bd8cb719c384e104e0f23d2e08d60bc355d..7a7c323afc674bf689596b95a28fd5f7a76a01ad 100644 (file)
@@ -145,6 +145,7 @@ int ctf_float_read(struct stream_pos *ppos, struct definition *definition)
        struct definition *tmpdef;
        struct definition_float *tmpfloat;
        struct ctf_stream_pos destp;
+       struct mmap_align mma;
        int ret;
 
        switch (float_declaration->mantissa->len + 1) {
@@ -163,7 +164,8 @@ int ctf_float_read(struct stream_pos *ppos, struct definition *definition)
        }
        tmpfloat = container_of(tmpdef, struct definition_float, p);
        ctf_init_pos(&destp, -1, O_RDWR);
-       destp.base = (char *) u.bits;
+       mmap_align_set_addr(&mma, (char *) u.bits);
+       destp.base_mma = &mma;
        destp.packet_size = sizeof(u) * CHAR_BIT;
        ctf_align_pos(pos, float_declaration->p.alignment);
        ret = _ctf_float_copy(&destp.parent, tmpfloat, ppos, float_definition);
@@ -192,6 +194,7 @@ int ctf_float_write(struct stream_pos *ppos, struct definition *definition)
        struct definition *tmpdef;
        struct definition_float *tmpfloat;
        struct ctf_stream_pos srcp;
+       struct mmap_align mma;
        int ret;
 
        switch (float_declaration->mantissa->len + 1) {
@@ -210,7 +213,8 @@ int ctf_float_write(struct stream_pos *ppos, struct definition *definition)
        }
        tmpfloat = container_of(tmpdef, struct definition_float, p);
        ctf_init_pos(&srcp, -1, O_RDONLY);
-       srcp.base = (char *) u.bits;
+       mmap_align_set_addr(&mma, (char *) u.bits);
+       srcp.base_mma = &mma;
        srcp.packet_size = sizeof(u) * CHAR_BIT;
        switch (float_declaration->mantissa->len + 1) {
        case FLT_MANT_DIG:
index 5b56227d85d4d0ccfbd323aa52d3804c31d3682b..07c07be6e709aeb7eccd065fe17f02fc320a8b57 100644 (file)
@@ -223,20 +223,20 @@ int ctf_integer_read(struct stream_pos *ppos, struct definition *definition)
 
        if (!integer_declaration->signedness) {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
-                       bt_bitfield_read_le(pos->base, unsigned long,
+                       bt_bitfield_read_le(mmap_align_addr(pos->base_mma), unsigned long,
                                pos->offset, integer_declaration->len,
                                &integer_definition->value._unsigned);
                else
-                       bt_bitfield_read_be(pos->base, unsigned long,
+                       bt_bitfield_read_be(mmap_align_addr(pos->base_mma), unsigned long,
                                pos->offset, integer_declaration->len,
                                &integer_definition->value._unsigned);
        } else {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
-                       bt_bitfield_read_le(pos->base, unsigned long,
+                       bt_bitfield_read_le(mmap_align_addr(pos->base_mma), unsigned long,
                                pos->offset, integer_declaration->len,
                                &integer_definition->value._signed);
                else
-                       bt_bitfield_read_be(pos->base, unsigned long,
+                       bt_bitfield_read_be(mmap_align_addr(pos->base_mma), unsigned long,
                                pos->offset, integer_declaration->len,
                                &integer_definition->value._signed);
        }
@@ -266,20 +266,20 @@ int ctf_integer_write(struct stream_pos *ppos, struct definition *definition)
                goto end;
        if (!integer_declaration->signedness) {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
-                       bt_bitfield_write_le(pos->base, unsigned long,
+                       bt_bitfield_write_le(mmap_align_addr(pos->base_mma), unsigned long,
                                pos->offset, integer_declaration->len,
                                integer_definition->value._unsigned);
                else
-                       bt_bitfield_write_be(pos->base, unsigned long,
+                       bt_bitfield_write_be(mmap_align_addr(pos->base_mma), unsigned long,
                                pos->offset, integer_declaration->len,
                                integer_definition->value._unsigned);
        } else {
                if (integer_declaration->byte_order == LITTLE_ENDIAN)
-                       bt_bitfield_write_le(pos->base, unsigned long,
+                       bt_bitfield_write_le(mmap_align_addr(pos->base_mma), unsigned long,
                                pos->offset, integer_declaration->len,
                                integer_definition->value._signed);
                else
-                       bt_bitfield_write_be(pos->base, unsigned long,
+                       bt_bitfield_write_be(mmap_align_addr(pos->base_mma), unsigned long,
                                pos->offset, integer_declaration->len,
                                integer_definition->value._signed);
        }
index 14a7e3d4b0fedc51408e7f3a5141506f8952cfdb..3b9039e037c13009aad1cb01d6d68327d35c4408 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <sys/mman.h>
 #include <errno.h>
 #include <stdint.h>
 #include <unistd.h>
 #include <glib.h>
 #include <stdio.h>
+#include <babeltrace/mmap-align.h>
 
 struct bt_stream_callbacks;
 
@@ -59,7 +59,7 @@ struct ctf_stream_pos {
        size_t packet_size;     /* current packet size, in bits */
        size_t content_size;    /* current content size, in bits */
        uint32_t *content_size_loc; /* pointer to current content size */
-       char *base;             /* mmap base address */
+       struct mmap_align *base_mma;/* mmap base address */
        ssize_t offset;         /* offset from base, in bits. EOF for end of file. */
        ssize_t last_offset;    /* offset before the last read_event */
        size_t cur_index;       /* current index in packet index */
@@ -145,7 +145,7 @@ char *ctf_get_pos_addr(struct ctf_stream_pos *pos)
 {
        /* Only makes sense to get the address after aligning on CHAR_BIT */
        assert(!(pos->offset % CHAR_BIT));
-       return pos->base + (pos->offset / CHAR_BIT);
+       return mmap_align_addr(pos->base_mma) + (pos->offset / CHAR_BIT);
 }
 
 static inline
This page took 0.042066 seconds and 4 git commands to generate.