| 1 | /* |
| 2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "NOTIF-DISCARDED" |
| 24 | #include <babeltrace/lib-logging-internal.h> |
| 25 | |
| 26 | #include <babeltrace/object-internal.h> |
| 27 | #include <babeltrace/compiler-internal.h> |
| 28 | #include <babeltrace/ctf-ir/clock-class.h> |
| 29 | #include <babeltrace/graph/clock-class-priority-map.h> |
| 30 | #include <babeltrace/graph/clock-class-priority-map-internal.h> |
| 31 | #include <babeltrace/graph/notification-internal.h> |
| 32 | #include <babeltrace/graph/notification-discarded-elements-internal.h> |
| 33 | #include <stdint.h> |
| 34 | #include <inttypes.h> |
| 35 | |
| 36 | static |
| 37 | void bt_notification_discarded_elements_destroy(struct bt_object *obj) |
| 38 | { |
| 39 | struct bt_notification_discarded_elements *notification = |
| 40 | (struct bt_notification_discarded_elements *) obj; |
| 41 | |
| 42 | BT_LOGD("Destroying discarded elements notification: addr=%p", |
| 43 | notification); |
| 44 | BT_LOGD_STR("Putting stream."); |
| 45 | bt_put(notification->stream); |
| 46 | BT_LOGD_STR("Putting beginning clock value."); |
| 47 | bt_put(notification->begin_clock_value); |
| 48 | BT_LOGD_STR("Putting end clock value."); |
| 49 | bt_put(notification->end_clock_value); |
| 50 | g_free(notification); |
| 51 | } |
| 52 | |
| 53 | BT_HIDDEN |
| 54 | struct bt_notification *bt_notification_discarded_elements_create( |
| 55 | enum bt_notification_type type, |
| 56 | struct bt_ctf_stream *stream, |
| 57 | struct bt_ctf_clock_value *begin_clock_value, |
| 58 | struct bt_ctf_clock_value *end_clock_value, |
| 59 | uint64_t count) |
| 60 | { |
| 61 | struct bt_notification_discarded_elements *notification; |
| 62 | struct bt_notification *ret_notif = NULL; |
| 63 | |
| 64 | if (!stream) { |
| 65 | BT_LOGW_STR("Invalid parameter: stream is NULL."); |
| 66 | } |
| 67 | |
| 68 | BT_LOGD("Creating discarded elements notification object: " |
| 69 | "type=%s, stream-addr=%p, stream-name=\"%s\", " |
| 70 | "begin-clock-value-addr=%p, end-clock-value-addr=%p, " |
| 71 | "count=%" PRIu64, |
| 72 | bt_notification_type_string(type), stream, |
| 73 | bt_ctf_stream_get_name(stream), begin_clock_value, |
| 74 | end_clock_value, count); |
| 75 | notification = g_new0(struct bt_notification_discarded_elements, 1); |
| 76 | if (!notification) { |
| 77 | BT_LOGE_STR("Failed to allocate one discarded elements notification."); |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | bt_notification_init(¬ification->parent, type, |
| 82 | bt_notification_discarded_elements_destroy); |
| 83 | ret_notif = ¬ification->parent; |
| 84 | notification->stream = bt_get(stream); |
| 85 | notification->begin_clock_value = bt_get(begin_clock_value); |
| 86 | notification->end_clock_value = bt_get(end_clock_value); |
| 87 | notification->count = (int64_t) count; |
| 88 | BT_LOGD("Created discarded elements notification object: " |
| 89 | "type=%s, stream-addr=%p, stream-name=\"%s\", " |
| 90 | "begin-clock-value-addr=%p, end-clock-value-addr=%p, " |
| 91 | "count=%" PRIu64 ", addr=%p", |
| 92 | bt_notification_type_string(type), stream, |
| 93 | bt_ctf_stream_get_name(stream), begin_clock_value, |
| 94 | end_clock_value, count, ret_notif); |
| 95 | goto end; |
| 96 | |
| 97 | error: |
| 98 | BT_PUT(ret_notif); |
| 99 | |
| 100 | end: |
| 101 | return ret_notif; |
| 102 | } |
| 103 | |
| 104 | BT_HIDDEN |
| 105 | struct bt_ctf_clock_value * |
| 106 | bt_notification_discarded_elements_get_begin_clock_value( |
| 107 | enum bt_notification_type type, |
| 108 | struct bt_notification *notification) |
| 109 | { |
| 110 | struct bt_ctf_clock_value *clock_value = NULL; |
| 111 | struct bt_notification_discarded_elements *discarded_elems_notif; |
| 112 | |
| 113 | if (!notification) { |
| 114 | BT_LOGW_STR("Invalid parameter: notification is NULL."); |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | if (bt_notification_get_type(notification) != |
| 119 | BT_NOTIFICATION_TYPE_DISCARDED_EVENTS) { |
| 120 | BT_LOGW("Invalid parameter: notification has not the expected type: " |
| 121 | "addr%p, expected-type=%s, notif-type=%s", |
| 122 | notification, bt_notification_type_string(type), |
| 123 | bt_notification_type_string( |
| 124 | bt_notification_get_type(notification))); |
| 125 | goto end; |
| 126 | } |
| 127 | |
| 128 | discarded_elems_notif = container_of(notification, |
| 129 | struct bt_notification_discarded_elements, parent); |
| 130 | clock_value = bt_get(discarded_elems_notif->begin_clock_value); |
| 131 | |
| 132 | end: |
| 133 | return clock_value; |
| 134 | } |
| 135 | |
| 136 | BT_HIDDEN |
| 137 | struct bt_ctf_clock_value * |
| 138 | bt_notification_discarded_elements_get_end_clock_value( |
| 139 | enum bt_notification_type type, |
| 140 | struct bt_notification *notification) |
| 141 | { |
| 142 | struct bt_ctf_clock_value *clock_value = NULL; |
| 143 | struct bt_notification_discarded_elements *discarded_elems_notif; |
| 144 | |
| 145 | if (!notification) { |
| 146 | BT_LOGW_STR("Invalid parameter: notification is NULL."); |
| 147 | goto end; |
| 148 | } |
| 149 | |
| 150 | if (bt_notification_get_type(notification) != |
| 151 | BT_NOTIFICATION_TYPE_DISCARDED_EVENTS) { |
| 152 | BT_LOGW("Invalid parameter: notification has not the expected type: " |
| 153 | "addr%p, expected-type=%s, notif-type=%s", |
| 154 | notification, bt_notification_type_string(type), |
| 155 | bt_notification_type_string( |
| 156 | bt_notification_get_type(notification))); |
| 157 | goto end; |
| 158 | } |
| 159 | |
| 160 | discarded_elems_notif = container_of(notification, |
| 161 | struct bt_notification_discarded_elements, parent); |
| 162 | clock_value = bt_get(discarded_elems_notif->end_clock_value); |
| 163 | |
| 164 | end: |
| 165 | return clock_value; |
| 166 | } |
| 167 | |
| 168 | BT_HIDDEN |
| 169 | int64_t bt_notification_discarded_elements_get_count( |
| 170 | enum bt_notification_type type, |
| 171 | struct bt_notification *notification) |
| 172 | { |
| 173 | int64_t count = (int64_t) -1; |
| 174 | struct bt_notification_discarded_elements *discarded_elems_notif; |
| 175 | |
| 176 | if (!notification) { |
| 177 | BT_LOGW_STR("Invalid parameter: notification is NULL."); |
| 178 | goto end; |
| 179 | } |
| 180 | |
| 181 | if (bt_notification_get_type(notification) != |
| 182 | BT_NOTIFICATION_TYPE_DISCARDED_EVENTS) { |
| 183 | BT_LOGW("Invalid parameter: notification has not the expected type: " |
| 184 | "addr%p, expected-type=%s, notif-type=%s", |
| 185 | notification, bt_notification_type_string(type), |
| 186 | bt_notification_type_string( |
| 187 | bt_notification_get_type(notification))); |
| 188 | goto end; |
| 189 | } |
| 190 | |
| 191 | discarded_elems_notif = container_of(notification, |
| 192 | struct bt_notification_discarded_elements, parent); |
| 193 | count = discarded_elems_notif->count; |
| 194 | |
| 195 | end: |
| 196 | return count; |
| 197 | } |
| 198 | |
| 199 | BT_HIDDEN |
| 200 | struct bt_ctf_stream *bt_notification_discarded_elements_get_stream( |
| 201 | enum bt_notification_type type, |
| 202 | struct bt_notification *notification) |
| 203 | { |
| 204 | struct bt_ctf_stream *stream = NULL; |
| 205 | struct bt_notification_discarded_elements *discarded_elems_notif; |
| 206 | |
| 207 | if (!notification) { |
| 208 | BT_LOGW_STR("Invalid parameter: notification is NULL."); |
| 209 | goto end; |
| 210 | } |
| 211 | |
| 212 | if (bt_notification_get_type(notification) != |
| 213 | BT_NOTIFICATION_TYPE_DISCARDED_EVENTS) { |
| 214 | BT_LOGW("Invalid parameter: notification has not the expected type: " |
| 215 | "addr%p, expected-type=%s, notif-type=%s", |
| 216 | notification, bt_notification_type_string(type), |
| 217 | bt_notification_type_string( |
| 218 | bt_notification_get_type(notification))); |
| 219 | goto end; |
| 220 | } |
| 221 | |
| 222 | discarded_elems_notif = container_of(notification, |
| 223 | struct bt_notification_discarded_elements, parent); |
| 224 | stream = bt_get(discarded_elems_notif->stream); |
| 225 | |
| 226 | end: |
| 227 | return stream; |
| 228 | } |