tests/bindings/python/bt2: remove unneeded `import` lines
[babeltrace.git] / src / 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
627c70e6 23#define BT_COMP_LOG_SELF_COMP (counter->self_comp)
c58e2582 24#define BT_LOG_OUTPUT_LEVEL (counter->log_level)
b03487ab 25#define BT_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER"
627c70e6 26#include "plugins/comp-logging.h"
68d9d039 27
71c5da58 28#include <babeltrace2/babeltrace.h>
85e7137b 29#include "common/macros.h"
57952005 30#include "common/common.h"
57952005 31#include "common/assert.h"
14b7ef9e
PP
32#include <inttypes.h>
33#include <stdint.h>
34
35#include "counter.h"
36
168baad4 37#define PRINTF_COUNT(_what, _var, args...) \
14b7ef9e
PP
38 do { \
39 if (counter->count._var != 0 || !counter->hide_zero) { \
168baad4 40 printf("%15" PRIu64 " %s message%s\n", \
14b7ef9e 41 counter->count._var, \
168baad4
PP
42 (_what), \
43 counter->count._var == 1 ? "" : "s"); \
14b7ef9e
PP
44 } \
45 } while (0)
46
1043fdea
PP
47static
48const char * const in_port_name = "in";
49
14b7ef9e
PP
50static
51uint64_t get_total_count(struct counter *counter)
52{
53 return counter->count.event +
54 counter->count.stream_begin +
55 counter->count.stream_end +
56 counter->count.packet_begin +
57 counter->count.packet_end +
168baad4
PP
58 counter->count.disc_events +
59 counter->count.disc_packets +
40bf6fd0 60 counter->count.msg_iter_inactivity +
14b7ef9e
PP
61 counter->count.other;
62}
63
64static
3fd7b79d 65void print_count(struct counter *counter)
14b7ef9e 66{
3fd7b79d
PP
67 uint64_t total = get_total_count(counter);
68
168baad4
PP
69 PRINTF_COUNT("Event", event);
70 PRINTF_COUNT("Stream beginning", stream_begin);
71 PRINTF_COUNT("Stream end", stream_end);
168baad4
PP
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
fb25b9e3 136bt_component_class_init_method_status counter_init(
8eee8ea2
PP
137 bt_self_component_sink *component,
138 const bt_value *params,
f807570c 139 __attribute__((unused)) void *init_method_data)
14b7ef9e 140{
fb25b9e3
PP
141 bt_component_class_init_method_status status =
142 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
143 bt_self_component_add_port_status add_port_status;
14b7ef9e 144 struct counter *counter = g_new0(struct counter, 1);
8eee8ea2
PP
145 const bt_value *step = NULL;
146 const bt_value *hide_zero = NULL;
14b7ef9e
PP
147
148 if (!counter) {
fb25b9e3 149 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
834e9996 150 goto error;
14b7ef9e
PP
151 }
152
627c70e6
PP
153 counter->self_comp =
154 bt_self_component_sink_as_self_component(component);
c58e2582 155 counter->log_level = bt_component_get_logging_level(
627c70e6 156 bt_self_component_as_component(counter->self_comp));
fb25b9e3 157 add_port_status = bt_self_component_sink_add_input_port(component,
14b7ef9e 158 "in", NULL, NULL);
fb25b9e3
PP
159 switch (add_port_status) {
160 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
161 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
834e9996 162 goto error;
fb25b9e3
PP
163 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
164 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
165 goto error;
166 default:
167 break;
14b7ef9e
PP
168 }
169
170 counter->last_printed_total = -1ULL;
a79f976c 171 counter->step = 10000;
ce141536 172 step = bt_value_map_borrow_entry_value_const(params, "step");
68d9d039
PP
173 if (step) {
174 if (!bt_value_is_unsigned_integer(step)) {
627c70e6 175 BT_COMP_LOGE("`step` parameter: expecting an unsigned integer value: "
68d9d039
PP
176 "type=%s", bt_common_value_type_string(
177 bt_value_get_type(step)));
178 goto error;
14b7ef9e 179 }
68d9d039 180
60bbfc7c 181 counter->step = bt_value_integer_unsigned_get(step);
14b7ef9e
PP
182 }
183
ce141536 184 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
68d9d039
PP
185 if (hide_zero) {
186 if (!bt_value_is_bool(hide_zero)) {
627c70e6 187 BT_COMP_LOGE("`hide-zero` parameter: expecting a boolean value: "
68d9d039
PP
188 "type=%s", bt_common_value_type_string(
189 bt_value_get_type(hide_zero)));
190 goto error;
191 }
14b7ef9e 192
68d9d039 193 counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
14b7ef9e
PP
194 }
195
834e9996 196 bt_self_component_set_data(
bb61965b 197 bt_self_component_sink_as_self_component(component),
834e9996 198 counter);
14b7ef9e
PP
199 goto end;
200
201error:
202 destroy_private_counter_data(counter);
fb25b9e3
PP
203
204 if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
205 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
206 }
14b7ef9e
PP
207
208end:
fb25b9e3 209 return status;
14b7ef9e
PP
210}
211
834e9996 212BT_HIDDEN
fb25b9e3
PP
213bt_component_class_sink_graph_is_configured_method_status
214counter_graph_is_configured(
1043fdea 215 bt_self_component_sink *comp)
14b7ef9e 216{
fb25b9e3
PP
217 bt_component_class_sink_graph_is_configured_method_status status =
218 BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
14b7ef9e 219 struct counter *counter;
b09a5592 220 bt_self_component_port_input_message_iterator *iterator;
14b7ef9e 221
834e9996 222 counter = bt_self_component_get_data(
bb61965b 223 bt_self_component_sink_as_self_component(comp));
8b45963b 224 BT_ASSERT(counter);
692f1a01
PP
225 iterator = bt_self_component_port_input_message_iterator_create_from_sink_component(
226 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
1043fdea 227 in_port_name));
834e9996 228 if (!iterator) {
fb25b9e3 229 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_MEMORY_ERROR;
14b7ef9e
PP
230 goto end;
231 }
232
b09a5592
PP
233 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
234 counter->msg_iter, iterator);
14b7ef9e
PP
235
236end:
634f394c 237 return status;
14b7ef9e
PP
238}
239
834e9996 240BT_HIDDEN
fb25b9e3 241bt_component_class_sink_consume_method_status counter_consume(
8eee8ea2 242 bt_self_component_sink *comp)
14b7ef9e 243{
fb25b9e3
PP
244 bt_component_class_sink_consume_method_status status =
245 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
14b7ef9e 246 struct counter *counter;
fb25b9e3 247 bt_message_iterator_next_status next_status;
b09a5592
PP
248 uint64_t msg_count;
249 bt_message_array_const msgs;
14b7ef9e 250
834e9996 251 counter = bt_self_component_get_data(
bb61965b 252 bt_self_component_sink_as_self_component(comp));
8b45963b 253 BT_ASSERT(counter);
14b7ef9e 254
85e7137b 255 if (G_UNLIKELY(!counter->msg_iter)) {
14b7ef9e 256 try_print_last(counter);
fb25b9e3 257 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
14b7ef9e
PP
258 goto end;
259 }
260
b09a5592 261 /* Consume messages */
fb25b9e3 262 next_status = bt_self_component_port_input_message_iterator_next(
b09a5592 263 counter->msg_iter, &msgs, &msg_count);
fb25b9e3
PP
264 if (next_status < 0) {
265 status = (int) next_status;
14b7ef9e
PP
266 goto end;
267 }
268
fb25b9e3
PP
269 switch (next_status) {
270 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
14b7ef9e 271 {
3fd7b79d
PP
272 uint64_t i;
273
b09a5592
PP
274 for (i = 0; i < msg_count; i++) {
275 const bt_message *msg = msgs[i];
3fd7b79d 276
b09a5592
PP
277 BT_ASSERT(msg);
278 switch (bt_message_get_type(msg)) {
279 case BT_MESSAGE_TYPE_EVENT:
3fd7b79d
PP
280 counter->count.event++;
281 break;
168baad4
PP
282 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
283 counter->count.packet_begin++;
284 break;
285 case BT_MESSAGE_TYPE_PACKET_END:
286 counter->count.packet_end++;
287 break;
40bf6fd0
FD
288 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
289 counter->count.msg_iter_inactivity++;
3fd7b79d 290 break;
b09a5592 291 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
3fd7b79d
PP
292 counter->count.stream_begin++;
293 break;
b09a5592 294 case BT_MESSAGE_TYPE_STREAM_END:
3fd7b79d
PP
295 counter->count.stream_end++;
296 break;
168baad4
PP
297 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
298 counter->count.disc_events++;
299 break;
300 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
301 counter->count.disc_packets++;
3fd7b79d 302 break;
3fd7b79d
PP
303 default:
304 counter->count.other++;
14b7ef9e 305 }
3fd7b79d 306
b09a5592 307 bt_message_put_ref(msg);
14b7ef9e 308 }
834e9996 309
fb25b9e3 310 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
834e9996 311 break;
14b7ef9e 312 }
fb25b9e3
PP
313 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
314 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
834e9996 315 goto end;
fb25b9e3 316 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
834e9996 317 try_print_last(counter);
fb25b9e3 318 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
834e9996 319 goto end;
fb25b9e3
PP
320 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
321 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
834e9996 322 goto end;
14b7ef9e
PP
323 default:
324 break;
325 }
326
b09a5592 327 try_print_count(counter, msg_count);
14b7ef9e
PP
328
329end:
fb25b9e3 330 return status;
14b7ef9e 331}
This page took 0.053083 seconds and 4 git commands to generate.