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