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