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