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 | ||
fdd3a2da PP |
23 | #define BT_LOG_TAG "PLUGIN-UTILS-COUNTER-FLT" |
24 | #include "logging.h" | |
25 | ||
3fadfbc0 | 26 | #include <babeltrace2/babeltrace.h> |
91d81473 | 27 | #include "common/macros.h" |
578e048b MJ |
28 | #include "common/common.h" |
29 | #include "plugins/plugins-common.h" | |
30 | #include "common/assert.h" | |
358340ec PP |
31 | #include <inttypes.h> |
32 | #include <stdint.h> | |
33 | ||
34 | #include "counter.h" | |
35 | ||
0e847b69 | 36 | #define PRINTF_COUNT(_what, _var, args...) \ |
358340ec PP |
37 | do { \ |
38 | if (counter->count._var != 0 || !counter->hide_zero) { \ | |
0e847b69 | 39 | printf("%15" PRIu64 " %s message%s\n", \ |
358340ec | 40 | counter->count._var, \ |
0e847b69 PP |
41 | (_what), \ |
42 | counter->count._var == 1 ? "" : "s"); \ | |
358340ec PP |
43 | } \ |
44 | } while (0) | |
45 | ||
5badd463 PP |
46 | static |
47 | const char * const in_port_name = "in"; | |
48 | ||
358340ec PP |
49 | static |
50 | uint64_t get_total_count(struct counter *counter) | |
51 | { | |
52 | return counter->count.event + | |
53 | counter->count.stream_begin + | |
54 | counter->count.stream_end + | |
0e847b69 PP |
55 | counter->count.stream_activity_begin + |
56 | counter->count.stream_activity_end + | |
358340ec PP |
57 | counter->count.packet_begin + |
58 | counter->count.packet_end + | |
0e847b69 PP |
59 | counter->count.disc_events + |
60 | counter->count.disc_packets + | |
b9fd9cbb | 61 | counter->count.msg_iter_inactivity + |
358340ec PP |
62 | counter->count.other; |
63 | } | |
64 | ||
65 | static | |
d4393e08 | 66 | void print_count(struct counter *counter) |
358340ec | 67 | { |
d4393e08 PP |
68 | uint64_t total = get_total_count(counter); |
69 | ||
0e847b69 PP |
70 | PRINTF_COUNT("Event", event); |
71 | PRINTF_COUNT("Stream beginning", stream_begin); | |
72 | PRINTF_COUNT("Stream end", stream_end); | |
73 | PRINTF_COUNT("Stream activity beginning", stream_activity_begin); | |
74 | PRINTF_COUNT("Stream activity end", stream_activity_end); | |
75 | PRINTF_COUNT("Packet beginning", packet_begin); | |
76 | PRINTF_COUNT("Packet end", packet_end); | |
77 | PRINTF_COUNT("Discarded event", disc_events); | |
78 | PRINTF_COUNT("Discarded packet", disc_packets); | |
79 | PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity); | |
358340ec PP |
80 | |
81 | if (counter->count.other > 0) { | |
0e847b69 | 82 | PRINTF_COUNT("Other (unknown)", other); |
358340ec PP |
83 | } |
84 | ||
d6e69534 | 85 | printf("%s%15" PRIu64 " message%s (TOTAL)%s\n", |
358340ec PP |
86 | bt_common_color_bold(), total, total == 1 ? "" : "s", |
87 | bt_common_color_reset()); | |
88 | counter->last_printed_total = total; | |
89 | } | |
90 | ||
91 | static | |
d6e69534 | 92 | void try_print_count(struct counter *counter, uint64_t msg_count) |
358340ec | 93 | { |
358340ec PP |
94 | if (counter->step == 0) { |
95 | /* No update */ | |
96 | return; | |
97 | } | |
98 | ||
d6e69534 | 99 | counter->at += msg_count; |
358340ec | 100 | |
d4393e08 PP |
101 | if (counter->at >= counter->step) { |
102 | counter->at = 0; | |
103 | print_count(counter); | |
358340ec PP |
104 | putchar('\n'); |
105 | } | |
106 | } | |
107 | ||
108 | static | |
109 | void try_print_last(struct counter *counter) | |
110 | { | |
111 | const uint64_t total = get_total_count(counter); | |
112 | ||
113 | if (total != counter->last_printed_total) { | |
d4393e08 | 114 | print_count(counter); |
358340ec PP |
115 | } |
116 | } | |
117 | ||
118 | void destroy_private_counter_data(struct counter *counter) | |
119 | { | |
d6e69534 | 120 | bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter); |
358340ec PP |
121 | g_free(counter); |
122 | } | |
123 | ||
d94d92ac | 124 | BT_HIDDEN |
b19ff26f | 125 | void counter_finalize(bt_self_component_sink *comp) |
358340ec PP |
126 | { |
127 | struct counter *counter; | |
128 | ||
d94d92ac PP |
129 | BT_ASSERT(comp); |
130 | counter = bt_self_component_get_data( | |
707b7d35 | 131 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 132 | BT_ASSERT(counter); |
358340ec | 133 | try_print_last(counter); |
d6e69534 | 134 | bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter); |
358340ec PP |
135 | g_free(counter); |
136 | } | |
137 | ||
d94d92ac | 138 | BT_HIDDEN |
4cdfc5e8 | 139 | bt_self_component_status counter_init( |
b19ff26f PP |
140 | bt_self_component_sink *component, |
141 | const bt_value *params, | |
05e21286 | 142 | UNUSED_VAR void *init_method_data) |
358340ec | 143 | { |
4cdfc5e8 | 144 | bt_self_component_status ret; |
358340ec | 145 | struct counter *counter = g_new0(struct counter, 1); |
b19ff26f PP |
146 | const bt_value *step = NULL; |
147 | const bt_value *hide_zero = NULL; | |
358340ec PP |
148 | |
149 | if (!counter) { | |
d94d92ac PP |
150 | ret = BT_SELF_COMPONENT_STATUS_NOMEM; |
151 | goto error; | |
358340ec PP |
152 | } |
153 | ||
d94d92ac | 154 | ret = bt_self_component_sink_add_input_port(component, |
358340ec | 155 | "in", NULL, NULL); |
d94d92ac PP |
156 | if (ret != BT_SELF_COMPONENT_STATUS_OK) { |
157 | goto error; | |
358340ec PP |
158 | } |
159 | ||
160 | counter->last_printed_total = -1ULL; | |
a5bc50e4 | 161 | counter->step = 10000; |
05e21286 | 162 | step = bt_value_map_borrow_entry_value_const(params, "step"); |
fdd3a2da PP |
163 | if (step) { |
164 | if (!bt_value_is_unsigned_integer(step)) { | |
165 | BT_LOGE("`step` parameter: expecting an unsigned integer value: " | |
166 | "type=%s", bt_common_value_type_string( | |
167 | bt_value_get_type(step))); | |
168 | goto error; | |
358340ec | 169 | } |
fdd3a2da PP |
170 | |
171 | counter->step = bt_value_unsigned_integer_get(step); | |
358340ec PP |
172 | } |
173 | ||
05e21286 | 174 | hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero"); |
fdd3a2da PP |
175 | if (hide_zero) { |
176 | if (!bt_value_is_bool(hide_zero)) { | |
177 | BT_LOGE("`hide-zero` parameter: expecting a boolean value: " | |
178 | "type=%s", bt_common_value_type_string( | |
179 | bt_value_get_type(hide_zero))); | |
180 | goto error; | |
181 | } | |
358340ec | 182 | |
fdd3a2da | 183 | counter->hide_zero = (bool) bt_value_bool_get(hide_zero); |
358340ec PP |
184 | } |
185 | ||
d94d92ac | 186 | bt_self_component_set_data( |
707b7d35 | 187 | bt_self_component_sink_as_self_component(component), |
d94d92ac | 188 | counter); |
358340ec PP |
189 | goto end; |
190 | ||
191 | error: | |
192 | destroy_private_counter_data(counter); | |
fdd3a2da | 193 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
358340ec PP |
194 | |
195 | end: | |
358340ec PP |
196 | return ret; |
197 | } | |
198 | ||
d94d92ac | 199 | BT_HIDDEN |
5badd463 PP |
200 | bt_self_component_status counter_graph_is_configured( |
201 | bt_self_component_sink *comp) | |
358340ec | 202 | { |
4cdfc5e8 | 203 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
358340ec | 204 | struct counter *counter; |
d6e69534 | 205 | bt_self_component_port_input_message_iterator *iterator; |
358340ec | 206 | |
d94d92ac | 207 | counter = bt_self_component_get_data( |
707b7d35 | 208 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 209 | BT_ASSERT(counter); |
d6e69534 | 210 | iterator = bt_self_component_port_input_message_iterator_create( |
5badd463 PP |
211 | bt_self_component_sink_borrow_input_port_by_name(comp, |
212 | in_port_name)); | |
d94d92ac PP |
213 | if (!iterator) { |
214 | status = BT_SELF_COMPONENT_STATUS_NOMEM; | |
358340ec PP |
215 | goto end; |
216 | } | |
217 | ||
d6e69534 PP |
218 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF( |
219 | counter->msg_iter, iterator); | |
358340ec PP |
220 | |
221 | end: | |
bf55043c | 222 | return status; |
358340ec PP |
223 | } |
224 | ||
d94d92ac | 225 | BT_HIDDEN |
4cdfc5e8 | 226 | bt_self_component_status counter_consume( |
b19ff26f | 227 | bt_self_component_sink *comp) |
358340ec | 228 | { |
4cdfc5e8 | 229 | bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
358340ec | 230 | struct counter *counter; |
4cdfc5e8 | 231 | bt_message_iterator_status it_ret; |
d6e69534 PP |
232 | uint64_t msg_count; |
233 | bt_message_array_const msgs; | |
358340ec | 234 | |
d94d92ac | 235 | counter = bt_self_component_get_data( |
707b7d35 | 236 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 237 | BT_ASSERT(counter); |
358340ec | 238 | |
91d81473 | 239 | if (G_UNLIKELY(!counter->msg_iter)) { |
358340ec | 240 | try_print_last(counter); |
d94d92ac | 241 | ret = BT_SELF_COMPONENT_STATUS_END; |
358340ec PP |
242 | goto end; |
243 | } | |
244 | ||
d6e69534 PP |
245 | /* Consume messages */ |
246 | it_ret = bt_self_component_port_input_message_iterator_next( | |
247 | counter->msg_iter, &msgs, &msg_count); | |
358340ec | 248 | if (it_ret < 0) { |
d94d92ac | 249 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
358340ec PP |
250 | goto end; |
251 | } | |
252 | ||
253 | switch (it_ret) { | |
d6e69534 | 254 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
358340ec | 255 | { |
d4393e08 PP |
256 | uint64_t i; |
257 | ||
d6e69534 PP |
258 | for (i = 0; i < msg_count; i++) { |
259 | const bt_message *msg = msgs[i]; | |
d4393e08 | 260 | |
d6e69534 PP |
261 | BT_ASSERT(msg); |
262 | switch (bt_message_get_type(msg)) { | |
263 | case BT_MESSAGE_TYPE_EVENT: | |
d4393e08 PP |
264 | counter->count.event++; |
265 | break; | |
0e847b69 PP |
266 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
267 | counter->count.packet_begin++; | |
268 | break; | |
269 | case BT_MESSAGE_TYPE_PACKET_END: | |
270 | counter->count.packet_end++; | |
271 | break; | |
b9fd9cbb FD |
272 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
273 | counter->count.msg_iter_inactivity++; | |
d4393e08 | 274 | break; |
d6e69534 | 275 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
d4393e08 PP |
276 | counter->count.stream_begin++; |
277 | break; | |
d6e69534 | 278 | case BT_MESSAGE_TYPE_STREAM_END: |
d4393e08 PP |
279 | counter->count.stream_end++; |
280 | break; | |
0e847b69 PP |
281 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: |
282 | counter->count.stream_activity_begin++; | |
d4393e08 | 283 | break; |
0e847b69 PP |
284 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: |
285 | counter->count.stream_activity_end++; | |
286 | break; | |
287 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
288 | counter->count.disc_events++; | |
289 | break; | |
290 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
291 | counter->count.disc_packets++; | |
d4393e08 | 292 | break; |
d4393e08 PP |
293 | default: |
294 | counter->count.other++; | |
358340ec | 295 | } |
d4393e08 | 296 | |
d6e69534 | 297 | bt_message_put_ref(msg); |
358340ec | 298 | } |
d94d92ac PP |
299 | |
300 | ret = BT_SELF_COMPONENT_STATUS_OK; | |
301 | break; | |
358340ec | 302 | } |
d6e69534 | 303 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
d94d92ac PP |
304 | ret = BT_SELF_COMPONENT_STATUS_AGAIN; |
305 | goto end; | |
d6e69534 | 306 | case BT_MESSAGE_ITERATOR_STATUS_END: |
d94d92ac PP |
307 | try_print_last(counter); |
308 | ret = BT_SELF_COMPONENT_STATUS_END; | |
309 | goto end; | |
d6e69534 | 310 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: |
d94d92ac PP |
311 | ret = BT_SELF_COMPONENT_STATUS_NOMEM; |
312 | goto end; | |
358340ec PP |
313 | default: |
314 | break; | |
315 | } | |
316 | ||
d6e69534 | 317 | try_print_count(counter, msg_count); |
358340ec PP |
318 | |
319 | end: | |
358340ec PP |
320 | return ret; |
321 | } |