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