ctf: remove unused fields from `babeltrace.trace-info` query
[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 "common/assert.h"
34 #include "metadata.h"
35 #include "../common/metadata/decoder.h"
36 #include "common/common.h"
37 #include "common/macros.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "fs.h"
40 #include "logging/comp-logging.h"
41
42 #define METADATA_TEXT_SIG "/* CTF 1.8"
43
44 struct range {
45 int64_t begin_ns;
46 int64_t end_ns;
47 bool set;
48 };
49
50 BT_HIDDEN
51 bt_component_class_query_method_status metadata_info_query(
52 bt_self_component_class_source *self_comp_class_src,
53 const bt_value *params, bt_logging_level log_level,
54 const bt_value **user_result)
55 {
56 bt_component_class_query_method_status status =
57 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
58 bt_self_component_class *self_comp_class =
59 bt_self_component_class_source_as_self_component_class(self_comp_class_src);
60 bt_value *result = NULL;
61 const bt_value *path_value = NULL;
62 char *metadata_text = NULL;
63 FILE *metadata_fp = NULL;
64 GString *g_metadata_text = NULL;
65 int ret;
66 int bo;
67 const char *path;
68 bool is_packetized;
69
70 result = bt_value_map_create();
71 if (!result) {
72 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
73 goto error;
74 }
75
76 BT_ASSERT(params);
77
78 if (!bt_value_is_map(params)) {
79 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
80 "Query parameters is not a map value object.");
81 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
82 goto error;
83 }
84
85 path_value = bt_value_map_borrow_entry_value_const(params, "path");
86 if (!path_value) {
87 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
88 "Mandatory `path` parameter missing");
89 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
90 goto error;
91 }
92
93 if (!bt_value_is_string(path_value)) {
94 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
95 "`path` parameter is required to be a string value");
96 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
97 goto error;
98 }
99
100 path = bt_value_string_get(path_value);
101
102 BT_ASSERT(path);
103 metadata_fp = ctf_fs_metadata_open_file(path);
104 if (!metadata_fp) {
105 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
106 "Cannot open trace metadata: path=\"%s\".", path);
107 goto error;
108 }
109
110 is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp,
111 &bo, log_level, NULL);
112
113 if (is_packetized) {
114 ret = ctf_metadata_decoder_packetized_file_stream_to_buf(
115 metadata_fp, &metadata_text, bo, NULL, NULL,
116 log_level, NULL);
117 if (ret) {
118 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
119 "Cannot decode packetized metadata file: path=\"%s\"",
120 path);
121 goto error;
122 }
123 } else {
124 long filesize;
125
126 ret = fseek(metadata_fp, 0, SEEK_END);
127 if (ret) {
128 BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class,
129 "Failed to seek to the end of the metadata file",
130 ": path=\"%s\"", path);
131 goto error;
132 }
133 filesize = ftell(metadata_fp);
134 if (filesize < 0) {
135 BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class,
136 "Failed to get the current position in the metadata file",
137 ": path=\"%s\"", path);
138 goto error;
139 }
140 rewind(metadata_fp);
141 metadata_text = malloc(filesize + 1);
142 if (!metadata_text) {
143 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
144 "Cannot allocate buffer for metadata text.");
145 goto error;
146 }
147
148 if (fread(metadata_text, filesize, 1, metadata_fp) != 1) {
149 BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class,
150 "Cannot read metadata file", ": path=\"%s\"",
151 path);
152 goto error;
153 }
154
155 metadata_text[filesize] = '\0';
156 }
157
158 g_metadata_text = g_string_new(NULL);
159 if (!g_metadata_text) {
160 goto error;
161 }
162
163 if (strncmp(metadata_text, METADATA_TEXT_SIG,
164 sizeof(METADATA_TEXT_SIG) - 1) != 0) {
165 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
166 g_string_append(g_metadata_text, " */\n\n");
167 }
168
169 g_string_append(g_metadata_text, metadata_text);
170
171 ret = bt_value_map_insert_string_entry(result, "text",
172 g_metadata_text->str);
173 if (ret) {
174 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
175 "Cannot insert metadata text into query result.");
176 goto error;
177 }
178
179 ret = bt_value_map_insert_bool_entry(result, "is-packetized",
180 is_packetized);
181 if (ret) {
182 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
183 "Cannot insert \"is-packetized\" attribute into query result.");
184 goto error;
185 }
186
187 goto end;
188
189 error:
190 BT_VALUE_PUT_REF_AND_RESET(result);
191 result = NULL;
192
193 if (status >= 0) {
194 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
195 }
196
197 end:
198 free(metadata_text);
199
200 if (g_metadata_text) {
201 g_string_free(g_metadata_text, TRUE);
202 }
203
204 if (metadata_fp) {
205 fclose(metadata_fp);
206 }
207
208 *user_result = result;
209 return status;
210 }
211
212 static
213 int add_range(bt_value *info, struct range *range,
214 const char *range_name)
215 {
216 int ret = 0;
217 bt_value_map_insert_entry_status status;
218 bt_value *range_map = NULL;
219
220 if (!range->set) {
221 /* Not an error. */
222 goto end;
223 }
224
225 range_map = bt_value_map_create();
226 if (!range_map) {
227 ret = -1;
228 goto end;
229 }
230
231 status = bt_value_map_insert_signed_integer_entry(range_map, "begin",
232 range->begin_ns);
233 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
234 ret = -1;
235 goto end;
236 }
237
238 status = bt_value_map_insert_signed_integer_entry(range_map, "end",
239 range->end_ns);
240 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
241 ret = -1;
242 goto end;
243 }
244
245 status = bt_value_map_insert_entry(info, range_name,
246 range_map);
247 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
248 ret = -1;
249 goto end;
250 }
251
252 end:
253 bt_value_put_ref(range_map);
254 return ret;
255 }
256
257 static
258 int populate_stream_info(struct ctf_fs_ds_file_group *group,
259 bt_value *group_info, struct range *stream_range)
260 {
261 int ret = 0;
262 bt_value_map_insert_entry_status insert_status;
263 struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry;
264 gchar *port_name = NULL;
265
266 /*
267 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
268 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
269 * the timestamp_begin of the first index entry and the timestamp_end
270 * of the last index entry.
271 */
272 BT_ASSERT(group->index);
273 BT_ASSERT(group->index->entries);
274 BT_ASSERT(group->index->entries->len > 0);
275
276 /* First entry. */
277 first_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
278 group->index->entries, 0);
279
280 /* Last entry. */
281 last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
282 group->index->entries, group->index->entries->len - 1);
283
284 stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns;
285 stream_range->end_ns = last_ds_index_entry->timestamp_end_ns;
286
287 /*
288 * If any of the begin and end timestamps is not set it means that
289 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
290 * in their packet context so we can't set the range.
291 */
292 stream_range->set = stream_range->begin_ns != UINT64_C(-1) &&
293 stream_range->end_ns != UINT64_C(-1);
294
295 ret = add_range(group_info, stream_range, "range-ns");
296 if (ret) {
297 goto end;
298 }
299
300 port_name = ctf_fs_make_port_name(group);
301 if (!port_name) {
302 ret = -1;
303 goto end;
304 }
305
306 insert_status = bt_value_map_insert_string_entry(group_info,
307 "port-name", port_name);
308 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
309 ret = -1;
310 goto end;
311 }
312
313 end:
314 g_free(port_name);
315 return ret;
316 }
317
318 static
319 int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info)
320 {
321 int ret = 0;
322 size_t group_idx;
323 bt_value_map_insert_entry_status insert_status;
324 bt_value_array_append_element_status append_status;
325 bt_value *file_groups = NULL;
326 struct range trace_intersection = {
327 .begin_ns = 0,
328 .end_ns = INT64_MAX,
329 .set = false,
330 };
331
332 BT_ASSERT(trace->ds_file_groups);
333 /* Add trace range info only if it contains streams. */
334 if (trace->ds_file_groups->len == 0) {
335 ret = -1;
336 goto end;
337 }
338
339 file_groups = bt_value_array_create();
340 if (!file_groups) {
341 goto end;
342 }
343
344 /* Find range of all stream groups, and of the trace. */
345 for (group_idx = 0; group_idx < trace->ds_file_groups->len;
346 group_idx++) {
347 bt_value *group_info;
348 struct range group_range = { .set = false };
349 struct ctf_fs_ds_file_group *group = g_ptr_array_index(
350 trace->ds_file_groups, group_idx);
351
352 group_info = bt_value_map_create();
353 if (!group_info) {
354 ret = -1;
355 goto end;
356 }
357
358 ret = populate_stream_info(group, group_info, &group_range);
359 if (ret) {
360 bt_value_put_ref(group_info);
361 goto end;
362 }
363
364 append_status = bt_value_array_append_element(file_groups,
365 group_info);
366 bt_value_put_ref(group_info);
367 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
368 goto end;
369 }
370
371 if (group_range.set) {
372 trace_intersection.begin_ns = MAX(trace_intersection.begin_ns,
373 group_range.begin_ns);
374 trace_intersection.end_ns = MIN(trace_intersection.end_ns,
375 group_range.end_ns);
376 trace_intersection.set = true;
377 }
378 }
379
380 if (trace_intersection.begin_ns < trace_intersection.end_ns) {
381 ret = add_range(trace_info, &trace_intersection,
382 "intersection-range-ns");
383 if (ret) {
384 goto end;
385 }
386 }
387
388 insert_status = bt_value_map_insert_entry(trace_info, "streams",
389 file_groups);
390 BT_VALUE_PUT_REF_AND_RESET(file_groups);
391 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
392 ret = -1;
393 goto end;
394 }
395
396 end:
397 bt_value_put_ref(file_groups);
398 return ret;
399 }
400
401 BT_HIDDEN
402 bt_component_class_query_method_status trace_info_query(
403 bt_self_component_class_source *self_comp_class_src,
404 const bt_value *params, bt_logging_level log_level,
405 const bt_value **user_result)
406 {
407 struct ctf_fs_component *ctf_fs = NULL;
408 bt_component_class_query_method_status status =
409 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
410 bt_self_component_class *self_comp_class =
411 bt_self_component_class_source_as_self_component_class(
412 self_comp_class_src);
413 bt_value *result = NULL;
414 const bt_value *inputs_value = NULL;
415 int ret = 0;
416 guint i;
417
418 BT_ASSERT(params);
419
420 if (!bt_value_is_map(params)) {
421 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
422 "Query parameters is not a map value object.");
423 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
424 goto error;
425 }
426
427 ctf_fs = ctf_fs_component_create(log_level, NULL);
428 if (!ctf_fs) {
429 goto error;
430 }
431
432 if (!read_src_fs_parameters(params, &inputs_value, ctf_fs, NULL,
433 self_comp_class)) {
434 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
435 goto error;
436 }
437
438 if (ctf_fs_component_create_ctf_fs_traces(ctf_fs, inputs_value, NULL,
439 self_comp_class)) {
440 goto error;
441 }
442
443 result = bt_value_array_create();
444 if (!result) {
445 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
446 goto error;
447 }
448
449 for (i = 0; i < ctf_fs->traces->len; i++) {
450 struct ctf_fs_trace *trace;
451 bt_value *trace_info;
452 bt_value_array_append_element_status append_status;
453
454 trace = g_ptr_array_index(ctf_fs->traces, i);
455 BT_ASSERT(trace);
456
457 trace_info = bt_value_map_create();
458 if (!trace_info) {
459 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class,
460 "Failed to create trace info map.");
461 goto error;
462 }
463
464 ret = populate_trace_info(trace, trace_info);
465 if (ret) {
466 bt_value_put_ref(trace_info);
467 goto error;
468 }
469
470 append_status = bt_value_array_append_element(result,
471 trace_info);
472 bt_value_put_ref(trace_info);
473 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
474 goto error;
475 }
476 }
477
478 goto end;
479
480 error:
481 BT_VALUE_PUT_REF_AND_RESET(result);
482 result = NULL;
483
484 if (status >= 0) {
485 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
486 }
487
488 end:
489 if (ctf_fs) {
490 ctf_fs_destroy(ctf_fs);
491 ctf_fs = NULL;
492 }
493
494 *user_result = result;
495 return status;
496 }
497
498 BT_HIDDEN
499 bt_component_class_query_method_status support_info_query(
500 bt_self_component_class_source *comp_class,
501 const bt_value *params, bt_logging_level log_level,
502 const bt_value **user_result)
503 {
504 const bt_value *input_type_value;
505 const char *input_type;
506 bt_component_class_query_method_status status;
507 double weight = 0;
508 gchar *metadata_path = NULL;
509 bt_value *result = NULL;
510
511 input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
512 BT_ASSERT(input_type_value);
513 BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING);
514 input_type = bt_value_string_get(input_type_value);
515
516 result = bt_value_map_create();
517 if (!result) {
518 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
519 goto end;
520 }
521
522 if (strcmp(input_type, "directory") == 0) {
523 const bt_value *input_value;
524 const char *path;
525
526 input_value = bt_value_map_borrow_entry_value_const(params, "input");
527 BT_ASSERT(input_value);
528 BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
529 path = bt_value_string_get(input_value);
530
531 metadata_path = g_build_filename(path, CTF_FS_METADATA_FILENAME, NULL);
532 if (!metadata_path) {
533 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
534 goto end;
535 }
536
537 /*
538 * If the metadata file exists in this directory, consider it to
539 * be a CTF trace.
540 */
541 if (g_file_test(metadata_path, G_FILE_TEST_EXISTS)) {
542 weight = 0.5;
543 }
544 }
545
546 if (bt_value_map_insert_real_entry(result, "weight", weight) != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
547 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
548 goto end;
549 }
550
551 /*
552 * Use the arbitrary constant string "ctf" as the group, such that all
553 * found ctf traces are passed to the same instance of src.ctf.fs.
554 */
555 if (bt_value_map_insert_string_entry(result, "group", "ctf") != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
556 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
557 goto end;
558 }
559
560 *user_result = result;
561 result = NULL;
562 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
563
564 end:
565 g_free(metadata_path);
566 bt_value_put_ref(result);
567
568 return status;
569 }
This page took 0.04046 seconds and 5 git commands to generate.