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 "plugins/comp-logging.h"
28 #include <babeltrace2/babeltrace.h>
29 #include "common/macros.h"
30 #include "common/common.h"
31 #include "common/assert.h"
37 #define PRINTF_COUNT(_what, _var, args...) \
39 if (counter->count._var != 0 || !counter->hide_zero) { \
40 printf("%15" PRIu64 " %s message%s\n", \
41 counter->count._var, \
43 counter->count._var == 1 ? "" : "s"); \
48 const char * const in_port_name
= "in";
51 uint64_t get_total_count(struct counter
*counter
)
53 return counter
->count
.event
+
54 counter
->count
.stream_begin
+
55 counter
->count
.stream_end
+
56 counter
->count
.stream_activity_begin
+
57 counter
->count
.stream_activity_end
+
58 counter
->count
.packet_begin
+
59 counter
->count
.packet_end
+
60 counter
->count
.disc_events
+
61 counter
->count
.disc_packets
+
62 counter
->count
.msg_iter_inactivity
+
67 void print_count(struct counter
*counter
)
69 uint64_t total
= get_total_count(counter
);
71 PRINTF_COUNT("Event", event
);
72 PRINTF_COUNT("Stream beginning", stream_begin
);
73 PRINTF_COUNT("Stream end", stream_end
);
74 PRINTF_COUNT("Stream activity beginning", stream_activity_begin
);
75 PRINTF_COUNT("Stream activity end", stream_activity_end
);
76 PRINTF_COUNT("Packet beginning", packet_begin
);
77 PRINTF_COUNT("Packet end", packet_end
);
78 PRINTF_COUNT("Discarded event", disc_events
);
79 PRINTF_COUNT("Discarded packet", disc_packets
);
80 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity
);
82 if (counter
->count
.other
> 0) {
83 PRINTF_COUNT("Other (unknown)", other
);
86 printf("%s%15" PRIu64
" message%s (TOTAL)%s\n",
87 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
88 bt_common_color_reset());
89 counter
->last_printed_total
= total
;
93 void try_print_count(struct counter
*counter
, uint64_t msg_count
)
95 if (counter
->step
== 0) {
100 counter
->at
+= msg_count
;
102 if (counter
->at
>= counter
->step
) {
104 print_count(counter
);
110 void try_print_last(struct counter
*counter
)
112 const uint64_t total
= get_total_count(counter
);
114 if (total
!= counter
->last_printed_total
) {
115 print_count(counter
);
119 void destroy_private_counter_data(struct counter
*counter
)
121 bt_self_component_port_input_message_iterator_put_ref(counter
->msg_iter
);
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
);
140 bt_self_component_status
counter_init(
141 bt_self_component_sink
*component
,
142 const bt_value
*params
,
143 __attribute__((unused
)) void *init_method_data
)
145 bt_self_component_status ret
;
146 struct counter
*counter
= g_new0(struct counter
, 1);
147 const bt_value
*step
= NULL
;
148 const bt_value
*hide_zero
= NULL
;
151 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
156 bt_self_component_sink_as_self_component(component
);
157 counter
->log_level
= bt_component_get_logging_level(
158 bt_self_component_as_component(counter
->self_comp
));
159 ret
= bt_self_component_sink_add_input_port(component
,
161 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
165 counter
->last_printed_total
= -1ULL;
166 counter
->step
= 10000;
167 step
= bt_value_map_borrow_entry_value_const(params
, "step");
169 if (!bt_value_is_unsigned_integer(step
)) {
170 BT_COMP_LOGE("`step` parameter: expecting an unsigned integer value: "
171 "type=%s", bt_common_value_type_string(
172 bt_value_get_type(step
)));
176 counter
->step
= bt_value_unsigned_integer_get(step
);
179 hide_zero
= bt_value_map_borrow_entry_value_const(params
, "hide-zero");
181 if (!bt_value_is_bool(hide_zero
)) {
182 BT_COMP_LOGE("`hide-zero` parameter: expecting a boolean value: "
183 "type=%s", bt_common_value_type_string(
184 bt_value_get_type(hide_zero
)));
188 counter
->hide_zero
= (bool) bt_value_bool_get(hide_zero
);
191 bt_self_component_set_data(
192 bt_self_component_sink_as_self_component(component
),
197 destroy_private_counter_data(counter
);
198 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
205 bt_self_component_status
counter_graph_is_configured(
206 bt_self_component_sink
*comp
)
208 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
209 struct counter
*counter
;
210 bt_self_component_port_input_message_iterator
*iterator
;
212 counter
= bt_self_component_get_data(
213 bt_self_component_sink_as_self_component(comp
));
215 iterator
= bt_self_component_port_input_message_iterator_create(
216 bt_self_component_sink_borrow_input_port_by_name(comp
,
219 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
223 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
224 counter
->msg_iter
, iterator
);
231 bt_self_component_status
counter_consume(
232 bt_self_component_sink
*comp
)
234 bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
235 struct counter
*counter
;
236 bt_message_iterator_status it_ret
;
238 bt_message_array_const msgs
;
240 counter
= bt_self_component_get_data(
241 bt_self_component_sink_as_self_component(comp
));
244 if (G_UNLIKELY(!counter
->msg_iter
)) {
245 try_print_last(counter
);
246 ret
= BT_SELF_COMPONENT_STATUS_END
;
250 /* Consume messages */
251 it_ret
= bt_self_component_port_input_message_iterator_next(
252 counter
->msg_iter
, &msgs
, &msg_count
);
254 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
259 case BT_MESSAGE_ITERATOR_STATUS_OK
:
263 for (i
= 0; i
< msg_count
; i
++) {
264 const bt_message
*msg
= msgs
[i
];
267 switch (bt_message_get_type(msg
)) {
268 case BT_MESSAGE_TYPE_EVENT
:
269 counter
->count
.event
++;
271 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
272 counter
->count
.packet_begin
++;
274 case BT_MESSAGE_TYPE_PACKET_END
:
275 counter
->count
.packet_end
++;
277 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
278 counter
->count
.msg_iter_inactivity
++;
280 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
281 counter
->count
.stream_begin
++;
283 case BT_MESSAGE_TYPE_STREAM_END
:
284 counter
->count
.stream_end
++;
286 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
287 counter
->count
.stream_activity_begin
++;
289 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
290 counter
->count
.stream_activity_end
++;
292 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
293 counter
->count
.disc_events
++;
295 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
296 counter
->count
.disc_packets
++;
299 counter
->count
.other
++;
302 bt_message_put_ref(msg
);
305 ret
= BT_SELF_COMPONENT_STATUS_OK
;
308 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
309 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
311 case BT_MESSAGE_ITERATOR_STATUS_END
:
312 try_print_last(counter
);
313 ret
= BT_SELF_COMPONENT_STATUS_END
;
315 case BT_MESSAGE_ITERATOR_STATUS_NOMEM
:
316 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
322 try_print_count(counter
, msg_count
);