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 <babeltrace2/assert-internal.h>
40 #include <babeltrace2/babeltrace-internal.h>
41 #include <babeltrace2/common-internal.h>
42 #include <babeltrace2/ctfser-internal.h>
43 #include <babeltrace2/compat/unistd-internal.h>
44 #include <babeltrace2/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
,
56 PROT_READ
| PROT_WRITE
,
57 MAP_SHARED
, ctfser
->fd
, ctfser
->mmap_offset
);
61 int _bt_ctfser_increase_cur_packet_size(struct bt_ctfser
*ctfser
)
66 BT_LOGV("Increasing stream file's current packet size: "
67 "path=\"%s\", fd=%d, "
68 "offset-in-cur-packet-bits=%" PRIu64
", "
69 "cur-packet-size-bytes=%" PRIu64
,
70 ctfser
->path
->str
, ctfser
->fd
,
71 ctfser
->offset_in_cur_packet_bits
,
72 ctfser
->cur_packet_size_bytes
);
73 ret
= munmap_align(ctfser
->base_mma
);
75 BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping",
80 ctfser
->cur_packet_size_bytes
+= get_packet_size_increment_bytes();
83 ret
= bt_posix_fallocate(ctfser
->fd
, ctfser
->mmap_offset
,
84 ctfser
->cur_packet_size_bytes
);
85 } while (ret
== EINTR
);
88 BT_LOGE("Failed to preallocate memory space: ret=%d", ret
);
92 mmap_align_ctfser(ctfser
);
93 if (ctfser
->base_mma
== MAP_FAILED
) {
94 BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
100 BT_LOGV("Increased packet size: "
101 "path=\"%s\", fd=%d, "
102 "offset-in-cur-packet-bits=%" PRIu64
", "
103 "new-packet-size-bytes=%" PRIu64
,
104 ctfser
->path
->str
, ctfser
->fd
,
105 ctfser
->offset_in_cur_packet_bits
,
106 ctfser
->cur_packet_size_bytes
);
113 int bt_ctfser_init(struct bt_ctfser
*ctfser
, const char *path
)
118 memset(ctfser
, 0, sizeof(*ctfser
));
119 ctfser
->fd
= open(path
, O_RDWR
| O_CREAT
| O_TRUNC
,
120 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
121 if (ctfser
->fd
< 0) {
122 BT_LOGW_ERRNO("Failed to open stream file for writing",
123 ": path=\"%s\", ret=%d",
129 ctfser
->path
= g_string_new(path
);
136 int bt_ctfser_fini(struct bt_ctfser
*ctfser
)
140 if (ctfser
->fd
== -1) {
145 * Truncate the stream file's size to the minimum required to
146 * fit the last packet as we might have grown it too much during
147 * the last memory map.
150 ret
= ftruncate(ctfser
->fd
, ctfser
->stream_size_bytes
);
151 } while (ret
== -1 && errno
== EINTR
);
154 BT_LOGE_ERRNO("Failed to truncate stream file",
155 ": ret=%d, size-bytes=%" PRIu64
,
156 ret
, ctfser
->stream_size_bytes
);
160 if (ctfser
->base_mma
) {
162 ret
= munmap_align(ctfser
->base_mma
);
164 BT_LOGE_ERRNO("Failed to unmap stream file",
165 ": ret=%d, size-bytes=%" PRIu64
,
166 ret
, ctfser
->stream_size_bytes
);
170 ctfser
->base_mma
= NULL
;
173 ret
= close(ctfser
->fd
);
175 BT_LOGE_ERRNO("Failed to close stream file",
184 g_string_free(ctfser
->path
, TRUE
);
193 int bt_ctfser_open_packet(struct bt_ctfser
*ctfser
)
197 BT_LOGV("Opening packet: path=\"%s\", fd=%d, "
198 "prev-packet-size-bytes=%" PRIu64
,
199 ctfser
->path
->str
, ctfser
->fd
,
200 ctfser
->prev_packet_size_bytes
);
202 if (ctfser
->base_mma
) {
203 /* Unmap old base (previous packet) */
204 ret
= munmap_align(ctfser
->base_mma
);
206 BT_LOGE_ERRNO("Failed to unmap stream file",
207 ": ret=%d, size-bytes=%" PRIu64
,
208 ret
, ctfser
->stream_size_bytes
);
212 ctfser
->base_mma
= NULL
;
216 * Add the previous packet's size to the memory map address
217 * offset to start writing immediately after it.
219 ctfser
->mmap_offset
+= ctfser
->prev_packet_size_bytes
;
220 ctfser
->prev_packet_size_bytes
= 0;
222 /* Make initial space for the current packet */
223 ctfser
->cur_packet_size_bytes
= get_packet_size_increment_bytes();
226 ret
= bt_posix_fallocate(ctfser
->fd
, ctfser
->mmap_offset
,
227 ctfser
->cur_packet_size_bytes
);
228 } while (ret
== EINTR
);
231 BT_LOGE("Failed to preallocate memory space: ret=%d", ret
);
235 /* Start writing at the beginning of the current packet */
236 ctfser
->offset_in_cur_packet_bits
= 0;
238 /* Get new base address */
239 mmap_align_ctfser(ctfser
);
240 if (ctfser
->base_mma
== MAP_FAILED
) {
241 BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
247 BT_LOGV("Opened packet: path=\"%s\", fd=%d, "
248 "cur-packet-size-bytes=%" PRIu64
,
249 ctfser
->path
->str
, ctfser
->fd
,
250 ctfser
->cur_packet_size_bytes
);
257 void bt_ctfser_close_current_packet(struct bt_ctfser
*ctfser
,
258 uint64_t packet_size_bytes
)
260 BT_LOGV("Closing packet: path=\"%s\", fd=%d, "
261 "offset-in-cur-packet-bits=%" PRIu64
262 "cur-packet-size-bytes=%" PRIu64
,
263 ctfser
->path
->str
, ctfser
->fd
,
264 ctfser
->offset_in_cur_packet_bits
,
265 ctfser
->cur_packet_size_bytes
);
268 * This will be used during the next call to
269 * bt_ctfser_open_packet(): we add
270 * `ctfser->prev_packet_size_bytes` to the current memory map
271 * address offset (first byte of _this_ packet), effectively
272 * making _this_ packet the required size.
274 ctfser
->prev_packet_size_bytes
= packet_size_bytes
;
275 ctfser
->stream_size_bytes
+= packet_size_bytes
;
276 BT_LOGV("Closed packet: path=\"%s\", fd=%d, "
277 "stream-file-size-bytes=%" PRIu64
,
278 ctfser
->path
->str
, ctfser
->fd
,
279 ctfser
->stream_size_bytes
);