Move ctf-fs source query implementations to their own file
[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>
78586d8a
JG
39#include "file.h"
40#include "metadata.h"
41#include "../common/notif-iter/notif-iter.h"
42#include <assert.h>
94cf822e 43#include "data-stream-file.h"
e9383dfd 44#include <string.h>
e98a2d6e
PP
45
46#define PRINT_ERR_STREAM ctf_fs->error_fp
47#define PRINT_PREFIX "ctf-fs-data-stream"
cd00e1d3
MD
48#define PRINT_DBG_CHECK ctf_fs_debug
49#include "../print.h"
e98a2d6e 50
4f1f88a6 51static inline
94cf822e 52size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
e98a2d6e 53{
94cf822e 54 return ds_file->mmap_valid_len - ds_file->request_offset;
e98a2d6e
PP
55}
56
5b29e799 57static
94cf822e 58int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
e98a2d6e 59{
fc9a526c 60 int ret = 0;
d238196d 61 struct ctf_fs_component *ctf_fs;
e98a2d6e 62
d238196d 63 if (!ds_file || !ds_file->mmap_addr) {
94cf822e
PP
64 goto end;
65 }
66
d238196d 67 ctf_fs = ds_file->file->ctf_fs;
94cf822e 68 if (munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
e98a2d6e 69 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
94cf822e
PP
70 ds_file->mmap_addr, ds_file->mmap_len,
71 ds_file->file->path->str, ds_file->file->fp,
e98a2d6e 72 strerror(errno));
fc9a526c
JG
73 ret = -1;
74 goto end;
e98a2d6e 75 }
94cf822e
PP
76
77 ds_file->mmap_addr = NULL;
78
fc9a526c
JG
79end:
80 return ret;
e98a2d6e
PP
81}
82
5b29e799 83static
94cf822e
PP
84enum bt_ctf_notif_iter_medium_status ds_file_mmap_next(
85 struct ctf_fs_ds_file *ds_file)
e98a2d6e 86{
e0f6a64a
JG
87 enum bt_ctf_notif_iter_medium_status ret =
88 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
94cf822e 89 struct ctf_fs_component *ctf_fs = ds_file->file->ctf_fs;
e98a2d6e
PP
90
91 /* Unmap old region */
94cf822e
PP
92 if (ds_file->mmap_addr) {
93 if (ds_file_munmap(ds_file)) {
e98a2d6e
PP
94 goto error;
95 }
96
94cf822e
PP
97 ds_file->mmap_offset += ds_file->mmap_valid_len;
98 ds_file->request_offset = 0;
e98a2d6e
PP
99 }
100
94cf822e
PP
101 ds_file->mmap_valid_len = MIN(ds_file->file->size - ds_file->mmap_offset,
102 ds_file->mmap_max_len);
103 if (ds_file->mmap_valid_len == 0) {
e0f6a64a
JG
104 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
105 goto end;
106 }
fc9a526c 107 /* Round up to next page, assuming page size being a power of 2. */
94cf822e 108 ds_file->mmap_len = (ds_file->mmap_valid_len + ctf_fs->page_size - 1)
fc9a526c 109 & ~(ctf_fs->page_size - 1);
e98a2d6e 110 /* Map new region */
94cf822e
PP
111 assert(ds_file->mmap_len);
112 ds_file->mmap_addr = mmap((void *) 0, ds_file->mmap_len,
113 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
114 ds_file->mmap_offset);
115 if (ds_file->mmap_addr == MAP_FAILED) {
e98a2d6e 116 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
94cf822e
PP
117 ds_file->mmap_len, ds_file->file->path->str,
118 ds_file->file->fp, 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
PP
138 struct ctf_fs_ds_file *ds_file = data;
139 struct ctf_fs_component *ctf_fs = ds_file->file->ctf_fs;
e98a2d6e
PP
140
141 if (request_sz == 0) {
142 goto end;
143 }
144
e98a2d6e 145 /* Check if we have at least one memory-mapped byte left */
94cf822e 146 if (remaining_mmap_bytes(ds_file) == 0) {
e98a2d6e 147 /* Are we at the end of the file? */
94cf822e 148 if (ds_file->mmap_offset >= ds_file->file->size) {
e98a2d6e 149 PDBG("Reached end of file \"%s\" (%p)\n",
94cf822e 150 ds_file->file->path->str, ds_file->file->fp);
e98a2d6e
PP
151 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
152 goto end;
153 }
154
94cf822e 155 status = ds_file_mmap_next(ds_file);
e0f6a64a
JG
156 switch (status) {
157 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK:
158 break;
159 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF:
160 goto end;
161 default:
e98a2d6e 162 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
94cf822e
PP
163 ds_file->file->path->str,
164 ds_file->file->fp);
e98a2d6e
PP
165 goto error;
166 }
167 }
168
94cf822e
PP
169 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
170 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
171 ds_file->request_offset += *buffer_sz;
e98a2d6e
PP
172 goto end;
173
174error:
175 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
176
177end:
178 return status;
179}
180
5b29e799
JG
181static
182struct bt_ctf_stream *medop_get_stream(
e98a2d6e
PP
183 struct bt_ctf_stream_class *stream_class, void *data)
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
206static struct bt_ctf_notif_iter_medium_ops medops = {
207 .request_bytes = medop_request_bytes,
208 .get_stream = medop_get_stream,
209};
210
b6c3dcb2 211static
94cf822e 212int build_index_from_idx_file(struct ctf_fs_ds_file *ds_file)
b6c3dcb2
JG
213{
214 int ret = 0;
215 gchar *directory = NULL;
216 gchar *basename = NULL;
217 GString *index_basename = NULL;
218 gchar *index_file_path = NULL;
219 GMappedFile *mapped_file = NULL;
220 gsize filesize;
221 const struct ctf_packet_index_file_hdr *header;
222 const char *mmap_begin, *file_pos;
223 struct index_entry *index;
224 uint64_t total_packets_size = 0;
225 size_t file_index_entry_size;
226 size_t file_entry_count;
227 size_t i;
228
229 /* Look for index file in relative path index/name.idx. */
94cf822e 230 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2
JG
231 if (!basename) {
232 ret = -1;
233 goto end;
234 }
235
94cf822e 236 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2
JG
237 if (!directory) {
238 ret = -1;
239 goto end;
240 }
241
242 index_basename = g_string_new(basename);
243 if (!index_basename) {
244 ret = -1;
245 goto end;
246 }
247
248 g_string_append(index_basename, ".idx");
249 index_file_path = g_build_filename(directory, "index",
250 index_basename->str, NULL);
251 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3
JG
252 if (!mapped_file) {
253 ret = -1;
254 goto end;
255 }
b6c3dcb2
JG
256 filesize = g_mapped_file_get_length(mapped_file);
257 if (filesize < sizeof(*header)) {
258 printf_error("Invalid LTTng trace index: file size < header size");
259 ret = -1;
260 goto end;
261 }
262
263 mmap_begin = g_mapped_file_get_contents(mapped_file);
264 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
265
266 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
267 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
268 printf_error("Invalid LTTng trace index: \"magic\" validation failed");
269 ret = -1;
270 goto end;
271 }
b6c3dcb2
JG
272
273 file_index_entry_size = be32toh(header->packet_index_len);
274 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
275 if ((filesize - sizeof(*header)) % (file_entry_count * file_index_entry_size)) {
276 printf_error("Invalid index file size; not a multiple of index entry size");
277 ret = -1;
278 goto end;
279 }
280
94cf822e 281 ds_file->index.entries = g_array_sized_new(FALSE, TRUE,
b6c3dcb2 282 sizeof(struct index_entry), file_entry_count);
94cf822e 283 if (!ds_file->index.entries) {
b6c3dcb2
JG
284 ret = -1;
285 goto end;
286 }
94cf822e 287 index = (struct index_entry *) ds_file->index.entries->data;
b6c3dcb2
JG
288 for (i = 0; i < file_entry_count; i++) {
289 struct ctf_packet_index *file_index =
290 (struct ctf_packet_index *) file_pos;
291 uint64_t packet_size = be64toh(file_index->packet_size);
292
293 if (packet_size % CHAR_BIT) {
294 ret = -1;
295 printf_error("Invalid packet size encountered in index file");
296 goto invalid_index;
297 }
298
299 /* Convert size in bits to bytes. */
300 packet_size /= CHAR_BIT;
301 index->packet_size = packet_size;
302
303 index->offset = be64toh(file_index->offset);
304 if (i != 0 && index->offset < (index - 1)->offset) {
305 printf_error("Invalid, non-monotonic, packet offset encountered in index file");
306 ret = -1;
307 goto invalid_index;
308 }
309
310 index->timestamp_begin = be64toh(file_index->timestamp_begin);
311 index->timestamp_end = be64toh(file_index->timestamp_end);
312 if (index->timestamp_end < index->timestamp_begin) {
313 printf_error("Invalid packet time bounds encountered in index file");
314 ret = -1;
315 goto invalid_index;
316 }
317
318 total_packets_size += packet_size;
319 file_pos += file_index_entry_size;
320 index++;
321 }
322
323 /* Validate that the index addresses the complete stream. */
94cf822e 324 if (ds_file->file->size != total_packets_size) {
b6c3dcb2
JG
325 printf_error("Invalid index; indexed size != stream file size");
326 ret = -1;
327 goto invalid_index;
328 }
329end:
330 g_free(directory);
331 g_free(basename);
332 g_free(index_file_path);
06367fa3
JG
333 if (index_basename) {
334 g_string_free(index_basename, TRUE);
335 }
336 if (mapped_file) {
337 g_mapped_file_unref(mapped_file);
338 }
b6c3dcb2
JG
339 return ret;
340invalid_index:
94cf822e 341 g_array_free(ds_file->index.entries, TRUE);
b6c3dcb2
JG
342 goto end;
343}
344
345static
94cf822e 346int build_index_from_data_stream_file(struct ctf_fs_ds_file *stream)
b6c3dcb2 347{
4f1f88a6 348 return 0;
b6c3dcb2
JG
349}
350
351static
94cf822e 352int init_stream_index(struct ctf_fs_ds_file *ds_file)
b6c3dcb2
JG
353{
354 int ret;
355
94cf822e 356 ret = build_index_from_idx_file(ds_file);
b6c3dcb2
JG
357 if (!ret) {
358 goto end;
359 }
360
94cf822e 361 ret = build_index_from_data_stream_file(ds_file);
b6c3dcb2
JG
362end:
363 return ret;
364}
365
e7a4393b 366BT_HIDDEN
94cf822e
PP
367struct ctf_fs_ds_file *ctf_fs_ds_file_create(
368 struct ctf_fs_trace *ctf_fs_trace,
369 struct bt_ctf_stream *stream, const char *path,
370 bool build_index)
e98a2d6e 371{
b6c3dcb2 372 int ret;
94cf822e 373 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 374
94cf822e 375 if (!ds_file) {
e98a2d6e
PP
376 goto error;
377 }
378
94cf822e
PP
379 ds_file->file = ctf_fs_file_create(ctf_fs_trace->ctf_fs);
380 if (!ds_file->file) {
4f1f88a6
PP
381 goto error;
382 }
383
94cf822e
PP
384 ds_file->stream = bt_get(stream);
385 ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map);
386 g_string_assign(ds_file->file->path, path);
387 ret = ctf_fs_file_open(ctf_fs_trace->ctf_fs, ds_file->file, "rb");
4f1f88a6
PP
388 if (ret) {
389 goto error;
390 }
391
94cf822e 392 ds_file->notif_iter = bt_ctf_notif_iter_create(
1a9f7075 393 ctf_fs_trace->metadata->trace,
94cf822e 394 ctf_fs_trace->ctf_fs->page_size, medops, ds_file,
1a9f7075 395 ctf_fs_trace->ctf_fs->error_fp);
94cf822e 396 if (!ds_file->notif_iter) {
e98a2d6e
PP
397 goto error;
398 }
5b29e799 399
94cf822e
PP
400 ds_file->mmap_max_len = ctf_fs_trace->ctf_fs->page_size * 2048;
401
402 if (build_index) {
403 ret = init_stream_index(ds_file);
404 if (ret) {
405 goto error;
406 }
b6c3dcb2 407 }
1a9f7075 408
e98a2d6e 409 goto end;
1a9f7075 410
e98a2d6e 411error:
78586d8a 412 /* Do not touch "borrowed" file. */
94cf822e
PP
413 ctf_fs_ds_file_destroy(ds_file);
414 ds_file = NULL;
1a9f7075 415
e98a2d6e 416end:
94cf822e 417 return ds_file;
e98a2d6e
PP
418}
419
5b29e799 420BT_HIDDEN
94cf822e 421void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 422{
94cf822e 423 if (!ds_file) {
5b29e799 424 return;
043e2020
JG
425 }
426
94cf822e
PP
427 bt_put(ds_file->cc_prio_map);
428 bt_put(ds_file->stream);
429 (void) ds_file_munmap(ds_file);
0982a26d 430
94cf822e
PP
431 if (ds_file->file) {
432 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
433 }
434
94cf822e
PP
435 if (ds_file->notif_iter) {
436 bt_ctf_notif_iter_destroy(ds_file->notif_iter);
043e2020 437 }
5b29e799 438
94cf822e
PP
439 if (ds_file->index.entries) {
440 g_array_free(ds_file->index.entries, TRUE);
78586d8a 441 }
5b29e799 442
94cf822e 443 g_free(ds_file);
e98a2d6e 444}
4f1f88a6
PP
445
446BT_HIDDEN
94cf822e
PP
447struct bt_notification_iterator_next_return ctf_fs_ds_file_next(
448 struct ctf_fs_ds_file *ds_file)
4f1f88a6
PP
449{
450 enum bt_ctf_notif_iter_status notif_iter_status;
451 struct bt_notification_iterator_next_return ret = {
452 .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR,
453 .notification = NULL,
454 };
455
8915b7a7 456 notif_iter_status = bt_ctf_notif_iter_get_next_notification(
94cf822e 457 ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification);
4f1f88a6 458
4f1f88a6
PP
459 switch (notif_iter_status) {
460 case BT_CTF_NOTIF_ITER_STATUS_EOF:
461 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
462 break;
463 case BT_CTF_NOTIF_ITER_STATUS_OK:
464 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
465 break;
466 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
467 /*
468 * Should not make it this far as this is
469 * medium-specific; there is nothing for the user to do
470 * and it should have been handled upstream.
471 */
0fbb9a9f 472 abort();
4f1f88a6
PP
473 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
474 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
475 default:
476 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
477 break;
478 }
479
480 return ret;
481}
94cf822e
PP
482
483BT_HIDDEN
484int ctf_fs_ds_file_get_packet_header_context_fields(
485 struct ctf_fs_trace *ctf_fs_trace, const char *path,
486 struct bt_ctf_field **packet_header_field,
487 struct bt_ctf_field **packet_context_field)
488{
489 enum bt_ctf_notif_iter_status notif_iter_status;
490 struct ctf_fs_ds_file *ds_file;
491 int ret = 0;
492
493 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, path, false);
494 if (!ds_file) {
495 goto error;
496 }
497
498 notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields(
499 ds_file->notif_iter, packet_header_field, packet_context_field);
500 switch (notif_iter_status) {
501 case BT_CTF_NOTIF_ITER_STATUS_EOF:
502 case BT_CTF_NOTIF_ITER_STATUS_OK:
503 break;
504 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
0fbb9a9f 505 abort();
94cf822e
PP
506 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
507 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
508 default:
509 goto error;
510 break;
511 }
512
513 goto end;
514
515error:
516 ret = -1;
517
518 if (packet_header_field) {
519 bt_put(*packet_header_field);
520 }
521
522 if (packet_context_field) {
523 bt_put(*packet_context_field);
524 }
525
526end:
527 ctf_fs_ds_file_destroy(ds_file);
528 return ret;
529}
This page took 0.054807 seconds and 4 git commands to generate.