bt_field_class_*_create(): accept mandatory trace class
[babeltrace.git] / plugins / ctf / fs-src / data-stream-file.c
CommitLineData
e98a2d6e 1/*
94cf822e 2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
fc9a526c 3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
e98a2d6e
PP
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
e98a2d6e
PP
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
0fbb9a9f 25#include <stdlib.h>
e98a2d6e
PP
26#include <stdio.h>
27#include <stdint.h>
28#include <stdlib.h>
29#include <stdbool.h>
30#include <glib.h>
31#include <inttypes.h>
8f76831a 32#include <babeltrace/compat/mman-internal.h>
e9383dfd 33#include <babeltrace/endian-internal.h>
9d408fca 34#include <babeltrace/babeltrace.h>
55314f2a 35#include <babeltrace/common-internal.h>
78586d8a
JG
36#include "file.h"
37#include "metadata.h"
d6e69534 38#include "../common/msg-iter/msg-iter.h"
f6ccaed9 39#include <babeltrace/assert-internal.h>
94cf822e 40#include "data-stream-file.h"
e9383dfd 41#include <string.h>
e98a2d6e 42
55314f2a
JG
43#define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS"
44#include "logging.h"
e98a2d6e 45
4f1f88a6 46static inline
94cf822e 47size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
e98a2d6e 48{
2c701ca6 49 return ds_file->mmap_len - ds_file->request_offset;
e98a2d6e
PP
50}
51
5b29e799 52static
94cf822e 53int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
e98a2d6e 54{
fc9a526c 55 int ret = 0;
e98a2d6e 56
d238196d 57 if (!ds_file || !ds_file->mmap_addr) {
94cf822e
PP
58 goto end;
59 }
60
04394229 61 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
cd232764
JG
62 BT_LOGE_ERRNO("Cannot memory-unmap file",
63 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
94cf822e 64 ds_file->mmap_addr, ds_file->mmap_len,
9e0c8dbb 65 ds_file->file ? ds_file->file->path->str : "NULL",
cd232764 66 ds_file->file ? ds_file->file->fp : NULL);
fc9a526c
JG
67 ret = -1;
68 goto end;
e98a2d6e 69 }
94cf822e
PP
70
71 ds_file->mmap_addr = NULL;
72
fc9a526c
JG
73end:
74 return ret;
e98a2d6e
PP
75}
76
5b29e799 77static
d6e69534 78enum bt_msg_iter_medium_status ds_file_mmap_next(
94cf822e 79 struct ctf_fs_ds_file *ds_file)
e98a2d6e 80{
d6e69534
PP
81 enum bt_msg_iter_medium_status ret =
82 BT_MSG_ITER_MEDIUM_STATUS_OK;
e98a2d6e
PP
83
84 /* Unmap old region */
94cf822e
PP
85 if (ds_file->mmap_addr) {
86 if (ds_file_munmap(ds_file)) {
e98a2d6e
PP
87 goto error;
88 }
89
2f5a009b 90 /*
2c701ca6 91 * mmap_len is guaranteed to be page-aligned except on the
2f5a009b
JG
92 * last mapping where it may not be possible (since the file's
93 * size itself may not be a page multiple).
94 */
2c701ca6 95 ds_file->mmap_offset += ds_file->mmap_len;
94cf822e 96 ds_file->request_offset = 0;
e98a2d6e
PP
97 }
98
2c701ca6 99 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset,
94cf822e 100 ds_file->mmap_max_len);
2c701ca6 101 if (ds_file->mmap_len == 0) {
d6e69534 102 ret = BT_MSG_ITER_MEDIUM_STATUS_EOF;
e0f6a64a
JG
103 goto end;
104 }
e98a2d6e 105 /* Map new region */
f6ccaed9 106 BT_ASSERT(ds_file->mmap_len);
04394229 107 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
94cf822e
PP
108 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
109 ds_file->mmap_offset);
110 if (ds_file->mmap_addr == MAP_FAILED) {
1974687e 111 BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
94cf822e 112 ds_file->mmap_len, ds_file->file->path->str,
1974687e 113 ds_file->file->fp, (intmax_t) ds_file->mmap_offset,
78586d8a 114 strerror(errno));
e98a2d6e
PP
115 goto error;
116 }
117
118 goto end;
e98a2d6e 119error:
94cf822e 120 ds_file_munmap(ds_file);
d6e69534 121 ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
122end:
123 return ret;
124}
125
5b29e799 126static
d6e69534 127enum bt_msg_iter_medium_status medop_request_bytes(
e98a2d6e
PP
128 size_t request_sz, uint8_t **buffer_addr,
129 size_t *buffer_sz, void *data)
130{
d6e69534
PP
131 enum bt_msg_iter_medium_status status =
132 BT_MSG_ITER_MEDIUM_STATUS_OK;
94cf822e 133 struct ctf_fs_ds_file *ds_file = data;
e98a2d6e
PP
134
135 if (request_sz == 0) {
136 goto end;
137 }
138
e98a2d6e 139 /* Check if we have at least one memory-mapped byte left */
94cf822e 140 if (remaining_mmap_bytes(ds_file) == 0) {
e98a2d6e 141 /* Are we at the end of the file? */
94cf822e 142 if (ds_file->mmap_offset >= ds_file->file->size) {
55314f2a 143 BT_LOGD("Reached end of file \"%s\" (%p)",
94cf822e 144 ds_file->file->path->str, ds_file->file->fp);
d6e69534 145 status = BT_MSG_ITER_MEDIUM_STATUS_EOF;
e98a2d6e
PP
146 goto end;
147 }
148
94cf822e 149 status = ds_file_mmap_next(ds_file);
e0f6a64a 150 switch (status) {
d6e69534 151 case BT_MSG_ITER_MEDIUM_STATUS_OK:
e0f6a64a 152 break;
d6e69534 153 case BT_MSG_ITER_MEDIUM_STATUS_EOF:
e0f6a64a
JG
154 goto end;
155 default:
55314f2a 156 BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
94cf822e
PP
157 ds_file->file->path->str,
158 ds_file->file->fp);
e98a2d6e
PP
159 goto error;
160 }
161 }
162
94cf822e
PP
163 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
164 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
165 ds_file->request_offset += *buffer_sz;
e98a2d6e
PP
166 goto end;
167
168error:
d6e69534 169 status = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
170
171end:
172 return status;
173}
174
5b29e799 175static
b19ff26f
PP
176bt_stream *medop_borrow_stream(
177 bt_stream_class *stream_class, int64_t stream_id,
b92735af 178 void *data)
e98a2d6e 179{
94cf822e 180 struct ctf_fs_ds_file *ds_file = data;
b19ff26f
PP
181 bt_stream_class *ds_file_stream_class;
182 bt_stream *stream = NULL;
e5be10ef 183
40f4ba76 184 ds_file_stream_class = bt_stream_borrow_class(
e5be10ef 185 ds_file->stream);
94cf822e 186
94cf822e
PP
187 if (stream_class != ds_file_stream_class) {
188 /*
189 * Not supported: two packets described by two different
190 * stream classes within the same data stream file.
191 */
192 goto end;
e98a2d6e
PP
193 }
194
94cf822e
PP
195 stream = ds_file->stream;
196
197end:
198 return stream;
e98a2d6e
PP
199}
200
9e0c8dbb 201static
d6e69534
PP
202enum bt_msg_iter_medium_status medop_seek(
203 enum bt_msg_iter_seek_whence whence, off_t offset,
9e0c8dbb
JG
204 void *data)
205{
d6e69534
PP
206 enum bt_msg_iter_medium_status ret =
207 BT_MSG_ITER_MEDIUM_STATUS_OK;
9e0c8dbb
JG
208 struct ctf_fs_ds_file *ds_file = data;
209 off_t file_size = ds_file->file->size;
210
d6e69534 211 if (whence != BT_MSG_ITER_SEEK_WHENCE_SET ||
9e0c8dbb
JG
212 offset < 0 || offset > file_size) {
213 BT_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
214 "file-size=%jd", (int) whence, offset,
215 file_size);
d6e69534 216 ret = BT_MSG_ITER_MEDIUM_STATUS_INVAL;
9e0c8dbb
JG
217 goto end;
218 }
219
220 /*
221 * Determine whether or not the destination is contained within the
222 * current mapping.
223 */
224 if (ds_file->mmap_addr && (offset < ds_file->mmap_offset ||
225 offset >= ds_file->mmap_offset + ds_file->mmap_len)) {
226 int unmap_ret;
227 off_t offset_in_mapping = offset % bt_common_get_page_size();
228
229 BT_LOGD("Medium seek request cannot be accomodated by the current "
63489ca2 230 "file mapping: offset=%jd, mmap-offset=%jd, "
9e0c8dbb
JG
231 "mmap-len=%zu", offset, ds_file->mmap_offset,
232 ds_file->mmap_len);
233 unmap_ret = ds_file_munmap(ds_file);
234 if (unmap_ret) {
d6e69534 235 ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
9e0c8dbb
JG
236 goto end;
237 }
238
239 ds_file->mmap_offset = offset - offset_in_mapping;
240 ds_file->request_offset = offset_in_mapping;
241 ret = ds_file_mmap_next(ds_file);
d6e69534 242 if (ret != BT_MSG_ITER_MEDIUM_STATUS_OK) {
9e0c8dbb
JG
243 goto end;
244 }
245 } else {
246 ds_file->request_offset = offset - ds_file->mmap_offset;
247 }
248
249 ds_file->end_reached = (offset == file_size);
250end:
251 return ret;
252}
253
6de92955 254BT_HIDDEN
d6e69534 255struct bt_msg_iter_medium_ops ctf_fs_ds_file_medops = {
e98a2d6e 256 .request_bytes = medop_request_bytes,
312c056a 257 .borrow_stream = medop_borrow_stream,
9e0c8dbb 258 .seek = medop_seek,
e98a2d6e 259};
6de92955 260
97ade20b
JG
261static
262struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length)
263{
264 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
265
266 if (!index) {
267 BT_LOGE_STR("Failed to allocate index");
268 goto error;
269 }
270
271 index->entries = g_array_sized_new(FALSE, TRUE,
272 sizeof(struct ctf_fs_ds_index_entry), length);
273 if (!index->entries) {
274 BT_LOGE("Failed to allocate %zu index entries.", length);
275 goto error;
276 }
277 g_array_set_size(index->entries, length);
278end:
279 return index;
280error:
281 ctf_fs_ds_index_destroy(index);
282 goto end;
283}
e98a2d6e 284
9e0c8dbb
JG
285/* Returns a new, zeroed, index entry. */
286static
287struct ctf_fs_ds_index_entry *ctf_fs_ds_index_add_new_entry(
288 struct ctf_fs_ds_index *index)
289{
290 g_array_set_size(index->entries, index->entries->len + 1);
291 return &g_array_index(index->entries, struct ctf_fs_ds_index_entry,
292 index->entries->len - 1);
293}
294
97ade20b 295static
0f2d58c9 296int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
97ade20b 297 uint64_t cycles, int64_t *ns)
b6c3dcb2 298{
0f2d58c9
PP
299 return bt_util_clock_cycles_to_ns_from_origin(cycles,
300 clock_class->frequency, clock_class->offset_seconds,
301 clock_class->offset_cycles, ns);
97ade20b
JG
302}
303
304static
305struct ctf_fs_ds_index *build_index_from_idx_file(
306 struct ctf_fs_ds_file *ds_file)
307{
308 int ret;
b6c3dcb2
JG
309 gchar *directory = NULL;
310 gchar *basename = NULL;
311 GString *index_basename = NULL;
312 gchar *index_file_path = NULL;
313 GMappedFile *mapped_file = NULL;
314 gsize filesize;
97ade20b
JG
315 const char *mmap_begin = NULL, *file_pos = NULL;
316 const struct ctf_packet_index_file_hdr *header = NULL;
317 struct ctf_fs_ds_index *index = NULL;
318 struct ctf_fs_ds_index_entry *index_entry = NULL;
b6c3dcb2
JG
319 uint64_t total_packets_size = 0;
320 size_t file_index_entry_size;
321 size_t file_entry_count;
322 size_t i;
44c440bc 323 struct ctf_stream_class *sc;
d6e69534 324 struct bt_msg_iter_packet_properties props;
97ade20b
JG
325
326 BT_LOGD("Building index from .idx file of stream file %s",
327 ds_file->file->path->str);
41693723 328 ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props);
97ade20b 329 if (ret) {
41693723 330 BT_LOGD_STR("Cannot read first packet's header and context fields.");
44c440bc
PP
331 goto error;
332 }
333
44c440bc
PP
334 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
335 props.stream_class_id);
336 BT_ASSERT(sc);
337 if (!sc->default_clock_class) {
338 BT_LOGD_STR("Cannot find stream class's default clock class.");
97ade20b
JG
339 goto error;
340 }
b6c3dcb2
JG
341
342 /* Look for index file in relative path index/name.idx. */
94cf822e 343 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2 344 if (!basename) {
97ade20b 345 BT_LOGE("Cannot get the basename of datastream file %s",
55314f2a 346 ds_file->file->path->str);
97ade20b 347 goto error;
b6c3dcb2
JG
348 }
349
94cf822e 350 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2 351 if (!directory) {
97ade20b 352 BT_LOGE("Cannot get dirname of datastream file %s",
55314f2a 353 ds_file->file->path->str);
97ade20b 354 goto error;
b6c3dcb2
JG
355 }
356
357 index_basename = g_string_new(basename);
358 if (!index_basename) {
9e0c8dbb 359 BT_LOGE_STR("Cannot allocate index file basename string");
97ade20b 360 goto error;
b6c3dcb2
JG
361 }
362
363 g_string_append(index_basename, ".idx");
364 index_file_path = g_build_filename(directory, "index",
365 index_basename->str, NULL);
366 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3 367 if (!mapped_file) {
97ade20b 368 BT_LOGD("Cannot create new mapped file %s",
55314f2a 369 index_file_path);
97ade20b 370 goto error;
06367fa3 371 }
d14940a7
JG
372
373 /*
374 * The g_mapped_file API limits us to 4GB files on 32-bit.
375 * Traces with such large indexes have never been seen in the wild,
376 * but this would need to be adjusted to support them.
377 */
b6c3dcb2
JG
378 filesize = g_mapped_file_get_length(mapped_file);
379 if (filesize < sizeof(*header)) {
d14940a7
JG
380 BT_LOGW("Invalid LTTng trace index file: "
381 "file size (%zu bytes) < header size (%zu bytes)",
382 filesize, sizeof(*header));
97ade20b 383 goto error;
b6c3dcb2
JG
384 }
385
386 mmap_begin = g_mapped_file_get_contents(mapped_file);
387 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
388
389 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
390 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
9e0c8dbb 391 BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
97ade20b 392 goto error;
b6c3dcb2 393 }
b6c3dcb2
JG
394
395 file_index_entry_size = be32toh(header->packet_index_len);
396 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
97ade20b 397 if ((filesize - sizeof(*header)) % file_index_entry_size) {
d14940a7
JG
398 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
399 "(%zu bytes) is not a multiple of the index entry size "
400 "(%zu bytes)", (filesize - sizeof(*header)),
401 sizeof(*header));
97ade20b 402 goto error;
b6c3dcb2
JG
403 }
404
97ade20b
JG
405 index = ctf_fs_ds_index_create(file_entry_count);
406 if (!index) {
407 goto error;
b6c3dcb2 408 }
97ade20b
JG
409
410 index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index(
411 index->entries, struct ctf_fs_ds_index_entry, 0);
b6c3dcb2
JG
412 for (i = 0; i < file_entry_count; i++) {
413 struct ctf_packet_index *file_index =
414 (struct ctf_packet_index *) file_pos;
415 uint64_t packet_size = be64toh(file_index->packet_size);
416
417 if (packet_size % CHAR_BIT) {
d14940a7 418 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
97ade20b 419 goto error;
b6c3dcb2
JG
420 }
421
422 /* Convert size in bits to bytes. */
423 packet_size /= CHAR_BIT;
97ade20b 424 index_entry->packet_size = packet_size;
b6c3dcb2 425
97ade20b
JG
426 index_entry->offset = be64toh(file_index->offset);
427 if (i != 0 && index_entry->offset < (index_entry - 1)->offset) {
d14940a7
JG
428 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
429 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
430 (index_entry - 1)->offset, index_entry->offset);
97ade20b 431 goto error;
b6c3dcb2
JG
432 }
433
97ade20b
JG
434 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
435 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
436 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
d14940a7
JG
437 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
438 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
439 index_entry->timestamp_begin,
440 index_entry->timestamp_end);
97ade20b
JG
441 goto error;
442 }
443
444 /* Convert the packet's bound to nanoseconds since Epoch. */
44c440bc 445 ret = convert_cycles_to_ns(sc->default_clock_class,
97ade20b
JG
446 index_entry->timestamp_begin,
447 &index_entry->timestamp_begin_ns);
448 if (ret) {
9e0c8dbb 449 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
97ade20b
JG
450 goto error;
451 }
44c440bc 452 ret = convert_cycles_to_ns(sc->default_clock_class,
97ade20b
JG
453 index_entry->timestamp_end,
454 &index_entry->timestamp_end_ns);
455 if (ret) {
9e0c8dbb 456 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
97ade20b 457 goto error;
b6c3dcb2
JG
458 }
459
460 total_packets_size += packet_size;
461 file_pos += file_index_entry_size;
97ade20b 462 index_entry++;
b6c3dcb2
JG
463 }
464
465 /* Validate that the index addresses the complete stream. */
94cf822e 466 if (ds_file->file->size != total_packets_size) {
d14940a7 467 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
9e0c8dbb 468 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
d14940a7 469 ds_file->file->size, total_packets_size);
97ade20b 470 goto error;
b6c3dcb2
JG
471 }
472end:
473 g_free(directory);
474 g_free(basename);
475 g_free(index_file_path);
06367fa3
JG
476 if (index_basename) {
477 g_string_free(index_basename, TRUE);
478 }
479 if (mapped_file) {
480 g_mapped_file_unref(mapped_file);
481 }
97ade20b
JG
482 return index;
483error:
484 ctf_fs_ds_index_destroy(index);
485 index = NULL;
b6c3dcb2
JG
486 goto end;
487}
488
9e0c8dbb
JG
489static
490int init_index_entry(struct ctf_fs_ds_index_entry *entry,
44c440bc 491 struct ctf_fs_ds_file *ds_file,
d6e69534 492 struct bt_msg_iter_packet_properties *props,
44c440bc 493 off_t packet_size, off_t packet_offset)
9e0c8dbb
JG
494{
495 int ret;
44c440bc 496 struct ctf_stream_class *sc;
9e0c8dbb 497
44c440bc
PP
498 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
499 props->stream_class_id);
500 BT_ASSERT(sc);
f6ccaed9 501 BT_ASSERT(packet_offset >= 0);
9e0c8dbb 502 entry->offset = packet_offset;
f6ccaed9 503 BT_ASSERT(packet_size >= 0);
9e0c8dbb
JG
504 entry->packet_size = packet_size;
505
9e0c8dbb 506 /* Convert the packet's bound to nanoseconds since Epoch. */
44c440bc
PP
507 ret = convert_cycles_to_ns(sc->default_clock_class,
508 props->snapshots.beginning_clock,
9e0c8dbb
JG
509 &entry->timestamp_begin_ns);
510 if (ret) {
511 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
512 goto end;
513 }
514
44c440bc
PP
515 ret = convert_cycles_to_ns(sc->default_clock_class,
516 props->snapshots.end_clock,
9e0c8dbb
JG
517 &entry->timestamp_end_ns);
518 if (ret) {
519 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
520 goto end;
521 }
0b29603d 522
9e0c8dbb 523end:
9e0c8dbb
JG
524 return ret;
525}
526
527static
528struct ctf_fs_ds_index *build_index_from_stream_file(
529 struct ctf_fs_ds_file *ds_file)
530{
531 int ret;
532 struct ctf_fs_ds_index *index = NULL;
d6e69534 533 enum bt_msg_iter_status iter_status;
9e0c8dbb
JG
534
535 BT_LOGD("Indexing stream file %s", ds_file->file->path->str);
536
537 index = ctf_fs_ds_index_create(0);
538 if (!index) {
539 goto error;
540 }
541
542 do {
543 off_t current_packet_offset;
544 off_t next_packet_offset;
44c440bc 545 off_t current_packet_size_bytes;
9e0c8dbb 546 struct ctf_fs_ds_index_entry *entry;
d6e69534 547 struct bt_msg_iter_packet_properties props;
9e0c8dbb 548
41693723
PP
549 iter_status = bt_msg_iter_get_packet_properties(
550 ds_file->msg_iter, &props);
d6e69534
PP
551 if (iter_status != BT_MSG_ITER_STATUS_OK) {
552 if (iter_status == BT_MSG_ITER_STATUS_EOF) {
9e0c8dbb
JG
553 break;
554 }
555 goto error;
556 }
312c056a 557
9e0c8dbb 558 current_packet_offset =
d6e69534
PP
559 bt_msg_iter_get_current_packet_offset(
560 ds_file->msg_iter);
9e0c8dbb
JG
561 if (current_packet_offset < 0) {
562 BT_LOGE_STR("Cannot get the current packet's offset.");
563 goto error;
564 }
565
9e0c8dbb 566 current_packet_size_bytes =
44c440bc 567 ((props.exp_packet_total_size + 7) & ~7) / CHAR_BIT;
9e0c8dbb
JG
568
569 if (current_packet_offset + current_packet_size_bytes >
570 ds_file->file->size) {
571 BT_LOGW("Invalid packet size reported in file: stream=\"%s\", "
572 "packet-offset=%jd, packet-size-bytes=%jd, "
573 "file-size=%jd",
574 ds_file->file->path->str,
575 current_packet_offset,
576 current_packet_size_bytes,
577 ds_file->file->size);
578 goto error;
579 }
580
581 next_packet_offset = current_packet_offset +
312c056a 582 current_packet_size_bytes;
9e0c8dbb 583 BT_LOGD("Seeking to next packet: current-packet-offset=%jd, "
312c056a
PP
584 "next-packet-offset=%jd", current_packet_offset,
585 next_packet_offset);
9e0c8dbb
JG
586
587 entry = ctf_fs_ds_index_add_new_entry(index);
588 if (!entry) {
589 BT_LOGE_STR("Failed to allocate a new index entry.");
590 goto error;
591 }
592
44c440bc
PP
593 ret = init_index_entry(entry, ds_file, &props,
594 current_packet_size_bytes, current_packet_offset);
9e0c8dbb
JG
595 if (ret) {
596 goto error;
597 }
598
d6e69534 599 iter_status = bt_msg_iter_seek(ds_file->msg_iter,
9e0c8dbb 600 next_packet_offset);
d6e69534 601 } while (iter_status == BT_MSG_ITER_STATUS_OK);
9e0c8dbb 602
d6e69534 603 if (iter_status != BT_MSG_ITER_STATUS_EOF) {
9e0c8dbb
JG
604 goto error;
605 }
312c056a 606
9e0c8dbb 607end:
9e0c8dbb 608 return index;
312c056a 609
9e0c8dbb
JG
610error:
611 ctf_fs_ds_index_destroy(index);
612 index = NULL;
613 goto end;
614}
615
e7a4393b 616BT_HIDDEN
94cf822e
PP
617struct ctf_fs_ds_file *ctf_fs_ds_file_create(
618 struct ctf_fs_trace *ctf_fs_trace,
d6e69534
PP
619 bt_self_message_iterator *pc_msg_iter,
620 struct bt_msg_iter *msg_iter,
b19ff26f 621 bt_stream *stream, const char *path)
e98a2d6e 622{
b6c3dcb2 623 int ret;
55314f2a 624 const size_t page_size = bt_common_get_page_size();
94cf822e 625 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 626
94cf822e 627 if (!ds_file) {
e98a2d6e
PP
628 goto error;
629 }
630
d6e69534 631 ds_file->pc_msg_iter = pc_msg_iter;
55314f2a 632 ds_file->file = ctf_fs_file_create();
94cf822e 633 if (!ds_file->file) {
4f1f88a6
PP
634 goto error;
635 }
636
398454ed 637 ds_file->stream = stream;
c5b9b441 638 bt_stream_get_ref(ds_file->stream);
44c440bc 639 ds_file->metadata = ctf_fs_trace->metadata;
94cf822e 640 g_string_assign(ds_file->file->path, path);
55314f2a 641 ret = ctf_fs_file_open(ds_file->file, "rb");
4f1f88a6
PP
642 if (ret) {
643 goto error;
644 }
645
d6e69534
PP
646 ds_file->msg_iter = msg_iter;
647 bt_msg_iter_set_medops_data(ds_file->msg_iter, ds_file);
648 if (!ds_file->msg_iter) {
e98a2d6e
PP
649 goto error;
650 }
5b29e799 651
55314f2a 652 ds_file->mmap_max_len = page_size * 2048;
94cf822e 653
e98a2d6e 654 goto end;
1a9f7075 655
e98a2d6e 656error:
78586d8a 657 /* Do not touch "borrowed" file. */
94cf822e
PP
658 ctf_fs_ds_file_destroy(ds_file);
659 ds_file = NULL;
1a9f7075 660
e98a2d6e 661end:
94cf822e 662 return ds_file;
e98a2d6e
PP
663}
664
97ade20b
JG
665BT_HIDDEN
666struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
667 struct ctf_fs_ds_file *ds_file)
668{
9e0c8dbb
JG
669 struct ctf_fs_ds_index *index;
670
671 index = build_index_from_idx_file(ds_file);
672 if (index) {
673 goto end;
674 }
675
676 BT_LOGD("Failed to build index from .index file; "
677 "falling back to stream indexing.");
678 index = build_index_from_stream_file(ds_file);
679end:
680 return index;
97ade20b
JG
681}
682
5b29e799 683BT_HIDDEN
94cf822e 684void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 685{
94cf822e 686 if (!ds_file) {
5b29e799 687 return;
043e2020
JG
688 }
689
c5b9b441 690 bt_stream_put_ref(ds_file->stream);
94cf822e 691 (void) ds_file_munmap(ds_file);
0982a26d 692
94cf822e
PP
693 if (ds_file->file) {
694 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
695 }
696
94cf822e 697 g_free(ds_file);
e98a2d6e 698}
4f1f88a6
PP
699
700BT_HIDDEN
4cdfc5e8 701bt_message_iterator_status ctf_fs_ds_file_next(
d4393e08 702 struct ctf_fs_ds_file *ds_file,
d6e69534 703 bt_message **msg)
4f1f88a6 704{
d6e69534 705 enum bt_msg_iter_status msg_iter_status;
4cdfc5e8 706 bt_message_iterator_status status;
4f1f88a6 707
d6e69534
PP
708 msg_iter_status = bt_msg_iter_get_next_message(
709 ds_file->msg_iter, ds_file->pc_msg_iter, msg);
4f1f88a6 710
d6e69534
PP
711 switch (msg_iter_status) {
712 case BT_MSG_ITER_STATUS_EOF:
713 status = BT_MESSAGE_ITERATOR_STATUS_END;
4f1f88a6 714 break;
d6e69534
PP
715 case BT_MSG_ITER_STATUS_OK:
716 status = BT_MESSAGE_ITERATOR_STATUS_OK;
4f1f88a6 717 break;
d6e69534 718 case BT_MSG_ITER_STATUS_AGAIN:
4f1f88a6
PP
719 /*
720 * Should not make it this far as this is
721 * medium-specific; there is nothing for the user to do
722 * and it should have been handled upstream.
723 */
0fbb9a9f 724 abort();
d6e69534
PP
725 case BT_MSG_ITER_STATUS_INVAL:
726 case BT_MSG_ITER_STATUS_ERROR:
4f1f88a6 727 default:
d6e69534 728 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
4f1f88a6
PP
729 break;
730 }
d4393e08 731 return status;
4f1f88a6 732}
94cf822e 733
97ade20b
JG
734BT_HIDDEN
735void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
736{
737 if (!index) {
738 return;
739 }
740
741 if (index->entries) {
742 g_array_free(index->entries, TRUE);
743 }
744 g_free(index);
745}
This page took 0.079386 seconds and 4 git commands to generate.