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 | ||
e0831b38 | 23 | #include <babeltrace/babeltrace.h> |
14b7ef9e PP |
24 | #include <babeltrace/babeltrace-internal.h> |
25 | #include <babeltrace/common-internal.h> | |
14b7ef9e | 26 | #include <plugins-common.h> |
8b45963b | 27 | #include <babeltrace/assert-internal.h> |
14b7ef9e PP |
28 | #include <inttypes.h> |
29 | #include <stdint.h> | |
30 | ||
31 | #include "counter.h" | |
32 | ||
33 | #define PRINTF_COUNT(_what_sing, _what_plur, _var, args...) \ | |
34 | do { \ | |
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); \ | |
39 | } \ | |
40 | } while (0) | |
41 | ||
42 | static | |
43 | uint64_t get_total_count(struct counter *counter) | |
44 | { | |
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 + | |
14b7ef9e PP |
51 | counter->count.other; |
52 | } | |
53 | ||
54 | static | |
3fd7b79d | 55 | void print_count(struct counter *counter) |
14b7ef9e | 56 | { |
3fd7b79d PP |
57 | uint64_t total = get_total_count(counter); |
58 | ||
14b7ef9e PP |
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); | |
14b7ef9e PP |
65 | |
66 | if (counter->count.other > 0) { | |
b09a5592 PP |
67 | PRINTF_COUNT(" other (unknown) message", |
68 | " other (unknown) messages", other); | |
14b7ef9e PP |
69 | } |
70 | ||
b09a5592 | 71 | printf("%s%15" PRIu64 " message%s (TOTAL)%s\n", |
14b7ef9e PP |
72 | bt_common_color_bold(), total, total == 1 ? "" : "s", |
73 | bt_common_color_reset()); | |
74 | counter->last_printed_total = total; | |
75 | } | |
76 | ||
77 | static | |
b09a5592 | 78 | void try_print_count(struct counter *counter, uint64_t msg_count) |
14b7ef9e | 79 | { |
14b7ef9e PP |
80 | if (counter->step == 0) { |
81 | /* No update */ | |
82 | return; | |
83 | } | |
84 | ||
b09a5592 | 85 | counter->at += msg_count; |
14b7ef9e | 86 | |
3fd7b79d PP |
87 | if (counter->at >= counter->step) { |
88 | counter->at = 0; | |
89 | print_count(counter); | |
14b7ef9e PP |
90 | putchar('\n'); |
91 | } | |
92 | } | |
93 | ||
94 | static | |
95 | void try_print_last(struct counter *counter) | |
96 | { | |
97 | const uint64_t total = get_total_count(counter); | |
98 | ||
99 | if (total != counter->last_printed_total) { | |
3fd7b79d | 100 | print_count(counter); |
14b7ef9e PP |
101 | } |
102 | } | |
103 | ||
104 | void destroy_private_counter_data(struct counter *counter) | |
105 | { | |
b09a5592 | 106 | bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter); |
14b7ef9e PP |
107 | g_free(counter); |
108 | } | |
109 | ||
834e9996 | 110 | BT_HIDDEN |
8eee8ea2 | 111 | void counter_finalize(bt_self_component_sink *comp) |
14b7ef9e PP |
112 | { |
113 | struct counter *counter; | |
114 | ||
834e9996 PP |
115 | BT_ASSERT(comp); |
116 | counter = bt_self_component_get_data( | |
bb61965b | 117 | bt_self_component_sink_as_self_component(comp)); |
8b45963b | 118 | BT_ASSERT(counter); |
14b7ef9e | 119 | try_print_last(counter); |
b09a5592 | 120 | bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter); |
14b7ef9e PP |
121 | g_free(counter); |
122 | } | |
123 | ||
834e9996 | 124 | BT_HIDDEN |
ee78f405 | 125 | bt_self_component_status counter_init( |
8eee8ea2 PP |
126 | bt_self_component_sink *component, |
127 | const bt_value *params, | |
ce141536 | 128 | UNUSED_VAR void *init_method_data) |
14b7ef9e | 129 | { |
ee78f405 | 130 | bt_self_component_status ret; |
14b7ef9e | 131 | struct counter *counter = g_new0(struct counter, 1); |
8eee8ea2 PP |
132 | const bt_value *step = NULL; |
133 | const bt_value *hide_zero = NULL; | |
14b7ef9e PP |
134 | |
135 | if (!counter) { | |
834e9996 PP |
136 | ret = BT_SELF_COMPONENT_STATUS_NOMEM; |
137 | goto error; | |
14b7ef9e PP |
138 | } |
139 | ||
834e9996 | 140 | ret = bt_self_component_sink_add_input_port(component, |
14b7ef9e | 141 | "in", NULL, NULL); |
834e9996 PP |
142 | if (ret != BT_SELF_COMPONENT_STATUS_OK) { |
143 | goto error; | |
14b7ef9e PP |
144 | } |
145 | ||
146 | counter->last_printed_total = -1ULL; | |
147 | counter->step = 1000; | |
ce141536 | 148 | step = bt_value_map_borrow_entry_value_const(params, "step"); |
8b45963b | 149 | if (step && bt_value_is_integer(step)) { |
14b7ef9e | 150 | int64_t val; |
14b7ef9e | 151 | |
b5cdc106 | 152 | val = bt_value_integer_get(step); |
14b7ef9e PP |
153 | if (val >= 0) { |
154 | counter->step = (uint64_t) val; | |
155 | } | |
156 | } | |
157 | ||
ce141536 | 158 | hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero"); |
8b45963b | 159 | if (hide_zero && bt_value_is_bool(hide_zero)) { |
14b7ef9e | 160 | bt_bool val; |
14b7ef9e | 161 | |
b5cdc106 | 162 | val = bt_value_bool_get(hide_zero); |
14b7ef9e PP |
163 | counter->hide_zero = (bool) val; |
164 | } | |
165 | ||
834e9996 | 166 | bt_self_component_set_data( |
bb61965b | 167 | bt_self_component_sink_as_self_component(component), |
834e9996 | 168 | counter); |
14b7ef9e PP |
169 | goto end; |
170 | ||
171 | error: | |
172 | destroy_private_counter_data(counter); | |
173 | ||
174 | end: | |
14b7ef9e PP |
175 | return ret; |
176 | } | |
177 | ||
834e9996 | 178 | BT_HIDDEN |
ee78f405 | 179 | bt_self_component_status counter_port_connected( |
8eee8ea2 PP |
180 | bt_self_component_sink *comp, |
181 | bt_self_component_port_input *self_port, | |
182 | const bt_port_output *other_port) | |
14b7ef9e | 183 | { |
ee78f405 | 184 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
14b7ef9e | 185 | struct counter *counter; |
b09a5592 | 186 | bt_self_component_port_input_message_iterator *iterator; |
14b7ef9e | 187 | |
834e9996 | 188 | counter = bt_self_component_get_data( |
bb61965b | 189 | bt_self_component_sink_as_self_component(comp)); |
8b45963b | 190 | BT_ASSERT(counter); |
b09a5592 | 191 | iterator = bt_self_component_port_input_message_iterator_create( |
834e9996 PP |
192 | self_port); |
193 | if (!iterator) { | |
194 | status = BT_SELF_COMPONENT_STATUS_NOMEM; | |
14b7ef9e PP |
195 | goto end; |
196 | } | |
197 | ||
b09a5592 PP |
198 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF( |
199 | counter->msg_iter, iterator); | |
14b7ef9e PP |
200 | |
201 | end: | |
634f394c | 202 | return status; |
14b7ef9e PP |
203 | } |
204 | ||
834e9996 | 205 | BT_HIDDEN |
ee78f405 | 206 | bt_self_component_status counter_consume( |
8eee8ea2 | 207 | bt_self_component_sink *comp) |
14b7ef9e | 208 | { |
ee78f405 | 209 | bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
14b7ef9e | 210 | struct counter *counter; |
ee78f405 | 211 | bt_message_iterator_status it_ret; |
b09a5592 PP |
212 | uint64_t msg_count; |
213 | bt_message_array_const msgs; | |
14b7ef9e | 214 | |
834e9996 | 215 | counter = bt_self_component_get_data( |
bb61965b | 216 | bt_self_component_sink_as_self_component(comp)); |
8b45963b | 217 | BT_ASSERT(counter); |
14b7ef9e | 218 | |
b09a5592 | 219 | if (unlikely(!counter->msg_iter)) { |
14b7ef9e | 220 | try_print_last(counter); |
834e9996 | 221 | ret = BT_SELF_COMPONENT_STATUS_END; |
14b7ef9e PP |
222 | goto end; |
223 | } | |
224 | ||
b09a5592 PP |
225 | /* Consume messages */ |
226 | it_ret = bt_self_component_port_input_message_iterator_next( | |
227 | counter->msg_iter, &msgs, &msg_count); | |
14b7ef9e | 228 | if (it_ret < 0) { |
834e9996 | 229 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
14b7ef9e PP |
230 | goto end; |
231 | } | |
232 | ||
233 | switch (it_ret) { | |
b09a5592 | 234 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
14b7ef9e | 235 | { |
3fd7b79d PP |
236 | uint64_t i; |
237 | ||
b09a5592 PP |
238 | for (i = 0; i < msg_count; i++) { |
239 | const bt_message *msg = msgs[i]; | |
3fd7b79d | 240 | |
b09a5592 PP |
241 | BT_ASSERT(msg); |
242 | switch (bt_message_get_type(msg)) { | |
243 | case BT_MESSAGE_TYPE_EVENT: | |
3fd7b79d PP |
244 | counter->count.event++; |
245 | break; | |
b09a5592 | 246 | case BT_MESSAGE_TYPE_INACTIVITY: |
3fd7b79d PP |
247 | counter->count.inactivity++; |
248 | break; | |
b09a5592 | 249 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
3fd7b79d PP |
250 | counter->count.stream_begin++; |
251 | break; | |
b09a5592 | 252 | case BT_MESSAGE_TYPE_STREAM_END: |
3fd7b79d PP |
253 | counter->count.stream_end++; |
254 | break; | |
b09a5592 | 255 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
3fd7b79d PP |
256 | counter->count.packet_begin++; |
257 | break; | |
b09a5592 | 258 | case BT_MESSAGE_TYPE_PACKET_END: |
3fd7b79d PP |
259 | counter->count.packet_end++; |
260 | break; | |
3fd7b79d PP |
261 | default: |
262 | counter->count.other++; | |
14b7ef9e | 263 | } |
3fd7b79d | 264 | |
b09a5592 | 265 | bt_message_put_ref(msg); |
14b7ef9e | 266 | } |
834e9996 PP |
267 | |
268 | ret = BT_SELF_COMPONENT_STATUS_OK; | |
269 | break; | |
14b7ef9e | 270 | } |
b09a5592 | 271 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
834e9996 PP |
272 | ret = BT_SELF_COMPONENT_STATUS_AGAIN; |
273 | goto end; | |
b09a5592 | 274 | case BT_MESSAGE_ITERATOR_STATUS_END: |
834e9996 PP |
275 | try_print_last(counter); |
276 | ret = BT_SELF_COMPONENT_STATUS_END; | |
277 | goto end; | |
b09a5592 | 278 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: |
834e9996 PP |
279 | ret = BT_SELF_COMPONENT_STATUS_NOMEM; |
280 | goto end; | |
14b7ef9e PP |
281 | default: |
282 | break; | |
283 | } | |
284 | ||
b09a5592 | 285 | try_print_count(counter, msg_count); |
14b7ef9e PP |
286 | |
287 | end: | |
14b7ef9e PP |
288 | return ret; |
289 | } |