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