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 | { | |
65300d60 | 106 | bt_object_put_ref(counter->notif_iter); |
358340ec PP |
107 | g_free(counter); |
108 | } | |
109 | ||
110 | void counter_finalize(struct bt_private_component *component) | |
111 | { | |
112 | struct counter *counter; | |
113 | ||
f6ccaed9 | 114 | BT_ASSERT(component); |
358340ec | 115 | counter = bt_private_component_get_user_data(component); |
f6ccaed9 | 116 | BT_ASSERT(counter); |
358340ec | 117 | try_print_last(counter); |
65300d60 | 118 | bt_object_put_ref(counter->notif_iter); |
358340ec PP |
119 | g_free(counter); |
120 | } | |
121 | ||
122 | enum bt_component_status counter_init(struct bt_private_component *component, | |
123 | struct bt_value *params, UNUSED_VAR void *init_method_data) | |
124 | { | |
125 | enum bt_component_status ret; | |
126 | struct counter *counter = g_new0(struct counter, 1); | |
127 | struct bt_value *step = NULL; | |
128 | struct bt_value *hide_zero = NULL; | |
129 | ||
130 | if (!counter) { | |
131 | ret = BT_COMPONENT_STATUS_NOMEM; | |
132 | goto end; | |
133 | } | |
134 | ||
135 | ret = bt_private_component_sink_add_input_private_port(component, | |
136 | "in", NULL, NULL); | |
137 | if (ret != BT_COMPONENT_STATUS_OK) { | |
138 | goto end; | |
139 | } | |
140 | ||
141 | counter->last_printed_total = -1ULL; | |
142 | counter->step = 1000; | |
07208d85 | 143 | step = bt_value_map_borrow_entry_value(params, "step"); |
f6ccaed9 | 144 | if (step && bt_value_is_integer(step)) { |
358340ec | 145 | int64_t val; |
358340ec | 146 | |
f6ccaed9 | 147 | (void) bt_value_integer_get(step, &val); |
358340ec PP |
148 | |
149 | if (val >= 0) { | |
150 | counter->step = (uint64_t) val; | |
151 | } | |
152 | } | |
153 | ||
07208d85 | 154 | hide_zero = bt_value_map_borrow_entry_value(params, "hide-zero"); |
f6ccaed9 | 155 | if (hide_zero && bt_value_is_bool(hide_zero)) { |
358340ec | 156 | bt_bool val; |
358340ec | 157 | |
f6ccaed9 | 158 | (void) bt_value_bool_get(hide_zero, &val); |
358340ec PP |
159 | counter->hide_zero = (bool) val; |
160 | } | |
161 | ||
162 | ret = bt_private_component_set_user_data(component, counter); | |
163 | if (ret != BT_COMPONENT_STATUS_OK) { | |
164 | goto error; | |
165 | } | |
166 | ||
167 | goto end; | |
168 | ||
169 | error: | |
170 | destroy_private_counter_data(counter); | |
171 | ||
172 | end: | |
358340ec PP |
173 | return ret; |
174 | } | |
175 | ||
bf55043c | 176 | enum bt_component_status counter_port_connected( |
358340ec PP |
177 | struct bt_private_component *component, |
178 | struct bt_private_port *self_port, | |
179 | struct bt_port *other_port) | |
180 | { | |
bf55043c | 181 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
358340ec PP |
182 | struct counter *counter; |
183 | struct bt_notification_iterator *iterator; | |
184 | struct bt_private_connection *connection; | |
185 | enum bt_connection_status conn_status; | |
186 | ||
187 | counter = bt_private_component_get_user_data(component); | |
f6ccaed9 | 188 | BT_ASSERT(counter); |
358340ec | 189 | connection = bt_private_port_get_private_connection(self_port); |
f6ccaed9 | 190 | BT_ASSERT(connection); |
358340ec | 191 | conn_status = bt_private_connection_create_notification_iterator( |
f42867e2 | 192 | connection, &iterator); |
358340ec | 193 | if (conn_status != BT_CONNECTION_STATUS_OK) { |
bf55043c | 194 | status = BT_COMPONENT_STATUS_ERROR; |
358340ec PP |
195 | goto end; |
196 | } | |
197 | ||
65300d60 | 198 | BT_OBJECT_MOVE_REF(counter->notif_iter, iterator); |
358340ec PP |
199 | |
200 | end: | |
65300d60 | 201 | bt_object_put_ref(connection); |
bf55043c | 202 | return status; |
358340ec PP |
203 | } |
204 | ||
205 | enum bt_component_status counter_consume(struct bt_private_component *component) | |
206 | { | |
207 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
358340ec PP |
208 | struct counter *counter; |
209 | enum bt_notification_iterator_status it_ret; | |
d4393e08 PP |
210 | uint64_t notif_count; |
211 | bt_notification_array notifs; | |
358340ec PP |
212 | |
213 | counter = bt_private_component_get_user_data(component); | |
f6ccaed9 | 214 | BT_ASSERT(counter); |
358340ec | 215 | |
358340ec PP |
216 | if (unlikely(!counter->notif_iter)) { |
217 | try_print_last(counter); | |
218 | ret = BT_COMPONENT_STATUS_END; | |
219 | goto end; | |
220 | } | |
221 | ||
d4393e08 | 222 | /* Consume notifications */ |
07245ac2 | 223 | it_ret = bt_private_connection_notification_iterator_next( |
d4393e08 | 224 | counter->notif_iter, ¬ifs, ¬if_count); |
358340ec PP |
225 | if (it_ret < 0) { |
226 | ret = BT_COMPONENT_STATUS_ERROR; | |
227 | goto end; | |
228 | } | |
229 | ||
230 | switch (it_ret) { | |
231 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
232 | ret = BT_COMPONENT_STATUS_AGAIN; | |
233 | goto end; | |
234 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
235 | try_print_last(counter); | |
236 | ret = BT_COMPONENT_STATUS_END; | |
237 | goto end; | |
238 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
239 | { | |
d4393e08 PP |
240 | uint64_t i; |
241 | ||
242 | for (i = 0; i < notif_count; i++) { | |
243 | struct bt_notification *notif = notifs[i]; | |
244 | ||
245 | BT_ASSERT(notif); | |
246 | switch (bt_notification_get_type(notif)) { | |
247 | case BT_NOTIFICATION_TYPE_EVENT: | |
248 | counter->count.event++; | |
249 | break; | |
250 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
251 | counter->count.inactivity++; | |
252 | break; | |
253 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: | |
254 | counter->count.stream_begin++; | |
255 | break; | |
256 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
257 | counter->count.stream_end++; | |
258 | break; | |
259 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
260 | counter->count.packet_begin++; | |
261 | break; | |
262 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
263 | counter->count.packet_end++; | |
264 | break; | |
d4393e08 PP |
265 | default: |
266 | counter->count.other++; | |
358340ec | 267 | } |
d4393e08 | 268 | |
65300d60 | 269 | bt_object_put_ref(notif); |
358340ec | 270 | } |
358340ec PP |
271 | } |
272 | default: | |
273 | break; | |
274 | } | |
275 | ||
d4393e08 | 276 | try_print_count(counter, notif_count); |
358340ec PP |
277 | |
278 | end: | |
358340ec PP |
279 | return ret; |
280 | } |