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