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