babeltrace-cfg: fix terminology (component instance vs. class)
[babeltrace.git] / converter / babeltrace.c
CommitLineData
34ac0e6c
MD
1/*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
34ac0e6c
MD
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.
c462e188
MD
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.
34ac0e6c 27 */
4c8bfb7e 28
95d36295 29#include <babeltrace/babeltrace.h>
33bceaf8 30#include <babeltrace/plugin/component-factory.h>
7c7c0433
JG
31#include <babeltrace/plugin/plugin.h>
32#include <babeltrace/plugin/component-class.h>
90298357 33#include <babeltrace/plugin/notification/iterator.h>
2e339de1
JG
34#include <babeltrace/ref.h>
35#include <babeltrace/values.h>
34ac0e6c 36#include <stdlib.h>
a44bc4c9 37#include <babeltrace/ctf-ir/metadata.h> /* for clocks */
7f26a816
PP
38#include <popt.h>
39#include <string.h>
40#include <stdio.h>
c42c79ea 41#include "babeltrace-cfg.h"
34ac0e6c 42
7c7c0433
JG
43static
44const char *component_type_str(enum bt_component_type type)
45{
46 switch (type) {
47 case BT_COMPONENT_TYPE_SOURCE:
48 return "source";
49 case BT_COMPONENT_TYPE_SINK:
50 return "sink";
51 case BT_COMPONENT_TYPE_FILTER:
52 return "filter";
53 case BT_COMPONENT_TYPE_UNKNOWN:
54 default:
55 return "unknown";
56 }
57}
58
59static
c42c79ea 60void print_component_classes_found(struct bt_component_factory *factory)
7c7c0433
JG
61{
62 int count, i;
63
64 if (!babeltrace_verbose) {
65 return;
66 }
67
68 count = bt_component_factory_get_component_class_count(factory);
69 if (count <= 0) {
5e86a071 70 fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.\n");
7c7c0433
JG
71 return;
72 }
73
74 printf_verbose("Found %d component classes.\n", count);
75 for (i = 0; i < count; i++) {
76 struct bt_component_class *component_class =
77 bt_component_factory_get_component_class_index(
78 factory, i);
79 struct bt_plugin *plugin = bt_component_class_get_plugin(
80 component_class);
81 const char *plugin_name = bt_plugin_get_name(plugin);
82 const char *component_name = bt_component_class_get_name(
83 component_class);
84 const char *path = bt_plugin_get_path(plugin);
85 const char *author = bt_plugin_get_author(plugin);
86 const char *license = bt_plugin_get_license(plugin);
87 const char *plugin_description = bt_plugin_get_description(
88 plugin);
89 const char *component_description =
90 bt_component_class_get_description(
91 component_class);
92 enum bt_component_type type = bt_component_class_get_type(
93 component_class);
94
95 printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
96 component_type_str(type));
85411a39
JG
97 printf_verbose("\tpath: %s\n", path ? path : "None");
98 printf_verbose("\tauthor: %s\n", author ? author : "Unknown");
99 printf_verbose("\tlicense: %s\n", license ? license : "Unknown");
7c7c0433
JG
100 printf_verbose("\tplugin description: %s\n",
101 plugin_description ? plugin_description : "None");
102 printf_verbose("\tcomponent description: %s\n",
103 component_description ? component_description : "None");
0cc9e945
JG
104
105 bt_put(plugin);
106 bt_put(component_class);
7c7c0433
JG
107 }
108}
109
c42c79ea
PP
110static
111void print_indent(size_t indent)
112{
113 size_t i;
114
115 for (i = 0; i < indent; i++) {
116 printf(" ");
117 }
118}
119
120static
121void print_value(struct bt_value *, size_t, bool);
122
123static
124bool print_map_value(const char *key, struct bt_value *object, void *data)
125{
126 size_t indent = (size_t) data;
127
128 print_indent(indent);
129 printf("\"%s\": ", key);
130 print_value(object, indent, false);
131
132 return true;
133}
134
135static
136void print_value(struct bt_value *value, size_t indent, bool do_indent)
137{
138 bool bool_val;
139 int64_t int_val;
140 double dbl_val;
141 const char *str_val;
142 int size;
143 int i;
144
145 if (!value) {
146 return;
147 }
148
149 if (do_indent) {
150 print_indent(indent);
151 }
152
153 switch (bt_value_get_type(value)) {
154 case BT_VALUE_TYPE_NULL:
155 printf("null\n");
156 break;
157 case BT_VALUE_TYPE_BOOL:
158 bt_value_bool_get(value, &bool_val);
159 printf("%s\n", bool_val ? "true" : "false");
160 break;
161 case BT_VALUE_TYPE_INTEGER:
162 bt_value_integer_get(value, &int_val);
163 printf("%" PRId64 "\n", int_val);
164 break;
165 case BT_VALUE_TYPE_FLOAT:
166 bt_value_float_get(value, &dbl_val);
167 printf("%lf\n", dbl_val);
168 break;
169 case BT_VALUE_TYPE_STRING:
170 bt_value_string_get(value, &str_val);
171 printf("\"%s\"\n", str_val);
172 break;
173 case BT_VALUE_TYPE_ARRAY:
174 size = bt_value_array_size(value);
175 printf("[\n");
176
177 for (i = 0; i < size; i++) {
178 struct bt_value *element =
179 bt_value_array_get(value, i);
180
181 print_value(element, indent + 2, true);
182 BT_PUT(element);
183 }
184
185 print_indent(indent);
186 printf("]\n");
187 break;
188 case BT_VALUE_TYPE_MAP:
189 if (bt_value_map_is_empty(value)) {
190 printf("{}\n");
191 return;
192 }
193
194 printf("{\n");
195 bt_value_map_foreach(value, print_map_value,
196 (void *) (indent + 2));
197 print_indent(indent);
198 printf("}\n");
199 break;
200 default:
201 assert(false);
202 }
203}
204
205static
206void print_bt_config_component(struct bt_config_component *bt_config_component)
207{
5f45c11f 208 printf(" %s.%s\n", bt_config_component->plugin_name->str,
c42c79ea 209 bt_config_component->component_name->str);
e442b41e
PP
210 printf(" begin timestamp: ");
211
212 if (bt_config_component->begin_ns == -1ULL) {
213 printf("not set\n");
214 } else {
215 printf("%" PRIu64 " ns\n", bt_config_component->begin_ns);
216 }
217
218 printf(" end timestamp: ");
219
220 if (bt_config_component->end_ns == -1ULL) {
221 printf("not set\n");
222 } else {
223 printf("%" PRIu64 " ns\n", bt_config_component->end_ns);
224 }
225
c42c79ea
PP
226 printf(" params:\n");
227 print_value(bt_config_component->params, 6, true);
228}
229
230static
231void print_bt_config_components(GPtrArray *array)
232{
233 size_t i;
234
235 for (i = 0; i < array->len; i++) {
236 struct bt_config_component *cfg_component =
237 bt_config_get_component(array, i);
238 print_bt_config_component(cfg_component);
239 BT_PUT(cfg_component);
240 }
241}
242
243static
244void print_cfg(struct bt_config *cfg)
245{
246 printf("debug: %d\n", cfg->debug);
247 printf("verbose: %d\n", cfg->verbose);
248 printf("do list: %d\n", cfg->do_list);
249 printf("force correlate: %d\n", cfg->force_correlate);
250 printf("plugin paths:\n");
251 print_value(cfg->plugin_paths, 2, true);
252 printf("sources:\n");
253 print_bt_config_components(cfg->sources);
254 printf("sinks:\n");
255 print_bt_config_components(cfg->sinks);
256}
257
bbefb8dd 258int main(int argc, char **argv)
34ac0e6c 259{
7f26a816 260 int ret;
7c7c0433 261 struct bt_component_factory *component_factory = NULL;
c42c79ea
PP
262 struct bt_component_class *source_class = NULL;
263 struct bt_component_class *sink_class = NULL;
7c7c0433
JG
264 struct bt_component *source = NULL, *sink = NULL;
265 struct bt_value *source_params = NULL, *sink_params = NULL;
c42c79ea 266 struct bt_config *cfg;
90298357 267 struct bt_notification_iterator *it = NULL;
fec2a9f2 268 enum bt_component_status sink_status;
c42c79ea
PP
269 struct bt_value *first_plugin_path_value = NULL;
270 const char *first_plugin_path;
271 struct bt_config_component *cfg_component;
272
273 cfg = bt_config_from_args(argc, argv, &ret);
274 if (cfg) {
275 print_cfg(cfg);
276 } else {
56a1cced
JG
277 goto end;
278 }
279
8b596b57
JG
280 babeltrace_verbose = cfg->verbose;
281 babeltrace_debug = cfg->debug;
282
c42c79ea
PP
283 /* TODO handle more than 1 source and 1 sink. */
284 if (cfg->sources->len != 1 || cfg->sinks->len != 1) {
5f45c11f 285 fprintf(stderr, "Unexpected configuration, aborting...\n");
56a1cced
JG
286 ret = -1;
287 goto end;
288 }
c42c79ea
PP
289 cfg_component = bt_config_get_component(cfg->sources, 0);
290 source_params = bt_get(cfg_component->params);
291 BT_PUT(cfg_component);
292 cfg_component = bt_config_get_component(cfg->sinks, 0);
293 sink_params = bt_get(cfg_component->params);
294 BT_PUT(cfg_component);
56a1cced 295
34ac0e6c
MD
296 printf_verbose("Verbose mode active.\n");
297 printf_debug("Debug mode active.\n");
33bceaf8
JG
298 component_factory = bt_component_factory_create();
299 if (!component_factory) {
300 fprintf(stderr, "Failed to create component factory.\n");
301 ret = -1;
302 goto end;
303 }
304
b86d17b1
JG
305 if (cfg->plugin_paths && !bt_value_array_is_empty(cfg->plugin_paths)) {
306 first_plugin_path_value = bt_value_array_get(
307 cfg->plugin_paths, 0);
308 bt_value_string_get(first_plugin_path_value,
309 &first_plugin_path);
310 ret = bt_component_factory_load_recursive(component_factory,
311 first_plugin_path);
312 if (ret) {
313 fprintf(stderr, "Failed to dynamically load plugins.\n");
314 goto end;
315 }
33bceaf8
JG
316 }
317
cba174d5
JG
318 ret = bt_component_factory_load_static(component_factory);
319 if (ret) {
320 fprintf(stderr, "Failed to load static plugins.\n");
321 goto end;
322 }
323
c42c79ea 324 print_component_classes_found(component_factory);
56a1cced
JG
325 source_class = bt_component_factory_get_component_class(
326 component_factory, "ctf", BT_COMPONENT_TYPE_SOURCE,
327 "fs");
328 if (!source_class) {
043e2020 329 fprintf(stderr, "Could not find ctf-fs source component class. Aborting...\n");
56a1cced
JG
330 ret = -1;
331 goto end;
332 }
7c7c0433
JG
333
334 sink_class = bt_component_factory_get_component_class(component_factory,
c42c79ea 335 NULL, BT_COMPONENT_TYPE_SINK, "text");
7c7c0433 336 if (!sink_class) {
c42c79ea 337 fprintf(stderr, "Could not find text output component class. Aborting...\n");
7c7c0433
JG
338 ret = -1;
339 goto end;
340 }
341
56a1cced
JG
342 source = bt_component_create(source_class, "ctf-fs", source_params);
343 if (!source) {
c42c79ea
PP
344 fprintf(stderr, "Failed to instantiate ctf-fs source component. Aborting...\n");
345 ret = -1;
346 goto end;
347 }
56a1cced 348
c42c79ea 349 sink = bt_component_create(sink_class, "text", sink_params);
7c7c0433 350 if (!sink) {
c42c79ea 351 fprintf(stderr, "Failed to instanciate text output component. Aborting...\n");
7c7c0433 352 ret = -1;
2e339de1
JG
353 goto end;
354 }
355
90298357
JG
356 it = bt_component_source_create_iterator(source);
357 if (!it) {
358 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
c42c79ea
PP
359 ret = -1;
360 goto end;
361 }
90298357 362
fec2a9f2
JG
363 sink_status = bt_component_sink_add_iterator(sink, it);
364 if (sink_status != BT_COMPONENT_STATUS_OK) {
365 ret = -1;
366 goto end;
367 }
78586d8a 368
fec2a9f2
JG
369 while (true) {
370 sink_status = bt_component_sink_consume(sink);
fec2a9f2
JG
371 switch (sink_status) {
372 case BT_COMPONENT_STATUS_AGAIN:
373 /* Wait for an arbitraty 500 ms. */
374 usleep(500000);
78586d8a 375 break;
fec2a9f2
JG
376 case BT_COMPONENT_STATUS_OK:
377 break;
378 case BT_COMPONENT_STATUS_END:
379 goto end;
380 default:
381 fprintf(stderr, "Sink component returned an error, aborting...\n");
382 ret = -1;
383 goto end;
78586d8a 384 }
fec2a9f2 385 }
11e1d048 386end:
2e339de1 387 BT_PUT(component_factory);
7c7c0433
JG
388 BT_PUT(sink_class);
389 BT_PUT(source_class);
390 BT_PUT(source);
391 BT_PUT(sink);
392 BT_PUT(source_params);
393 BT_PUT(sink_params);
90298357 394 BT_PUT(it);
c42c79ea
PP
395 BT_PUT(cfg);
396 BT_PUT(first_plugin_path_value);
7f26a816 397 return ret ? 1 : 0;
4c8bfb7e 398}
This page took 0.056962 seconds and 4 git commands to generate.