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"
34 #include "plugins/common/param-validation/param-validation.h"
38 #define PRINTF_COUNT(_what, _var, args...) \
40 if (counter->count._var != 0 || !counter->hide_zero) { \
41 printf("%15" PRIu64 " %s message%s\n", \
42 counter->count._var, \
44 counter->count._var == 1 ? "" : "s"); \
49 const char * const in_port_name
= "in";
52 uint64_t get_total_count(struct counter
*counter
)
54 return counter
->count
.event
+
55 counter
->count
.stream_begin
+
56 counter
->count
.stream_end
+
57 counter
->count
.packet_begin
+
58 counter
->count
.packet_end
+
59 counter
->count
.disc_events
+
60 counter
->count
.disc_packets
+
61 counter
->count
.msg_iter_inactivity
+
66 void print_count(struct counter
*counter
)
68 uint64_t total
= get_total_count(counter
);
70 PRINTF_COUNT("Event", event
);
71 PRINTF_COUNT("Stream beginning", stream_begin
);
72 PRINTF_COUNT("Stream end", stream_end
);
73 PRINTF_COUNT("Packet beginning", packet_begin
);
74 PRINTF_COUNT("Packet end", packet_end
);
75 PRINTF_COUNT("Discarded event", disc_events
);
76 PRINTF_COUNT("Discarded packet", disc_packets
);
77 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity
);
79 if (counter
->count
.other
> 0) {
80 PRINTF_COUNT("Other (unknown)", other
);
83 printf("%s%15" PRIu64
" message%s (TOTAL)%s\n",
84 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
85 bt_common_color_reset());
86 counter
->last_printed_total
= total
;
90 void try_print_count(struct counter
*counter
, uint64_t msg_count
)
92 if (counter
->step
== 0) {
97 counter
->at
+= msg_count
;
99 if (counter
->at
>= counter
->step
) {
101 print_count(counter
);
107 void try_print_last(struct counter
*counter
)
109 const uint64_t total
= get_total_count(counter
);
111 if (total
!= counter
->last_printed_total
) {
112 print_count(counter
);
116 void destroy_private_counter_data(struct counter
*counter
)
119 bt_self_component_port_input_message_iterator_put_ref(
126 void counter_finalize(bt_self_component_sink
*comp
)
128 struct counter
*counter
;
131 counter
= bt_self_component_get_data(
132 bt_self_component_sink_as_self_component(comp
));
134 try_print_last(counter
);
135 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
139 struct bt_param_validation_map_value_entry_descr counter_params
[] = {
140 { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_UNSIGNED_INTEGER
} },
141 { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
142 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
146 bt_component_class_initialize_method_status
counter_init(
147 bt_self_component_sink
*component
,
148 bt_self_component_sink_configuration
*config
,
149 const bt_value
*params
,
150 __attribute__((unused
)) void *init_method_data
)
152 bt_component_class_initialize_method_status status
;
153 bt_self_component_add_port_status add_port_status
;
154 struct counter
*counter
= g_new0(struct counter
, 1);
155 const bt_value
*step
= NULL
;
156 const bt_value
*hide_zero
= NULL
;
157 enum bt_param_validation_status validation_status
;
158 gchar
*validate_error
= NULL
;
161 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
166 bt_self_component_sink_as_self_component(component
);
167 counter
->log_level
= bt_component_get_logging_level(
168 bt_self_component_as_component(counter
->self_comp
));
169 add_port_status
= bt_self_component_sink_add_input_port(component
,
171 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
172 status
= (int) add_port_status
;
176 validation_status
= bt_param_validation_validate(params
,
177 counter_params
, &validate_error
);
178 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
179 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
181 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
182 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
183 BT_COMP_LOGE_APPEND_CAUSE(counter
->self_comp
,
184 "%s", validate_error
);
188 counter
->last_printed_total
= -1ULL;
189 counter
->step
= 10000;
190 step
= bt_value_map_borrow_entry_value_const(params
, "step");
192 counter
->step
= bt_value_integer_unsigned_get(step
);
195 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
197 counter
->hide_zero
= (bool) bt_value_bool_get(hide_zero
);
200 bt_self_component_set_data(
201 bt_self_component_sink_as_self_component(component
),
204 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
208 destroy_private_counter_data(counter
);
211 g_free(validate_error
);
216 bt_component_class_sink_graph_is_configured_method_status
217 counter_graph_is_configured(
218 bt_self_component_sink
*comp
)
220 bt_component_class_sink_graph_is_configured_method_status status
;
221 bt_self_component_port_input_message_iterator_create_from_sink_component_status
223 struct counter
*counter
;
224 bt_self_component_port_input_message_iterator
*iterator
;
226 counter
= bt_self_component_get_data(
227 bt_self_component_sink_as_self_component(comp
));
230 msg_iter_status
= bt_self_component_port_input_message_iterator_create_from_sink_component(
231 comp
, bt_self_component_sink_borrow_input_port_by_name(comp
,
232 in_port_name
), &iterator
);
233 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK
) {
234 status
= (int) msg_iter_status
;
238 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
239 counter
->msg_iter
, iterator
);
241 status
= BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK
;
247 bt_component_class_sink_consume_method_status
counter_consume(
248 bt_self_component_sink
*comp
)
250 bt_component_class_sink_consume_method_status status
=
251 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
252 struct counter
*counter
;
253 bt_message_iterator_next_status next_status
;
255 bt_message_array_const msgs
;
257 counter
= bt_self_component_get_data(
258 bt_self_component_sink_as_self_component(comp
));
261 if (G_UNLIKELY(!counter
->msg_iter
)) {
262 try_print_last(counter
);
263 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
267 /* Consume messages */
268 next_status
= bt_self_component_port_input_message_iterator_next(
269 counter
->msg_iter
, &msgs
, &msg_count
);
270 if (next_status
< 0) {
271 status
= (int) next_status
;
275 switch (next_status
) {
276 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
280 for (i
= 0; i
< msg_count
; i
++) {
281 const bt_message
*msg
= msgs
[i
];
284 switch (bt_message_get_type(msg
)) {
285 case BT_MESSAGE_TYPE_EVENT
:
286 counter
->count
.event
++;
288 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
289 counter
->count
.packet_begin
++;
291 case BT_MESSAGE_TYPE_PACKET_END
:
292 counter
->count
.packet_end
++;
294 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
295 counter
->count
.msg_iter_inactivity
++;
297 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
298 counter
->count
.stream_begin
++;
300 case BT_MESSAGE_TYPE_STREAM_END
:
301 counter
->count
.stream_end
++;
303 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
304 counter
->count
.disc_events
++;
306 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
307 counter
->count
.disc_packets
++;
310 counter
->count
.other
++;
313 bt_message_put_ref(msg
);
316 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK
;
319 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
320 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN
;
322 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
:
323 try_print_last(counter
);
324 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END
;
326 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
327 status
= BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR
;
333 try_print_count(counter
, msg_count
);