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 | ||
9d408fca | 23 | #include <babeltrace/babeltrace.h> |
358340ec PP |
24 | #include <babeltrace/babeltrace-internal.h> |
25 | #include <babeltrace/common-internal.h> | |
358340ec | 26 | #include <plugins-common.h> |
f6ccaed9 | 27 | #include <babeltrace/assert-internal.h> |
358340ec PP |
28 | #include <inttypes.h> |
29 | #include <stdint.h> | |
30 | ||
31 | #include "counter.h" | |
32 | ||
33 | #define PRINTF_COUNT(_what_sing, _what_plur, _var, args...) \ | |
34 | do { \ | |
35 | if (counter->count._var != 0 || !counter->hide_zero) { \ | |
36 | printf("%15" PRIu64 " %s\n", \ | |
37 | counter->count._var, \ | |
38 | counter->count._var == 1 ? _what_sing : _what_plur); \ | |
39 | } \ | |
40 | } while (0) | |
41 | ||
42 | static | |
43 | uint64_t get_total_count(struct counter *counter) | |
44 | { | |
45 | return counter->count.event + | |
46 | counter->count.stream_begin + | |
47 | counter->count.stream_end + | |
48 | counter->count.packet_begin + | |
49 | counter->count.packet_end + | |
50 | counter->count.inactivity + | |
51 | counter->count.discarded_events + | |
52 | counter->count.discarded_packets + | |
53 | counter->count.other; | |
54 | } | |
55 | ||
56 | static | |
d4393e08 | 57 | void print_count(struct counter *counter) |
358340ec | 58 | { |
d4393e08 PP |
59 | uint64_t total = get_total_count(counter); |
60 | ||
358340ec PP |
61 | PRINTF_COUNT("event", "events", event); |
62 | PRINTF_COUNT("stream beginning", "stream beginnings", stream_begin); | |
63 | PRINTF_COUNT("stream end", "stream ends", stream_end); | |
64 | PRINTF_COUNT("packet beginning", "packet beginnings", packet_begin); | |
65 | PRINTF_COUNT("packet end", "packet ends", packet_end); | |
66 | PRINTF_COUNT("inactivity", "inactivities", inactivity); | |
67 | PRINTF_COUNT("discarded events notification", | |
68 | "discarded events notifications", discarded_events_notifs); | |
69 | PRINTF_COUNT(" known discarded event", " known discarded events", | |
70 | discarded_events); | |
71 | PRINTF_COUNT("discarded packets notification", | |
72 | "discarded packets notifications", discarded_packets_notifs); | |
73 | PRINTF_COUNT(" known discarded packet", " known discarded packets", | |
74 | discarded_packets); | |
75 | ||
76 | if (counter->count.other > 0) { | |
77 | PRINTF_COUNT(" other (unknown) notification", | |
78 | " other (unknown) notifications", other); | |
79 | } | |
80 | ||
81 | printf("%s%15" PRIu64 " notification%s (TOTAL)%s\n", | |
82 | bt_common_color_bold(), total, total == 1 ? "" : "s", | |
83 | bt_common_color_reset()); | |
84 | counter->last_printed_total = total; | |
85 | } | |
86 | ||
87 | static | |
d4393e08 | 88 | void try_print_count(struct counter *counter, uint64_t notif_count) |
358340ec | 89 | { |
358340ec PP |
90 | if (counter->step == 0) { |
91 | /* No update */ | |
92 | return; | |
93 | } | |
94 | ||
d4393e08 | 95 | counter->at += notif_count; |
358340ec | 96 | |
d4393e08 PP |
97 | if (counter->at >= counter->step) { |
98 | counter->at = 0; | |
99 | print_count(counter); | |
358340ec PP |
100 | putchar('\n'); |
101 | } | |
102 | } | |
103 | ||
104 | static | |
105 | void try_print_last(struct counter *counter) | |
106 | { | |
107 | const uint64_t total = get_total_count(counter); | |
108 | ||
109 | if (total != counter->last_printed_total) { | |
d4393e08 | 110 | print_count(counter); |
358340ec PP |
111 | } |
112 | } | |
113 | ||
114 | void destroy_private_counter_data(struct counter *counter) | |
115 | { | |
116 | bt_put(counter->notif_iter); | |
117 | g_free(counter); | |
118 | } | |
119 | ||
120 | void counter_finalize(struct bt_private_component *component) | |
121 | { | |
122 | struct counter *counter; | |
123 | ||
f6ccaed9 | 124 | BT_ASSERT(component); |
358340ec | 125 | counter = bt_private_component_get_user_data(component); |
f6ccaed9 | 126 | BT_ASSERT(counter); |
358340ec PP |
127 | try_print_last(counter); |
128 | bt_put(counter->notif_iter); | |
129 | g_free(counter); | |
130 | } | |
131 | ||
132 | enum bt_component_status counter_init(struct bt_private_component *component, | |
133 | struct bt_value *params, UNUSED_VAR void *init_method_data) | |
134 | { | |
135 | enum bt_component_status ret; | |
136 | struct counter *counter = g_new0(struct counter, 1); | |
137 | struct bt_value *step = NULL; | |
138 | struct bt_value *hide_zero = NULL; | |
139 | ||
140 | if (!counter) { | |
141 | ret = BT_COMPONENT_STATUS_NOMEM; | |
142 | goto end; | |
143 | } | |
144 | ||
145 | ret = bt_private_component_sink_add_input_private_port(component, | |
146 | "in", NULL, NULL); | |
147 | if (ret != BT_COMPONENT_STATUS_OK) { | |
148 | goto end; | |
149 | } | |
150 | ||
151 | counter->last_printed_total = -1ULL; | |
152 | counter->step = 1000; | |
0e4db2d5 | 153 | step = bt_value_map_borrow(params, "step"); |
f6ccaed9 | 154 | if (step && bt_value_is_integer(step)) { |
358340ec | 155 | int64_t val; |
358340ec | 156 | |
f6ccaed9 | 157 | (void) bt_value_integer_get(step, &val); |
358340ec PP |
158 | |
159 | if (val >= 0) { | |
160 | counter->step = (uint64_t) val; | |
161 | } | |
162 | } | |
163 | ||
0e4db2d5 | 164 | hide_zero = bt_value_map_borrow(params, "hide-zero"); |
f6ccaed9 | 165 | if (hide_zero && bt_value_is_bool(hide_zero)) { |
358340ec | 166 | bt_bool val; |
358340ec | 167 | |
f6ccaed9 | 168 | (void) bt_value_bool_get(hide_zero, &val); |
358340ec PP |
169 | counter->hide_zero = (bool) val; |
170 | } | |
171 | ||
172 | ret = bt_private_component_set_user_data(component, counter); | |
173 | if (ret != BT_COMPONENT_STATUS_OK) { | |
174 | goto error; | |
175 | } | |
176 | ||
177 | goto end; | |
178 | ||
179 | error: | |
180 | destroy_private_counter_data(counter); | |
181 | ||
182 | end: | |
358340ec PP |
183 | return ret; |
184 | } | |
185 | ||
186 | void counter_port_connected( | |
187 | struct bt_private_component *component, | |
188 | struct bt_private_port *self_port, | |
189 | struct bt_port *other_port) | |
190 | { | |
191 | struct counter *counter; | |
192 | struct bt_notification_iterator *iterator; | |
193 | struct bt_private_connection *connection; | |
194 | enum bt_connection_status conn_status; | |
195 | ||
196 | counter = bt_private_component_get_user_data(component); | |
f6ccaed9 | 197 | BT_ASSERT(counter); |
358340ec | 198 | connection = bt_private_port_get_private_connection(self_port); |
f6ccaed9 | 199 | BT_ASSERT(connection); |
358340ec | 200 | conn_status = bt_private_connection_create_notification_iterator( |
f42867e2 | 201 | connection, &iterator); |
358340ec PP |
202 | if (conn_status != BT_CONNECTION_STATUS_OK) { |
203 | counter->error = true; | |
204 | goto end; | |
205 | } | |
206 | ||
207 | BT_MOVE(counter->notif_iter, iterator); | |
208 | ||
209 | end: | |
210 | bt_put(connection); | |
211 | } | |
212 | ||
213 | enum bt_component_status counter_consume(struct bt_private_component *component) | |
214 | { | |
215 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
358340ec PP |
216 | struct counter *counter; |
217 | enum bt_notification_iterator_status it_ret; | |
218 | int64_t count; | |
d4393e08 PP |
219 | uint64_t notif_count; |
220 | bt_notification_array notifs; | |
358340ec PP |
221 | |
222 | counter = bt_private_component_get_user_data(component); | |
f6ccaed9 | 223 | BT_ASSERT(counter); |
358340ec PP |
224 | |
225 | if (unlikely(counter->error)) { | |
226 | ret = BT_COMPONENT_STATUS_ERROR; | |
227 | goto end; | |
228 | } | |
229 | ||
230 | if (unlikely(!counter->notif_iter)) { | |
231 | try_print_last(counter); | |
232 | ret = BT_COMPONENT_STATUS_END; | |
233 | goto end; | |
234 | } | |
235 | ||
d4393e08 | 236 | /* Consume notifications */ |
07245ac2 | 237 | it_ret = bt_private_connection_notification_iterator_next( |
d4393e08 | 238 | counter->notif_iter, ¬ifs, ¬if_count); |
358340ec PP |
239 | if (it_ret < 0) { |
240 | ret = BT_COMPONENT_STATUS_ERROR; | |
241 | goto end; | |
242 | } | |
243 | ||
244 | switch (it_ret) { | |
245 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
246 | ret = BT_COMPONENT_STATUS_AGAIN; | |
247 | goto end; | |
248 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
249 | try_print_last(counter); | |
250 | ret = BT_COMPONENT_STATUS_END; | |
251 | goto end; | |
252 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
253 | { | |
d4393e08 PP |
254 | uint64_t i; |
255 | ||
256 | for (i = 0; i < notif_count; i++) { | |
257 | struct bt_notification *notif = notifs[i]; | |
258 | ||
259 | BT_ASSERT(notif); | |
260 | switch (bt_notification_get_type(notif)) { | |
261 | case BT_NOTIFICATION_TYPE_EVENT: | |
262 | counter->count.event++; | |
263 | break; | |
264 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
265 | counter->count.inactivity++; | |
266 | break; | |
267 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: | |
268 | counter->count.stream_begin++; | |
269 | break; | |
270 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
271 | counter->count.stream_end++; | |
272 | break; | |
273 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
274 | counter->count.packet_begin++; | |
275 | break; | |
276 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
277 | counter->count.packet_end++; | |
278 | break; | |
279 | case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS: | |
280 | counter->count.discarded_events_notifs++; | |
281 | count = bt_notification_discarded_events_get_count( | |
282 | notif); | |
283 | if (count >= 0) { | |
284 | counter->count.discarded_events += count; | |
285 | } | |
286 | break; | |
287 | case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS: | |
288 | counter->count.discarded_packets_notifs++; | |
289 | count = bt_notification_discarded_packets_get_count( | |
290 | notif); | |
291 | if (count >= 0) { | |
292 | counter->count.discarded_packets += count; | |
293 | } | |
294 | break; | |
295 | default: | |
296 | counter->count.other++; | |
358340ec | 297 | } |
d4393e08 PP |
298 | |
299 | bt_put(notif); | |
358340ec | 300 | } |
358340ec PP |
301 | } |
302 | default: | |
303 | break; | |
304 | } | |
305 | ||
d4393e08 | 306 | try_print_count(counter, notif_count); |
358340ec PP |
307 | |
308 | end: | |
358340ec PP |
309 | return ret; |
310 | } |