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