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