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-UTILS-COUNTER-FLT"
26 #include <babeltrace2/babeltrace.h>
27 #include "common/babeltrace.h"
28 #include "common/common.h"
29 #include "plugins/plugins-common.h"
30 #include "common/assert.h"
36 #define PRINTF_COUNT(_what, _var, args...) \
38 if (counter->count._var != 0 || !counter->hide_zero) { \
39 printf("%15" PRIu64 " %s message%s\n", \
40 counter->count._var, \
42 counter->count._var == 1 ? "" : "s"); \
47 const char * const in_port_name
= "in";
50 uint64_t get_total_count(struct counter
*counter
)
52 return counter
->count
.event
+
53 counter
->count
.stream_begin
+
54 counter
->count
.stream_end
+
55 counter
->count
.stream_activity_begin
+
56 counter
->count
.stream_activity_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("Stream activity beginning", stream_activity_begin
);
74 PRINTF_COUNT("Stream activity end", stream_activity_end
);
75 PRINTF_COUNT("Packet beginning", packet_begin
);
76 PRINTF_COUNT("Packet end", packet_end
);
77 PRINTF_COUNT("Discarded event", disc_events
);
78 PRINTF_COUNT("Discarded packet", disc_packets
);
79 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity
);
81 if (counter
->count
.other
> 0) {
82 PRINTF_COUNT("Other (unknown)", other
);
85 printf("%s%15" PRIu64
" message%s (TOTAL)%s\n",
86 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
87 bt_common_color_reset());
88 counter
->last_printed_total
= total
;
92 void try_print_count(struct counter
*counter
, uint64_t msg_count
)
94 if (counter
->step
== 0) {
99 counter
->at
+= msg_count
;
101 if (counter
->at
>= counter
->step
) {
103 print_count(counter
);
109 void try_print_last(struct counter
*counter
)
111 const uint64_t total
= get_total_count(counter
);
113 if (total
!= counter
->last_printed_total
) {
114 print_count(counter
);
118 void destroy_private_counter_data(struct counter
*counter
)
120 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
125 void counter_finalize(bt_self_component_sink
*comp
)
127 struct counter
*counter
;
130 counter
= bt_self_component_get_data(
131 bt_self_component_sink_as_self_component(comp
));
133 try_print_last(counter
);
134 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
139 bt_self_component_status
counter_init(
140 bt_self_component_sink
*component
,
141 const bt_value
*params
,
142 UNUSED_VAR
void *init_method_data
)
144 bt_self_component_status ret
;
145 struct counter
*counter
= g_new0(struct counter
, 1);
146 const bt_value
*step
= NULL
;
147 const bt_value
*hide_zero
= NULL
;
150 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
154 ret
= bt_self_component_sink_add_input_port(component
,
156 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
160 counter
->last_printed_total
= -1ULL;
161 counter
->step
= 10000;
162 step
= bt_value_map_borrow_entry_value_const(params
, "step");
164 if (!bt_value_is_unsigned_integer(step
)) {
165 BT_LOGE("`step` parameter: expecting an unsigned integer value: "
166 "type=%s", bt_common_value_type_string(
167 bt_value_get_type(step
)));
171 counter
->step
= bt_value_unsigned_integer_get(step
);
174 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
176 if (!bt_value_is_bool(hide_zero
)) {
177 BT_LOGE("`hide-zero` parameter: expecting a boolean value: "
178 "type=%s", bt_common_value_type_string(
179 bt_value_get_type(hide_zero
)));
183 counter
->hide_zero
= (bool) bt_value_bool_get(hide_zero
);
186 bt_self_component_set_data(
187 bt_self_component_sink_as_self_component(component
),
192 destroy_private_counter_data(counter
);
193 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
200 bt_self_component_status
counter_graph_is_configured(
201 bt_self_component_sink
*comp
)
203 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
204 struct counter
*counter
;
205 bt_self_component_port_input_message_iterator
*iterator
;
207 counter
= bt_self_component_get_data(
208 bt_self_component_sink_as_self_component(comp
));
210 iterator
= bt_self_component_port_input_message_iterator_create(
211 bt_self_component_sink_borrow_input_port_by_name(comp
,
214 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
218 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
219 counter
->msg_iter
, iterator
);
226 bt_self_component_status
counter_consume(
227 bt_self_component_sink
*comp
)
229 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
230 struct counter
*counter
;
231 bt_message_iterator_status it_ret
;
233 bt_message_array_const msgs
;
235 counter
= bt_self_component_get_data(
236 bt_self_component_sink_as_self_component(comp
));
239 if (unlikely(!counter
->msg_iter
)) {
240 try_print_last(counter
);
241 ret
= BT_SELF_COMPONENT_STATUS_END
;
245 /* Consume messages */
246 it_ret
= bt_self_component_port_input_message_iterator_next(
247 counter
->msg_iter
, &msgs
, &msg_count
);
249 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
254 case BT_MESSAGE_ITERATOR_STATUS_OK
:
258 for (i
= 0; i
< msg_count
; i
++) {
259 const bt_message
*msg
= msgs
[i
];
262 switch (bt_message_get_type(msg
)) {
263 case BT_MESSAGE_TYPE_EVENT
:
264 counter
->count
.event
++;
266 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
267 counter
->count
.packet_begin
++;
269 case BT_MESSAGE_TYPE_PACKET_END
:
270 counter
->count
.packet_end
++;
272 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
273 counter
->count
.msg_iter_inactivity
++;
275 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
276 counter
->count
.stream_begin
++;
278 case BT_MESSAGE_TYPE_STREAM_END
:
279 counter
->count
.stream_end
++;
281 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
282 counter
->count
.stream_activity_begin
++;
284 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
285 counter
->count
.stream_activity_end
++;
287 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
288 counter
->count
.disc_events
++;
290 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
291 counter
->count
.disc_packets
++;
294 counter
->count
.other
++;
297 bt_message_put_ref(msg
);
300 ret
= BT_SELF_COMPONENT_STATUS_OK
;
303 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
304 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
306 case BT_MESSAGE_ITERATOR_STATUS_END
:
307 try_print_last(counter
);
308 ret
= BT_SELF_COMPONENT_STATUS_END
;
310 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
311 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
317 try_print_count(counter
, msg_count
);