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