Commit | Line | Data |
---|---|---|
d51202da JG |
1 | /* |
2 | * Babeltrace Plug-in Event Notification | |
3 | * | |
4 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
546a0c4a PP |
27 | #define BT_LOG_TAG "NOTIF-EVENT" |
28 | #include <babeltrace/lib-logging-internal.h> | |
29 | ||
3d9990ac | 30 | #include <babeltrace/compiler-internal.h> |
599faa1c | 31 | #include <babeltrace/ctf-ir/event.h> |
80f00602 | 32 | #include <babeltrace/ctf-ir/event-internal.h> |
d8a36e14 PP |
33 | #include <babeltrace/ctf-ir/event-class-internal.h> |
34 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
599faa1c PP |
35 | #include <babeltrace/ctf-ir/trace.h> |
36 | #include <babeltrace/graph/clock-class-priority-map.h> | |
fe650ea4 | 37 | #include <babeltrace/graph/clock-class-priority-map-internal.h> |
b2e0c907 | 38 | #include <babeltrace/graph/notification-event-internal.h> |
c55a9f58 | 39 | #include <babeltrace/types.h> |
8b45963b PP |
40 | #include <babeltrace/assert-internal.h> |
41 | #include <babeltrace/assert-pre-internal.h> | |
a68c0f97 | 42 | #include <stdbool.h> |
546a0c4a | 43 | #include <inttypes.h> |
d51202da JG |
44 | |
45 | static | |
46 | void bt_notification_event_destroy(struct bt_object *obj) | |
47 | { | |
48 | struct bt_notification_event *notification = | |
49 | (struct bt_notification_event *) obj; | |
50 | ||
546a0c4a PP |
51 | BT_LOGD("Destroying event notification: addr=%p", notification); |
52 | BT_LOGD_STR("Putting event."); | |
d51202da | 53 | BT_PUT(notification->event); |
546a0c4a | 54 | BT_LOGD_STR("Putting clock class priority map."); |
599faa1c | 55 | BT_PUT(notification->cc_prio_map); |
d51202da JG |
56 | g_free(notification); |
57 | } | |
58 | ||
8b45963b | 59 | BT_ASSERT_PRE_FUNC static inline |
c55a9f58 | 60 | bt_bool validate_clock_classes(struct bt_notification_event *notif) |
599faa1c PP |
61 | { |
62 | /* | |
d8a36e14 PP |
63 | * For each clock class found in the notification's clock class |
64 | * priority map, make sure the event has a clock value set for | |
65 | * this clock class. Also make sure that those clock classes | |
66 | * are part of the trace to which the event belongs. | |
599faa1c | 67 | */ |
c55a9f58 | 68 | bt_bool is_valid = BT_TRUE; |
1149be64 | 69 | |
d8a36e14 PP |
70 | int trace_cc_count; |
71 | int cc_prio_map_cc_count; | |
72 | size_t cc_prio_map_cc_i, trace_cc_i; | |
839d52a5 PP |
73 | struct bt_clock_value *clock_value = NULL; |
74 | struct bt_clock_class *clock_class = NULL; | |
75 | struct bt_event_class *event_class = NULL; | |
76 | struct bt_stream_class *stream_class = NULL; | |
77 | struct bt_trace *trace = NULL; | |
599faa1c | 78 | |
8deee039 | 79 | event_class = bt_event_borrow_class(notif->event); |
8b45963b | 80 | BT_ASSERT(event_class); |
839d52a5 | 81 | stream_class = bt_event_class_borrow_stream_class(event_class); |
8b45963b | 82 | BT_ASSERT(stream_class); |
839d52a5 | 83 | trace = bt_stream_class_borrow_trace(stream_class); |
8b45963b | 84 | BT_ASSERT(trace); |
839d52a5 | 85 | trace_cc_count = bt_trace_get_clock_class_count(trace); |
8b45963b | 86 | BT_ASSERT(trace_cc_count >= 0); |
d8a36e14 PP |
87 | cc_prio_map_cc_count = |
88 | bt_clock_class_priority_map_get_clock_class_count( | |
89 | notif->cc_prio_map); | |
8b45963b | 90 | BT_ASSERT(cc_prio_map_cc_count >= 0); |
599faa1c | 91 | |
d8a36e14 PP |
92 | for (cc_prio_map_cc_i = 0; cc_prio_map_cc_i < cc_prio_map_cc_count; |
93 | cc_prio_map_cc_i++) { | |
c55a9f58 | 94 | bt_bool found_in_trace = BT_FALSE; |
599faa1c | 95 | |
1149be64 PP |
96 | clock_class = |
97 | bt_clock_class_priority_map_get_clock_class_by_index( | |
98 | notif->cc_prio_map, cc_prio_map_cc_i); | |
8b45963b | 99 | BT_ASSERT(clock_class); |
839d52a5 | 100 | clock_value = bt_event_get_clock_value(notif->event, |
d8a36e14 PP |
101 | clock_class); |
102 | if (!clock_value) { | |
8b45963b | 103 | BT_ASSERT_PRE_MSG("Event has no clock value for a clock class which exists in the notification's clock class priority map: " |
546a0c4a PP |
104 | "notif-addr=%p, event-addr=%p, " |
105 | "event-class-addr=%p, event-class-name=\"%s\", " | |
106 | "event-class-id=%" PRId64 ", " | |
107 | "cc-prio-map-addr=%p, " | |
108 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
109 | notif, notif->event, event_class, | |
839d52a5 PP |
110 | bt_event_class_get_name(event_class), |
111 | bt_event_class_get_id(event_class), | |
546a0c4a | 112 | notif->cc_prio_map, clock_class, |
839d52a5 | 113 | bt_clock_class_get_name(clock_class)); |
c55a9f58 | 114 | is_valid = BT_FALSE; |
d8a36e14 PP |
115 | goto end; |
116 | } | |
117 | ||
d8a36e14 PP |
118 | for (trace_cc_i = 0; trace_cc_i < trace_cc_count; |
119 | trace_cc_i++) { | |
839d52a5 PP |
120 | struct bt_clock_class *trace_clock_class = |
121 | bt_trace_get_clock_class_by_index(trace, | |
9ac68eb1 | 122 | trace_cc_i); |
d8a36e14 | 123 | |
8b45963b | 124 | BT_ASSERT(trace_clock_class); |
1149be64 | 125 | bt_put(trace_clock_class); |
d8a36e14 PP |
126 | |
127 | if (trace_clock_class == clock_class) { | |
c55a9f58 | 128 | found_in_trace = BT_TRUE; |
d8a36e14 PP |
129 | break; |
130 | } | |
131 | } | |
132 | ||
d8a36e14 | 133 | if (!found_in_trace) { |
8b45963b | 134 | BT_ASSERT_PRE_MSG("A clock class found in the event notification's clock class priority map does not exist in the notification's event's trace: " |
546a0c4a PP |
135 | "notif-addr=%p, trace-addr=%p, " |
136 | "trace-name=\"%s\", cc-prio-map-addr=%p, " | |
137 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
839d52a5 | 138 | notif, trace, bt_trace_get_name(trace), |
546a0c4a | 139 | notif->cc_prio_map, clock_class, |
839d52a5 | 140 | bt_clock_class_get_name(clock_class)); |
c55a9f58 | 141 | is_valid = BT_FALSE; |
599faa1c PP |
142 | goto end; |
143 | } | |
1149be64 PP |
144 | |
145 | BT_PUT(clock_value); | |
146 | BT_PUT(clock_class); | |
599faa1c PP |
147 | } |
148 | ||
149 | end: | |
1149be64 PP |
150 | bt_put(clock_value); |
151 | bt_put(clock_class); | |
599faa1c PP |
152 | return is_valid; |
153 | } | |
154 | ||
8b45963b PP |
155 | BT_ASSERT_PRE_FUNC |
156 | static inline bool event_has_trace(struct bt_event *event) | |
a68c0f97 | 157 | { |
839d52a5 PP |
158 | struct bt_event_class *event_class; |
159 | struct bt_stream_class *stream_class; | |
a68c0f97 | 160 | |
8deee039 | 161 | event_class = bt_event_borrow_class(event); |
8b45963b | 162 | BT_ASSERT(event_class); |
839d52a5 | 163 | stream_class = bt_event_class_borrow_stream_class(event_class); |
8b45963b | 164 | BT_ASSERT(stream_class); |
839d52a5 | 165 | return bt_stream_class_borrow_trace(stream_class) != NULL; |
a68c0f97 PP |
166 | } |
167 | ||
839d52a5 | 168 | struct bt_notification *bt_notification_event_create(struct bt_event *event, |
599faa1c | 169 | struct bt_clock_class_priority_map *cc_prio_map) |
d51202da | 170 | { |
80f00602 | 171 | struct bt_notification_event *notification = NULL; |
839d52a5 | 172 | struct bt_event_class *event_class; |
546a0c4a | 173 | |
8b45963b | 174 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
d51202da | 175 | |
81512f1e PP |
176 | if (cc_prio_map) { |
177 | /* Function's reference, released at the end */ | |
178 | bt_get(cc_prio_map); | |
179 | } else { | |
180 | cc_prio_map = bt_clock_class_priority_map_create(); | |
181 | if (!cc_prio_map) { | |
182 | BT_LOGE_STR("Cannot create empty clock class priority map."); | |
183 | goto error; | |
184 | } | |
d51202da JG |
185 | } |
186 | ||
8b45963b | 187 | BT_ASSERT(cc_prio_map); |
8deee039 | 188 | event_class = bt_event_borrow_class(event); |
8b45963b | 189 | BT_ASSERT(event_class); |
546a0c4a PP |
190 | BT_LOGD("Creating event notification object: " |
191 | "event-addr=%p, event-class-addr=%p, " | |
192 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
193 | "cc-prio-map-addr=%p", | |
194 | event, event_class, | |
839d52a5 PP |
195 | bt_event_class_get_name(event_class), |
196 | bt_event_class_get_id(event_class), cc_prio_map); | |
546a0c4a | 197 | |
8b45963b PP |
198 | BT_ASSERT_PRE(bt_event_borrow_packet(event), |
199 | "Event has no packet: %!+e", event); | |
200 | BT_ASSERT_PRE(event_has_trace(event), | |
201 | "Event has no trace: %!+e", event); | |
d51202da | 202 | notification = g_new0(struct bt_notification_event, 1); |
24626e8b | 203 | if (!notification) { |
546a0c4a | 204 | BT_LOGE_STR("Failed to allocate one event notification."); |
24626e8b JG |
205 | goto error; |
206 | } | |
546a0c4a PP |
207 | |
208 | bt_notification_init(¬ification->parent, BT_NOTIFICATION_TYPE_EVENT, | |
209 | bt_notification_event_destroy); | |
d51202da | 210 | notification->event = bt_get(event); |
599faa1c | 211 | notification->cc_prio_map = bt_get(cc_prio_map); |
8b45963b PP |
212 | BT_ASSERT_PRE(validate_clock_classes(notification), |
213 | "Invalid clock classes: %![event-]+e", event); | |
546a0c4a | 214 | BT_LOGD_STR("Freezing event notification's event."); |
839d52a5 | 215 | bt_event_freeze(notification->event); |
546a0c4a | 216 | BT_LOGD_STR("Freezing event notification's clock class priority map."); |
fe650ea4 | 217 | bt_clock_class_priority_map_freeze(notification->cc_prio_map); |
546a0c4a PP |
218 | BT_LOGD("Created event notification object: " |
219 | "event-addr=%p, event-class-addr=%p, " | |
220 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
221 | "cc-prio-map-addr=%p, notif-addr=%p", | |
222 | event, event_class, | |
839d52a5 PP |
223 | bt_event_class_get_name(event_class), |
224 | bt_event_class_get_id(event_class), cc_prio_map, | |
546a0c4a | 225 | notification); |
3fd45dc7 | 226 | goto end; |
546a0c4a | 227 | |
d51202da | 228 | error: |
3fd45dc7 PP |
229 | BT_PUT(notification); |
230 | ||
231 | end: | |
81512f1e | 232 | bt_put(cc_prio_map); |
3fd45dc7 | 233 | return ¬ification->parent; |
d51202da JG |
234 | } |
235 | ||
839d52a5 | 236 | struct bt_event *bt_notification_event_get_event( |
d51202da JG |
237 | struct bt_notification *notification) |
238 | { | |
239 | struct bt_notification_event *event_notification; | |
240 | ||
8b45963b PP |
241 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
242 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT); | |
d51202da JG |
243 | event_notification = container_of(notification, |
244 | struct bt_notification_event, parent); | |
8b45963b | 245 | return bt_get(event_notification->event); |
d51202da | 246 | } |
599faa1c PP |
247 | |
248 | extern struct bt_clock_class_priority_map * | |
249 | bt_notification_event_get_clock_class_priority_map( | |
250 | struct bt_notification *notification) | |
251 | { | |
599faa1c PP |
252 | struct bt_notification_event *event_notification; |
253 | ||
8b45963b PP |
254 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
255 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT); | |
599faa1c PP |
256 | event_notification = container_of(notification, |
257 | struct bt_notification_event, parent); | |
8b45963b | 258 | return bt_get(event_notification->cc_prio_map); |
599faa1c | 259 | } |