Commit | Line | Data |
---|---|---|
358340ec 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 | ||
9d408fca | 23 | #include <babeltrace/babeltrace.h> |
358340ec PP |
24 | #include <babeltrace/babeltrace-internal.h> |
25 | #include <babeltrace/common-internal.h> | |
358340ec | 26 | #include <plugins-common.h> |
f6ccaed9 | 27 | #include <babeltrace/assert-internal.h> |
358340ec 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 + | |
358340ec PP |
51 | counter->count.other; |
52 | } | |
53 | ||
54 | static | |
d4393e08 | 55 | void print_count(struct counter *counter) |
358340ec | 56 | { |
d4393e08 PP |
57 | uint64_t total = get_total_count(counter); |
58 | ||
358340ec 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); | |
358340ec PP |
65 | |
66 | if (counter->count.other > 0) { | |
67 | PRINTF_COUNT(" other (unknown) notification", | |
68 | " other (unknown) notifications", other); | |
69 | } | |
70 | ||
71 | printf("%s%15" PRIu64 " notification%s (TOTAL)%s\n", | |
72 | bt_common_color_bold(), total, total == 1 ? "" : "s", | |
73 | bt_common_color_reset()); | |
74 | counter->last_printed_total = total; | |
75 | } | |
76 | ||
77 | static | |
d4393e08 | 78 | void try_print_count(struct counter *counter, uint64_t notif_count) |
358340ec | 79 | { |
358340ec PP |
80 | if (counter->step == 0) { |
81 | /* No update */ | |
82 | return; | |
83 | } | |
84 | ||
d4393e08 | 85 | counter->at += notif_count; |
358340ec | 86 | |
d4393e08 PP |
87 | if (counter->at >= counter->step) { |
88 | counter->at = 0; | |
89 | print_count(counter); | |
358340ec 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) { | |
d4393e08 | 100 | print_count(counter); |
358340ec PP |
101 | } |
102 | } | |
103 | ||
104 | void destroy_private_counter_data(struct counter *counter) | |
105 | { | |
c5b9b441 | 106 | bt_self_component_port_input_notification_iterator_put_ref(counter->notif_iter); |
358340ec PP |
107 | g_free(counter); |
108 | } | |
109 | ||
d94d92ac PP |
110 | BT_HIDDEN |
111 | void counter_finalize(struct bt_self_component_sink *comp) | |
358340ec PP |
112 | { |
113 | struct counter *counter; | |
114 | ||
d94d92ac PP |
115 | BT_ASSERT(comp); |
116 | counter = bt_self_component_get_data( | |
707b7d35 | 117 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 118 | BT_ASSERT(counter); |
358340ec | 119 | try_print_last(counter); |
c5b9b441 | 120 | bt_self_component_port_input_notification_iterator_put_ref(counter->notif_iter); |
358340ec PP |
121 | g_free(counter); |
122 | } | |
123 | ||
d94d92ac PP |
124 | BT_HIDDEN |
125 | enum bt_self_component_status counter_init( | |
126 | struct bt_self_component_sink *component, | |
05e21286 PP |
127 | const struct bt_value *params, |
128 | UNUSED_VAR void *init_method_data) | |
358340ec | 129 | { |
d94d92ac | 130 | enum bt_self_component_status ret; |
358340ec | 131 | struct counter *counter = g_new0(struct counter, 1); |
05e21286 PP |
132 | const struct bt_value *step = NULL; |
133 | const struct bt_value *hide_zero = NULL; | |
358340ec PP |
134 | |
135 | if (!counter) { | |
d94d92ac PP |
136 | ret = BT_SELF_COMPONENT_STATUS_NOMEM; |
137 | goto error; | |
358340ec PP |
138 | } |
139 | ||
d94d92ac | 140 | ret = bt_self_component_sink_add_input_port(component, |
358340ec | 141 | "in", NULL, NULL); |
d94d92ac PP |
142 | if (ret != BT_SELF_COMPONENT_STATUS_OK) { |
143 | goto error; | |
358340ec PP |
144 | } |
145 | ||
146 | counter->last_printed_total = -1ULL; | |
147 | counter->step = 1000; | |
05e21286 | 148 | step = bt_value_map_borrow_entry_value_const(params, "step"); |
f6ccaed9 | 149 | if (step && bt_value_is_integer(step)) { |
358340ec | 150 | int64_t val; |
358340ec | 151 | |
601b0d3c | 152 | val = bt_value_integer_get(step); |
358340ec PP |
153 | if (val >= 0) { |
154 | counter->step = (uint64_t) val; | |
155 | } | |
156 | } | |
157 | ||
05e21286 | 158 | hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero"); |
f6ccaed9 | 159 | if (hide_zero && bt_value_is_bool(hide_zero)) { |
358340ec | 160 | bt_bool val; |
358340ec | 161 | |
601b0d3c | 162 | val = bt_value_bool_get(hide_zero); |
358340ec PP |
163 | counter->hide_zero = (bool) val; |
164 | } | |
165 | ||
d94d92ac | 166 | bt_self_component_set_data( |
707b7d35 | 167 | bt_self_component_sink_as_self_component(component), |
d94d92ac | 168 | counter); |
358340ec PP |
169 | goto end; |
170 | ||
171 | error: | |
172 | destroy_private_counter_data(counter); | |
173 | ||
174 | end: | |
358340ec PP |
175 | return ret; |
176 | } | |
177 | ||
d94d92ac PP |
178 | BT_HIDDEN |
179 | enum bt_self_component_status counter_port_connected( | |
180 | struct bt_self_component_sink *comp, | |
181 | struct bt_self_component_port_input *self_port, | |
0d72b8c3 | 182 | const struct bt_port_output *other_port) |
358340ec | 183 | { |
d94d92ac | 184 | enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
358340ec | 185 | struct counter *counter; |
d94d92ac | 186 | struct bt_self_component_port_input_notification_iterator *iterator; |
358340ec | 187 | |
d94d92ac | 188 | counter = bt_self_component_get_data( |
707b7d35 | 189 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 190 | BT_ASSERT(counter); |
d94d92ac PP |
191 | iterator = bt_self_component_port_input_notification_iterator_create( |
192 | self_port); | |
193 | if (!iterator) { | |
194 | status = BT_SELF_COMPONENT_STATUS_NOMEM; | |
358340ec PP |
195 | goto end; |
196 | } | |
197 | ||
c5b9b441 PP |
198 | BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_MOVE_REF( |
199 | counter->notif_iter, iterator); | |
358340ec PP |
200 | |
201 | end: | |
bf55043c | 202 | return status; |
358340ec PP |
203 | } |
204 | ||
d94d92ac PP |
205 | BT_HIDDEN |
206 | enum bt_self_component_status counter_consume( | |
207 | struct bt_self_component_sink *comp) | |
358340ec | 208 | { |
d94d92ac | 209 | enum bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
358340ec PP |
210 | struct counter *counter; |
211 | enum bt_notification_iterator_status it_ret; | |
d4393e08 | 212 | uint64_t notif_count; |
0d72b8c3 | 213 | bt_notification_array_const notifs; |
358340ec | 214 | |
d94d92ac | 215 | counter = bt_self_component_get_data( |
707b7d35 | 216 | bt_self_component_sink_as_self_component(comp)); |
f6ccaed9 | 217 | BT_ASSERT(counter); |
358340ec | 218 | |
358340ec PP |
219 | if (unlikely(!counter->notif_iter)) { |
220 | try_print_last(counter); | |
d94d92ac | 221 | ret = BT_SELF_COMPONENT_STATUS_END; |
358340ec PP |
222 | goto end; |
223 | } | |
224 | ||
d4393e08 | 225 | /* Consume notifications */ |
d94d92ac | 226 | it_ret = bt_self_component_port_input_notification_iterator_next( |
d4393e08 | 227 | counter->notif_iter, ¬ifs, ¬if_count); |
358340ec | 228 | if (it_ret < 0) { |
d94d92ac | 229 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
358340ec PP |
230 | goto end; |
231 | } | |
232 | ||
233 | switch (it_ret) { | |
358340ec PP |
234 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: |
235 | { | |
d4393e08 PP |
236 | uint64_t i; |
237 | ||
238 | for (i = 0; i < notif_count; i++) { | |
0d72b8c3 | 239 | const struct bt_notification *notif = notifs[i]; |
d4393e08 PP |
240 | |
241 | BT_ASSERT(notif); | |
242 | switch (bt_notification_get_type(notif)) { | |
243 | case BT_NOTIFICATION_TYPE_EVENT: | |
244 | counter->count.event++; | |
245 | break; | |
246 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
247 | counter->count.inactivity++; | |
248 | break; | |
bb5bb160 | 249 | case BT_NOTIFICATION_TYPE_STREAM_BEGINNING: |
d4393e08 PP |
250 | counter->count.stream_begin++; |
251 | break; | |
252 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
253 | counter->count.stream_end++; | |
254 | break; | |
bb5bb160 | 255 | case BT_NOTIFICATION_TYPE_PACKET_BEGINNING: |
d4393e08 PP |
256 | counter->count.packet_begin++; |
257 | break; | |
258 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
259 | counter->count.packet_end++; | |
260 | break; | |
d4393e08 PP |
261 | default: |
262 | counter->count.other++; | |
358340ec | 263 | } |
d4393e08 | 264 | |
c5b9b441 | 265 | bt_notification_put_ref(notif); |
358340ec | 266 | } |
d94d92ac PP |
267 | |
268 | ret = BT_SELF_COMPONENT_STATUS_OK; | |
269 | break; | |
358340ec | 270 | } |
d94d92ac PP |
271 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: |
272 | ret = BT_SELF_COMPONENT_STATUS_AGAIN; | |
273 | goto end; | |
274 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
275 | try_print_last(counter); | |
276 | ret = BT_SELF_COMPONENT_STATUS_END; | |
277 | goto end; | |
278 | case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM: | |
279 | ret = BT_SELF_COMPONENT_STATUS_NOMEM; | |
280 | goto end; | |
358340ec PP |
281 | default: |
282 | break; | |
283 | } | |
284 | ||
d4393e08 | 285 | try_print_count(counter, notif_count); |
358340ec PP |
286 | |
287 | end: | |
358340ec PP |
288 | return ret; |
289 | } |