| 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-INACTIVITY" |
| 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-inactivity-internal.h> |
| 33 | |
| 34 | static |
| 35 | void bt_notification_inactivity_destroy(struct bt_object *obj) |
| 36 | { |
| 37 | struct bt_notification_inactivity *notification = |
| 38 | (struct bt_notification_inactivity *) obj; |
| 39 | |
| 40 | BT_LOGD("Destroying inactivity notification: addr=%p", notification); |
| 41 | BT_LOGD_STR("Putting clock class priority map."); |
| 42 | bt_put(notification->cc_prio_map); |
| 43 | |
| 44 | if (notification->clock_values) { |
| 45 | BT_LOGD_STR("Putting clock values."); |
| 46 | g_hash_table_destroy(notification->clock_values); |
| 47 | } |
| 48 | |
| 49 | g_free(notification); |
| 50 | } |
| 51 | |
| 52 | struct bt_notification *bt_notification_inactivity_create( |
| 53 | struct bt_clock_class_priority_map *cc_prio_map) |
| 54 | { |
| 55 | struct bt_notification_inactivity *notification; |
| 56 | struct bt_notification *ret_notif = NULL; |
| 57 | |
| 58 | if (!cc_prio_map) { |
| 59 | BT_LOGW_STR("Invalid parameter: clock class priority map is NULL."); |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | BT_LOGD("Creating inactivity notification object: " |
| 64 | "cc-prio-map-addr=%p", |
| 65 | cc_prio_map); |
| 66 | notification = g_new0(struct bt_notification_inactivity, 1); |
| 67 | if (!notification) { |
| 68 | BT_LOGE_STR("Failed to allocate one inactivity notification."); |
| 69 | goto error; |
| 70 | } |
| 71 | bt_notification_init(¬ification->parent, |
| 72 | BT_NOTIFICATION_TYPE_INACTIVITY, |
| 73 | bt_notification_inactivity_destroy); |
| 74 | ret_notif = ¬ification->parent; |
| 75 | notification->clock_values = g_hash_table_new_full(g_direct_hash, |
| 76 | g_direct_equal, bt_put, bt_put); |
| 77 | if (!notification->clock_values) { |
| 78 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 79 | goto error; |
| 80 | } |
| 81 | |
| 82 | notification->cc_prio_map = bt_get(cc_prio_map); |
| 83 | BT_LOGD_STR("Freezing inactivity notification's clock class priority map."); |
| 84 | bt_clock_class_priority_map_freeze(cc_prio_map); |
| 85 | BT_LOGD("Created inactivity notification object: " |
| 86 | "cc-prio-map-addr=%p, notif-addr=%p", |
| 87 | cc_prio_map, ret_notif); |
| 88 | goto end; |
| 89 | |
| 90 | error: |
| 91 | BT_PUT(ret_notif); |
| 92 | |
| 93 | end: |
| 94 | return ret_notif; |
| 95 | } |
| 96 | |
| 97 | extern struct bt_clock_class_priority_map * |
| 98 | bt_notification_inactivity_get_clock_class_priority_map( |
| 99 | struct bt_notification *notification) |
| 100 | { |
| 101 | struct bt_clock_class_priority_map *cc_prio_map = NULL; |
| 102 | struct bt_notification_inactivity *inactivity_notification; |
| 103 | |
| 104 | if (!notification) { |
| 105 | BT_LOGW_STR("Invalid parameter: notification is NULL."); |
| 106 | goto end; |
| 107 | } |
| 108 | |
| 109 | if (bt_notification_get_type(notification) != |
| 110 | BT_NOTIFICATION_TYPE_INACTIVITY) { |
| 111 | BT_LOGW("Invalid parameter: notification is not an inactivity notification: " |
| 112 | "addr%p, notif-type=%s", |
| 113 | notification, bt_notification_type_string( |
| 114 | bt_notification_get_type(notification))); |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | inactivity_notification = container_of(notification, |
| 119 | struct bt_notification_inactivity, parent); |
| 120 | cc_prio_map = bt_get(inactivity_notification->cc_prio_map); |
| 121 | end: |
| 122 | return cc_prio_map; |
| 123 | } |
| 124 | |
| 125 | struct bt_ctf_clock_value *bt_notification_inactivity_get_clock_value( |
| 126 | struct bt_notification *notification, |
| 127 | struct bt_ctf_clock_class *clock_class) |
| 128 | { |
| 129 | struct bt_ctf_clock_value *clock_value = NULL; |
| 130 | struct bt_notification_inactivity *inactivity_notification; |
| 131 | |
| 132 | if (!notification) { |
| 133 | BT_LOGW_STR("Invalid parameter: notification is NULL."); |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | if (!clock_class) { |
| 138 | BT_LOGW_STR("Invalid parameter: clock class is NULL."); |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 142 | if (bt_notification_get_type(notification) != |
| 143 | BT_NOTIFICATION_TYPE_INACTIVITY) { |
| 144 | BT_LOGW("Invalid parameter: notification is not an inactivity notification: " |
| 145 | "addr%p, notif-type=%s", |
| 146 | notification, bt_notification_type_string( |
| 147 | bt_notification_get_type(notification))); |
| 148 | goto end; |
| 149 | } |
| 150 | |
| 151 | inactivity_notification = container_of(notification, |
| 152 | struct bt_notification_inactivity, parent); |
| 153 | clock_value = g_hash_table_lookup(inactivity_notification->clock_values, |
| 154 | clock_class); |
| 155 | bt_get(clock_value); |
| 156 | |
| 157 | end: |
| 158 | return clock_value; |
| 159 | } |
| 160 | |
| 161 | int bt_notification_inactivity_set_clock_value( |
| 162 | struct bt_notification *notification, |
| 163 | struct bt_ctf_clock_value *clock_value) |
| 164 | { |
| 165 | int ret = 0; |
| 166 | uint64_t prio; |
| 167 | struct bt_ctf_clock_class *clock_class = NULL; |
| 168 | struct bt_notification_inactivity *inactivity_notification; |
| 169 | |
| 170 | if (!notification) { |
| 171 | BT_LOGW_STR("Invalid parameter: notification is NULL."); |
| 172 | ret = -1; |
| 173 | goto end; |
| 174 | } |
| 175 | |
| 176 | if (!clock_value) { |
| 177 | BT_LOGW_STR("Invalid parameter: clock value is NULL."); |
| 178 | ret = -1; |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | if (notification->frozen) { |
| 183 | BT_LOGW_STR("Invalid parameter: notification is frozen."); |
| 184 | ret = -1; |
| 185 | goto end; |
| 186 | } |
| 187 | |
| 188 | if (bt_notification_get_type(notification) != |
| 189 | BT_NOTIFICATION_TYPE_INACTIVITY) { |
| 190 | BT_LOGW("Invalid parameter: notification is not an inactivity notification: " |
| 191 | "addr%p, notif-type=%s", |
| 192 | notification, bt_notification_type_string( |
| 193 | bt_notification_get_type(notification))); |
| 194 | ret = -1; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | inactivity_notification = container_of(notification, |
| 199 | struct bt_notification_inactivity, parent); |
| 200 | clock_class = bt_ctf_clock_value_get_class(clock_value); |
| 201 | ret = bt_clock_class_priority_map_get_clock_class_priority( |
| 202 | inactivity_notification->cc_prio_map, clock_class, &prio); |
| 203 | if (ret) { |
| 204 | BT_LOGW("Clock value's class is not mapped to a priority within the scope of the inactivity notification: " |
| 205 | "notif-addr=%p, cc-prio-map-addr=%p, " |
| 206 | "clock-class-addr=%p, clock-class-name=\"%s\", " |
| 207 | "clock-value-addr=%p", |
| 208 | inactivity_notification, |
| 209 | inactivity_notification->cc_prio_map, |
| 210 | clock_class, bt_ctf_clock_class_get_name(clock_class), |
| 211 | clock_value); |
| 212 | ret = -1; |
| 213 | goto end; |
| 214 | } |
| 215 | |
| 216 | g_hash_table_insert(inactivity_notification->clock_values, |
| 217 | clock_class, bt_get(clock_value)); |
| 218 | clock_class = NULL; |
| 219 | BT_LOGV("Set inactivity notification's clock value: " |
| 220 | "notif-addr=%p, cc-prio-map-addr=%p, " |
| 221 | "clock-class-addr=%p, clock-class-name=\"%s\", " |
| 222 | "clock-value-addr=%p", |
| 223 | inactivity_notification, |
| 224 | inactivity_notification->cc_prio_map, |
| 225 | clock_class, bt_ctf_clock_class_get_name(clock_class), |
| 226 | clock_value); |
| 227 | |
| 228 | end: |
| 229 | bt_put(clock_class); |
| 230 | return ret; |
| 231 | } |