lib: move bt_notification_iterator_next_return to component-class.h
[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
PP
35#include <babeltrace/ctf-ir/trace.h>
36#include <babeltrace/graph/clock-class-priority-map.h>
fe650ea4 37#include <babeltrace/graph/clock-class-priority-map-internal.h>
b2e0c907 38#include <babeltrace/graph/notification-event-internal.h>
c55a9f58 39#include <babeltrace/types.h>
a68c0f97 40#include <stdbool.h>
6072db50 41#include <inttypes.h>
d51202da
JG
42
43static
44void bt_notification_event_destroy(struct bt_object *obj)
45{
46 struct bt_notification_event *notification =
47 (struct bt_notification_event *) obj;
48
6072db50
PP
49 BT_LOGD("Destroying event notification: addr=%p", notification);
50 BT_LOGD_STR("Putting event.");
d51202da 51 BT_PUT(notification->event);
6072db50 52 BT_LOGD_STR("Putting clock class priority map.");
599faa1c 53 BT_PUT(notification->cc_prio_map);
d51202da
JG
54 g_free(notification);
55}
56
599faa1c 57static
c55a9f58 58bt_bool validate_clock_classes(struct bt_notification_event *notif)
599faa1c
PP
59{
60 /*
d8a36e14
PP
61 * For each clock class found in the notification's clock class
62 * priority map, make sure the event has a clock value set for
63 * this clock class. Also make sure that those clock classes
64 * are part of the trace to which the event belongs.
599faa1c 65 */
c55a9f58 66 bt_bool is_valid = BT_TRUE;
1149be64 67
d8a36e14
PP
68 int trace_cc_count;
69 int cc_prio_map_cc_count;
70 size_t cc_prio_map_cc_i, trace_cc_i;
1149be64
PP
71 struct bt_ctf_clock_value *clock_value = NULL;
72 struct bt_ctf_clock_class *clock_class = NULL;
599faa1c
PP
73 struct bt_ctf_event_class *event_class = NULL;
74 struct bt_ctf_stream_class *stream_class = NULL;
75 struct bt_ctf_trace *trace = NULL;
599faa1c 76
d8a36e14 77 event_class = bt_ctf_event_borrow_event_class(notif->event);
599faa1c 78 assert(event_class);
d8a36e14 79 stream_class = bt_ctf_event_class_borrow_stream_class(event_class);
599faa1c 80 assert(stream_class);
d8a36e14 81 trace = bt_ctf_stream_class_borrow_trace(stream_class);
599faa1c 82 assert(trace);
d8a36e14
PP
83 trace_cc_count = bt_ctf_trace_get_clock_class_count(trace);
84 assert(trace_cc_count >= 0);
85 cc_prio_map_cc_count =
86 bt_clock_class_priority_map_get_clock_class_count(
87 notif->cc_prio_map);
88 assert(cc_prio_map_cc_count >= 0);
599faa1c 89
d8a36e14
PP
90 for (cc_prio_map_cc_i = 0; cc_prio_map_cc_i < cc_prio_map_cc_count;
91 cc_prio_map_cc_i++) {
c55a9f58 92 bt_bool found_in_trace = BT_FALSE;
599faa1c 93
1149be64
PP
94 clock_class =
95 bt_clock_class_priority_map_get_clock_class_by_index(
96 notif->cc_prio_map, cc_prio_map_cc_i);
599faa1c 97 assert(clock_class);
d8a36e14
PP
98 clock_value = bt_ctf_event_get_clock_value(notif->event,
99 clock_class);
100 if (!clock_value) {
6072db50
PP
101 BT_LOGW("Event has no clock value for a clock class which exists in the notification's clock class priority map: "
102 "notif-addr=%p, event-addr=%p, "
103 "event-class-addr=%p, event-class-name=\"%s\", "
104 "event-class-id=%" PRId64 ", "
105 "cc-prio-map-addr=%p, "
106 "clock-class-addr=%p, clock-class-name=\"%s\"",
107 notif, notif->event, event_class,
108 bt_ctf_event_class_get_name(event_class),
109 bt_ctf_event_class_get_id(event_class),
110 notif->cc_prio_map, clock_class,
111 bt_ctf_clock_class_get_name(clock_class));
c55a9f58 112 is_valid = BT_FALSE;
d8a36e14
PP
113 goto end;
114 }
115
d8a36e14
PP
116 for (trace_cc_i = 0; trace_cc_i < trace_cc_count;
117 trace_cc_i++) {
118 struct bt_ctf_clock_class *trace_clock_class =
9ac68eb1
PP
119 bt_ctf_trace_get_clock_class_by_index(trace,
120 trace_cc_i);
d8a36e14
PP
121
122 assert(trace_clock_class);
1149be64 123 bt_put(trace_clock_class);
d8a36e14
PP
124
125 if (trace_clock_class == clock_class) {
c55a9f58 126 found_in_trace = BT_TRUE;
d8a36e14
PP
127 break;
128 }
129 }
130
d8a36e14 131 if (!found_in_trace) {
6072db50
PP
132 BT_LOGW("A clock class found in the event notification's clock class priority map does not exist in the notification's event's trace: "
133 "notif-addr=%p, trace-addr=%p, "
134 "trace-name=\"%s\", cc-prio-map-addr=%p, "
135 "clock-class-addr=%p, clock-class-name=\"%s\"",
136 notif, trace, bt_ctf_trace_get_name(trace),
137 notif->cc_prio_map, clock_class,
138 bt_ctf_clock_class_get_name(clock_class));
c55a9f58 139 is_valid = BT_FALSE;
599faa1c
PP
140 goto end;
141 }
1149be64
PP
142
143 BT_PUT(clock_value);
144 BT_PUT(clock_class);
599faa1c
PP
145 }
146
147end:
1149be64
PP
148 bt_put(clock_value);
149 bt_put(clock_class);
599faa1c
PP
150 return is_valid;
151}
152
a68c0f97
PP
153static
154bool event_has_trace(struct bt_ctf_event *event)
155{
156 struct bt_ctf_event_class *event_class;
157 struct bt_ctf_stream_class *stream_class;
158
159 event_class = bt_ctf_event_borrow_event_class(event);
160 assert(event_class);
161 stream_class = bt_ctf_event_class_borrow_stream_class(event_class);
162 assert(stream_class);
163 return bt_ctf_stream_class_borrow_trace(stream_class) != NULL;
164}
165
599faa1c
PP
166struct bt_notification *bt_notification_event_create(struct bt_ctf_event *event,
167 struct bt_clock_class_priority_map *cc_prio_map)
d51202da 168{
80f00602 169 struct bt_notification_event *notification = NULL;
6072db50
PP
170 struct bt_ctf_event_class *event_class;
171
172 if (!event) {
173 BT_LOGW_STR("Invalid parameter: event is NULL.");
174 goto error;
175 }
d51202da 176
6072db50
PP
177 if (!cc_prio_map) {
178 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
d51202da
JG
179 goto error;
180 }
181
6072db50
PP
182 event_class = bt_ctf_event_borrow_event_class(event);
183 assert(event_class);
184 BT_LOGD("Creating event notification object: "
185 "event-addr=%p, event-class-addr=%p, "
186 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
187 "cc-prio-map-addr=%p",
188 event, event_class,
189 bt_ctf_event_class_get_name(event_class),
190 bt_ctf_event_class_get_id(event_class), cc_prio_map);
191
80f00602 192 if (!bt_ctf_event_borrow_packet(event)) {
6072db50
PP
193 BT_LOGW("Invalid parameter: event has no packet: "
194 "event-addr=%p, event-class-addr=%p, "
195 "event-class-name=\"%s\", "
196 "event-class-id=%" PRId64,
197 event, event_class,
198 bt_ctf_event_class_get_name(event_class),
199 bt_ctf_event_class_get_id(event_class));
80f00602
PP
200 goto error;
201 }
202
a68c0f97 203 if (!event_has_trace(event)) {
6072db50
PP
204 BT_LOGW("Invalid parameter: event has no trace: "
205 "event-addr=%p, event-class-addr=%p, "
206 "event-class-name=\"%s\", "
207 "event-class-id=%" PRId64,
208 event, event_class,
209 bt_ctf_event_class_get_name(event_class),
210 bt_ctf_event_class_get_id(event_class));
a68c0f97
PP
211 goto error;
212 }
213
d51202da 214 notification = g_new0(struct bt_notification_event, 1);
24626e8b 215 if (!notification) {
6072db50 216 BT_LOGE_STR("Failed to allocate one event notification.");
24626e8b
JG
217 goto error;
218 }
6072db50
PP
219
220 bt_notification_init(&notification->parent, BT_NOTIFICATION_TYPE_EVENT,
221 bt_notification_event_destroy);
d51202da 222 notification->event = bt_get(event);
599faa1c 223 notification->cc_prio_map = bt_get(cc_prio_map);
599faa1c 224 if (!validate_clock_classes(notification)) {
6072db50
PP
225 BT_LOGW("Invalid event: invalid clock class: "
226 "event-addr=%p, event-class-addr=%p, "
227 "event-class-name=\"%s\", "
228 "event-class-id=%" PRId64,
229 event, event_class,
230 bt_ctf_event_class_get_name(event_class),
231 bt_ctf_event_class_get_id(event_class));
599faa1c
PP
232 goto error;
233 }
234
6072db50 235 BT_LOGD_STR("Freezing event notification's event.");
80f00602 236 bt_ctf_event_freeze(notification->event);
6072db50 237 BT_LOGD_STR("Freezing event notification's clock class priority map.");
fe650ea4 238 bt_clock_class_priority_map_freeze(notification->cc_prio_map);
6072db50
PP
239 BT_LOGD("Created event notification object: "
240 "event-addr=%p, event-class-addr=%p, "
241 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
242 "cc-prio-map-addr=%p, notif-addr=%p",
243 event, event_class,
244 bt_ctf_event_class_get_name(event_class),
245 bt_ctf_event_class_get_id(event_class), cc_prio_map,
246 notification);
d51202da 247 return &notification->parent;
6072db50 248
d51202da 249error:
80f00602 250 bt_put(notification);
d51202da
JG
251 return NULL;
252}
253
254struct bt_ctf_event *bt_notification_event_get_event(
255 struct bt_notification *notification)
256{
08bfb670 257 struct bt_ctf_event *event = NULL;
d51202da
JG
258 struct bt_notification_event *event_notification;
259
6072db50
PP
260 if (!notification) {
261 BT_LOGW_STR("Invalid parameter: notification is NULL.");
262 goto end;
263 }
264
08bfb670
JG
265 if (bt_notification_get_type(notification) !=
266 BT_NOTIFICATION_TYPE_EVENT) {
6072db50
PP
267 BT_LOGW("Invalid parameter: notification is not an event notification: "
268 "addr%p, notif-type=%s",
269 notification, bt_notification_type_string(
270 bt_notification_get_type(notification)));
08bfb670
JG
271 goto end;
272 }
6072db50 273
d51202da
JG
274 event_notification = container_of(notification,
275 struct bt_notification_event, parent);
08bfb670 276 event = bt_get(event_notification->event);
6072db50 277
08bfb670
JG
278end:
279 return event;
d51202da 280}
599faa1c
PP
281
282extern struct bt_clock_class_priority_map *
283bt_notification_event_get_clock_class_priority_map(
284 struct bt_notification *notification)
285{
286 struct bt_clock_class_priority_map *cc_prio_map = NULL;
287 struct bt_notification_event *event_notification;
288
6072db50
PP
289 if (!notification) {
290 BT_LOGW_STR("Invalid parameter: notification is NULL.");
291 goto end;
292 }
293
599faa1c
PP
294 if (bt_notification_get_type(notification) !=
295 BT_NOTIFICATION_TYPE_EVENT) {
6072db50
PP
296 BT_LOGW("Invalid parameter: notification is not an event notification: "
297 "addr%p, notif-type=%s",
298 notification, bt_notification_type_string(
299 bt_notification_get_type(notification)));
599faa1c
PP
300 goto end;
301 }
302
303 event_notification = container_of(notification,
304 struct bt_notification_event, parent);
305 cc_prio_map = bt_get(event_notification->cc_prio_map);
6072db50 306
599faa1c
PP
307end:
308 return cc_prio_map;
309}
This page took 0.046361 seconds and 4 git commands to generate.