Commit | Line | Data |
---|---|---|
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 | ||
3d9990ac | 27 | #include <babeltrace/compiler-internal.h> |
599faa1c | 28 | #include <babeltrace/ctf-ir/event.h> |
80f00602 | 29 | #include <babeltrace/ctf-ir/event-internal.h> |
d8a36e14 PP |
30 | #include <babeltrace/ctf-ir/event-class-internal.h> |
31 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
599faa1c PP |
32 | #include <babeltrace/ctf-ir/trace.h> |
33 | #include <babeltrace/graph/clock-class-priority-map.h> | |
fe650ea4 | 34 | #include <babeltrace/graph/clock-class-priority-map-internal.h> |
b2e0c907 | 35 | #include <babeltrace/graph/notification-event-internal.h> |
c55a9f58 | 36 | #include <babeltrace/types.h> |
a68c0f97 | 37 | #include <stdbool.h> |
d51202da JG |
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); | |
599faa1c | 46 | BT_PUT(notification->cc_prio_map); |
d51202da JG |
47 | g_free(notification); |
48 | } | |
49 | ||
599faa1c | 50 | static |
c55a9f58 | 51 | bt_bool validate_clock_classes(struct bt_notification_event *notif) |
599faa1c PP |
52 | { |
53 | /* | |
d8a36e14 PP |
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. | |
599faa1c | 58 | */ |
c55a9f58 | 59 | bt_bool is_valid = BT_TRUE; |
1149be64 | 60 | |
d8a36e14 PP |
61 | int trace_cc_count; |
62 | int cc_prio_map_cc_count; | |
63 | size_t cc_prio_map_cc_i, trace_cc_i; | |
1149be64 PP |
64 | struct bt_ctf_clock_value *clock_value = NULL; |
65 | struct bt_ctf_clock_class *clock_class = NULL; | |
599faa1c PP |
66 | struct bt_ctf_event_class *event_class = NULL; |
67 | struct bt_ctf_stream_class *stream_class = NULL; | |
68 | struct bt_ctf_trace *trace = NULL; | |
599faa1c | 69 | |
d8a36e14 | 70 | event_class = bt_ctf_event_borrow_event_class(notif->event); |
599faa1c | 71 | assert(event_class); |
d8a36e14 | 72 | stream_class = bt_ctf_event_class_borrow_stream_class(event_class); |
599faa1c | 73 | assert(stream_class); |
d8a36e14 | 74 | trace = bt_ctf_stream_class_borrow_trace(stream_class); |
599faa1c | 75 | assert(trace); |
d8a36e14 PP |
76 | trace_cc_count = bt_ctf_trace_get_clock_class_count(trace); |
77 | assert(trace_cc_count >= 0); | |
78 | cc_prio_map_cc_count = | |
79 | bt_clock_class_priority_map_get_clock_class_count( | |
80 | notif->cc_prio_map); | |
81 | assert(cc_prio_map_cc_count >= 0); | |
599faa1c | 82 | |
d8a36e14 PP |
83 | for (cc_prio_map_cc_i = 0; cc_prio_map_cc_i < cc_prio_map_cc_count; |
84 | cc_prio_map_cc_i++) { | |
c55a9f58 | 85 | bt_bool found_in_trace = BT_FALSE; |
599faa1c | 86 | |
1149be64 PP |
87 | clock_class = |
88 | bt_clock_class_priority_map_get_clock_class_by_index( | |
89 | notif->cc_prio_map, cc_prio_map_cc_i); | |
599faa1c | 90 | assert(clock_class); |
d8a36e14 PP |
91 | clock_value = bt_ctf_event_get_clock_value(notif->event, |
92 | clock_class); | |
93 | if (!clock_value) { | |
c55a9f58 | 94 | is_valid = BT_FALSE; |
d8a36e14 PP |
95 | goto end; |
96 | } | |
97 | ||
d8a36e14 PP |
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 = | |
9ac68eb1 PP |
101 | bt_ctf_trace_get_clock_class_by_index(trace, |
102 | trace_cc_i); | |
d8a36e14 PP |
103 | |
104 | assert(trace_clock_class); | |
1149be64 | 105 | bt_put(trace_clock_class); |
d8a36e14 PP |
106 | |
107 | if (trace_clock_class == clock_class) { | |
c55a9f58 | 108 | found_in_trace = BT_TRUE; |
d8a36e14 PP |
109 | break; |
110 | } | |
111 | } | |
112 | ||
d8a36e14 | 113 | if (!found_in_trace) { |
c55a9f58 | 114 | is_valid = BT_FALSE; |
599faa1c PP |
115 | goto end; |
116 | } | |
1149be64 PP |
117 | |
118 | BT_PUT(clock_value); | |
119 | BT_PUT(clock_class); | |
599faa1c PP |
120 | } |
121 | ||
122 | end: | |
1149be64 PP |
123 | bt_put(clock_value); |
124 | bt_put(clock_class); | |
599faa1c PP |
125 | return is_valid; |
126 | } | |
127 | ||
a68c0f97 PP |
128 | static |
129 | bool event_has_trace(struct bt_ctf_event *event) | |
130 | { | |
131 | struct bt_ctf_event_class *event_class; | |
132 | struct bt_ctf_stream_class *stream_class; | |
133 | ||
134 | event_class = bt_ctf_event_borrow_event_class(event); | |
135 | assert(event_class); | |
136 | stream_class = bt_ctf_event_class_borrow_stream_class(event_class); | |
137 | assert(stream_class); | |
138 | return bt_ctf_stream_class_borrow_trace(stream_class) != NULL; | |
139 | } | |
140 | ||
599faa1c PP |
141 | struct bt_notification *bt_notification_event_create(struct bt_ctf_event *event, |
142 | struct bt_clock_class_priority_map *cc_prio_map) | |
d51202da | 143 | { |
80f00602 | 144 | struct bt_notification_event *notification = NULL; |
d51202da | 145 | |
599faa1c | 146 | if (!event || !cc_prio_map) { |
d51202da JG |
147 | goto error; |
148 | } | |
149 | ||
80f00602 PP |
150 | if (!bt_ctf_event_borrow_packet(event)) { |
151 | goto error; | |
152 | } | |
153 | ||
a68c0f97 PP |
154 | if (!event_has_trace(event)) { |
155 | goto error; | |
156 | } | |
157 | ||
d51202da | 158 | notification = g_new0(struct bt_notification_event, 1); |
24626e8b JG |
159 | if (!notification) { |
160 | goto error; | |
161 | } | |
d51202da JG |
162 | bt_notification_init(¬ification->parent, |
163 | BT_NOTIFICATION_TYPE_EVENT, | |
164 | bt_notification_event_destroy); | |
165 | notification->event = bt_get(event); | |
599faa1c | 166 | notification->cc_prio_map = bt_get(cc_prio_map); |
599faa1c | 167 | if (!validate_clock_classes(notification)) { |
599faa1c PP |
168 | goto error; |
169 | } | |
170 | ||
80f00602 | 171 | bt_ctf_event_freeze(notification->event); |
fe650ea4 | 172 | bt_clock_class_priority_map_freeze(notification->cc_prio_map); |
d51202da JG |
173 | return ¬ification->parent; |
174 | error: | |
80f00602 | 175 | bt_put(notification); |
d51202da JG |
176 | return NULL; |
177 | } | |
178 | ||
179 | struct bt_ctf_event *bt_notification_event_get_event( | |
180 | struct bt_notification *notification) | |
181 | { | |
08bfb670 | 182 | struct bt_ctf_event *event = NULL; |
d51202da JG |
183 | struct bt_notification_event *event_notification; |
184 | ||
08bfb670 JG |
185 | if (bt_notification_get_type(notification) != |
186 | BT_NOTIFICATION_TYPE_EVENT) { | |
187 | goto end; | |
188 | } | |
d51202da JG |
189 | event_notification = container_of(notification, |
190 | struct bt_notification_event, parent); | |
08bfb670 JG |
191 | event = bt_get(event_notification->event); |
192 | end: | |
193 | return event; | |
d51202da | 194 | } |
599faa1c PP |
195 | |
196 | extern struct bt_clock_class_priority_map * | |
197 | bt_notification_event_get_clock_class_priority_map( | |
198 | struct bt_notification *notification) | |
199 | { | |
200 | struct bt_clock_class_priority_map *cc_prio_map = NULL; | |
201 | struct bt_notification_event *event_notification; | |
202 | ||
203 | if (bt_notification_get_type(notification) != | |
204 | BT_NOTIFICATION_TYPE_EVENT) { | |
205 | goto end; | |
206 | } | |
207 | ||
208 | event_notification = container_of(notification, | |
209 | struct bt_notification_event, parent); | |
210 | cc_prio_map = bt_get(event_notification->cc_prio_map); | |
211 | end: | |
212 | return cc_prio_map; | |
213 | } |