488675038e7c925f876f748470d61421ff820197
[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 bt2c::GCharUP port_name;
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.get());
242 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
243 ret = -1;
244 goto end;
245 }
246
247 end:
248 return ret;
249 }
250
251 static int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info,
252 const bt2c::Logger& logger)
253 {
254 int ret = 0;
255 size_t group_idx;
256 bt_value_map_insert_entry_status insert_status;
257 bt_value_array_append_element_status append_status;
258 bt_value *file_groups = NULL;
259
260 BT_ASSERT(trace->ds_file_groups);
261 /* Add trace range info only if it contains streams. */
262 if (trace->ds_file_groups->len == 0) {
263 ret = -1;
264 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Trace has no streams: trace-path={}",
265 trace->path->str);
266 goto end;
267 }
268
269 insert_status = bt_value_map_insert_empty_array_entry(trace_info, "stream-infos", &file_groups);
270 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
271 ret = -1;
272 goto end;
273 }
274
275 /* Find range of all stream groups, and of the trace. */
276 for (group_idx = 0; group_idx < trace->ds_file_groups->len; group_idx++) {
277 bt_value *group_info;
278 range group_range;
279 ctf_fs_ds_file_group *group =
280 (ctf_fs_ds_file_group *) g_ptr_array_index(trace->ds_file_groups, group_idx);
281
282 append_status = bt_value_array_append_empty_map_element(file_groups, &group_info);
283 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
284 ret = -1;
285 goto end;
286 }
287
288 ret = populate_stream_info(group, group_info, &group_range);
289 if (ret) {
290 goto end;
291 }
292 }
293
294 end:
295 return ret;
296 }
297
298 bt_component_class_query_method_status
299 trace_infos_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
300 {
301 ctf_fs_component::UP ctf_fs;
302 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
303 bt_value *result = NULL;
304 const bt_value *inputs_value = NULL;
305 const bt_value *trace_name_value;
306 int ret = 0;
307 bt_value *trace_info = NULL;
308 bt_value_array_append_element_status append_status;
309
310 BT_ASSERT(params);
311
312 if (!bt_value_is_map(params)) {
313 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
314 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
315 goto error;
316 }
317
318 ctf_fs = ctf_fs_component_create(logger);
319 if (!ctf_fs) {
320 goto error;
321 }
322
323 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs.get())) {
324 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
325 goto error;
326 }
327
328 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), inputs_value, trace_name_value, NULL)) {
329 goto error;
330 }
331
332 result = bt_value_array_create();
333 if (!result) {
334 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
335 goto error;
336 }
337
338 append_status = bt_value_array_append_empty_map_element(result, &trace_info);
339 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
340 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create trace info map.");
341 goto error;
342 }
343
344 ret = populate_trace_info(ctf_fs->trace, trace_info, logger);
345 if (ret) {
346 goto error;
347 }
348
349 goto end;
350
351 error:
352 BT_VALUE_PUT_REF_AND_RESET(result);
353
354 if (status >= 0) {
355 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
356 }
357
358 end:
359 *user_result = result;
360 return status;
361 }
362
363 bt_component_class_query_method_status
364 support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
365 {
366 const bt_value *input_type_value;
367 const char *input_type;
368 bt_component_class_query_method_status status;
369 bt_value_map_insert_entry_status insert_entry_status;
370 double weight = 0;
371 gchar *metadata_path = NULL;
372 bt_value *result = NULL;
373 struct ctf_metadata_decoder *metadata_decoder = NULL;
374 FILE *metadata_file = NULL;
375 char uuid_str[BT_UUID_STR_LEN + 1];
376 bool has_uuid = false;
377 const bt_value *input_value;
378 const char *input;
379
380 input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
381 BT_ASSERT(input_type_value);
382 BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING);
383 input_type = bt_value_string_get(input_type_value);
384
385 if (strcmp(input_type, "directory") != 0) {
386 goto create_result;
387 }
388
389 input_value = bt_value_map_borrow_entry_value_const(params, "input");
390 BT_ASSERT(input_value);
391 BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
392 input = bt_value_string_get(input_value);
393
394 metadata_path = g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL);
395 if (!metadata_path) {
396 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
397 goto end;
398 }
399
400 metadata_file = g_fopen(metadata_path, "rb");
401 if (metadata_file) {
402 enum ctf_metadata_decoder_status decoder_status;
403 bt_uuid_t uuid;
404
405 ctf_metadata_decoder_config metadata_decoder_config {logger};
406
407 metadata_decoder = ctf_metadata_decoder_create(&metadata_decoder_config);
408 if (!metadata_decoder) {
409 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
410 goto end;
411 }
412
413 decoder_status = ctf_metadata_decoder_append_content(metadata_decoder, metadata_file);
414 if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) {
415 BT_CPPLOGW_SPEC(logger, "cannot append metadata content: metadata-decoder-status={}",
416 decoder_status);
417 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
418 goto end;
419 }
420
421 /*
422 * We were able to parse the metadata file, so we are
423 * confident it's a CTF trace.
424 */
425 weight = 0.75;
426
427 /* If the trace has a UUID, return the stringified UUID as the group. */
428 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder, uuid) == 0) {
429 bt_uuid_to_str(uuid, uuid_str);
430 has_uuid = true;
431 }
432 }
433
434 create_result:
435 result = bt_value_map_create();
436 if (!result) {
437 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
438 goto end;
439 }
440
441 insert_entry_status = bt_value_map_insert_real_entry(result, "weight", weight);
442 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
443 status = (bt_component_class_query_method_status) insert_entry_status;
444 goto end;
445 }
446
447 /* We are not supposed to have weight == 0 and a UUID. */
448 BT_ASSERT(weight > 0 || !has_uuid);
449
450 if (weight > 0 && has_uuid) {
451 insert_entry_status = bt_value_map_insert_string_entry(result, "group", uuid_str);
452 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
453 status = (bt_component_class_query_method_status) insert_entry_status;
454 goto end;
455 }
456 }
457
458 *user_result = result;
459 result = NULL;
460 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
461
462 end:
463 g_free(metadata_path);
464 bt_value_put_ref(result);
465 ctf_metadata_decoder_destroy(metadata_decoder);
466
467 return status;
468 }
This page took 0.03743 seconds and 3 git commands to generate.