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 #include <babeltrace/babeltrace.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/common-internal.h>
26 #include <plugins-common.h>
27 #include <babeltrace/assert-internal.h>
33 #define PRINTF_COUNT(_what, _var, args...) \
35 if (counter->count._var != 0 || !counter->hide_zero) { \
36 printf("%15" PRIu64 " %s message%s\n", \
37 counter->count._var, \
39 counter->count._var == 1 ? "" : "s"); \
44 const char * const in_port_name
= "in";
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
.stream_activity_begin
+
53 counter
->count
.stream_activity_end
+
54 counter
->count
.packet_begin
+
55 counter
->count
.packet_end
+
56 counter
->count
.disc_events
+
57 counter
->count
.disc_packets
+
58 counter
->count
.msg_iter_inactivity
+
63 void print_count(struct counter
*counter
)
65 uint64_t total
= get_total_count(counter
);
67 PRINTF_COUNT("Event", event
);
68 PRINTF_COUNT("Stream beginning", stream_begin
);
69 PRINTF_COUNT("Stream end", stream_end
);
70 PRINTF_COUNT("Stream activity beginning", stream_activity_begin
);
71 PRINTF_COUNT("Stream activity end", stream_activity_end
);
72 PRINTF_COUNT("Packet beginning", packet_begin
);
73 PRINTF_COUNT("Packet end", packet_end
);
74 PRINTF_COUNT("Discarded event", disc_events
);
75 PRINTF_COUNT("Discarded packet", disc_packets
);
76 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity
);
78 if (counter
->count
.other
> 0) {
79 PRINTF_COUNT("Other (unknown)", other
);
82 printf("%s%15" PRIu64
" message%s (TOTAL)%s\n",
83 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
84 bt_common_color_reset());
85 counter
->last_printed_total
= total
;
89 void try_print_count(struct counter
*counter
, uint64_t msg_count
)
91 if (counter
->step
== 0) {
96 counter
->at
+= msg_count
;
98 if (counter
->at
>= counter
->step
) {
100 print_count(counter
);
106 void try_print_last(struct counter
*counter
)
108 const uint64_t total
= get_total_count(counter
);
110 if (total
!= counter
->last_printed_total
) {
111 print_count(counter
);
115 void destroy_private_counter_data(struct counter
*counter
)
117 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
122 void counter_finalize(bt_self_component_sink
*comp
)
124 struct counter
*counter
;
127 counter
= bt_self_component_get_data(
128 bt_self_component_sink_as_self_component(comp
));
130 try_print_last(counter
);
131 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
136 bt_self_component_status
counter_init(
137 bt_self_component_sink
*component
,
138 const bt_value
*params
,
139 UNUSED_VAR
void *init_method_data
)
141 bt_self_component_status ret
;
142 struct counter
*counter
= g_new0(struct counter
, 1);
143 const bt_value
*step
= NULL
;
144 const bt_value
*hide_zero
= NULL
;
147 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
151 ret
= bt_self_component_sink_add_input_port(component
,
153 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
157 counter
->last_printed_total
= -1ULL;
158 counter
->step
= 1000;
159 step
= bt_value_map_borrow_entry_value_const(params
, "step");
160 if (step
&& bt_value_is_integer(step
)) {
163 val
= bt_value_integer_get(step
);
165 counter
->step
= (uint64_t) val
;
169 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
170 if (hide_zero
&& bt_value_is_bool(hide_zero
)) {
173 val
= bt_value_bool_get(hide_zero
);
174 counter
->hide_zero
= (bool) val
;
177 bt_self_component_set_data(
178 bt_self_component_sink_as_self_component(component
),
183 destroy_private_counter_data(counter
);
190 bt_self_component_status
counter_graph_is_configured(
191 bt_self_component_sink
*comp
)
193 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
194 struct counter
*counter
;
195 bt_self_component_port_input_message_iterator
*iterator
;
197 counter
= bt_self_component_get_data(
198 bt_self_component_sink_as_self_component(comp
));
200 iterator
= bt_self_component_port_input_message_iterator_create(
201 bt_self_component_sink_borrow_input_port_by_name(comp
,
204 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
208 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
209 counter
->msg_iter
, iterator
);
216 bt_self_component_status
counter_consume(
217 bt_self_component_sink
*comp
)
219 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
220 struct counter
*counter
;
221 bt_message_iterator_status it_ret
;
223 bt_message_array_const msgs
;
225 counter
= bt_self_component_get_data(
226 bt_self_component_sink_as_self_component(comp
));
229 if (unlikely(!counter
->msg_iter
)) {
230 try_print_last(counter
);
231 ret
= BT_SELF_COMPONENT_STATUS_END
;
235 /* Consume messages */
236 it_ret
= bt_self_component_port_input_message_iterator_next(
237 counter
->msg_iter
, &msgs
, &msg_count
);
239 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
244 case BT_MESSAGE_ITERATOR_STATUS_OK
:
248 for (i
= 0; i
< msg_count
; i
++) {
249 const bt_message
*msg
= msgs
[i
];
252 switch (bt_message_get_type(msg
)) {
253 case BT_MESSAGE_TYPE_EVENT
:
254 counter
->count
.event
++;
256 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
257 counter
->count
.packet_begin
++;
259 case BT_MESSAGE_TYPE_PACKET_END
:
260 counter
->count
.packet_end
++;
262 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
263 counter
->count
.msg_iter_inactivity
++;
265 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
266 counter
->count
.stream_begin
++;
268 case BT_MESSAGE_TYPE_STREAM_END
:
269 counter
->count
.stream_end
++;
271 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
272 counter
->count
.stream_activity_begin
++;
274 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
275 counter
->count
.stream_activity_end
++;
277 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
278 counter
->count
.disc_events
++;
280 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
281 counter
->count
.disc_packets
++;
284 counter
->count
.other
++;
287 bt_message_put_ref(msg
);
290 ret
= BT_SELF_COMPONENT_STATUS_OK
;
293 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
294 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
296 case BT_MESSAGE_ITERATOR_STATUS_END
:
297 try_print_last(counter
);
298 ret
= BT_SELF_COMPONENT_STATUS_END
;
300 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
301 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
307 try_print_count(counter
, msg_count
);