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