Support for --clock-offset and --clock-offset-ns
[babeltrace.git] / plugins / ctf / fs / fs.c
CommitLineData
7a278c8e 1/*
ea0b4b9e 2 * fs.c
7a278c8e 3 *
ea0b4b9e 4 * Babeltrace CTF file system Reader Component
7a278c8e 5 *
f3bc2010 6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7a278c8e
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
5b29e799 29#include <babeltrace/ctf-ir/packet.h>
ac0c6bdd 30#include <babeltrace/ctf-ir/clock-class.h>
4f1f88a6 31#include <babeltrace/graph/private-port.h>
b2e0c907 32#include <babeltrace/graph/private-component.h>
4f1f88a6
PP
33#include <babeltrace/graph/private-component-source.h>
34#include <babeltrace/graph/private-notification-iterator.h>
b2e0c907
PP
35#include <babeltrace/graph/component.h>
36#include <babeltrace/graph/notification-iterator.h>
599faa1c 37#include <babeltrace/graph/clock-class-priority-map.h>
7d61fa8e 38#include <plugins-common.h>
ea0b4b9e
JG
39#include <glib.h>
40#include <assert.h>
56a1cced
JG
41#include <unistd.h>
42#include "fs.h"
413bc2c4
JG
43#include "metadata.h"
44#include "data-stream.h"
e7a4393b
JG
45#include "file.h"
46
47#define PRINT_ERR_STREAM ctf_fs->error_fp
48#define PRINT_PREFIX "ctf-fs"
49#include "print.h"
33f93973 50#define METADATA_TEXT_SIG "/* CTF 1.8"
ea0b4b9e 51
78bb6992
MD
52BT_HIDDEN
53bool ctf_fs_debug;
ea0b4b9e 54
41a2b7ae 55struct bt_notification_iterator_next_return ctf_fs_iterator_next(
4f1f88a6 56 struct bt_private_notification_iterator *iterator)
ea0b4b9e 57{
4f1f88a6
PP
58 struct ctf_fs_stream *fs_stream =
59 bt_private_notification_iterator_get_user_data(iterator);
d01e0f33 60
4f1f88a6 61 return ctf_fs_stream_next(fs_stream);
ea0b4b9e 62}
bfd20a42 63
4f1f88a6 64void ctf_fs_iterator_finalize(struct bt_private_notification_iterator *it)
760051fa 65{
4f1f88a6
PP
66 void *ctf_fs_stream =
67 bt_private_notification_iterator_get_user_data(it);
760051fa 68
4f1f88a6 69 ctf_fs_stream_destroy(ctf_fs_stream);
760051fa
JG
70}
71
4f1f88a6
PP
72enum bt_notification_iterator_status ctf_fs_iterator_init(
73 struct bt_private_notification_iterator *it,
74 struct bt_private_port *port)
4c1456f0 75{
4f1f88a6
PP
76 struct ctf_fs_stream *stream = NULL;
77 struct ctf_fs_component *ctf_fs;
78 struct ctf_fs_port_data *port_data;
79 struct bt_private_component *priv_comp =
80 bt_private_notification_iterator_get_private_component(it);
81 enum bt_notification_iterator_status ret =
82 BT_NOTIFICATION_ITERATOR_STATUS_OK;
760051fa 83
4f1f88a6 84 assert(priv_comp);
5b29e799 85
4f1f88a6
PP
86 ctf_fs = bt_private_component_get_user_data(priv_comp);
87 if (!ctf_fs) {
88 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
89 goto error;
760051fa
JG
90 }
91
4f1f88a6
PP
92 port_data = bt_private_port_get_user_data(port);
93 if (!port_data) {
94 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
95 goto error;
96 }
5b29e799 97
4f1f88a6 98 stream = ctf_fs_stream_create(ctf_fs, port_data->path->str);
5b29e799 99 if (!stream) {
4f1f88a6 100 goto error;
760051fa
JG
101 }
102
4f1f88a6
PP
103 ret = bt_private_notification_iterator_set_user_data(it, stream);
104 if (ret) {
105 goto error;
760051fa
JG
106 }
107
4f1f88a6
PP
108 stream = NULL;
109 goto end;
5b29e799 110
4f1f88a6
PP
111error:
112 (void) bt_private_notification_iterator_set_user_data(it, NULL);
113
114 if (ret == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
115 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
760051fa 116 }
5b29e799 117
760051fa 118end:
4f1f88a6
PP
119 ctf_fs_stream_destroy(stream);
120 bt_put(priv_comp);
760051fa
JG
121 return ret;
122}
123
760051fa 124static
4f1f88a6 125void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs)
760051fa 126{
4f1f88a6 127 if (!ctf_fs) {
8fa760ba
JG
128 return;
129 }
4f1f88a6
PP
130
131 if (ctf_fs->trace_path) {
132 g_string_free(ctf_fs->trace_path, TRUE);
56a1cced 133 }
4f1f88a6
PP
134
135 if (ctf_fs->port_data) {
136 g_ptr_array_free(ctf_fs->port_data, TRUE);
c14d7e26 137 }
760051fa 138
4f1f88a6
PP
139 if (ctf_fs->metadata) {
140 ctf_fs_metadata_fini(ctf_fs->metadata);
141 g_free(ctf_fs->metadata);
142 }
760051fa 143
599faa1c 144 bt_put(ctf_fs->cc_prio_map);
4f1f88a6 145 g_free(ctf_fs);
4c1456f0
JG
146}
147
4f1f88a6 148void ctf_fs_finalize(struct bt_private_component *component)
a4792757 149{
4f1f88a6
PP
150 void *data = bt_private_component_get_user_data(component);
151
152 ctf_fs_destroy_data(data);
a4792757
JG
153}
154
e7a4393b 155static
4f1f88a6
PP
156void port_data_destroy(void *data) {
157 struct ctf_fs_port_data *port_data = data;
158
159 if (!port_data) {
160 return;
161 }
162
163 if (port_data->path) {
164 g_string_free(port_data->path, TRUE);
165 }
166
167 g_free(port_data);
5b29e799
JG
168}
169
170static
4f1f88a6
PP
171int create_one_port(struct ctf_fs_component *ctf_fs,
172 const char *stream_basename, const char *stream_path)
5b29e799 173{
4f1f88a6
PP
174 int ret = 0;
175 struct bt_private_port *port = NULL;
176 struct ctf_fs_port_data *port_data = NULL;
177 GString *port_name = NULL;
178
179 port_name = g_string_new(NULL);
180 if (!port_name) {
181 goto error;
182 }
183
184 /* Assign the name for the new output port */
185 g_string_assign(port_name, "");
186 g_string_printf(port_name, "trace0-stream-%s", stream_basename);
187 PDBG("Creating one port named `%s` associated with path `%s`\n",
188 port_name->str, stream_path);
189
190 /* Create output port for this file */
191 port = bt_private_component_source_add_output_private_port(
192 ctf_fs->priv_comp, port_name->str);
193 if (!port) {
194 goto error;
195 }
196
197 port_data = g_new0(struct ctf_fs_port_data, 1);
198 if (!port_data) {
199 goto error;
200 }
201
202 port_data->path = g_string_new(stream_path);
203 if (!port_data->path) {
204 goto error;
205 }
206
207 ret = bt_private_port_set_user_data(port, port_data);
208 if (ret) {
209 goto error;
210 }
211
212 g_ptr_array_add(ctf_fs->port_data, port_data);
213 port_data = NULL;
214 goto end;
215
216error:
217 ret = -1;
218
219end:
220 if (port_name) {
221 g_string_free(port_name, TRUE);
222 }
223
224 bt_put(port);
225 port_data_destroy(port_data);
226 return ret;
5b29e799
JG
227}
228
229static
4f1f88a6 230int create_ports(struct ctf_fs_component *ctf_fs)
e7a4393b
JG
231{
232 int ret = 0;
4f1f88a6 233 const char *basename;
e7a4393b 234 GError *error = NULL;
4f1f88a6
PP
235 GDir *dir = NULL;
236 struct bt_private_port *def_port;
237 struct ctf_fs_file *file = NULL;
e7a4393b 238
4f1f88a6
PP
239 /* Remove default port if needed */
240 def_port = bt_private_component_source_get_default_output_private_port(
241 ctf_fs->priv_comp);
242 if (def_port) {
243 bt_private_port_remove_from_component(def_port);
244 bt_put(def_port);
245 }
246
247 /* Create one output port for each stream file */
248 dir = g_dir_open(ctf_fs->trace_path->str, 0, &error);
e7a4393b 249 if (!dir) {
4f1f88a6
PP
250 PERR("Cannot open directory `%s`: %s (code %d)\n",
251 ctf_fs->trace_path->str, error->message,
252 error->code);
e7a4393b
JG
253 goto error;
254 }
255
4f1f88a6
PP
256 while ((basename = g_dir_read_name(dir))) {
257 if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) {
e7a4393b 258 /* Ignore the metadata stream. */
4f1f88a6 259 PDBG("Ignoring metadata file `%s`\n", basename);
e7a4393b
JG
260 continue;
261 }
262
4f1f88a6
PP
263 if (basename[0] == '.') {
264 PDBG("Ignoring hidden file `%s`\n", basename);
e7a4393b
JG
265 continue;
266 }
267
268 /* Create the file. */
269 file = ctf_fs_file_create(ctf_fs);
270 if (!file) {
4f1f88a6
PP
271 PERR("Cannot create stream file object for file `%s`\n",
272 basename);
e7a4393b
JG
273 goto error;
274 }
275
276 /* Create full path string. */
277 g_string_append_printf(file->path, "%s/%s",
4f1f88a6 278 ctf_fs->trace_path->str, basename);
e7a4393b 279 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
4f1f88a6 280 PDBG("Ignoring non-regular file `%s`\n", basename);
e7a4393b 281 ctf_fs_file_destroy(file);
4f1f88a6 282 file = NULL;
e7a4393b
JG
283 continue;
284 }
285
4f1f88a6
PP
286 ret = ctf_fs_file_open(ctf_fs, file, "rb");
287 if (ret) {
288 PERR("Cannot open stream file `%s`\n", basename);
e7a4393b
JG
289 goto error;
290 }
291
9fa0891a
JG
292 if (file->size == 0) {
293 /* Skip empty stream. */
4f1f88a6 294 PDBG("Ignoring empty file `%s`\n", basename);
9fa0891a 295 ctf_fs_file_destroy(file);
4f1f88a6 296 file = NULL;
9fa0891a
JG
297 continue;
298 }
299
4f1f88a6
PP
300 ret = create_one_port(ctf_fs, basename, file->path->str);
301 if (ret) {
302 PERR("Cannot create output port for file `%s`\n",
303 basename);
e7a4393b
JG
304 goto error;
305 }
306
4f1f88a6
PP
307 ctf_fs_file_destroy(file);
308 file = NULL;
e7a4393b
JG
309 }
310
311 goto end;
4f1f88a6 312
e7a4393b
JG
313error:
314 ret = -1;
4f1f88a6 315
e7a4393b
JG
316end:
317 if (dir) {
318 g_dir_close(dir);
319 dir = NULL;
320 }
4f1f88a6 321
e7a4393b
JG
322 if (error) {
323 g_error_free(error);
324 }
5b29e799 325
4f1f88a6 326 ctf_fs_file_destroy(file);
91457551 327 return ret;
5b29e799
JG
328}
329
599faa1c
PP
330static
331int create_cc_prio_map(struct ctf_fs_component *ctf_fs)
332{
333 int ret = 0;
334 size_t i;
335 int count;
336
337 assert(ctf_fs);
338 ctf_fs->cc_prio_map = bt_clock_class_priority_map_create();
339 if (!ctf_fs->cc_prio_map) {
340 ret = -1;
341 goto end;
342 }
343
344 count = bt_ctf_trace_get_clock_class_count(ctf_fs->metadata->trace);
345 assert(count >= 0);
346
347 for (i = 0; i < count; i++) {
348 struct bt_ctf_clock_class *clock_class =
349 bt_ctf_trace_get_clock_class(ctf_fs->metadata->trace,
350 i);
351
352 assert(clock_class);
353 ret = bt_clock_class_priority_map_add_clock_class(
354 ctf_fs->cc_prio_map, clock_class, 0);
355 BT_PUT(clock_class);
356
357 if (ret) {
358 goto end;
359 }
360 }
361
362end:
363 return ret;
364}
365
5b29e799 366static
4f1f88a6
PP
367struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
368 struct bt_value *params)
56a1cced
JG
369{
370 struct ctf_fs_component *ctf_fs;
1ef09eb5 371 struct bt_value *value = NULL;
56a1cced 372 const char *path;
4f1f88a6 373 int ret;
56a1cced
JG
374
375 ctf_fs = g_new0(struct ctf_fs_component, 1);
376 if (!ctf_fs) {
377 goto end;
378 }
379
4f1f88a6
PP
380 /*
381 * We don't need to get a new reference here because as long as
382 * our private ctf_fs_component object exists, the containing
383 * private component should also exist.
384 */
385 ctf_fs->priv_comp = priv_comp;
386
56a1cced
JG
387 /* FIXME: should probably look for a source URI */
388 value = bt_value_map_get(params, "path");
389 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
390 goto error;
391 }
392
393 ret = bt_value_string_get(value, &path);
4f1f88a6
PP
394 if (ret) {
395 goto error;
396 }
397
398 ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy);
399 if (!ctf_fs->port_data) {
56a1cced
JG
400 goto error;
401 }
402
403 ctf_fs->trace_path = g_string_new(path);
404 if (!ctf_fs->trace_path) {
92540773 405 BT_PUT(value);
56a1cced
JG
406 goto error;
407 }
92540773
JD
408 bt_put(value);
409
410 value = bt_value_map_get(params, "offset-s");
411 if (value) {
412 int64_t offset;
413
414 if (!bt_value_is_integer(value)) {
415 fprintf(stderr,
416 "offset-s should be an integer\n");
417 goto error;
418 }
419 ret = bt_value_integer_get(value, &offset);
420 if (ret != BT_VALUE_STATUS_OK) {
421 fprintf(stderr,
422 "Failed to get offset-s value\n");
423 goto error;
424 }
425 ctf_fs->options.clock_offset = offset;
426 bt_put(value);
427 }
428
429 value = bt_value_map_get(params, "offset-ns");
430 if (value) {
431 int64_t offset;
432
433 if (!bt_value_is_integer(value)) {
434 fprintf(stderr,
435 "offset-ns should be an integer\n");
436 goto error;
437 }
438 ret = bt_value_integer_get(value, &offset);
439 if (ret != BT_VALUE_STATUS_OK) {
440 fprintf(stderr,
441 "Failed to get offset-ns value\n");
442 goto error;
443 }
444 ctf_fs->options.clock_offset_ns = offset;
445 bt_put(value);
446 }
447
56a1cced
JG
448 ctf_fs->error_fp = stderr;
449 ctf_fs->page_size = (size_t) getpagesize();
e7a4393b
JG
450
451 // FIXME: check error.
5b29e799
JG
452 ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1);
453 if (!ctf_fs->metadata) {
e7a4393b
JG
454 goto error;
455 }
4f1f88a6
PP
456
457 ret = ctf_fs_metadata_set_trace(ctf_fs);
458 if (ret) {
459 goto error;
460 }
461
599faa1c
PP
462 ret = create_cc_prio_map(ctf_fs);
463 if (ret) {
464 goto error;
465 }
466
4f1f88a6
PP
467 ret = create_ports(ctf_fs);
468 if (ret) {
469 goto error;
470 }
471
1ef09eb5
JG
472 goto end;
473
56a1cced
JG
474error:
475 ctf_fs_destroy_data(ctf_fs);
e7a4393b 476 ctf_fs = NULL;
1ef09eb5 477end:
56a1cced
JG
478 return ctf_fs;
479}
480
ea0b4b9e 481BT_HIDDEN
4f1f88a6 482enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp,
7d61fa8e 483 struct bt_value *params, UNUSED_VAR void *init_method_data)
ea0b4b9e
JG
484{
485 struct ctf_fs_component *ctf_fs;
486 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
487
4f1f88a6 488 assert(priv_comp);
ea0b4b9e 489 ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
4f1f88a6 490 ctf_fs = ctf_fs_create(priv_comp, params);
ea0b4b9e
JG
491 if (!ctf_fs) {
492 ret = BT_COMPONENT_STATUS_NOMEM;
493 goto end;
494 }
4c1456f0 495
4f1f88a6 496 ret = bt_private_component_set_user_data(priv_comp, ctf_fs);
ea0b4b9e
JG
497 if (ret != BT_COMPONENT_STATUS_OK) {
498 goto error;
499 }
ea0b4b9e
JG
500end:
501 return ret;
502error:
4f1f88a6 503 (void) bt_private_component_set_user_data(priv_comp, NULL);
760051fa 504 ctf_fs_destroy_data(ctf_fs);
ea0b4b9e
JG
505 return ret;
506}
33f93973
PP
507
508BT_HIDDEN
a67681c1
PP
509struct bt_value *ctf_fs_query(struct bt_component_class *comp_class,
510 const char *object, struct bt_value *params)
33f93973
PP
511{
512 struct bt_value *results = NULL;
513 struct bt_value *path_value = NULL;
514 char *metadata_text = NULL;
515 FILE *metadata_fp = NULL;
516 GString *g_metadata_text = NULL;
517
a67681c1 518 if (strcmp(object, "metadata-info") == 0) {
33f93973
PP
519 int ret;
520 int bo;
521 const char *path;
522 bool is_packetized;
523
524 results = bt_value_map_create();
525 if (!results) {
526 goto error;
527 }
528
529 if (!bt_value_is_map(params)) {
530 fprintf(stderr,
a67681c1 531 "Query parameters is not a map value object\n");
33f93973
PP
532 goto error;
533 }
534
535 path_value = bt_value_map_get(params, "path");
536 ret = bt_value_string_get(path_value, &path);
537 if (ret) {
538 fprintf(stderr,
539 "Cannot get `path` string parameter\n");
540 goto error;
541 }
542
543 assert(path);
544 metadata_fp = ctf_fs_metadata_open_file(path);
545 if (!metadata_fp) {
546 fprintf(stderr,
547 "Cannot open trace at path `%s`\n", path);
548 goto error;
549 }
550
551 is_packetized = ctf_metadata_is_packetized(metadata_fp, &bo);
552
553 if (is_packetized) {
554 ret = ctf_metadata_packetized_file_to_buf(NULL,
555 metadata_fp, (uint8_t **) &metadata_text, bo);
556 if (ret) {
557 fprintf(stderr,
558 "Cannot decode packetized metadata file\n");
559 goto error;
560 }
561 } else {
562 long filesize;
563
564 fseek(metadata_fp, 0, SEEK_END);
565 filesize = ftell(metadata_fp);
566 rewind(metadata_fp);
567 metadata_text = malloc(filesize + 1);
568 if (!metadata_text) {
569 fprintf(stderr,
570 "Cannot allocate buffer for metadata text\n");
571 goto error;
572 }
573
574 if (fread(metadata_text, filesize, 1, metadata_fp) !=
575 1) {
576 fprintf(stderr,
577 "Cannot read metadata file\n");
578 goto error;
579 }
580
581 metadata_text[filesize] = '\0';
582 }
583
584 g_metadata_text = g_string_new(NULL);
585 if (!g_metadata_text) {
586 goto error;
587 }
588
589 if (strncmp(metadata_text, METADATA_TEXT_SIG,
590 sizeof(METADATA_TEXT_SIG) - 1) != 0) {
591 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
592 g_string_append(g_metadata_text, " */\n\n");
593 }
594
595 g_string_append(g_metadata_text, metadata_text);
596
597 ret = bt_value_map_insert_string(results, "text",
598 g_metadata_text->str);
599 if (ret) {
600 fprintf(stderr, "Cannot insert metadata text into results\n");
601 goto error;
602 }
603
604 ret = bt_value_map_insert_bool(results, "is-packetized",
605 is_packetized);
606 if (ret) {
607 fprintf(stderr, "Cannot insert is packetized into results\n");
608 goto error;
609 }
610 } else {
a67681c1 611 fprintf(stderr, "Unknown query object `%s`\n", object);
33f93973
PP
612 goto error;
613 }
614
615 goto end;
616
617error:
618 BT_PUT(results);
619
620end:
621 bt_put(path_value);
622 free(metadata_text);
623
624 if (g_metadata_text) {
625 g_string_free(g_metadata_text, TRUE);
626 }
627
628 if (metadata_fp) {
629 fclose(metadata_fp);
630 }
631 return results;
632}
This page took 0.057817 seconds and 4 git commands to generate.