Trace IR and notification APIs: split into private and public APIs
[babeltrace.git] / lib / graph / notification / event.c
1 /*
2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #define BT_LOG_TAG "NOTIF-EVENT"
26 #include <babeltrace/lib-logging-internal.h>
27
28 #include <babeltrace/compiler-internal.h>
29 #include <babeltrace/object-internal.h>
30 #include <babeltrace/trace-ir/event.h>
31 #include <babeltrace/trace-ir/event-internal.h>
32 #include <babeltrace/trace-ir/event-class-internal.h>
33 #include <babeltrace/trace-ir/stream-class-internal.h>
34 #include <babeltrace/trace-ir/trace.h>
35 #include <babeltrace/graph/graph-internal.h>
36 #include <babeltrace/graph/private-notification-event.h>
37 #include <babeltrace/graph/notification-event-internal.h>
38 #include <babeltrace/graph/private-connection-private-notification-iterator.h>
39 #include <babeltrace/types.h>
40 #include <babeltrace/assert-internal.h>
41 #include <babeltrace/assert-pre-internal.h>
42 #include <babeltrace/object.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_OBJECT_PUT_REF_AND_RESET(notification);
74
75 end:
76 return (void *) notification;
77 }
78
79 struct bt_private_notification *bt_private_notification_event_create(
80 struct bt_private_connection_private_notification_iterator *notif_iter,
81 struct bt_private_event_class *priv_event_class,
82 struct bt_private_packet *priv_packet)
83 {
84 struct bt_event_class *event_class = (void *) priv_event_class;
85 struct bt_packet *packet = (void *) priv_packet;
86 struct bt_notification_event *notification = NULL;
87 struct bt_event *event;
88 struct bt_graph *graph;
89
90 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
91 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
92 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
93 BT_LOGD("Creating event notification object: "
94 "event-class-addr=%p, "
95 "event-class-name=\"%s\", event-class-id=%" PRId64,
96 event_class,
97 bt_event_class_get_name(event_class),
98 bt_event_class_get_id(event_class));
99 BT_ASSERT_PRE(event_class_has_trace(event_class),
100 "Event class is not part of a trace: %!+E", event_class);
101 event = bt_event_create(event_class, packet);
102 if (unlikely(!event)) {
103 BT_LIB_LOGE("Cannot create event from event class: "
104 "%![event-class-]+E", event_class);
105 goto error;
106 }
107
108 /*
109 * Create notification from pool _after_ we have everything
110 * (in this case, a valid event object) so that we never have an
111 * error condition with a non-NULL notification object.
112 * Otherwise:
113 *
114 * * We cannot recycle the notification on error because
115 * bt_notification_event_recycle() expects a complete
116 * notification (and the event or clock class priority map
117 * object could be unset).
118 *
119 * * We cannot destroy the notification because we would need
120 * to notify the graph (pool owner) so that it removes the
121 * notification from its notification array.
122 */
123 graph = bt_private_connection_private_notification_iterator_borrow_graph(
124 notif_iter);
125 notification = (void *) bt_notification_create_from_pool(
126 &graph->event_notif_pool, graph);
127 if (unlikely(!notification)) {
128 /* bt_notification_create_from_pool() logs errors */
129 goto error;
130 }
131
132 BT_ASSERT(!notification->event);
133 notification->event = event;
134 bt_packet_set_is_frozen(packet, true);
135 bt_event_class_freeze(event_class);
136 BT_LOGD("Created event notification object: "
137 "event-addr=%p, event-class-addr=%p, "
138 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
139 "notif-addr=%p",
140 notification->event, event_class,
141 bt_event_class_get_name(event_class),
142 bt_event_class_get_id(event_class),
143 notification);
144 goto end;
145
146 error:
147 BT_ASSERT(!notification);
148 bt_event_destroy(event);
149
150 end:
151 return (void *) notification;
152 }
153
154 BT_HIDDEN
155 void bt_notification_event_destroy(struct bt_notification *notif)
156 {
157 struct bt_notification_event *event_notif = (void *) notif;
158
159 BT_LOGD("Destroying event notification: addr=%p", notif);
160
161 if (event_notif->event) {
162 BT_LOGD_STR("Recycling event.");
163 bt_event_recycle(event_notif->event);
164 }
165
166 g_free(notif);
167 }
168
169 BT_HIDDEN
170 void bt_notification_event_recycle(struct bt_notification *notif)
171 {
172 struct bt_notification_event *event_notif = (void *) notif;
173 struct bt_graph *graph;
174
175 BT_ASSERT(event_notif);
176
177 if (unlikely(!notif->graph)) {
178 bt_notification_event_destroy(notif);
179 return;
180 }
181
182 BT_LOGD("Recycling event notification: addr=%p", notif);
183 bt_notification_reset(notif);
184 BT_ASSERT(event_notif->event);
185 BT_LOGD_STR("Recycling event.");
186 bt_event_recycle(event_notif->event);
187 event_notif->event = NULL;
188 graph = notif->graph;
189 notif->graph = NULL;
190 bt_object_pool_recycle_object(&graph->event_notif_pool, notif);
191 }
192
193 struct bt_event *bt_notification_event_borrow_event(
194 struct bt_notification *notification)
195 {
196 struct bt_notification_event *event_notification;
197
198 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
199 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT);
200 event_notification = container_of(notification,
201 struct bt_notification_event, parent);
202 return event_notification->event;
203 }
204
205 struct bt_private_event *bt_private_notification_event_borrow_private_event(
206 struct bt_private_notification *notification)
207 {
208 return (void *) bt_notification_event_borrow_event(
209 (void *) notification);
210 }
This page took 0.033824 seconds and 4 git commands to generate.