sink.utils.counter: use a default step of 10,000
[deliverable/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
ad0c0f09 33#define PRINTF_COUNT(_what, _var, args...) \
358340ec
PP
34 do { \
35 if (counter->count._var != 0 || !counter->hide_zero) { \
ad0c0f09 36 printf("%15" PRIu64 " %s message%s\n", \
358340ec 37 counter->count._var, \
ad0c0f09
PP
38 (_what), \
39 counter->count._var == 1 ? "" : "s"); \
358340ec
PP
40 } \
41 } while (0)
42
734a0572
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 +
ad0c0f09
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 +
ad0c0f09
PP
56 counter->count.disc_events +
57 counter->count.disc_packets +
12ccec6b 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
ad0c0f09
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) {
ad0c0f09 79 PRINTF_COUNT("Other (unknown)", other);
358340ec
PP
80 }
81
c14831f0 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
c14831f0 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
c14831f0 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{
c14831f0 117 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
358340ec
PP
118 g_free(counter);
119}
120
d27a81b2 121BT_HIDDEN
b9879703 122void counter_finalize(bt_self_component_sink *comp)
358340ec
PP
123{
124 struct counter *counter;
125
d27a81b2
PP
126 BT_ASSERT(comp);
127 counter = bt_self_component_get_data(
27c0f01f 128 bt_self_component_sink_as_self_component(comp));
f6ccaed9 129 BT_ASSERT(counter);
358340ec 130 try_print_last(counter);
c14831f0 131 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
358340ec
PP
132 g_free(counter);
133}
134
d27a81b2 135BT_HIDDEN
b7c5b62e 136bt_self_component_status counter_init(
b9879703
PP
137 bt_self_component_sink *component,
138 const bt_value *params,
82fa4539 139 UNUSED_VAR void *init_method_data)
358340ec 140{
b7c5b62e 141 bt_self_component_status ret;
358340ec 142 struct counter *counter = g_new0(struct counter, 1);
b9879703
PP
143 const bt_value *step = NULL;
144 const bt_value *hide_zero = NULL;
358340ec
PP
145
146 if (!counter) {
d27a81b2
PP
147 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
148 goto error;
358340ec
PP
149 }
150
d27a81b2 151 ret = bt_self_component_sink_add_input_port(component,
358340ec 152 "in", NULL, NULL);
d27a81b2
PP
153 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
154 goto error;
358340ec
PP
155 }
156
157 counter->last_printed_total = -1ULL;
ed78dfc7 158 counter->step = 10000;
82fa4539 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
605c62ba 163 val = bt_value_integer_get(step);
358340ec
PP
164 if (val >= 0) {
165 counter->step = (uint64_t) val;
166 }
167 }
168
82fa4539 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
605c62ba 173 val = bt_value_bool_get(hide_zero);
358340ec
PP
174 counter->hide_zero = (bool) val;
175 }
176
d27a81b2 177 bt_self_component_set_data(
27c0f01f 178 bt_self_component_sink_as_self_component(component),
d27a81b2 179 counter);
358340ec
PP
180 goto end;
181
182error:
183 destroy_private_counter_data(counter);
184
185end:
358340ec
PP
186 return ret;
187}
188
d27a81b2 189BT_HIDDEN
734a0572
PP
190bt_self_component_status counter_graph_is_configured(
191 bt_self_component_sink *comp)
358340ec 192{
b7c5b62e 193 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
358340ec 194 struct counter *counter;
c14831f0 195 bt_self_component_port_input_message_iterator *iterator;
358340ec 196
d27a81b2 197 counter = bt_self_component_get_data(
27c0f01f 198 bt_self_component_sink_as_self_component(comp));
f6ccaed9 199 BT_ASSERT(counter);
c14831f0 200 iterator = bt_self_component_port_input_message_iterator_create(
734a0572
PP
201 bt_self_component_sink_borrow_input_port_by_name(comp,
202 in_port_name));
d27a81b2
PP
203 if (!iterator) {
204 status = BT_SELF_COMPONENT_STATUS_NOMEM;
358340ec
PP
205 goto end;
206 }
207
c14831f0
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
d27a81b2 215BT_HIDDEN
b7c5b62e 216bt_self_component_status counter_consume(
b9879703 217 bt_self_component_sink *comp)
358340ec 218{
b7c5b62e 219 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
358340ec 220 struct counter *counter;
b7c5b62e 221 bt_message_iterator_status it_ret;
c14831f0
PP
222 uint64_t msg_count;
223 bt_message_array_const msgs;
358340ec 224
d27a81b2 225 counter = bt_self_component_get_data(
27c0f01f 226 bt_self_component_sink_as_self_component(comp));
f6ccaed9 227 BT_ASSERT(counter);
358340ec 228
c14831f0 229 if (unlikely(!counter->msg_iter)) {
358340ec 230 try_print_last(counter);
d27a81b2 231 ret = BT_SELF_COMPONENT_STATUS_END;
358340ec
PP
232 goto end;
233 }
234
c14831f0
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) {
d27a81b2 239 ret = BT_SELF_COMPONENT_STATUS_ERROR;
358340ec
PP
240 goto end;
241 }
242
243 switch (it_ret) {
c14831f0 244 case BT_MESSAGE_ITERATOR_STATUS_OK:
358340ec 245 {
d4393e08
PP
246 uint64_t i;
247
c14831f0
PP
248 for (i = 0; i < msg_count; i++) {
249 const bt_message *msg = msgs[i];
d4393e08 250
c14831f0
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;
ad0c0f09
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;
12ccec6b
FD
262 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
263 counter->count.msg_iter_inactivity++;
d4393e08 264 break;
c14831f0 265 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
d4393e08
PP
266 counter->count.stream_begin++;
267 break;
c14831f0 268 case BT_MESSAGE_TYPE_STREAM_END:
d4393e08
PP
269 counter->count.stream_end++;
270 break;
ad0c0f09
PP
271 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
272 counter->count.stream_activity_begin++;
d4393e08 273 break;
ad0c0f09
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
c14831f0 287 bt_message_put_ref(msg);
358340ec 288 }
d27a81b2
PP
289
290 ret = BT_SELF_COMPONENT_STATUS_OK;
291 break;
358340ec 292 }
c14831f0 293 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
d27a81b2
PP
294 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
295 goto end;
c14831f0 296 case BT_MESSAGE_ITERATOR_STATUS_END:
d27a81b2
PP
297 try_print_last(counter);
298 ret = BT_SELF_COMPONENT_STATUS_END;
299 goto end;
c14831f0 300 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
d27a81b2
PP
301 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
302 goto end;
358340ec
PP
303 default:
304 break;
305 }
306
c14831f0 307 try_print_count(counter, msg_count);
358340ec
PP
308
309end:
358340ec
PP
310 return ret;
311}
This page took 0.044807 seconds and 5 git commands to generate.