Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / plugins / utils / counter / counter.c
CommitLineData
358340ec
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
82e80647 23#define BT_COMP_LOG_SELF_COMP (counter->self_comp)
9d87d0ea 24#define BT_LOG_OUTPUT_LEVEL (counter->log_level)
350ad6c1 25#define BT_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER"
d9c39b0a 26#include "logging/comp-logging.h"
fdd3a2da 27
3fadfbc0 28#include <babeltrace2/babeltrace.h>
91d81473 29#include "common/macros.h"
578e048b 30#include "common/common.h"
578e048b 31#include "common/assert.h"
358340ec 32#include <inttypes.h>
c4f23e30 33#include <stdbool.h>
358340ec 34#include <stdint.h>
21348968 35#include "plugins/common/param-validation/param-validation.h"
358340ec
PP
36
37#include "counter.h"
38
0e847b69 39#define PRINTF_COUNT(_what, _var, args...) \
358340ec
PP
40 do { \
41 if (counter->count._var != 0 || !counter->hide_zero) { \
0e847b69 42 printf("%15" PRIu64 " %s message%s\n", \
358340ec 43 counter->count._var, \
0e847b69
PP
44 (_what), \
45 counter->count._var == 1 ? "" : "s"); \
358340ec
PP
46 } \
47 } while (0)
48
5badd463
PP
49static
50const char * const in_port_name = "in";
51
358340ec
PP
52static
53uint64_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 +
0e847b69
PP
60 counter->count.disc_events +
61 counter->count.disc_packets +
b9fd9cbb 62 counter->count.msg_iter_inactivity +
358340ec
PP
63 counter->count.other;
64}
65
66static
d4393e08 67void print_count(struct counter *counter)
358340ec 68{
d4393e08
PP
69 uint64_t total = get_total_count(counter);
70
0e847b69
PP
71 PRINTF_COUNT("Event", event);
72 PRINTF_COUNT("Stream beginning", stream_begin);
73 PRINTF_COUNT("Stream end", stream_end);
0e847b69
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);
358340ec
PP
79
80 if (counter->count.other > 0) {
0e847b69 81 PRINTF_COUNT("Other (unknown)", other);
358340ec
PP
82 }
83
d6e69534 84 printf("%s%15" PRIu64 " message%s (TOTAL)%s\n",
358340ec
PP
85 bt_common_color_bold(), total, total == 1 ? "" : "s",
86 bt_common_color_reset());
87 counter->last_printed_total = total;
88}
89
90static
d6e69534 91void try_print_count(struct counter *counter, uint64_t msg_count)
358340ec 92{
358340ec
PP
93 if (counter->step == 0) {
94 /* No update */
95 return;
96 }
97
d6e69534 98 counter->at += msg_count;
358340ec 99
d4393e08
PP
100 if (counter->at >= counter->step) {
101 counter->at = 0;
102 print_count(counter);
358340ec
PP
103 putchar('\n');
104 }
105}
106
107static
108void try_print_last(struct counter *counter)
109{
110 const uint64_t total = get_total_count(counter);
111
112 if (total != counter->last_printed_total) {
d4393e08 113 print_count(counter);
358340ec
PP
114 }
115}
116
7c7301d5 117static
358340ec
PP
118void destroy_private_counter_data(struct counter *counter)
119{
718c4b7c
FD
120 if (counter) {
121 bt_self_component_port_input_message_iterator_put_ref(
122 counter->msg_iter);
123 g_free(counter);
124 }
358340ec
PP
125}
126
d94d92ac 127BT_HIDDEN
b19ff26f 128void counter_finalize(bt_self_component_sink *comp)
358340ec
PP
129{
130 struct counter *counter;
131
d94d92ac
PP
132 BT_ASSERT(comp);
133 counter = bt_self_component_get_data(
707b7d35 134 bt_self_component_sink_as_self_component(comp));
f6ccaed9 135 BT_ASSERT(counter);
358340ec 136 try_print_last(counter);
d6e69534 137 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
358340ec
PP
138 g_free(counter);
139}
140
21348968
SM
141struct 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
d94d92ac 147BT_HIDDEN
21a9f056 148bt_component_class_initialize_method_status counter_init(
b19ff26f 149 bt_self_component_sink *component,
59225a3e 150 bt_self_component_sink_configuration *config,
b19ff26f 151 const bt_value *params,
c88dd1cb 152 __attribute__((unused)) void *init_method_data)
358340ec 153{
21348968 154 bt_component_class_initialize_method_status status;
d24d5663 155 bt_self_component_add_port_status add_port_status;
358340ec 156 struct counter *counter = g_new0(struct counter, 1);
b19ff26f
PP
157 const bt_value *step = NULL;
158 const bt_value *hide_zero = NULL;
21348968
SM
159 enum bt_param_validation_status validation_status;
160 gchar *validate_error = NULL;
358340ec
PP
161
162 if (!counter) {
21a9f056 163 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
d94d92ac 164 goto error;
358340ec
PP
165 }
166
82e80647
PP
167 counter->self_comp =
168 bt_self_component_sink_as_self_component(component);
9d87d0ea 169 counter->log_level = bt_component_get_logging_level(
82e80647 170 bt_self_component_as_component(counter->self_comp));
d24d5663 171 add_port_status = bt_self_component_sink_add_input_port(component,
358340ec 172 "in", NULL, NULL);
21348968
SM
173 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
174 status = (int) add_port_status;
d94d92ac 175 goto error;
21348968
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) {
21a9f056 181 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
d24d5663 182 goto error;
21348968
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;
358340ec
PP
188 }
189
190 counter->last_printed_total = -1ULL;
a5bc50e4 191 counter->step = 10000;
05e21286 192 step = bt_value_map_borrow_entry_value_const(params, "step");
fdd3a2da 193 if (step) {
9c08c816 194 counter->step = bt_value_integer_unsigned_get(step);
358340ec
PP
195 }
196
05e21286 197 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
fdd3a2da 198 if (hide_zero) {
fdd3a2da 199 counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
358340ec
PP
200 }
201
d94d92ac 202 bt_self_component_set_data(
707b7d35 203 bt_self_component_sink_as_self_component(component),
d94d92ac 204 counter);
21348968
SM
205
206 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
358340ec
PP
207 goto end;
208
209error:
210 destroy_private_counter_data(counter);
d24d5663 211
358340ec 212end:
21348968 213 g_free(validate_error);
d24d5663 214 return status;
358340ec
PP
215}
216
d94d92ac 217BT_HIDDEN
d24d5663
PP
218bt_component_class_sink_graph_is_configured_method_status
219counter_graph_is_configured(
5badd463 220 bt_self_component_sink *comp)
358340ec 221{
e803df70
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;
358340ec 225 struct counter *counter;
d6e69534 226 bt_self_component_port_input_message_iterator *iterator;
358340ec 227
d94d92ac 228 counter = bt_self_component_get_data(
707b7d35 229 bt_self_component_sink_as_self_component(comp));
f6ccaed9 230 BT_ASSERT(counter);
e803df70
SM
231
232 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component(
ca02df0a 233 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
e803df70
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;
358340ec
PP
237 goto end;
238 }
239
d6e69534
PP
240 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
241 counter->msg_iter, iterator);
358340ec 242
e803df70 243 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
358340ec 244end:
bf55043c 245 return status;
358340ec
PP
246}
247
d94d92ac 248BT_HIDDEN
d24d5663 249bt_component_class_sink_consume_method_status counter_consume(
b19ff26f 250 bt_self_component_sink *comp)
358340ec 251{
d24d5663
PP
252 bt_component_class_sink_consume_method_status status =
253 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
358340ec 254 struct counter *counter;
d24d5663 255 bt_message_iterator_next_status next_status;
d6e69534
PP
256 uint64_t msg_count;
257 bt_message_array_const msgs;
358340ec 258
d94d92ac 259 counter = bt_self_component_get_data(
707b7d35 260 bt_self_component_sink_as_self_component(comp));
98b15851 261 BT_ASSERT_DBG(counter);
358340ec 262
91d81473 263 if (G_UNLIKELY(!counter->msg_iter)) {
358340ec 264 try_print_last(counter);
d24d5663 265 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
358340ec
PP
266 goto end;
267 }
268
d6e69534 269 /* Consume messages */
d24d5663 270 next_status = bt_self_component_port_input_message_iterator_next(
d6e69534 271 counter->msg_iter, &msgs, &msg_count);
d24d5663
PP
272 if (next_status < 0) {
273 status = (int) next_status;
358340ec
PP
274 goto end;
275 }
276
d24d5663
PP
277 switch (next_status) {
278 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
358340ec 279 {
d4393e08
PP
280 uint64_t i;
281
d6e69534
PP
282 for (i = 0; i < msg_count; i++) {
283 const bt_message *msg = msgs[i];
d4393e08 284
98b15851 285 BT_ASSERT_DBG(msg);
d6e69534
PP
286 switch (bt_message_get_type(msg)) {
287 case BT_MESSAGE_TYPE_EVENT:
d4393e08
PP
288 counter->count.event++;
289 break;
0e847b69
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;
b9fd9cbb
FD
296 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
297 counter->count.msg_iter_inactivity++;
d4393e08 298 break;
d6e69534 299 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
d4393e08
PP
300 counter->count.stream_begin++;
301 break;
d6e69534 302 case BT_MESSAGE_TYPE_STREAM_END:
d4393e08
PP
303 counter->count.stream_end++;
304 break;
0e847b69
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++;
d4393e08 310 break;
d4393e08
PP
311 default:
312 counter->count.other++;
358340ec 313 }
d4393e08 314
d6e69534 315 bt_message_put_ref(msg);
358340ec 316 }
d94d92ac 317
d24d5663 318 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
d94d92ac 319 break;
358340ec 320 }
d24d5663
PP
321 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
322 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
d94d92ac 323 goto end;
d24d5663 324 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
d94d92ac 325 try_print_last(counter);
d24d5663 326 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
d94d92ac 327 goto end;
d24d5663
PP
328 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
329 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
d94d92ac 330 goto end;
358340ec
PP
331 default:
332 break;
333 }
334
d6e69534 335 try_print_count(counter, msg_count);
358340ec
PP
336
337end:
d24d5663 338 return status;
358340ec 339}
This page took 0.065139 seconds and 4 git commands to generate.