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";
36 bt_component_class_get_supported_mip_versions_method_status
37 counter_supported_mip_versions(
38 bt_self_component_class_sink
*self_component_class
__attribute__((unused
)),
39 const bt_value
*params
__attribute__((unused
)),
40 void *initialize_method_data
__attribute__((unused
)),
41 bt_logging_level logging_level
__attribute__((unused
)),
42 bt_integer_range_set_unsigned
*supported_versions
) {
43 return (int) bt_integer_range_set_unsigned_add_range(supported_versions
, 0, 1);
47 uint64_t get_total_count(struct counter
*counter
)
49 return counter
->count
.event
+
50 counter
->count
.stream_begin
+
51 counter
->count
.stream_end
+
52 counter
->count
.packet_begin
+
53 counter
->count
.packet_end
+
54 counter
->count
.disc_events
+
55 counter
->count
.disc_packets
+
56 counter
->count
.msg_iter_inactivity
+
61 void print_count(struct counter
*counter
)
63 uint64_t total
= get_total_count(counter
);
65 PRINTF_COUNT("Event", event
);
66 PRINTF_COUNT("Stream beginning", stream_begin
);
67 PRINTF_COUNT("Stream end", stream_end
);
68 PRINTF_COUNT("Packet beginning", packet_begin
);
69 PRINTF_COUNT("Packet end", packet_end
);
70 PRINTF_COUNT("Discarded event", disc_events
);
71 PRINTF_COUNT("Discarded packet", disc_packets
);
72 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity
);
74 if (counter
->count
.other
> 0) {
75 PRINTF_COUNT("Other (unknown)", other
);
78 printf("%s%15" PRIu64
" message%s (TOTAL)%s\n",
79 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
80 bt_common_color_reset());
81 counter
->last_printed_total
= total
;
85 void try_print_count(struct counter
*counter
, uint64_t msg_count
)
87 if (counter
->step
== 0) {
92 counter
->at
+= msg_count
;
94 if (counter
->at
>= counter
->step
) {
102 void try_print_last(struct counter
*counter
)
104 const uint64_t total
= get_total_count(counter
);
106 if (total
!= counter
->last_printed_total
) {
107 print_count(counter
);
112 void destroy_private_counter_data(struct counter
*counter
)
115 bt_message_iterator_put_ref(
121 void counter_finalize(bt_self_component_sink
*comp
)
123 struct counter
*counter
;
126 counter
= bt_self_component_get_data(
127 bt_self_component_sink_as_self_component(comp
));
129 try_print_last(counter
);
130 bt_message_iterator_put_ref(counter
->msg_iter
);
135 struct bt_param_validation_map_value_entry_descr counter_params
[] = {
136 { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_UNSIGNED_INTEGER
} },
137 { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
138 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
141 bt_component_class_initialize_method_status
counter_init(
142 bt_self_component_sink
*component
,
143 bt_self_component_sink_configuration
*config
__attribute__((unused
)),
144 const bt_value
*params
,
145 void *init_method_data
__attribute__((unused
)))
147 bt_component_class_initialize_method_status status
;
148 bt_self_component_add_port_status add_port_status
;
149 struct counter
*counter
= g_new0(struct counter
, 1);
150 const bt_value
*step
= NULL
;
151 const bt_value
*hide_zero
= NULL
;
152 enum bt_param_validation_status validation_status
;
153 gchar
*validate_error
= NULL
;
156 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
161 bt_self_component_sink_as_self_component(component
);
162 counter
->log_level
= bt_component_get_logging_level(
163 bt_self_component_as_component(counter
->self_comp
));
164 add_port_status
= bt_self_component_sink_add_input_port(component
,
166 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
167 status
= (int) add_port_status
;
171 validation_status
= bt_param_validation_validate(params
,
172 counter_params
, &validate_error
);
173 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
174 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
176 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
177 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
178 BT_COMP_LOGE_APPEND_CAUSE(counter
->self_comp
,
179 "%s", validate_error
);
183 counter
->last_printed_total
= -1ULL;
184 counter
->step
= 10000;
185 step
= bt_value_map_borrow_entry_value_const(params
, "step");
187 counter
->step
= bt_value_integer_unsigned_get(step
);
190 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
192 counter
->hide_zero
= (bool) bt_value_bool_get(hide_zero
);
195 bt_self_component_set_data(
196 bt_self_component_sink_as_self_component(component
),
199 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
203 destroy_private_counter_data(counter
);
206 g_free(validate_error
);
210 bt_component_class_sink_graph_is_configured_method_status
211 counter_graph_is_configured(
212 bt_self_component_sink
*comp
)
214 bt_component_class_sink_graph_is_configured_method_status status
;
215 bt_message_iterator_create_from_sink_component_status
217 struct counter
*counter
;
218 bt_message_iterator
*iterator
;
220 counter
= bt_self_component_get_data(
221 bt_self_component_sink_as_self_component(comp
));
224 msg_iter_status
= bt_message_iterator_create_from_sink_component(
225 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
226 in_port_name
), &iterator
);
227 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
228 status
= (int) msg_iter_status
;
232 BT_MESSAGE_ITERATOR_MOVE_REF(
233 counter
->msg_iter
, iterator
);
235 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
240 bt_component_class_sink_consume_method_status
counter_consume(
241 bt_self_component_sink
*comp
)
243 struct counter
*counter
;
244 bt_message_iterator_next_status next_status
;
246 bt_message_array_const msgs
;
247 bt_self_component
*self_comp
=
248 bt_self_component_sink_as_self_component(comp
);
250 counter
= bt_self_component_get_data(self_comp
);
251 BT_ASSERT_DBG(counter
);
252 BT_ASSERT_DBG(counter
->msg_iter
);
254 /* Consume messages */
255 next_status
= bt_message_iterator_next(
256 counter
->msg_iter
, &msgs
, &msg_count
);
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 try_print_count(counter
, msg_count
);
302 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
303 try_print_last(counter
);
305 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
:
306 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
307 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(self_comp
,
308 "Failed to get messages from upstream component");
314 return (int) next_status
;