Commit | Line | Data |
---|---|---|
358340ec PP |
1 | /* |
2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #include <babeltrace/plugin/plugin-dev.h> | |
24 | #include <babeltrace/graph/connection.h> | |
25 | #include <babeltrace/graph/component.h> | |
26 | #include <babeltrace/graph/private-component.h> | |
27 | #include <babeltrace/graph/private-component-sink.h> | |
28 | #include <babeltrace/graph/private-port.h> | |
29 | #include <babeltrace/graph/port.h> | |
30 | #include <babeltrace/graph/private-connection.h> | |
31 | #include <babeltrace/graph/component-sink.h> | |
32 | #include <babeltrace/graph/notification-iterator.h> | |
33 | #include <babeltrace/graph/notification.h> | |
34 | #include <babeltrace/graph/notification-discarded-events.h> | |
35 | #include <babeltrace/graph/notification-discarded-packets.h> | |
36 | #include <babeltrace/babeltrace-internal.h> | |
37 | #include <babeltrace/common-internal.h> | |
38 | #include <babeltrace/values.h> | |
39 | #include <plugins-common.h> | |
40 | #include <assert.h> | |
41 | #include <inttypes.h> | |
42 | #include <stdint.h> | |
43 | ||
44 | #include "counter.h" | |
45 | ||
46 | #define PRINTF_COUNT(_what_sing, _what_plur, _var, args...) \ | |
47 | do { \ | |
48 | if (counter->count._var != 0 || !counter->hide_zero) { \ | |
49 | printf("%15" PRIu64 " %s\n", \ | |
50 | counter->count._var, \ | |
51 | counter->count._var == 1 ? _what_sing : _what_plur); \ | |
52 | } \ | |
53 | } while (0) | |
54 | ||
55 | static | |
56 | uint64_t get_total_count(struct counter *counter) | |
57 | { | |
58 | return counter->count.event + | |
59 | counter->count.stream_begin + | |
60 | counter->count.stream_end + | |
61 | counter->count.packet_begin + | |
62 | counter->count.packet_end + | |
63 | counter->count.inactivity + | |
64 | counter->count.discarded_events + | |
65 | counter->count.discarded_packets + | |
66 | counter->count.other; | |
67 | } | |
68 | ||
69 | static | |
70 | void print_count(struct counter *counter, uint64_t total) | |
71 | { | |
72 | PRINTF_COUNT("event", "events", event); | |
73 | PRINTF_COUNT("stream beginning", "stream beginnings", stream_begin); | |
74 | PRINTF_COUNT("stream end", "stream ends", stream_end); | |
75 | PRINTF_COUNT("packet beginning", "packet beginnings", packet_begin); | |
76 | PRINTF_COUNT("packet end", "packet ends", packet_end); | |
77 | PRINTF_COUNT("inactivity", "inactivities", inactivity); | |
78 | PRINTF_COUNT("discarded events notification", | |
79 | "discarded events notifications", discarded_events_notifs); | |
80 | PRINTF_COUNT(" known discarded event", " known discarded events", | |
81 | discarded_events); | |
82 | PRINTF_COUNT("discarded packets notification", | |
83 | "discarded packets notifications", discarded_packets_notifs); | |
84 | PRINTF_COUNT(" known discarded packet", " known discarded packets", | |
85 | discarded_packets); | |
86 | ||
87 | if (counter->count.other > 0) { | |
88 | PRINTF_COUNT(" other (unknown) notification", | |
89 | " other (unknown) notifications", other); | |
90 | } | |
91 | ||
92 | printf("%s%15" PRIu64 " notification%s (TOTAL)%s\n", | |
93 | bt_common_color_bold(), total, total == 1 ? "" : "s", | |
94 | bt_common_color_reset()); | |
95 | counter->last_printed_total = total; | |
96 | } | |
97 | ||
98 | static | |
99 | void try_print_count(struct counter *counter) | |
100 | { | |
101 | uint64_t total; | |
102 | ||
103 | if (counter->step == 0) { | |
104 | /* No update */ | |
105 | return; | |
106 | } | |
107 | ||
108 | total = get_total_count(counter); | |
109 | ||
110 | if (total % counter->step == 0) { | |
111 | print_count(counter, total); | |
112 | putchar('\n'); | |
113 | } | |
114 | } | |
115 | ||
116 | static | |
117 | void try_print_last(struct counter *counter) | |
118 | { | |
119 | const uint64_t total = get_total_count(counter); | |
120 | ||
121 | if (total != counter->last_printed_total) { | |
122 | print_count(counter, total); | |
123 | } | |
124 | } | |
125 | ||
126 | void destroy_private_counter_data(struct counter *counter) | |
127 | { | |
128 | bt_put(counter->notif_iter); | |
129 | g_free(counter); | |
130 | } | |
131 | ||
132 | void counter_finalize(struct bt_private_component *component) | |
133 | { | |
134 | struct counter *counter; | |
135 | ||
136 | assert(component); | |
137 | counter = bt_private_component_get_user_data(component); | |
138 | assert(counter); | |
139 | try_print_last(counter); | |
140 | bt_put(counter->notif_iter); | |
141 | g_free(counter); | |
142 | } | |
143 | ||
144 | enum bt_component_status counter_init(struct bt_private_component *component, | |
145 | struct bt_value *params, UNUSED_VAR void *init_method_data) | |
146 | { | |
147 | enum bt_component_status ret; | |
148 | struct counter *counter = g_new0(struct counter, 1); | |
149 | struct bt_value *step = NULL; | |
150 | struct bt_value *hide_zero = NULL; | |
151 | ||
152 | if (!counter) { | |
153 | ret = BT_COMPONENT_STATUS_NOMEM; | |
154 | goto end; | |
155 | } | |
156 | ||
157 | ret = bt_private_component_sink_add_input_private_port(component, | |
158 | "in", NULL, NULL); | |
159 | if (ret != BT_COMPONENT_STATUS_OK) { | |
160 | goto end; | |
161 | } | |
162 | ||
163 | counter->last_printed_total = -1ULL; | |
164 | counter->step = 1000; | |
165 | step = bt_value_map_get(params, "step"); | |
166 | if (bt_value_is_integer(step)) { | |
167 | int64_t val; | |
168 | int vret = bt_value_integer_get(step, &val); | |
169 | ||
170 | assert(vret == 0); | |
171 | ||
172 | if (val >= 0) { | |
173 | counter->step = (uint64_t) val; | |
174 | } | |
175 | } | |
176 | ||
177 | hide_zero = bt_value_map_get(params, "hide-zero"); | |
178 | if (bt_value_is_bool(hide_zero)) { | |
179 | bt_bool val; | |
180 | int vret = bt_value_bool_get(hide_zero, &val); | |
181 | ||
182 | assert(vret == 0); | |
183 | counter->hide_zero = (bool) val; | |
184 | } | |
185 | ||
186 | ret = bt_private_component_set_user_data(component, counter); | |
187 | if (ret != BT_COMPONENT_STATUS_OK) { | |
188 | goto error; | |
189 | } | |
190 | ||
191 | goto end; | |
192 | ||
193 | error: | |
194 | destroy_private_counter_data(counter); | |
195 | ||
196 | end: | |
197 | bt_put(step); | |
198 | bt_put(hide_zero); | |
199 | return ret; | |
200 | } | |
201 | ||
202 | void counter_port_connected( | |
203 | struct bt_private_component *component, | |
204 | struct bt_private_port *self_port, | |
205 | struct bt_port *other_port) | |
206 | { | |
207 | struct counter *counter; | |
208 | struct bt_notification_iterator *iterator; | |
209 | struct bt_private_connection *connection; | |
210 | enum bt_connection_status conn_status; | |
211 | ||
212 | counter = bt_private_component_get_user_data(component); | |
213 | assert(counter); | |
214 | connection = bt_private_port_get_private_connection(self_port); | |
215 | assert(connection); | |
216 | conn_status = bt_private_connection_create_notification_iterator( | |
217 | connection, NULL, &iterator); | |
218 | if (conn_status != BT_CONNECTION_STATUS_OK) { | |
219 | counter->error = true; | |
220 | goto end; | |
221 | } | |
222 | ||
223 | BT_MOVE(counter->notif_iter, iterator); | |
224 | ||
225 | end: | |
226 | bt_put(connection); | |
227 | } | |
228 | ||
229 | enum bt_component_status counter_consume(struct bt_private_component *component) | |
230 | { | |
231 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
232 | struct bt_notification *notif = NULL; | |
233 | struct counter *counter; | |
234 | enum bt_notification_iterator_status it_ret; | |
235 | int64_t count; | |
236 | ||
237 | counter = bt_private_component_get_user_data(component); | |
238 | assert(counter); | |
239 | ||
240 | if (unlikely(counter->error)) { | |
241 | ret = BT_COMPONENT_STATUS_ERROR; | |
242 | goto end; | |
243 | } | |
244 | ||
245 | if (unlikely(!counter->notif_iter)) { | |
246 | try_print_last(counter); | |
247 | ret = BT_COMPONENT_STATUS_END; | |
248 | goto end; | |
249 | } | |
250 | ||
251 | /* Consume one notification */ | |
252 | it_ret = bt_notification_iterator_next(counter->notif_iter); | |
253 | if (it_ret < 0) { | |
254 | ret = BT_COMPONENT_STATUS_ERROR; | |
255 | goto end; | |
256 | } | |
257 | ||
258 | switch (it_ret) { | |
259 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
260 | ret = BT_COMPONENT_STATUS_AGAIN; | |
261 | goto end; | |
262 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
263 | try_print_last(counter); | |
264 | ret = BT_COMPONENT_STATUS_END; | |
265 | goto end; | |
266 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
267 | { | |
268 | struct bt_notification *notif = | |
269 | bt_notification_iterator_get_notification(counter->notif_iter); | |
270 | ||
271 | assert(notif); | |
272 | switch (bt_notification_get_type(notif)) { | |
273 | case BT_NOTIFICATION_TYPE_EVENT: | |
274 | counter->count.event++; | |
275 | break; | |
276 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
277 | counter->count.inactivity++; | |
278 | break; | |
279 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: | |
280 | counter->count.stream_begin++; | |
281 | break; | |
282 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
283 | counter->count.stream_end++; | |
284 | break; | |
285 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
286 | counter->count.packet_begin++; | |
287 | break; | |
288 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
289 | counter->count.packet_end++; | |
290 | break; | |
291 | case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS: | |
292 | counter->count.discarded_events_notifs++; | |
293 | count = bt_notification_discarded_events_get_count( | |
294 | notif); | |
295 | if (count >= 0) { | |
296 | counter->count.discarded_events += count; | |
297 | } | |
298 | break; | |
299 | case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS: | |
300 | counter->count.discarded_packets_notifs++; | |
301 | count = bt_notification_discarded_packets_get_count( | |
302 | notif); | |
303 | if (count >= 0) { | |
304 | counter->count.discarded_packets += count; | |
305 | } | |
306 | break; | |
307 | default: | |
308 | counter->count.other++; | |
309 | } | |
310 | ||
311 | bt_put(notif); | |
312 | } | |
313 | default: | |
314 | break; | |
315 | } | |
316 | ||
317 | try_print_count(counter); | |
318 | ||
319 | end: | |
320 | bt_put(notif); | |
321 | return ret; | |
322 | } |