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 | ||
6072db50 PP |
27 | #define BT_LOG_TAG "NOTIF-EVENT" |
28 | #include <babeltrace/lib-logging-internal.h> | |
29 | ||
3d9990ac | 30 | #include <babeltrace/compiler-internal.h> |
6c677fb5 | 31 | #include <babeltrace/object-internal.h> |
599faa1c | 32 | #include <babeltrace/ctf-ir/event.h> |
80f00602 | 33 | #include <babeltrace/ctf-ir/event-internal.h> |
d8a36e14 PP |
34 | #include <babeltrace/ctf-ir/event-class-internal.h> |
35 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
599faa1c | 36 | #include <babeltrace/ctf-ir/trace.h> |
5c563278 | 37 | #include <babeltrace/graph/graph-internal.h> |
b2e0c907 | 38 | #include <babeltrace/graph/notification-event-internal.h> |
c55a9f58 | 39 | #include <babeltrace/types.h> |
f6ccaed9 PP |
40 | #include <babeltrace/assert-internal.h> |
41 | #include <babeltrace/assert-pre-internal.h> | |
a68c0f97 | 42 | #include <stdbool.h> |
6072db50 | 43 | #include <inttypes.h> |
d51202da | 44 | |
f6ccaed9 | 45 | BT_ASSERT_PRE_FUNC |
312c056a | 46 | static inline bool event_class_has_trace(struct bt_event_class *event_class) |
a68c0f97 | 47 | { |
50842bdc | 48 | struct bt_stream_class *stream_class; |
a68c0f97 | 49 | |
50842bdc | 50 | stream_class = bt_event_class_borrow_stream_class(event_class); |
f6ccaed9 | 51 | BT_ASSERT(stream_class); |
50842bdc | 52 | return bt_stream_class_borrow_trace(stream_class) != NULL; |
a68c0f97 PP |
53 | } |
54 | ||
5c563278 PP |
55 | BT_HIDDEN |
56 | struct bt_notification *bt_notification_event_new(struct bt_graph *graph) | |
57 | { | |
58 | struct bt_notification_event *notification = NULL; | |
59 | ||
60 | notification = g_new0(struct bt_notification_event, 1); | |
61 | if (!notification) { | |
62 | BT_LOGE_STR("Failed to allocate one event notification."); | |
63 | goto error; | |
64 | } | |
65 | ||
66 | bt_notification_init(¬ification->parent, BT_NOTIFICATION_TYPE_EVENT, | |
67 | (bt_object_release_func) bt_notification_event_recycle, graph); | |
68 | goto end; | |
69 | ||
70 | error: | |
71 | BT_PUT(notification); | |
72 | ||
73 | end: | |
74 | return (void *) notification; | |
75 | } | |
76 | ||
312c056a | 77 | struct bt_notification *bt_notification_event_create( |
5c563278 | 78 | struct bt_graph *graph, |
312c056a | 79 | struct bt_event_class *event_class, |
e22b45d0 | 80 | struct bt_packet *packet) |
d51202da | 81 | { |
80f00602 | 82 | struct bt_notification_event *notification = NULL; |
6c677fb5 | 83 | struct bt_event *event; |
6072db50 | 84 | |
312c056a PP |
85 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
86 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); | |
6072db50 | 87 | BT_LOGD("Creating event notification object: " |
312c056a | 88 | "event-class-addr=%p, " |
e22b45d0 | 89 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
312c056a | 90 | event_class, |
50842bdc | 91 | bt_event_class_get_name(event_class), |
e22b45d0 | 92 | bt_event_class_get_id(event_class)); |
312c056a PP |
93 | BT_ASSERT_PRE(event_class_has_trace(event_class), |
94 | "Event class is not part of a trace: %!+E", event_class); | |
6c677fb5 PP |
95 | event = bt_event_create(event_class, packet); |
96 | if (unlikely(!event)) { | |
97 | BT_LIB_LOGE("Cannot create event from event class: " | |
98 | "%![event-class-]+E", event_class); | |
24626e8b JG |
99 | goto error; |
100 | } | |
6072db50 | 101 | |
6c677fb5 PP |
102 | /* |
103 | * Create notification from pool _after_ we have everything | |
104 | * (in this case, a valid event object) so that we never have an | |
105 | * error condition with a non-NULL notification object. | |
106 | * Otherwise: | |
107 | * | |
108 | * * We cannot recycle the notification on error because | |
109 | * bt_notification_event_recycle() expects a complete | |
110 | * notification (and the event or clock class priority map | |
111 | * object could be unset). | |
112 | * | |
113 | * * We cannot destroy the notification because we would need | |
114 | * to notify the graph (pool owner) so that it removes the | |
115 | * notification from its notification array. | |
116 | */ | |
117 | notification = (void *) bt_notification_create_from_pool( | |
118 | &graph->event_notif_pool, graph); | |
119 | if (unlikely(!notification)) { | |
120 | /* bt_notification_create_from_pool() logs errors */ | |
312c056a PP |
121 | goto error; |
122 | } | |
123 | ||
6c677fb5 PP |
124 | BT_ASSERT(!notification->event); |
125 | notification->event = event; | |
6072db50 PP |
126 | BT_LOGD("Created event notification object: " |
127 | "event-addr=%p, event-class-addr=%p, " | |
128 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
e22b45d0 | 129 | "notif-addr=%p", |
312c056a | 130 | notification->event, event_class, |
50842bdc | 131 | bt_event_class_get_name(event_class), |
e22b45d0 | 132 | bt_event_class_get_id(event_class), |
6072db50 | 133 | notification); |
d4629a98 | 134 | goto end; |
6072db50 | 135 | |
d51202da | 136 | error: |
6c677fb5 PP |
137 | BT_ASSERT(!notification); |
138 | bt_event_destroy(event); | |
d4629a98 PP |
139 | |
140 | end: | |
5c563278 PP |
141 | return (void *) notification; |
142 | } | |
143 | ||
144 | BT_HIDDEN | |
145 | void bt_notification_event_destroy(struct bt_notification *notif) | |
146 | { | |
147 | struct bt_notification_event *event_notif = (void *) notif; | |
148 | ||
149 | BT_LOGD("Destroying event notification: addr=%p", notif); | |
150 | ||
151 | if (event_notif->event) { | |
152 | BT_LOGD_STR("Recycling event."); | |
153 | bt_event_recycle(event_notif->event); | |
154 | } | |
155 | ||
5c563278 PP |
156 | g_free(notif); |
157 | } | |
158 | ||
159 | BT_HIDDEN | |
160 | void bt_notification_event_recycle(struct bt_notification *notif) | |
161 | { | |
162 | struct bt_notification_event *event_notif = (void *) notif; | |
163 | struct bt_graph *graph; | |
164 | ||
165 | BT_ASSERT(event_notif); | |
166 | ||
6c677fb5 | 167 | if (unlikely(!notif->graph)) { |
5c563278 PP |
168 | bt_notification_event_destroy(notif); |
169 | return; | |
170 | } | |
171 | ||
172 | BT_LOGD("Recycling event notification: addr=%p", notif); | |
173 | bt_notification_reset(notif); | |
6c677fb5 PP |
174 | BT_ASSERT(event_notif->event); |
175 | BT_LOGD_STR("Recycling event."); | |
176 | bt_event_recycle(event_notif->event); | |
177 | event_notif->event = NULL; | |
5c563278 PP |
178 | graph = notif->graph; |
179 | notif->graph = NULL; | |
180 | bt_object_pool_recycle_object(&graph->event_notif_pool, notif); | |
d51202da JG |
181 | } |
182 | ||
094ff7c0 | 183 | struct bt_event *bt_notification_event_borrow_event( |
d51202da JG |
184 | struct bt_notification *notification) |
185 | { | |
186 | struct bt_notification_event *event_notification; | |
187 | ||
f6ccaed9 PP |
188 | BT_ASSERT_PRE_NON_NULL(notification, "Notification"); |
189 | BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT); | |
d51202da JG |
190 | event_notification = container_of(notification, |
191 | struct bt_notification_event, parent); | |
094ff7c0 | 192 | return event_notification->event; |
d51202da | 193 | } |