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