src.ctf.fs: use unique_ptr to manage ctf_fs_component lifetime
[babeltrace.git] / src / plugins / ctf / fs-src / query.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Babeltrace CTF file system Reader Component queries
7 */
8
9 #include <glib.h>
10 #include <glib/gstdio.h>
11 #include <sys/types.h>
12
13 #include <babeltrace2/babeltrace.h>
14
15 #include "../common/src/metadata/tsdl/decoder.hpp"
16 #include "fs.hpp"
17 #include "query.hpp"
18
19 #define METADATA_TEXT_SIG "/* CTF 1.8"
20
21 struct range
22 {
23 int64_t begin_ns = 0;
24 int64_t end_ns = 0;
25 bool set = false;
26 };
27
28 bt_component_class_query_method_status metadata_info_query(const bt_value *params,
29 const bt2c::Logger& logger,
30 const bt_value **user_result)
31 {
32 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
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;
41 ctf_metadata_decoder_config decoder_cfg {logger};
42 enum ctf_metadata_decoder_status decoder_status;
43 GString *g_metadata_text = NULL;
44 const char *plaintext;
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)) {
55 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
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) {
62 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Mandatory `path` parameter missing");
63 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
64 goto error;
65 }
66
67 if (!bt_value_is_string(path_value)) {
68 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`path` parameter is required to be a string value");
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);
76 metadata_fp = ctf_fs_metadata_open_file(path, logger);
77 if (!metadata_fp) {
78 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot open trace metadata: path=\"{}\".", path);
79 goto error;
80 }
81
82 ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized, &bo, logger);
83 if (ret) {
84 BT_CPPLOGE_APPEND_CAUSE_SPEC(
85 logger, "Cannot check whether or not the metadata stream is packetized: path=\"{}\".",
86 path);
87 goto error;
88 }
89
90 decoder_cfg.keep_plain_text = true;
91 decoder = ctf_metadata_decoder_create(&decoder_cfg);
92 if (!decoder) {
93 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot create metadata decoder: path=\"{}\".", path);
94 goto error;
95 }
96
97 rewind(metadata_fp);
98 decoder_status = ctf_metadata_decoder_append_content(decoder, metadata_fp);
99 if (decoder_status) {
100 BT_CPPLOGE_APPEND_CAUSE_SPEC(
101 logger, "Cannot update metadata decoder's content: path=\"{}\".", path);
102 goto error;
103 }
104
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);
120 if (ret) {
121 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot insert metadata text into query result.");
122 goto error;
123 }
124
125 ret = bt_value_map_insert_bool_entry(result, "is-packetized", is_packetized);
126 if (ret) {
127 BT_CPPLOGE_APPEND_CAUSE_SPEC(
128 logger, "Cannot insert \"is-packetized\" attribute into query result.");
129 goto error;
130 }
131
132 goto end;
133
134 error:
135 BT_VALUE_PUT_REF_AND_RESET(result);
136 result = NULL;
137
138 if (status >= 0) {
139 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
140 }
141
142 end:
143 if (g_metadata_text) {
144 g_string_free(g_metadata_text, TRUE);
145 }
146 ctf_metadata_decoder_destroy(decoder);
147
148 if (metadata_fp) {
149 ret = fclose(metadata_fp);
150 if (ret) {
151 BT_CPPLOGE_ERRNO_SPEC(logger, "Cannot close metadata file stream", ": path=\"{}\"",
152 path);
153 }
154 }
155
156 *user_result = result;
157 return status;
158 }
159
160 static int add_range(bt_value *info, struct range *range, const char *range_name)
161 {
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 }
188
189 end:
190 return ret;
191 }
192
193 static int populate_stream_info(struct ctf_fs_ds_file_group *group, bt_value *group_info,
194 struct range *stream_range)
195 {
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 }
246
247 end:
248 g_free(port_name);
249 return ret;
250 }
251
252 static int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info,
253 const bt2c::Logger& logger)
254 {
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_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Trace has no streams: trace-path={}",
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 }
294
295 end:
296 return ret;
297 }
298
299 bt_component_class_query_method_status
300 trace_infos_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
301 {
302 ctf_fs_component::UP ctf_fs;
303 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
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)) {
314 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
315 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
316 goto error;
317 }
318
319 ctf_fs = ctf_fs_component_create(logger);
320 if (!ctf_fs) {
321 goto error;
322 }
323
324 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs.get())) {
325 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
326 goto error;
327 }
328
329 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), inputs_value, trace_name_value, NULL)) {
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) {
341 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create trace info map.");
342 goto error;
343 }
344
345 ret = populate_trace_info(ctf_fs->trace, trace_info, logger);
346 if (ret) {
347 goto error;
348 }
349
350 goto end;
351
352 error:
353 BT_VALUE_PUT_REF_AND_RESET(result);
354
355 if (status >= 0) {
356 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
357 }
358
359 end:
360 *user_result = result;
361 return status;
362 }
363
364 bt_component_class_query_method_status
365 support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
366 {
367 const bt_value *input_type_value;
368 const char *input_type;
369 bt_component_class_query_method_status status;
370 bt_value_map_insert_entry_status insert_entry_status;
371 double weight = 0;
372 gchar *metadata_path = NULL;
373 bt_value *result = NULL;
374 struct ctf_metadata_decoder *metadata_decoder = NULL;
375 FILE *metadata_file = NULL;
376 char uuid_str[BT_UUID_STR_LEN + 1];
377 bool has_uuid = false;
378 const bt_value *input_value;
379 const char *input;
380
381 input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
382 BT_ASSERT(input_type_value);
383 BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING);
384 input_type = bt_value_string_get(input_type_value);
385
386 if (strcmp(input_type, "directory") != 0) {
387 goto create_result;
388 }
389
390 input_value = bt_value_map_borrow_entry_value_const(params, "input");
391 BT_ASSERT(input_value);
392 BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
393 input = bt_value_string_get(input_value);
394
395 metadata_path = g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL);
396 if (!metadata_path) {
397 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
398 goto end;
399 }
400
401 metadata_file = g_fopen(metadata_path, "rb");
402 if (metadata_file) {
403 enum ctf_metadata_decoder_status decoder_status;
404 bt_uuid_t uuid;
405
406 ctf_metadata_decoder_config metadata_decoder_config {logger};
407
408 metadata_decoder = ctf_metadata_decoder_create(&metadata_decoder_config);
409 if (!metadata_decoder) {
410 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
411 goto end;
412 }
413
414 decoder_status = ctf_metadata_decoder_append_content(metadata_decoder, metadata_file);
415 if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) {
416 BT_CPPLOGW_SPEC(logger, "cannot append metadata content: metadata-decoder-status={}",
417 decoder_status);
418 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
419 goto end;
420 }
421
422 /*
423 * We were able to parse the metadata file, so we are
424 * confident it's a CTF trace.
425 */
426 weight = 0.75;
427
428 /* If the trace has a UUID, return the stringified UUID as the group. */
429 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder, uuid) == 0) {
430 bt_uuid_to_str(uuid, uuid_str);
431 has_uuid = true;
432 }
433 }
434
435 create_result:
436 result = bt_value_map_create();
437 if (!result) {
438 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
439 goto end;
440 }
441
442 insert_entry_status = bt_value_map_insert_real_entry(result, "weight", weight);
443 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
444 status = (bt_component_class_query_method_status) insert_entry_status;
445 goto end;
446 }
447
448 /* We are not supposed to have weight == 0 and a UUID. */
449 BT_ASSERT(weight > 0 || !has_uuid);
450
451 if (weight > 0 && has_uuid) {
452 insert_entry_status = bt_value_map_insert_string_entry(result, "group", uuid_str);
453 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
454 status = (bt_component_class_query_method_status) insert_entry_status;
455 goto end;
456 }
457 }
458
459 *user_result = result;
460 result = NULL;
461 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
462
463 end:
464 g_free(metadata_path);
465 bt_value_put_ref(result);
466 ctf_metadata_decoder_destroy(metadata_decoder);
467
468 return status;
469 }
This page took 0.042504 seconds and 4 git commands to generate.