sink.utils.counter: append error cause when call to bt_message_iterator_next()` fails
[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{
6b8362f3 233 bt_component_class_sink_consume_method_status status;
358340ec 234 struct counter *counter;
d24d5663 235 bt_message_iterator_next_status next_status;
d6e69534
PP
236 uint64_t msg_count;
237 bt_message_array_const msgs;
6b8362f3
SM
238 bt_self_component *self_comp =
239 bt_self_component_sink_as_self_component(comp);
358340ec 240
6b8362f3 241 counter = bt_self_component_get_data(self_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);
358340ec 253
d24d5663
PP
254 switch (next_status) {
255 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
358340ec 256 {
d4393e08
PP
257 uint64_t i;
258
d6e69534
PP
259 for (i = 0; i < msg_count; i++) {
260 const bt_message *msg = msgs[i];
d4393e08 261
98b15851 262 BT_ASSERT_DBG(msg);
d6e69534
PP
263 switch (bt_message_get_type(msg)) {
264 case BT_MESSAGE_TYPE_EVENT:
d4393e08
PP
265 counter->count.event++;
266 break;
0e847b69
PP
267 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
268 counter->count.packet_begin++;
269 break;
270 case BT_MESSAGE_TYPE_PACKET_END:
271 counter->count.packet_end++;
272 break;
b9fd9cbb
FD
273 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
274 counter->count.msg_iter_inactivity++;
d4393e08 275 break;
d6e69534 276 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
d4393e08
PP
277 counter->count.stream_begin++;
278 break;
d6e69534 279 case BT_MESSAGE_TYPE_STREAM_END:
d4393e08
PP
280 counter->count.stream_end++;
281 break;
0e847b69
PP
282 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
283 counter->count.disc_events++;
284 break;
285 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
286 counter->count.disc_packets++;
d4393e08 287 break;
d4393e08
PP
288 default:
289 counter->count.other++;
358340ec 290 }
d4393e08 291
d6e69534 292 bt_message_put_ref(msg);
358340ec 293 }
d94d92ac 294
6b8362f3 295 try_print_count(counter, msg_count);
d94d92ac 296 break;
358340ec 297 }
d24d5663 298 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
d94d92ac 299 try_print_last(counter);
6b8362f3
SM
300 break;
301 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
d24d5663 302 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
6b8362f3
SM
303 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(self_comp,
304 "Failed to get messages from upstream component");
305 break;
358340ec
PP
306 default:
307 break;
308 }
309
6b8362f3 310 status = (int) next_status;
358340ec
PP
311
312end:
d24d5663 313 return status;
358340ec 314}
This page took 0.095774 seconds and 4 git commands to generate.