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
3d731ea9
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>
57952005 33#include "common/assert.h"
04c0ba87
JG
34#include "metadata.h"
35#include "../common/metadata/decoder.h"
57952005 36#include "common/common.h"
85e7137b 37#include "common/macros.h"
71c5da58 38#include <babeltrace2/babeltrace.h>
97ade20b 39#include "fs.h"
5c3441d8 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
fb25b9e3 51bt_component_class_query_method_status metadata_info_query(
5c3441d8 52 bt_self_component_class_source *self_comp_class_src,
3d731ea9 53 const bt_value *params, bt_logging_level log_level,
8eee8ea2 54 const bt_value **user_result)
04c0ba87 55{
fb25b9e3
PP
56 bt_component_class_query_method_status status =
57 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
5c3441d8
SM
58 bt_self_component_class *self_comp_class =
59 bt_self_component_class_source_as_self_component_class(self_comp_class_src);
8eee8ea2
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;
526c3a49
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
ce141536 71 result = bt_value_map_create();
17582c6d 72 if (!result) {
fb25b9e3 73 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
04c0ba87
JG
74 goto error;
75 }
76
8b45963b
PP
77 BT_ASSERT(params);
78
04c0ba87 79 if (!bt_value_is_map(params)) {
5c3441d8
SM
80 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
81 "Query parameters is not a map value object.");
574dea68 82 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
04c0ba87
JG
83 goto error;
84 }
85
ce141536 86 path_value = bt_value_map_borrow_entry_value_const(params, "path");
43eacc52 87 if (!path_value) {
5c3441d8
SM
88 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
89 "Mandatory `path` parameter missing");
574dea68 90 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
43eacc52
FD
91 goto error;
92 }
93
94 if (!bt_value_is_string(path_value)) {
5c3441d8
SM
95 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
96 "`path` parameter is required to be a string value");
574dea68 97 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
43eacc52
FD
98 goto error;
99 }
100
b5cdc106 101 path = bt_value_string_get(path_value);
04c0ba87 102
8b45963b 103 BT_ASSERT(path);
04c0ba87
JG
104 metadata_fp = ctf_fs_metadata_open_file(path);
105 if (!metadata_fp) {
5c3441d8
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
526c3a49 111 ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized,
3d731ea9 112 &bo, log_level, NULL);
526c3a49
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
526c3a49
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
526c3a49
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
ce141536 139 ret = bt_value_map_insert_string_entry(result, "text",
526c3a49 140 ctf_metadata_decoder_get_text(decoder));
04c0ba87 141 if (ret) {
5c3441d8
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
ce141536 147 ret = bt_value_map_insert_bool_entry(result, "is-packetized",
04c0ba87
JG
148 is_packetized);
149 if (ret) {
5c3441d8
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:
8c6884d9 158 BT_VALUE_PUT_REF_AND_RESET(result);
834e9996 159 result = NULL;
1286dcbb 160
834e9996 161 if (status >= 0) {
fb25b9e3 162 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
1286dcbb 163 }
04c0ba87
JG
164
165end:
526c3a49 166 ctf_metadata_decoder_destroy(decoder);
04c0ba87
JG
167
168 if (metadata_fp) {
169 fclose(metadata_fp);
170 }
1286dcbb 171
ce141536 172 *user_result = result;
834e9996 173 return status;
97ade20b 174}
13160acc 175
97ade20b 176static
8eee8ea2 177int add_range(bt_value *info, struct range *range,
97ade20b
JG
178 const char *range_name)
179{
180 int ret = 0;
fb25b9e3 181 bt_value_map_insert_entry_status status;
de37c3a9 182 bt_value *range_map;
97ade20b
JG
183
184 if (!range->set) {
185 /* Not an error. */
186 goto end;
187 }
188
de37c3a9
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
68d9d039 196 status = bt_value_map_insert_signed_integer_entry(range_map, "begin",
97ade20b 197 range->begin_ns);
fb25b9e3 198 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
97ade20b
JG
199 ret = -1;
200 goto end;
201 }
202
68d9d039 203 status = bt_value_map_insert_signed_integer_entry(range_map, "end",
97ade20b 204 range->end_ns);
fb25b9e3 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,
8eee8ea2 216 bt_value *group_info, struct range *stream_range)
97ade20b
JG
217{
218 int ret = 0;
fb25b9e3 219 bt_value_map_insert_entry_status insert_status;
1924bed7 220 struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry;
ddf49b27 221 gchar *port_name = NULL;
97ade20b 222
1924bed7 223 /*
779f4a23
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.
1924bed7 228 */
779f4a23
FD
229 BT_ASSERT(group->index);
230 BT_ASSERT(group->index->entries);
231 BT_ASSERT(group->index->entries->len > 0);
1924bed7 232
779f4a23
FD
233 /* First entry. */
234 first_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
235 group->index->entries, 0);
1924bed7 236
779f4a23
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);
1924bed7
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;
1924bed7 243
58117c6d
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
ddf49b27
SM
257 port_name = ctf_fs_make_port_name(group);
258 if (!port_name) {
259 ret = -1;
260 goto end;
261 }
262
fb25b9e3
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) {
ddf49b27
SM
266 ret = -1;
267 goto end;
268 }
269
97ade20b 270end:
be1b2e7e 271 g_free(port_name);
97ade20b
JG
272 return ret;
273}
274
275static
bf5b7748 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;
fb25b9e3
PP
280 bt_value_map_insert_entry_status insert_status;
281 bt_value_array_append_element_status append_status;
bf5b7748 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
bf5b7748
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
de37c3a9
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++) {
8eee8ea2 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
de37c3a9
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) {
ab19273b
FD
320 goto end;
321 }
322
97ade20b 323 if (group_range.set) {
85e7137b 324 trace_intersection.begin_ns = MAX(trace_intersection.begin_ns,
97ade20b 325 group_range.begin_ns);
85e7137b 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
8ea849b8
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
fb25b9e3 345bt_component_class_query_method_status trace_info_query(
5c3441d8 346 bt_self_component_class_source *self_comp_class_src,
3d731ea9 347 const bt_value *params, bt_logging_level log_level,
8eee8ea2 348 const bt_value **user_result)
97ade20b 349{
bf5b7748 350 struct ctf_fs_component *ctf_fs = NULL;
fb25b9e3
PP
351 bt_component_class_query_method_status status =
352 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
5c3441d8
SM
353 bt_self_component_class *self_comp_class =
354 bt_self_component_class_source_as_self_component_class(
355 self_comp_class_src);
8eee8ea2 356 bt_value *result = NULL;
a1040187 357 const bt_value *inputs_value = NULL;
97ade20b 358 int ret = 0;
bf5b7748 359 guint i;
97ade20b 360
8b45963b
PP
361 BT_ASSERT(params);
362
97ade20b 363 if (!bt_value_is_map(params)) {
5c3441d8
SM
364 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
365 "Query parameters is not a map value object.");
574dea68 366 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
97ade20b
JG
367 goto error;
368 }
369
f67894f9 370 ctf_fs = ctf_fs_component_create(log_level, NULL);
c80d4c65 371 if (!ctf_fs) {
97ade20b
JG
372 goto error;
373 }
97ade20b 374
5c3441d8
SM
375 if (!read_src_fs_parameters(params, &inputs_value, ctf_fs, NULL,
376 self_comp_class)) {
574dea68 377 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
97ade20b
JG
378 goto error;
379 }
380
5c3441d8
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
ce141536 386 result = bt_value_array_create();
17582c6d 387 if (!result) {
fb25b9e3 388 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
97ade20b
JG
389 goto error;
390 }
391
bf5b7748
SM
392 for (i = 0; i < ctf_fs->traces->len; i++) {
393 struct ctf_fs_trace *trace;
8eee8ea2 394 bt_value *trace_info;
fb25b9e3 395 bt_value_array_append_element_status append_status;
bf5b7748
SM
396
397 trace = g_ptr_array_index(ctf_fs->traces, i);
398 BT_ASSERT(trace);
97ade20b 399
de37c3a9
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) {
5c3441d8
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
bf5b7748 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:
8c6884d9 417 BT_VALUE_PUT_REF_AND_RESET(result);
834e9996 418 result = NULL;
1286dcbb 419
834e9996 420 if (status >= 0) {
fb25b9e3 421 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
1286dcbb
PP
422 }
423
97ade20b 424end:
bf5b7748
SM
425 if (ctf_fs) {
426 ctf_fs_destroy(ctf_fs);
427 ctf_fs = NULL;
97ade20b 428 }
834e9996 429
ce141536 430 *user_result = result;
834e9996 431 return status;
04c0ba87 432}
a1040187
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.070421 seconds and 4 git commands to generate.