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