Commit | Line | Data |
---|---|---|
14b7ef9e PP |
1 | /* |
2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
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: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
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 | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
627c70e6 | 23 | #define BT_COMP_LOG_SELF_COMP (counter->self_comp) |
c58e2582 | 24 | #define BT_LOG_OUTPUT_LEVEL (counter->log_level) |
b03487ab | 25 | #define BT_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER" |
3fa1b6a3 | 26 | #include "logging/comp-logging.h" |
68d9d039 | 27 | |
71c5da58 | 28 | #include <babeltrace2/babeltrace.h> |
85e7137b | 29 | #include "common/macros.h" |
57952005 | 30 | #include "common/common.h" |
57952005 | 31 | #include "common/assert.h" |
14b7ef9e PP |
32 | #include <inttypes.h> |
33 | #include <stdint.h> | |
34 | ||
35 | #include "counter.h" | |
36 | ||
168baad4 | 37 | #define PRINTF_COUNT(_what, _var, args...) \ |
14b7ef9e PP |
38 | do { \ |
39 | if (counter->count._var != 0 || !counter->hide_zero) { \ | |
168baad4 | 40 | printf("%15" PRIu64 " %s message%s\n", \ |
14b7ef9e | 41 | counter->count._var, \ |
168baad4 PP |
42 | (_what), \ |
43 | counter->count._var == 1 ? "" : "s"); \ | |
14b7ef9e PP |
44 | } \ |
45 | } while (0) | |
46 | ||
1043fdea PP |
47 | static |
48 | const char * const in_port_name = "in"; | |
49 | ||
14b7ef9e PP |
50 | static |
51 | uint64_t get_total_count(struct counter *counter) | |
52 | { | |
53 | return counter->count.event + | |
54 | counter->count.stream_begin + | |
55 | counter->count.stream_end + | |
56 | counter->count.packet_begin + | |
57 | counter->count.packet_end + | |
168baad4 PP |
58 | counter->count.disc_events + |
59 | counter->count.disc_packets + | |
40bf6fd0 | 60 | counter->count.msg_iter_inactivity + |
14b7ef9e PP |
61 | counter->count.other; |
62 | } | |
63 | ||
64 | static | |
3fd7b79d | 65 | void print_count(struct counter *counter) |
14b7ef9e | 66 | { |
3fd7b79d PP |
67 | uint64_t total = get_total_count(counter); |
68 | ||
168baad4 PP |
69 | PRINTF_COUNT("Event", event); |
70 | PRINTF_COUNT("Stream beginning", stream_begin); | |
71 | PRINTF_COUNT("Stream end", stream_end); | |
168baad4 PP |
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); | |
14b7ef9e PP |
77 | |
78 | if (counter->count.other > 0) { | |
168baad4 | 79 | PRINTF_COUNT("Other (unknown)", other); |
14b7ef9e PP |
80 | } |
81 | ||
b09a5592 | 82 | printf("%s%15" PRIu64 " message%s (TOTAL)%s\n", |
14b7ef9e PP |
83 | bt_common_color_bold(), total, total == 1 ? "" : "s", |
84 | bt_common_color_reset()); | |
85 | counter->last_printed_total = total; | |
86 | } | |
87 | ||
88 | static | |
b09a5592 | 89 | void try_print_count(struct counter *counter, uint64_t msg_count) |
14b7ef9e | 90 | { |
14b7ef9e PP |
91 | if (counter->step == 0) { |
92 | /* No update */ | |
93 | return; | |
94 | } | |
95 | ||
b09a5592 | 96 | counter->at += msg_count; |
14b7ef9e | 97 | |
3fd7b79d PP |
98 | if (counter->at >= counter->step) { |
99 | counter->at = 0; | |
100 | print_count(counter); | |
14b7ef9e PP |
101 | putchar('\n'); |
102 | } | |
103 | } | |
104 | ||
105 | static | |
106 | void try_print_last(struct counter *counter) | |
107 | { | |
108 | const uint64_t total = get_total_count(counter); | |
109 | ||
110 | if (total != counter->last_printed_total) { | |
3fd7b79d | 111 | print_count(counter); |
14b7ef9e PP |
112 | } |
113 | } | |
114 | ||
115 | void destroy_private_counter_data(struct counter *counter) | |
116 | { | |
888e0925 FD |
117 | if (counter) { |
118 | bt_self_component_port_input_message_iterator_put_ref( | |
119 | counter->msg_iter); | |
120 | g_free(counter); | |
121 | } | |
14b7ef9e PP |
122 | } |
123 | ||
834e9996 | 124 | BT_HIDDEN |
8eee8ea2 | 125 | void counter_finalize(bt_self_component_sink *comp) |
14b7ef9e PP |
126 | { |
127 | struct counter *counter; | |
128 | ||
834e9996 PP |
129 | BT_ASSERT(comp); |
130 | counter = bt_self_component_get_data( | |
bb61965b | 131 | bt_self_component_sink_as_self_component(comp)); |
8b45963b | 132 | BT_ASSERT(counter); |
14b7ef9e | 133 | try_print_last(counter); |
b09a5592 | 134 | bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter); |
14b7ef9e PP |
135 | g_free(counter); |
136 | } | |
137 | ||
834e9996 | 138 | BT_HIDDEN |
fb25b9e3 | 139 | bt_component_class_init_method_status counter_init( |
8eee8ea2 PP |
140 | bt_self_component_sink *component, |
141 | const bt_value *params, | |
f807570c | 142 | __attribute__((unused)) void *init_method_data) |
14b7ef9e | 143 | { |
fb25b9e3 PP |
144 | bt_component_class_init_method_status status = |
145 | BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK; | |
146 | bt_self_component_add_port_status add_port_status; | |
14b7ef9e | 147 | struct counter *counter = g_new0(struct counter, 1); |
8eee8ea2 PP |
148 | const bt_value *step = NULL; |
149 | const bt_value *hide_zero = NULL; | |
14b7ef9e PP |
150 | |
151 | if (!counter) { | |
fb25b9e3 | 152 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR; |
834e9996 | 153 | goto error; |
14b7ef9e PP |
154 | } |
155 | ||
627c70e6 PP |
156 | counter->self_comp = |
157 | bt_self_component_sink_as_self_component(component); | |
c58e2582 | 158 | counter->log_level = bt_component_get_logging_level( |
627c70e6 | 159 | bt_self_component_as_component(counter->self_comp)); |
fb25b9e3 | 160 | add_port_status = bt_self_component_sink_add_input_port(component, |
14b7ef9e | 161 | "in", NULL, NULL); |
fb25b9e3 PP |
162 | switch (add_port_status) { |
163 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR: | |
164 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR; | |
834e9996 | 165 | goto error; |
fb25b9e3 PP |
166 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR: |
167 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR; | |
168 | goto error; | |
169 | default: | |
170 | break; | |
14b7ef9e PP |
171 | } |
172 | ||
173 | counter->last_printed_total = -1ULL; | |
a79f976c | 174 | counter->step = 10000; |
ce141536 | 175 | step = bt_value_map_borrow_entry_value_const(params, "step"); |
68d9d039 PP |
176 | if (step) { |
177 | if (!bt_value_is_unsigned_integer(step)) { | |
627c70e6 | 178 | BT_COMP_LOGE("`step` parameter: expecting an unsigned integer value: " |
68d9d039 PP |
179 | "type=%s", bt_common_value_type_string( |
180 | bt_value_get_type(step))); | |
181 | goto error; | |
14b7ef9e | 182 | } |
68d9d039 | 183 | |
60bbfc7c | 184 | counter->step = bt_value_integer_unsigned_get(step); |
14b7ef9e PP |
185 | } |
186 | ||
ce141536 | 187 | hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero"); |
68d9d039 PP |
188 | if (hide_zero) { |
189 | if (!bt_value_is_bool(hide_zero)) { | |
627c70e6 | 190 | BT_COMP_LOGE("`hide-zero` parameter: expecting a boolean value: " |
68d9d039 PP |
191 | "type=%s", bt_common_value_type_string( |
192 | bt_value_get_type(hide_zero))); | |
193 | goto error; | |
194 | } | |
14b7ef9e | 195 | |
68d9d039 | 196 | counter->hide_zero = (bool) bt_value_bool_get(hide_zero); |
14b7ef9e PP |
197 | } |
198 | ||
834e9996 | 199 | bt_self_component_set_data( |
bb61965b | 200 | bt_self_component_sink_as_self_component(component), |
834e9996 | 201 | counter); |
14b7ef9e PP |
202 | goto end; |
203 | ||
204 | error: | |
205 | destroy_private_counter_data(counter); | |
fb25b9e3 PP |
206 | |
207 | if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) { | |
208 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR; | |
209 | } | |
14b7ef9e PP |
210 | |
211 | end: | |
fb25b9e3 | 212 | return status; |
14b7ef9e PP |
213 | } |
214 | ||
834e9996 | 215 | BT_HIDDEN |
fb25b9e3 PP |
216 | bt_component_class_sink_graph_is_configured_method_status |
217 | counter_graph_is_configured( | |
1043fdea | 218 | bt_self_component_sink *comp) |
14b7ef9e | 219 | { |
ab8b2b1b SM |
220 | bt_component_class_sink_graph_is_configured_method_status status; |
221 | bt_self_component_port_input_message_iterator_create_from_sink_component_status | |
222 | msg_iter_status; | |
14b7ef9e | 223 | struct counter *counter; |
b09a5592 | 224 | bt_self_component_port_input_message_iterator *iterator; |
14b7ef9e | 225 | |
834e9996 | 226 | counter = bt_self_component_get_data( |
bb61965b | 227 | bt_self_component_sink_as_self_component(comp)); |
8b45963b | 228 | BT_ASSERT(counter); |
ab8b2b1b SM |
229 | |
230 | msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component( | |
692f1a01 | 231 | comp, bt_self_component_sink_borrow_input_port_by_name(comp, |
ab8b2b1b SM |
232 | in_port_name), &iterator); |
233 | if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { | |
234 | status = (int) msg_iter_status; | |
14b7ef9e PP |
235 | goto end; |
236 | } | |
237 | ||
b09a5592 PP |
238 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF( |
239 | counter->msg_iter, iterator); | |
14b7ef9e | 240 | |
ab8b2b1b | 241 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; |
14b7ef9e | 242 | end: |
634f394c | 243 | return status; |
14b7ef9e PP |
244 | } |
245 | ||
834e9996 | 246 | BT_HIDDEN |
fb25b9e3 | 247 | bt_component_class_sink_consume_method_status counter_consume( |
8eee8ea2 | 248 | bt_self_component_sink *comp) |
14b7ef9e | 249 | { |
fb25b9e3 PP |
250 | bt_component_class_sink_consume_method_status status = |
251 | BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; | |
14b7ef9e | 252 | struct counter *counter; |
fb25b9e3 | 253 | bt_message_iterator_next_status next_status; |
b09a5592 PP |
254 | uint64_t msg_count; |
255 | bt_message_array_const msgs; | |
14b7ef9e | 256 | |
834e9996 | 257 | counter = bt_self_component_get_data( |
bb61965b | 258 | bt_self_component_sink_as_self_component(comp)); |
8b45963b | 259 | BT_ASSERT(counter); |
14b7ef9e | 260 | |
85e7137b | 261 | if (G_UNLIKELY(!counter->msg_iter)) { |
14b7ef9e | 262 | try_print_last(counter); |
fb25b9e3 | 263 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
14b7ef9e PP |
264 | goto end; |
265 | } | |
266 | ||
b09a5592 | 267 | /* Consume messages */ |
fb25b9e3 | 268 | next_status = bt_self_component_port_input_message_iterator_next( |
b09a5592 | 269 | counter->msg_iter, &msgs, &msg_count); |
fb25b9e3 PP |
270 | if (next_status < 0) { |
271 | status = (int) next_status; | |
14b7ef9e PP |
272 | goto end; |
273 | } | |
274 | ||
fb25b9e3 PP |
275 | switch (next_status) { |
276 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK: | |
14b7ef9e | 277 | { |
3fd7b79d PP |
278 | uint64_t i; |
279 | ||
b09a5592 PP |
280 | for (i = 0; i < msg_count; i++) { |
281 | const bt_message *msg = msgs[i]; | |
3fd7b79d | 282 | |
b09a5592 PP |
283 | BT_ASSERT(msg); |
284 | switch (bt_message_get_type(msg)) { | |
285 | case BT_MESSAGE_TYPE_EVENT: | |
3fd7b79d PP |
286 | counter->count.event++; |
287 | break; | |
168baad4 PP |
288 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
289 | counter->count.packet_begin++; | |
290 | break; | |
291 | case BT_MESSAGE_TYPE_PACKET_END: | |
292 | counter->count.packet_end++; | |
293 | break; | |
40bf6fd0 FD |
294 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
295 | counter->count.msg_iter_inactivity++; | |
3fd7b79d | 296 | break; |
b09a5592 | 297 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
3fd7b79d PP |
298 | counter->count.stream_begin++; |
299 | break; | |
b09a5592 | 300 | case BT_MESSAGE_TYPE_STREAM_END: |
3fd7b79d PP |
301 | counter->count.stream_end++; |
302 | break; | |
168baad4 PP |
303 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
304 | counter->count.disc_events++; | |
305 | break; | |
306 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
307 | counter->count.disc_packets++; | |
3fd7b79d | 308 | break; |
3fd7b79d PP |
309 | default: |
310 | counter->count.other++; | |
14b7ef9e | 311 | } |
3fd7b79d | 312 | |
b09a5592 | 313 | bt_message_put_ref(msg); |
14b7ef9e | 314 | } |
834e9996 | 315 | |
fb25b9e3 | 316 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
834e9996 | 317 | break; |
14b7ef9e | 318 | } |
fb25b9e3 PP |
319 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN: |
320 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN; | |
834e9996 | 321 | goto end; |
fb25b9e3 | 322 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: |
834e9996 | 323 | try_print_last(counter); |
fb25b9e3 | 324 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
834e9996 | 325 | goto end; |
fb25b9e3 PP |
326 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR: |
327 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR; | |
834e9996 | 328 | goto end; |
14b7ef9e PP |
329 | default: |
330 | break; | |
331 | } | |
332 | ||
b09a5592 | 333 | try_print_count(counter, msg_count); |
14b7ef9e PP |
334 | |
335 | end: | |
fb25b9e3 | 336 | return status; |
14b7ef9e | 337 | } |