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_sing, _what_plur, _var, args...) \
35 if (counter->count._var != 0 || !counter->hide_zero) { \
36 printf("%15" PRIu64 " %s\n", \
37 counter->count._var, \
38 counter->count._var == 1 ? _what_sing : _what_plur); \
43 const char * const in_port_name
= "in";
46 uint64_t get_total_count(struct counter
*counter
)
48 return counter
->count
.event
+
49 counter
->count
.stream_begin
+
50 counter
->count
.stream_end
+
51 counter
->count
.packet_begin
+
52 counter
->count
.packet_end
+
53 counter
->count
.msg_iter_inactivity
+
58 void print_count(struct counter
*counter
)
60 uint64_t total
= get_total_count(counter
);
62 PRINTF_COUNT("event", "events", event
);
63 PRINTF_COUNT("stream beginning", "stream beginnings", stream_begin
);
64 PRINTF_COUNT("stream end", "stream ends", stream_end
);
65 PRINTF_COUNT("packet beginning", "packet beginnings", packet_begin
);
66 PRINTF_COUNT("packet end", "packet ends", packet_end
);
67 PRINTF_COUNT("message iterator inactivity",
68 "message iterator inactivities", msg_iter_inactivity
);
70 if (counter
->count
.other
> 0) {
71 PRINTF_COUNT(" other (unknown) message",
72 " other (unknown) messages", other
);
75 printf("%s%15" PRIu64
" message%s (TOTAL)%s\n",
76 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
77 bt_common_color_reset());
78 counter
->last_printed_total
= total
;
82 void try_print_count(struct counter
*counter
, uint64_t msg_count
)
84 if (counter
->step
== 0) {
89 counter
->at
+= msg_count
;
91 if (counter
->at
>= counter
->step
) {
99 void try_print_last(struct counter
*counter
)
101 const uint64_t total
= get_total_count(counter
);
103 if (total
!= counter
->last_printed_total
) {
104 print_count(counter
);
108 void destroy_private_counter_data(struct counter
*counter
)
110 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
115 void counter_finalize(bt_self_component_sink
*comp
)
117 struct counter
*counter
;
120 counter
= bt_self_component_get_data(
121 bt_self_component_sink_as_self_component(comp
));
123 try_print_last(counter
);
124 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
129 bt_self_component_status
counter_init(
130 bt_self_component_sink
*component
,
131 const bt_value
*params
,
132 UNUSED_VAR
void *init_method_data
)
134 bt_self_component_status ret
;
135 struct counter
*counter
= g_new0(struct counter
, 1);
136 const bt_value
*step
= NULL
;
137 const bt_value
*hide_zero
= NULL
;
140 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
144 ret
= bt_self_component_sink_add_input_port(component
,
146 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
150 counter
->last_printed_total
= -1ULL;
151 counter
->step
= 1000;
152 step
= bt_value_map_borrow_entry_value_const(params
, "step");
153 if (step
&& bt_value_is_integer(step
)) {
156 val
= bt_value_integer_get(step
);
158 counter
->step
= (uint64_t) val
;
162 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
163 if (hide_zero
&& bt_value_is_bool(hide_zero
)) {
166 val
= bt_value_bool_get(hide_zero
);
167 counter
->hide_zero
= (bool) val
;
170 bt_self_component_set_data(
171 bt_self_component_sink_as_self_component(component
),
176 destroy_private_counter_data(counter
);
183 bt_self_component_status
counter_graph_is_configured(
184 bt_self_component_sink
*comp
)
186 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
187 struct counter
*counter
;
188 bt_self_component_port_input_message_iterator
*iterator
;
190 counter
= bt_self_component_get_data(
191 bt_self_component_sink_as_self_component(comp
));
193 iterator
= bt_self_component_port_input_message_iterator_create(
194 bt_self_component_sink_borrow_input_port_by_name(comp
,
197 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
201 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
202 counter
->msg_iter
, iterator
);
209 bt_self_component_status
counter_consume(
210 bt_self_component_sink
*comp
)
212 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
213 struct counter
*counter
;
214 bt_message_iterator_status it_ret
;
216 bt_message_array_const msgs
;
218 counter
= bt_self_component_get_data(
219 bt_self_component_sink_as_self_component(comp
));
222 if (unlikely(!counter
->msg_iter
)) {
223 try_print_last(counter
);
224 ret
= BT_SELF_COMPONENT_STATUS_END
;
228 /* Consume messages */
229 it_ret
= bt_self_component_port_input_message_iterator_next(
230 counter
->msg_iter
, &msgs
, &msg_count
);
232 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
237 case BT_MESSAGE_ITERATOR_STATUS_OK
:
241 for (i
= 0; i
< msg_count
; i
++) {
242 const bt_message
*msg
= msgs
[i
];
245 switch (bt_message_get_type(msg
)) {
246 case BT_MESSAGE_TYPE_EVENT
:
247 counter
->count
.event
++;
249 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
250 counter
->count
.msg_iter_inactivity
++;
252 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
253 counter
->count
.stream_begin
++;
255 case BT_MESSAGE_TYPE_STREAM_END
:
256 counter
->count
.stream_end
++;
258 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
259 counter
->count
.packet_begin
++;
261 case BT_MESSAGE_TYPE_PACKET_END
:
262 counter
->count
.packet_end
++;
265 counter
->count
.other
++;
268 bt_message_put_ref(msg
);
271 ret
= BT_SELF_COMPONENT_STATUS_OK
;
274 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
275 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
277 case BT_MESSAGE_ITERATOR_STATUS_END
:
278 try_print_last(counter
);
279 ret
= BT_SELF_COMPONENT_STATUS_END
;
281 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
282 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
288 try_print_count(counter
, msg_count
);