2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #define BT_COMP_LOG_SELF_COMP (counter->self_comp)
24 #define BT_LOG_OUTPUT_LEVEL (counter->log_level)
25 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER"
26 #include "logging/comp-logging.h"
28 #include <babeltrace2/babeltrace.h>
29 #include "common/macros.h"
30 #include "common/common.h"
31 #include "common/assert.h"
35 #include "plugins/common/param-validation/param-validation.h"
39 #define PRINTF_COUNT(_what, _var, args...) \
41 if (counter->count._var != 0 || !counter->hide_zero) { \
42 printf("%15" PRIu64 " %s message%s\n", \
43 counter->count._var, \
45 counter->count._var == 1 ? "" : "s"); \
50 const char * const in_port_name
= "in";
53 uint64_t get_total_count(struct counter
*counter
)
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
+
60 counter
->count
.disc_events
+
61 counter
->count
.disc_packets
+
62 counter
->count
.msg_iter_inactivity
+
67 void print_count(struct counter
*counter
)
69 uint64_t total
= get_total_count(counter
);
71 PRINTF_COUNT("Event", event
);
72 PRINTF_COUNT("Stream beginning", stream_begin
);
73 PRINTF_COUNT("Stream end", stream_end
);
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
);
80 if (counter
->count
.other
> 0) {
81 PRINTF_COUNT("Other (unknown)", other
);
84 printf("%s%15" PRIu64
" message%s (TOTAL)%s\n",
85 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
86 bt_common_color_reset());
87 counter
->last_printed_total
= total
;
91 void try_print_count(struct counter
*counter
, uint64_t msg_count
)
93 if (counter
->step
== 0) {
98 counter
->at
+= msg_count
;
100 if (counter
->at
>= counter
->step
) {
102 print_count(counter
);
108 void try_print_last(struct counter
*counter
)
110 const uint64_t total
= get_total_count(counter
);
112 if (total
!= counter
->last_printed_total
) {
113 print_count(counter
);
118 void destroy_private_counter_data(struct counter
*counter
)
121 bt_self_component_port_input_message_iterator_put_ref(
128 void counter_finalize(bt_self_component_sink
*comp
)
130 struct counter
*counter
;
133 counter
= bt_self_component_get_data(
134 bt_self_component_sink_as_self_component(comp
));
136 try_print_last(counter
);
137 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
141 struct 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
148 bt_component_class_initialize_method_status
counter_init(
149 bt_self_component_sink
*component
,
150 bt_self_component_sink_configuration
*config
,
151 const bt_value
*params
,
152 __attribute__((unused
)) void *init_method_data
)
154 bt_component_class_initialize_method_status status
;
155 bt_self_component_add_port_status add_port_status
;
156 struct counter
*counter
= g_new0(struct counter
, 1);
157 const bt_value
*step
= NULL
;
158 const bt_value
*hide_zero
= NULL
;
159 enum bt_param_validation_status validation_status
;
160 gchar
*validate_error
= NULL
;
163 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
168 bt_self_component_sink_as_self_component(component
);
169 counter
->log_level
= bt_component_get_logging_level(
170 bt_self_component_as_component(counter
->self_comp
));
171 add_port_status
= bt_self_component_sink_add_input_port(component
,
173 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
174 status
= (int) add_port_status
;
178 validation_status
= bt_param_validation_validate(params
,
179 counter_params
, &validate_error
);
180 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
181 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
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
);
190 counter
->last_printed_total
= -1ULL;
191 counter
->step
= 10000;
192 step
= bt_value_map_borrow_entry_value_const(params
, "step");
194 counter
->step
= bt_value_integer_unsigned_get(step
);
197 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
199 counter
->hide_zero
= (bool) bt_value_bool_get(hide_zero
);
202 bt_self_component_set_data(
203 bt_self_component_sink_as_self_component(component
),
206 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
210 destroy_private_counter_data(counter
);
213 g_free(validate_error
);
218 bt_component_class_sink_graph_is_configured_method_status
219 counter_graph_is_configured(
220 bt_self_component_sink
*comp
)
222 bt_component_class_sink_graph_is_configured_method_status status
;
223 bt_self_component_port_input_message_iterator_create_from_sink_component_status
225 struct counter
*counter
;
226 bt_self_component_port_input_message_iterator
*iterator
;
228 counter
= bt_self_component_get_data(
229 bt_self_component_sink_as_self_component(comp
));
232 msg_iter_status
= bt_self_component_port_input_message_iterator_create_from_sink_component(
233 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
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
;
240 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
241 counter
->msg_iter
, iterator
);
243 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
249 bt_component_class_sink_consume_method_status
counter_consume(
250 bt_self_component_sink
*comp
)
252 bt_component_class_sink_consume_method_status status
=
253 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
254 struct counter
*counter
;
255 bt_message_iterator_next_status next_status
;
257 bt_message_array_const msgs
;
259 counter
= bt_self_component_get_data(
260 bt_self_component_sink_as_self_component(comp
));
261 BT_ASSERT_DBG(counter
);
263 if (G_UNLIKELY(!counter
->msg_iter
)) {
264 try_print_last(counter
);
265 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
269 /* Consume messages */
270 next_status
= bt_self_component_port_input_message_iterator_next(
271 counter
->msg_iter
, &msgs
, &msg_count
);
272 if (next_status
< 0) {
273 status
= (int) next_status
;
277 switch (next_status
) {
278 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
282 for (i
= 0; i
< msg_count
; i
++) {
283 const bt_message
*msg
= msgs
[i
];
286 switch (bt_message_get_type(msg
)) {
287 case BT_MESSAGE_TYPE_EVENT
:
288 counter
->count
.event
++;
290 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
291 counter
->count
.packet_begin
++;
293 case BT_MESSAGE_TYPE_PACKET_END
:
294 counter
->count
.packet_end
++;
296 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
297 counter
->count
.msg_iter_inactivity
++;
299 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
300 counter
->count
.stream_begin
++;
302 case BT_MESSAGE_TYPE_STREAM_END
:
303 counter
->count
.stream_end
++;
305 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
306 counter
->count
.disc_events
++;
308 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
309 counter
->count
.disc_packets
++;
312 counter
->count
.other
++;
315 bt_message_put_ref(msg
);
318 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
321 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
322 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
324 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
325 try_print_last(counter
);
326 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
328 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
329 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;
335 try_print_count(counter
, msg_count
);