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