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>
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 uint64_t get_total_count(struct counter
*counter
)
45 return counter
->count
.event
+
46 counter
->count
.stream_begin
+
47 counter
->count
.stream_end
+
48 counter
->count
.packet_begin
+
49 counter
->count
.packet_end
+
50 counter
->count
.inactivity
+
51 counter
->count
.discarded_events
+
52 counter
->count
.discarded_packets
+
57 void print_count(struct counter
*counter
, uint64_t total
)
59 PRINTF_COUNT("event", "events", event
);
60 PRINTF_COUNT("stream beginning", "stream beginnings", stream_begin
);
61 PRINTF_COUNT("stream end", "stream ends", stream_end
);
62 PRINTF_COUNT("packet beginning", "packet beginnings", packet_begin
);
63 PRINTF_COUNT("packet end", "packet ends", packet_end
);
64 PRINTF_COUNT("inactivity", "inactivities", inactivity
);
65 PRINTF_COUNT("discarded events notification",
66 "discarded events notifications", discarded_events_notifs
);
67 PRINTF_COUNT(" known discarded event", " known discarded events",
69 PRINTF_COUNT("discarded packets notification",
70 "discarded packets notifications", discarded_packets_notifs
);
71 PRINTF_COUNT(" known discarded packet", " known discarded packets",
74 if (counter
->count
.other
> 0) {
75 PRINTF_COUNT(" other (unknown) notification",
76 " other (unknown) notifications", other
);
79 printf("%s%15" PRIu64
" notification%s (TOTAL)%s\n",
80 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
81 bt_common_color_reset());
82 counter
->last_printed_total
= total
;
86 void try_print_count(struct counter
*counter
)
90 if (counter
->step
== 0) {
95 total
= get_total_count(counter
);
97 if (total
% counter
->step
== 0) {
98 print_count(counter
, total
);
104 void try_print_last(struct counter
*counter
)
106 const uint64_t total
= get_total_count(counter
);
108 if (total
!= counter
->last_printed_total
) {
109 print_count(counter
, total
);
113 void destroy_private_counter_data(struct counter
*counter
)
115 bt_put(counter
->notif_iter
);
119 void counter_finalize(struct bt_private_component
*component
)
121 struct counter
*counter
;
124 counter
= bt_private_component_get_user_data(component
);
126 try_print_last(counter
);
127 bt_put(counter
->notif_iter
);
131 enum bt_component_status
counter_init(struct bt_private_component
*component
,
132 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
134 enum bt_component_status ret
;
135 struct counter
*counter
= g_new0(struct counter
, 1);
136 struct bt_value
*step
= NULL
;
137 struct bt_value
*hide_zero
= NULL
;
140 ret
= BT_COMPONENT_STATUS_NOMEM
;
144 ret
= bt_private_component_sink_add_input_private_port(component
,
146 if (ret
!= BT_COMPONENT_STATUS_OK
) {
150 counter
->last_printed_total
= -1ULL;
151 counter
->step
= 1000;
152 step
= bt_value_map_get(params
, "step");
153 if (bt_value_is_integer(step
)) {
155 int vret
= bt_value_integer_get(step
, &val
);
160 counter
->step
= (uint64_t) val
;
164 hide_zero
= bt_value_map_get(params
, "hide-zero");
165 if (bt_value_is_bool(hide_zero
)) {
167 int vret
= bt_value_bool_get(hide_zero
, &val
);
170 counter
->hide_zero
= (bool) val
;
173 ret
= bt_private_component_set_user_data(component
, counter
);
174 if (ret
!= BT_COMPONENT_STATUS_OK
) {
181 destroy_private_counter_data(counter
);
189 void counter_port_connected(
190 struct bt_private_component
*component
,
191 struct bt_private_port
*self_port
,
192 struct bt_port
*other_port
)
194 struct counter
*counter
;
195 struct bt_notification_iterator
*iterator
;
196 struct bt_private_connection
*connection
;
197 enum bt_connection_status conn_status
;
199 counter
= bt_private_component_get_user_data(component
);
201 connection
= bt_private_port_get_private_connection(self_port
);
203 conn_status
= bt_private_connection_create_notification_iterator(
204 connection
, NULL
, &iterator
);
205 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
206 counter
->error
= true;
210 BT_MOVE(counter
->notif_iter
, iterator
);
216 enum bt_component_status
counter_consume(struct bt_private_component
*component
)
218 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
219 struct bt_notification
*notif
= NULL
;
220 struct counter
*counter
;
221 enum bt_notification_iterator_status it_ret
;
224 counter
= bt_private_component_get_user_data(component
);
227 if (unlikely(counter
->error
)) {
228 ret
= BT_COMPONENT_STATUS_ERROR
;
232 if (unlikely(!counter
->notif_iter
)) {
233 try_print_last(counter
);
234 ret
= BT_COMPONENT_STATUS_END
;
238 /* Consume one notification */
239 it_ret
= bt_notification_iterator_next(counter
->notif_iter
);
241 ret
= BT_COMPONENT_STATUS_ERROR
;
246 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
247 ret
= BT_COMPONENT_STATUS_AGAIN
;
249 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
250 try_print_last(counter
);
251 ret
= BT_COMPONENT_STATUS_END
;
253 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
255 struct bt_notification
*notif
=
256 bt_notification_iterator_get_notification(counter
->notif_iter
);
259 switch (bt_notification_get_type(notif
)) {
260 case BT_NOTIFICATION_TYPE_EVENT
:
261 counter
->count
.event
++;
263 case BT_NOTIFICATION_TYPE_INACTIVITY
:
264 counter
->count
.inactivity
++;
266 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
267 counter
->count
.stream_begin
++;
269 case BT_NOTIFICATION_TYPE_STREAM_END
:
270 counter
->count
.stream_end
++;
272 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
273 counter
->count
.packet_begin
++;
275 case BT_NOTIFICATION_TYPE_PACKET_END
:
276 counter
->count
.packet_end
++;
278 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
279 counter
->count
.discarded_events_notifs
++;
280 count
= bt_notification_discarded_events_get_count(
283 counter
->count
.discarded_events
+= count
;
286 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
287 counter
->count
.discarded_packets_notifs
++;
288 count
= bt_notification_discarded_packets_get_count(
291 counter
->count
.discarded_packets
+= count
;
295 counter
->count
.other
++;
304 try_print_count(counter
);