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