Commit | Line | Data |
---|---|---|
6f4eefbe PP |
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 <babeltrace/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 <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> | |
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, PROT_WRITE, | |
56 | MAP_SHARED, ctfser->fd, ctfser->mmap_offset); | |
57 | } | |
58 | ||
59 | BT_HIDDEN | |
60 | int _bt_ctfser_increase_cur_packet_size(struct bt_ctfser *ctfser) | |
61 | { | |
62 | int ret; | |
63 | ||
64 | BT_ASSERT(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); | |
73 | if (ret) { | |
74 | BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping", | |
75 | ": ret=%d", ret); | |
76 | goto end; | |
77 | } | |
78 | ||
79 | ctfser->cur_packet_size_bytes += get_packet_size_increment_bytes(); | |
80 | ||
81 | do { | |
82 | ret = bt_posix_fallocate(ctfser->fd, ctfser->mmap_offset, | |
83 | ctfser->cur_packet_size_bytes); | |
84 | } while (ret == EINTR); | |
85 | ||
86 | if (ret) { | |
87 | BT_LOGE("Failed to preallocate memory space: ret=%d", ret); | |
88 | goto end; | |
89 | } | |
90 | ||
91 | mmap_align_ctfser(ctfser); | |
92 | if (ctfser->base_mma == MAP_FAILED) { | |
93 | BT_LOGE_ERRNO("Failed to perform an aligned memory mapping", | |
94 | ": ret=%d", ret); | |
95 | ret = -1; | |
96 | goto end; | |
97 | } | |
98 | ||
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); | |
106 | ||
107 | end: | |
108 | return ret; | |
109 | } | |
110 | ||
111 | BT_HIDDEN | |
112 | int bt_ctfser_init(struct bt_ctfser *ctfser, const char *path) | |
113 | { | |
114 | int ret = 0; | |
115 | ||
116 | BT_ASSERT(ctfser); | |
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", | |
123 | path, ctfser->fd); | |
124 | ret = -1; | |
125 | goto end; | |
126 | } | |
127 | ||
128 | ctfser->path = g_string_new(path); | |
129 | ||
130 | end: | |
131 | return ret; | |
132 | } | |
133 | ||
134 | BT_HIDDEN | |
135 | int bt_ctfser_fini(struct bt_ctfser *ctfser) | |
136 | { | |
137 | int ret = 0; | |
138 | ||
139 | if (ctfser->fd == -1) { | |
140 | goto free_path; | |
141 | } | |
142 | ||
143 | /* | |
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. | |
147 | */ | |
148 | do { | |
149 | ret = ftruncate(ctfser->fd, ctfser->stream_size_bytes); | |
150 | } while (ret == -1 && errno == EINTR); | |
151 | ||
152 | if (ret) { | |
153 | BT_LOGE_ERRNO("Failed to truncate stream file", | |
154 | ": ret=%d, size-bytes=%" PRIu64, | |
155 | ret, ctfser->stream_size_bytes); | |
156 | goto end; | |
157 | } | |
158 | ||
159 | if (ctfser->base_mma) { | |
160 | /* Unmap old base */ | |
161 | ret = munmap_align(ctfser->base_mma); | |
162 | if (ret) { | |
163 | BT_LOGE_ERRNO("Failed to unmap stream file", | |
164 | ": ret=%d, size-bytes=%" PRIu64, | |
165 | ret, ctfser->stream_size_bytes); | |
166 | goto end; | |
167 | } | |
168 | ||
169 | ctfser->base_mma = NULL; | |
170 | } | |
171 | ||
172 | ret = close(ctfser->fd); | |
173 | if (ret) { | |
174 | BT_LOGE_ERRNO("Failed to close stream file", | |
175 | ": ret=%d", ret); | |
176 | goto end; | |
177 | } | |
178 | ||
179 | ctfser->fd = -1; | |
180 | ||
181 | free_path: | |
182 | if (ctfser->path) { | |
183 | g_string_free(ctfser->path, TRUE); | |
184 | ctfser->path = NULL; | |
185 | } | |
186 | ||
187 | end: | |
188 | return ret; | |
189 | } | |
190 | ||
191 | BT_HIDDEN | |
192 | int bt_ctfser_open_packet(struct bt_ctfser *ctfser) | |
193 | { | |
194 | int ret = 0; | |
195 | ||
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); | |
200 | ||
201 | if (ctfser->base_mma) { | |
202 | /* Unmap old base (previous packet) */ | |
203 | ret = munmap_align(ctfser->base_mma); | |
204 | if (ret) { | |
205 | BT_LOGE_ERRNO("Failed to unmap stream file", | |
206 | ": ret=%d, size-bytes=%" PRIu64, | |
207 | ret, ctfser->stream_size_bytes); | |
208 | goto end; | |
209 | } | |
210 | ||
211 | ctfser->base_mma = NULL; | |
212 | } | |
213 | ||
214 | /* | |
215 | * Add the previous packet's size to the memory map address | |
216 | * offset to start writing immediately after it. | |
217 | */ | |
218 | ctfser->mmap_offset += ctfser->prev_packet_size_bytes; | |
219 | ctfser->prev_packet_size_bytes = 0; | |
220 | ||
221 | /* Make initial space for the current packet */ | |
222 | ctfser->cur_packet_size_bytes = get_packet_size_increment_bytes(); | |
223 | ||
224 | do { | |
225 | ret = bt_posix_fallocate(ctfser->fd, ctfser->mmap_offset, | |
226 | ctfser->cur_packet_size_bytes); | |
227 | } while (ret == EINTR); | |
228 | ||
229 | if (ret) { | |
230 | BT_LOGE("Failed to preallocate memory space: ret=%d", ret); | |
231 | goto end; | |
232 | } | |
233 | ||
234 | /* Start writing at the beginning of the current packet */ | |
235 | ctfser->offset_in_cur_packet_bits = 0; | |
236 | ||
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", | |
241 | ": ret=%d", ret); | |
242 | ret = -1; | |
243 | goto end; | |
244 | } | |
245 | ||
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); | |
250 | ||
251 | end: | |
252 | return ret; | |
253 | } | |
254 | ||
255 | BT_HIDDEN | |
256 | void bt_ctfser_close_current_packet(struct bt_ctfser *ctfser, | |
257 | uint64_t packet_size_bytes) | |
258 | { | |
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); | |
265 | ||
266 | /* | |
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. | |
272 | */ | |
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); | |
279 | } |