sink.utils.counter: remove unnecessary check in counter_consume()
[babeltrace.git] / src / plugins / utils / counter / counter.c
CommitLineData
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
33static
34const char * const in_port_name = "in";
35
358340ec
PP
36static
37uint64_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
50static
d4393e08 51void 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
74static
d6e69534 75void 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
91static
92void 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 101static
358340ec
PP
102void 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 111void 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 124static
21348968
SM
125struct 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 131bt_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
192error:
193 destroy_private_counter_data(counter);
d24d5663 194
358340ec 195end:
21348968 196 g_free(validate_error);
d24d5663 197 return status;
358340ec
PP
198}
199
d24d5663
PP
200bt_component_class_sink_graph_is_configured_method_status
201counter_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 226end:
bf55043c 227 return status;
358340ec
PP
228}
229
d24d5663 230bt_component_class_sink_consume_method_status counter_consume(
b19ff26f 231 bt_self_component_sink *comp)
358340ec 232{
358340ec 233 struct counter *counter;
d24d5663 234 bt_message_iterator_next_status next_status;
d6e69534
PP
235 uint64_t msg_count;
236 bt_message_array_const msgs;
6b8362f3
SM
237 bt_self_component *self_comp =
238 bt_self_component_sink_as_self_component(comp);
358340ec 239
6b8362f3 240 counter = bt_self_component_get_data(self_comp);
98b15851 241 BT_ASSERT_DBG(counter);
c6a7ea93 242 BT_ASSERT_DBG(counter->msg_iter);
358340ec 243
d6e69534 244 /* Consume messages */
9a2c8b8e 245 next_status = bt_message_iterator_next(
d6e69534 246 counter->msg_iter, &msgs, &msg_count);
358340ec 247
d24d5663
PP
248 switch (next_status) {
249 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
358340ec 250 {
d4393e08
PP
251 uint64_t i;
252
d6e69534
PP
253 for (i = 0; i < msg_count; i++) {
254 const bt_message *msg = msgs[i];
d4393e08 255
98b15851 256 BT_ASSERT_DBG(msg);
d6e69534
PP
257 switch (bt_message_get_type(msg)) {
258 case BT_MESSAGE_TYPE_EVENT:
d4393e08
PP
259 counter->count.event++;
260 break;
0e847b69
PP
261 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
262 counter->count.packet_begin++;
263 break;
264 case BT_MESSAGE_TYPE_PACKET_END:
265 counter->count.packet_end++;
266 break;
b9fd9cbb
FD
267 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
268 counter->count.msg_iter_inactivity++;
d4393e08 269 break;
d6e69534 270 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
d4393e08
PP
271 counter->count.stream_begin++;
272 break;
d6e69534 273 case BT_MESSAGE_TYPE_STREAM_END:
d4393e08
PP
274 counter->count.stream_end++;
275 break;
0e847b69
PP
276 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
277 counter->count.disc_events++;
278 break;
279 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
280 counter->count.disc_packets++;
d4393e08 281 break;
d4393e08
PP
282 default:
283 counter->count.other++;
358340ec 284 }
d4393e08 285
d6e69534 286 bt_message_put_ref(msg);
358340ec 287 }
d94d92ac 288
6b8362f3 289 try_print_count(counter, msg_count);
d94d92ac 290 break;
358340ec 291 }
d24d5663 292 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
d94d92ac 293 try_print_last(counter);
6b8362f3
SM
294 break;
295 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
d24d5663 296 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
6b8362f3
SM
297 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(self_comp,
298 "Failed to get messages from upstream component");
299 break;
358340ec
PP
300 default:
301 break;
302 }
303
c6a7ea93 304 return (int) next_status;
358340ec 305}
This page took 0.093765 seconds and 4 git commands to generate.