tap-driver.sh: flush stdout after each test result
[babeltrace.git] / plugins / utils / counter / counter.c
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
23 #define BT_LOG_TAG "PLUGIN-UTILS-COUNTER-FLT"
24 #include "logging.h"
25
26 #include <babeltrace2/babeltrace.h>
27 #include <babeltrace2/babeltrace-internal.h>
28 #include <babeltrace2/common-internal.h>
29 #include <plugins-common.h>
30 #include <babeltrace2/assert-internal.h>
31 #include <inttypes.h>
32 #include <stdint.h>
33
34 #include "counter.h"
35
36 #define PRINTF_COUNT(_what, _var, args...) \
37 do { \
38 if (counter->count._var != 0 || !counter->hide_zero) { \
39 printf("%15" PRIu64 " %s message%s\n", \
40 counter->count._var, \
41 (_what), \
42 counter->count._var == 1 ? "" : "s"); \
43 } \
44 } while (0)
45
46 static
47 const char * const in_port_name = "in";
48
49 static
50 uint64_t get_total_count(struct counter *counter)
51 {
52 return counter->count.event +
53 counter->count.stream_begin +
54 counter->count.stream_end +
55 counter->count.stream_activity_begin +
56 counter->count.stream_activity_end +
57 counter->count.packet_begin +
58 counter->count.packet_end +
59 counter->count.disc_events +
60 counter->count.disc_packets +
61 counter->count.msg_iter_inactivity +
62 counter->count.other;
63 }
64
65 static
66 void print_count(struct counter *counter)
67 {
68 uint64_t total = get_total_count(counter);
69
70 PRINTF_COUNT("Event", event);
71 PRINTF_COUNT("Stream beginning", stream_begin);
72 PRINTF_COUNT("Stream end", stream_end);
73 PRINTF_COUNT("Stream activity beginning", stream_activity_begin);
74 PRINTF_COUNT("Stream activity end", stream_activity_end);
75 PRINTF_COUNT("Packet beginning", packet_begin);
76 PRINTF_COUNT("Packet end", packet_end);
77 PRINTF_COUNT("Discarded event", disc_events);
78 PRINTF_COUNT("Discarded packet", disc_packets);
79 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity);
80
81 if (counter->count.other > 0) {
82 PRINTF_COUNT("Other (unknown)", other);
83 }
84
85 printf("%s%15" PRIu64 " message%s (TOTAL)%s\n",
86 bt_common_color_bold(), total, total == 1 ? "" : "s",
87 bt_common_color_reset());
88 counter->last_printed_total = total;
89 }
90
91 static
92 void try_print_count(struct counter *counter, uint64_t msg_count)
93 {
94 if (counter->step == 0) {
95 /* No update */
96 return;
97 }
98
99 counter->at += msg_count;
100
101 if (counter->at >= counter->step) {
102 counter->at = 0;
103 print_count(counter);
104 putchar('\n');
105 }
106 }
107
108 static
109 void try_print_last(struct counter *counter)
110 {
111 const uint64_t total = get_total_count(counter);
112
113 if (total != counter->last_printed_total) {
114 print_count(counter);
115 }
116 }
117
118 void destroy_private_counter_data(struct counter *counter)
119 {
120 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
121 g_free(counter);
122 }
123
124 BT_HIDDEN
125 void counter_finalize(bt_self_component_sink *comp)
126 {
127 struct counter *counter;
128
129 BT_ASSERT(comp);
130 counter = bt_self_component_get_data(
131 bt_self_component_sink_as_self_component(comp));
132 BT_ASSERT(counter);
133 try_print_last(counter);
134 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
135 g_free(counter);
136 }
137
138 BT_HIDDEN
139 bt_self_component_status counter_init(
140 bt_self_component_sink *component,
141 const bt_value *params,
142 UNUSED_VAR void *init_method_data)
143 {
144 bt_self_component_status ret;
145 struct counter *counter = g_new0(struct counter, 1);
146 const bt_value *step = NULL;
147 const bt_value *hide_zero = NULL;
148
149 if (!counter) {
150 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
151 goto error;
152 }
153
154 ret = bt_self_component_sink_add_input_port(component,
155 "in", NULL, NULL);
156 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
157 goto error;
158 }
159
160 counter->last_printed_total = -1ULL;
161 counter->step = 10000;
162 step = bt_value_map_borrow_entry_value_const(params, "step");
163 if (step) {
164 if (!bt_value_is_unsigned_integer(step)) {
165 BT_LOGE("`step` parameter: expecting an unsigned integer value: "
166 "type=%s", bt_common_value_type_string(
167 bt_value_get_type(step)));
168 goto error;
169 }
170
171 counter->step = bt_value_unsigned_integer_get(step);
172 }
173
174 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
175 if (hide_zero) {
176 if (!bt_value_is_bool(hide_zero)) {
177 BT_LOGE("`hide-zero` parameter: expecting a boolean value: "
178 "type=%s", bt_common_value_type_string(
179 bt_value_get_type(hide_zero)));
180 goto error;
181 }
182
183 counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
184 }
185
186 bt_self_component_set_data(
187 bt_self_component_sink_as_self_component(component),
188 counter);
189 goto end;
190
191 error:
192 destroy_private_counter_data(counter);
193 ret = BT_SELF_COMPONENT_STATUS_ERROR;
194
195 end:
196 return ret;
197 }
198
199 BT_HIDDEN
200 bt_self_component_status counter_graph_is_configured(
201 bt_self_component_sink *comp)
202 {
203 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
204 struct counter *counter;
205 bt_self_component_port_input_message_iterator *iterator;
206
207 counter = bt_self_component_get_data(
208 bt_self_component_sink_as_self_component(comp));
209 BT_ASSERT(counter);
210 iterator = bt_self_component_port_input_message_iterator_create(
211 bt_self_component_sink_borrow_input_port_by_name(comp,
212 in_port_name));
213 if (!iterator) {
214 status = BT_SELF_COMPONENT_STATUS_NOMEM;
215 goto end;
216 }
217
218 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
219 counter->msg_iter, iterator);
220
221 end:
222 return status;
223 }
224
225 BT_HIDDEN
226 bt_self_component_status counter_consume(
227 bt_self_component_sink *comp)
228 {
229 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
230 struct counter *counter;
231 bt_message_iterator_status it_ret;
232 uint64_t msg_count;
233 bt_message_array_const msgs;
234
235 counter = bt_self_component_get_data(
236 bt_self_component_sink_as_self_component(comp));
237 BT_ASSERT(counter);
238
239 if (unlikely(!counter->msg_iter)) {
240 try_print_last(counter);
241 ret = BT_SELF_COMPONENT_STATUS_END;
242 goto end;
243 }
244
245 /* Consume messages */
246 it_ret = bt_self_component_port_input_message_iterator_next(
247 counter->msg_iter, &msgs, &msg_count);
248 if (it_ret < 0) {
249 ret = BT_SELF_COMPONENT_STATUS_ERROR;
250 goto end;
251 }
252
253 switch (it_ret) {
254 case BT_MESSAGE_ITERATOR_STATUS_OK:
255 {
256 uint64_t i;
257
258 for (i = 0; i < msg_count; i++) {
259 const bt_message *msg = msgs[i];
260
261 BT_ASSERT(msg);
262 switch (bt_message_get_type(msg)) {
263 case BT_MESSAGE_TYPE_EVENT:
264 counter->count.event++;
265 break;
266 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
267 counter->count.packet_begin++;
268 break;
269 case BT_MESSAGE_TYPE_PACKET_END:
270 counter->count.packet_end++;
271 break;
272 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
273 counter->count.msg_iter_inactivity++;
274 break;
275 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
276 counter->count.stream_begin++;
277 break;
278 case BT_MESSAGE_TYPE_STREAM_END:
279 counter->count.stream_end++;
280 break;
281 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
282 counter->count.stream_activity_begin++;
283 break;
284 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
285 counter->count.stream_activity_end++;
286 break;
287 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
288 counter->count.disc_events++;
289 break;
290 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
291 counter->count.disc_packets++;
292 break;
293 default:
294 counter->count.other++;
295 }
296
297 bt_message_put_ref(msg);
298 }
299
300 ret = BT_SELF_COMPONENT_STATUS_OK;
301 break;
302 }
303 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
304 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
305 goto end;
306 case BT_MESSAGE_ITERATOR_STATUS_END:
307 try_print_last(counter);
308 ret = BT_SELF_COMPONENT_STATUS_END;
309 goto end;
310 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
311 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
312 goto end;
313 default:
314 break;
315 }
316
317 try_print_count(counter, msg_count);
318
319 end:
320 return ret;
321 }
This page took 0.034882 seconds and 4 git commands to generate.