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