Event notification: make sure contained event has a trace
[babeltrace.git] / lib / graph / notification / event.c
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
27 #include <babeltrace/compiler-internal.h>
28 #include <babeltrace/ctf-ir/event.h>
29 #include <babeltrace/ctf-ir/event-internal.h>
30 #include <babeltrace/ctf-ir/event-class-internal.h>
31 #include <babeltrace/ctf-ir/stream-class-internal.h>
32 #include <babeltrace/ctf-ir/trace.h>
33 #include <babeltrace/graph/clock-class-priority-map.h>
34 #include <babeltrace/graph/clock-class-priority-map-internal.h>
35 #include <babeltrace/graph/notification-event-internal.h>
36 #include <babeltrace/types.h>
37 #include <stdbool.h>
38
39 static
40 void bt_notification_event_destroy(struct bt_object *obj)
41 {
42 struct bt_notification_event *notification =
43 (struct bt_notification_event *) obj;
44
45 BT_PUT(notification->event);
46 BT_PUT(notification->cc_prio_map);
47 g_free(notification);
48 }
49
50 static
51 bt_bool validate_clock_classes(struct bt_notification_event *notif)
52 {
53 /*
54 * For each clock class found in the notification's clock class
55 * priority map, make sure the event has a clock value set for
56 * this clock class. Also make sure that those clock classes
57 * are part of the trace to which the event belongs.
58 */
59 bt_bool is_valid = BT_TRUE;
60 int trace_cc_count;
61 int cc_prio_map_cc_count;
62 size_t cc_prio_map_cc_i, trace_cc_i;
63 struct bt_ctf_event_class *event_class = NULL;
64 struct bt_ctf_stream_class *stream_class = NULL;
65 struct bt_ctf_trace *trace = NULL;
66
67 event_class = bt_ctf_event_borrow_event_class(notif->event);
68 assert(event_class);
69 stream_class = bt_ctf_event_class_borrow_stream_class(event_class);
70 assert(stream_class);
71 trace = bt_ctf_stream_class_borrow_trace(stream_class);
72 assert(trace);
73 trace_cc_count = bt_ctf_trace_get_clock_class_count(trace);
74 assert(trace_cc_count >= 0);
75 cc_prio_map_cc_count =
76 bt_clock_class_priority_map_get_clock_class_count(
77 notif->cc_prio_map);
78 assert(cc_prio_map_cc_count >= 0);
79
80 for (cc_prio_map_cc_i = 0; cc_prio_map_cc_i < cc_prio_map_cc_count;
81 cc_prio_map_cc_i++) {
82 struct bt_ctf_clock_class *clock_class =
83 bt_clock_class_priority_map_get_clock_class_by_index(
84 notif->cc_prio_map, cc_prio_map_cc_i);
85 struct bt_ctf_clock_value *clock_value;
86 bt_bool found_in_trace = BT_FALSE;
87
88 assert(clock_class);
89 clock_value = bt_ctf_event_get_clock_value(notif->event,
90 clock_class);
91 if (!clock_value) {
92 is_valid = BT_FALSE;
93 goto end;
94 }
95
96 bt_put(clock_value);
97
98 for (trace_cc_i = 0; trace_cc_i < trace_cc_count;
99 trace_cc_i++) {
100 struct bt_ctf_clock_class *trace_clock_class =
101 bt_ctf_trace_get_clock_class_by_index(trace,
102 trace_cc_i);
103
104 assert(trace_clock_class);
105
106 if (trace_clock_class == clock_class) {
107 found_in_trace = BT_TRUE;
108 break;
109 }
110 }
111
112 bt_put(clock_class);
113
114 if (!found_in_trace) {
115 is_valid = BT_FALSE;
116 goto end;
117 }
118 }
119
120 end:
121 return is_valid;
122 }
123
124 static
125 bool event_has_trace(struct bt_ctf_event *event)
126 {
127 struct bt_ctf_event_class *event_class;
128 struct bt_ctf_stream_class *stream_class;
129
130 event_class = bt_ctf_event_borrow_event_class(event);
131 assert(event_class);
132 stream_class = bt_ctf_event_class_borrow_stream_class(event_class);
133 assert(stream_class);
134 return bt_ctf_stream_class_borrow_trace(stream_class) != NULL;
135 }
136
137 struct bt_notification *bt_notification_event_create(struct bt_ctf_event *event,
138 struct bt_clock_class_priority_map *cc_prio_map)
139 {
140 struct bt_notification_event *notification = NULL;
141
142 if (!event || !cc_prio_map) {
143 goto error;
144 }
145
146 if (!bt_ctf_event_borrow_packet(event)) {
147 goto error;
148 }
149
150 if (!event_has_trace(event)) {
151 goto error;
152 }
153
154 notification = g_new0(struct bt_notification_event, 1);
155 if (!notification) {
156 goto error;
157 }
158 bt_notification_init(&notification->parent,
159 BT_NOTIFICATION_TYPE_EVENT,
160 bt_notification_event_destroy);
161 notification->event = bt_get(event);
162 notification->cc_prio_map = bt_get(cc_prio_map);
163 if (!validate_clock_classes(notification)) {
164 goto error;
165 }
166
167 bt_ctf_event_freeze(notification->event);
168 bt_clock_class_priority_map_freeze(notification->cc_prio_map);
169 return &notification->parent;
170 error:
171 bt_put(notification);
172 return NULL;
173 }
174
175 struct bt_ctf_event *bt_notification_event_get_event(
176 struct bt_notification *notification)
177 {
178 struct bt_ctf_event *event = NULL;
179 struct bt_notification_event *event_notification;
180
181 if (bt_notification_get_type(notification) !=
182 BT_NOTIFICATION_TYPE_EVENT) {
183 goto end;
184 }
185 event_notification = container_of(notification,
186 struct bt_notification_event, parent);
187 event = bt_get(event_notification->event);
188 end:
189 return event;
190 }
191
192 extern struct bt_clock_class_priority_map *
193 bt_notification_event_get_clock_class_priority_map(
194 struct bt_notification *notification)
195 {
196 struct bt_clock_class_priority_map *cc_prio_map = NULL;
197 struct bt_notification_event *event_notification;
198
199 if (bt_notification_get_type(notification) !=
200 BT_NOTIFICATION_TYPE_EVENT) {
201 goto end;
202 }
203
204 event_notification = container_of(notification,
205 struct bt_notification_event, parent);
206 cc_prio_map = bt_get(event_notification->cc_prio_map);
207 end:
208 return cc_prio_map;
209 }
This page took 0.035231 seconds and 5 git commands to generate.