95453ada5595eb4db7a10228bfcf95bc6135b10f
[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 "cpp-common/bt2c/libc-up.hpp"
16
17 #include "../common/src/metadata/tsdl/decoder.hpp"
18 #include "fs.hpp"
19 #include "query.hpp"
20
21 #define METADATA_TEXT_SIG "/* CTF 1.8"
22
23 struct range
24 {
25 int64_t begin_ns = 0;
26 int64_t end_ns = 0;
27 bool set = false;
28 };
29
30 bt_component_class_query_method_status metadata_info_query(const bt_value *params,
31 const bt2c::Logger& logger,
32 const bt_value **user_result)
33 {
34 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
35 bt_value *result = NULL;
36 const bt_value *path_value = NULL;
37 bt2c::FileUP metadata_fp;
38 int ret;
39 int bo;
40 const char *path;
41 bool is_packetized;
42 struct ctf_metadata_decoder *decoder = NULL;
43 ctf_metadata_decoder_config decoder_cfg {logger};
44 enum ctf_metadata_decoder_status decoder_status;
45 GString *g_metadata_text = NULL;
46 const char *plaintext;
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)) {
57 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
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) {
64 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Mandatory `path` parameter missing");
65 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
66 goto error;
67 }
68
69 if (!bt_value_is_string(path_value)) {
70 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`path` parameter is required to be a string value");
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);
78 metadata_fp.reset(ctf_fs_metadata_open_file(path, logger));
79 if (!metadata_fp) {
80 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot open trace metadata: path=\"{}\".", path);
81 goto error;
82 }
83
84 ret = ctf_metadata_decoder_is_packetized(metadata_fp.get(), &is_packetized, &bo, logger);
85 if (ret) {
86 BT_CPPLOGE_APPEND_CAUSE_SPEC(
87 logger, "Cannot check whether or not the metadata stream is packetized: path=\"{}\".",
88 path);
89 goto error;
90 }
91
92 decoder_cfg.keep_plain_text = true;
93 decoder = ctf_metadata_decoder_create(&decoder_cfg);
94 if (!decoder) {
95 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot create metadata decoder: path=\"{}\".", path);
96 goto error;
97 }
98
99 rewind(metadata_fp.get());
100 decoder_status = ctf_metadata_decoder_append_content(decoder, metadata_fp.get());
101 if (decoder_status) {
102 BT_CPPLOGE_APPEND_CAUSE_SPEC(
103 logger, "Cannot update metadata decoder's content: path=\"{}\".", path);
104 goto error;
105 }
106
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);
122 if (ret) {
123 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot insert metadata text into query result.");
124 goto error;
125 }
126
127 ret = bt_value_map_insert_bool_entry(result, "is-packetized", is_packetized);
128 if (ret) {
129 BT_CPPLOGE_APPEND_CAUSE_SPEC(
130 logger, "Cannot insert \"is-packetized\" attribute into query result.");
131 goto error;
132 }
133
134 goto end;
135
136 error:
137 BT_VALUE_PUT_REF_AND_RESET(result);
138 result = NULL;
139
140 if (status >= 0) {
141 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
142 }
143
144 end:
145 if (g_metadata_text) {
146 g_string_free(g_metadata_text, TRUE);
147 }
148 ctf_metadata_decoder_destroy(decoder);
149
150 *user_result = result;
151 return status;
152 }
153
154 static int add_range(bt_value *info, struct range *range, const char *range_name)
155 {
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 }
182
183 end:
184 return ret;
185 }
186
187 static int populate_stream_info(struct ctf_fs_ds_file_group *group, bt_value *group_info,
188 struct range *stream_range)
189 {
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;
193 bt2c::GCharUP port_name;
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
235 insert_status = bt_value_map_insert_string_entry(group_info, "port-name", port_name.get());
236 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
237 ret = -1;
238 goto end;
239 }
240
241 end:
242 return ret;
243 }
244
245 static int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info,
246 const bt2c::Logger& logger)
247 {
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;
258 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Trace has no streams: trace-path={}",
259 trace->path->str);
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 }
287
288 end:
289 return ret;
290 }
291
292 bt_component_class_query_method_status
293 trace_infos_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
294 {
295 ctf_fs_component::UP ctf_fs;
296 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
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)) {
307 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
308 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
309 goto error;
310 }
311
312 ctf_fs = ctf_fs_component_create(logger);
313 if (!ctf_fs) {
314 goto error;
315 }
316
317 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs.get())) {
318 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
319 goto error;
320 }
321
322 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), inputs_value, trace_name_value, NULL)) {
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) {
334 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create trace info map.");
335 goto error;
336 }
337
338 ret = populate_trace_info(ctf_fs->trace, trace_info, logger);
339 if (ret) {
340 goto error;
341 }
342
343 goto end;
344
345 error:
346 BT_VALUE_PUT_REF_AND_RESET(result);
347
348 if (status >= 0) {
349 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
350 }
351
352 end:
353 *user_result = result;
354 return status;
355 }
356
357 bt_component_class_query_method_status
358 support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
359 {
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;
365 bt2c::GCharUP metadata_path;
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
388 metadata_path.reset(g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL));
389 if (!metadata_path) {
390 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
391 goto end;
392 }
393
394 metadata_file = g_fopen(metadata_path.get(), "rb");
395 if (metadata_file) {
396 enum ctf_metadata_decoder_status decoder_status;
397 bt_uuid_t uuid;
398
399 ctf_metadata_decoder_config metadata_decoder_config {logger};
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) {
409 BT_CPPLOGW_SPEC(logger, "cannot append metadata content: metadata-decoder-status={}",
410 decoder_status);
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 }
427
428 create_result:
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;
455
456 end:
457 bt_value_put_ref(result);
458 ctf_metadata_decoder_destroy(metadata_decoder);
459
460 return status;
461 }
This page took 0.046782 seconds and 3 git commands to generate.