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