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