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_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER"
26 #include <babeltrace2/babeltrace.h>
27 #include "common/macros.h"
28 #include "common/common.h"
29 #include "common/assert.h"
35 #define PRINTF_COUNT(_what, _var, args...) \
37 if (counter->count._var != 0 || !counter->hide_zero) { \
38 printf("%15" PRIu64 " %s message%s\n", \
39 counter->count._var, \
41 counter->count._var == 1 ? "" : "s"); \
46 const char * const in_port_name
= "in";
49 uint64_t get_total_count(struct counter
*counter
)
51 return counter
->count
.event
+
52 counter
->count
.stream_begin
+
53 counter
->count
.stream_end
+
54 counter
->count
.stream_activity_begin
+
55 counter
->count
.stream_activity_end
+
56 counter
->count
.packet_begin
+
57 counter
->count
.packet_end
+
58 counter
->count
.disc_events
+
59 counter
->count
.disc_packets
+
60 counter
->count
.msg_iter_inactivity
+
65 void print_count(struct counter
*counter
)
67 uint64_t total
= get_total_count(counter
);
69 PRINTF_COUNT("Event", event
);
70 PRINTF_COUNT("Stream beginning", stream_begin
);
71 PRINTF_COUNT("Stream end", stream_end
);
72 PRINTF_COUNT("Stream activity beginning", stream_activity_begin
);
73 PRINTF_COUNT("Stream activity end", stream_activity_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
);
117 void destroy_private_counter_data(struct counter
*counter
)
119 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
124 void counter_finalize(bt_self_component_sink
*comp
)
126 struct counter
*counter
;
129 counter
= bt_self_component_get_data(
130 bt_self_component_sink_as_self_component(comp
));
132 try_print_last(counter
);
133 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
138 bt_self_component_status
counter_init(
139 bt_self_component_sink
*component
,
140 const bt_value
*params
,
141 __attribute__((unused
)) void *init_method_data
)
143 bt_self_component_status ret
;
144 struct counter
*counter
= g_new0(struct counter
, 1);
145 const bt_value
*step
= NULL
;
146 const bt_value
*hide_zero
= NULL
;
149 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
153 ret
= bt_self_component_sink_add_input_port(component
,
155 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
159 counter
->last_printed_total
= -1ULL;
160 counter
->step
= 10000;
161 step
= bt_value_map_borrow_entry_value_const(params
, "step");
163 if (!bt_value_is_unsigned_integer(step
)) {
164 BT_LOGE("`step` parameter: expecting an unsigned integer value: "
165 "type=%s", bt_common_value_type_string(
166 bt_value_get_type(step
)));
170 counter
->step
= bt_value_unsigned_integer_get(step
);
173 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
175 if (!bt_value_is_bool(hide_zero
)) {
176 BT_LOGE("`hide-zero` parameter: expecting a boolean value: "
177 "type=%s", bt_common_value_type_string(
178 bt_value_get_type(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
),
191 destroy_private_counter_data(counter
);
192 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
199 bt_self_component_status
counter_graph_is_configured(
200 bt_self_component_sink
*comp
)
202 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
203 struct counter
*counter
;
204 bt_self_component_port_input_message_iterator
*iterator
;
206 counter
= bt_self_component_get_data(
207 bt_self_component_sink_as_self_component(comp
));
209 iterator
= bt_self_component_port_input_message_iterator_create(
210 bt_self_component_sink_borrow_input_port_by_name(comp
,
213 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
217 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
218 counter
->msg_iter
, iterator
);
225 bt_self_component_status
counter_consume(
226 bt_self_component_sink
*comp
)
228 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
229 struct counter
*counter
;
230 bt_message_iterator_status it_ret
;
232 bt_message_array_const msgs
;
234 counter
= bt_self_component_get_data(
235 bt_self_component_sink_as_self_component(comp
));
238 if (G_UNLIKELY(!counter
->msg_iter
)) {
239 try_print_last(counter
);
240 ret
= BT_SELF_COMPONENT_STATUS_END
;
244 /* Consume messages */
245 it_ret
= bt_self_component_port_input_message_iterator_next(
246 counter
->msg_iter
, &msgs
, &msg_count
);
248 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
253 case BT_MESSAGE_ITERATOR_STATUS_OK
:
257 for (i
= 0; i
< msg_count
; i
++) {
258 const bt_message
*msg
= msgs
[i
];
261 switch (bt_message_get_type(msg
)) {
262 case BT_MESSAGE_TYPE_EVENT
:
263 counter
->count
.event
++;
265 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
266 counter
->count
.packet_begin
++;
268 case BT_MESSAGE_TYPE_PACKET_END
:
269 counter
->count
.packet_end
++;
271 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
272 counter
->count
.msg_iter_inactivity
++;
274 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
275 counter
->count
.stream_begin
++;
277 case BT_MESSAGE_TYPE_STREAM_END
:
278 counter
->count
.stream_end
++;
280 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
281 counter
->count
.stream_activity_begin
++;
283 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
284 counter
->count
.stream_activity_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 ret
= BT_SELF_COMPONENT_STATUS_OK
;
302 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
303 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
305 case BT_MESSAGE_ITERATOR_STATUS_END
:
306 try_print_last(counter
);
307 ret
= BT_SELF_COMPONENT_STATUS_END
;
309 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
310 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
316 try_print_count(counter
, msg_count
);