+static
+int set_stream_intersections(struct cmd_run_ctx *ctx,
+ struct bt_config_component *cfg_comp,
+ struct bt_component_class *comp_cls)
+{
+ int ret = 0;
+ uint64_t trace_idx;
+ int64_t trace_count;
+ enum bt_value_status value_status;
+ const char *path = NULL;
+ struct bt_value *component_path_value = NULL;
+ struct bt_value *query_params = NULL;
+ struct bt_value *query_result = NULL;
+ struct bt_value *trace_info = NULL;
+ struct bt_value *intersection_range = NULL;
+ struct bt_value *intersection_begin = NULL;
+ struct bt_value *intersection_end = NULL;
+ struct bt_value *stream_path_value = NULL;
+ struct bt_value *stream_paths = NULL;
+ struct bt_value *stream_infos = NULL;
+ struct bt_value *stream_info = NULL;
+ struct port_id *port_id = NULL;
+ struct trace_range *trace_range = NULL;
+
+ component_path_value = bt_value_map_get(cfg_comp->params, "path");
+ if (!bt_value_is_string(component_path_value)) {
+ BT_LOGD("Cannot get path parameter: component-name=%s",
+ cfg_comp->instance_name->str);
+ ret = -1;
+ goto error;
+ }
+
+ value_status = bt_value_string_get(component_path_value, &path);
+ if (value_status != BT_VALUE_STATUS_OK) {
+ BT_LOGD("Cannot get path string value: component-name=%s",
+ cfg_comp->instance_name->str);
+ ret = -1;
+ goto error;
+ }
+
+ query_params = bt_value_map_create();
+ if (!query_params) {
+ BT_LOGE_STR("Cannot create query parameters.");
+ ret = -1;
+ goto error;
+ }
+
+ value_status = bt_value_map_insert(query_params, "path", component_path_value);
+ if (value_status != BT_VALUE_STATUS_OK) {
+ BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
+ ret = -1;
+ goto error;
+ }
+
+ query_result = bt_component_class_query(comp_cls, "trace-info",
+ query_params);
+ if (!query_result) {
+ BT_LOGD("Component class \'%s\' does not support the \'trace-info\' query.",
+ bt_component_class_get_name(comp_cls));
+ ret = -1;
+ goto error;
+ }
+
+ if (!bt_value_is_array(query_result)) {
+ BT_LOGD("Unexpected format of \'trace-info\' query result: "
+ "component-class-name=%s",
+ bt_component_class_get_name(comp_cls));
+ ret = -1;
+ goto error;
+ }
+
+ trace_count = bt_value_array_size(query_result);
+ if (trace_count < 0) {
+ ret = -1;
+ goto error;
+ }
+
+ for (trace_idx = 0; trace_idx < trace_count; trace_idx++) {
+ int64_t begin, end;
+ uint64_t stream_idx;
+ int64_t stream_count;
+
+ trace_info = bt_value_array_get(query_result, trace_idx);
+ if (!trace_info || !bt_value_is_map(trace_info)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve trace from query result.");
+ goto error;
+ }
+
+ intersection_range = bt_value_map_get(trace_info,
+ "intersection-range-ns");
+ if (!intersection_range) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
+ goto error;
+ }
+
+ intersection_begin = bt_value_map_get(intersection_range,
+ "begin");
+ if (!intersection_begin) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
+ goto error;
+ }
+
+ intersection_end = bt_value_map_get(intersection_range,
+ "end");
+ if (!intersection_end) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
+ goto error;
+ }
+
+ value_status = bt_value_integer_get(intersection_begin, &begin);
+ if (value_status != BT_VALUE_STATUS_OK) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
+ goto error;
+ }
+
+ value_status = bt_value_integer_get(intersection_end, &end);
+ if (value_status != BT_VALUE_STATUS_OK) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
+ goto error;
+ }
+
+ if (begin < 0 || end < 0 || end < begin) {
+ BT_LOGW("Invalid trace stream intersection values: "
+ "intersection-range-ns:begin=%" PRId64
+ ", intersection-range-ns:end=%" PRId64,
+ begin, end);
+ ret = -1;
+ goto error;
+ }
+
+ stream_infos = bt_value_map_get(trace_info, "streams");
+ if (!stream_infos || !bt_value_is_array(stream_infos)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
+ goto error;
+ }
+
+ stream_count = bt_value_array_size(stream_infos);
+ if (stream_count < 0) {
+ ret = -1;
+ goto error;
+ }
+
+ /*
+ * FIXME
+ *
+ * The first path of a stream's "paths" is currently used to
+ * associate streams/ports to a given trace intersection.
+ *
+ * This is a fragile hack as it relies on the port names
+ * being set to the various streams path.
+ *
+ * A stream name should be introduced as part of the trace-info
+ * query result.
+ */
+ for (stream_idx = 0; stream_idx < stream_count; stream_idx++) {
+ const char *stream_path;
+
+ port_id = g_new0(struct port_id, 1);
+ if (!port_id) {
+ ret = -1;
+ BT_LOGE_STR("Cannot allocate memory for port_id structure.");
+ goto error;
+ }
+ port_id->instance_name = strdup(cfg_comp->instance_name->str);
+ if (!port_id->instance_name) {
+ ret = -1;
+ BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
+ goto error;
+ }
+
+ trace_range = g_new0(struct trace_range, 1);
+ if (!trace_range) {
+ ret = -1;
+ BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
+ goto error;
+ }
+ trace_range->intersection_range_begin_ns = begin;
+ trace_range->intersection_range_end_ns = end;
+
+ stream_info = bt_value_array_get(stream_infos,
+ stream_idx);
+ if (!stream_info || !bt_value_is_map(stream_info)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
+ goto error;
+ }
+
+ stream_paths = bt_value_map_get(stream_info, "paths");
+ if (!stream_paths || !bt_value_is_array(stream_paths)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
+ goto error;
+ }
+
+ stream_path_value = bt_value_array_get(stream_paths, 0);
+ if (!stream_path_value ||
+ !bt_value_is_string(stream_path_value)) {
+ ret = -1;
+ BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
+ goto error;
+ }
+
+ value_status = bt_value_string_get(stream_path_value,
+ &stream_path);
+ if (value_status != BT_VALUE_STATUS_OK) {
+ ret = -1;
+ goto error;
+ }
+
+ port_id->port_name = strdup(stream_path);
+ if (!port_id->port_name) {
+ ret = -1;
+ BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
+ goto error;
+ }
+
+ BT_LOGD("Inserting stream intersection ");
+
+ g_hash_table_insert(ctx->intersections, port_id, trace_range);
+
+ port_id = NULL;
+ trace_range = NULL;
+ BT_PUT(stream_info);
+ BT_PUT(stream_paths);
+ BT_PUT(stream_path_value);
+ }
+
+ BT_PUT(trace_info);
+ BT_PUT(stream_paths);
+ BT_PUT(stream_path_value);
+ BT_PUT(intersection_range);
+ BT_PUT(intersection_begin);
+ BT_PUT(intersection_end);
+ BT_PUT(stream_paths);
+ BT_PUT(stream_path_value);
+ }
+
+ goto end;
+
+error:
+ fprintf(stderr, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
+ bt_common_color_bold(),
+ bt_common_color_fg_yellow(),
+ path ? path : "(unknown)",
+ bt_common_color_reset());
+end:
+ bt_put(component_path_value);
+ bt_put(query_params);
+ bt_put(query_result);
+ bt_put(trace_info);
+ bt_put(intersection_range);
+ bt_put(intersection_begin);
+ bt_put(intersection_end);
+ bt_put(stream_infos);
+ bt_put(stream_info);
+ bt_put(stream_paths);
+ bt_put(stream_path_value);
+ g_free(port_id);
+ g_free(trace_range);
+ return ret;
+}
+