6251979d266a20f845334277302a1ea18077e725
[babeltrace.git] / lib / graph / notification / event.c
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
27 #define BT_LOG_TAG "NOTIF-EVENT"
28 #include <babeltrace/lib-logging-internal.h>
29
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/object-internal.h>
32 #include <babeltrace/ctf-ir/event.h>
33 #include <babeltrace/ctf-ir/event-internal.h>
34 #include <babeltrace/ctf-ir/event-class-internal.h>
35 #include <babeltrace/ctf-ir/stream-class-internal.h>
36 #include <babeltrace/ctf-ir/trace.h>
37 #include <babeltrace/graph/graph-internal.h>
38 #include <babeltrace/graph/notification-event-internal.h>
39 #include <babeltrace/graph/private-connection-private-notification-iterator.h>
40 #include <babeltrace/types.h>
41 #include <babeltrace/assert-internal.h>
42 #include <babeltrace/assert-pre-internal.h>
43 #include <stdbool.h>
44 #include <inttypes.h>
45
46 BT_ASSERT_PRE_FUNC
47 static inline bool event_class_has_trace(struct bt_event_class *event_class)
48 {
49 struct bt_stream_class *stream_class;
50
51 stream_class = bt_event_class_borrow_stream_class(event_class);
52 BT_ASSERT(stream_class);
53 return bt_stream_class_borrow_trace(stream_class) != NULL;
54 }
55
56 BT_HIDDEN
57 struct bt_notification *bt_notification_event_new(
58 struct bt_graph *graph)
59 {
60 struct bt_notification_event *notification = NULL;
61
62 notification = g_new0(struct bt_notification_event, 1);
63 if (!notification) {
64 BT_LOGE_STR("Failed to allocate one event notification.");
65 goto error;
66 }
67
68 bt_notification_init(&notification->parent, BT_NOTIFICATION_TYPE_EVENT,
69 (bt_object_release_func) bt_notification_event_recycle, graph);
70 goto end;
71
72 error:
73 BT_PUT(notification);
74
75 end:
76 return (void *) notification;
77 }
78
79 struct bt_notification *bt_notification_event_create(
80 struct bt_private_connection_private_notification_iterator *notif_iter,
81 struct bt_event_class *event_class,
82 struct bt_packet *packet)
83 {
84 struct bt_notification_event *notification = NULL;
85 struct bt_event *event;
86 struct bt_graph *graph;
87 int ret;
88
89 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
90 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
91 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
92 BT_LOGD("Creating event notification object: "
93 "event-class-addr=%p, "
94 "event-class-name=\"%s\", event-class-id=%" PRId64,
95 event_class,
96 bt_event_class_get_name(event_class),
97 bt_event_class_get_id(event_class));
98 BT_ASSERT_PRE(event_class_has_trace(event_class),
99 "Event class is not part of a trace: %!+E", event_class);
100 event = bt_event_create(event_class, packet);
101 if (unlikely(!event)) {
102 BT_LIB_LOGE("Cannot create event from event class: "
103 "%![event-class-]+E", event_class);
104 goto error;
105 }
106
107 /*
108 * Set packet's properties. This can fail so it happens before
109 * creating the notification below. We freeze it after we know
110 * this function succeeds.
111 */
112 if (unlikely(!packet->props_are_set)) {
113 ret = bt_packet_set_properties(packet);
114 if (ret) {
115 BT_LIB_LOGE("Cannot update packet's properties: "
116 "%![prev-packet-]+a", packet);
117 goto error;
118 }
119 }
120
121 /*
122 * Create notification from pool _after_ we have everything
123 * (in this case, a valid event object) so that we never have an
124 * error condition with a non-NULL notification object.
125 * Otherwise:
126 *
127 * * We cannot recycle the notification on error because
128 * bt_notification_event_recycle() expects a complete
129 * notification (and the event or clock class priority map
130 * object could be unset).
131 *
132 * * We cannot destroy the notification because we would need
133 * to notify the graph (pool owner) so that it removes the
134 * notification from its notification array.
135 */
136 graph = bt_private_connection_private_notification_iterator_borrow_graph(
137 notif_iter);
138 notification = (void *) bt_notification_create_from_pool(
139 &graph->event_notif_pool, graph);
140 if (unlikely(!notification)) {
141 /* bt_notification_create_from_pool() logs errors */
142 goto error;
143 }
144
145 BT_ASSERT(!notification->event);
146 notification->event = event;
147 bt_packet_set_is_frozen(packet, true);
148 bt_packet_validate_properties(packet);
149 BT_LOGD("Created event notification object: "
150 "event-addr=%p, event-class-addr=%p, "
151 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
152 "notif-addr=%p",
153 notification->event, event_class,
154 bt_event_class_get_name(event_class),
155 bt_event_class_get_id(event_class),
156 notification);
157 goto end;
158
159 error:
160 BT_ASSERT(!notification);
161 bt_event_destroy(event);
162
163 end:
164 return (void *) notification;
165 }
166
167 BT_HIDDEN
168 void bt_notification_event_destroy(struct bt_notification *notif)
169 {
170 struct bt_notification_event *event_notif = (void *) notif;
171
172 BT_LOGD("Destroying event notification: addr=%p", notif);
173
174 if (event_notif->event) {
175 BT_LOGD_STR("Recycling event.");
176 bt_event_recycle(event_notif->event);
177 }
178
179 g_free(notif);
180 }
181
182 BT_HIDDEN
183 void bt_notification_event_recycle(struct bt_notification *notif)
184 {
185 struct bt_notification_event *event_notif = (void *) notif;
186 struct bt_graph *graph;
187
188 BT_ASSERT(event_notif);
189
190 if (unlikely(!notif->graph)) {
191 bt_notification_event_destroy(notif);
192 return;
193 }
194
195 BT_LOGD("Recycling event notification: addr=%p", notif);
196 bt_notification_reset(notif);
197 BT_ASSERT(event_notif->event);
198 BT_LOGD_STR("Recycling event.");
199 bt_event_recycle(event_notif->event);
200 event_notif->event = NULL;
201 graph = notif->graph;
202 notif->graph = NULL;
203 bt_object_pool_recycle_object(&graph->event_notif_pool, notif);
204 }
205
206 struct bt_event *bt_notification_event_borrow_event(
207 struct bt_notification *notification)
208 {
209 struct bt_notification_event *event_notification;
210
211 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
212 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT);
213 event_notification = container_of(notification,
214 struct bt_notification_event, parent);
215 return event_notification->event;
216 }
This page took 0.033882 seconds and 3 git commands to generate.