From aee35fcc7e82d20396d82d151de93b1b51325398 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Tue, 29 May 2012 18:54:43 -0400 Subject: [PATCH] Use mmap_align Allow reading traces with packet size different from the machine page size. Fixes #237 Signed-off-by: Mathieu Desnoyers --- formats/ctf/ctf.c | 29 +++++++++++++++-------------- formats/ctf/types/float.c | 8 ++++++-- formats/ctf/types/integer.c | 16 ++++++++-------- include/babeltrace/ctf/types.h | 6 +++--- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 1ba7e949..e3a487d6 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -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; diff --git a/formats/ctf/types/float.c b/formats/ctf/types/float.c index b2433bd8..7a7c323a 100644 --- a/formats/ctf/types/float.c +++ b/formats/ctf/types/float.c @@ -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: diff --git a/formats/ctf/types/integer.c b/formats/ctf/types/integer.c index 5b56227d..07c07be6 100644 --- a/formats/ctf/types/integer.c +++ b/formats/ctf/types/integer.c @@ -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); } diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index 14a7e3d4..3b9039e0 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -24,12 +24,12 @@ #include #include #include -#include #include #include #include #include #include +#include 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 -- 2.34.1