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> |
d51202da JG |
36 | |
37 | static | |
38 | void bt_notification_event_destroy(struct bt_object *obj) | |
39 | { | |
40 | struct bt_notification_event *notification = | |
41 | (struct bt_notification_event *) obj; | |
42 | ||
43 | BT_PUT(notification->event); | |
599faa1c | 44 | BT_PUT(notification->cc_prio_map); |
d51202da JG |
45 | g_free(notification); |
46 | } | |
47 | ||
599faa1c PP |
48 | static |
49 | bool validate_clock_classes(struct bt_notification_event *notif) | |
50 | { | |
51 | /* | |
d8a36e14 PP |
52 | * For each clock class found in the notification's clock class |
53 | * priority map, make sure the event has a clock value set for | |
54 | * this clock class. Also make sure that those clock classes | |
55 | * are part of the trace to which the event belongs. | |
599faa1c PP |
56 | */ |
57 | bool is_valid = true; | |
d8a36e14 PP |
58 | int trace_cc_count; |
59 | int cc_prio_map_cc_count; | |
60 | size_t cc_prio_map_cc_i, trace_cc_i; | |
599faa1c PP |
61 | struct bt_ctf_event_class *event_class = NULL; |
62 | struct bt_ctf_stream_class *stream_class = NULL; | |
63 | struct bt_ctf_trace *trace = NULL; | |
599faa1c | 64 | |
d8a36e14 | 65 | event_class = bt_ctf_event_borrow_event_class(notif->event); |
599faa1c | 66 | assert(event_class); |
d8a36e14 | 67 | stream_class = bt_ctf_event_class_borrow_stream_class(event_class); |
599faa1c | 68 | assert(stream_class); |
d8a36e14 | 69 | trace = bt_ctf_stream_class_borrow_trace(stream_class); |
599faa1c | 70 | assert(trace); |
d8a36e14 PP |
71 | trace_cc_count = bt_ctf_trace_get_clock_class_count(trace); |
72 | assert(trace_cc_count >= 0); | |
73 | cc_prio_map_cc_count = | |
74 | bt_clock_class_priority_map_get_clock_class_count( | |
75 | notif->cc_prio_map); | |
76 | assert(cc_prio_map_cc_count >= 0); | |
599faa1c | 77 | |
d8a36e14 PP |
78 | for (cc_prio_map_cc_i = 0; cc_prio_map_cc_i < cc_prio_map_cc_count; |
79 | cc_prio_map_cc_i++) { | |
599faa1c | 80 | struct bt_ctf_clock_class *clock_class = |
9ac68eb1 | 81 | bt_clock_class_priority_map_get_clock_class_by_index( |
d8a36e14 PP |
82 | notif->cc_prio_map, cc_prio_map_cc_i); |
83 | struct bt_ctf_clock_value *clock_value; | |
84 | bool found_in_trace = false; | |
599faa1c PP |
85 | |
86 | assert(clock_class); | |
d8a36e14 PP |
87 | clock_value = bt_ctf_event_get_clock_value(notif->event, |
88 | clock_class); | |
89 | if (!clock_value) { | |
90 | is_valid = false; | |
91 | goto end; | |
92 | } | |
93 | ||
94 | bt_put(clock_value); | |
95 | ||
96 | for (trace_cc_i = 0; trace_cc_i < trace_cc_count; | |
97 | trace_cc_i++) { | |
98 | struct bt_ctf_clock_class *trace_clock_class = | |
9ac68eb1 PP |
99 | bt_ctf_trace_get_clock_class_by_index(trace, |
100 | trace_cc_i); | |
d8a36e14 PP |
101 | |
102 | assert(trace_clock_class); | |
103 | ||
104 | if (trace_clock_class == clock_class) { | |
105 | found_in_trace = true; | |
106 | break; | |
107 | } | |
108 | } | |
109 | ||
599faa1c | 110 | bt_put(clock_class); |
d8a36e14 PP |
111 | |
112 | if (!found_in_trace) { | |
599faa1c PP |
113 | is_valid = false; |
114 | goto end; | |
115 | } | |
116 | } | |
117 | ||
118 | end: | |
599faa1c PP |
119 | return is_valid; |
120 | } | |
121 | ||
122 | struct bt_notification *bt_notification_event_create(struct bt_ctf_event *event, | |
123 | struct bt_clock_class_priority_map *cc_prio_map) | |
d51202da | 124 | { |
80f00602 | 125 | struct bt_notification_event *notification = NULL; |
d51202da | 126 | |
599faa1c | 127 | if (!event || !cc_prio_map) { |
d51202da JG |
128 | goto error; |
129 | } | |
130 | ||
80f00602 PP |
131 | if (!bt_ctf_event_borrow_packet(event)) { |
132 | goto error; | |
133 | } | |
134 | ||
d51202da | 135 | notification = g_new0(struct bt_notification_event, 1); |
24626e8b JG |
136 | if (!notification) { |
137 | goto error; | |
138 | } | |
d51202da JG |
139 | bt_notification_init(¬ification->parent, |
140 | BT_NOTIFICATION_TYPE_EVENT, | |
141 | bt_notification_event_destroy); | |
142 | notification->event = bt_get(event); | |
599faa1c | 143 | notification->cc_prio_map = bt_get(cc_prio_map); |
599faa1c | 144 | if (!validate_clock_classes(notification)) { |
599faa1c PP |
145 | goto error; |
146 | } | |
147 | ||
80f00602 | 148 | bt_ctf_event_freeze(notification->event); |
fe650ea4 | 149 | bt_clock_class_priority_map_freeze(notification->cc_prio_map); |
d51202da JG |
150 | return ¬ification->parent; |
151 | error: | |
80f00602 | 152 | bt_put(notification); |
d51202da JG |
153 | return NULL; |
154 | } | |
155 | ||
156 | struct bt_ctf_event *bt_notification_event_get_event( | |
157 | struct bt_notification *notification) | |
158 | { | |
08bfb670 | 159 | struct bt_ctf_event *event = NULL; |
d51202da JG |
160 | struct bt_notification_event *event_notification; |
161 | ||
08bfb670 JG |
162 | if (bt_notification_get_type(notification) != |
163 | BT_NOTIFICATION_TYPE_EVENT) { | |
164 | goto end; | |
165 | } | |
d51202da JG |
166 | event_notification = container_of(notification, |
167 | struct bt_notification_event, parent); | |
08bfb670 JG |
168 | event = bt_get(event_notification->event); |
169 | end: | |
170 | return event; | |
d51202da | 171 | } |
599faa1c PP |
172 | |
173 | extern struct bt_clock_class_priority_map * | |
174 | bt_notification_event_get_clock_class_priority_map( | |
175 | struct bt_notification *notification) | |
176 | { | |
177 | struct bt_clock_class_priority_map *cc_prio_map = NULL; | |
178 | struct bt_notification_event *event_notification; | |
179 | ||
180 | if (bt_notification_get_type(notification) != | |
181 | BT_NOTIFICATION_TYPE_EVENT) { | |
182 | goto end; | |
183 | } | |
184 | ||
185 | event_notification = container_of(notification, | |
186 | struct bt_notification_event, parent); | |
187 | cc_prio_map = bt_get(event_notification->cc_prio_map); | |
188 | end: | |
189 | return cc_prio_map; | |
190 | } |