Commit | Line | Data |
---|---|---|
ece3fb0f PP |
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 | ||
e8892093 PP |
23 | #define BT_LOG_TAG "NOTIF-INACTIVITY" |
24 | #include <babeltrace/lib-logging-internal.h> | |
25 | ||
ece3fb0f | 26 | #include <babeltrace/object-internal.h> |
3d9990ac | 27 | #include <babeltrace/compiler-internal.h> |
ece3fb0f | 28 | #include <babeltrace/ctf-ir/clock-class.h> |
8b45963b | 29 | #include <babeltrace/ctf-ir/clock-value-internal.h> |
ece3fb0f | 30 | #include <babeltrace/graph/clock-class-priority-map.h> |
fe650ea4 | 31 | #include <babeltrace/graph/clock-class-priority-map-internal.h> |
ece3fb0f PP |
32 | #include <babeltrace/graph/notification-internal.h> |
33 | #include <babeltrace/graph/notification-inactivity-internal.h> | |
8b45963b | 34 | #include <babeltrace/assert-pre-internal.h> |
ece3fb0f PP |
35 | |
36 | static | |
37 | void bt_notification_inactivity_destroy(struct bt_object *obj) | |
38 | { | |
39 | struct bt_notification_inactivity *notification = | |
40 | (struct bt_notification_inactivity *) obj; | |
41 | ||
e8892093 PP |
42 | BT_LOGD("Destroying inactivity notification: addr=%p", notification); |
43 | BT_LOGD_STR("Putting clock class priority map."); | |
ece3fb0f PP |
44 | bt_put(notification->cc_prio_map); |
45 | ||
46 | if (notification->clock_values) { | |
e8892093 | 47 | BT_LOGD_STR("Putting clock values."); |
ece3fb0f PP |
48 | g_hash_table_destroy(notification->clock_values); |
49 | } | |
50 | ||
51 | g_free(notification); | |
52 | } | |
53 | ||
54 | struct bt_notification *bt_notification_inactivity_create( | |
55 | struct bt_clock_class_priority_map *cc_prio_map) | |
56 | { | |
57 | struct bt_notification_inactivity *notification; | |
58 | struct bt_notification *ret_notif = NULL; | |
59 | ||
6ce7345f PP |
60 | if (cc_prio_map) { |
61 | /* Function's reference, released at the end */ | |
62 | bt_get(cc_prio_map); | |
63 | } else { | |
64 | cc_prio_map = bt_clock_class_priority_map_create(); | |
65 | if (!cc_prio_map) { | |
66 | BT_LOGE_STR("Cannot create empty clock class priority map."); | |
67 | goto error; | |
68 | } | |
ece3fb0f PP |
69 | } |
70 | ||
e8892093 PP |
71 | BT_LOGD("Creating inactivity notification object: " |
72 | "cc-prio-map-addr=%p", | |
73 | cc_prio_map); | |
ece3fb0f PP |
74 | notification = g_new0(struct bt_notification_inactivity, 1); |
75 | if (!notification) { | |
e8892093 | 76 | BT_LOGE_STR("Failed to allocate one inactivity notification."); |
ece3fb0f PP |
77 | goto error; |
78 | } | |
79 | bt_notification_init(¬ification->parent, | |
e8892093 PP |
80 | BT_NOTIFICATION_TYPE_INACTIVITY, |
81 | bt_notification_inactivity_destroy); | |
ece3fb0f PP |
82 | ret_notif = ¬ification->parent; |
83 | notification->clock_values = g_hash_table_new_full(g_direct_hash, | |
e8892093 | 84 | g_direct_equal, bt_put, bt_put); |
ece3fb0f | 85 | if (!notification->clock_values) { |
e8892093 | 86 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
ece3fb0f PP |
87 | goto error; |
88 | } | |
89 | ||
90 | notification->cc_prio_map = bt_get(cc_prio_map); | |
e8892093 | 91 | BT_LOGD_STR("Freezing inactivity notification's clock class priority map."); |
fe650ea4 | 92 | bt_clock_class_priority_map_freeze(cc_prio_map); |
e8892093 PP |
93 | BT_LOGD("Created inactivity notification object: " |
94 | "cc-prio-map-addr=%p, notif-addr=%p", | |
95 | cc_prio_map, ret_notif); | |
ece3fb0f PP |
96 | goto end; |
97 | ||
98 | error: | |
99 | BT_PUT(ret_notif); | |
100 | ||
101 | end: | |
6ce7345f | 102 | bt_put(cc_prio_map); |
ece3fb0f PP |
103 | return ret_notif; |
104 | } | |
105 | ||
106 | extern struct bt_clock_class_priority_map * | |
107 | bt_notification_inactivity_get_clock_class_priority_map( | |
108 | struct bt_notification *notification) | |
109 | { | |
ece3fb0f PP |
110 | struct bt_notification_inactivity *inactivity_notification; |
111 | ||
8b45963b PP |
112 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
113 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, | |
114 | BT_NOTIFICATION_TYPE_INACTIVITY); | |
ece3fb0f PP |
115 | inactivity_notification = container_of(notification, |
116 | struct bt_notification_inactivity, parent); | |
8b45963b | 117 | return bt_get(inactivity_notification->cc_prio_map); |
ece3fb0f PP |
118 | } |
119 | ||
839d52a5 | 120 | struct bt_clock_value *bt_notification_inactivity_get_clock_value( |
ece3fb0f | 121 | struct bt_notification *notification, |
839d52a5 | 122 | struct bt_clock_class *clock_class) |
ece3fb0f | 123 | { |
ece3fb0f PP |
124 | struct bt_notification_inactivity *inactivity_notification; |
125 | ||
8b45963b PP |
126 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
127 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock_class"); | |
128 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, | |
129 | BT_NOTIFICATION_TYPE_INACTIVITY); | |
ece3fb0f | 130 | inactivity_notification = container_of(notification, |
e8892093 | 131 | struct bt_notification_inactivity, parent); |
8b45963b PP |
132 | return bt_get(g_hash_table_lookup( |
133 | inactivity_notification->clock_values, clock_class)); | |
134 | } | |
ece3fb0f | 135 | |
8b45963b PP |
136 | BT_ASSERT_PRE_FUNC |
137 | static inline bool cc_prio_map_contains_clock_class( | |
138 | struct bt_clock_class_priority_map *cc_prio_map, | |
139 | struct bt_clock_class *clock_class) | |
140 | { | |
141 | int ret = 0; | |
142 | uint64_t prio; | |
143 | ||
144 | ret = bt_clock_class_priority_map_get_clock_class_priority( | |
145 | cc_prio_map, clock_class, &prio); | |
146 | return ret == 0; | |
ece3fb0f PP |
147 | } |
148 | ||
149 | int bt_notification_inactivity_set_clock_value( | |
150 | struct bt_notification *notification, | |
839d52a5 | 151 | struct bt_clock_value *clock_value) |
ece3fb0f | 152 | { |
ece3fb0f PP |
153 | struct bt_notification_inactivity *inactivity_notification; |
154 | ||
8b45963b PP |
155 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
156 | BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value"); | |
6ff151ad | 157 | BT_ASSERT_PRE_HOT(notification, "Notification", |
8b45963b PP |
158 | ": +%!+n", notification); |
159 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, | |
160 | BT_NOTIFICATION_TYPE_INACTIVITY); | |
ece3fb0f PP |
161 | inactivity_notification = container_of(notification, |
162 | struct bt_notification_inactivity, parent); | |
8b45963b PP |
163 | BT_ASSERT_PRE(cc_prio_map_contains_clock_class( |
164 | inactivity_notification->cc_prio_map, clock_value->clock_class), | |
165 | "Clock value's class is not mapped to a priority within the scope of the inactivity notification: " | |
166 | "notif-addr=%p, cc-prio-map-addr=%p, " | |
167 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
168 | "clock-value-addr=%p", | |
169 | inactivity_notification, | |
170 | inactivity_notification->cc_prio_map, | |
171 | clock_value->clock_class, | |
172 | bt_clock_class_get_name(clock_value->clock_class), clock_value); | |
ece3fb0f | 173 | g_hash_table_insert(inactivity_notification->clock_values, |
8b45963b | 174 | clock_value->clock_class, bt_get(clock_value)); |
e8892093 PP |
175 | BT_LOGV("Set inactivity notification's clock value: " |
176 | "notif-addr=%p, cc-prio-map-addr=%p, " | |
177 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
178 | "clock-value-addr=%p", | |
179 | inactivity_notification, | |
180 | inactivity_notification->cc_prio_map, | |
8b45963b PP |
181 | clock_value->clock_class, |
182 | bt_clock_class_get_name(clock_value->clock_class), clock_value); | |
183 | return 0; | |
ece3fb0f | 184 | } |