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 | ||
b19ff26f | 111 | void counter_finalize(bt_self_component_sink *comp) |
358340ec PP |
112 | { |
113 | struct counter *counter; | |
114 | ||
d94d92ac PP |
115 | BT_ASSERT(comp); |
116 | counter = bt_self_component_get_data( | |
707b7d35 | 117 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 118 | BT_ASSERT(counter); |
358340ec | 119 | try_print_last(counter); |
9a2c8b8e | 120 | bt_message_iterator_put_ref(counter->msg_iter); |
358340ec PP |
121 | g_free(counter); |
122 | } | |
123 | ||
d9120ccb | 124 | static |
21348968 SM |
125 | struct bt_param_validation_map_value_entry_descr counter_params[] = { |
126 | { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_UNSIGNED_INTEGER } }, | |
127 | { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
128 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
129 | }; | |
130 | ||
21a9f056 | 131 | bt_component_class_initialize_method_status counter_init( |
b19ff26f | 132 | bt_self_component_sink *component, |
ecd7492f | 133 | bt_self_component_sink_configuration *config __attribute__((unused)), |
b19ff26f | 134 | const bt_value *params, |
ecd7492f | 135 | void *init_method_data __attribute__((unused))) |
358340ec | 136 | { |
21348968 | 137 | bt_component_class_initialize_method_status status; |
d24d5663 | 138 | bt_self_component_add_port_status add_port_status; |
358340ec | 139 | struct counter *counter = g_new0(struct counter, 1); |
b19ff26f PP |
140 | const bt_value *step = NULL; |
141 | const bt_value *hide_zero = NULL; | |
21348968 SM |
142 | enum bt_param_validation_status validation_status; |
143 | gchar *validate_error = NULL; | |
358340ec PP |
144 | |
145 | if (!counter) { | |
21a9f056 | 146 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
d94d92ac | 147 | goto error; |
358340ec PP |
148 | } |
149 | ||
82e80647 PP |
150 | counter->self_comp = |
151 | bt_self_component_sink_as_self_component(component); | |
9d87d0ea | 152 | counter->log_level = bt_component_get_logging_level( |
82e80647 | 153 | bt_self_component_as_component(counter->self_comp)); |
d24d5663 | 154 | add_port_status = bt_self_component_sink_add_input_port(component, |
358340ec | 155 | "in", NULL, NULL); |
21348968 SM |
156 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { |
157 | status = (int) add_port_status; | |
d94d92ac | 158 | goto error; |
21348968 SM |
159 | } |
160 | ||
161 | validation_status = bt_param_validation_validate(params, | |
162 | counter_params, &validate_error); | |
163 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
21a9f056 | 164 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
d24d5663 | 165 | goto error; |
21348968 SM |
166 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
167 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
168 | BT_COMP_LOGE_APPEND_CAUSE(counter->self_comp, | |
169 | "%s", validate_error); | |
170 | goto error; | |
358340ec PP |
171 | } |
172 | ||
173 | counter->last_printed_total = -1ULL; | |
a5bc50e4 | 174 | counter->step = 10000; |
05e21286 | 175 | step = bt_value_map_borrow_entry_value_const(params, "step"); |
fdd3a2da | 176 | if (step) { |
9c08c816 | 177 | counter->step = bt_value_integer_unsigned_get(step); |
358340ec PP |
178 | } |
179 | ||
05e21286 | 180 | hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero"); |
fdd3a2da | 181 | if (hide_zero) { |
fdd3a2da | 182 | counter->hide_zero = (bool) bt_value_bool_get(hide_zero); |
358340ec PP |
183 | } |
184 | ||
d94d92ac | 185 | bt_self_component_set_data( |
707b7d35 | 186 | bt_self_component_sink_as_self_component(component), |
d94d92ac | 187 | counter); |
21348968 SM |
188 | |
189 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
358340ec PP |
190 | goto end; |
191 | ||
192 | error: | |
193 | destroy_private_counter_data(counter); | |
d24d5663 | 194 | |
358340ec | 195 | end: |
21348968 | 196 | g_free(validate_error); |
d24d5663 | 197 | return status; |
358340ec PP |
198 | } |
199 | ||
d24d5663 PP |
200 | bt_component_class_sink_graph_is_configured_method_status |
201 | counter_graph_is_configured( | |
5badd463 | 202 | bt_self_component_sink *comp) |
358340ec | 203 | { |
e803df70 | 204 | bt_component_class_sink_graph_is_configured_method_status status; |
9a2c8b8e | 205 | bt_message_iterator_create_from_sink_component_status |
e803df70 | 206 | msg_iter_status; |
358340ec | 207 | struct counter *counter; |
9a2c8b8e | 208 | bt_message_iterator *iterator; |
358340ec | 209 | |
d94d92ac | 210 | counter = bt_self_component_get_data( |
707b7d35 | 211 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 212 | BT_ASSERT(counter); |
e803df70 | 213 | |
9a2c8b8e | 214 | msg_iter_status = bt_message_iterator_create_from_sink_component( |
ca02df0a | 215 | comp, bt_self_component_sink_borrow_input_port_by_name(comp, |
e803df70 | 216 | in_port_name), &iterator); |
9a2c8b8e | 217 | if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { |
e803df70 | 218 | status = (int) msg_iter_status; |
358340ec PP |
219 | goto end; |
220 | } | |
221 | ||
9a2c8b8e | 222 | BT_MESSAGE_ITERATOR_MOVE_REF( |
d6e69534 | 223 | counter->msg_iter, iterator); |
358340ec | 224 | |
e803df70 | 225 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; |
358340ec | 226 | end: |
bf55043c | 227 | return status; |
358340ec PP |
228 | } |
229 | ||
d24d5663 | 230 | bt_component_class_sink_consume_method_status counter_consume( |
b19ff26f | 231 | bt_self_component_sink *comp) |
358340ec | 232 | { |
d24d5663 PP |
233 | bt_component_class_sink_consume_method_status status = |
234 | BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; | |
358340ec | 235 | struct counter *counter; |
d24d5663 | 236 | bt_message_iterator_next_status next_status; |
d6e69534 PP |
237 | uint64_t msg_count; |
238 | bt_message_array_const msgs; | |
358340ec | 239 | |
d94d92ac | 240 | counter = bt_self_component_get_data( |
707b7d35 | 241 | bt_self_component_sink_as_self_component(comp)); |
98b15851 | 242 | BT_ASSERT_DBG(counter); |
358340ec | 243 | |
91d81473 | 244 | if (G_UNLIKELY(!counter->msg_iter)) { |
358340ec | 245 | try_print_last(counter); |
d24d5663 | 246 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
358340ec PP |
247 | goto end; |
248 | } | |
249 | ||
d6e69534 | 250 | /* Consume messages */ |
9a2c8b8e | 251 | next_status = bt_message_iterator_next( |
d6e69534 | 252 | counter->msg_iter, &msgs, &msg_count); |
d24d5663 PP |
253 | if (next_status < 0) { |
254 | status = (int) next_status; | |
358340ec PP |
255 | goto end; |
256 | } | |
257 | ||
d24d5663 PP |
258 | switch (next_status) { |
259 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK: | |
358340ec | 260 | { |
d4393e08 PP |
261 | uint64_t i; |
262 | ||
d6e69534 PP |
263 | for (i = 0; i < msg_count; i++) { |
264 | const bt_message *msg = msgs[i]; | |
d4393e08 | 265 | |
98b15851 | 266 | BT_ASSERT_DBG(msg); |
d6e69534 PP |
267 | switch (bt_message_get_type(msg)) { |
268 | case BT_MESSAGE_TYPE_EVENT: | |
d4393e08 PP |
269 | counter->count.event++; |
270 | break; | |
0e847b69 PP |
271 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
272 | counter->count.packet_begin++; | |
273 | break; | |
274 | case BT_MESSAGE_TYPE_PACKET_END: | |
275 | counter->count.packet_end++; | |
276 | break; | |
b9fd9cbb FD |
277 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
278 | counter->count.msg_iter_inactivity++; | |
d4393e08 | 279 | break; |
d6e69534 | 280 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
d4393e08 PP |
281 | counter->count.stream_begin++; |
282 | break; | |
d6e69534 | 283 | case BT_MESSAGE_TYPE_STREAM_END: |
d4393e08 PP |
284 | counter->count.stream_end++; |
285 | break; | |
0e847b69 PP |
286 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
287 | counter->count.disc_events++; | |
288 | break; | |
289 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
290 | counter->count.disc_packets++; | |
d4393e08 | 291 | break; |
d4393e08 PP |
292 | default: |
293 | counter->count.other++; | |
358340ec | 294 | } |
d4393e08 | 295 | |
d6e69534 | 296 | bt_message_put_ref(msg); |
358340ec | 297 | } |
d94d92ac | 298 | |
d24d5663 | 299 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
d94d92ac | 300 | break; |
358340ec | 301 | } |
d24d5663 PP |
302 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN: |
303 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN; | |
d94d92ac | 304 | goto end; |
d24d5663 | 305 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: |
d94d92ac | 306 | try_print_last(counter); |
d24d5663 | 307 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
d94d92ac | 308 | goto end; |
d24d5663 PP |
309 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR: |
310 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR; | |
d94d92ac | 311 | goto end; |
358340ec PP |
312 | default: |
313 | break; | |
314 | } | |
315 | ||
d6e69534 | 316 | try_print_count(counter, msg_count); |
358340ec PP |
317 | |
318 | end: | |
d24d5663 | 319 | return status; |
358340ec | 320 | } |