2 * SPDX-License-Identifier: MIT
4 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Babeltrace CTF file system Reader Component queries
9 #define BT_CLOG_CFG logCfg
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY"
15 #include <glib/gstdio.h>
17 #include <sys/types.h>
19 #include "common/assert.h"
20 #include "metadata.hpp"
21 #include "../common/src/metadata/tsdl/decoder.hpp"
22 #include "common/common.h"
23 #include "common/macros.h"
24 #include "plugins/common/param-validation/param-validation.h"
25 #include <babeltrace2/babeltrace.h>
27 #include "cpp-common/cfg-logging-error-reporting.hpp"
28 #include "cpp-common/cfg-logging-error-reporting-throw.hpp"
29 #include "cpp-common/libc-up.hpp"
30 #include "cpp-common/exc.hpp"
32 #define METADATA_TEXT_SIG "/* CTF 1.8"
41 static struct bt_param_validation_map_value_entry_descr metadataInfoQueryParamsDesc
[] = {
42 {"path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
43 bt_param_validation_value_descr::makeString()},
44 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
47 bt2::Value::Shared
metadata_info_query(bt2::ConstMapValue params
, const bt2_common::LogCfg
& logCfg
)
49 gchar
*validateError
= NULL
;
50 auto validationStatus
= bt_param_validation_validate(
51 params
.libObjPtr(), metadataInfoQueryParamsDesc
, &validateError
);
53 if (validationStatus
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
54 throw bt2_common::MemoryError
{};
55 } else if (validationStatus
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
56 const std::string error
{validateError
};
58 g_free(validateError
);
59 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "%s", error
.data());
62 const char *path
= params
["path"]->asString().value().data();
64 bt2_common::FileUP
metadataFp(ctf_fs_metadata_open_file(path
));
66 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
,
67 "Cannot open trace metadata: path=\"%s\".", path
);
72 int ret
= ctf_metadata_decoder_is_packetized(metadataFp
.get(), &is_packetized
, &bo
, logCfg
);
74 BT_CLOGE_APPEND_CAUSE_AND_THROW(
76 "Cannot check whether or not the metadata stream is packetized: path=\"%s\".", path
);
79 ctf_metadata_decoder_config
decoder_cfg(logCfg
);
80 decoder_cfg
.keep_plain_text
= true;
81 ctf_metadata_decoder_up decoder
= ctf_metadata_decoder_create(&decoder_cfg
);
83 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
,
84 "Cannot create metadata decoder: path=\"%s\".", path
);
87 rewind(metadataFp
.get());
88 ctf_metadata_decoder_status decoder_status
=
89 ctf_metadata_decoder_append_content(decoder
.get(), metadataFp
.get());
91 BT_CLOGE_APPEND_CAUSE_AND_THROW(
92 bt2_common::Error
, "Cannot update metadata decoder's content: path=\"%s\".", path
);
95 bt2::MapValue::Shared result
= bt2::MapValue::create();
96 result
->insert("text", ctf_metadata_decoder_get_text(decoder
.get()));
97 result
->insert("is-packetized", is_packetized
);
102 static void add_range(bt2::MapValue info
, struct range
*range
, const char *range_name
)
109 bt2::MapValue rangeMap
= info
.insertEmptyMap(range_name
);
110 rangeMap
.insert("begin", range
->begin_ns
);
111 rangeMap
.insert("end", range
->end_ns
);
114 static void populate_stream_info(struct ctf_fs_ds_file_group
*group
, bt2::MapValue groupInfo
,
115 struct range
*stream_range
, const bt2_common::LogCfg
& logCfg
)
118 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
119 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
120 * the timestamp_begin of the first index entry and the timestamp_end
121 * of the last index entry.
123 BT_ASSERT(group
->index
);
124 BT_ASSERT(group
->index
->entries
);
125 BT_ASSERT(group
->index
->entries
->len
> 0);
128 ctf_fs_ds_index_entry
*first_ds_index_entry
=
129 (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(group
->index
->entries
, 0);
132 ctf_fs_ds_index_entry
*last_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
133 group
->index
->entries
, group
->index
->entries
->len
- 1);
135 stream_range
->begin_ns
= first_ds_index_entry
->timestamp_begin_ns
;
136 stream_range
->end_ns
= last_ds_index_entry
->timestamp_end_ns
;
139 * If any of the begin and end timestamps is not set it means that
140 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
141 * in their packet context so we can't set the range.
144 stream_range
->begin_ns
!= UINT64_C(-1) && stream_range
->end_ns
!= UINT64_C(-1);
146 add_range(groupInfo
, stream_range
, "range-ns");
148 bt2_common::GCharUP portName
= ctf_fs_make_port_name(group
);
150 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "Failed to make port name");
153 groupInfo
.insert("port-name", portName
.get());
156 static void populate_trace_info(const struct ctf_fs_trace
*trace
, bt2::MapValue traceInfo
,
157 const bt2_common::LogCfg
& logCfg
)
159 BT_ASSERT(trace
->ds_file_groups
);
160 /* Add trace range info only if it contains streams. */
161 if (trace
->ds_file_groups
->len
== 0) {
162 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "Trace has no streams: trace-path=%s",
166 bt2::ArrayValue fileGroups
= traceInfo
.insertEmptyArray("stream-infos");
168 /* Find range of all stream groups, and of the trace. */
169 for (size_t group_idx
= 0; group_idx
< trace
->ds_file_groups
->len
; group_idx
++) {
171 ctf_fs_ds_file_group
*group
=
172 (ctf_fs_ds_file_group
*) g_ptr_array_index(trace
->ds_file_groups
, group_idx
);
174 bt2::MapValue groupInfo
= fileGroups
.appendEmptyMap();
175 populate_stream_info(group
, groupInfo
, &group_range
, logCfg
);
180 bt2::Value::Shared
trace_infos_query(bt2::ConstMapValue params
, const bt2_common::LogCfg
& logCfg
)
182 ctf_fs_component::UP ctf_fs
= ctf_fs_component_create(logCfg
);
184 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "Cannot create ctf_fs_component");
187 const bt_value
*inputs_value
= NULL
;
188 const bt_value
*trace_name_value
;
190 if (!read_src_fs_parameters(params
.libObjPtr(), &inputs_value
, &trace_name_value
,
192 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "Failed to read parameters");
195 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs
.get(), inputs_value
, trace_name_value
,
197 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "Failed to create trace");
200 bt2::ArrayValue::Shared result
= bt2::ArrayValue::create();
201 bt2::MapValue traceInfo
= result
->appendEmptyMap();
202 populate_trace_info(ctf_fs
->trace
, traceInfo
, logCfg
);
207 static struct bt_param_validation_map_value_entry_descr supportInfoQueryParamsDesc
[] = {
208 {"type", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
209 bt_param_validation_value_descr::makeString()},
210 {"input", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
211 bt_param_validation_value_descr::makeString()},
212 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
215 bt2::Value::Shared
support_info_query(bt2::ConstMapValue params
, const bt2_common::LogCfg
& logCfg
)
217 gchar
*validateError
= NULL
;
218 auto validationStatus
= bt_param_validation_validate(
219 params
.libObjPtr(), supportInfoQueryParamsDesc
, &validateError
);
221 if (validationStatus
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
222 throw bt2_common::MemoryError
{};
223 } else if (validationStatus
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
224 const std::string error
{validateError
};
226 g_free(validateError
);
227 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "%s", error
.data());
230 bpstd::string_view type
= params
["type"]->asString().value();
232 if (type
!= "directory") {
234 * The input type is not a directory so we are 100% sure it's not a CTF
235 * 1.8 trace as it would need a directory with at least 1 metadata file
236 * and 1 data stream file.
238 bt2::MapValue::Shared result
= bt2::MapValue::create();
239 result
->insert("weight", 0.0f
);
243 bpstd::string_view input
= params
["input"]->asString().value();
245 bt2_common::GCharUP metadataPath
{
246 g_build_filename(input
.c_str(), CTF_FS_METADATA_FILENAME
, NULL
)};
248 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "Failed to read parameters");
252 char uuid_str
[BT_UUID_STR_LEN
+ 1];
253 bool has_uuid
= false;
254 bt2_common::FileUP metadataFile
{g_fopen(metadataPath
.get(), "rb")};
256 enum ctf_metadata_decoder_status decoder_status
;
259 ctf_metadata_decoder_config
metadata_decoder_config(logCfg
);
261 ctf_metadata_decoder_up metadata_decoder
=
262 ctf_metadata_decoder_create(&metadata_decoder_config
);
263 if (!metadata_decoder
) {
264 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error
, "Failed to create metadata decoder");
268 ctf_metadata_decoder_append_content(metadata_decoder
.get(), metadataFile
.get());
269 if (decoder_status
!= CTF_METADATA_DECODER_STATUS_OK
) {
270 BT_CLOGE_APPEND_CAUSE_AND_THROW(
271 bt2_common::Error
, "Failed to append metadata content: metadata-decoder-status=%d",
276 * We were able to parse the metadata file, so we are
277 * confident it's a CTF trace.
281 /* If the trace has a UUID, return the stringified UUID as the group. */
282 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder
.get(), uuid
) == 0) {
283 bt_uuid_to_str(uuid
, uuid_str
);
288 bt2::MapValue::Shared result
= bt2::MapValue::create();
289 result
->insert("weight", weight
);
291 /* We are not supposed to have weight == 0 and a UUID. */
292 BT_ASSERT(weight
> 0 || !has_uuid
);
294 if (weight
> 0 && has_uuid
) {
295 result
->insert("group", uuid_str
);