0246475ca94c845dd7aa18ef14e384dd7c15003b
[babeltrace.git] / converter / babeltrace.c
1 /*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@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/babeltrace.h>
30 #include <babeltrace/plugin/component-factory.h>
31 #include <babeltrace/plugin/plugin.h>
32 #include <babeltrace/plugin/component-class.h>
33 #include <babeltrace/plugin/notification/iterator.h>
34 #include <babeltrace/ref.h>
35 #include <babeltrace/values.h>
36 #include <stdlib.h>
37 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
38 #include <popt.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include "babeltrace-cfg.h"
42
43 static struct bt_component_factory *component_factory;
44
45 static
46 const char *component_type_str(enum bt_component_type type)
47 {
48 switch (type) {
49 case BT_COMPONENT_TYPE_SOURCE:
50 return "source";
51 case BT_COMPONENT_TYPE_SINK:
52 return "sink";
53 case BT_COMPONENT_TYPE_FILTER:
54 return "filter";
55 case BT_COMPONENT_TYPE_UNKNOWN:
56 default:
57 return "unknown";
58 }
59 }
60
61 static
62 void print_component_classes_found(struct bt_component_factory *factory)
63 {
64 int count, i;
65
66 if (!babeltrace_verbose) {
67 return;
68 }
69
70 count = bt_component_factory_get_component_class_count(factory);
71 if (count <= 0) {
72 fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.\n");
73 return;
74 }
75
76 printf_verbose("Found %d component classes.\n", count);
77 for (i = 0; i < count; i++) {
78 struct bt_component_class *component_class =
79 bt_component_factory_get_component_class_index(
80 factory, i);
81 struct bt_plugin *plugin = bt_component_class_get_plugin(
82 component_class);
83 const char *plugin_name = bt_plugin_get_name(plugin);
84 const char *component_name = bt_component_class_get_name(
85 component_class);
86 const char *path = bt_plugin_get_path(plugin);
87 const char *author = bt_plugin_get_author(plugin);
88 const char *license = bt_plugin_get_license(plugin);
89 const char *plugin_description = bt_plugin_get_description(
90 plugin);
91 const char *component_description =
92 bt_component_class_get_description(
93 component_class);
94 enum bt_component_type type = bt_component_class_get_type(
95 component_class);
96
97 printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
98 component_type_str(type));
99 printf_verbose("\tpath: %s\n", path ? path : "None");
100 printf_verbose("\tauthor: %s\n", author ? author : "Unknown");
101 printf_verbose("\tlicense: %s\n", license ? license : "Unknown");
102 printf_verbose("\tplugin description: %s\n",
103 plugin_description ? plugin_description : "None");
104 printf_verbose("\tcomponent description: %s\n",
105 component_description ? component_description : "None");
106
107 bt_put(plugin);
108 bt_put(component_class);
109 }
110 }
111
112 static
113 void print_indent(size_t indent)
114 {
115 size_t i;
116
117 for (i = 0; i < indent; i++) {
118 printf(" ");
119 }
120 }
121
122 static
123 void print_value(struct bt_value *, size_t, bool);
124
125 static
126 bool print_map_value(const char *key, struct bt_value *object, void *data)
127 {
128 size_t indent = (size_t) data;
129
130 print_indent(indent);
131 printf("\"%s\": ", key);
132 print_value(object, indent, false);
133
134 return true;
135 }
136
137 static
138 void print_value(struct bt_value *value, size_t indent, bool do_indent)
139 {
140 bool bool_val;
141 int64_t int_val;
142 double dbl_val;
143 const char *str_val;
144 int size;
145 int i;
146
147 if (!value) {
148 return;
149 }
150
151 if (do_indent) {
152 print_indent(indent);
153 }
154
155 switch (bt_value_get_type(value)) {
156 case BT_VALUE_TYPE_NULL:
157 printf("null\n");
158 break;
159 case BT_VALUE_TYPE_BOOL:
160 bt_value_bool_get(value, &bool_val);
161 printf("%s\n", bool_val ? "true" : "false");
162 break;
163 case BT_VALUE_TYPE_INTEGER:
164 bt_value_integer_get(value, &int_val);
165 printf("%" PRId64 "\n", int_val);
166 break;
167 case BT_VALUE_TYPE_FLOAT:
168 bt_value_float_get(value, &dbl_val);
169 printf("%lf\n", dbl_val);
170 break;
171 case BT_VALUE_TYPE_STRING:
172 bt_value_string_get(value, &str_val);
173 printf("\"%s\"\n", str_val);
174 break;
175 case BT_VALUE_TYPE_ARRAY:
176 size = bt_value_array_size(value);
177 printf("[\n");
178
179 for (i = 0; i < size; i++) {
180 struct bt_value *element =
181 bt_value_array_get(value, i);
182
183 print_value(element, indent + 2, true);
184 BT_PUT(element);
185 }
186
187 print_indent(indent);
188 printf("]\n");
189 break;
190 case BT_VALUE_TYPE_MAP:
191 if (bt_value_map_is_empty(value)) {
192 printf("{}\n");
193 return;
194 }
195
196 printf("{\n");
197 bt_value_map_foreach(value, print_map_value,
198 (void *) (indent + 2));
199 print_indent(indent);
200 printf("}\n");
201 break;
202 default:
203 assert(false);
204 }
205 }
206
207 static
208 void print_bt_config_component(struct bt_config_component *bt_config_component)
209 {
210 printf(" %s.%s\n", bt_config_component->plugin_name->str,
211 bt_config_component->component_name->str);
212 printf(" begin timestamp: ");
213
214 if (!bt_config_component->begin.set) {
215 printf("not set\n");
216 } else {
217 printf("%" PRIu64 " ns\n", bt_config_component->begin.value_ns);
218 }
219
220 printf(" end timestamp: ");
221
222 if (!bt_config_component->end.set) {
223 printf("not set\n");
224 } else {
225 printf("%" PRIu64 " ns\n", bt_config_component->end.value_ns);
226 }
227
228 printf(" params:\n");
229 print_value(bt_config_component->params, 6, true);
230 }
231
232 static
233 void print_bt_config_components(GPtrArray *array)
234 {
235 size_t i;
236
237 for (i = 0; i < array->len; i++) {
238 struct bt_config_component *cfg_component =
239 bt_config_get_component(array, i);
240 print_bt_config_component(cfg_component);
241 BT_PUT(cfg_component);
242 }
243 }
244
245 static
246 void print_cfg(struct bt_config *cfg)
247 {
248 printf("debug: %d\n", cfg->debug);
249 printf("verbose: %d\n", cfg->verbose);
250 printf("do list: %d\n", cfg->do_list);
251 printf("force correlate: %d\n", cfg->force_correlate);
252 printf("plugin paths:\n");
253 print_value(cfg->plugin_paths, 2, true);
254 printf("sources:\n");
255 print_bt_config_components(cfg->sources);
256 printf("sinks:\n");
257 print_bt_config_components(cfg->sinks);
258 }
259
260 static
261 struct bt_component *create_trimmer(struct bt_config_component *source_cfg)
262 {
263 struct bt_component *trimmer = NULL;
264 struct bt_component_class *trimmer_class = NULL;
265 struct bt_value *trimmer_params = NULL;
266
267 assert(component_factory);
268 trimmer_params = bt_value_map_create();
269 if (!trimmer_params) {
270 goto end;
271 }
272
273 if (source_cfg->begin.set) {
274 enum bt_value_status ret;
275 struct bt_value *value;
276
277 value = bt_value_integer_create_init(
278 source_cfg->begin.value_ns);
279 if (!value) {
280 goto end;
281 }
282
283 ret = bt_value_map_insert(trimmer_params, "begin_ns_epoch",
284 value);
285 BT_PUT(value);
286 if (ret) {
287 goto end;
288 }
289 }
290 if (source_cfg->end.set) {
291 enum bt_value_status ret;
292 struct bt_value *value;
293
294 value = bt_value_integer_create_init(
295 source_cfg->end.value_ns);
296 if (!value) {
297 goto end;
298 }
299
300 ret = bt_value_map_insert(trimmer_params, "end_ns_epoch",
301 value);
302 BT_PUT(value);
303 if (ret) {
304 goto end;
305 }
306 }
307
308 trimmer_class = bt_component_factory_get_component_class(
309 component_factory, "utils", BT_COMPONENT_TYPE_FILTER,
310 "trimmer");
311 if (!trimmer_class) {
312 fprintf(stderr, "Could not find trimmer component class. Aborting...\n");
313 goto end;
314 }
315 trimmer = bt_component_create(trimmer_class, "source_trimmer",
316 trimmer_params);
317 if (!trimmer) {
318 goto end;
319 }
320 end:
321 bt_put(trimmer_params);
322 bt_put(trimmer_class);
323 return trimmer;
324 }
325
326 static
327 int connect_source_sink(struct bt_component *source,
328 struct bt_config_component *source_cfg,
329 struct bt_component *sink)
330 {
331 int ret = 0;
332 enum bt_component_status sink_status;
333 struct bt_component *trimmer = NULL;
334 struct bt_notification_iterator *source_it = NULL;
335 struct bt_notification_iterator *to_sink_it = NULL;
336
337 source_it = bt_component_source_create_iterator(source);
338 if (!source_it) {
339 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
340 ret = -1;
341 goto end;
342 }
343
344 if (source_cfg->begin.set || source_cfg->begin.set) {
345 /* A trimmer must be inserted in the graph. */
346 enum bt_component_status trimmer_status;
347
348 trimmer = create_trimmer(source_cfg);
349 if (!trimmer) {
350 fprintf(stderr, "Failed to create trimmer component. Aborting...\n");
351 ret = -1;
352 goto end;
353 }
354
355 trimmer_status = bt_component_filter_add_iterator(trimmer,
356 source_it);
357 BT_PUT(source_it);
358 if (trimmer_status != BT_COMPONENT_STATUS_OK) {
359 fprintf(stderr, "Failed to connect source to trimmer. Aborting...\n");
360 ret = -1;
361 goto end;
362 }
363
364 to_sink_it = bt_component_filter_create_iterator(trimmer);
365 if (!to_sink_it) {
366 fprintf(stderr, "Failed to instantiate trimmer iterator. Aborting...\n");
367 ret = -1;
368 goto end;
369 }
370 } else {
371 BT_MOVE(to_sink_it, source_it);
372 }
373
374 sink_status = bt_component_sink_add_iterator(sink, to_sink_it);
375 if (sink_status != BT_COMPONENT_STATUS_OK) {
376 fprintf(stderr, "Failed to connect to sink component. Aborting...\n");
377 ret = -1;
378 goto end;
379 }
380 end:
381 bt_put(trimmer);
382 bt_put(source_it);
383 bt_put(to_sink_it);
384 return ret;
385 }
386
387 int main(int argc, char **argv)
388 {
389 int ret;
390 struct bt_component_class *source_class = NULL;
391 struct bt_component_class *sink_class = NULL;
392 struct bt_component *source = NULL, *sink = NULL;
393 struct bt_value *source_params = NULL, *sink_params = NULL;
394 struct bt_config *cfg;
395 enum bt_component_status sink_status;
396 struct bt_value *first_plugin_path_value = NULL;
397 const char *first_plugin_path;
398 struct bt_config_component *source_cfg = NULL, *sink_cfg = NULL;
399
400 cfg = bt_config_from_args(argc, argv, &ret);
401 if (cfg) {
402 print_cfg(cfg);
403 } else {
404 goto end;
405 }
406
407 babeltrace_verbose = cfg->verbose;
408 babeltrace_debug = cfg->debug;
409
410 /* TODO handle more than 1 source and 1 sink. */
411 if (cfg->sources->len != 1 || cfg->sinks->len != 1) {
412 fprintf(stderr, "Unexpected configuration, aborting...\n");
413 ret = -1;
414 goto end;
415 }
416
417 printf_verbose("Verbose mode active.\n");
418 printf_debug("Debug mode active.\n");
419 component_factory = bt_component_factory_create();
420 if (!component_factory) {
421 fprintf(stderr, "Failed to create component factory.\n");
422 ret = -1;
423 goto end;
424 }
425
426 if (cfg->plugin_paths && !bt_value_array_is_empty(cfg->plugin_paths)) {
427 first_plugin_path_value = bt_value_array_get(
428 cfg->plugin_paths, 0);
429 bt_value_string_get(first_plugin_path_value,
430 &first_plugin_path);
431 ret = bt_component_factory_load_recursive(component_factory,
432 first_plugin_path);
433 if (ret) {
434 fprintf(stderr, "Failed to dynamically load plugins.\n");
435 goto end;
436 }
437 }
438
439 ret = bt_component_factory_load_static(component_factory);
440 if (ret) {
441 fprintf(stderr, "Failed to load static plugins.\n");
442 goto end;
443 }
444
445 print_component_classes_found(component_factory);
446
447 source_cfg = bt_config_get_component(cfg->sources, 0);
448 source_params = bt_get(source_cfg->params);
449 source_class = bt_component_factory_get_component_class(
450 component_factory, source_cfg->plugin_name->str,
451 BT_COMPONENT_TYPE_SOURCE,
452 source_cfg->component_name->str);
453 if (!source_class) {
454 fprintf(stderr, "Could not find %s.%s source component class. Aborting...\n",
455 source_cfg->plugin_name->str,
456 source_cfg->component_name->str);
457 ret = -1;
458 goto end;
459 }
460
461 sink_cfg = bt_config_get_component(cfg->sinks, 0);
462 sink_params = bt_get(sink_cfg->params);
463 sink_class = bt_component_factory_get_component_class(component_factory,
464 sink_cfg->plugin_name->str, BT_COMPONENT_TYPE_SINK,
465 sink_cfg->component_name->str);
466 if (!sink_class) {
467 fprintf(stderr, "Could not find %s.%s output component class. Aborting...\n",
468 sink_cfg->plugin_name->str,
469 sink_cfg->component_name->str);
470 ret = -1;
471 goto end;
472 }
473
474 source = bt_component_create(source_class, "source", source_params);
475 if (!source) {
476 fprintf(stderr, "Failed to instantiate selected source component. Aborting...\n");
477 ret = -1;
478 goto end;
479 }
480
481 sink = bt_component_create(sink_class, "sink", sink_params);
482 if (!sink) {
483 fprintf(stderr, "Failed to instantiate selected output component. Aborting...\n");
484 ret = -1;
485 goto end;
486 }
487
488 ret = connect_source_sink(source, source_cfg, sink);
489 if (ret) {
490 goto end;
491 }
492
493 while (true) {
494 sink_status = bt_component_sink_consume(sink);
495 switch (sink_status) {
496 case BT_COMPONENT_STATUS_AGAIN:
497 /* Wait for an arbitraty 500 ms. */
498 usleep(500000);
499 break;
500 case BT_COMPONENT_STATUS_OK:
501 break;
502 case BT_COMPONENT_STATUS_END:
503 goto end;
504 default:
505 fprintf(stderr, "Sink component returned an error, aborting...\n");
506 ret = -1;
507 goto end;
508 }
509 }
510 end:
511 BT_PUT(component_factory);
512 BT_PUT(sink_class);
513 BT_PUT(source_class);
514 BT_PUT(source);
515 BT_PUT(sink);
516 BT_PUT(source_params);
517 BT_PUT(sink_params);
518 BT_PUT(cfg);
519 BT_PUT(first_plugin_path_value);
520 BT_PUT(sink_cfg);
521 BT_PUT(source_cfg);
522 return ret ? 1 : 0;
523 }
This page took 0.03838 seconds and 3 git commands to generate.