ea33974bde4045609ece5e481b483e538970cbe7
[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
47 #define PRINT_ERR_STREAM ctf_fs->error_fp
48 #define PRINT_PREFIX "ctf-fs"
49 #include "print.h"
50 #define METADATA_TEXT_SIG "/* CTF 1.8"
51
52 BT_HIDDEN
53 bool ctf_fs_debug;
54
55 struct bt_notification_iterator_next_return ctf_fs_iterator_next(
56 struct bt_private_notification_iterator *iterator)
57 {
58 struct ctf_fs_stream *fs_stream =
59 bt_private_notification_iterator_get_user_data(iterator);
60
61 return ctf_fs_stream_next(fs_stream);
62 }
63
64 void ctf_fs_iterator_finalize(struct bt_private_notification_iterator *it)
65 {
66 void *ctf_fs_stream =
67 bt_private_notification_iterator_get_user_data(it);
68
69 ctf_fs_stream_destroy(ctf_fs_stream);
70 }
71
72 enum bt_notification_iterator_status ctf_fs_iterator_init(
73 struct bt_private_notification_iterator *it,
74 struct bt_private_port *port)
75 {
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;
83
84 assert(priv_comp);
85
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;
90 }
91
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 }
97
98 stream = ctf_fs_stream_create(ctf_fs, port_data->path->str);
99 if (!stream) {
100 goto error;
101 }
102
103 ret = bt_private_notification_iterator_set_user_data(it, stream);
104 if (ret) {
105 goto error;
106 }
107
108 stream = NULL;
109 goto end;
110
111 error:
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;
116 }
117
118 end:
119 ctf_fs_stream_destroy(stream);
120 bt_put(priv_comp);
121 return ret;
122 }
123
124 static
125 void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs)
126 {
127 if (!ctf_fs) {
128 return;
129 }
130
131 if (ctf_fs->trace_path) {
132 g_string_free(ctf_fs->trace_path, TRUE);
133 }
134
135 if (ctf_fs->port_data) {
136 g_ptr_array_free(ctf_fs->port_data, TRUE);
137 }
138
139 if (ctf_fs->metadata) {
140 ctf_fs_metadata_fini(ctf_fs->metadata);
141 g_free(ctf_fs->metadata);
142 }
143
144 bt_put(ctf_fs->cc_prio_map);
145 g_free(ctf_fs);
146 }
147
148 void ctf_fs_finalize(struct bt_private_component *component)
149 {
150 void *data = bt_private_component_get_user_data(component);
151
152 ctf_fs_destroy_data(data);
153 }
154
155 static
156 void 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);
168 }
169
170 static
171 int create_one_port(struct ctf_fs_component *ctf_fs,
172 const char *stream_basename, const char *stream_path)
173 {
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
216 error:
217 ret = -1;
218
219 end:
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;
227 }
228
229 static
230 int create_ports(struct ctf_fs_component *ctf_fs)
231 {
232 int ret = 0;
233 const char *basename;
234 GError *error = NULL;
235 GDir *dir = NULL;
236 struct bt_private_port *def_port;
237 struct ctf_fs_file *file = NULL;
238
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);
249 if (!dir) {
250 PERR("Cannot open directory `%s`: %s (code %d)\n",
251 ctf_fs->trace_path->str, error->message,
252 error->code);
253 goto error;
254 }
255
256 while ((basename = g_dir_read_name(dir))) {
257 if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) {
258 /* Ignore the metadata stream. */
259 PDBG("Ignoring metadata file `%s`\n", basename);
260 continue;
261 }
262
263 if (basename[0] == '.') {
264 PDBG("Ignoring hidden file `%s`\n", basename);
265 continue;
266 }
267
268 /* Create the file. */
269 file = ctf_fs_file_create(ctf_fs);
270 if (!file) {
271 PERR("Cannot create stream file object for file `%s`\n",
272 basename);
273 goto error;
274 }
275
276 /* Create full path string. */
277 g_string_append_printf(file->path, "%s/%s",
278 ctf_fs->trace_path->str, basename);
279 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
280 PDBG("Ignoring non-regular file `%s`\n", basename);
281 ctf_fs_file_destroy(file);
282 file = NULL;
283 continue;
284 }
285
286 ret = ctf_fs_file_open(ctf_fs, file, "rb");
287 if (ret) {
288 PERR("Cannot open stream file `%s`\n", basename);
289 goto error;
290 }
291
292 if (file->size == 0) {
293 /* Skip empty stream. */
294 PDBG("Ignoring empty file `%s`\n", basename);
295 ctf_fs_file_destroy(file);
296 file = NULL;
297 continue;
298 }
299
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);
304 goto error;
305 }
306
307 ctf_fs_file_destroy(file);
308 file = NULL;
309 }
310
311 goto end;
312
313 error:
314 ret = -1;
315
316 end:
317 if (dir) {
318 g_dir_close(dir);
319 dir = NULL;
320 }
321
322 if (error) {
323 g_error_free(error);
324 }
325
326 ctf_fs_file_destroy(file);
327 return ret;
328 }
329
330 static
331 int 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
362 end:
363 return ret;
364 }
365
366 static
367 struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
368 struct bt_value *params)
369 {
370 struct ctf_fs_component *ctf_fs;
371 struct bt_value *value = NULL;
372 const char *path;
373 int ret;
374
375 ctf_fs = g_new0(struct ctf_fs_component, 1);
376 if (!ctf_fs) {
377 goto end;
378 }
379
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
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);
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) {
400 goto error;
401 }
402
403 ctf_fs->trace_path = g_string_new(path);
404 if (!ctf_fs->trace_path) {
405 goto error;
406 }
407 ctf_fs->error_fp = stderr;
408 ctf_fs->page_size = (size_t) getpagesize();
409
410 // FIXME: check error.
411 ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1);
412 if (!ctf_fs->metadata) {
413 goto error;
414 }
415
416 ret = ctf_fs_metadata_set_trace(ctf_fs);
417 if (ret) {
418 goto error;
419 }
420
421 ret = create_cc_prio_map(ctf_fs);
422 if (ret) {
423 goto error;
424 }
425
426 ret = create_ports(ctf_fs);
427 if (ret) {
428 goto error;
429 }
430
431 goto end;
432
433 error:
434 ctf_fs_destroy_data(ctf_fs);
435 ctf_fs = NULL;
436 end:
437 BT_PUT(value);
438 return ctf_fs;
439 }
440
441 BT_HIDDEN
442 enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp,
443 struct bt_value *params, UNUSED_VAR void *init_method_data)
444 {
445 struct ctf_fs_component *ctf_fs;
446 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
447
448 assert(priv_comp);
449 ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0;
450 ctf_fs = ctf_fs_create(priv_comp, params);
451 if (!ctf_fs) {
452 ret = BT_COMPONENT_STATUS_NOMEM;
453 goto end;
454 }
455
456 ret = bt_private_component_set_user_data(priv_comp, ctf_fs);
457 if (ret != BT_COMPONENT_STATUS_OK) {
458 goto error;
459 }
460 end:
461 return ret;
462 error:
463 (void) bt_private_component_set_user_data(priv_comp, NULL);
464 ctf_fs_destroy_data(ctf_fs);
465 return ret;
466 }
467
468 BT_HIDDEN
469 struct bt_value *ctf_fs_query(struct bt_component_class *comp_class,
470 const char *object, struct bt_value *params)
471 {
472 struct bt_value *results = NULL;
473 struct bt_value *path_value = NULL;
474 char *metadata_text = NULL;
475 FILE *metadata_fp = NULL;
476 GString *g_metadata_text = NULL;
477
478 if (strcmp(object, "metadata-info") == 0) {
479 int ret;
480 int bo;
481 const char *path;
482 bool is_packetized;
483
484 results = bt_value_map_create();
485 if (!results) {
486 goto error;
487 }
488
489 if (!bt_value_is_map(params)) {
490 fprintf(stderr,
491 "Query parameters is not a map value object\n");
492 goto error;
493 }
494
495 path_value = bt_value_map_get(params, "path");
496 ret = bt_value_string_get(path_value, &path);
497 if (ret) {
498 fprintf(stderr,
499 "Cannot get `path` string parameter\n");
500 goto error;
501 }
502
503 assert(path);
504 metadata_fp = ctf_fs_metadata_open_file(path);
505 if (!metadata_fp) {
506 fprintf(stderr,
507 "Cannot open trace at path `%s`\n", path);
508 goto error;
509 }
510
511 is_packetized = ctf_metadata_is_packetized(metadata_fp, &bo);
512
513 if (is_packetized) {
514 ret = ctf_metadata_packetized_file_to_buf(NULL,
515 metadata_fp, (uint8_t **) &metadata_text, bo);
516 if (ret) {
517 fprintf(stderr,
518 "Cannot decode packetized metadata file\n");
519 goto error;
520 }
521 } else {
522 long filesize;
523
524 fseek(metadata_fp, 0, SEEK_END);
525 filesize = ftell(metadata_fp);
526 rewind(metadata_fp);
527 metadata_text = malloc(filesize + 1);
528 if (!metadata_text) {
529 fprintf(stderr,
530 "Cannot allocate buffer for metadata text\n");
531 goto error;
532 }
533
534 if (fread(metadata_text, filesize, 1, metadata_fp) !=
535 1) {
536 fprintf(stderr,
537 "Cannot read metadata file\n");
538 goto error;
539 }
540
541 metadata_text[filesize] = '\0';
542 }
543
544 g_metadata_text = g_string_new(NULL);
545 if (!g_metadata_text) {
546 goto error;
547 }
548
549 if (strncmp(metadata_text, METADATA_TEXT_SIG,
550 sizeof(METADATA_TEXT_SIG) - 1) != 0) {
551 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
552 g_string_append(g_metadata_text, " */\n\n");
553 }
554
555 g_string_append(g_metadata_text, metadata_text);
556
557 ret = bt_value_map_insert_string(results, "text",
558 g_metadata_text->str);
559 if (ret) {
560 fprintf(stderr, "Cannot insert metadata text into results\n");
561 goto error;
562 }
563
564 ret = bt_value_map_insert_bool(results, "is-packetized",
565 is_packetized);
566 if (ret) {
567 fprintf(stderr, "Cannot insert is packetized into results\n");
568 goto error;
569 }
570 } else {
571 fprintf(stderr, "Unknown query object `%s`\n", object);
572 goto error;
573 }
574
575 goto end;
576
577 error:
578 BT_PUT(results);
579
580 end:
581 bt_put(path_value);
582 free(metadata_text);
583
584 if (g_metadata_text) {
585 g_string_free(g_metadata_text, TRUE);
586 }
587
588 if (metadata_fp) {
589 fclose(metadata_fp);
590 }
591 return results;
592 }
This page took 0.046898 seconds and 3 git commands to generate.