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