| 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 | #include <babeltrace/object-internal.h> |
| 24 | #include <babeltrace/compiler-internal.h> |
| 25 | #include <babeltrace/ctf-ir/clock-class.h> |
| 26 | #include <babeltrace/graph/clock-class-priority-map.h> |
| 27 | #include <babeltrace/graph/clock-class-priority-map-internal.h> |
| 28 | #include <babeltrace/graph/notification-internal.h> |
| 29 | #include <babeltrace/graph/notification-inactivity-internal.h> |
| 30 | |
| 31 | static |
| 32 | void bt_notification_inactivity_destroy(struct bt_object *obj) |
| 33 | { |
| 34 | struct bt_notification_inactivity *notification = |
| 35 | (struct bt_notification_inactivity *) obj; |
| 36 | |
| 37 | bt_put(notification->cc_prio_map); |
| 38 | |
| 39 | if (notification->clock_values) { |
| 40 | g_hash_table_destroy(notification->clock_values); |
| 41 | } |
| 42 | |
| 43 | g_free(notification); |
| 44 | } |
| 45 | |
| 46 | struct bt_notification *bt_notification_inactivity_create( |
| 47 | struct bt_clock_class_priority_map *cc_prio_map) |
| 48 | { |
| 49 | struct bt_notification_inactivity *notification; |
| 50 | struct bt_notification *ret_notif = NULL; |
| 51 | |
| 52 | if (!cc_prio_map) { |
| 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | notification = g_new0(struct bt_notification_inactivity, 1); |
| 57 | if (!notification) { |
| 58 | goto error; |
| 59 | } |
| 60 | bt_notification_init(¬ification->parent, |
| 61 | BT_NOTIFICATION_TYPE_INACTIVITY, |
| 62 | bt_notification_inactivity_destroy); |
| 63 | ret_notif = ¬ification->parent; |
| 64 | notification->clock_values = g_hash_table_new_full(g_direct_hash, |
| 65 | g_direct_equal, bt_put, bt_put); |
| 66 | if (!notification->clock_values) { |
| 67 | goto error; |
| 68 | } |
| 69 | |
| 70 | notification->cc_prio_map = bt_get(cc_prio_map); |
| 71 | bt_clock_class_priority_map_freeze(cc_prio_map); |
| 72 | goto end; |
| 73 | |
| 74 | error: |
| 75 | BT_PUT(ret_notif); |
| 76 | |
| 77 | end: |
| 78 | return ret_notif; |
| 79 | } |
| 80 | |
| 81 | extern struct bt_clock_class_priority_map * |
| 82 | bt_notification_inactivity_get_clock_class_priority_map( |
| 83 | struct bt_notification *notification) |
| 84 | { |
| 85 | struct bt_clock_class_priority_map *cc_prio_map = NULL; |
| 86 | struct bt_notification_inactivity *inactivity_notification; |
| 87 | |
| 88 | if (bt_notification_get_type(notification) != |
| 89 | BT_NOTIFICATION_TYPE_INACTIVITY) { |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | inactivity_notification = container_of(notification, |
| 94 | struct bt_notification_inactivity, parent); |
| 95 | cc_prio_map = bt_get(inactivity_notification->cc_prio_map); |
| 96 | end: |
| 97 | return cc_prio_map; |
| 98 | } |
| 99 | |
| 100 | struct bt_ctf_clock_value *bt_notification_inactivity_get_clock_value( |
| 101 | struct bt_notification *notification, |
| 102 | struct bt_ctf_clock_class *clock_class) |
| 103 | { |
| 104 | struct bt_ctf_clock_value *clock_value = NULL; |
| 105 | struct bt_notification_inactivity *inactivity_notification; |
| 106 | |
| 107 | if (!notification || !clock_class) { |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | if (bt_notification_get_type(notification) != |
| 112 | BT_NOTIFICATION_TYPE_INACTIVITY) { |
| 113 | goto end; |
| 114 | } |
| 115 | |
| 116 | inactivity_notification = container_of(notification, |
| 117 | struct bt_notification_inactivity, parent); |
| 118 | clock_value = g_hash_table_lookup(inactivity_notification->clock_values, |
| 119 | clock_class); |
| 120 | bt_get(clock_value); |
| 121 | |
| 122 | end: |
| 123 | return clock_value; |
| 124 | } |
| 125 | |
| 126 | int bt_notification_inactivity_set_clock_value( |
| 127 | struct bt_notification *notification, |
| 128 | struct bt_ctf_clock_value *clock_value) |
| 129 | { |
| 130 | int ret = 0; |
| 131 | uint64_t prio; |
| 132 | struct bt_ctf_clock_class *clock_class = NULL; |
| 133 | struct bt_notification_inactivity *inactivity_notification; |
| 134 | |
| 135 | if (!notification || !clock_value || notification->frozen) { |
| 136 | ret = -1; |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | if (bt_notification_get_type(notification) != |
| 141 | BT_NOTIFICATION_TYPE_INACTIVITY) { |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | inactivity_notification = container_of(notification, |
| 146 | struct bt_notification_inactivity, parent); |
| 147 | clock_class = bt_ctf_clock_value_get_class(clock_value); |
| 148 | ret = bt_clock_class_priority_map_get_clock_class_priority( |
| 149 | inactivity_notification->cc_prio_map, clock_class, &prio); |
| 150 | if (ret) { |
| 151 | /* |
| 152 | * Clock value's class is not mapped to a priority |
| 153 | * within the scope of this notification. |
| 154 | */ |
| 155 | goto end; |
| 156 | } |
| 157 | |
| 158 | g_hash_table_insert(inactivity_notification->clock_values, |
| 159 | clock_class, bt_get(clock_value)); |
| 160 | clock_class = NULL; |
| 161 | |
| 162 | end: |
| 163 | bt_put(clock_class); |
| 164 | return ret; |
| 165 | } |