lib: use object pool for event and packet notifications
[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>
599faa1c 31#include <babeltrace/ctf-ir/event.h>
80f00602 32#include <babeltrace/ctf-ir/event-internal.h>
d8a36e14
PP
33#include <babeltrace/ctf-ir/event-class-internal.h>
34#include <babeltrace/ctf-ir/stream-class-internal.h>
599faa1c 35#include <babeltrace/ctf-ir/trace.h>
5c563278 36#include <babeltrace/graph/graph-internal.h>
599faa1c 37#include <babeltrace/graph/clock-class-priority-map.h>
fe650ea4 38#include <babeltrace/graph/clock-class-priority-map-internal.h>
b2e0c907 39#include <babeltrace/graph/notification-event-internal.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
PP
56BT_HIDDEN
57struct bt_notification *bt_notification_event_new(struct bt_graph *graph)
58{
59 struct bt_notification_event *notification = NULL;
60
61 notification = g_new0(struct bt_notification_event, 1);
62 if (!notification) {
63 BT_LOGE_STR("Failed to allocate one event notification.");
64 goto error;
65 }
66
67 bt_notification_init(&notification->parent, BT_NOTIFICATION_TYPE_EVENT,
68 (bt_object_release_func) bt_notification_event_recycle, graph);
69 goto end;
70
71error:
72 BT_PUT(notification);
73
74end:
75 return (void *) notification;
76}
77
312c056a 78struct bt_notification *bt_notification_event_create(
5c563278 79 struct bt_graph *graph,
312c056a
PP
80 struct bt_event_class *event_class,
81 struct bt_packet *packet,
599faa1c 82 struct bt_clock_class_priority_map *cc_prio_map)
d51202da 83{
80f00602 84 struct bt_notification_event *notification = NULL;
6072db50 85
312c056a
PP
86 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
87 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
d51202da 88
bbc83bc9
PP
89 if (cc_prio_map) {
90 /* Function's reference, released at the end */
91 bt_get(cc_prio_map);
92 } else {
93 cc_prio_map = bt_clock_class_priority_map_create();
94 if (!cc_prio_map) {
95 BT_LOGE_STR("Cannot create empty clock class priority map.");
96 goto error;
97 }
d51202da
JG
98 }
99
f6ccaed9 100 BT_ASSERT(cc_prio_map);
6072db50 101 BT_LOGD("Creating event notification object: "
312c056a 102 "event-class-addr=%p, "
6072db50
PP
103 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
104 "cc-prio-map-addr=%p",
312c056a 105 event_class,
50842bdc
PP
106 bt_event_class_get_name(event_class),
107 bt_event_class_get_id(event_class), cc_prio_map);
6072db50 108
312c056a
PP
109 BT_ASSERT_PRE(event_class_has_trace(event_class),
110 "Event class is not part of a trace: %!+E", event_class);
5c563278
PP
111 notification = (void *) bt_notification_create_from_pool(
112 &graph->event_notif_pool, graph);
24626e8b 113 if (!notification) {
5c563278 114 /* bt_notification_create_from_pool() logs errors */
24626e8b
JG
115 goto error;
116 }
6072db50 117
312c056a
PP
118 notification->event = bt_event_create(event_class, packet);
119 if (!notification->event) {
120 BT_LIB_LOGE("Cannot create event from event class: "
121 "%![event-class-]+E", event_class);
122 goto error;
123 }
124
599faa1c 125 notification->cc_prio_map = bt_get(cc_prio_map);
6072db50 126 BT_LOGD_STR("Freezing event notification's clock class priority map.");
fe650ea4 127 bt_clock_class_priority_map_freeze(notification->cc_prio_map);
6072db50
PP
128 BT_LOGD("Created event notification object: "
129 "event-addr=%p, event-class-addr=%p, "
130 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
131 "cc-prio-map-addr=%p, notif-addr=%p",
312c056a 132 notification->event, event_class,
50842bdc
PP
133 bt_event_class_get_name(event_class),
134 bt_event_class_get_id(event_class), cc_prio_map,
6072db50 135 notification);
d4629a98 136 goto end;
6072db50 137
d51202da 138error:
d4629a98
PP
139 BT_PUT(notification);
140
141end:
bbc83bc9 142 bt_put(cc_prio_map);
5c563278
PP
143 return (void *) notification;
144}
145
146BT_HIDDEN
147void bt_notification_event_destroy(struct bt_notification *notif)
148{
149 struct bt_notification_event *event_notif = (void *) notif;
150
151 BT_LOGD("Destroying event notification: addr=%p", notif);
152
153 if (event_notif->event) {
154 BT_LOGD_STR("Recycling event.");
155 bt_event_recycle(event_notif->event);
156 }
157
158 BT_LOGD_STR("Putting clock class priority map.");
159 BT_PUT(event_notif->cc_prio_map);
160 g_free(notif);
161}
162
163BT_HIDDEN
164void bt_notification_event_recycle(struct bt_notification *notif)
165{
166 struct bt_notification_event *event_notif = (void *) notif;
167 struct bt_graph *graph;
168
169 BT_ASSERT(event_notif);
170
171 if (!notif->graph) {
172 bt_notification_event_destroy(notif);
173 return;
174 }
175
176 BT_LOGD("Recycling event notification: addr=%p", notif);
177 bt_notification_reset(notif);
178
179 if (event_notif->event) {
180 BT_LOGD_STR("Recycling event.");
181 bt_event_recycle(event_notif->event);
182 event_notif->event = NULL;
183 }
184
185 BT_PUT(event_notif->cc_prio_map);
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}
599faa1c
PP
202
203extern struct bt_clock_class_priority_map *
094ff7c0 204bt_notification_event_borrow_clock_class_priority_map(
599faa1c
PP
205 struct bt_notification *notification)
206{
599faa1c
PP
207 struct bt_notification_event *event_notification;
208
f6ccaed9
PP
209 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
210 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT);
599faa1c
PP
211 event_notification = container_of(notification,
212 struct bt_notification_event, parent);
094ff7c0 213 return event_notification->cc_prio_map;
599faa1c 214}
This page took 0.047437 seconds and 4 git commands to generate.