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