Make API CTF-agnostic
[babeltrace.git] / lib / graph / notification / event.c
CommitLineData
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>
f0010051 39#include <babeltrace/graph/private-connection-private-notification-iterator.h>
c55a9f58 40#include <babeltrace/types.h>
f6ccaed9
PP
41#include <babeltrace/assert-internal.h>
42#include <babeltrace/assert-pre-internal.h>
a68c0f97 43#include <stdbool.h>
6072db50 44#include <inttypes.h>
d51202da 45
f6ccaed9 46BT_ASSERT_PRE_FUNC
312c056a 47static inline bool event_class_has_trace(struct bt_event_class *event_class)
a68c0f97 48{
50842bdc 49 struct bt_stream_class *stream_class;
a68c0f97 50
50842bdc 51 stream_class = bt_event_class_borrow_stream_class(event_class);
f6ccaed9 52 BT_ASSERT(stream_class);
50842bdc 53 return bt_stream_class_borrow_trace(stream_class) != NULL;
a68c0f97
PP
54}
55
5c563278 56BT_HIDDEN
f0010051
PP
57struct bt_notification *bt_notification_event_new(
58 struct bt_graph *graph)
5c563278
PP
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
72error:
73 BT_PUT(notification);
74
75end:
76 return (void *) notification;
77}
78
312c056a 79struct bt_notification *bt_notification_event_create(
f0010051 80 struct bt_private_connection_private_notification_iterator *notif_iter,
312c056a 81 struct bt_event_class *event_class,
e22b45d0 82 struct bt_packet *packet)
d51202da 83{
80f00602 84 struct bt_notification_event *notification = NULL;
6c677fb5 85 struct bt_event *event;
f0010051 86 struct bt_graph *graph;
6072db50 87
f0010051 88 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
312c056a
PP
89 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
90 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
6072db50 91 BT_LOGD("Creating event notification object: "
312c056a 92 "event-class-addr=%p, "
e22b45d0 93 "event-class-name=\"%s\", event-class-id=%" PRId64,
312c056a 94 event_class,
50842bdc 95 bt_event_class_get_name(event_class),
e22b45d0 96 bt_event_class_get_id(event_class));
312c056a
PP
97 BT_ASSERT_PRE(event_class_has_trace(event_class),
98 "Event class is not part of a trace: %!+E", event_class);
6c677fb5
PP
99 event = bt_event_create(event_class, packet);
100 if (unlikely(!event)) {
101 BT_LIB_LOGE("Cannot create event from event class: "
102 "%![event-class-]+E", event_class);
24626e8b
JG
103 goto error;
104 }
6072db50 105
6c677fb5
PP
106 /*
107 * Create notification from pool _after_ we have everything
108 * (in this case, a valid event object) so that we never have an
109 * error condition with a non-NULL notification object.
110 * Otherwise:
111 *
112 * * We cannot recycle the notification on error because
113 * bt_notification_event_recycle() expects a complete
114 * notification (and the event or clock class priority map
115 * object could be unset).
116 *
117 * * We cannot destroy the notification because we would need
118 * to notify the graph (pool owner) so that it removes the
119 * notification from its notification array.
120 */
f0010051
PP
121 graph = bt_private_connection_private_notification_iterator_borrow_graph(
122 notif_iter);
6c677fb5
PP
123 notification = (void *) bt_notification_create_from_pool(
124 &graph->event_notif_pool, graph);
125 if (unlikely(!notification)) {
126 /* bt_notification_create_from_pool() logs errors */
312c056a
PP
127 goto error;
128 }
129
6c677fb5
PP
130 BT_ASSERT(!notification->event);
131 notification->event = event;
ccf82993 132 bt_packet_set_is_frozen(packet, true);
44c440bc 133 bt_event_class_freeze(event_class);
6072db50
PP
134 BT_LOGD("Created event notification object: "
135 "event-addr=%p, event-class-addr=%p, "
136 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
e22b45d0 137 "notif-addr=%p",
312c056a 138 notification->event, event_class,
50842bdc 139 bt_event_class_get_name(event_class),
e22b45d0 140 bt_event_class_get_id(event_class),
6072db50 141 notification);
d4629a98 142 goto end;
6072db50 143
d51202da 144error:
6c677fb5
PP
145 BT_ASSERT(!notification);
146 bt_event_destroy(event);
d4629a98
PP
147
148end:
5c563278
PP
149 return (void *) notification;
150}
151
152BT_HIDDEN
153void bt_notification_event_destroy(struct bt_notification *notif)
154{
155 struct bt_notification_event *event_notif = (void *) notif;
156
157 BT_LOGD("Destroying event notification: addr=%p", notif);
158
159 if (event_notif->event) {
160 BT_LOGD_STR("Recycling event.");
161 bt_event_recycle(event_notif->event);
162 }
163
5c563278
PP
164 g_free(notif);
165}
166
167BT_HIDDEN
168void bt_notification_event_recycle(struct bt_notification *notif)
169{
170 struct bt_notification_event *event_notif = (void *) notif;
171 struct bt_graph *graph;
172
173 BT_ASSERT(event_notif);
174
6c677fb5 175 if (unlikely(!notif->graph)) {
5c563278
PP
176 bt_notification_event_destroy(notif);
177 return;
178 }
179
180 BT_LOGD("Recycling event notification: addr=%p", notif);
181 bt_notification_reset(notif);
6c677fb5
PP
182 BT_ASSERT(event_notif->event);
183 BT_LOGD_STR("Recycling event.");
184 bt_event_recycle(event_notif->event);
185 event_notif->event = NULL;
5c563278
PP
186 graph = notif->graph;
187 notif->graph = NULL;
188 bt_object_pool_recycle_object(&graph->event_notif_pool, notif);
d51202da
JG
189}
190
094ff7c0 191struct bt_event *bt_notification_event_borrow_event(
d51202da
JG
192 struct bt_notification *notification)
193{
194 struct bt_notification_event *event_notification;
195
f6ccaed9
PP
196 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
197 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT);
d51202da
JG
198 event_notification = container_of(notification,
199 struct bt_notification_event, parent);
094ff7c0 200 return event_notification->event;
d51202da 201}
This page took 0.046529 seconds and 4 git commands to generate.