0806c8bab73899da5e4e40939d52623b107bc92e
[babeltrace.git] / src / ctfser / ctfser.c
1 /*
2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_OUTPUT_LEVEL (ctfser->log_level)
24 #define BT_LOG_TAG "CTFSER"
25 #include "logging/log.h"
26
27 #include <unistd.h>
28 #include <string.h>
29 #include <inttypes.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include "common/assert.h"
34 #include <stdarg.h>
35 #include <ctype.h>
36 #include <glib.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <wchar.h>
40 #include "common/macros.h"
41 #include "common/common.h"
42 #include "ctfser/ctfser.h"
43 #include "compat/unistd.h"
44 #include "compat/fcntl.h"
45
46 static inline
47 uint64_t get_packet_size_increment_bytes(struct bt_ctfser *ctfser)
48 {
49 return bt_common_get_page_size(ctfser->log_level) * 8;
50 }
51
52 static inline
53 void mmap_align_ctfser(struct bt_ctfser *ctfser)
54 {
55 ctfser->base_mma = mmap_align(ctfser->cur_packet_size_bytes,
56 PROT_READ | PROT_WRITE,
57 MAP_SHARED, ctfser->fd, ctfser->mmap_offset, ctfser->log_level);
58 }
59
60 BT_HIDDEN
61 int _bt_ctfser_increase_cur_packet_size(struct bt_ctfser *ctfser)
62 {
63 int ret;
64
65 BT_ASSERT(ctfser);
66 BT_LOGD("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);
74 if (ret) {
75 BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping",
76 ": ret=%d", ret);
77 goto end;
78 }
79
80 ctfser->cur_packet_size_bytes += get_packet_size_increment_bytes(
81 ctfser);
82
83 do {
84 ret = bt_posix_fallocate(ctfser->fd, ctfser->mmap_offset,
85 ctfser->cur_packet_size_bytes);
86 } while (ret == EINTR);
87
88 if (ret) {
89 BT_LOGE("Failed to preallocate memory space: ret=%d", ret);
90 goto end;
91 }
92
93 mmap_align_ctfser(ctfser);
94 if (ctfser->base_mma == MAP_FAILED) {
95 BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
96 ": ret=%d", ret);
97 ret = -1;
98 goto end;
99 }
100
101 BT_LOGD("Increased packet size: "
102 "path=\"%s\", fd=%d, "
103 "offset-in-cur-packet-bits=%" PRIu64 ", "
104 "new-packet-size-bytes=%" PRIu64,
105 ctfser->path->str, ctfser->fd,
106 ctfser->offset_in_cur_packet_bits,
107 ctfser->cur_packet_size_bytes);
108
109 end:
110 return ret;
111 }
112
113 BT_HIDDEN
114 int bt_ctfser_init(struct bt_ctfser *ctfser, const char *path, int log_level)
115 {
116 int ret = 0;
117
118 BT_ASSERT(ctfser);
119 memset(ctfser, 0, sizeof(*ctfser));
120 ctfser->fd = open(path, O_RDWR | O_CREAT | O_TRUNC,
121 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
122 ctfser->log_level = log_level;
123 if (ctfser->fd < 0) {
124 BT_LOGW_ERRNO("Failed to open stream file for writing",
125 ": path=\"%s\", ret=%d",
126 path, ctfser->fd);
127 ret = -1;
128 goto end;
129 }
130
131 ctfser->path = g_string_new(path);
132
133 end:
134 return ret;
135 }
136
137 BT_HIDDEN
138 int bt_ctfser_fini(struct bt_ctfser *ctfser)
139 {
140 int ret = 0;
141
142 if (ctfser->fd == -1) {
143 goto free_path;
144 }
145
146 if (ctfser->base_mma) {
147 /* Unmap old base */
148 ret = munmap_align(ctfser->base_mma);
149 if (ret) {
150 BT_LOGE_ERRNO("Failed to unmap stream file",
151 ": ret=%d, size-bytes=%" PRIu64,
152 ret, ctfser->stream_size_bytes);
153 goto end;
154 }
155
156 ctfser->base_mma = NULL;
157 }
158
159 /*
160 * Truncate the stream file's size to the minimum required to
161 * fit the last packet as we might have grown it too much during
162 * the last memory map.
163 */
164 do {
165 ret = ftruncate(ctfser->fd, ctfser->stream_size_bytes);
166 } while (ret == -1 && errno == EINTR);
167
168 if (ret) {
169 BT_LOGE_ERRNO("Failed to truncate stream file",
170 ": ret=%d, size-bytes=%" PRIu64,
171 ret, ctfser->stream_size_bytes);
172 goto end;
173 }
174
175
176 ret = close(ctfser->fd);
177 if (ret) {
178 BT_LOGE_ERRNO("Failed to close stream file",
179 ": ret=%d", ret);
180 goto end;
181 }
182
183 ctfser->fd = -1;
184
185 free_path:
186 if (ctfser->path) {
187 g_string_free(ctfser->path, TRUE);
188 ctfser->path = NULL;
189 }
190
191 end:
192 return ret;
193 }
194
195 BT_HIDDEN
196 int bt_ctfser_open_packet(struct bt_ctfser *ctfser)
197 {
198 int ret = 0;
199
200 BT_LOGD("Opening packet: path=\"%s\", fd=%d, "
201 "prev-packet-size-bytes=%" PRIu64,
202 ctfser->path->str, ctfser->fd,
203 ctfser->prev_packet_size_bytes);
204
205 if (ctfser->base_mma) {
206 /* Unmap old base (previous packet) */
207 ret = munmap_align(ctfser->base_mma);
208 if (ret) {
209 BT_LOGE_ERRNO("Failed to unmap stream file",
210 ": ret=%d, size-bytes=%" PRIu64,
211 ret, ctfser->stream_size_bytes);
212 goto end;
213 }
214
215 ctfser->base_mma = NULL;
216 }
217
218 /*
219 * Add the previous packet's size to the memory map address
220 * offset to start writing immediately after it.
221 */
222 ctfser->mmap_offset += ctfser->prev_packet_size_bytes;
223 ctfser->prev_packet_size_bytes = 0;
224
225 /* Make initial space for the current packet */
226 ctfser->cur_packet_size_bytes = get_packet_size_increment_bytes(
227 ctfser);
228
229 do {
230 ret = bt_posix_fallocate(ctfser->fd, ctfser->mmap_offset,
231 ctfser->cur_packet_size_bytes);
232 } while (ret == EINTR);
233
234 if (ret) {
235 BT_LOGE("Failed to preallocate memory space: ret=%d", ret);
236 goto end;
237 }
238
239 /* Start writing at the beginning of the current packet */
240 ctfser->offset_in_cur_packet_bits = 0;
241
242 /* Get new base address */
243 mmap_align_ctfser(ctfser);
244 if (ctfser->base_mma == MAP_FAILED) {
245 BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
246 ": ret=%d", ret);
247 ret = -1;
248 goto end;
249 }
250
251 BT_LOGD("Opened packet: path=\"%s\", fd=%d, "
252 "cur-packet-size-bytes=%" PRIu64,
253 ctfser->path->str, ctfser->fd,
254 ctfser->cur_packet_size_bytes);
255
256 end:
257 return ret;
258 }
259
260 BT_HIDDEN
261 void bt_ctfser_close_current_packet(struct bt_ctfser *ctfser,
262 uint64_t packet_size_bytes)
263 {
264 BT_LOGD("Closing packet: path=\"%s\", fd=%d, "
265 "offset-in-cur-packet-bits=%" PRIu64
266 "cur-packet-size-bytes=%" PRIu64,
267 ctfser->path->str, ctfser->fd,
268 ctfser->offset_in_cur_packet_bits,
269 ctfser->cur_packet_size_bytes);
270
271 /*
272 * This will be used during the next call to
273 * bt_ctfser_open_packet(): we add
274 * `ctfser->prev_packet_size_bytes` to the current memory map
275 * address offset (first byte of _this_ packet), effectively
276 * making _this_ packet the required size.
277 */
278 ctfser->prev_packet_size_bytes = packet_size_bytes;
279 ctfser->stream_size_bytes += packet_size_bytes;
280 BT_LOGD("Closed packet: path=\"%s\", fd=%d, "
281 "stream-file-size-bytes=%" PRIu64,
282 ctfser->path->str, ctfser->fd,
283 ctfser->stream_size_bytes);
284 }
This page took 0.038817 seconds and 3 git commands to generate.