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 | ||
82e80647 | 23 | #define BT_COMP_LOG_SELF_COMP (counter->self_comp) |
9d87d0ea | 24 | #define BT_LOG_OUTPUT_LEVEL (counter->log_level) |
350ad6c1 | 25 | #define BT_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER" |
d9c39b0a | 26 | #include "logging/comp-logging.h" |
fdd3a2da | 27 | |
3fadfbc0 | 28 | #include <babeltrace2/babeltrace.h> |
91d81473 | 29 | #include "common/macros.h" |
578e048b | 30 | #include "common/common.h" |
578e048b | 31 | #include "common/assert.h" |
358340ec PP |
32 | #include <inttypes.h> |
33 | #include <stdint.h> | |
21348968 | 34 | #include "plugins/common/param-validation/param-validation.h" |
358340ec PP |
35 | |
36 | #include "counter.h" | |
37 | ||
0e847b69 | 38 | #define PRINTF_COUNT(_what, _var, args...) \ |
358340ec PP |
39 | do { \ |
40 | if (counter->count._var != 0 || !counter->hide_zero) { \ | |
0e847b69 | 41 | printf("%15" PRIu64 " %s message%s\n", \ |
358340ec | 42 | counter->count._var, \ |
0e847b69 PP |
43 | (_what), \ |
44 | counter->count._var == 1 ? "" : "s"); \ | |
358340ec PP |
45 | } \ |
46 | } while (0) | |
47 | ||
5badd463 PP |
48 | static |
49 | const char * const in_port_name = "in"; | |
50 | ||
358340ec PP |
51 | static |
52 | uint64_t get_total_count(struct counter *counter) | |
53 | { | |
54 | return counter->count.event + | |
55 | counter->count.stream_begin + | |
56 | counter->count.stream_end + | |
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); | |
0e847b69 PP |
73 | PRINTF_COUNT("Packet beginning", packet_begin); |
74 | PRINTF_COUNT("Packet end", packet_end); | |
75 | PRINTF_COUNT("Discarded event", disc_events); | |
76 | PRINTF_COUNT("Discarded packet", disc_packets); | |
77 | PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity); | |
358340ec PP |
78 | |
79 | if (counter->count.other > 0) { | |
0e847b69 | 80 | PRINTF_COUNT("Other (unknown)", other); |
358340ec PP |
81 | } |
82 | ||
d6e69534 | 83 | printf("%s%15" PRIu64 " message%s (TOTAL)%s\n", |
358340ec PP |
84 | bt_common_color_bold(), total, total == 1 ? "" : "s", |
85 | bt_common_color_reset()); | |
86 | counter->last_printed_total = total; | |
87 | } | |
88 | ||
89 | static | |
d6e69534 | 90 | void try_print_count(struct counter *counter, uint64_t msg_count) |
358340ec | 91 | { |
358340ec PP |
92 | if (counter->step == 0) { |
93 | /* No update */ | |
94 | return; | |
95 | } | |
96 | ||
d6e69534 | 97 | counter->at += msg_count; |
358340ec | 98 | |
d4393e08 PP |
99 | if (counter->at >= counter->step) { |
100 | counter->at = 0; | |
101 | print_count(counter); | |
358340ec PP |
102 | putchar('\n'); |
103 | } | |
104 | } | |
105 | ||
106 | static | |
107 | void try_print_last(struct counter *counter) | |
108 | { | |
109 | const uint64_t total = get_total_count(counter); | |
110 | ||
111 | if (total != counter->last_printed_total) { | |
d4393e08 | 112 | print_count(counter); |
358340ec PP |
113 | } |
114 | } | |
115 | ||
7c7301d5 | 116 | static |
358340ec PP |
117 | void destroy_private_counter_data(struct counter *counter) |
118 | { | |
718c4b7c FD |
119 | if (counter) { |
120 | bt_self_component_port_input_message_iterator_put_ref( | |
121 | counter->msg_iter); | |
122 | g_free(counter); | |
123 | } | |
358340ec PP |
124 | } |
125 | ||
d94d92ac | 126 | BT_HIDDEN |
b19ff26f | 127 | void counter_finalize(bt_self_component_sink *comp) |
358340ec PP |
128 | { |
129 | struct counter *counter; | |
130 | ||
d94d92ac PP |
131 | BT_ASSERT(comp); |
132 | counter = bt_self_component_get_data( | |
707b7d35 | 133 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 134 | BT_ASSERT(counter); |
358340ec | 135 | try_print_last(counter); |
d6e69534 | 136 | bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter); |
358340ec PP |
137 | g_free(counter); |
138 | } | |
139 | ||
21348968 SM |
140 | struct bt_param_validation_map_value_entry_descr counter_params[] = { |
141 | { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_UNSIGNED_INTEGER } }, | |
142 | { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
143 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
144 | }; | |
145 | ||
d94d92ac | 146 | BT_HIDDEN |
21a9f056 | 147 | bt_component_class_initialize_method_status counter_init( |
b19ff26f | 148 | bt_self_component_sink *component, |
59225a3e | 149 | bt_self_component_sink_configuration *config, |
b19ff26f | 150 | const bt_value *params, |
c88dd1cb | 151 | __attribute__((unused)) void *init_method_data) |
358340ec | 152 | { |
21348968 | 153 | bt_component_class_initialize_method_status status; |
d24d5663 | 154 | bt_self_component_add_port_status add_port_status; |
358340ec | 155 | struct counter *counter = g_new0(struct counter, 1); |
b19ff26f PP |
156 | const bt_value *step = NULL; |
157 | const bt_value *hide_zero = NULL; | |
21348968 SM |
158 | enum bt_param_validation_status validation_status; |
159 | gchar *validate_error = NULL; | |
358340ec PP |
160 | |
161 | if (!counter) { | |
21a9f056 | 162 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
d94d92ac | 163 | goto error; |
358340ec PP |
164 | } |
165 | ||
82e80647 PP |
166 | counter->self_comp = |
167 | bt_self_component_sink_as_self_component(component); | |
9d87d0ea | 168 | counter->log_level = bt_component_get_logging_level( |
82e80647 | 169 | bt_self_component_as_component(counter->self_comp)); |
d24d5663 | 170 | add_port_status = bt_self_component_sink_add_input_port(component, |
358340ec | 171 | "in", NULL, NULL); |
21348968 SM |
172 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { |
173 | status = (int) add_port_status; | |
d94d92ac | 174 | goto error; |
21348968 SM |
175 | } |
176 | ||
177 | validation_status = bt_param_validation_validate(params, | |
178 | counter_params, &validate_error); | |
179 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
21a9f056 | 180 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
d24d5663 | 181 | goto error; |
21348968 SM |
182 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
183 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
184 | BT_COMP_LOGE_APPEND_CAUSE(counter->self_comp, | |
185 | "%s", validate_error); | |
186 | goto error; | |
358340ec PP |
187 | } |
188 | ||
189 | counter->last_printed_total = -1ULL; | |
a5bc50e4 | 190 | counter->step = 10000; |
05e21286 | 191 | step = bt_value_map_borrow_entry_value_const(params, "step"); |
fdd3a2da | 192 | if (step) { |
9c08c816 | 193 | counter->step = bt_value_integer_unsigned_get(step); |
358340ec PP |
194 | } |
195 | ||
05e21286 | 196 | hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero"); |
fdd3a2da | 197 | if (hide_zero) { |
fdd3a2da | 198 | counter->hide_zero = (bool) bt_value_bool_get(hide_zero); |
358340ec PP |
199 | } |
200 | ||
d94d92ac | 201 | bt_self_component_set_data( |
707b7d35 | 202 | bt_self_component_sink_as_self_component(component), |
d94d92ac | 203 | counter); |
21348968 SM |
204 | |
205 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
358340ec PP |
206 | goto end; |
207 | ||
208 | error: | |
209 | destroy_private_counter_data(counter); | |
d24d5663 | 210 | |
358340ec | 211 | end: |
21348968 | 212 | g_free(validate_error); |
d24d5663 | 213 | return status; |
358340ec PP |
214 | } |
215 | ||
d94d92ac | 216 | BT_HIDDEN |
d24d5663 PP |
217 | bt_component_class_sink_graph_is_configured_method_status |
218 | counter_graph_is_configured( | |
5badd463 | 219 | bt_self_component_sink *comp) |
358340ec | 220 | { |
e803df70 SM |
221 | bt_component_class_sink_graph_is_configured_method_status status; |
222 | bt_self_component_port_input_message_iterator_create_from_sink_component_status | |
223 | msg_iter_status; | |
358340ec | 224 | struct counter *counter; |
d6e69534 | 225 | bt_self_component_port_input_message_iterator *iterator; |
358340ec | 226 | |
d94d92ac | 227 | counter = bt_self_component_get_data( |
707b7d35 | 228 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 229 | BT_ASSERT(counter); |
e803df70 SM |
230 | |
231 | msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component( | |
ca02df0a | 232 | comp, bt_self_component_sink_borrow_input_port_by_name(comp, |
e803df70 SM |
233 | in_port_name), &iterator); |
234 | if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { | |
235 | status = (int) msg_iter_status; | |
358340ec PP |
236 | goto end; |
237 | } | |
238 | ||
d6e69534 PP |
239 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF( |
240 | counter->msg_iter, iterator); | |
358340ec | 241 | |
e803df70 | 242 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; |
358340ec | 243 | end: |
bf55043c | 244 | return status; |
358340ec PP |
245 | } |
246 | ||
d94d92ac | 247 | BT_HIDDEN |
d24d5663 | 248 | bt_component_class_sink_consume_method_status counter_consume( |
b19ff26f | 249 | bt_self_component_sink *comp) |
358340ec | 250 | { |
d24d5663 PP |
251 | bt_component_class_sink_consume_method_status status = |
252 | BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; | |
358340ec | 253 | struct counter *counter; |
d24d5663 | 254 | bt_message_iterator_next_status next_status; |
d6e69534 PP |
255 | uint64_t msg_count; |
256 | bt_message_array_const msgs; | |
358340ec | 257 | |
d94d92ac | 258 | counter = bt_self_component_get_data( |
707b7d35 | 259 | bt_self_component_sink_as_self_component(comp)); |
98b15851 | 260 | BT_ASSERT_DBG(counter); |
358340ec | 261 | |
91d81473 | 262 | if (G_UNLIKELY(!counter->msg_iter)) { |
358340ec | 263 | try_print_last(counter); |
d24d5663 | 264 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
358340ec PP |
265 | goto end; |
266 | } | |
267 | ||
d6e69534 | 268 | /* Consume messages */ |
d24d5663 | 269 | next_status = bt_self_component_port_input_message_iterator_next( |
d6e69534 | 270 | counter->msg_iter, &msgs, &msg_count); |
d24d5663 PP |
271 | if (next_status < 0) { |
272 | status = (int) next_status; | |
358340ec PP |
273 | goto end; |
274 | } | |
275 | ||
d24d5663 PP |
276 | switch (next_status) { |
277 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK: | |
358340ec | 278 | { |
d4393e08 PP |
279 | uint64_t i; |
280 | ||
d6e69534 PP |
281 | for (i = 0; i < msg_count; i++) { |
282 | const bt_message *msg = msgs[i]; | |
d4393e08 | 283 | |
98b15851 | 284 | BT_ASSERT_DBG(msg); |
d6e69534 PP |
285 | switch (bt_message_get_type(msg)) { |
286 | case BT_MESSAGE_TYPE_EVENT: | |
d4393e08 PP |
287 | counter->count.event++; |
288 | break; | |
0e847b69 PP |
289 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
290 | counter->count.packet_begin++; | |
291 | break; | |
292 | case BT_MESSAGE_TYPE_PACKET_END: | |
293 | counter->count.packet_end++; | |
294 | break; | |
b9fd9cbb FD |
295 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
296 | counter->count.msg_iter_inactivity++; | |
d4393e08 | 297 | break; |
d6e69534 | 298 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
d4393e08 PP |
299 | counter->count.stream_begin++; |
300 | break; | |
d6e69534 | 301 | case BT_MESSAGE_TYPE_STREAM_END: |
d4393e08 PP |
302 | counter->count.stream_end++; |
303 | break; | |
0e847b69 PP |
304 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
305 | counter->count.disc_events++; | |
306 | break; | |
307 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
308 | counter->count.disc_packets++; | |
d4393e08 | 309 | break; |
d4393e08 PP |
310 | default: |
311 | counter->count.other++; | |
358340ec | 312 | } |
d4393e08 | 313 | |
d6e69534 | 314 | bt_message_put_ref(msg); |
358340ec | 315 | } |
d94d92ac | 316 | |
d24d5663 | 317 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
d94d92ac | 318 | break; |
358340ec | 319 | } |
d24d5663 PP |
320 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN: |
321 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN; | |
d94d92ac | 322 | goto end; |
d24d5663 | 323 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: |
d94d92ac | 324 | try_print_last(counter); |
d24d5663 | 325 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
d94d92ac | 326 | goto end; |
d24d5663 PP |
327 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR: |
328 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR; | |
d94d92ac | 329 | goto end; |
358340ec PP |
330 | default: |
331 | break; | |
332 | } | |
333 | ||
d6e69534 | 334 | try_print_count(counter, msg_count); |
358340ec PP |
335 | |
336 | end: | |
d24d5663 | 337 | return status; |
358340ec | 338 | } |