Commit | Line | Data |
---|---|---|
e98a2d6e | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
e98a2d6e | 3 | * |
0235b0db MJ |
4 | * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com> |
5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation | |
e98a2d6e PP |
7 | */ |
8 | ||
c802cacb | 9 | #include <glib.h> |
c802cacb SM |
10 | #include <stdint.h> |
11 | #include <stdio.h> | |
c802cacb | 12 | |
83ad336c | 13 | #include "compat/endian.h" /* IWYU pragma: keep */ |
0f5c5d5c | 14 | #include "compat/mman.h" /* IWYU: pragma keep */ |
ffb66082 | 15 | #include "cpp-common/bt2c/glib-up.hpp" |
2cef6403 | 16 | #include "cpp-common/bt2s/make-unique.hpp" |
0f5c5d5c | 17 | #include "cpp-common/vendor/fmt/format.h" |
c802cacb | 18 | |
5656cea5 | 19 | #include "../common/src/msg-iter/msg-iter.hpp" |
087cd0f5 | 20 | #include "data-stream-file.hpp" |
c802cacb | 21 | #include "file.hpp" |
c7e1be4b SM |
22 | #include "fs.hpp" |
23 | #include "lttng-index.hpp" | |
e98a2d6e | 24 | |
4164020e | 25 | static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 26 | { |
4164020e SM |
27 | BT_ASSERT_DBG(ds_file->mmap_len >= ds_file->request_offset_in_mapping); |
28 | return ds_file->mmap_len - ds_file->request_offset_in_mapping; | |
e98a2d6e PP |
29 | } |
30 | ||
127e2341 SM |
31 | /* |
32 | * Return true if `offset_in_file` is in the current mapping. | |
33 | */ | |
34 | ||
4164020e | 35 | static bool offset_ist_mapped(struct ctf_fs_ds_file *ds_file, off_t offset_in_file) |
127e2341 | 36 | { |
4164020e SM |
37 | return offset_in_file >= ds_file->mmap_offset_in_file && |
38 | offset_in_file < (ds_file->mmap_offset_in_file + ds_file->mmap_len); | |
127e2341 SM |
39 | } |
40 | ||
4164020e | 41 | static enum ctf_msg_iter_medium_status ds_file_munmap(struct ctf_fs_ds_file *ds_file) |
e98a2d6e | 42 | { |
4164020e SM |
43 | BT_ASSERT(ds_file); |
44 | ||
45 | if (!ds_file->mmap_addr) { | |
08bbca9a | 46 | return CTF_MSG_ITER_MEDIUM_STATUS_OK; |
4164020e SM |
47 | } |
48 | ||
49 | if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) { | |
0f5c5d5c SM |
50 | BT_CPPLOGE_ERRNO_SPEC(ds_file->logger, "Cannot memory-unmap file", |
51 | ": address={}, size={}, file_path=\"{}\", file={}", | |
52 | fmt::ptr(ds_file->mmap_addr), ds_file->mmap_len, | |
a39d9817 | 53 | ds_file->file ? ds_file->file->path : "NULL", |
0f5c5d5c | 54 | ds_file->file ? fmt::ptr(ds_file->file->fp) : NULL); |
08bbca9a | 55 | return CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
4164020e SM |
56 | } |
57 | ||
58 | ds_file->mmap_addr = NULL; | |
59 | ||
08bbca9a | 60 | return CTF_MSG_ITER_MEDIUM_STATUS_OK; |
e98a2d6e PP |
61 | } |
62 | ||
127e2341 SM |
63 | /* |
64 | * mmap a region of `ds_file` such that `requested_offset_in_file` is in the | |
65 | * mapping. If the currently mmap-ed region already contains | |
66 | * `requested_offset_in_file`, the mapping is kept. | |
67 | * | |
f6e68e70 SM |
68 | * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`, |
69 | * such that the next call to `request_bytes` will return bytes starting at that | |
70 | * position. | |
127e2341 SM |
71 | * |
72 | * `requested_offset_in_file` must be a valid offset in the file. | |
73 | */ | |
4164020e SM |
74 | static enum ctf_msg_iter_medium_status ds_file_mmap(struct ctf_fs_ds_file *ds_file, |
75 | off_t requested_offset_in_file) | |
e98a2d6e | 76 | { |
4164020e SM |
77 | /* Ensure the requested offset is in the file range. */ |
78 | BT_ASSERT(requested_offset_in_file >= 0); | |
79 | BT_ASSERT(requested_offset_in_file < ds_file->file->size); | |
80 | ||
81 | /* | |
82 | * If the mapping already contains the requested offset, just adjust | |
83 | * requested_offset_in_mapping. | |
84 | */ | |
85 | if (offset_ist_mapped(ds_file, requested_offset_in_file)) { | |
86 | ds_file->request_offset_in_mapping = | |
87 | requested_offset_in_file - ds_file->mmap_offset_in_file; | |
08bbca9a | 88 | return CTF_MSG_ITER_MEDIUM_STATUS_OK; |
4164020e SM |
89 | } |
90 | ||
91 | /* Unmap old region */ | |
08bbca9a | 92 | ctf_msg_iter_medium_status status = ds_file_munmap(ds_file); |
4164020e | 93 | if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) { |
08bbca9a | 94 | return status; |
4164020e SM |
95 | } |
96 | ||
97 | /* | |
98 | * Compute a mapping that has the required alignment properties and | |
99 | * contains `requested_offset_in_file`. | |
100 | */ | |
101 | ds_file->request_offset_in_mapping = | |
0f5c5d5c SM |
102 | requested_offset_in_file % |
103 | bt_mmap_get_offset_align_size(static_cast<int>(ds_file->logger.level())); | |
4164020e SM |
104 | ds_file->mmap_offset_in_file = requested_offset_in_file - ds_file->request_offset_in_mapping; |
105 | ds_file->mmap_len = | |
106 | MIN(ds_file->file->size - ds_file->mmap_offset_in_file, ds_file->mmap_max_len); | |
107 | ||
108 | BT_ASSERT(ds_file->mmap_len > 0); | |
109 | ||
110 | ds_file->mmap_addr = | |
85a25425 | 111 | bt_mmap(ds_file->mmap_len, PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp.get()), |
0f5c5d5c | 112 | ds_file->mmap_offset_in_file, static_cast<int>(ds_file->logger.level())); |
4164020e | 113 | if (ds_file->mmap_addr == MAP_FAILED) { |
0f5c5d5c SM |
114 | BT_CPPLOGE_SPEC(ds_file->logger, |
115 | "Cannot memory-map address (size {}) of file \"{}\" ({}) at offset {}: {}", | |
a39d9817 | 116 | ds_file->mmap_len, ds_file->file->path, fmt::ptr(ds_file->file->fp), |
0f5c5d5c | 117 | (intmax_t) ds_file->mmap_offset_in_file, strerror(errno)); |
08bbca9a | 118 | return CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
4164020e SM |
119 | } |
120 | ||
08bbca9a | 121 | return CTF_MSG_ITER_MEDIUM_STATUS_OK; |
127e2341 SM |
122 | } |
123 | ||
124 | /* | |
125 | * Change the mapping of the file to read the region that follows the current | |
126 | * mapping. | |
127 | * | |
128 | * If the file hasn't been mapped yet, then everything (mmap_offset_in_file, | |
129 | * mmap_len, request_offset_in_mapping) should have the value 0, which will | |
130 | * result in the beginning of the file getting mapped. | |
131 | * | |
132 | * return _EOF if the current mapping is the end of the file. | |
133 | */ | |
134 | ||
4164020e | 135 | static enum ctf_msg_iter_medium_status ds_file_mmap_next(struct ctf_fs_ds_file *ds_file) |
127e2341 | 136 | { |
4164020e SM |
137 | /* |
138 | * If we're called, it's because more bytes are requested but we have | |
139 | * given all the bytes of the current mapping. | |
140 | */ | |
141 | BT_ASSERT(ds_file->request_offset_in_mapping == ds_file->mmap_len); | |
142 | ||
143 | /* | |
144 | * If the current mapping coincides with the end of the file, there is | |
145 | * no next mapping. | |
146 | */ | |
147 | if (ds_file->mmap_offset_in_file + ds_file->mmap_len == ds_file->file->size) { | |
08bbca9a | 148 | return CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
4164020e SM |
149 | } |
150 | ||
08bbca9a | 151 | return ds_file_mmap(ds_file, ds_file->mmap_offset_in_file + ds_file->mmap_len); |
e98a2d6e PP |
152 | } |
153 | ||
4164020e SM |
154 | static enum ctf_msg_iter_medium_status medop_request_bytes(size_t request_sz, uint8_t **buffer_addr, |
155 | size_t *buffer_sz, void *data) | |
e98a2d6e | 156 | { |
4164020e | 157 | struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data; |
4164020e SM |
158 | |
159 | BT_ASSERT(request_sz > 0); | |
160 | ||
161 | /* | |
162 | * Check if we have at least one memory-mapped byte left. If we don't, | |
163 | * mmap the next file. | |
164 | */ | |
165 | if (remaining_mmap_bytes(ds_file) == 0) { | |
166 | /* Are we at the end of the file? */ | |
167 | if (ds_file->mmap_offset_in_file >= ds_file->file->size) { | |
a39d9817 SM |
168 | BT_CPPLOGD_SPEC(ds_file->logger, "Reached end of file \"{}\" ({})", ds_file->file->path, |
169 | fmt::ptr(ds_file->file->fp)); | |
08bbca9a | 170 | return CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
4164020e SM |
171 | } |
172 | ||
08bbca9a | 173 | ctf_msg_iter_medium_status status = ds_file_mmap_next(ds_file); |
4164020e SM |
174 | switch (status) { |
175 | case CTF_MSG_ITER_MEDIUM_STATUS_OK: | |
176 | break; | |
177 | case CTF_MSG_ITER_MEDIUM_STATUS_EOF: | |
08bbca9a | 178 | return CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
4164020e | 179 | default: |
0f5c5d5c | 180 | BT_CPPLOGE_SPEC(ds_file->logger, "Cannot memory-map next region of file \"{}\" ({})", |
a39d9817 | 181 | ds_file->file->path, fmt::ptr(ds_file->file->fp)); |
08bbca9a | 182 | return status; |
4164020e SM |
183 | } |
184 | } | |
185 | ||
186 | BT_ASSERT(remaining_mmap_bytes(ds_file) > 0); | |
187 | *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz); | |
188 | ||
189 | BT_ASSERT(ds_file->mmap_addr); | |
190 | *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset_in_mapping; | |
191 | ||
192 | ds_file->request_offset_in_mapping += *buffer_sz; | |
e98a2d6e | 193 | |
08bbca9a | 194 | return CTF_MSG_ITER_MEDIUM_STATUS_OK; |
e98a2d6e PP |
195 | } |
196 | ||
ecd7492f | 197 | static bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t, void *data) |
e98a2d6e | 198 | { |
4164020e SM |
199 | struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data; |
200 | bt_stream_class *ds_file_stream_class; | |
e5be10ef | 201 | |
265d4ba2 | 202 | ds_file_stream_class = ds_file->stream->cls().libObjPtr(); |
94cf822e | 203 | |
4164020e SM |
204 | if (stream_class != ds_file_stream_class) { |
205 | /* | |
206 | * Not supported: two packets described by two different | |
207 | * stream classes within the same data stream file. | |
208 | */ | |
08bbca9a | 209 | return nullptr; |
4164020e | 210 | } |
e98a2d6e | 211 | |
08bbca9a | 212 | return ds_file->stream->libObjPtr(); |
e98a2d6e PP |
213 | } |
214 | ||
4164020e | 215 | static enum ctf_msg_iter_medium_status medop_seek(off_t offset, void *data) |
9e0c8dbb | 216 | { |
4164020e | 217 | struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data; |
9e0c8dbb | 218 | |
4164020e SM |
219 | BT_ASSERT(offset >= 0); |
220 | BT_ASSERT(offset < ds_file->file->size); | |
9e0c8dbb | 221 | |
4164020e | 222 | return ds_file_mmap(ds_file, offset); |
9e0c8dbb JG |
223 | } |
224 | ||
18a1979b | 225 | struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = { |
4164020e SM |
226 | medop_request_bytes, |
227 | medop_seek, | |
228 | nullptr, | |
229 | medop_borrow_stream, | |
e98a2d6e | 230 | }; |
6de92955 | 231 | |
4164020e SM |
232 | struct ctf_fs_ds_group_medops_data |
233 | { | |
0f5c5d5c SM |
234 | explicit ctf_fs_ds_group_medops_data(const bt2c::Logger& parentLogger) : |
235 | logger {parentLogger, "PLUGIN/SRC.CTF.FS/DS-GROUP-MEDOPS"} | |
236 | { | |
237 | } | |
238 | ||
239 | bt2c::Logger logger; | |
240 | ||
4164020e | 241 | /* Weak, set once at creation time. */ |
afb0f12b | 242 | struct ctf_fs_ds_file_group *ds_file_group = nullptr; |
4164020e SM |
243 | |
244 | /* | |
245 | * Index (as in element rank) of the index entry of ds_file_groups' | |
246 | * index we will read next (so, the one after the one we are reading | |
247 | * right now). | |
248 | */ | |
afb0f12b | 249 | guint next_index_entry_index = 0; |
4164020e SM |
250 | |
251 | /* | |
252 | * File we are currently reading. Changes whenever we switch to | |
253 | * reading another data file. | |
4164020e | 254 | */ |
55ea683f | 255 | ctf_fs_ds_file::UP file; |
4164020e SM |
256 | |
257 | /* Weak, for context / logging / appending causes. */ | |
afb0f12b | 258 | bt_self_message_iterator *self_msg_iter = nullptr; |
f6e68e70 SM |
259 | }; |
260 | ||
4164020e SM |
261 | static enum ctf_msg_iter_medium_status medop_group_request_bytes(size_t request_sz, |
262 | uint8_t **buffer_addr, | |
263 | size_t *buffer_sz, void *void_data) | |
f6e68e70 | 264 | { |
4164020e | 265 | struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data; |
f6e68e70 | 266 | |
4164020e | 267 | /* Return bytes from the current file. */ |
55ea683f | 268 | return medop_request_bytes(request_sz, buffer_addr, buffer_sz, data->file.get()); |
f6e68e70 SM |
269 | } |
270 | ||
4164020e SM |
271 | static bt_stream *medop_group_borrow_stream(bt_stream_class *stream_class, int64_t stream_id, |
272 | void *void_data) | |
f6e68e70 | 273 | { |
4164020e | 274 | struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data; |
f6e68e70 | 275 | |
55ea683f | 276 | return medop_borrow_stream(stream_class, stream_id, data->file.get()); |
f6e68e70 SM |
277 | } |
278 | ||
279 | /* | |
280 | * Set `data->file` to prepare it to read the packet described | |
281 | * by `index_entry`. | |
282 | */ | |
283 | ||
4164020e SM |
284 | static enum ctf_msg_iter_medium_status |
285 | ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data *data, | |
0f5c5d5c | 286 | struct ctf_fs_ds_index_entry *index_entry) |
f6e68e70 | 287 | { |
4164020e SM |
288 | BT_ASSERT(data); |
289 | BT_ASSERT(index_entry); | |
290 | ||
291 | /* Check if that file is already the one mapped. */ | |
a39d9817 | 292 | if (!data->file || data->file->file->path != index_entry->path) { |
4164020e | 293 | /* Create the new file. */ |
265d4ba2 SM |
294 | data->file = |
295 | ctf_fs_ds_file_create(data->ds_file_group->ctf_fs_trace, data->ds_file_group->stream, | |
55ea683f | 296 | index_entry->path, data->logger); |
4164020e | 297 | if (!data->file) { |
0f5c5d5c | 298 | BT_CPPLOGE_APPEND_CAUSE_SPEC(data->logger, "failed to create ctf_fs_ds_file."); |
08bbca9a | 299 | return CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
4164020e SM |
300 | } |
301 | } | |
302 | ||
303 | /* | |
304 | * Ensure the right portion of the file will be returned on the next | |
305 | * request_bytes call. | |
306 | */ | |
08bbca9a | 307 | return ds_file_mmap(data->file.get(), index_entry->offset.bytes()); |
f6e68e70 SM |
308 | } |
309 | ||
4164020e | 310 | static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data) |
f6e68e70 | 311 | { |
4164020e | 312 | struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data; |
4164020e SM |
313 | |
314 | /* If we have gone through all index entries, we are done. */ | |
c46b32d8 | 315 | if (data->next_index_entry_index >= data->ds_file_group->index.entries.size()) { |
08bbca9a | 316 | return CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
4164020e SM |
317 | } |
318 | ||
319 | /* | |
320 | * Otherwise, look up the next index entry / packet and prepare it | |
321 | * for reading. | |
322 | */ | |
0011731e | 323 | ctf_msg_iter_medium_status status = ctf_fs_ds_group_medops_set_file( |
c46b32d8 | 324 | data, &data->ds_file_group->index.entries[data->next_index_entry_index]); |
4164020e | 325 | if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) { |
08bbca9a | 326 | return status; |
4164020e SM |
327 | } |
328 | ||
329 | data->next_index_entry_index++; | |
330 | ||
08bbca9a | 331 | return CTF_MSG_ITER_MEDIUM_STATUS_OK; |
f6e68e70 SM |
332 | } |
333 | ||
3cf88182 SM |
334 | void ctf_fs_ds_group_medops_data_deleter::operator()(ctf_fs_ds_group_medops_data *data) noexcept |
335 | { | |
2db013e0 | 336 | delete data; |
3cf88182 SM |
337 | } |
338 | ||
f6e68e70 | 339 | enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create( |
4164020e | 340 | struct ctf_fs_ds_file_group *ds_file_group, bt_self_message_iterator *self_msg_iter, |
3cf88182 | 341 | const bt2c::Logger& parentLogger, ctf_fs_ds_group_medops_data_up& out) |
f6e68e70 | 342 | { |
4164020e SM |
343 | BT_ASSERT(self_msg_iter); |
344 | BT_ASSERT(ds_file_group); | |
c46b32d8 | 345 | BT_ASSERT(!ds_file_group->index.entries.empty()); |
4164020e | 346 | |
3cf88182 SM |
347 | out.reset(new ctf_fs_ds_group_medops_data {parentLogger}); |
348 | ||
349 | out->ds_file_group = ds_file_group; | |
350 | out->self_msg_iter = self_msg_iter; | |
4164020e SM |
351 | |
352 | /* | |
353 | * No need to prepare the first file. ctf_msg_iter will call | |
354 | * switch_packet before reading the first packet, it will be | |
355 | * done then. | |
356 | */ | |
357 | ||
afb0f12b | 358 | return CTF_MSG_ITER_MEDIUM_STATUS_OK; |
f6e68e70 SM |
359 | } |
360 | ||
361 | void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data *data) | |
362 | { | |
4164020e | 363 | data->next_index_entry_index = 0; |
f6e68e70 SM |
364 | } |
365 | ||
366 | struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = { | |
4164020e | 367 | .request_bytes = medop_group_request_bytes, |
f6e68e70 | 368 | |
4164020e SM |
369 | /* |
370 | * We don't support seeking using this medops. It would probably be | |
371 | * possible, but it's not needed at the moment. | |
372 | */ | |
373 | .seek = NULL, | |
087cd0f5 | 374 | |
4164020e SM |
375 | .switch_packet = medop_group_switch_packet, |
376 | .borrow_stream = medop_group_borrow_stream, | |
f6e68e70 SM |
377 | }; |
378 | ||
4164020e | 379 | static int convert_cycles_to_ns(struct ctf_clock_class *clock_class, uint64_t cycles, int64_t *ns) |
b6c3dcb2 | 380 | { |
4164020e SM |
381 | return bt_util_clock_cycles_to_ns_from_origin(cycles, clock_class->frequency, |
382 | clock_class->offset_seconds, | |
383 | clock_class->offset_cycles, ns); | |
97ade20b JG |
384 | } |
385 | ||
c46b32d8 SM |
386 | static bt2s::optional<ctf_fs_ds_index> |
387 | build_index_from_idx_file(struct ctf_fs_ds_file *ds_file, struct ctf_fs_ds_file_info *file_info, | |
388 | struct ctf_msg_iter *msg_iter) | |
97ade20b | 389 | { |
0f5c5d5c | 390 | BT_CPPLOGI_SPEC(ds_file->logger, "Building index from .idx file of stream file {}", |
a39d9817 | 391 | ds_file->file->path); |
3ecf3ba0 | 392 | ctf_msg_iter_packet_properties props; |
08bbca9a | 393 | int ret = ctf_msg_iter_get_packet_properties(msg_iter, &props); |
4164020e | 394 | if (ret) { |
e27adb90 | 395 | BT_CPPLOGI_SPEC(ds_file->logger, "Cannot read first packet's header and context fields."); |
c46b32d8 | 396 | return bt2s::nullopt; |
4164020e SM |
397 | } |
398 | ||
3ecf3ba0 SM |
399 | ctf_stream_class *sc = |
400 | ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id); | |
4164020e SM |
401 | BT_ASSERT(sc); |
402 | if (!sc->default_clock_class) { | |
e27adb90 | 403 | BT_CPPLOGI_SPEC(ds_file->logger, "Cannot find stream class's default clock class."); |
c46b32d8 | 404 | return bt2s::nullopt; |
4164020e SM |
405 | } |
406 | ||
407 | /* Look for index file in relative path index/name.idx. */ | |
3ecf3ba0 | 408 | bt2c::GCharUP basename {g_path_get_basename(ds_file->file->path.c_str())}; |
4164020e | 409 | if (!basename) { |
0f5c5d5c | 410 | BT_CPPLOGE_SPEC(ds_file->logger, "Cannot get the basename of datastream file {}", |
a39d9817 | 411 | ds_file->file->path); |
c46b32d8 | 412 | return bt2s::nullopt; |
4164020e SM |
413 | } |
414 | ||
3ecf3ba0 | 415 | bt2c::GCharUP directory {g_path_get_dirname(ds_file->file->path.c_str())}; |
4164020e | 416 | if (!directory) { |
0f5c5d5c | 417 | BT_CPPLOGE_SPEC(ds_file->logger, "Cannot get dirname of datastream file {}", |
a39d9817 | 418 | ds_file->file->path); |
c46b32d8 | 419 | return bt2s::nullopt; |
4164020e SM |
420 | } |
421 | ||
3ecf3ba0 SM |
422 | std::string index_basename = fmt::format("{}.idx", basename.get()); |
423 | bt2c::GCharUP index_file_path { | |
424 | g_build_filename(directory.get(), "index", index_basename.c_str(), NULL)}; | |
425 | bt2c::GMappedFileUP mapped_file {g_mapped_file_new(index_file_path.get(), FALSE, NULL)}; | |
4164020e | 426 | if (!mapped_file) { |
ffb66082 | 427 | BT_CPPLOGD_SPEC(ds_file->logger, "Cannot create new mapped file {}", index_file_path.get()); |
c46b32d8 | 428 | return bt2s::nullopt; |
4164020e SM |
429 | } |
430 | ||
431 | /* | |
432 | * The g_mapped_file API limits us to 4GB files on 32-bit. | |
433 | * Traces with such large indexes have never been seen in the wild, | |
434 | * but this would need to be adjusted to support them. | |
435 | */ | |
3ecf3ba0 SM |
436 | gsize filesize = g_mapped_file_get_length(mapped_file.get()); |
437 | if (filesize < sizeof(ctf_packet_index_file_hdr)) { | |
0f5c5d5c SM |
438 | BT_CPPLOGW_SPEC(ds_file->logger, |
439 | "Invalid LTTng trace index file: " | |
440 | "file size ({} bytes) < header size ({} bytes)", | |
3ecf3ba0 | 441 | filesize, sizeof(ctf_packet_index_file_hdr)); |
c46b32d8 | 442 | return bt2s::nullopt; |
4164020e SM |
443 | } |
444 | ||
3ecf3ba0 SM |
445 | const char *mmap_begin = g_mapped_file_get_contents(mapped_file.get()); |
446 | const ctf_packet_index_file_hdr *header = (const ctf_packet_index_file_hdr *) mmap_begin; | |
4164020e | 447 | |
3ecf3ba0 | 448 | const char *file_pos = g_mapped_file_get_contents(mapped_file.get()) + sizeof(*header); |
4164020e | 449 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { |
e27adb90 PP |
450 | BT_CPPLOGW_SPEC(ds_file->logger, |
451 | "Invalid LTTng trace index: \"magic\" field validation failed"); | |
c46b32d8 | 452 | return bt2s::nullopt; |
4164020e SM |
453 | } |
454 | ||
3ecf3ba0 SM |
455 | uint32_t version_major = be32toh(header->index_major); |
456 | uint32_t version_minor = be32toh(header->index_minor); | |
4164020e | 457 | if (version_major != 1) { |
0f5c5d5c SM |
458 | BT_CPPLOGW_SPEC(ds_file->logger, "Unknown LTTng trace index version: major={}, minor={}", |
459 | version_major, version_minor); | |
c46b32d8 | 460 | return bt2s::nullopt; |
4164020e SM |
461 | } |
462 | ||
3ecf3ba0 | 463 | size_t file_index_entry_size = be32toh(header->packet_index_len); |
4164020e | 464 | if (file_index_entry_size < CTF_INDEX_1_0_SIZE) { |
0f5c5d5c SM |
465 | BT_CPPLOGW_SPEC( |
466 | ds_file->logger, | |
4164020e | 467 | "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): " |
0f5c5d5c | 468 | "packet_index_len={}, CTF_INDEX_1_0_SIZE={}", |
4164020e | 469 | file_index_entry_size, CTF_INDEX_1_0_SIZE); |
c46b32d8 | 470 | return bt2s::nullopt; |
4164020e SM |
471 | } |
472 | ||
3ecf3ba0 | 473 | size_t file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; |
4164020e | 474 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
0f5c5d5c SM |
475 | BT_CPPLOGW_SPEC(ds_file->logger, |
476 | "Invalid LTTng trace index: the index's size after the header " | |
477 | "({} bytes) is not a multiple of the index entry size " | |
478 | "({} bytes)", | |
479 | (filesize - sizeof(*header)), sizeof(*header)); | |
c46b32d8 | 480 | return bt2s::nullopt; |
4164020e SM |
481 | } |
482 | ||
c46b32d8 | 483 | ctf_fs_ds_index index; |
3ecf3ba0 SM |
484 | ctf_fs_ds_index_entry *prev_index_entry = nullptr; |
485 | auto totalPacketsSize = bt2c::DataLen::fromBytes(0); | |
4164020e | 486 | |
3ecf3ba0 | 487 | for (size_t i = 0; i < file_entry_count; i++) { |
4164020e | 488 | struct ctf_packet_index *file_index = (struct ctf_packet_index *) file_pos; |
ef7d7ac2 | 489 | const auto packetSize = bt2c::DataLen::fromBits(be64toh(file_index->packet_size)); |
4164020e | 490 | |
ef7d7ac2 | 491 | if (packetSize.hasExtraBits()) { |
0f5c5d5c SM |
492 | BT_CPPLOGW_SPEC(ds_file->logger, |
493 | "Invalid packet size encountered in LTTng trace index file"); | |
c46b32d8 | 494 | return bt2s::nullopt; |
4164020e SM |
495 | } |
496 | ||
ef7d7ac2 SM |
497 | const auto offset = bt2c::DataLen::fromBytes(be64toh(file_index->offset)); |
498 | ||
499 | if (i != 0 && offset < prev_index_entry->offset) { | |
500 | BT_CPPLOGW_SPEC( | |
501 | ds_file->logger, | |
502 | "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " | |
503 | "previous offset={} bytes, current offset={} bytes", | |
504 | prev_index_entry->offset.bytes(), offset.bytes()); | |
c46b32d8 | 505 | return bt2s::nullopt; |
ef7d7ac2 SM |
506 | } |
507 | ||
f9b673b0 | 508 | ctf_fs_ds_index_entry index_entry {file_info->path.c_str(), offset, packetSize}; |
0011731e SM |
509 | index_entry.timestamp_begin = be64toh(file_index->timestamp_begin); |
510 | index_entry.timestamp_end = be64toh(file_index->timestamp_end); | |
511 | if (index_entry.timestamp_end < index_entry.timestamp_begin) { | |
0f5c5d5c SM |
512 | BT_CPPLOGW_SPEC( |
513 | ds_file->logger, | |
4164020e | 514 | "Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
0f5c5d5c | 515 | "timestamp_begin={}, timestamp_end={}", |
0011731e | 516 | index_entry.timestamp_begin, index_entry.timestamp_end); |
c46b32d8 | 517 | return bt2s::nullopt; |
4164020e SM |
518 | } |
519 | ||
520 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
0011731e SM |
521 | ret = convert_cycles_to_ns(sc->default_clock_class, index_entry.timestamp_begin, |
522 | &index_entry.timestamp_begin_ns); | |
4164020e | 523 | if (ret) { |
e27adb90 | 524 | BT_CPPLOGI_SPEC( |
0f5c5d5c | 525 | ds_file->logger, |
4164020e | 526 | "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
c46b32d8 | 527 | return bt2s::nullopt; |
4164020e | 528 | } |
0011731e SM |
529 | ret = convert_cycles_to_ns(sc->default_clock_class, index_entry.timestamp_end, |
530 | &index_entry.timestamp_end_ns); | |
4164020e | 531 | if (ret) { |
e27adb90 | 532 | BT_CPPLOGI_SPEC( |
0f5c5d5c | 533 | ds_file->logger, |
4164020e | 534 | "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
c46b32d8 | 535 | return bt2s::nullopt; |
4164020e SM |
536 | } |
537 | ||
538 | if (version_minor >= 1) { | |
0011731e | 539 | index_entry.packet_seq_num = be64toh(file_index->packet_seq_num); |
4164020e SM |
540 | } |
541 | ||
ef7d7ac2 | 542 | totalPacketsSize += packetSize; |
4164020e SM |
543 | file_pos += file_index_entry_size; |
544 | ||
c46b32d8 | 545 | index.entries.emplace_back(index_entry); |
4164020e | 546 | |
c46b32d8 | 547 | prev_index_entry = &index.entries.back(); |
4164020e SM |
548 | } |
549 | ||
550 | /* Validate that the index addresses the complete stream. */ | |
ef7d7ac2 | 551 | if (ds_file->file->size != totalPacketsSize.bytes()) { |
0f5c5d5c SM |
552 | BT_CPPLOGW_SPEC(ds_file->logger, |
553 | "Invalid LTTng trace index file; indexed size != stream file size: " | |
ef7d7ac2 SM |
554 | "file-size={} bytes, total-packets-size={} bytes", |
555 | ds_file->file->size, totalPacketsSize.bytes()); | |
c46b32d8 | 556 | return bt2s::nullopt; |
4164020e | 557 | } |
08bbca9a | 558 | |
4164020e | 559 | return index; |
b6c3dcb2 JG |
560 | } |
561 | ||
0011731e | 562 | static int init_index_entry(ctf_fs_ds_index_entry& entry, struct ctf_fs_ds_file *ds_file, |
ef7d7ac2 | 563 | struct ctf_msg_iter_packet_properties *props) |
9e0c8dbb | 564 | { |
3ecf3ba0 SM |
565 | ctf_stream_class *sc = |
566 | ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props->stream_class_id); | |
4164020e | 567 | BT_ASSERT(sc); |
4164020e SM |
568 | |
569 | if (props->snapshots.beginning_clock != UINT64_C(-1)) { | |
0011731e | 570 | entry.timestamp_begin = props->snapshots.beginning_clock; |
4164020e SM |
571 | |
572 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
08bbca9a | 573 | int ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.beginning_clock, |
0011731e | 574 | &entry.timestamp_begin_ns); |
4164020e | 575 | if (ret) { |
e27adb90 PP |
576 | BT_CPPLOGI_SPEC(ds_file->logger, |
577 | "Failed to convert raw timestamp to nanoseconds since Epoch."); | |
08bbca9a | 578 | return ret; |
4164020e SM |
579 | } |
580 | } else { | |
0011731e SM |
581 | entry.timestamp_begin = UINT64_C(-1); |
582 | entry.timestamp_begin_ns = UINT64_C(-1); | |
4164020e SM |
583 | } |
584 | ||
585 | if (props->snapshots.end_clock != UINT64_C(-1)) { | |
0011731e | 586 | entry.timestamp_end = props->snapshots.end_clock; |
4164020e SM |
587 | |
588 | /* Convert the packet's bound to nanoseconds since Epoch. */ | |
08bbca9a | 589 | int ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.end_clock, |
0011731e | 590 | &entry.timestamp_end_ns); |
4164020e | 591 | if (ret) { |
e27adb90 PP |
592 | BT_CPPLOGI_SPEC(ds_file->logger, |
593 | "Failed to convert raw timestamp to nanoseconds since Epoch."); | |
08bbca9a | 594 | return ret; |
4164020e SM |
595 | } |
596 | } else { | |
0011731e SM |
597 | entry.timestamp_end = UINT64_C(-1); |
598 | entry.timestamp_end_ns = UINT64_C(-1); | |
4164020e | 599 | } |
0b29603d | 600 | |
08bbca9a | 601 | return 0; |
9e0c8dbb JG |
602 | } |
603 | ||
c46b32d8 SM |
604 | static bt2s::optional<ctf_fs_ds_index> |
605 | build_index_from_stream_file(struct ctf_fs_ds_file *ds_file, struct ctf_fs_ds_file_info *file_info, | |
606 | struct ctf_msg_iter *msg_iter) | |
9e0c8dbb | 607 | { |
a39d9817 | 608 | BT_CPPLOGI_SPEC(ds_file->logger, "Indexing stream file {}", ds_file->file->path); |
4164020e | 609 | |
c46b32d8 | 610 | ctf_fs_ds_index index; |
3ecf3ba0 | 611 | auto currentPacketOffset = bt2c::DataLen::fromBytes(0); |
4164020e SM |
612 | |
613 | while (true) { | |
4164020e SM |
614 | struct ctf_msg_iter_packet_properties props; |
615 | ||
ef7d7ac2 | 616 | if (currentPacketOffset.bytes() > ds_file->file->size) { |
e27adb90 PP |
617 | BT_CPPLOGE_SPEC(ds_file->logger, |
618 | "Unexpected current packet's offset (larger than file)."); | |
c46b32d8 | 619 | return bt2s::nullopt; |
ef7d7ac2 | 620 | } else if (currentPacketOffset.bytes() == ds_file->file->size) { |
4164020e SM |
621 | /* No more data */ |
622 | break; | |
623 | } | |
624 | ||
3ecf3ba0 | 625 | ctf_msg_iter_status iter_status = ctf_msg_iter_seek(msg_iter, currentPacketOffset.bytes()); |
4164020e | 626 | if (iter_status != CTF_MSG_ITER_STATUS_OK) { |
c46b32d8 | 627 | return bt2s::nullopt; |
4164020e SM |
628 | } |
629 | ||
630 | iter_status = ctf_msg_iter_get_packet_properties(msg_iter, &props); | |
631 | if (iter_status != CTF_MSG_ITER_STATUS_OK) { | |
c46b32d8 | 632 | return bt2s::nullopt; |
4164020e SM |
633 | } |
634 | ||
ef7d7ac2 SM |
635 | /* |
636 | * Get the current packet size from the packet header, if set. Else, | |
637 | * assume there is a single packet in the file, so take the file size | |
638 | * as the packet size. | |
639 | */ | |
640 | const auto currentPacketSize = props.exp_packet_total_size >= 0 ? | |
641 | bt2c::DataLen::fromBits(props.exp_packet_total_size) : | |
642 | bt2c::DataLen::fromBytes(ds_file->file->size); | |
4164020e | 643 | |
ef7d7ac2 | 644 | if ((currentPacketOffset + currentPacketSize).bytes() > ds_file->file->size) { |
0f5c5d5c SM |
645 | BT_CPPLOGW_SPEC(ds_file->logger, |
646 | "Invalid packet size reported in file: stream=\"{}\", " | |
ef7d7ac2 SM |
647 | "packet-offset-bytes={}, packet-size-bytes={}, " |
648 | "file-size-bytes={}", | |
a39d9817 | 649 | ds_file->file->path, currentPacketOffset.bytes(), |
ef7d7ac2 | 650 | currentPacketSize.bytes(), ds_file->file->size); |
c46b32d8 | 651 | return bt2s::nullopt; |
4164020e SM |
652 | } |
653 | ||
f9b673b0 | 654 | ctf_fs_ds_index_entry index_entry {file_info->path, currentPacketOffset, currentPacketSize}; |
4164020e | 655 | |
3ecf3ba0 | 656 | int ret = init_index_entry(index_entry, ds_file, &props); |
4164020e | 657 | if (ret) { |
c46b32d8 | 658 | return bt2s::nullopt; |
4164020e SM |
659 | } |
660 | ||
c46b32d8 | 661 | index.entries.emplace_back(index_entry); |
4164020e | 662 | |
ef7d7ac2 | 663 | currentPacketOffset += currentPacketSize; |
0f5c5d5c | 664 | BT_CPPLOGD_SPEC(ds_file->logger, |
ef7d7ac2 SM |
665 | "Seeking to next packet: current-packet-offset-bytes={}, " |
666 | "next-packet-offset-bytes={}", | |
667 | (currentPacketOffset - currentPacketSize).bytes(), | |
668 | currentPacketOffset.bytes()); | |
4164020e | 669 | } |
312c056a | 670 | |
4164020e | 671 | return index; |
9e0c8dbb JG |
672 | } |
673 | ||
89f88383 SM |
674 | ctf_fs_ds_file::UP ctf_fs_ds_file_create(struct ctf_fs_trace *ctf_fs_trace, |
675 | bt2::Stream::Shared stream, const char *path, | |
676 | const bt2c::Logger& parentLogger) | |
e98a2d6e | 677 | { |
89f88383 | 678 | auto ds_file = bt2s::make_unique<ctf_fs_ds_file>(parentLogger); |
4164020e | 679 | |
4726b1ee | 680 | ds_file->file = bt2s::make_unique<ctf_fs_file>(parentLogger); |
265d4ba2 | 681 | ds_file->stream = std::move(stream); |
2dba3a29 | 682 | ds_file->metadata = ctf_fs_trace->metadata.get(); |
a39d9817 | 683 | ds_file->file->path = path; |
3ecf3ba0 | 684 | int ret = ctf_fs_file_open(ds_file->file.get(), "rb"); |
4164020e | 685 | if (ret) { |
08bbca9a | 686 | return nullptr; |
4164020e SM |
687 | } |
688 | ||
3ecf3ba0 SM |
689 | const size_t offset_align = |
690 | bt_mmap_get_offset_align_size(static_cast<int>(ds_file->logger.level())); | |
4164020e SM |
691 | ds_file->mmap_max_len = offset_align * 2048; |
692 | ||
4164020e | 693 | return ds_file; |
e98a2d6e PP |
694 | } |
695 | ||
c46b32d8 SM |
696 | bt2s::optional<ctf_fs_ds_index> ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file, |
697 | struct ctf_fs_ds_file_info *file_info, | |
698 | struct ctf_msg_iter *msg_iter) | |
97ade20b | 699 | { |
441fa755 | 700 | auto index = build_index_from_idx_file(ds_file, file_info, msg_iter); |
4164020e | 701 | if (index) { |
08bbca9a | 702 | return index; |
4164020e SM |
703 | } |
704 | ||
0f5c5d5c SM |
705 | BT_CPPLOGI_SPEC(ds_file->logger, "Failed to build index from .index file; " |
706 | "falling back to stream indexing."); | |
08bbca9a | 707 | return build_index_from_stream_file(ds_file, file_info, msg_iter); |
97ade20b JG |
708 | } |
709 | ||
3199f1ba | 710 | ctf_fs_ds_file::~ctf_fs_ds_file() |
e98a2d6e | 711 | { |
3199f1ba | 712 | (void) ds_file_munmap(this); |
e98a2d6e | 713 | } |
4f1f88a6 | 714 | |
2cef6403 | 715 | ctf_fs_ds_file_info::UP ctf_fs_ds_file_info_create(const char *path, int64_t begin_ns) |
873c329a | 716 | { |
2cef6403 | 717 | ctf_fs_ds_file_info::UP ds_file_info = bt2s::make_unique<ctf_fs_ds_file_info>(); |
873c329a | 718 | |
4d199954 | 719 | ds_file_info->path = path; |
873c329a | 720 | ds_file_info->begin_ns = begin_ns; |
873c329a SM |
721 | return ds_file_info; |
722 | } | |
723 | ||
1cb63a52 SM |
724 | void ctf_fs_ds_file_group::insert_ds_file_info_sorted(ctf_fs_ds_file_info::UP ds_file_info) |
725 | { | |
726 | /* Find the spot where to insert this ds_file_info. */ | |
727 | auto it = this->ds_file_infos.begin(); | |
728 | ||
729 | for (; it != this->ds_file_infos.end(); ++it) { | |
730 | const ctf_fs_ds_file_info& other_ds_file_info = **it; | |
731 | ||
732 | if (ds_file_info->begin_ns < other_ds_file_info.begin_ns) { | |
733 | break; | |
734 | } | |
735 | } | |
736 | ||
737 | this->ds_file_infos.insert(it, std::move(ds_file_info)); | |
738 | } |