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