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