Port fix: don't round mmap offset to next page
[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>
e98a2d6e 34#include <babeltrace/ctf-ir/stream.h>
b2e0c907 35#include <babeltrace/graph/notification-iterator.h>
4f1f88a6
PP
36#include <babeltrace/graph/notification-stream.h>
37#include <babeltrace/graph/notification-event.h>
38#include <babeltrace/graph/notification-packet.h>
55314f2a 39#include <babeltrace/common-internal.h>
78586d8a
JG
40#include "file.h"
41#include "metadata.h"
42#include "../common/notif-iter/notif-iter.h"
43#include <assert.h>
94cf822e 44#include "data-stream-file.h"
e9383dfd 45#include <string.h>
e98a2d6e 46
55314f2a
JG
47#define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS"
48#include "logging.h"
e98a2d6e 49
4f1f88a6 50static inline
94cf822e 51size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
e98a2d6e 52{
2c701ca6 53 return ds_file->mmap_len - ds_file->request_offset;
e98a2d6e
PP
54}
55
5b29e799 56static
94cf822e 57int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
e98a2d6e 58{
fc9a526c 59 int ret = 0;
e98a2d6e 60
d238196d 61 if (!ds_file || !ds_file->mmap_addr) {
94cf822e
PP
62 goto end;
63 }
64
04394229 65 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
cd232764
JG
66 BT_LOGE_ERRNO("Cannot memory-unmap file",
67 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
94cf822e 68 ds_file->mmap_addr, ds_file->mmap_len,
cd232764
JG
69 ds_file->file->path->str,
70 ds_file->file ? ds_file->file->fp : NULL);
fc9a526c
JG
71 ret = -1;
72 goto end;
e98a2d6e 73 }
94cf822e
PP
74
75 ds_file->mmap_addr = NULL;
76
fc9a526c
JG
77end:
78 return ret;
e98a2d6e
PP
79}
80
5b29e799 81static
94cf822e
PP
82enum bt_ctf_notif_iter_medium_status ds_file_mmap_next(
83 struct ctf_fs_ds_file *ds_file)
e98a2d6e 84{
55314f2a 85 const size_t page_size = bt_common_get_page_size();
e0f6a64a
JG
86 enum bt_ctf_notif_iter_medium_status ret =
87 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
e98a2d6e
PP
88
89 /* Unmap old region */
94cf822e
PP
90 if (ds_file->mmap_addr) {
91 if (ds_file_munmap(ds_file)) {
e98a2d6e
PP
92 goto error;
93 }
94
2f5a009b 95 /*
2c701ca6 96 * mmap_len is guaranteed to be page-aligned except on the
2f5a009b
JG
97 * last mapping where it may not be possible (since the file's
98 * size itself may not be a page multiple).
99 */
2c701ca6 100 ds_file->mmap_offset += ds_file->mmap_len;
94cf822e 101 ds_file->request_offset = 0;
e98a2d6e
PP
102 }
103
2c701ca6 104 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset,
94cf822e 105 ds_file->mmap_max_len);
2c701ca6 106 if (ds_file->mmap_len == 0) {
e0f6a64a
JG
107 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
108 goto end;
109 }
e98a2d6e 110 /* Map new region */
94cf822e 111 assert(ds_file->mmap_len);
04394229 112 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
94cf822e
PP
113 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
114 ds_file->mmap_offset);
115 if (ds_file->mmap_addr == MAP_FAILED) {
1974687e 116 BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
94cf822e 117 ds_file->mmap_len, ds_file->file->path->str,
1974687e 118 ds_file->file->fp, (intmax_t) ds_file->mmap_offset,
78586d8a 119 strerror(errno));
e98a2d6e
PP
120 goto error;
121 }
122
123 goto end;
e98a2d6e 124error:
94cf822e 125 ds_file_munmap(ds_file);
e0f6a64a 126 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
127end:
128 return ret;
129}
130
5b29e799
JG
131static
132enum bt_ctf_notif_iter_medium_status medop_request_bytes(
e98a2d6e
PP
133 size_t request_sz, uint8_t **buffer_addr,
134 size_t *buffer_sz, void *data)
135{
136 enum bt_ctf_notif_iter_medium_status status =
137 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
94cf822e 138 struct ctf_fs_ds_file *ds_file = data;
e98a2d6e
PP
139
140 if (request_sz == 0) {
141 goto end;
142 }
143
e98a2d6e 144 /* Check if we have at least one memory-mapped byte left */
94cf822e 145 if (remaining_mmap_bytes(ds_file) == 0) {
e98a2d6e 146 /* Are we at the end of the file? */
94cf822e 147 if (ds_file->mmap_offset >= ds_file->file->size) {
55314f2a 148 BT_LOGD("Reached end of file \"%s\" (%p)",
94cf822e 149 ds_file->file->path->str, ds_file->file->fp);
e98a2d6e
PP
150 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
151 goto end;
152 }
153
94cf822e 154 status = ds_file_mmap_next(ds_file);
e0f6a64a
JG
155 switch (status) {
156 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK:
157 break;
158 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF:
159 goto end;
160 default:
55314f2a 161 BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
94cf822e
PP
162 ds_file->file->path->str,
163 ds_file->file->fp);
e98a2d6e
PP
164 goto error;
165 }
166 }
167
94cf822e
PP
168 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
169 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
170 ds_file->request_offset += *buffer_sz;
e98a2d6e
PP
171 goto end;
172
173error:
174 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
175
176end:
177 return status;
178}
179
5b29e799
JG
180static
181struct bt_ctf_stream *medop_get_stream(
b92735af
PP
182 struct bt_ctf_stream_class *stream_class, uint64_t stream_id,
183 void *data)
e98a2d6e 184{
94cf822e
PP
185 struct ctf_fs_ds_file *ds_file = data;
186 struct bt_ctf_stream_class *ds_file_stream_class;
187 struct bt_ctf_stream *stream = NULL;
188
189 ds_file_stream_class = bt_ctf_stream_get_class(ds_file->stream);
190 bt_put(ds_file_stream_class);
191
192 if (stream_class != ds_file_stream_class) {
193 /*
194 * Not supported: two packets described by two different
195 * stream classes within the same data stream file.
196 */
197 goto end;
e98a2d6e
PP
198 }
199
94cf822e
PP
200 stream = ds_file->stream;
201
202end:
203 return stream;
e98a2d6e
PP
204}
205
6de92955
PP
206BT_HIDDEN
207struct bt_ctf_notif_iter_medium_ops ctf_fs_ds_file_medops = {
e98a2d6e
PP
208 .request_bytes = medop_request_bytes,
209 .get_stream = medop_get_stream,
210};
6de92955 211
97ade20b
JG
212static
213struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length)
214{
215 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
216
217 if (!index) {
218 BT_LOGE_STR("Failed to allocate index");
219 goto error;
220 }
221
222 index->entries = g_array_sized_new(FALSE, TRUE,
223 sizeof(struct ctf_fs_ds_index_entry), length);
224 if (!index->entries) {
225 BT_LOGE("Failed to allocate %zu index entries.", length);
226 goto error;
227 }
228 g_array_set_size(index->entries, length);
229end:
230 return index;
231error:
232 ctf_fs_ds_index_destroy(index);
233 goto end;
234}
e98a2d6e 235
b6c3dcb2 236static
97ade20b
JG
237struct bt_ctf_clock_class *get_field_mapped_clock_class(
238 struct bt_ctf_field *field)
239{
240 struct bt_ctf_field_type *field_type;
241 struct bt_ctf_clock_class *clock_class = NULL;
242
243 field_type = bt_ctf_field_get_type(field);
244 if (!field_type) {
245 goto end;
246 }
247
248 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(
249 field_type);
250 if (!clock_class) {
251 goto end;
252 }
253end:
254 bt_put(field_type);
255 return clock_class;
256}
257
258static
259int get_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file *ds_file,
260 struct bt_ctf_clock_class **_timestamp_begin_cc,
261 struct bt_ctf_clock_class **_timestamp_end_cc)
262{
263 int ret;
89b08333 264 struct bt_ctf_field *timestamp_field = NULL;
97ade20b
JG
265 struct bt_ctf_field *packet_context_field = NULL;
266 struct bt_ctf_clock_class *timestamp_begin_cc = NULL;
267 struct bt_ctf_clock_class *timestamp_end_cc = NULL;
268
269 ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file,
270 NULL, &packet_context_field);
271 if (ret || !packet_context_field) {
272 BT_LOGD("Cannot retrieve packet context field of stream \'%s\'",
273 ds_file->file->path->str);
274 ret = -1;
275 goto end;
276 }
277
278 timestamp_field = bt_ctf_field_structure_get_field_by_name(
279 packet_context_field, "timestamp_begin");
280 if (!timestamp_field) {
281 BT_LOGD("Cannot retrieve timestamp_begin field in packet context of stream \'%s\'",
282 ds_file->file->path->str);
283 ret = -1;
284 goto end;
285 }
286
287 timestamp_begin_cc = get_field_mapped_clock_class(timestamp_field);
288 if (!timestamp_begin_cc) {
289 BT_LOGD("Cannot retrieve the clock mapped to timestamp_begin of stream \'%s\'",
290 ds_file->file->path->str);
291 }
89b08333 292 BT_PUT(timestamp_field);
97ade20b
JG
293
294 timestamp_field = bt_ctf_field_structure_get_field_by_name(
295 packet_context_field, "timestamp_end");
296 if (!timestamp_field) {
297 BT_LOGD("Cannot retrieve timestamp_end field in packet context of stream \'%s\'",
298 ds_file->file->path->str);
299 ret = -1;
300 goto end;
301 }
302
303 timestamp_end_cc = get_field_mapped_clock_class(timestamp_field);
304 if (!timestamp_end_cc) {
305 BT_LOGD("Cannot retrieve the clock mapped to timestamp_end in stream \'%s\'",
306 ds_file->file->path->str);
307 }
308
309 if (_timestamp_begin_cc) {
310 *_timestamp_begin_cc = bt_get(timestamp_begin_cc);
311 }
312 if (_timestamp_end_cc) {
313 *_timestamp_end_cc = bt_get(timestamp_end_cc);
314 }
315end:
316 bt_put(packet_context_field);
317 bt_put(timestamp_field);
318 bt_put(timestamp_begin_cc);
319 bt_put(timestamp_end_cc);
320 return ret;
321}
322
323static
324int convert_cycles_to_ns(struct bt_ctf_clock_class *clock_class,
325 uint64_t cycles, int64_t *ns)
b6c3dcb2
JG
326{
327 int ret = 0;
97ade20b
JG
328 struct bt_ctf_clock_value *clock_value;
329
330 assert(ns);
331 clock_value = bt_ctf_clock_value_create(clock_class, cycles);
332 if (!clock_value) {
333 ret = -1;
334 goto end;
335 }
336
337 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns);
338 if (ret) {
339 goto end;
340 }
341end:
342 bt_put(clock_value);
343 return ret;
344}
345
346static
347struct ctf_fs_ds_index *build_index_from_idx_file(
348 struct ctf_fs_ds_file *ds_file)
349{
350 int ret;
b6c3dcb2
JG
351 gchar *directory = NULL;
352 gchar *basename = NULL;
353 GString *index_basename = NULL;
354 gchar *index_file_path = NULL;
355 GMappedFile *mapped_file = NULL;
356 gsize filesize;
97ade20b
JG
357 const char *mmap_begin = NULL, *file_pos = NULL;
358 const struct ctf_packet_index_file_hdr *header = NULL;
359 struct ctf_fs_ds_index *index = NULL;
360 struct ctf_fs_ds_index_entry *index_entry = NULL;
b6c3dcb2
JG
361 uint64_t total_packets_size = 0;
362 size_t file_index_entry_size;
363 size_t file_entry_count;
364 size_t i;
97ade20b
JG
365 struct bt_ctf_clock_class *timestamp_begin_cc = NULL;
366 struct bt_ctf_clock_class *timestamp_end_cc = NULL;
367
368 BT_LOGD("Building index from .idx file of stream file %s",
369 ds_file->file->path->str);
370
371 ret = get_ds_file_packet_bounds_clock_classes(ds_file,
372 &timestamp_begin_cc, &timestamp_end_cc);
373 if (ret) {
374 BT_LOGD("Cannot get clock classes of \"timestamp_begin\" and \"timestamp_end\" fields");
375 goto error;
376 }
b6c3dcb2
JG
377
378 /* Look for index file in relative path index/name.idx. */
94cf822e 379 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2 380 if (!basename) {
97ade20b 381 BT_LOGE("Cannot get the basename of datastream file %s",
55314f2a 382 ds_file->file->path->str);
97ade20b 383 goto error;
b6c3dcb2
JG
384 }
385
94cf822e 386 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2 387 if (!directory) {
97ade20b 388 BT_LOGE("Cannot get dirname of datastream file %s",
55314f2a 389 ds_file->file->path->str);
97ade20b 390 goto error;
b6c3dcb2
JG
391 }
392
393 index_basename = g_string_new(basename);
394 if (!index_basename) {
97ade20b
JG
395 BT_LOGE("Cannot allocate index file basename string");
396 goto error;
b6c3dcb2
JG
397 }
398
399 g_string_append(index_basename, ".idx");
400 index_file_path = g_build_filename(directory, "index",
401 index_basename->str, NULL);
402 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3 403 if (!mapped_file) {
97ade20b 404 BT_LOGD("Cannot create new mapped file %s",
55314f2a 405 index_file_path);
97ade20b 406 goto error;
06367fa3 407 }
d14940a7
JG
408
409 /*
410 * The g_mapped_file API limits us to 4GB files on 32-bit.
411 * Traces with such large indexes have never been seen in the wild,
412 * but this would need to be adjusted to support them.
413 */
b6c3dcb2
JG
414 filesize = g_mapped_file_get_length(mapped_file);
415 if (filesize < sizeof(*header)) {
d14940a7
JG
416 BT_LOGW("Invalid LTTng trace index file: "
417 "file size (%zu bytes) < header size (%zu bytes)",
418 filesize, sizeof(*header));
97ade20b 419 goto error;
b6c3dcb2
JG
420 }
421
422 mmap_begin = g_mapped_file_get_contents(mapped_file);
423 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
424
425 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
426 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
d14940a7 427 BT_LOGW("Invalid LTTng trace index: \"magic\" field validation failed");
97ade20b 428 goto error;
b6c3dcb2 429 }
b6c3dcb2
JG
430
431 file_index_entry_size = be32toh(header->packet_index_len);
432 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
97ade20b 433 if ((filesize - sizeof(*header)) % file_index_entry_size) {
d14940a7
JG
434 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
435 "(%zu bytes) is not a multiple of the index entry size "
436 "(%zu bytes)", (filesize - sizeof(*header)),
437 sizeof(*header));
97ade20b 438 goto error;
b6c3dcb2
JG
439 }
440
97ade20b
JG
441 index = ctf_fs_ds_index_create(file_entry_count);
442 if (!index) {
443 goto error;
b6c3dcb2 444 }
97ade20b
JG
445
446 index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index(
447 index->entries, struct ctf_fs_ds_index_entry, 0);
b6c3dcb2
JG
448 for (i = 0; i < file_entry_count; i++) {
449 struct ctf_packet_index *file_index =
450 (struct ctf_packet_index *) file_pos;
451 uint64_t packet_size = be64toh(file_index->packet_size);
452
453 if (packet_size % CHAR_BIT) {
d14940a7 454 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
97ade20b 455 goto error;
b6c3dcb2
JG
456 }
457
458 /* Convert size in bits to bytes. */
459 packet_size /= CHAR_BIT;
97ade20b 460 index_entry->packet_size = packet_size;
b6c3dcb2 461
97ade20b
JG
462 index_entry->offset = be64toh(file_index->offset);
463 if (i != 0 && index_entry->offset < (index_entry - 1)->offset) {
d14940a7
JG
464 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
465 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
466 (index_entry - 1)->offset, index_entry->offset);
97ade20b 467 goto error;
b6c3dcb2
JG
468 }
469
97ade20b
JG
470 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
471 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
472 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
d14940a7
JG
473 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
474 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
475 index_entry->timestamp_begin,
476 index_entry->timestamp_end);
97ade20b
JG
477 goto error;
478 }
479
480 /* Convert the packet's bound to nanoseconds since Epoch. */
481 ret = convert_cycles_to_ns(timestamp_begin_cc,
482 index_entry->timestamp_begin,
483 &index_entry->timestamp_begin_ns);
484 if (ret) {
d14940a7 485 BT_LOGD("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
97ade20b
JG
486 goto error;
487 }
488 ret = convert_cycles_to_ns(timestamp_end_cc,
489 index_entry->timestamp_end,
490 &index_entry->timestamp_end_ns);
491 if (ret) {
d14940a7 492 BT_LOGD("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
97ade20b 493 goto error;
b6c3dcb2
JG
494 }
495
496 total_packets_size += packet_size;
497 file_pos += file_index_entry_size;
97ade20b 498 index_entry++;
b6c3dcb2
JG
499 }
500
501 /* Validate that the index addresses the complete stream. */
94cf822e 502 if (ds_file->file->size != total_packets_size) {
d14940a7
JG
503 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
504 "file_size=%" PRIu64 ", total_packets_size=%" PRIu64,
505 ds_file->file->size, total_packets_size);
97ade20b 506 goto error;
b6c3dcb2
JG
507 }
508end:
509 g_free(directory);
510 g_free(basename);
511 g_free(index_file_path);
06367fa3
JG
512 if (index_basename) {
513 g_string_free(index_basename, TRUE);
514 }
515 if (mapped_file) {
516 g_mapped_file_unref(mapped_file);
517 }
97ade20b
JG
518 bt_put(timestamp_begin_cc);
519 bt_put(timestamp_end_cc);
520 return index;
521error:
522 ctf_fs_ds_index_destroy(index);
523 index = NULL;
b6c3dcb2
JG
524 goto end;
525}
526
e7a4393b 527BT_HIDDEN
94cf822e
PP
528struct ctf_fs_ds_file *ctf_fs_ds_file_create(
529 struct ctf_fs_trace *ctf_fs_trace,
6de92955 530 struct bt_ctf_notif_iter *notif_iter,
97ade20b 531 struct bt_ctf_stream *stream, const char *path)
e98a2d6e 532{
b6c3dcb2 533 int ret;
55314f2a 534 const size_t page_size = bt_common_get_page_size();
94cf822e 535 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 536
94cf822e 537 if (!ds_file) {
e98a2d6e
PP
538 goto error;
539 }
540
55314f2a 541 ds_file->file = ctf_fs_file_create();
94cf822e 542 if (!ds_file->file) {
4f1f88a6
PP
543 goto error;
544 }
545
94cf822e
PP
546 ds_file->stream = bt_get(stream);
547 ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map);
548 g_string_assign(ds_file->file->path, path);
55314f2a 549 ret = ctf_fs_file_open(ds_file->file, "rb");
4f1f88a6
PP
550 if (ret) {
551 goto error;
552 }
553
6de92955
PP
554 ds_file->notif_iter = notif_iter;
555 bt_ctf_notif_iter_set_medops_data(ds_file->notif_iter, ds_file);
94cf822e 556 if (!ds_file->notif_iter) {
e98a2d6e
PP
557 goto error;
558 }
5b29e799 559
55314f2a 560 ds_file->mmap_max_len = page_size * 2048;
94cf822e 561
e98a2d6e 562 goto end;
1a9f7075 563
e98a2d6e 564error:
78586d8a 565 /* Do not touch "borrowed" file. */
94cf822e
PP
566 ctf_fs_ds_file_destroy(ds_file);
567 ds_file = NULL;
1a9f7075 568
e98a2d6e 569end:
94cf822e 570 return ds_file;
e98a2d6e
PP
571}
572
97ade20b
JG
573BT_HIDDEN
574struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
575 struct ctf_fs_ds_file *ds_file)
576{
577 return build_index_from_idx_file(ds_file);
578}
579
5b29e799 580BT_HIDDEN
94cf822e 581void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 582{
94cf822e 583 if (!ds_file) {
5b29e799 584 return;
043e2020
JG
585 }
586
94cf822e
PP
587 bt_put(ds_file->cc_prio_map);
588 bt_put(ds_file->stream);
589 (void) ds_file_munmap(ds_file);
0982a26d 590
94cf822e
PP
591 if (ds_file->file) {
592 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
593 }
594
94cf822e 595 g_free(ds_file);
e98a2d6e 596}
4f1f88a6
PP
597
598BT_HIDDEN
94cf822e
PP
599struct bt_notification_iterator_next_return ctf_fs_ds_file_next(
600 struct ctf_fs_ds_file *ds_file)
4f1f88a6
PP
601{
602 enum bt_ctf_notif_iter_status notif_iter_status;
603 struct bt_notification_iterator_next_return ret = {
604 .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR,
605 .notification = NULL,
606 };
607
8915b7a7 608 notif_iter_status = bt_ctf_notif_iter_get_next_notification(
94cf822e 609 ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification);
4f1f88a6 610
4f1f88a6
PP
611 switch (notif_iter_status) {
612 case BT_CTF_NOTIF_ITER_STATUS_EOF:
613 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
614 break;
615 case BT_CTF_NOTIF_ITER_STATUS_OK:
616 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
617 break;
618 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
619 /*
620 * Should not make it this far as this is
621 * medium-specific; there is nothing for the user to do
622 * and it should have been handled upstream.
623 */
0fbb9a9f 624 abort();
4f1f88a6
PP
625 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
626 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
627 default:
628 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
629 break;
630 }
631
632 return ret;
633}
94cf822e
PP
634
635BT_HIDDEN
636int ctf_fs_ds_file_get_packet_header_context_fields(
97ade20b 637 struct ctf_fs_ds_file *ds_file,
94cf822e
PP
638 struct bt_ctf_field **packet_header_field,
639 struct bt_ctf_field **packet_context_field)
640{
641 enum bt_ctf_notif_iter_status notif_iter_status;
94cf822e
PP
642 int ret = 0;
643
97ade20b 644 assert(ds_file);
94cf822e
PP
645 notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields(
646 ds_file->notif_iter, packet_header_field, packet_context_field);
647 switch (notif_iter_status) {
648 case BT_CTF_NOTIF_ITER_STATUS_EOF:
649 case BT_CTF_NOTIF_ITER_STATUS_OK:
650 break;
651 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
0fbb9a9f 652 abort();
94cf822e
PP
653 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
654 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
655 default:
656 goto error;
657 break;
658 }
659
660 goto end;
661
662error:
663 ret = -1;
664
665 if (packet_header_field) {
666 bt_put(*packet_header_field);
667 }
668
669 if (packet_context_field) {
670 bt_put(*packet_context_field);
671 }
672
673end:
94cf822e
PP
674 return ret;
675}
97ade20b
JG
676
677BT_HIDDEN
678void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
679{
680 if (!index) {
681 return;
682 }
683
684 if (index->entries) {
685 g_array_free(index->entries, TRUE);
686 }
687 g_free(index);
688}
This page took 0.066577 seconds and 4 git commands to generate.