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