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