| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #define BT_COMP_LOG_SELF_COMP (log_cfg->self_comp) |
| 8 | #define BT_LOG_OUTPUT_LEVEL (log_cfg->log_level) |
| 9 | #define BT_LOG_TAG "PLUGIN/CTF/META/UPDATE-DEF-CC" |
| 10 | #include "logging/comp-logging.h" |
| 11 | |
| 12 | #include <babeltrace2/babeltrace.h> |
| 13 | #include "common/macros.h" |
| 14 | #include "common/assert.h" |
| 15 | #include <glib.h> |
| 16 | #include <stdint.h> |
| 17 | #include <string.h> |
| 18 | #include <inttypes.h> |
| 19 | |
| 20 | #include "ctf-meta-visitors.h" |
| 21 | #include "logging.h" |
| 22 | |
| 23 | static inline |
| 24 | int find_mapped_clock_class(struct ctf_field_class *fc, |
| 25 | struct ctf_clock_class **clock_class, |
| 26 | struct meta_log_config *log_cfg) |
| 27 | { |
| 28 | int ret = 0; |
| 29 | uint64_t i; |
| 30 | |
| 31 | if (!fc) { |
| 32 | goto end; |
| 33 | } |
| 34 | |
| 35 | switch (fc->type) { |
| 36 | case CTF_FIELD_CLASS_TYPE_INT: |
| 37 | case CTF_FIELD_CLASS_TYPE_ENUM: |
| 38 | { |
| 39 | struct ctf_field_class_int *int_fc = (void *) fc; |
| 40 | |
| 41 | if (int_fc->mapped_clock_class) { |
| 42 | if (*clock_class && *clock_class != |
| 43 | int_fc->mapped_clock_class) { |
| 44 | BT_COMP_LOGE("Stream class contains more than one " |
| 45 | "clock class: expected-cc-name=\"%s\", " |
| 46 | "other-cc-name=\"%s\"", |
| 47 | (*clock_class)->name->str, |
| 48 | int_fc->mapped_clock_class->name->str); |
| 49 | ret = -1; |
| 50 | goto end; |
| 51 | } |
| 52 | |
| 53 | *clock_class = int_fc->mapped_clock_class; |
| 54 | } |
| 55 | |
| 56 | break; |
| 57 | } |
| 58 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 59 | { |
| 60 | struct ctf_field_class_struct *struct_fc = (void *) fc; |
| 61 | |
| 62 | for (i = 0; i < struct_fc->members->len; i++) { |
| 63 | struct ctf_named_field_class *named_fc = |
| 64 | ctf_field_class_struct_borrow_member_by_index( |
| 65 | struct_fc, i); |
| 66 | |
| 67 | ret = find_mapped_clock_class(named_fc->fc, |
| 68 | clock_class, log_cfg); |
| 69 | if (ret) { |
| 70 | goto end; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | break; |
| 75 | } |
| 76 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 77 | { |
| 78 | struct ctf_field_class_variant *var_fc = (void *) fc; |
| 79 | |
| 80 | for (i = 0; i < var_fc->options->len; i++) { |
| 81 | struct ctf_named_field_class *named_fc = |
| 82 | ctf_field_class_variant_borrow_option_by_index( |
| 83 | var_fc, i); |
| 84 | |
| 85 | ret = find_mapped_clock_class(named_fc->fc, |
| 86 | clock_class, log_cfg); |
| 87 | if (ret) { |
| 88 | goto end; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | break; |
| 93 | } |
| 94 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 95 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 96 | { |
| 97 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
| 98 | |
| 99 | ret = find_mapped_clock_class(array_fc->elem_fc, clock_class, |
| 100 | log_cfg); |
| 101 | if (ret) { |
| 102 | goto end; |
| 103 | } |
| 104 | |
| 105 | break; |
| 106 | } |
| 107 | default: |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | end: |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | static inline |
| 116 | int update_stream_class_default_clock_class( |
| 117 | struct ctf_stream_class *stream_class, |
| 118 | struct meta_log_config *log_cfg) |
| 119 | { |
| 120 | int ret = 0; |
| 121 | struct ctf_clock_class *clock_class = |
| 122 | stream_class->default_clock_class; |
| 123 | uint64_t i; |
| 124 | |
| 125 | ret = find_mapped_clock_class(stream_class->packet_context_fc, |
| 126 | &clock_class, log_cfg); |
| 127 | if (ret) { |
| 128 | goto end; |
| 129 | } |
| 130 | |
| 131 | ret = find_mapped_clock_class(stream_class->event_header_fc, |
| 132 | &clock_class, log_cfg); |
| 133 | if (ret) { |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | ret = find_mapped_clock_class(stream_class->event_common_context_fc, |
| 138 | &clock_class, log_cfg); |
| 139 | if (ret) { |
| 140 | goto end; |
| 141 | } |
| 142 | |
| 143 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 144 | struct ctf_event_class *event_class = |
| 145 | stream_class->event_classes->pdata[i]; |
| 146 | |
| 147 | ret = find_mapped_clock_class(event_class->spec_context_fc, |
| 148 | &clock_class, log_cfg); |
| 149 | if (ret) { |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | ret = find_mapped_clock_class(event_class->payload_fc, |
| 154 | &clock_class, log_cfg); |
| 155 | if (ret) { |
| 156 | goto end; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (!stream_class->default_clock_class) { |
| 161 | stream_class->default_clock_class = clock_class; |
| 162 | } |
| 163 | |
| 164 | end: |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | BT_HIDDEN |
| 169 | int ctf_trace_class_update_default_clock_classes(struct ctf_trace_class *ctf_tc, |
| 170 | struct meta_log_config *log_cfg) |
| 171 | { |
| 172 | uint64_t i; |
| 173 | int ret = 0; |
| 174 | struct ctf_clock_class *clock_class = NULL; |
| 175 | |
| 176 | ret = find_mapped_clock_class(ctf_tc->packet_header_fc, &clock_class, |
| 177 | log_cfg); |
| 178 | if (ret) { |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | if (clock_class) { |
| 183 | ret = -1; |
| 184 | goto end; |
| 185 | } |
| 186 | |
| 187 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { |
| 188 | struct ctf_stream_class *sc = |
| 189 | ctf_tc->stream_classes->pdata[i]; |
| 190 | |
| 191 | ret = update_stream_class_default_clock_class( |
| 192 | ctf_tc->stream_classes->pdata[i], log_cfg); |
| 193 | if (ret) { |
| 194 | BT_COMP_LOGE("Stream class contains more than one " |
| 195 | "clock class: stream-class-id=%" PRIu64, |
| 196 | sc->id); |
| 197 | goto end; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | end: |
| 202 | return ret; |
| 203 | } |