Rename `init` methods to `initialize`
[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"
3fa1b6a3 26#include "logging/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{
888e0925
FD
117 if (counter) {
118 bt_self_component_port_input_message_iterator_put_ref(
119 counter->msg_iter);
120 g_free(counter);
121 }
14b7ef9e
PP
122}
123
834e9996 124BT_HIDDEN
8eee8ea2 125void counter_finalize(bt_self_component_sink *comp)
14b7ef9e
PP
126{
127 struct counter *counter;
128
834e9996
PP
129 BT_ASSERT(comp);
130 counter = bt_self_component_get_data(
bb61965b 131 bt_self_component_sink_as_self_component(comp));
8b45963b 132 BT_ASSERT(counter);
14b7ef9e 133 try_print_last(counter);
b09a5592 134 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
14b7ef9e
PP
135 g_free(counter);
136}
137
834e9996 138BT_HIDDEN
4175c1d5 139bt_component_class_initialize_method_status counter_init(
8eee8ea2 140 bt_self_component_sink *component,
e3250e61 141 bt_self_component_sink_configuration *config,
8eee8ea2 142 const bt_value *params,
f807570c 143 __attribute__((unused)) void *init_method_data)
14b7ef9e 144{
4175c1d5
FD
145 bt_component_class_initialize_method_status status =
146 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
fb25b9e3 147 bt_self_component_add_port_status add_port_status;
14b7ef9e 148 struct counter *counter = g_new0(struct counter, 1);
8eee8ea2
PP
149 const bt_value *step = NULL;
150 const bt_value *hide_zero = NULL;
14b7ef9e
PP
151
152 if (!counter) {
4175c1d5 153 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
834e9996 154 goto error;
14b7ef9e
PP
155 }
156
627c70e6
PP
157 counter->self_comp =
158 bt_self_component_sink_as_self_component(component);
c58e2582 159 counter->log_level = bt_component_get_logging_level(
627c70e6 160 bt_self_component_as_component(counter->self_comp));
fb25b9e3 161 add_port_status = bt_self_component_sink_add_input_port(component,
14b7ef9e 162 "in", NULL, NULL);
fb25b9e3
PP
163 switch (add_port_status) {
164 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
4175c1d5 165 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
834e9996 166 goto error;
fb25b9e3 167 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
4175c1d5 168 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
fb25b9e3
PP
169 goto error;
170 default:
171 break;
14b7ef9e
PP
172 }
173
174 counter->last_printed_total = -1ULL;
a79f976c 175 counter->step = 10000;
ce141536 176 step = bt_value_map_borrow_entry_value_const(params, "step");
68d9d039
PP
177 if (step) {
178 if (!bt_value_is_unsigned_integer(step)) {
627c70e6 179 BT_COMP_LOGE("`step` parameter: expecting an unsigned integer value: "
68d9d039
PP
180 "type=%s", bt_common_value_type_string(
181 bt_value_get_type(step)));
182 goto error;
14b7ef9e 183 }
68d9d039 184
60bbfc7c 185 counter->step = bt_value_integer_unsigned_get(step);
14b7ef9e
PP
186 }
187
ce141536 188 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
68d9d039
PP
189 if (hide_zero) {
190 if (!bt_value_is_bool(hide_zero)) {
627c70e6 191 BT_COMP_LOGE("`hide-zero` parameter: expecting a boolean value: "
68d9d039
PP
192 "type=%s", bt_common_value_type_string(
193 bt_value_get_type(hide_zero)));
194 goto error;
195 }
14b7ef9e 196
68d9d039 197 counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
14b7ef9e
PP
198 }
199
834e9996 200 bt_self_component_set_data(
bb61965b 201 bt_self_component_sink_as_self_component(component),
834e9996 202 counter);
14b7ef9e
PP
203 goto end;
204
205error:
206 destroy_private_counter_data(counter);
fb25b9e3 207
4175c1d5
FD
208 if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
209 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
fb25b9e3 210 }
14b7ef9e
PP
211
212end:
fb25b9e3 213 return status;
14b7ef9e
PP
214}
215
834e9996 216BT_HIDDEN
fb25b9e3
PP
217bt_component_class_sink_graph_is_configured_method_status
218counter_graph_is_configured(
1043fdea 219 bt_self_component_sink *comp)
14b7ef9e 220{
ab8b2b1b
SM
221 bt_component_class_sink_graph_is_configured_method_status status;
222 bt_self_component_port_input_message_iterator_create_from_sink_component_status
223 msg_iter_status;
14b7ef9e 224 struct counter *counter;
b09a5592 225 bt_self_component_port_input_message_iterator *iterator;
14b7ef9e 226
834e9996 227 counter = bt_self_component_get_data(
bb61965b 228 bt_self_component_sink_as_self_component(comp));
8b45963b 229 BT_ASSERT(counter);
ab8b2b1b
SM
230
231 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component(
692f1a01 232 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
ab8b2b1b
SM
233 in_port_name), &iterator);
234 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
235 status = (int) msg_iter_status;
14b7ef9e
PP
236 goto end;
237 }
238
b09a5592
PP
239 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
240 counter->msg_iter, iterator);
14b7ef9e 241
ab8b2b1b 242 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
14b7ef9e 243end:
634f394c 244 return status;
14b7ef9e
PP
245}
246
834e9996 247BT_HIDDEN
fb25b9e3 248bt_component_class_sink_consume_method_status counter_consume(
8eee8ea2 249 bt_self_component_sink *comp)
14b7ef9e 250{
fb25b9e3
PP
251 bt_component_class_sink_consume_method_status status =
252 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
14b7ef9e 253 struct counter *counter;
fb25b9e3 254 bt_message_iterator_next_status next_status;
b09a5592
PP
255 uint64_t msg_count;
256 bt_message_array_const msgs;
14b7ef9e 257
834e9996 258 counter = bt_self_component_get_data(
bb61965b 259 bt_self_component_sink_as_self_component(comp));
8b45963b 260 BT_ASSERT(counter);
14b7ef9e 261
85e7137b 262 if (G_UNLIKELY(!counter->msg_iter)) {
14b7ef9e 263 try_print_last(counter);
fb25b9e3 264 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
14b7ef9e
PP
265 goto end;
266 }
267
b09a5592 268 /* Consume messages */
fb25b9e3 269 next_status = bt_self_component_port_input_message_iterator_next(
b09a5592 270 counter->msg_iter, &msgs, &msg_count);
fb25b9e3
PP
271 if (next_status < 0) {
272 status = (int) next_status;
14b7ef9e
PP
273 goto end;
274 }
275
fb25b9e3
PP
276 switch (next_status) {
277 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
14b7ef9e 278 {
3fd7b79d
PP
279 uint64_t i;
280
b09a5592
PP
281 for (i = 0; i < msg_count; i++) {
282 const bt_message *msg = msgs[i];
3fd7b79d 283
b09a5592
PP
284 BT_ASSERT(msg);
285 switch (bt_message_get_type(msg)) {
286 case BT_MESSAGE_TYPE_EVENT:
3fd7b79d
PP
287 counter->count.event++;
288 break;
168baad4
PP
289 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
290 counter->count.packet_begin++;
291 break;
292 case BT_MESSAGE_TYPE_PACKET_END:
293 counter->count.packet_end++;
294 break;
40bf6fd0
FD
295 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
296 counter->count.msg_iter_inactivity++;
3fd7b79d 297 break;
b09a5592 298 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
3fd7b79d
PP
299 counter->count.stream_begin++;
300 break;
b09a5592 301 case BT_MESSAGE_TYPE_STREAM_END:
3fd7b79d
PP
302 counter->count.stream_end++;
303 break;
168baad4
PP
304 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
305 counter->count.disc_events++;
306 break;
307 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
308 counter->count.disc_packets++;
3fd7b79d 309 break;
3fd7b79d
PP
310 default:
311 counter->count.other++;
14b7ef9e 312 }
3fd7b79d 313
b09a5592 314 bt_message_put_ref(msg);
14b7ef9e 315 }
834e9996 316
fb25b9e3 317 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
834e9996 318 break;
14b7ef9e 319 }
fb25b9e3
PP
320 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
321 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
834e9996 322 goto end;
fb25b9e3 323 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
834e9996 324 try_print_last(counter);
fb25b9e3 325 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
834e9996 326 goto end;
fb25b9e3
PP
327 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
328 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
834e9996 329 goto end;
14b7ef9e
PP
330 default:
331 break;
332 }
333
b09a5592 334 try_print_count(counter, msg_count);
14b7ef9e
PP
335
336end:
fb25b9e3 337 return status;
14b7ef9e 338}
This page took 0.075592 seconds and 4 git commands to generate.