2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "CTFSER"
29 #include <sys/types.h>
32 #include <babeltrace/assert-internal.h>
40 #include <babeltrace/babeltrace-internal.h>
41 #include <babeltrace/common-internal.h>
42 #include <babeltrace/ctfser-internal.h>
43 #include <babeltrace/compat/unistd-internal.h>
44 #include <babeltrace/compat/fcntl-internal.h>
47 uint64_t get_packet_size_increment_bytes(void)
49 return bt_common_get_page_size() * 8;
53 void mmap_align_ctfser(struct bt_ctfser
*ctfser
)
55 ctfser
->base_mma
= mmap_align(ctfser
->cur_packet_size_bytes
, PROT_WRITE
,
56 MAP_SHARED
, ctfser
->fd
, ctfser
->mmap_offset
);
60 int _bt_ctfser_increase_cur_packet_size(struct bt_ctfser
*ctfser
)
65 BT_LOGV("Increasing stream file's current packet size: "
66 "path=\"%s\", fd=%d, "
67 "offset-in-cur-packet-bits=%" PRIu64
", "
68 "cur-packet-size-bytes=%" PRIu64
,
69 ctfser
->path
->str
, ctfser
->fd
,
70 ctfser
->offset_in_cur_packet_bits
,
71 ctfser
->cur_packet_size_bytes
);
72 ret
= munmap_align(ctfser
->base_mma
);
74 BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping",
79 ctfser
->cur_packet_size_bytes
+= get_packet_size_increment_bytes();
82 ret
= bt_posix_fallocate(ctfser
->fd
, ctfser
->mmap_offset
,
83 ctfser
->cur_packet_size_bytes
);
84 } while (ret
== EINTR
);
87 BT_LOGE("Failed to preallocate memory space: ret=%d", ret
);
91 mmap_align_ctfser(ctfser
);
92 if (ctfser
->base_mma
== MAP_FAILED
) {
93 BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
99 BT_LOGV("Increased packet size: "
100 "path=\"%s\", fd=%d, "
101 "offset-in-cur-packet-bits=%" PRIu64
", "
102 "new-packet-size-bytes=%" PRIu64
,
103 ctfser
->path
->str
, ctfser
->fd
,
104 ctfser
->offset_in_cur_packet_bits
,
105 ctfser
->cur_packet_size_bytes
);
112 int bt_ctfser_init(struct bt_ctfser
*ctfser
, const char *path
)
117 memset(ctfser
, 0, sizeof(*ctfser
));
118 ctfser
->fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
,
119 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
120 if (ctfser
->fd
< 0) {
121 BT_LOGW_ERRNO("Failed to open stream file for writing",
122 ": path=\"%s\", ret=%d",
128 ctfser
->path
= g_string_new(path
);
135 int bt_ctfser_fini(struct bt_ctfser
*ctfser
)
139 if (ctfser
->fd
== -1) {
144 * Truncate the stream file's size to the minimum required to
145 * fit the last packet as we might have grown it too much during
146 * the last memory map.
149 ret
= ftruncate(ctfser
->fd
, ctfser
->stream_size_bytes
);
150 } while (ret
== -1 && errno
== EINTR
);
153 BT_LOGE_ERRNO("Failed to truncate stream file",
154 ": ret=%d, size-bytes=%" PRIu64
,
155 ret
, ctfser
->stream_size_bytes
);
159 if (ctfser
->base_mma
) {
161 ret
= munmap_align(ctfser
->base_mma
);
163 BT_LOGE_ERRNO("Failed to unmap stream file",
164 ": ret=%d, size-bytes=%" PRIu64
,
165 ret
, ctfser
->stream_size_bytes
);
169 ctfser
->base_mma
= NULL
;
172 ret
= close(ctfser
->fd
);
174 BT_LOGE_ERRNO("Failed to close stream file",
183 g_string_free(ctfser
->path
, TRUE
);
192 int bt_ctfser_open_packet(struct bt_ctfser
*ctfser
)
196 BT_LOGV("Opening packet: path=\"%s\", fd=%d, "
197 "prev-packet-size-bytes=%" PRIu64
,
198 ctfser
->path
->str
, ctfser
->fd
,
199 ctfser
->prev_packet_size_bytes
);
201 if (ctfser
->base_mma
) {
202 /* Unmap old base (previous packet) */
203 ret
= munmap_align(ctfser
->base_mma
);
205 BT_LOGE_ERRNO("Failed to unmap stream file",
206 ": ret=%d, size-bytes=%" PRIu64
,
207 ret
, ctfser
->stream_size_bytes
);
211 ctfser
->base_mma
= NULL
;
215 * Add the previous packet's size to the memory map address
216 * offset to start writing immediately after it.
218 ctfser
->mmap_offset
+= ctfser
->prev_packet_size_bytes
;
219 ctfser
->prev_packet_size_bytes
= 0;
221 /* Make initial space for the current packet */
222 ctfser
->cur_packet_size_bytes
= get_packet_size_increment_bytes();
225 ret
= bt_posix_fallocate(ctfser
->fd
, ctfser
->mmap_offset
,
226 ctfser
->cur_packet_size_bytes
);
227 } while (ret
== EINTR
);
230 BT_LOGE("Failed to preallocate memory space: ret=%d", ret
);
234 /* Start writing at the beginning of the current packet */
235 ctfser
->offset_in_cur_packet_bits
= 0;
237 /* Get new base address */
238 mmap_align_ctfser(ctfser
);
239 if (ctfser
->base_mma
== MAP_FAILED
) {
240 BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
246 BT_LOGV("Opened packet: path=\"%s\", fd=%d, "
247 "cur-packet-size-bytes=%" PRIu64
,
248 ctfser
->path
->str
, ctfser
->fd
,
249 ctfser
->cur_packet_size_bytes
);
256 void bt_ctfser_close_current_packet(struct bt_ctfser
*ctfser
,
257 uint64_t packet_size_bytes
)
259 BT_LOGV("Closing packet: path=\"%s\", fd=%d, "
260 "offset-in-cur-packet-bits=%" PRIu64
261 "cur-packet-size-bytes=%" PRIu64
,
262 ctfser
->path
->str
, ctfser
->fd
,
263 ctfser
->offset_in_cur_packet_bits
,
264 ctfser
->cur_packet_size_bytes
);
267 * This will be used during the next call to
268 * bt_ctfser_open_packet(): we add
269 * `ctfser->prev_packet_size_bytes` to the current memory map
270 * address offset (first byte of _this_ packet), effectively
271 * making _this_ packet the required size.
273 ctfser
->prev_packet_size_bytes
= packet_size_bytes
;
274 ctfser
->stream_size_bytes
+= packet_size_bytes
;
275 BT_LOGV("Closed packet: path=\"%s\", fd=%d, "
276 "stream-file-size-bytes=%" PRIu64
,
277 ctfser
->path
->str
, ctfser
->fd
,
278 ctfser
->stream_size_bytes
);