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