2 * Babeltrace Plug-in Event Notification
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
27 #define BT_LOG_TAG "NOTIF-EVENT"
28 #include <babeltrace/lib-logging-internal.h>
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/ctf-ir/event.h>
32 #include <babeltrace/ctf-ir/event-internal.h>
33 #include <babeltrace/ctf-ir/event-class-internal.h>
34 #include <babeltrace/ctf-ir/stream-class-internal.h>
35 #include <babeltrace/ctf-ir/trace.h>
36 #include <babeltrace/graph/clock-class-priority-map.h>
37 #include <babeltrace/graph/clock-class-priority-map-internal.h>
38 #include <babeltrace/graph/notification-event-internal.h>
39 #include <babeltrace/types.h>
44 void bt_notification_event_destroy(struct bt_object
*obj
)
46 struct bt_notification_event
*notification
=
47 (struct bt_notification_event
*) obj
;
49 BT_LOGD("Destroying event notification: addr=%p", notification
);
50 BT_LOGD_STR("Putting event.");
51 BT_PUT(notification
->event
);
52 BT_LOGD_STR("Putting clock class priority map.");
53 BT_PUT(notification
->cc_prio_map
);
58 bt_bool
validate_clock_classes(struct bt_notification_event
*notif
)
61 * For each clock class found in the notification's clock class
62 * priority map, make sure the event has a clock value set for
63 * this clock class. Also make sure that those clock classes
64 * are part of the trace to which the event belongs.
66 bt_bool is_valid
= BT_TRUE
;
69 int cc_prio_map_cc_count
;
70 size_t cc_prio_map_cc_i
, trace_cc_i
;
71 struct bt_clock_value
*clock_value
= NULL
;
72 struct bt_clock_class
*clock_class
= NULL
;
73 struct bt_event_class
*event_class
= NULL
;
74 struct bt_stream_class
*stream_class
= NULL
;
75 struct bt_trace
*trace
= NULL
;
77 event_class
= bt_event_borrow_event_class(notif
->event
);
79 stream_class
= bt_event_class_borrow_stream_class(event_class
);
81 trace
= bt_stream_class_borrow_trace(stream_class
);
83 trace_cc_count
= bt_trace_get_clock_class_count(trace
);
84 assert(trace_cc_count
>= 0);
85 cc_prio_map_cc_count
=
86 bt_clock_class_priority_map_get_clock_class_count(
88 assert(cc_prio_map_cc_count
>= 0);
90 for (cc_prio_map_cc_i
= 0; cc_prio_map_cc_i
< cc_prio_map_cc_count
;
92 bt_bool found_in_trace
= BT_FALSE
;
95 bt_clock_class_priority_map_get_clock_class_by_index(
96 notif
->cc_prio_map
, cc_prio_map_cc_i
);
98 clock_value
= bt_event_get_clock_value(notif
->event
,
101 BT_LOGW("Event has no clock value for a clock class which exists in the notification's clock class priority map: "
102 "notif-addr=%p, event-addr=%p, "
103 "event-class-addr=%p, event-class-name=\"%s\", "
104 "event-class-id=%" PRId64
", "
105 "cc-prio-map-addr=%p, "
106 "clock-class-addr=%p, clock-class-name=\"%s\"",
107 notif
, notif
->event
, event_class
,
108 bt_event_class_get_name(event_class
),
109 bt_event_class_get_id(event_class
),
110 notif
->cc_prio_map
, clock_class
,
111 bt_clock_class_get_name(clock_class
));
116 for (trace_cc_i
= 0; trace_cc_i
< trace_cc_count
;
118 struct bt_clock_class
*trace_clock_class
=
119 bt_trace_get_clock_class_by_index(trace
,
122 assert(trace_clock_class
);
123 bt_put(trace_clock_class
);
125 if (trace_clock_class
== clock_class
) {
126 found_in_trace
= BT_TRUE
;
131 if (!found_in_trace
) {
132 BT_LOGW("A clock class found in the event notification's clock class priority map does not exist in the notification's event's trace: "
133 "notif-addr=%p, trace-addr=%p, "
134 "trace-name=\"%s\", cc-prio-map-addr=%p, "
135 "clock-class-addr=%p, clock-class-name=\"%s\"",
136 notif
, trace
, bt_trace_get_name(trace
),
137 notif
->cc_prio_map
, clock_class
,
138 bt_clock_class_get_name(clock_class
));
154 bool event_has_trace(struct bt_event
*event
)
156 struct bt_event_class
*event_class
;
157 struct bt_stream_class
*stream_class
;
159 event_class
= bt_event_borrow_event_class(event
);
161 stream_class
= bt_event_class_borrow_stream_class(event_class
);
162 assert(stream_class
);
163 return bt_stream_class_borrow_trace(stream_class
) != NULL
;
166 struct bt_notification
*bt_notification_event_create(struct bt_event
*event
,
167 struct bt_clock_class_priority_map
*cc_prio_map
)
169 struct bt_notification_event
*notification
= NULL
;
170 struct bt_event_class
*event_class
;
173 BT_LOGW_STR("Invalid parameter: event is NULL.");
178 /* Function's reference, released at the end */
181 cc_prio_map
= bt_clock_class_priority_map_create();
183 BT_LOGE_STR("Cannot create empty clock class priority map.");
189 event_class
= bt_event_borrow_event_class(event
);
191 BT_LOGD("Creating event notification object: "
192 "event-addr=%p, event-class-addr=%p, "
193 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
194 "cc-prio-map-addr=%p",
196 bt_event_class_get_name(event_class
),
197 bt_event_class_get_id(event_class
), cc_prio_map
);
199 if (!bt_event_borrow_packet(event
)) {
200 BT_LOGW("Invalid parameter: event has no packet: "
201 "event-addr=%p, event-class-addr=%p, "
202 "event-class-name=\"%s\", "
203 "event-class-id=%" PRId64
,
205 bt_event_class_get_name(event_class
),
206 bt_event_class_get_id(event_class
));
210 if (!event_has_trace(event
)) {
211 BT_LOGW("Invalid parameter: event has no trace: "
212 "event-addr=%p, event-class-addr=%p, "
213 "event-class-name=\"%s\", "
214 "event-class-id=%" PRId64
,
216 bt_event_class_get_name(event_class
),
217 bt_event_class_get_id(event_class
));
221 notification
= g_new0(struct bt_notification_event
, 1);
223 BT_LOGE_STR("Failed to allocate one event notification.");
227 bt_notification_init(¬ification
->parent
, BT_NOTIFICATION_TYPE_EVENT
,
228 bt_notification_event_destroy
);
229 notification
->event
= bt_get(event
);
230 notification
->cc_prio_map
= bt_get(cc_prio_map
);
231 if (!validate_clock_classes(notification
)) {
232 BT_LOGW("Invalid event: invalid clock class: "
233 "event-addr=%p, event-class-addr=%p, "
234 "event-class-name=\"%s\", "
235 "event-class-id=%" PRId64
,
237 bt_event_class_get_name(event_class
),
238 bt_event_class_get_id(event_class
));
242 BT_LOGD_STR("Freezing event notification's event.");
243 bt_event_freeze(notification
->event
);
244 BT_LOGD_STR("Freezing event notification's clock class priority map.");
245 bt_clock_class_priority_map_freeze(notification
->cc_prio_map
);
246 BT_LOGD("Created event notification object: "
247 "event-addr=%p, event-class-addr=%p, "
248 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
249 "cc-prio-map-addr=%p, notif-addr=%p",
251 bt_event_class_get_name(event_class
),
252 bt_event_class_get_id(event_class
), cc_prio_map
,
257 BT_PUT(notification
);
261 return ¬ification
->parent
;
264 struct bt_event
*bt_notification_event_get_event(
265 struct bt_notification
*notification
)
267 struct bt_event
*event
= NULL
;
268 struct bt_notification_event
*event_notification
;
271 BT_LOGW_STR("Invalid parameter: notification is NULL.");
275 if (bt_notification_get_type(notification
) !=
276 BT_NOTIFICATION_TYPE_EVENT
) {
277 BT_LOGW("Invalid parameter: notification is not an event notification: "
278 "addr%p, notif-type=%s",
279 notification
, bt_notification_type_string(
280 bt_notification_get_type(notification
)));
284 event_notification
= container_of(notification
,
285 struct bt_notification_event
, parent
);
286 event
= bt_get(event_notification
->event
);
292 extern struct bt_clock_class_priority_map
*
293 bt_notification_event_get_clock_class_priority_map(
294 struct bt_notification
*notification
)
296 struct bt_clock_class_priority_map
*cc_prio_map
= NULL
;
297 struct bt_notification_event
*event_notification
;
300 BT_LOGW_STR("Invalid parameter: notification is NULL.");
304 if (bt_notification_get_type(notification
) !=
305 BT_NOTIFICATION_TYPE_EVENT
) {
306 BT_LOGW("Invalid parameter: notification is not an event notification: "
307 "addr%p, notif-type=%s",
308 notification
, bt_notification_type_string(
309 bt_notification_get_type(notification
)));
313 event_notification
= container_of(notification
,
314 struct bt_notification_event
, parent
);
315 cc_prio_map
= bt_get(event_notification
->cc_prio_map
);