Writer: minimize packet padding
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 27 Jan 2017 04:19:40 +0000 (23:19 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 28 May 2017 16:57:36 +0000 (12:57 -0400)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
formats/ctf/ir/stream.c
include/babeltrace/ctf-ir/stream-internal.h

index 16bb0adad187be70e590fc566c5b9f61235c8acc..979d40e92839fd94f066bad1d8d226e5153e170b 100644 (file)
@@ -873,6 +873,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
        struct bt_ctf_field *integer = NULL;
        struct ctf_stream_pos packet_context_pos;
        bool empty_packet;
+       uint64_t packet_size_bits;
 
        if (!stream || stream->pos.fd < 0) {
                /*
@@ -993,6 +994,11 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                }
        }
 
+       /* Rounded-up in case content_size is not byte-aligned. */
+       packet_size_bits = (stream->pos.offset + (CHAR_BIT - 1)) &
+               ~(CHAR_BIT - 1);
+       stream->pos.packet_size = packet_size_bits;
+
        if (stream->packet_context) {
                /*
                 * Update the packet total size and content size and overwrite
@@ -1008,7 +1014,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                }
 
                ret = set_structure_field_integer(stream->packet_context,
-                       "packet_size", stream->pos.packet_size);
+                       "packet_size", packet_size_bits);
                if (ret) {
                        goto end;
                }
@@ -1022,6 +1028,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
 
        g_ptr_array_set_size(stream->events, 0);
        stream->flushed_packet_count++;
+       stream->size += packet_size_bits / CHAR_BIT;
 end:
        bt_put(integer);
        return ret;
@@ -1044,8 +1051,24 @@ void bt_ctf_stream_destroy(struct bt_object *obj)
 
        stream = container_of(obj, struct bt_ctf_stream, base);
        ctf_fini_pos(&stream->pos);
-       if (stream->pos.fd >= 0 && close(stream->pos.fd)) {
-               perror("close");
+       if (stream->pos.fd >= 0) {
+               int ret;
+
+               /*
+                * Truncate the file's size to the minimum required to fit the
+                * last packet as we might have grown it too much on the last
+                * mmap.
+                */
+               do {
+                       ret = ftruncate(stream->pos.fd, stream->size);
+               } while (ret == -1 && errno == EINTR);
+               if (ret) {
+                       perror("ftruncate");
+               }
+
+               if (close(stream->pos.fd)) {
+                       perror("close");
+               }
        }
 
        if (stream->events) {
index a39b1529108bfdbd511ab8162a0815f8c9c1548a..0e0e2498b4d43bf40c0303074a16cb845885b060 100644 (file)
@@ -40,13 +40,16 @@ struct bt_ctf_stream {
        struct bt_object base;
        uint32_t id;
        struct bt_ctf_stream_class *stream_class;
+       GString *name;
+       struct bt_ctf_field *packet_header;
+       struct bt_ctf_field *packet_context;
+
+       /* Writer-specific members. */
        /* Array of pointers to bt_ctf_event for the current packet */
        GPtrArray *events;
        struct ctf_stream_pos pos;
        unsigned int flushed_packet_count;
-       GString *name;
-       struct bt_ctf_field *packet_header;
-       struct bt_ctf_field *packet_context;
+       uint64_t size;
 };
 
 BT_HIDDEN
This page took 0.027045 seconds and 4 git commands to generate.