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/plugin/plugin-dev.h>
24 #include <babeltrace/graph/connection.h>
25 #include <babeltrace/graph/component.h>
26 #include <babeltrace/graph/private-component.h>
27 #include <babeltrace/graph/private-component-sink.h>
28 #include <babeltrace/graph/private-port.h>
29 #include <babeltrace/graph/port.h>
30 #include <babeltrace/graph/private-connection.h>
31 #include <babeltrace/graph/component-sink.h>
32 #include <babeltrace/graph/notification-iterator.h>
33 #include <babeltrace/graph/notification.h>
34 #include <babeltrace/graph/notification-discarded-events.h>
35 #include <babeltrace/graph/notification-discarded-packets.h>
36 #include <babeltrace/babeltrace-internal.h>
37 #include <babeltrace/common-internal.h>
38 #include <babeltrace/values.h>
39 #include <plugins-common.h>
46 #define PRINTF_COUNT(_what_sing, _what_plur, _var, args...) \
48 if (counter->count._var != 0 || !counter->hide_zero) { \
49 printf("%15" PRIu64 " %s\n", \
50 counter->count._var, \
51 counter->count._var == 1 ? _what_sing : _what_plur); \
56 uint64_t get_total_count(struct counter
*counter
)
58 return counter
->count
.event
+
59 counter
->count
.stream_begin
+
60 counter
->count
.stream_end
+
61 counter
->count
.packet_begin
+
62 counter
->count
.packet_end
+
63 counter
->count
.inactivity
+
64 counter
->count
.discarded_events
+
65 counter
->count
.discarded_packets
+
70 void print_count(struct counter
*counter
, uint64_t total
)
72 PRINTF_COUNT("event", "events", event
);
73 PRINTF_COUNT("stream beginning", "stream beginnings", stream_begin
);
74 PRINTF_COUNT("stream end", "stream ends", stream_end
);
75 PRINTF_COUNT("packet beginning", "packet beginnings", packet_begin
);
76 PRINTF_COUNT("packet end", "packet ends", packet_end
);
77 PRINTF_COUNT("inactivity", "inactivities", inactivity
);
78 PRINTF_COUNT("discarded events notification",
79 "discarded events notifications", discarded_events_notifs
);
80 PRINTF_COUNT(" known discarded event", " known discarded events",
82 PRINTF_COUNT("discarded packets notification",
83 "discarded packets notifications", discarded_packets_notifs
);
84 PRINTF_COUNT(" known discarded packet", " known discarded packets",
87 if (counter
->count
.other
> 0) {
88 PRINTF_COUNT(" other (unknown) notification",
89 " other (unknown) notifications", other
);
92 printf("%s%15" PRIu64
" notification%s (TOTAL)%s\n",
93 bt_common_color_bold(), total
, total
== 1 ? "" : "s",
94 bt_common_color_reset());
95 counter
->last_printed_total
= total
;
99 void try_print_count(struct counter
*counter
)
103 if (counter
->step
== 0) {
108 total
= get_total_count(counter
);
110 if (total
% counter
->step
== 0) {
111 print_count(counter
, total
);
117 void try_print_last(struct counter
*counter
)
119 const uint64_t total
= get_total_count(counter
);
121 if (total
!= counter
->last_printed_total
) {
122 print_count(counter
, total
);
126 void destroy_private_counter_data(struct counter
*counter
)
128 bt_put(counter
->notif_iter
);
132 void counter_finalize(struct bt_private_component
*component
)
134 struct counter
*counter
;
137 counter
= bt_private_component_get_user_data(component
);
139 try_print_last(counter
);
140 bt_put(counter
->notif_iter
);
144 enum bt_component_status
counter_init(struct bt_private_component
*component
,
145 struct bt_value
*params
, UNUSED_VAR
void *init_method_data
)
147 enum bt_component_status ret
;
148 struct counter
*counter
= g_new0(struct counter
, 1);
149 struct bt_value
*step
= NULL
;
150 struct bt_value
*hide_zero
= NULL
;
153 ret
= BT_COMPONENT_STATUS_NOMEM
;
157 ret
= bt_private_component_sink_add_input_private_port(component
,
159 if (ret
!= BT_COMPONENT_STATUS_OK
) {
163 counter
->last_printed_total
= -1ULL;
164 counter
->step
= 1000;
165 step
= bt_value_map_get(params
, "step");
166 if (bt_value_is_integer(step
)) {
168 int vret
= bt_value_integer_get(step
, &val
);
173 counter
->step
= (uint64_t) val
;
177 hide_zero
= bt_value_map_get(params
, "hide-zero");
178 if (bt_value_is_bool(hide_zero
)) {
180 int vret
= bt_value_bool_get(hide_zero
, &val
);
183 counter
->hide_zero
= (bool) val
;
186 ret
= bt_private_component_set_user_data(component
, counter
);
187 if (ret
!= BT_COMPONENT_STATUS_OK
) {
194 destroy_private_counter_data(counter
);
202 void counter_port_connected(
203 struct bt_private_component
*component
,
204 struct bt_private_port
*self_port
,
205 struct bt_port
*other_port
)
207 struct counter
*counter
;
208 struct bt_notification_iterator
*iterator
;
209 struct bt_private_connection
*connection
;
210 enum bt_connection_status conn_status
;
212 counter
= bt_private_component_get_user_data(component
);
214 connection
= bt_private_port_get_private_connection(self_port
);
216 conn_status
= bt_private_connection_create_notification_iterator(
217 connection
, NULL
, &iterator
);
218 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
219 counter
->error
= true;
223 BT_MOVE(counter
->notif_iter
, iterator
);
229 enum bt_component_status
counter_consume(struct bt_private_component
*component
)
231 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
232 struct bt_notification
*notif
= NULL
;
233 struct counter
*counter
;
234 enum bt_notification_iterator_status it_ret
;
237 counter
= bt_private_component_get_user_data(component
);
240 if (unlikely(counter
->error
)) {
241 ret
= BT_COMPONENT_STATUS_ERROR
;
245 if (unlikely(!counter
->notif_iter
)) {
246 try_print_last(counter
);
247 ret
= BT_COMPONENT_STATUS_END
;
251 /* Consume one notification */
252 it_ret
= bt_notification_iterator_next(counter
->notif_iter
);
254 ret
= BT_COMPONENT_STATUS_ERROR
;
259 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
260 ret
= BT_COMPONENT_STATUS_AGAIN
;
262 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
263 try_print_last(counter
);
264 ret
= BT_COMPONENT_STATUS_END
;
266 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
268 struct bt_notification
*notif
=
269 bt_notification_iterator_get_notification(counter
->notif_iter
);
272 switch (bt_notification_get_type(notif
)) {
273 case BT_NOTIFICATION_TYPE_EVENT
:
274 counter
->count
.event
++;
276 case BT_NOTIFICATION_TYPE_INACTIVITY
:
277 counter
->count
.inactivity
++;
279 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
280 counter
->count
.stream_begin
++;
282 case BT_NOTIFICATION_TYPE_STREAM_END
:
283 counter
->count
.stream_end
++;
285 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
286 counter
->count
.packet_begin
++;
288 case BT_NOTIFICATION_TYPE_PACKET_END
:
289 counter
->count
.packet_end
++;
291 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
292 counter
->count
.discarded_events_notifs
++;
293 count
= bt_notification_discarded_events_get_count(
296 counter
->count
.discarded_events
+= count
;
299 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
300 counter
->count
.discarded_packets_notifs
++;
301 count
= bt_notification_discarded_packets_get_count(
304 counter
->count
.discarded_packets
+= count
;
308 counter
->count
.other
++;
317 try_print_count(counter
);