Commit | Line | Data |
---|---|---|
013f35c6 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
013f35c6 | 3 | * |
0235b0db | 4 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
013f35c6 PP |
5 | */ |
6 | ||
86d8b7b8 | 7 | #define BT_LOG_OUTPUT_LEVEL (ctfser->log_level) |
013f35c6 | 8 | #define BT_LOG_TAG "CTFSER" |
86d8b7b8 | 9 | #include "logging/log.h" |
013f35c6 PP |
10 | |
11 | #include <unistd.h> | |
12 | #include <string.h> | |
13 | #include <inttypes.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/stat.h> | |
16 | #include <unistd.h> | |
578e048b | 17 | #include "common/assert.h" |
013f35c6 PP |
18 | #include <stdarg.h> |
19 | #include <ctype.h> | |
20 | #include <glib.h> | |
21 | #include <stdlib.h> | |
22 | #include <stdio.h> | |
23 | #include <wchar.h> | |
91d81473 | 24 | #include "common/macros.h" |
578e048b MJ |
25 | #include "common/common.h" |
26 | #include "ctfser/ctfser.h" | |
27 | #include "compat/unistd.h" | |
28 | #include "compat/fcntl.h" | |
013f35c6 PP |
29 | |
30 | static inline | |
86d8b7b8 | 31 | uint64_t get_packet_size_increment_bytes(struct bt_ctfser *ctfser) |
013f35c6 | 32 | { |
86d8b7b8 | 33 | return bt_common_get_page_size(ctfser->log_level) * 8; |
013f35c6 PP |
34 | } |
35 | ||
36 | static inline | |
37 | void mmap_align_ctfser(struct bt_ctfser *ctfser) | |
38 | { | |
9223d1f3 MJ |
39 | ctfser->base_mma = mmap_align(ctfser->cur_packet_size_bytes, |
40 | PROT_READ | PROT_WRITE, | |
86d8b7b8 | 41 | MAP_SHARED, ctfser->fd, ctfser->mmap_offset, ctfser->log_level); |
013f35c6 PP |
42 | } |
43 | ||
44 | BT_HIDDEN | |
45 | int _bt_ctfser_increase_cur_packet_size(struct bt_ctfser *ctfser) | |
46 | { | |
47 | int ret; | |
48 | ||
49 | BT_ASSERT(ctfser); | |
3f7d4d90 | 50 | BT_LOGD("Increasing stream file's current packet size: " |
013f35c6 PP |
51 | "path=\"%s\", fd=%d, " |
52 | "offset-in-cur-packet-bits=%" PRIu64 ", " | |
53 | "cur-packet-size-bytes=%" PRIu64, | |
54 | ctfser->path->str, ctfser->fd, | |
55 | ctfser->offset_in_cur_packet_bits, | |
56 | ctfser->cur_packet_size_bytes); | |
57 | ret = munmap_align(ctfser->base_mma); | |
58 | if (ret) { | |
59 | BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping", | |
60 | ": ret=%d", ret); | |
61 | goto end; | |
62 | } | |
63 | ||
86d8b7b8 PP |
64 | ctfser->cur_packet_size_bytes += get_packet_size_increment_bytes( |
65 | ctfser); | |
013f35c6 PP |
66 | |
67 | do { | |
68 | ret = bt_posix_fallocate(ctfser->fd, ctfser->mmap_offset, | |
69 | ctfser->cur_packet_size_bytes); | |
70 | } while (ret == EINTR); | |
71 | ||
72 | if (ret) { | |
73 | BT_LOGE("Failed to preallocate memory space: ret=%d", ret); | |
74 | goto end; | |
75 | } | |
76 | ||
77 | mmap_align_ctfser(ctfser); | |
78 | if (ctfser->base_mma == MAP_FAILED) { | |
79 | BT_LOGE_ERRNO("Failed to perform an aligned memory mapping", | |
80 | ": ret=%d", ret); | |
81 | ret = -1; | |
82 | goto end; | |
83 | } | |
84 | ||
3f7d4d90 | 85 | BT_LOGD("Increased packet size: " |
013f35c6 PP |
86 | "path=\"%s\", fd=%d, " |
87 | "offset-in-cur-packet-bits=%" PRIu64 ", " | |
88 | "new-packet-size-bytes=%" PRIu64, | |
89 | ctfser->path->str, ctfser->fd, | |
90 | ctfser->offset_in_cur_packet_bits, | |
91 | ctfser->cur_packet_size_bytes); | |
92 | ||
93 | end: | |
94 | return ret; | |
95 | } | |
96 | ||
97 | BT_HIDDEN | |
86d8b7b8 | 98 | int bt_ctfser_init(struct bt_ctfser *ctfser, const char *path, int log_level) |
013f35c6 PP |
99 | { |
100 | int ret = 0; | |
101 | ||
102 | BT_ASSERT(ctfser); | |
103 | memset(ctfser, 0, sizeof(*ctfser)); | |
104 | ctfser->fd = open(path, O_RDWR | O_CREAT | O_TRUNC, | |
105 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
86d8b7b8 | 106 | ctfser->log_level = log_level; |
013f35c6 PP |
107 | if (ctfser->fd < 0) { |
108 | BT_LOGW_ERRNO("Failed to open stream file for writing", | |
109 | ": path=\"%s\", ret=%d", | |
110 | path, ctfser->fd); | |
111 | ret = -1; | |
112 | goto end; | |
113 | } | |
114 | ||
115 | ctfser->path = g_string_new(path); | |
116 | ||
117 | end: | |
118 | return ret; | |
119 | } | |
120 | ||
121 | BT_HIDDEN | |
122 | int bt_ctfser_fini(struct bt_ctfser *ctfser) | |
123 | { | |
124 | int ret = 0; | |
125 | ||
126 | if (ctfser->fd == -1) { | |
127 | goto free_path; | |
128 | } | |
129 | ||
a086573a JR |
130 | if (ctfser->base_mma) { |
131 | /* Unmap old base */ | |
132 | ret = munmap_align(ctfser->base_mma); | |
133 | if (ret) { | |
134 | BT_LOGE_ERRNO("Failed to unmap stream file", | |
135 | ": ret=%d, size-bytes=%" PRIu64, | |
136 | ret, ctfser->stream_size_bytes); | |
137 | goto end; | |
138 | } | |
139 | ||
140 | ctfser->base_mma = NULL; | |
141 | } | |
142 | ||
013f35c6 PP |
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 | ||
013f35c6 PP |
159 | |
160 | ret = close(ctfser->fd); | |
161 | if (ret) { | |
162 | BT_LOGE_ERRNO("Failed to close stream file", | |
163 | ": ret=%d", ret); | |
164 | goto end; | |
165 | } | |
166 | ||
167 | ctfser->fd = -1; | |
168 | ||
169 | free_path: | |
170 | if (ctfser->path) { | |
171 | g_string_free(ctfser->path, TRUE); | |
172 | ctfser->path = NULL; | |
173 | } | |
174 | ||
175 | end: | |
176 | return ret; | |
177 | } | |
178 | ||
179 | BT_HIDDEN | |
180 | int bt_ctfser_open_packet(struct bt_ctfser *ctfser) | |
181 | { | |
182 | int ret = 0; | |
183 | ||
3f7d4d90 | 184 | BT_LOGD("Opening packet: path=\"%s\", fd=%d, " |
013f35c6 PP |
185 | "prev-packet-size-bytes=%" PRIu64, |
186 | ctfser->path->str, ctfser->fd, | |
187 | ctfser->prev_packet_size_bytes); | |
188 | ||
189 | if (ctfser->base_mma) { | |
190 | /* Unmap old base (previous packet) */ | |
191 | ret = munmap_align(ctfser->base_mma); | |
192 | if (ret) { | |
193 | BT_LOGE_ERRNO("Failed to unmap stream file", | |
194 | ": ret=%d, size-bytes=%" PRIu64, | |
195 | ret, ctfser->stream_size_bytes); | |
196 | goto end; | |
197 | } | |
198 | ||
199 | ctfser->base_mma = NULL; | |
200 | } | |
201 | ||
202 | /* | |
203 | * Add the previous packet's size to the memory map address | |
204 | * offset to start writing immediately after it. | |
205 | */ | |
206 | ctfser->mmap_offset += ctfser->prev_packet_size_bytes; | |
207 | ctfser->prev_packet_size_bytes = 0; | |
208 | ||
209 | /* Make initial space for the current packet */ | |
86d8b7b8 PP |
210 | ctfser->cur_packet_size_bytes = get_packet_size_increment_bytes( |
211 | ctfser); | |
013f35c6 PP |
212 | |
213 | do { | |
214 | ret = bt_posix_fallocate(ctfser->fd, ctfser->mmap_offset, | |
215 | ctfser->cur_packet_size_bytes); | |
216 | } while (ret == EINTR); | |
217 | ||
218 | if (ret) { | |
219 | BT_LOGE("Failed to preallocate memory space: ret=%d", ret); | |
220 | goto end; | |
221 | } | |
222 | ||
223 | /* Start writing at the beginning of the current packet */ | |
224 | ctfser->offset_in_cur_packet_bits = 0; | |
225 | ||
226 | /* Get new base address */ | |
227 | mmap_align_ctfser(ctfser); | |
228 | if (ctfser->base_mma == MAP_FAILED) { | |
229 | BT_LOGE_ERRNO("Failed to perform an aligned memory mapping", | |
230 | ": ret=%d", ret); | |
231 | ret = -1; | |
232 | goto end; | |
233 | } | |
234 | ||
3f7d4d90 | 235 | BT_LOGD("Opened packet: path=\"%s\", fd=%d, " |
013f35c6 PP |
236 | "cur-packet-size-bytes=%" PRIu64, |
237 | ctfser->path->str, ctfser->fd, | |
238 | ctfser->cur_packet_size_bytes); | |
239 | ||
240 | end: | |
241 | return ret; | |
242 | } | |
243 | ||
244 | BT_HIDDEN | |
245 | void bt_ctfser_close_current_packet(struct bt_ctfser *ctfser, | |
246 | uint64_t packet_size_bytes) | |
247 | { | |
3f7d4d90 | 248 | BT_LOGD("Closing packet: path=\"%s\", fd=%d, " |
013f35c6 PP |
249 | "offset-in-cur-packet-bits=%" PRIu64 |
250 | "cur-packet-size-bytes=%" PRIu64, | |
251 | ctfser->path->str, ctfser->fd, | |
252 | ctfser->offset_in_cur_packet_bits, | |
253 | ctfser->cur_packet_size_bytes); | |
254 | ||
255 | /* | |
256 | * This will be used during the next call to | |
257 | * bt_ctfser_open_packet(): we add | |
258 | * `ctfser->prev_packet_size_bytes` to the current memory map | |
259 | * address offset (first byte of _this_ packet), effectively | |
260 | * making _this_ packet the required size. | |
261 | */ | |
262 | ctfser->prev_packet_size_bytes = packet_size_bytes; | |
263 | ctfser->stream_size_bytes += packet_size_bytes; | |
3f7d4d90 | 264 | BT_LOGD("Closed packet: path=\"%s\", fd=%d, " |
013f35c6 PP |
265 | "stream-file-size-bytes=%" PRIu64, |
266 | ctfser->path->str, ctfser->fd, | |
267 | ctfser->stream_size_bytes); | |
268 | } |