2 * SPDX-License-Identifier: MIT
4 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
7 #define BT_COMP_LOG_SELF_COMP (counter->self_comp)
8 #define BT_LOG_OUTPUT_LEVEL (counter->log_level)
9 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER"
10 #include "logging/comp-logging.h"
12 #include <babeltrace2/babeltrace.h>
13 #include "common/macros.h"
14 #include "common/common.h"
15 #include "common/assert.h"
19 #include "plugins/common/param-validation/param-validation.h"
23 #define PRINTF_COUNT(_what, _var, args...) \
25 if (counter->count._var != 0 || !counter->hide_zero) { \
26 printf("%15" PRIu64 " %s message%s\n", \
27 counter->count._var, \
29 counter->count._var == 1 ? "" : "s"); \
34 const char * const in_port_name
= "in";
37 uint64_t get_total_count(struct counter
*counter
)
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
+
44 counter
->count
.disc_events
+
45 counter
->count
.disc_packets
+
46 counter
->count
.msg_iter_inactivity
+
51 void print_count(struct counter
*counter
)
53 uint64_t total
= get_total_count(counter
);
55 PRINTF_COUNT("Event", event
);
56 PRINTF_COUNT("Stream beginning", stream_begin
);
57 PRINTF_COUNT("Stream end", stream_end
);
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
);
64 if (counter
->count
.other
> 0) {
65 PRINTF_COUNT("Other (unknown)", other
);
68 printf("%s%15" PRIu64
" message%s (TOTAL)%s\n",
69 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
70 bt_common_color_reset());
71 counter
->last_printed_total
= total
;
75 void try_print_count(struct counter
*counter
, uint64_t msg_count
)
77 if (counter
->step
== 0) {
82 counter
->at
+= msg_count
;
84 if (counter
->at
>= counter
->step
) {
92 void try_print_last(struct counter
*counter
)
94 const uint64_t total
= get_total_count(counter
);
96 if (total
!= counter
->last_printed_total
) {
102 void destroy_private_counter_data(struct counter
*counter
)
105 bt_message_iterator_put_ref(
111 void counter_finalize(bt_self_component_sink
*comp
)
113 struct counter
*counter
;
116 counter
= bt_self_component_get_data(
117 bt_self_component_sink_as_self_component(comp
));
119 try_print_last(counter
);
120 bt_message_iterator_put_ref(counter
->msg_iter
);
125 struct 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
131 bt_component_class_initialize_method_status
counter_init(
132 bt_self_component_sink
*component
,
133 bt_self_component_sink_configuration
*config
,
134 const bt_value
*params
,
135 __attribute__((unused
)) void *init_method_data
)
137 bt_component_class_initialize_method_status status
;
138 bt_self_component_add_port_status add_port_status
;
139 struct counter
*counter
= g_new0(struct counter
, 1);
140 const bt_value
*step
= NULL
;
141 const bt_value
*hide_zero
= NULL
;
142 enum bt_param_validation_status validation_status
;
143 gchar
*validate_error
= NULL
;
146 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
151 bt_self_component_sink_as_self_component(component
);
152 counter
->log_level
= bt_component_get_logging_level(
153 bt_self_component_as_component(counter
->self_comp
));
154 add_port_status
= bt_self_component_sink_add_input_port(component
,
156 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
157 status
= (int) add_port_status
;
161 validation_status
= bt_param_validation_validate(params
,
162 counter_params
, &validate_error
);
163 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
164 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
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
);
173 counter
->last_printed_total
= -1ULL;
174 counter
->step
= 10000;
175 step
= bt_value_map_borrow_entry_value_const(params
, "step");
177 counter
->step
= bt_value_integer_unsigned_get(step
);
180 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
182 counter
->hide_zero
= (bool) bt_value_bool_get(hide_zero
);
185 bt_self_component_set_data(
186 bt_self_component_sink_as_self_component(component
),
189 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
193 destroy_private_counter_data(counter
);
196 g_free(validate_error
);
200 bt_component_class_sink_graph_is_configured_method_status
201 counter_graph_is_configured(
202 bt_self_component_sink
*comp
)
204 bt_component_class_sink_graph_is_configured_method_status status
;
205 bt_message_iterator_create_from_sink_component_status
207 struct counter
*counter
;
208 bt_message_iterator
*iterator
;
210 counter
= bt_self_component_get_data(
211 bt_self_component_sink_as_self_component(comp
));
214 msg_iter_status
= bt_message_iterator_create_from_sink_component(
215 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
216 in_port_name
), &iterator
);
217 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
218 status
= (int) msg_iter_status
;
222 BT_MESSAGE_ITERATOR_MOVE_REF(
223 counter
->msg_iter
, iterator
);
225 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
230 bt_component_class_sink_consume_method_status
counter_consume(
231 bt_self_component_sink
*comp
)
233 bt_component_class_sink_consume_method_status status
=
234 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
235 struct counter
*counter
;
236 bt_message_iterator_next_status next_status
;
238 bt_message_array_const msgs
;
240 counter
= bt_self_component_get_data(
241 bt_self_component_sink_as_self_component(comp
));
242 BT_ASSERT_DBG(counter
);
244 if (G_UNLIKELY(!counter
->msg_iter
)) {
245 try_print_last(counter
);
246 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
250 /* Consume messages */
251 next_status
= bt_message_iterator_next(
252 counter
->msg_iter
, &msgs
, &msg_count
);
253 if (next_status
< 0) {
254 status
= (int) next_status
;
258 switch (next_status
) {
259 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
263 for (i
= 0; i
< msg_count
; i
++) {
264 const bt_message
*msg
= msgs
[i
];
267 switch (bt_message_get_type(msg
)) {
268 case BT_MESSAGE_TYPE_EVENT
:
269 counter
->count
.event
++;
271 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
272 counter
->count
.packet_begin
++;
274 case BT_MESSAGE_TYPE_PACKET_END
:
275 counter
->count
.packet_end
++;
277 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
278 counter
->count
.msg_iter_inactivity
++;
280 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
281 counter
->count
.stream_begin
++;
283 case BT_MESSAGE_TYPE_STREAM_END
:
284 counter
->count
.stream_end
++;
286 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
287 counter
->count
.disc_events
++;
289 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
290 counter
->count
.disc_packets
++;
293 counter
->count
.other
++;
296 bt_message_put_ref(msg
);
299 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
302 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
303 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
305 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
306 try_print_last(counter
);
307 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
309 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
310 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;
316 try_print_count(counter
, msg_count
);