ctf: add internal ctf_metadata_decoder_get_trace_class_uuid()
[babeltrace.git] / src / plugins / ctf / fs-src / query.c
CommitLineData
04c0ba87
JG
1/*
2 * query.c
3 *
4 * Babeltrace CTF file system Reader Component queries
5 *
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
98903a3e
PP
27#define BT_LOG_OUTPUT_LEVEL log_level
28#define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY"
29#include "logging/log.h"
30
04c0ba87
JG
31#include "query.h"
32#include <stdbool.h>
578e048b 33#include "common/assert.h"
04c0ba87
JG
34#include "metadata.h"
35#include "../common/metadata/decoder.h"
578e048b 36#include "common/common.h"
91d81473 37#include "common/macros.h"
3fadfbc0 38#include <babeltrace2/babeltrace.h>
97ade20b 39#include "fs.h"
d23b766e 40#include "logging/comp-logging.h"
55314f2a 41
04c0ba87
JG
42#define METADATA_TEXT_SIG "/* CTF 1.8"
43
97ade20b
JG
44struct range {
45 int64_t begin_ns;
46 int64_t end_ns;
47 bool set;
48};
49
04c0ba87 50BT_HIDDEN
d24d5663 51bt_component_class_query_method_status metadata_info_query(
d23b766e 52 bt_self_component_class_source *self_comp_class_src,
98903a3e 53 const bt_value *params, bt_logging_level log_level,
b19ff26f 54 const bt_value **user_result)
04c0ba87 55{
d24d5663
PP
56 bt_component_class_query_method_status status =
57 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
d23b766e
SM
58 bt_self_component_class *self_comp_class =
59 bt_self_component_class_source_as_self_component_class(self_comp_class_src);
b19ff26f
PP
60 bt_value *result = NULL;
61 const bt_value *path_value = NULL;
04c0ba87 62 FILE *metadata_fp = NULL;
04c0ba87
JG
63 int ret;
64 int bo;
65 const char *path;
66 bool is_packetized;
06be9946
PP
67 struct ctf_metadata_decoder *decoder = NULL;
68 struct ctf_metadata_decoder_config decoder_cfg = { 0 };
69 enum ctf_metadata_decoder_status decoder_status;
04c0ba87 70
05e21286 71 result = bt_value_map_create();
da91b29a 72 if (!result) {
d24d5663 73 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
04c0ba87
JG
74 goto error;
75 }
76
f6ccaed9
PP
77 BT_ASSERT(params);
78
04c0ba87 79 if (!bt_value_is_map(params)) {
d23b766e
SM
80 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
81 "Query parameters is not a map value object.");
a635e507 82 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
04c0ba87
JG
83 goto error;
84 }
85
05e21286 86 path_value = bt_value_map_borrow_entry_value_const(params, "path");
07330b50 87 if (!path_value) {
d23b766e
SM
88 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
89 "Mandatory `path` parameter missing");
a635e507 90 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
07330b50
FD
91 goto error;
92 }
93
94 if (!bt_value_is_string(path_value)) {
d23b766e
SM
95 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
96 "`path` parameter is required to be a string value");
a635e507 97 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
07330b50
FD
98 goto error;
99 }
100
601b0d3c 101 path = bt_value_string_get(path_value);
04c0ba87 102
f6ccaed9 103 BT_ASSERT(path);
04c0ba87
JG
104 metadata_fp = ctf_fs_metadata_open_file(path);
105 if (!metadata_fp) {
d23b766e
SM
106 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
107 "Cannot open trace metadata: path=\"%s\".", path);
04c0ba87
JG
108 goto error;
109 }
110
06be9946 111 ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized,
98903a3e 112 &bo, log_level, NULL);
06be9946
PP
113 if (ret) {
114 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
115 "Cannot check whether or not the metadata stream is packetized: path=\"%s\".",
116 path);
117 goto error;
04c0ba87
JG
118 }
119
06be9946
PP
120 decoder_cfg.log_level = log_level;
121 decoder_cfg.keep_plain_text = true;
122 decoder = ctf_metadata_decoder_create(&decoder_cfg);
123 if (!decoder) {
124 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
125 "Cannot create metadata decoder: path=\"%s\".", path);
04c0ba87
JG
126 goto error;
127 }
128
06be9946
PP
129 rewind(metadata_fp);
130 decoder_status = ctf_metadata_decoder_append_content(decoder,
131 metadata_fp);
132 if (decoder_status) {
133 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
134 "Cannot update metadata decoder's content: path=\"%s\".",
135 path);
136 goto error;
04c0ba87
JG
137 }
138
05e21286 139 ret = bt_value_map_insert_string_entry(result, "text",
06be9946 140 ctf_metadata_decoder_get_text(decoder));
04c0ba87 141 if (ret) {
d23b766e
SM
142 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
143 "Cannot insert metadata text into query result.");
04c0ba87
JG
144 goto error;
145 }
146
05e21286 147 ret = bt_value_map_insert_bool_entry(result, "is-packetized",
04c0ba87
JG
148 is_packetized);
149 if (ret) {
d23b766e
SM
150 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
151 "Cannot insert \"is-packetized\" attribute into query result.");
04c0ba87
JG
152 goto error;
153 }
154
155 goto end;
156
157error:
c5b9b441 158 BT_VALUE_PUT_REF_AND_RESET(result);
d94d92ac 159 result = NULL;
c7eee084 160
d94d92ac 161 if (status >= 0) {
d24d5663 162 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
c7eee084 163 }
04c0ba87
JG
164
165end:
06be9946 166 ctf_metadata_decoder_destroy(decoder);
04c0ba87
JG
167
168 if (metadata_fp) {
169 fclose(metadata_fp);
170 }
c7eee084 171
05e21286 172 *user_result = result;
d94d92ac 173 return status;
97ade20b 174}
9ec238a8 175
97ade20b 176static
b19ff26f 177int add_range(bt_value *info, struct range *range,
97ade20b
JG
178 const char *range_name)
179{
180 int ret = 0;
d24d5663 181 bt_value_map_insert_entry_status status;
760b266c 182 bt_value *range_map;
97ade20b
JG
183
184 if (!range->set) {
185 /* Not an error. */
186 goto end;
187 }
188
760b266c
SM
189 status = bt_value_map_insert_empty_map_entry(info, range_name,
190 &range_map);
191 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
97ade20b
JG
192 ret = -1;
193 goto end;
194 }
195
fdd3a2da 196 status = bt_value_map_insert_signed_integer_entry(range_map, "begin",
97ade20b 197 range->begin_ns);
d24d5663 198 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
97ade20b
JG
199 ret = -1;
200 goto end;
201 }
202
fdd3a2da 203 status = bt_value_map_insert_signed_integer_entry(range_map, "end",
97ade20b 204 range->end_ns);
d24d5663 205 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
97ade20b
JG
206 ret = -1;
207 goto end;
208 }
209
97ade20b 210end:
97ade20b
JG
211 return ret;
212}
213
97ade20b
JG
214static
215int populate_stream_info(struct ctf_fs_ds_file_group *group,
b19ff26f 216 bt_value *group_info, struct range *stream_range)
97ade20b
JG
217{
218 int ret = 0;
d24d5663 219 bt_value_map_insert_entry_status insert_status;
b1c001b6 220 struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry;
a38d7650 221 gchar *port_name = NULL;
97ade20b 222
b1c001b6 223 /*
7ed5243a
FD
224 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
225 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
226 * the timestamp_begin of the first index entry and the timestamp_end
227 * of the last index entry.
b1c001b6 228 */
7ed5243a
FD
229 BT_ASSERT(group->index);
230 BT_ASSERT(group->index->entries);
231 BT_ASSERT(group->index->entries->len > 0);
b1c001b6 232
7ed5243a
FD
233 /* First entry. */
234 first_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
235 group->index->entries, 0);
b1c001b6 236
7ed5243a
FD
237 /* Last entry. */
238 last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
239 group->index->entries, group->index->entries->len - 1);
b1c001b6
FD
240
241 stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns;
242 stream_range->end_ns = last_ds_index_entry->timestamp_end_ns;
b1c001b6 243
1d4ac4b6
FD
244 /*
245 * If any of the begin and end timestamps is not set it means that
246 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
247 * in their packet context so we can't set the range.
248 */
249 stream_range->set = stream_range->begin_ns != UINT64_C(-1) &&
250 stream_range->end_ns != UINT64_C(-1);
251
252 ret = add_range(group_info, stream_range, "range-ns");
253 if (ret) {
254 goto end;
97ade20b
JG
255 }
256
a38d7650
SM
257 port_name = ctf_fs_make_port_name(group);
258 if (!port_name) {
259 ret = -1;
260 goto end;
261 }
262
d24d5663
PP
263 insert_status = bt_value_map_insert_string_entry(group_info,
264 "port-name", port_name);
265 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
a38d7650
SM
266 ret = -1;
267 goto end;
268 }
269
97ade20b 270end:
19bbdc9b 271 g_free(port_name);
97ade20b
JG
272 return ret;
273}
274
275static
f280892e 276int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info)
97ade20b
JG
277{
278 int ret = 0;
279 size_t group_idx;
d24d5663
PP
280 bt_value_map_insert_entry_status insert_status;
281 bt_value_array_append_element_status append_status;
f280892e 282 bt_value *file_groups = NULL;
97ade20b
JG
283 struct range trace_intersection = {
284 .begin_ns = 0,
285 .end_ns = INT64_MAX,
286 .set = false,
287 };
288
f280892e
SM
289 BT_ASSERT(trace->ds_file_groups);
290 /* Add trace range info only if it contains streams. */
291 if (trace->ds_file_groups->len == 0) {
292 ret = -1;
293 goto end;
294 }
295
760b266c
SM
296 insert_status = bt_value_map_insert_empty_array_entry(trace_info,
297 "streams", &file_groups);
298 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
299 ret = -1;
97ade20b
JG
300 goto end;
301 }
302
97ade20b
JG
303 /* Find range of all stream groups, and of the trace. */
304 for (group_idx = 0; group_idx < trace->ds_file_groups->len;
305 group_idx++) {
b19ff26f 306 bt_value *group_info;
97ade20b
JG
307 struct range group_range = { .set = false };
308 struct ctf_fs_ds_file_group *group = g_ptr_array_index(
309 trace->ds_file_groups, group_idx);
310
760b266c
SM
311 append_status = bt_value_array_append_empty_map_element(
312 file_groups, &group_info);
313 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
97ade20b
JG
314 ret = -1;
315 goto end;
316 }
317
318 ret = populate_stream_info(group, group_info, &group_range);
319 if (ret) {
7233204d
FD
320 goto end;
321 }
322
97ade20b 323 if (group_range.set) {
91d81473 324 trace_intersection.begin_ns = MAX(trace_intersection.begin_ns,
97ade20b 325 group_range.begin_ns);
91d81473 326 trace_intersection.end_ns = MIN(trace_intersection.end_ns,
97ade20b
JG
327 group_range.end_ns);
328 trace_intersection.set = true;
97ade20b
JG
329 }
330 }
331
c6daf8b2
PP
332 if (trace_intersection.begin_ns < trace_intersection.end_ns) {
333 ret = add_range(trace_info, &trace_intersection,
334 "intersection-range-ns");
335 if (ret) {
336 goto end;
337 }
97ade20b
JG
338 }
339
97ade20b 340end:
97ade20b
JG
341 return ret;
342}
343
344BT_HIDDEN
d24d5663 345bt_component_class_query_method_status trace_info_query(
d23b766e 346 bt_self_component_class_source *self_comp_class_src,
98903a3e 347 const bt_value *params, bt_logging_level log_level,
b19ff26f 348 const bt_value **user_result)
97ade20b 349{
f280892e 350 struct ctf_fs_component *ctf_fs = NULL;
d24d5663
PP
351 bt_component_class_query_method_status status =
352 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
d23b766e
SM
353 bt_self_component_class *self_comp_class =
354 bt_self_component_class_source_as_self_component_class(
355 self_comp_class_src);
b19ff26f 356 bt_value *result = NULL;
73760435 357 const bt_value *inputs_value = NULL;
97ade20b 358 int ret = 0;
f280892e 359 guint i;
97ade20b 360
f6ccaed9
PP
361 BT_ASSERT(params);
362
97ade20b 363 if (!bt_value_is_map(params)) {
d23b766e
SM
364 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
365 "Query parameters is not a map value object.");
a635e507 366 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
97ade20b
JG
367 goto error;
368 }
369
4c65a157 370 ctf_fs = ctf_fs_component_create(log_level, NULL);
d907165c 371 if (!ctf_fs) {
97ade20b
JG
372 goto error;
373 }
97ade20b 374
d23b766e
SM
375 if (!read_src_fs_parameters(params, &inputs_value, ctf_fs, NULL,
376 self_comp_class)) {
a635e507 377 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
97ade20b
JG
378 goto error;
379 }
380
d23b766e
SM
381 if (ctf_fs_component_create_ctf_fs_traces(ctf_fs, inputs_value, NULL,
382 self_comp_class)) {
97ade20b
JG
383 goto error;
384 }
385
05e21286 386 result = bt_value_array_create();
da91b29a 387 if (!result) {
d24d5663 388 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
97ade20b
JG
389 goto error;
390 }
391
f280892e
SM
392 for (i = 0; i < ctf_fs->traces->len; i++) {
393 struct ctf_fs_trace *trace;
b19ff26f 394 bt_value *trace_info;
d24d5663 395 bt_value_array_append_element_status append_status;
f280892e
SM
396
397 trace = g_ptr_array_index(ctf_fs->traces, i);
398 BT_ASSERT(trace);
97ade20b 399
760b266c
SM
400 append_status = bt_value_array_append_empty_map_element(result,
401 &trace_info);
402 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
d23b766e
SM
403 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
404 "Failed to create trace info map.");
97ade20b
JG
405 goto error;
406 }
407
f280892e 408 ret = populate_trace_info(trace, trace_info);
97ade20b 409 if (ret) {
97ade20b
JG
410 goto error;
411 }
412 }
413
414 goto end;
415
416error:
c5b9b441 417 BT_VALUE_PUT_REF_AND_RESET(result);
d94d92ac 418 result = NULL;
c7eee084 419
d94d92ac 420 if (status >= 0) {
d24d5663 421 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
c7eee084
PP
422 }
423
97ade20b 424end:
f280892e
SM
425 if (ctf_fs) {
426 ctf_fs_destroy(ctf_fs);
427 ctf_fs = NULL;
97ade20b 428 }
d94d92ac 429
05e21286 430 *user_result = result;
d94d92ac 431 return status;
04c0ba87 432}
73760435
SM
433
434BT_HIDDEN
435bt_component_class_query_method_status support_info_query(
436 bt_self_component_class_source *comp_class,
437 const bt_value *params, bt_logging_level log_level,
438 const bt_value **user_result)
439{
440 const bt_value *input_type_value;
441 const char *input_type;
442 bt_component_class_query_method_status status;
443 double weight = 0;
444 gchar *metadata_path = NULL;
445 bt_value *result = NULL;
446
447 input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
448 BT_ASSERT(input_type_value);
449 BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING);
450 input_type = bt_value_string_get(input_type_value);
451
452 result = bt_value_map_create();
453 if (!result) {
454 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
455 goto end;
456 }
457
458 if (strcmp(input_type, "directory") == 0) {
459 const bt_value *input_value;
460 const char *path;
461
462 input_value = bt_value_map_borrow_entry_value_const(params, "input");
463 BT_ASSERT(input_value);
464 BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
465 path = bt_value_string_get(input_value);
466
467 metadata_path = g_build_filename(path, CTF_FS_METADATA_FILENAME, NULL);
468 if (!metadata_path) {
469 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
470 goto end;
471 }
472
473 /*
474 * If the metadata file exists in this directory, consider it to
475 * be a CTF trace.
476 */
477 if (g_file_test(metadata_path, G_FILE_TEST_EXISTS)) {
478 weight = 0.5;
479 }
480 }
481
482 if (bt_value_map_insert_real_entry(result, "weight", weight) != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
483 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
484 goto end;
485 }
486
487 /*
488 * Use the arbitrary constant string "ctf" as the group, such that all
489 * found ctf traces are passed to the same instance of src.ctf.fs.
490 */
491 if (bt_value_map_insert_string_entry(result, "group", "ctf") != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
492 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
493 goto end;
494 }
495
496 *user_result = result;
497 result = NULL;
498 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
499
500end:
501 g_free(metadata_path);
502 bt_value_put_ref(result);
503
504 return status;
505}
This page took 0.071069 seconds and 4 git commands to generate.