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(
112 void counter_finalize(bt_self_component_sink
*comp
)
114 struct counter
*counter
;
117 counter
= bt_self_component_get_data(
118 bt_self_component_sink_as_self_component(comp
));
120 try_print_last(counter
);
121 bt_message_iterator_put_ref(counter
->msg_iter
);
126 struct bt_param_validation_map_value_entry_descr counter_params
[] = {
127 { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_UNSIGNED_INTEGER
} },
128 { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
129 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
133 bt_component_class_initialize_method_status
counter_init(
134 bt_self_component_sink
*component
,
135 bt_self_component_sink_configuration
*config
,
136 const bt_value
*params
,
137 __attribute__((unused
)) void *init_method_data
)
139 bt_component_class_initialize_method_status status
;
140 bt_self_component_add_port_status add_port_status
;
141 struct counter
*counter
= g_new0(struct counter
, 1);
142 const bt_value
*step
= NULL
;
143 const bt_value
*hide_zero
= NULL
;
144 enum bt_param_validation_status validation_status
;
145 gchar
*validate_error
= NULL
;
148 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
153 bt_self_component_sink_as_self_component(component
);
154 counter
->log_level
= bt_component_get_logging_level(
155 bt_self_component_as_component(counter
->self_comp
));
156 add_port_status
= bt_self_component_sink_add_input_port(component
,
158 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
159 status
= (int) add_port_status
;
163 validation_status
= bt_param_validation_validate(params
,
164 counter_params
, &validate_error
);
165 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
166 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
168 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
169 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
170 BT_COMP_LOGE_APPEND_CAUSE(counter
->self_comp
,
171 "%s", validate_error
);
175 counter
->last_printed_total
= -1ULL;
176 counter
->step
= 10000;
177 step
= bt_value_map_borrow_entry_value_const(params
, "step");
179 counter
->step
= bt_value_integer_unsigned_get(step
);
182 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
184 counter
->hide_zero
= (bool) bt_value_bool_get(hide_zero
);
187 bt_self_component_set_data(
188 bt_self_component_sink_as_self_component(component
),
191 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
195 destroy_private_counter_data(counter
);
198 g_free(validate_error
);
203 bt_component_class_sink_graph_is_configured_method_status
204 counter_graph_is_configured(
205 bt_self_component_sink
*comp
)
207 bt_component_class_sink_graph_is_configured_method_status status
;
208 bt_message_iterator_create_from_sink_component_status
210 struct counter
*counter
;
211 bt_message_iterator
*iterator
;
213 counter
= bt_self_component_get_data(
214 bt_self_component_sink_as_self_component(comp
));
217 msg_iter_status
= bt_message_iterator_create_from_sink_component(
218 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
219 in_port_name
), &iterator
);
220 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
221 status
= (int) msg_iter_status
;
225 BT_MESSAGE_ITERATOR_MOVE_REF(
226 counter
->msg_iter
, iterator
);
228 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
234 bt_component_class_sink_consume_method_status
counter_consume(
235 bt_self_component_sink
*comp
)
237 bt_component_class_sink_consume_method_status status
=
238 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
239 struct counter
*counter
;
240 bt_message_iterator_next_status next_status
;
242 bt_message_array_const msgs
;
244 counter
= bt_self_component_get_data(
245 bt_self_component_sink_as_self_component(comp
));
246 BT_ASSERT_DBG(counter
);
248 if (G_UNLIKELY(!counter
->msg_iter
)) {
249 try_print_last(counter
);
250 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
254 /* Consume messages */
255 next_status
= bt_message_iterator_next(
256 counter
->msg_iter
, &msgs
, &msg_count
);
257 if (next_status
< 0) {
258 status
= (int) next_status
;
262 switch (next_status
) {
263 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
267 for (i
= 0; i
< msg_count
; i
++) {
268 const bt_message
*msg
= msgs
[i
];
271 switch (bt_message_get_type(msg
)) {
272 case BT_MESSAGE_TYPE_EVENT
:
273 counter
->count
.event
++;
275 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
276 counter
->count
.packet_begin
++;
278 case BT_MESSAGE_TYPE_PACKET_END
:
279 counter
->count
.packet_end
++;
281 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
282 counter
->count
.msg_iter_inactivity
++;
284 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
285 counter
->count
.stream_begin
++;
287 case BT_MESSAGE_TYPE_STREAM_END
:
288 counter
->count
.stream_end
++;
290 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
291 counter
->count
.disc_events
++;
293 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
294 counter
->count
.disc_packets
++;
297 counter
->count
.other
++;
300 bt_message_put_ref(msg
);
303 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
306 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
307 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
309 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
310 try_print_last(counter
);
311 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
313 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
314 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;
320 try_print_count(counter
, msg_count
);