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