ctf: use `bt2c::Logger` throughout `src.ctf.fs`, `src.ctf.lttng-live`
[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
5656cea5 15#include "../common/src/metadata/tsdl/decoder.hpp"
087cd0f5 16#include "fs.hpp"
c802cacb 17#include "query.hpp"
55314f2a 18
4164020e 19#define METADATA_TEXT_SIG "/* CTF 1.8"
04c0ba87 20
4164020e
SM
21struct range
22{
23 int64_t begin_ns = 0;
24 int64_t end_ns = 0;
25 bool set = false;
97ade20b
JG
26};
27
0f5c5d5c
SM
28bt_component_class_query_method_status metadata_info_query(const bt_value *params,
29 const bt2c::Logger& logger,
30 const bt_value **user_result)
04c0ba87 31{
4164020e 32 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
4164020e
SM
33 bt_value *result = NULL;
34 const bt_value *path_value = NULL;
35 FILE *metadata_fp = NULL;
36 int ret;
37 int bo;
38 const char *path;
39 bool is_packetized;
40 struct ctf_metadata_decoder *decoder = NULL;
0f5c5d5c 41 ctf_metadata_decoder_config decoder_cfg {logger};
4164020e 42 enum ctf_metadata_decoder_status decoder_status;
c719eabb
FD
43 GString *g_metadata_text = NULL;
44 const char *plaintext;
4164020e
SM
45
46 result = bt_value_map_create();
47 if (!result) {
48 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
49 goto error;
50 }
51
52 BT_ASSERT(params);
53
54 if (!bt_value_is_map(params)) {
0f5c5d5c 55 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
4164020e
SM
56 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
57 goto error;
58 }
59
60 path_value = bt_value_map_borrow_entry_value_const(params, "path");
61 if (!path_value) {
0f5c5d5c 62 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Mandatory `path` parameter missing");
4164020e
SM
63 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
64 goto error;
65 }
66
67 if (!bt_value_is_string(path_value)) {
0f5c5d5c 68 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`path` parameter is required to be a string value");
4164020e
SM
69 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
70 goto error;
71 }
72
73 path = bt_value_string_get(path_value);
74
75 BT_ASSERT(path);
0f5c5d5c 76 metadata_fp = ctf_fs_metadata_open_file(path, logger);
4164020e 77 if (!metadata_fp) {
0f5c5d5c 78 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot open trace metadata: path=\"{}\".", path);
4164020e
SM
79 goto error;
80 }
81
0f5c5d5c 82 ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized, &bo, logger);
4164020e 83 if (ret) {
0f5c5d5c
SM
84 BT_CPPLOGE_APPEND_CAUSE_SPEC(
85 logger, "Cannot check whether or not the metadata stream is packetized: path=\"{}\".",
86 path);
4164020e
SM
87 goto error;
88 }
89
4164020e
SM
90 decoder_cfg.keep_plain_text = true;
91 decoder = ctf_metadata_decoder_create(&decoder_cfg);
92 if (!decoder) {
0f5c5d5c 93 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot create metadata decoder: path=\"{}\".", path);
4164020e
SM
94 goto error;
95 }
96
97 rewind(metadata_fp);
98 decoder_status = ctf_metadata_decoder_append_content(decoder, metadata_fp);
99 if (decoder_status) {
0f5c5d5c
SM
100 BT_CPPLOGE_APPEND_CAUSE_SPEC(
101 logger, "Cannot update metadata decoder's content: path=\"{}\".", path);
4164020e
SM
102 goto error;
103 }
104
c719eabb
FD
105 plaintext = ctf_metadata_decoder_get_text(decoder);
106 g_metadata_text = g_string_new(NULL);
107
108 if (!g_metadata_text) {
109 goto error;
110 }
111
112 if (strncmp(plaintext, METADATA_TEXT_SIG, sizeof(METADATA_TEXT_SIG) - 1) != 0) {
113 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
114 g_string_append(g_metadata_text, " */\n\n");
115 }
116
117 g_string_append(g_metadata_text, plaintext);
118
119 ret = bt_value_map_insert_string_entry(result, "text", g_metadata_text->str);
4164020e 120 if (ret) {
0f5c5d5c 121 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot insert metadata text into query result.");
4164020e
SM
122 goto error;
123 }
124
125 ret = bt_value_map_insert_bool_entry(result, "is-packetized", is_packetized);
126 if (ret) {
0f5c5d5c
SM
127 BT_CPPLOGE_APPEND_CAUSE_SPEC(
128 logger, "Cannot insert \"is-packetized\" attribute into query result.");
4164020e
SM
129 goto error;
130 }
131
132 goto end;
04c0ba87
JG
133
134error:
4164020e
SM
135 BT_VALUE_PUT_REF_AND_RESET(result);
136 result = NULL;
c7eee084 137
4164020e
SM
138 if (status >= 0) {
139 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
140 }
04c0ba87
JG
141
142end:
c719eabb
FD
143 if (g_metadata_text) {
144 g_string_free(g_metadata_text, TRUE);
145 }
4164020e
SM
146 ctf_metadata_decoder_destroy(decoder);
147
148 if (metadata_fp) {
149 ret = fclose(metadata_fp);
150 if (ret) {
0f5c5d5c
SM
151 BT_CPPLOGE_ERRNO_SPEC(logger, "Cannot close metadata file stream", ": path=\"{}\"",
152 path);
4164020e
SM
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 252static int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info,
0f5c5d5c 253 const bt2c::Logger& logger)
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;
0f5c5d5c
SM
265 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Trace has no streams: trace-path={}",
266 trace->path->str);
4164020e
SM
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
4164020e 299bt_component_class_query_method_status
0f5c5d5c 300trace_infos_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
97ade20b 301{
4164020e
SM
302 struct ctf_fs_component *ctf_fs = NULL;
303 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
4164020e
SM
304 bt_value *result = NULL;
305 const bt_value *inputs_value = NULL;
306 const bt_value *trace_name_value;
307 int ret = 0;
308 bt_value *trace_info = NULL;
309 bt_value_array_append_element_status append_status;
310
311 BT_ASSERT(params);
312
313 if (!bt_value_is_map(params)) {
0f5c5d5c 314 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
4164020e
SM
315 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
316 goto error;
317 }
318
0f5c5d5c 319 ctf_fs = ctf_fs_component_create(logger);
4164020e
SM
320 if (!ctf_fs) {
321 goto error;
322 }
323
0f5c5d5c 324 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs)) {
4164020e
SM
325 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
326 goto error;
327 }
328
0f5c5d5c 329 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs, inputs_value, trace_name_value, NULL)) {
4164020e
SM
330 goto error;
331 }
332
333 result = bt_value_array_create();
334 if (!result) {
335 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
336 goto error;
337 }
338
339 append_status = bt_value_array_append_empty_map_element(result, &trace_info);
340 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
0f5c5d5c 341 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create trace info map.");
4164020e
SM
342 goto error;
343 }
344
0f5c5d5c 345 ret = populate_trace_info(ctf_fs->trace, trace_info, logger);
4164020e
SM
346 if (ret) {
347 goto error;
348 }
349
350 goto end;
97ade20b
JG
351
352error:
4164020e 353 BT_VALUE_PUT_REF_AND_RESET(result);
c7eee084 354
4164020e
SM
355 if (status >= 0) {
356 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
357 }
c7eee084 358
97ade20b 359end:
4164020e
SM
360 if (ctf_fs) {
361 ctf_fs_destroy(ctf_fs);
362 ctf_fs = NULL;
363 }
d94d92ac 364
4164020e
SM
365 *user_result = result;
366 return status;
04c0ba87 367}
73760435 368
4164020e 369bt_component_class_query_method_status
0f5c5d5c 370support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
73760435 371{
4164020e
SM
372 const bt_value *input_type_value;
373 const char *input_type;
374 bt_component_class_query_method_status status;
375 bt_value_map_insert_entry_status insert_entry_status;
376 double weight = 0;
377 gchar *metadata_path = NULL;
378 bt_value *result = NULL;
379 struct ctf_metadata_decoder *metadata_decoder = NULL;
380 FILE *metadata_file = NULL;
381 char uuid_str[BT_UUID_STR_LEN + 1];
382 bool has_uuid = false;
383 const bt_value *input_value;
384 const char *input;
385
386 input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
387 BT_ASSERT(input_type_value);
388 BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING);
389 input_type = bt_value_string_get(input_type_value);
390
391 if (strcmp(input_type, "directory") != 0) {
392 goto create_result;
393 }
394
395 input_value = bt_value_map_borrow_entry_value_const(params, "input");
396 BT_ASSERT(input_value);
397 BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
398 input = bt_value_string_get(input_value);
399
400 metadata_path = g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL);
401 if (!metadata_path) {
402 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
403 goto end;
404 }
405
406 metadata_file = g_fopen(metadata_path, "rb");
407 if (metadata_file) {
4164020e
SM
408 enum ctf_metadata_decoder_status decoder_status;
409 bt_uuid_t uuid;
410
0f5c5d5c 411 ctf_metadata_decoder_config metadata_decoder_config {logger};
4164020e
SM
412
413 metadata_decoder = ctf_metadata_decoder_create(&metadata_decoder_config);
414 if (!metadata_decoder) {
415 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
416 goto end;
417 }
418
419 decoder_status = ctf_metadata_decoder_append_content(metadata_decoder, metadata_file);
420 if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) {
0f5c5d5c
SM
421 BT_CPPLOGW_SPEC(logger, "cannot append metadata content: metadata-decoder-status={}",
422 decoder_status);
4164020e
SM
423 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
424 goto end;
425 }
426
427 /*
428 * We were able to parse the metadata file, so we are
429 * confident it's a CTF trace.
430 */
431 weight = 0.75;
432
433 /* If the trace has a UUID, return the stringified UUID as the group. */
434 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder, uuid) == 0) {
435 bt_uuid_to_str(uuid, uuid_str);
436 has_uuid = true;
437 }
438 }
73760435 439
493917ba 440create_result:
4164020e
SM
441 result = bt_value_map_create();
442 if (!result) {
443 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
444 goto end;
445 }
446
447 insert_entry_status = bt_value_map_insert_real_entry(result, "weight", weight);
448 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
449 status = (bt_component_class_query_method_status) insert_entry_status;
450 goto end;
451 }
452
453 /* We are not supposed to have weight == 0 and a UUID. */
454 BT_ASSERT(weight > 0 || !has_uuid);
455
456 if (weight > 0 && has_uuid) {
457 insert_entry_status = bt_value_map_insert_string_entry(result, "group", uuid_str);
458 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
459 status = (bt_component_class_query_method_status) insert_entry_status;
460 goto end;
461 }
462 }
463
464 *user_result = result;
465 result = NULL;
466 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
73760435
SM
467
468end:
4164020e
SM
469 g_free(metadata_path);
470 bt_value_put_ref(result);
471 ctf_metadata_decoder_destroy(metadata_decoder);
73760435 472
4164020e 473 return status;
73760435 474}
This page took 0.138291 seconds and 4 git commands to generate.