| 1 | /* |
| 2 | * event-class.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Event class |
| 5 | * |
| 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #define BT_LOG_TAG "EVENT-CLASS" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/assert-pre-internal.h> |
| 33 | #include <babeltrace/ctf-ir/clock-value-internal.h> |
| 34 | #include <babeltrace/ctf-ir/fields-internal.h> |
| 35 | #include <babeltrace/ctf-ir/field-types-internal.h> |
| 36 | #include <babeltrace/ctf-ir/event-class.h> |
| 37 | #include <babeltrace/ctf-ir/event-class-internal.h> |
| 38 | #include <babeltrace/ctf-ir/event-internal.h> |
| 39 | #include <babeltrace/ctf-ir/stream-class.h> |
| 40 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 41 | #include <babeltrace/ctf-ir/trace-internal.h> |
| 42 | #include <babeltrace/ctf-ir/validation-internal.h> |
| 43 | #include <babeltrace/ctf-ir/utils.h> |
| 44 | #include <babeltrace/ctf-ir/utils-internal.h> |
| 45 | #include <babeltrace/ref.h> |
| 46 | #include <babeltrace/ctf-ir/attributes-internal.h> |
| 47 | #include <babeltrace/compiler-internal.h> |
| 48 | #include <babeltrace/endian-internal.h> |
| 49 | #include <babeltrace/types.h> |
| 50 | #include <babeltrace/values-internal.h> |
| 51 | #include <babeltrace/assert-internal.h> |
| 52 | #include <inttypes.h> |
| 53 | #include <stdlib.h> |
| 54 | |
| 55 | BT_HIDDEN |
| 56 | void bt_event_class_common_finalize(struct bt_object *obj) |
| 57 | { |
| 58 | struct bt_event_class_common *event_class; |
| 59 | |
| 60 | event_class = container_of(obj, struct bt_event_class_common, base); |
| 61 | BT_LOGD("Finalizing common event class: addr=%p, name=\"%s\", id=%" PRId64, |
| 62 | event_class, bt_event_class_common_get_name(event_class), |
| 63 | bt_event_class_common_get_id(event_class)); |
| 64 | |
| 65 | if (event_class->name) { |
| 66 | g_string_free(event_class->name, TRUE); |
| 67 | } |
| 68 | |
| 69 | if (event_class->emf_uri) { |
| 70 | g_string_free(event_class->emf_uri, TRUE); |
| 71 | } |
| 72 | |
| 73 | BT_LOGD_STR("Putting context field type."); |
| 74 | bt_put(event_class->context_field_type); |
| 75 | BT_LOGD_STR("Putting payload field type."); |
| 76 | bt_put(event_class->payload_field_type); |
| 77 | } |
| 78 | |
| 79 | BT_HIDDEN |
| 80 | int bt_event_class_common_initialize(struct bt_event_class_common *event_class, |
| 81 | const char *name, bt_object_release_func release_func, |
| 82 | bt_field_type_structure_create_func ft_struct_create_func) |
| 83 | { |
| 84 | int ret = 0; |
| 85 | |
| 86 | BT_LOGD("Initializing common event class object: name=\"%s\"", |
| 87 | name); |
| 88 | bt_object_init(event_class, release_func); |
| 89 | event_class->payload_field_type = ft_struct_create_func(); |
| 90 | if (!event_class->payload_field_type) { |
| 91 | BT_LOGE_STR("Cannot create event class's initial payload field type object."); |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | event_class->id = -1; |
| 96 | event_class->name = g_string_new(name); |
| 97 | if (!event_class->name) { |
| 98 | BT_LOGE_STR("Failed to allocate a GString."); |
| 99 | goto error; |
| 100 | } |
| 101 | |
| 102 | event_class->emf_uri = g_string_new(NULL); |
| 103 | if (!event_class->emf_uri) { |
| 104 | BT_LOGE_STR("Failed to allocate a GString."); |
| 105 | goto error; |
| 106 | } |
| 107 | |
| 108 | event_class->log_level = BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED; |
| 109 | BT_LOGD("Initialized common event class object: addr=%p, name=\"%s\"", |
| 110 | event_class, bt_event_class_common_get_name(event_class)); |
| 111 | return ret; |
| 112 | |
| 113 | error: |
| 114 | ret = -1; |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static |
| 119 | void bt_event_class_destroy(struct bt_object *obj) |
| 120 | { |
| 121 | struct bt_event_class *event_class = (void *) obj; |
| 122 | |
| 123 | BT_LOGD("Destroying event class: addr=%p", obj); |
| 124 | bt_event_class_common_finalize(obj); |
| 125 | bt_object_pool_finalize(&event_class->event_pool); |
| 126 | g_free(obj); |
| 127 | } |
| 128 | |
| 129 | static |
| 130 | void free_event(struct bt_event *event, |
| 131 | struct bt_event_class *event_class) |
| 132 | { |
| 133 | bt_event_destroy(event); |
| 134 | } |
| 135 | |
| 136 | struct bt_event_class *bt_event_class_create(const char *name) |
| 137 | { |
| 138 | int ret; |
| 139 | struct bt_event_class *event_class = NULL; |
| 140 | |
| 141 | if (!name) { |
| 142 | BT_LOGW_STR("Invalid parameter: name is NULL."); |
| 143 | goto error; |
| 144 | } |
| 145 | |
| 146 | BT_LOGD("Creating event class object: name=\"%s\"", |
| 147 | name); |
| 148 | event_class = g_new0(struct bt_event_class, 1); |
| 149 | if (!event_class) { |
| 150 | BT_LOGE_STR("Failed to allocate one event class."); |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | ret = bt_event_class_common_initialize(BT_TO_COMMON(event_class), |
| 155 | name, bt_event_class_destroy, |
| 156 | (bt_field_type_structure_create_func) |
| 157 | bt_field_type_structure_create); |
| 158 | if (ret) { |
| 159 | goto error; |
| 160 | } |
| 161 | |
| 162 | ret = bt_object_pool_initialize(&event_class->event_pool, |
| 163 | (bt_object_pool_new_object_func) bt_event_new, |
| 164 | (bt_object_pool_destroy_object_func) free_event, |
| 165 | event_class); |
| 166 | if (ret) { |
| 167 | BT_LOGE("Failed to initialize event pool: ret=%d", |
| 168 | ret); |
| 169 | goto error; |
| 170 | } |
| 171 | |
| 172 | BT_LOGD("Created event class object: addr=%p, name=\"%s\"", |
| 173 | event_class, bt_event_class_get_name(event_class)); |
| 174 | goto end; |
| 175 | |
| 176 | error: |
| 177 | bt_put(event_class); |
| 178 | |
| 179 | end: |
| 180 | return event_class; |
| 181 | } |
| 182 | |
| 183 | const char *bt_event_class_get_name(struct bt_event_class *event_class) |
| 184 | { |
| 185 | return bt_event_class_common_get_name(BT_TO_COMMON(event_class)); |
| 186 | } |
| 187 | |
| 188 | int64_t bt_event_class_get_id(struct bt_event_class *event_class) |
| 189 | { |
| 190 | return bt_event_class_common_get_id(BT_TO_COMMON(event_class)); |
| 191 | } |
| 192 | |
| 193 | int bt_event_class_set_id(struct bt_event_class *event_class, uint64_t id) |
| 194 | { |
| 195 | return bt_event_class_common_set_id(BT_TO_COMMON(event_class), id); |
| 196 | } |
| 197 | |
| 198 | enum bt_event_class_log_level bt_event_class_get_log_level( |
| 199 | struct bt_event_class *event_class) |
| 200 | { |
| 201 | return bt_event_class_common_get_log_level(BT_TO_COMMON(event_class)); |
| 202 | } |
| 203 | |
| 204 | int bt_event_class_set_log_level(struct bt_event_class *event_class, |
| 205 | enum bt_event_class_log_level log_level) |
| 206 | { |
| 207 | return bt_event_class_common_set_log_level(BT_TO_COMMON(event_class), |
| 208 | log_level); |
| 209 | } |
| 210 | |
| 211 | const char *bt_event_class_get_emf_uri(struct bt_event_class *event_class) |
| 212 | { |
| 213 | return bt_event_class_common_get_emf_uri(BT_TO_COMMON(event_class)); |
| 214 | } |
| 215 | |
| 216 | int bt_event_class_set_emf_uri(struct bt_event_class *event_class, |
| 217 | const char *emf_uri) |
| 218 | { |
| 219 | return bt_event_class_common_set_emf_uri(BT_TO_COMMON(event_class), |
| 220 | emf_uri); |
| 221 | } |
| 222 | |
| 223 | struct bt_stream_class *bt_event_class_borrow_stream_class( |
| 224 | struct bt_event_class *event_class) |
| 225 | { |
| 226 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
| 227 | return BT_FROM_COMMON( |
| 228 | bt_event_class_common_borrow_stream_class(BT_TO_COMMON( |
| 229 | event_class))); |
| 230 | } |
| 231 | |
| 232 | struct bt_field_type *bt_event_class_borrow_payload_field_type( |
| 233 | struct bt_event_class *event_class) |
| 234 | { |
| 235 | return BT_FROM_COMMON(bt_event_class_common_borrow_payload_field_type( |
| 236 | BT_TO_COMMON(event_class))); |
| 237 | } |
| 238 | |
| 239 | int bt_event_class_set_payload_field_type(struct bt_event_class *event_class, |
| 240 | struct bt_field_type *field_type) |
| 241 | { |
| 242 | return bt_event_class_common_set_payload_field_type( |
| 243 | BT_TO_COMMON(event_class), (void *) field_type); |
| 244 | } |
| 245 | |
| 246 | struct bt_field_type *bt_event_class_borrow_context_field_type( |
| 247 | struct bt_event_class *event_class) |
| 248 | { |
| 249 | return BT_FROM_COMMON(bt_event_class_common_borrow_context_field_type( |
| 250 | BT_TO_COMMON(event_class))); |
| 251 | } |
| 252 | |
| 253 | int bt_event_class_set_context_field_type( |
| 254 | struct bt_event_class *event_class, |
| 255 | struct bt_field_type *field_type) |
| 256 | { |
| 257 | return bt_event_class_common_set_context_field_type( |
| 258 | BT_TO_COMMON(event_class), (void *) field_type); |
| 259 | } |
| 260 | |
| 261 | BT_HIDDEN |
| 262 | void bt_event_class_common_freeze(struct bt_event_class_common *event_class) |
| 263 | { |
| 264 | BT_ASSERT(event_class); |
| 265 | |
| 266 | if (event_class->frozen) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | BT_LOGD("Freezing event class: addr=%p, name=\"%s\", id=%" PRId64, |
| 271 | event_class, bt_event_class_common_get_name(event_class), |
| 272 | bt_event_class_common_get_id(event_class)); |
| 273 | event_class->frozen = 1; |
| 274 | BT_LOGD_STR("Freezing event class's context field type."); |
| 275 | bt_field_type_common_freeze(event_class->context_field_type); |
| 276 | BT_LOGD_STR("Freezing event class's payload field type."); |
| 277 | bt_field_type_common_freeze(event_class->payload_field_type); |
| 278 | } |
| 279 | |
| 280 | BT_HIDDEN |
| 281 | int bt_event_class_common_validate_single_clock_class( |
| 282 | struct bt_event_class_common *event_class, |
| 283 | struct bt_clock_class **expected_clock_class) |
| 284 | { |
| 285 | int ret = 0; |
| 286 | |
| 287 | BT_ASSERT(event_class); |
| 288 | BT_ASSERT(expected_clock_class); |
| 289 | ret = bt_field_type_common_validate_single_clock_class( |
| 290 | event_class->context_field_type, |
| 291 | expected_clock_class); |
| 292 | if (ret) { |
| 293 | BT_LOGW("Event class's context field type " |
| 294 | "is not recursively mapped to the " |
| 295 | "expected clock class: " |
| 296 | "event-class-addr=%p, " |
| 297 | "event-class-name=\"%s\", " |
| 298 | "event-class-id=%" PRId64 ", " |
| 299 | "ft-addr=%p", |
| 300 | event_class, |
| 301 | bt_event_class_common_get_name(event_class), |
| 302 | event_class->id, |
| 303 | event_class->context_field_type); |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | ret = bt_field_type_common_validate_single_clock_class( |
| 308 | event_class->payload_field_type, |
| 309 | expected_clock_class); |
| 310 | if (ret) { |
| 311 | BT_LOGW("Event class's payload field type " |
| 312 | "is not recursively mapped to the " |
| 313 | "expected clock class: " |
| 314 | "event-class-addr=%p, " |
| 315 | "event-class-name=\"%s\", " |
| 316 | "event-class-id=%" PRId64 ", " |
| 317 | "ft-addr=%p", |
| 318 | event_class, |
| 319 | bt_event_class_common_get_name(event_class), |
| 320 | event_class->id, |
| 321 | event_class->payload_field_type); |
| 322 | goto end; |
| 323 | } |
| 324 | |
| 325 | end: |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | BT_HIDDEN |
| 330 | int bt_event_class_update_event_pool_clock_values( |
| 331 | struct bt_event_class *event_class) |
| 332 | { |
| 333 | int ret = 0; |
| 334 | uint64_t i; |
| 335 | struct bt_stream_class *stream_class = |
| 336 | bt_event_class_borrow_stream_class(event_class); |
| 337 | |
| 338 | BT_ASSERT(stream_class->common.clock_class); |
| 339 | |
| 340 | for (i = 0; i < event_class->event_pool.size; i++) { |
| 341 | struct bt_clock_value *cv; |
| 342 | struct bt_event *event = |
| 343 | event_class->event_pool.objects->pdata[i]; |
| 344 | |
| 345 | BT_ASSERT(event); |
| 346 | |
| 347 | cv = g_hash_table_lookup(event->clock_values, |
| 348 | stream_class->common.clock_class); |
| 349 | if (cv) { |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | cv = bt_clock_value_create(stream_class->common.clock_class); |
| 354 | if (!cv) { |
| 355 | BT_LIB_LOGE("Cannot create clock value from clock class: " |
| 356 | "%![cc-]+K", stream_class->common.clock_class); |
| 357 | ret = -1; |
| 358 | goto end; |
| 359 | } |
| 360 | |
| 361 | g_hash_table_insert(event->clock_values, |
| 362 | stream_class->common.clock_class, cv); |
| 363 | } |
| 364 | |
| 365 | end: |
| 366 | return ret; |
| 367 | } |