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