sink.utils.counter: handle all types of messages
[babeltrace.git] / plugins / utils / counter / counter.c
CommitLineData
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
0e847b69 33#define PRINTF_COUNT(_what, _var, args...) \
358340ec
PP
34 do { \
35 if (counter->count._var != 0 || !counter->hide_zero) { \
0e847b69 36 printf("%15" PRIu64 " %s message%s\n", \
358340ec 37 counter->count._var, \
0e847b69
PP
38 (_what), \
39 counter->count._var == 1 ? "" : "s"); \
358340ec
PP
40 } \
41 } while (0)
42
5badd463
PP
43static
44const char * const in_port_name = "in";
45
358340ec
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 +
0e847b69
PP
52 counter->count.stream_activity_begin +
53 counter->count.stream_activity_end +
358340ec
PP
54 counter->count.packet_begin +
55 counter->count.packet_end +
0e847b69
PP
56 counter->count.disc_events +
57 counter->count.disc_packets +
b9fd9cbb 58 counter->count.msg_iter_inactivity +
358340ec
PP
59 counter->count.other;
60}
61
62static
d4393e08 63void print_count(struct counter *counter)
358340ec 64{
d4393e08
PP
65 uint64_t total = get_total_count(counter);
66
0e847b69
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);
358340ec
PP
77
78 if (counter->count.other > 0) {
0e847b69 79 PRINTF_COUNT("Other (unknown)", other);
358340ec
PP
80 }
81
d6e69534 82 printf("%s%15" PRIu64 " message%s (TOTAL)%s\n",
358340ec
PP
83 bt_common_color_bold(), total, total == 1 ? "" : "s",
84 bt_common_color_reset());
85 counter->last_printed_total = total;
86}
87
88static
d6e69534 89void try_print_count(struct counter *counter, uint64_t msg_count)
358340ec 90{
358340ec
PP
91 if (counter->step == 0) {
92 /* No update */
93 return;
94 }
95
d6e69534 96 counter->at += msg_count;
358340ec 97
d4393e08
PP
98 if (counter->at >= counter->step) {
99 counter->at = 0;
100 print_count(counter);
358340ec
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) {
d4393e08 111 print_count(counter);
358340ec
PP
112 }
113}
114
115void destroy_private_counter_data(struct counter *counter)
116{
d6e69534 117 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
358340ec
PP
118 g_free(counter);
119}
120
d94d92ac 121BT_HIDDEN
b19ff26f 122void counter_finalize(bt_self_component_sink *comp)
358340ec
PP
123{
124 struct counter *counter;
125
d94d92ac
PP
126 BT_ASSERT(comp);
127 counter = bt_self_component_get_data(
707b7d35 128 bt_self_component_sink_as_self_component(comp));
f6ccaed9 129 BT_ASSERT(counter);
358340ec 130 try_print_last(counter);
d6e69534 131 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
358340ec
PP
132 g_free(counter);
133}
134
d94d92ac 135BT_HIDDEN
4cdfc5e8 136bt_self_component_status counter_init(
b19ff26f
PP
137 bt_self_component_sink *component,
138 const bt_value *params,
05e21286 139 UNUSED_VAR void *init_method_data)
358340ec 140{
4cdfc5e8 141 bt_self_component_status ret;
358340ec 142 struct counter *counter = g_new0(struct counter, 1);
b19ff26f
PP
143 const bt_value *step = NULL;
144 const bt_value *hide_zero = NULL;
358340ec
PP
145
146 if (!counter) {
d94d92ac
PP
147 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
148 goto error;
358340ec
PP
149 }
150
d94d92ac 151 ret = bt_self_component_sink_add_input_port(component,
358340ec 152 "in", NULL, NULL);
d94d92ac
PP
153 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
154 goto error;
358340ec
PP
155 }
156
157 counter->last_printed_total = -1ULL;
158 counter->step = 1000;
05e21286 159 step = bt_value_map_borrow_entry_value_const(params, "step");
f6ccaed9 160 if (step && bt_value_is_integer(step)) {
358340ec 161 int64_t val;
358340ec 162
601b0d3c 163 val = bt_value_integer_get(step);
358340ec
PP
164 if (val >= 0) {
165 counter->step = (uint64_t) val;
166 }
167 }
168
05e21286 169 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
f6ccaed9 170 if (hide_zero && bt_value_is_bool(hide_zero)) {
358340ec 171 bt_bool val;
358340ec 172
601b0d3c 173 val = bt_value_bool_get(hide_zero);
358340ec
PP
174 counter->hide_zero = (bool) val;
175 }
176
d94d92ac 177 bt_self_component_set_data(
707b7d35 178 bt_self_component_sink_as_self_component(component),
d94d92ac 179 counter);
358340ec
PP
180 goto end;
181
182error:
183 destroy_private_counter_data(counter);
184
185end:
358340ec
PP
186 return ret;
187}
188
d94d92ac 189BT_HIDDEN
5badd463
PP
190bt_self_component_status counter_graph_is_configured(
191 bt_self_component_sink *comp)
358340ec 192{
4cdfc5e8 193 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
358340ec 194 struct counter *counter;
d6e69534 195 bt_self_component_port_input_message_iterator *iterator;
358340ec 196
d94d92ac 197 counter = bt_self_component_get_data(
707b7d35 198 bt_self_component_sink_as_self_component(comp));
f6ccaed9 199 BT_ASSERT(counter);
d6e69534 200 iterator = bt_self_component_port_input_message_iterator_create(
5badd463
PP
201 bt_self_component_sink_borrow_input_port_by_name(comp,
202 in_port_name));
d94d92ac
PP
203 if (!iterator) {
204 status = BT_SELF_COMPONENT_STATUS_NOMEM;
358340ec
PP
205 goto end;
206 }
207
d6e69534
PP
208 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
209 counter->msg_iter, iterator);
358340ec
PP
210
211end:
bf55043c 212 return status;
358340ec
PP
213}
214
d94d92ac 215BT_HIDDEN
4cdfc5e8 216bt_self_component_status counter_consume(
b19ff26f 217 bt_self_component_sink *comp)
358340ec 218{
4cdfc5e8 219 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
358340ec 220 struct counter *counter;
4cdfc5e8 221 bt_message_iterator_status it_ret;
d6e69534
PP
222 uint64_t msg_count;
223 bt_message_array_const msgs;
358340ec 224
d94d92ac 225 counter = bt_self_component_get_data(
707b7d35 226 bt_self_component_sink_as_self_component(comp));
f6ccaed9 227 BT_ASSERT(counter);
358340ec 228
d6e69534 229 if (unlikely(!counter->msg_iter)) {
358340ec 230 try_print_last(counter);
d94d92ac 231 ret = BT_SELF_COMPONENT_STATUS_END;
358340ec
PP
232 goto end;
233 }
234
d6e69534
PP
235 /* Consume messages */
236 it_ret = bt_self_component_port_input_message_iterator_next(
237 counter->msg_iter, &msgs, &msg_count);
358340ec 238 if (it_ret < 0) {
d94d92ac 239 ret = BT_SELF_COMPONENT_STATUS_ERROR;
358340ec
PP
240 goto end;
241 }
242
243 switch (it_ret) {
d6e69534 244 case BT_MESSAGE_ITERATOR_STATUS_OK:
358340ec 245 {
d4393e08
PP
246 uint64_t i;
247
d6e69534
PP
248 for (i = 0; i < msg_count; i++) {
249 const bt_message *msg = msgs[i];
d4393e08 250
d6e69534
PP
251 BT_ASSERT(msg);
252 switch (bt_message_get_type(msg)) {
253 case BT_MESSAGE_TYPE_EVENT:
d4393e08
PP
254 counter->count.event++;
255 break;
0e847b69
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;
b9fd9cbb
FD
262 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
263 counter->count.msg_iter_inactivity++;
d4393e08 264 break;
d6e69534 265 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
d4393e08
PP
266 counter->count.stream_begin++;
267 break;
d6e69534 268 case BT_MESSAGE_TYPE_STREAM_END:
d4393e08
PP
269 counter->count.stream_end++;
270 break;
0e847b69
PP
271 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
272 counter->count.stream_activity_begin++;
d4393e08 273 break;
0e847b69
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++;
d4393e08 282 break;
d4393e08
PP
283 default:
284 counter->count.other++;
358340ec 285 }
d4393e08 286
d6e69534 287 bt_message_put_ref(msg);
358340ec 288 }
d94d92ac
PP
289
290 ret = BT_SELF_COMPONENT_STATUS_OK;
291 break;
358340ec 292 }
d6e69534 293 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
d94d92ac
PP
294 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
295 goto end;
d6e69534 296 case BT_MESSAGE_ITERATOR_STATUS_END:
d94d92ac
PP
297 try_print_last(counter);
298 ret = BT_SELF_COMPONENT_STATUS_END;
299 goto end;
d6e69534 300 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
d94d92ac
PP
301 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
302 goto end;
358340ec
PP
303 default:
304 break;
305 }
306
d6e69534 307 try_print_count(counter, msg_count);
358340ec
PP
308
309end:
358340ec
PP
310 return ret;
311}
This page took 0.046862 seconds and 4 git commands to generate.