4 * Babeltrace CTF file system Reader Component queries
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/assert-internal.h>
31 #include "../common/metadata/decoder.h"
32 #include <babeltrace/common-internal.h>
33 #include <babeltrace/babeltrace-internal.h>
34 #include <babeltrace/babeltrace.h>
37 #define BT_LOG_TAG "PLUGIN-CTF-FS-QUERY-SRC"
40 #define METADATA_TEXT_SIG "/* CTF 1.8"
49 enum bt_query_status
metadata_info_query(
50 struct bt_self_component_class_source
*comp_class
,
51 struct bt_value
*params
, struct bt_value
**user_result
)
53 enum bt_query_status status
= BT_QUERY_STATUS_OK
;
54 struct bt_private_value
*result
= NULL
;
55 struct bt_value
*path_value
= NULL
;
56 char *metadata_text
= NULL
;
57 FILE *metadata_fp
= NULL
;
58 GString
*g_metadata_text
= NULL
;
64 result
= bt_private_value_map_create();
66 status
= BT_QUERY_STATUS_NOMEM
;
72 if (!bt_value_is_map(params
)) {
73 BT_LOGE_STR("Query parameters is not a map value object.");
74 status
= BT_QUERY_STATUS_INVALID_PARAMS
;
78 path_value
= bt_value_map_borrow_entry_value(params
, "path");
79 path
= bt_value_string_get(path_value
);
82 metadata_fp
= ctf_fs_metadata_open_file(path
);
84 BT_LOGE("Cannot open trace metadata: path=\"%s\".", path
);
88 is_packetized
= ctf_metadata_decoder_is_packetized(metadata_fp
,
92 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf(
93 metadata_fp
, &metadata_text
, bo
);
95 BT_LOGE("Cannot decode packetized metadata file: path=\"%s\"",
102 ret
= fseek(metadata_fp
, 0, SEEK_END
);
104 BT_LOGE_ERRNO("Failed to seek to the end of the metadata file",
105 ": path=\"%s\"", path
);
108 filesize
= ftell(metadata_fp
);
110 BT_LOGE_ERRNO("Failed to get the current position in the metadata file",
111 ": path=\"%s\"", path
);
115 metadata_text
= malloc(filesize
+ 1);
116 if (!metadata_text
) {
117 BT_LOGE_STR("Cannot allocate buffer for metadata text.");
121 if (fread(metadata_text
, filesize
, 1, metadata_fp
) != 1) {
122 BT_LOGE_ERRNO("Cannot read metadata file", ": path=\"%s\"",
127 metadata_text
[filesize
] = '\0';
130 g_metadata_text
= g_string_new(NULL
);
131 if (!g_metadata_text
) {
135 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
136 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
137 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
138 g_string_append(g_metadata_text
, " */\n\n");
141 g_string_append(g_metadata_text
, metadata_text
);
143 ret
= bt_private_value_map_insert_string_entry(result
, "text",
144 g_metadata_text
->str
);
146 BT_LOGE_STR("Cannot insert metadata text into query result.");
150 ret
= bt_private_value_map_insert_bool_entry(result
, "is-packetized",
153 BT_LOGE_STR("Cannot insert \"is-packetized\" attribute into query result.");
160 BT_OBJECT_PUT_REF_AND_RESET(result
);
164 status
= BT_QUERY_STATUS_ERROR
;
170 if (g_metadata_text
) {
171 g_string_free(g_metadata_text
, TRUE
);
178 *user_result
= bt_private_value_as_value(result
);
183 int add_range(struct bt_private_value
*info
, struct range
*range
,
184 const char *range_name
)
187 enum bt_value_status status
;
188 struct bt_private_value
*range_map
= NULL
;
195 range_map
= bt_private_value_map_create();
201 status
= bt_private_value_map_insert_integer_entry(range_map
, "begin",
203 if (status
!= BT_VALUE_STATUS_OK
) {
208 status
= bt_private_value_map_insert_integer_entry(range_map
, "end",
210 if (status
!= BT_VALUE_STATUS_OK
) {
215 status
= bt_private_value_map_insert_entry(info
, range_name
,
216 bt_private_value_as_value(range_map
));
217 if (status
!= BT_VALUE_STATUS_OK
) {
223 bt_object_put_ref(range_map
);
228 int add_stream_ids(struct bt_private_value
*info
,
229 struct bt_stream
*stream
)
232 int64_t stream_class_id
, stream_instance_id
;
233 enum bt_value_status status
;
234 struct bt_stream_class
*stream_class
= NULL
;
236 stream_instance_id
= bt_stream_get_id(stream
);
237 if (stream_instance_id
!= -1) {
238 status
= bt_private_value_map_insert_integer_entry(info
, "id",
240 if (status
!= BT_VALUE_STATUS_OK
) {
246 stream_class
= bt_stream_borrow_class(stream
);
252 stream_class_id
= bt_stream_class_get_id(stream_class
);
253 if (stream_class_id
== -1) {
258 status
= bt_private_value_map_insert_integer_entry(info
, "class-id", stream_class_id
);
259 if (status
!= BT_VALUE_STATUS_OK
) {
269 int populate_stream_info(struct ctf_fs_ds_file_group
*group
,
270 struct bt_private_value
*group_info
,
271 struct range
*stream_range
)
275 enum bt_value_status status
;
276 struct bt_private_value
*file_paths
;
278 stream_range
->begin_ns
= INT64_MAX
;
279 stream_range
->end_ns
= 0;
281 file_paths
= bt_private_value_array_create();
287 for (file_idx
= 0; file_idx
< group
->ds_file_infos
->len
; file_idx
++) {
288 int64_t file_begin_epoch
, file_end_epoch
;
289 struct ctf_fs_ds_file_info
*info
=
290 g_ptr_array_index(group
->ds_file_infos
,
293 if (!info
->index
|| info
->index
->entries
->len
== 0) {
294 BT_LOGW("Cannot determine range of unindexed stream file \'%s\'",
300 status
= bt_private_value_array_append_string_element(file_paths
,
302 if (status
!= BT_VALUE_STATUS_OK
) {
308 * file range is from timestamp_begin of the first entry to the
309 * timestamp_end of the last entry.
311 file_begin_epoch
= ((struct ctf_fs_ds_index_entry
*) &g_array_index(info
->index
->entries
,
312 struct ctf_fs_ds_index_entry
, 0))->timestamp_begin_ns
;
313 file_end_epoch
= ((struct ctf_fs_ds_index_entry
*) &g_array_index(info
->index
->entries
,
314 struct ctf_fs_ds_index_entry
, info
->index
->entries
->len
- 1))->timestamp_end_ns
;
316 stream_range
->begin_ns
= min(stream_range
->begin_ns
, file_begin_epoch
);
317 stream_range
->end_ns
= max(stream_range
->end_ns
, file_end_epoch
);
318 stream_range
->set
= true;
321 if (stream_range
->set
) {
322 ret
= add_range(group_info
, stream_range
, "range-ns");
328 status
= bt_private_value_map_insert_entry(group_info
, "paths",
329 bt_private_value_as_value(file_paths
));
330 if (status
!= BT_VALUE_STATUS_OK
) {
335 ret
= add_stream_ids(group_info
,
336 bt_private_stream_as_stream(group
->stream
));
341 bt_object_put_ref(file_paths
);
346 int populate_trace_info(const char *trace_path
, const char *trace_name
,
347 struct bt_private_value
*trace_info
)
351 struct ctf_fs_trace
*trace
= NULL
;
352 enum bt_value_status status
;
353 struct bt_private_value
*file_groups
;
354 struct range trace_range
= {
355 .begin_ns
= INT64_MAX
,
359 struct range trace_intersection
= {
365 file_groups
= bt_private_value_array_create();
370 status
= bt_private_value_map_insert_string_entry(trace_info
, "name",
372 if (status
!= BT_VALUE_STATUS_OK
) {
376 status
= bt_private_value_map_insert_string_entry(trace_info
, "path",
378 if (status
!= BT_VALUE_STATUS_OK
) {
383 trace
= ctf_fs_trace_create(trace_path
, trace_name
, NULL
);
385 BT_LOGE("Failed to create fs trace at \'%s\'", trace_path
);
390 BT_ASSERT(trace
->ds_file_groups
);
391 /* Add trace range info only if it contains streams. */
392 if (trace
->ds_file_groups
->len
== 0) {
397 /* Find range of all stream groups, and of the trace. */
398 for (group_idx
= 0; group_idx
< trace
->ds_file_groups
->len
;
400 struct bt_private_value
*group_info
;
401 struct range group_range
= { .set
= false };
402 struct ctf_fs_ds_file_group
*group
= g_ptr_array_index(
403 trace
->ds_file_groups
, group_idx
);
405 group_info
= bt_private_value_map_create();
411 ret
= populate_stream_info(group
, group_info
, &group_range
);
413 bt_object_put_ref(group_info
);
417 if (group_range
.set
) {
418 trace_range
.begin_ns
= min(trace_range
.begin_ns
,
419 group_range
.begin_ns
);
420 trace_range
.end_ns
= max(trace_range
.end_ns
,
422 trace_range
.set
= true;
424 trace_intersection
.begin_ns
= max(trace_intersection
.begin_ns
,
425 group_range
.begin_ns
);
426 trace_intersection
.end_ns
= min(trace_intersection
.end_ns
,
428 trace_intersection
.set
= true;
429 status
= bt_private_value_array_append_element(
431 bt_private_value_as_value(group_info
));
432 bt_object_put_ref(group_info
);
433 if (status
!= BT_VALUE_STATUS_OK
) {
439 ret
= add_range(trace_info
, &trace_range
, "range-ns");
444 if (trace_intersection
.begin_ns
< trace_intersection
.end_ns
) {
445 ret
= add_range(trace_info
, &trace_intersection
,
446 "intersection-range-ns");
452 status
= bt_private_value_map_insert_entry(trace_info
, "streams",
453 bt_private_value_as_value(file_groups
));
454 BT_OBJECT_PUT_REF_AND_RESET(file_groups
);
455 if (status
!= BT_VALUE_STATUS_OK
) {
461 bt_object_put_ref(file_groups
);
462 ctf_fs_trace_destroy(trace
);
467 enum bt_query_status
trace_info_query(
468 struct bt_self_component_class_source
*comp_class
,
469 struct bt_value
*params
, struct bt_value
**user_result
)
471 enum bt_query_status status
= BT_QUERY_STATUS_OK
;
472 struct bt_private_value
*result
= NULL
;
473 struct bt_value
*path_value
= NULL
;
475 const char *path
= NULL
;
476 GList
*trace_paths
= NULL
;
477 GList
*trace_names
= NULL
;
478 GList
*tp_node
= NULL
;
479 GList
*tn_node
= NULL
;
480 GString
*normalized_path
= NULL
;
484 if (!bt_value_is_map(params
)) {
485 BT_LOGE("Query parameters is not a map value object.");
486 status
= BT_QUERY_STATUS_INVALID_PARAMS
;
490 path_value
= bt_value_map_borrow_entry_value(params
, "path");
491 path
= bt_value_string_get(path_value
);
493 normalized_path
= bt_common_normalize_path(path
, NULL
);
494 if (!normalized_path
) {
495 BT_LOGE("Failed to normalize path: `%s`.", path
);
500 ret
= ctf_fs_find_traces(&trace_paths
, normalized_path
->str
);
505 trace_names
= ctf_fs_create_trace_names(trace_paths
,
506 normalized_path
->str
);
508 BT_LOGE("Cannot create trace names from trace paths.");
512 result
= bt_private_value_array_create();
514 status
= BT_QUERY_STATUS_NOMEM
;
518 /* Iterates over both trace paths and names simultaneously. */
519 for (tp_node
= trace_paths
, tn_node
= trace_names
; tp_node
;
520 tp_node
= g_list_next(tp_node
),
521 tn_node
= g_list_next(tn_node
)) {
522 GString
*trace_path
= tp_node
->data
;
523 GString
*trace_name
= tn_node
->data
;
524 enum bt_value_status status
;
525 struct bt_private_value
*trace_info
;
527 trace_info
= bt_private_value_map_create();
529 BT_LOGE("Failed to create trace info map.");
533 ret
= populate_trace_info(trace_path
->str
, trace_name
->str
,
536 bt_object_put_ref(trace_info
);
540 status
= bt_private_value_array_append_element(result
,
541 bt_private_value_as_value(trace_info
));
542 bt_object_put_ref(trace_info
);
543 if (status
!= BT_VALUE_STATUS_OK
) {
551 BT_OBJECT_PUT_REF_AND_RESET(result
);
555 status
= BT_QUERY_STATUS_ERROR
;
559 if (normalized_path
) {
560 g_string_free(normalized_path
, TRUE
);
563 for (tp_node
= trace_paths
; tp_node
; tp_node
= g_list_next(tp_node
)) {
565 g_string_free(tp_node
->data
, TRUE
);
568 g_list_free(trace_paths
);
571 for (tn_node
= trace_names
; tn_node
; tn_node
= g_list_next(tn_node
)) {
573 g_string_free(tn_node
->data
, TRUE
);
576 g_list_free(trace_names
);
579 *user_result
= bt_private_value_as_value(result
);