tap-driver.sh: flush stdout after each test result
[babeltrace.git] / 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_TAG "CTFSER"
24 #include "logging.h"
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <inttypes.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <babeltrace2/assert-internal.h>
33 #include <stdarg.h>
34 #include <ctype.h>
35 #include <glib.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <wchar.h>
39 #include <stdbool.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>
45
46 static inline
47 uint64_t get_packet_size_increment_bytes(void)
48 {
49 return bt_common_get_page_size() * 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);
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_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);
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
82 do {
83 ret = bt_posix_fallocate(ctfser->fd, ctfser->mmap_offset,
84 ctfser->cur_packet_size_bytes);
85 } while (ret == EINTR);
86
87 if (ret) {
88 BT_LOGE("Failed to preallocate memory space: ret=%d", ret);
89 goto end;
90 }
91
92 mmap_align_ctfser(ctfser);
93 if (ctfser->base_mma == MAP_FAILED) {
94 BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
95 ": ret=%d", ret);
96 ret = -1;
97 goto end;
98 }
99
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);
107
108 end:
109 return ret;
110 }
111
112 BT_HIDDEN
113 int bt_ctfser_init(struct bt_ctfser *ctfser, const char *path)
114 {
115 int ret = 0;
116
117 BT_ASSERT(ctfser);
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",
124 path, ctfser->fd);
125 ret = -1;
126 goto end;
127 }
128
129 ctfser->path = g_string_new(path);
130
131 end:
132 return ret;
133 }
134
135 BT_HIDDEN
136 int bt_ctfser_fini(struct bt_ctfser *ctfser)
137 {
138 int ret = 0;
139
140 if (ctfser->fd == -1) {
141 goto free_path;
142 }
143
144 /*
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.
148 */
149 do {
150 ret = ftruncate(ctfser->fd, ctfser->stream_size_bytes);
151 } while (ret == -1 && errno == EINTR);
152
153 if (ret) {
154 BT_LOGE_ERRNO("Failed to truncate stream file",
155 ": ret=%d, size-bytes=%" PRIu64,
156 ret, ctfser->stream_size_bytes);
157 goto end;
158 }
159
160 if (ctfser->base_mma) {
161 /* Unmap old base */
162 ret = munmap_align(ctfser->base_mma);
163 if (ret) {
164 BT_LOGE_ERRNO("Failed to unmap stream file",
165 ": ret=%d, size-bytes=%" PRIu64,
166 ret, ctfser->stream_size_bytes);
167 goto end;
168 }
169
170 ctfser->base_mma = NULL;
171 }
172
173 ret = close(ctfser->fd);
174 if (ret) {
175 BT_LOGE_ERRNO("Failed to close stream file",
176 ": ret=%d", ret);
177 goto end;
178 }
179
180 ctfser->fd = -1;
181
182 free_path:
183 if (ctfser->path) {
184 g_string_free(ctfser->path, TRUE);
185 ctfser->path = NULL;
186 }
187
188 end:
189 return ret;
190 }
191
192 BT_HIDDEN
193 int bt_ctfser_open_packet(struct bt_ctfser *ctfser)
194 {
195 int ret = 0;
196
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);
201
202 if (ctfser->base_mma) {
203 /* Unmap old base (previous packet) */
204 ret = munmap_align(ctfser->base_mma);
205 if (ret) {
206 BT_LOGE_ERRNO("Failed to unmap stream file",
207 ": ret=%d, size-bytes=%" PRIu64,
208 ret, ctfser->stream_size_bytes);
209 goto end;
210 }
211
212 ctfser->base_mma = NULL;
213 }
214
215 /*
216 * Add the previous packet's size to the memory map address
217 * offset to start writing immediately after it.
218 */
219 ctfser->mmap_offset += ctfser->prev_packet_size_bytes;
220 ctfser->prev_packet_size_bytes = 0;
221
222 /* Make initial space for the current packet */
223 ctfser->cur_packet_size_bytes = get_packet_size_increment_bytes();
224
225 do {
226 ret = bt_posix_fallocate(ctfser->fd, ctfser->mmap_offset,
227 ctfser->cur_packet_size_bytes);
228 } while (ret == EINTR);
229
230 if (ret) {
231 BT_LOGE("Failed to preallocate memory space: ret=%d", ret);
232 goto end;
233 }
234
235 /* Start writing at the beginning of the current packet */
236 ctfser->offset_in_cur_packet_bits = 0;
237
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",
242 ": ret=%d", ret);
243 ret = -1;
244 goto end;
245 }
246
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);
251
252 end:
253 return ret;
254 }
255
256 BT_HIDDEN
257 void bt_ctfser_close_current_packet(struct bt_ctfser *ctfser,
258 uint64_t packet_size_bytes)
259 {
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);
266
267 /*
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.
273 */
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);
280 }
This page took 0.033802 seconds and 4 git commands to generate.