cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / plugins / utils / counter / counter.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_COMP_LOG_SELF_COMP (counter->self_comp)
8 #define BT_LOG_OUTPUT_LEVEL (counter->log_level)
9 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER"
10 #include "logging/comp-logging.h"
11
12 #include <babeltrace2/babeltrace.h>
13 #include "common/macros.h"
14 #include "common/common.h"
15 #include "common/assert.h"
16 #include <inttypes.h>
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include "plugins/common/param-validation/param-validation.h"
20
21 #include "counter.h"
22
23 #define PRINTF_COUNT(_what, _var, args...) \
24 do { \
25 if (counter->count._var != 0 || !counter->hide_zero) { \
26 printf("%15" PRIu64 " %s message%s\n", \
27 counter->count._var, \
28 (_what), \
29 counter->count._var == 1 ? "" : "s"); \
30 } \
31 } while (0)
32
33 static
34 const char * const in_port_name = "in";
35
36 static
37 uint64_t get_total_count(struct counter *counter)
38 {
39 return counter->count.event +
40 counter->count.stream_begin +
41 counter->count.stream_end +
42 counter->count.packet_begin +
43 counter->count.packet_end +
44 counter->count.disc_events +
45 counter->count.disc_packets +
46 counter->count.msg_iter_inactivity +
47 counter->count.other;
48 }
49
50 static
51 void print_count(struct counter *counter)
52 {
53 uint64_t total = get_total_count(counter);
54
55 PRINTF_COUNT("Event", event);
56 PRINTF_COUNT("Stream beginning", stream_begin);
57 PRINTF_COUNT("Stream end", stream_end);
58 PRINTF_COUNT("Packet beginning", packet_begin);
59 PRINTF_COUNT("Packet end", packet_end);
60 PRINTF_COUNT("Discarded event", disc_events);
61 PRINTF_COUNT("Discarded packet", disc_packets);
62 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity);
63
64 if (counter->count.other > 0) {
65 PRINTF_COUNT("Other (unknown)", other);
66 }
67
68 printf("%s%15" PRIu64 " message%s (TOTAL)%s\n",
69 bt_common_color_bold(), total, total == 1 ? "" : "s",
70 bt_common_color_reset());
71 counter->last_printed_total = total;
72 }
73
74 static
75 void try_print_count(struct counter *counter, uint64_t msg_count)
76 {
77 if (counter->step == 0) {
78 /* No update */
79 return;
80 }
81
82 counter->at += msg_count;
83
84 if (counter->at >= counter->step) {
85 counter->at = 0;
86 print_count(counter);
87 putchar('\n');
88 }
89 }
90
91 static
92 void try_print_last(struct counter *counter)
93 {
94 const uint64_t total = get_total_count(counter);
95
96 if (total != counter->last_printed_total) {
97 print_count(counter);
98 }
99 }
100
101 static
102 void destroy_private_counter_data(struct counter *counter)
103 {
104 if (counter) {
105 bt_message_iterator_put_ref(
106 counter->msg_iter);
107 g_free(counter);
108 }
109 }
110
111 void counter_finalize(bt_self_component_sink *comp)
112 {
113 struct counter *counter;
114
115 BT_ASSERT(comp);
116 counter = bt_self_component_get_data(
117 bt_self_component_sink_as_self_component(comp));
118 BT_ASSERT(counter);
119 try_print_last(counter);
120 bt_message_iterator_put_ref(counter->msg_iter);
121 g_free(counter);
122 }
123
124 static
125 struct bt_param_validation_map_value_entry_descr counter_params[] = {
126 { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_UNSIGNED_INTEGER } },
127 { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
128 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
129 };
130
131 bt_component_class_initialize_method_status counter_init(
132 bt_self_component_sink *component,
133 bt_self_component_sink_configuration *config __attribute__((unused)),
134 const bt_value *params,
135 void *init_method_data __attribute__((unused)))
136 {
137 bt_component_class_initialize_method_status status;
138 bt_self_component_add_port_status add_port_status;
139 struct counter *counter = g_new0(struct counter, 1);
140 const bt_value *step = NULL;
141 const bt_value *hide_zero = NULL;
142 enum bt_param_validation_status validation_status;
143 gchar *validate_error = NULL;
144
145 if (!counter) {
146 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
147 goto error;
148 }
149
150 counter->self_comp =
151 bt_self_component_sink_as_self_component(component);
152 counter->log_level = bt_component_get_logging_level(
153 bt_self_component_as_component(counter->self_comp));
154 add_port_status = bt_self_component_sink_add_input_port(component,
155 "in", NULL, NULL);
156 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
157 status = (int) add_port_status;
158 goto error;
159 }
160
161 validation_status = bt_param_validation_validate(params,
162 counter_params, &validate_error);
163 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
164 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
165 goto error;
166 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
167 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
168 BT_COMP_LOGE_APPEND_CAUSE(counter->self_comp,
169 "%s", validate_error);
170 goto error;
171 }
172
173 counter->last_printed_total = -1ULL;
174 counter->step = 10000;
175 step = bt_value_map_borrow_entry_value_const(params, "step");
176 if (step) {
177 counter->step = bt_value_integer_unsigned_get(step);
178 }
179
180 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
181 if (hide_zero) {
182 counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
183 }
184
185 bt_self_component_set_data(
186 bt_self_component_sink_as_self_component(component),
187 counter);
188
189 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
190 goto end;
191
192 error:
193 destroy_private_counter_data(counter);
194
195 end:
196 g_free(validate_error);
197 return status;
198 }
199
200 bt_component_class_sink_graph_is_configured_method_status
201 counter_graph_is_configured(
202 bt_self_component_sink *comp)
203 {
204 bt_component_class_sink_graph_is_configured_method_status status;
205 bt_message_iterator_create_from_sink_component_status
206 msg_iter_status;
207 struct counter *counter;
208 bt_message_iterator *iterator;
209
210 counter = bt_self_component_get_data(
211 bt_self_component_sink_as_self_component(comp));
212 BT_ASSERT(counter);
213
214 msg_iter_status = bt_message_iterator_create_from_sink_component(
215 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
216 in_port_name), &iterator);
217 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
218 status = (int) msg_iter_status;
219 goto end;
220 }
221
222 BT_MESSAGE_ITERATOR_MOVE_REF(
223 counter->msg_iter, iterator);
224
225 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
226 end:
227 return status;
228 }
229
230 bt_component_class_sink_consume_method_status counter_consume(
231 bt_self_component_sink *comp)
232 {
233 struct counter *counter;
234 bt_message_iterator_next_status next_status;
235 uint64_t msg_count;
236 bt_message_array_const msgs;
237 bt_self_component *self_comp =
238 bt_self_component_sink_as_self_component(comp);
239
240 counter = bt_self_component_get_data(self_comp);
241 BT_ASSERT_DBG(counter);
242 BT_ASSERT_DBG(counter->msg_iter);
243
244 /* Consume messages */
245 next_status = bt_message_iterator_next(
246 counter->msg_iter, &msgs, &msg_count);
247
248 switch (next_status) {
249 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
250 {
251 uint64_t i;
252
253 for (i = 0; i < msg_count; i++) {
254 const bt_message *msg = msgs[i];
255
256 BT_ASSERT_DBG(msg);
257 switch (bt_message_get_type(msg)) {
258 case BT_MESSAGE_TYPE_EVENT:
259 counter->count.event++;
260 break;
261 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
262 counter->count.packet_begin++;
263 break;
264 case BT_MESSAGE_TYPE_PACKET_END:
265 counter->count.packet_end++;
266 break;
267 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
268 counter->count.msg_iter_inactivity++;
269 break;
270 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
271 counter->count.stream_begin++;
272 break;
273 case BT_MESSAGE_TYPE_STREAM_END:
274 counter->count.stream_end++;
275 break;
276 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
277 counter->count.disc_events++;
278 break;
279 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
280 counter->count.disc_packets++;
281 break;
282 default:
283 counter->count.other++;
284 }
285
286 bt_message_put_ref(msg);
287 }
288
289 try_print_count(counter, msg_count);
290 break;
291 }
292 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
293 try_print_last(counter);
294 break;
295 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
296 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
297 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(self_comp,
298 "Failed to get messages from upstream component");
299 break;
300 default:
301 break;
302 }
303
304 return (int) next_status;
305 }
This page took 0.038284 seconds and 5 git commands to generate.