Move to kernel style SPDX license identifiers
[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 BT_HIDDEN
112 void counter_finalize(bt_self_component_sink *comp)
113 {
114 struct counter *counter;
115
116 BT_ASSERT(comp);
117 counter = bt_self_component_get_data(
118 bt_self_component_sink_as_self_component(comp));
119 BT_ASSERT(counter);
120 try_print_last(counter);
121 bt_message_iterator_put_ref(counter->msg_iter);
122 g_free(counter);
123 }
124
125 static
126 struct bt_param_validation_map_value_entry_descr counter_params[] = {
127 { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_UNSIGNED_INTEGER } },
128 { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
129 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
130 };
131
132 BT_HIDDEN
133 bt_component_class_initialize_method_status counter_init(
134 bt_self_component_sink *component,
135 bt_self_component_sink_configuration *config,
136 const bt_value *params,
137 __attribute__((unused)) void *init_method_data)
138 {
139 bt_component_class_initialize_method_status status;
140 bt_self_component_add_port_status add_port_status;
141 struct counter *counter = g_new0(struct counter, 1);
142 const bt_value *step = NULL;
143 const bt_value *hide_zero = NULL;
144 enum bt_param_validation_status validation_status;
145 gchar *validate_error = NULL;
146
147 if (!counter) {
148 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
149 goto error;
150 }
151
152 counter->self_comp =
153 bt_self_component_sink_as_self_component(component);
154 counter->log_level = bt_component_get_logging_level(
155 bt_self_component_as_component(counter->self_comp));
156 add_port_status = bt_self_component_sink_add_input_port(component,
157 "in", NULL, NULL);
158 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
159 status = (int) add_port_status;
160 goto error;
161 }
162
163 validation_status = bt_param_validation_validate(params,
164 counter_params, &validate_error);
165 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
166 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
167 goto error;
168 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
169 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
170 BT_COMP_LOGE_APPEND_CAUSE(counter->self_comp,
171 "%s", validate_error);
172 goto error;
173 }
174
175 counter->last_printed_total = -1ULL;
176 counter->step = 10000;
177 step = bt_value_map_borrow_entry_value_const(params, "step");
178 if (step) {
179 counter->step = bt_value_integer_unsigned_get(step);
180 }
181
182 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
183 if (hide_zero) {
184 counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
185 }
186
187 bt_self_component_set_data(
188 bt_self_component_sink_as_self_component(component),
189 counter);
190
191 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
192 goto end;
193
194 error:
195 destroy_private_counter_data(counter);
196
197 end:
198 g_free(validate_error);
199 return status;
200 }
201
202 BT_HIDDEN
203 bt_component_class_sink_graph_is_configured_method_status
204 counter_graph_is_configured(
205 bt_self_component_sink *comp)
206 {
207 bt_component_class_sink_graph_is_configured_method_status status;
208 bt_message_iterator_create_from_sink_component_status
209 msg_iter_status;
210 struct counter *counter;
211 bt_message_iterator *iterator;
212
213 counter = bt_self_component_get_data(
214 bt_self_component_sink_as_self_component(comp));
215 BT_ASSERT(counter);
216
217 msg_iter_status = bt_message_iterator_create_from_sink_component(
218 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
219 in_port_name), &iterator);
220 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
221 status = (int) msg_iter_status;
222 goto end;
223 }
224
225 BT_MESSAGE_ITERATOR_MOVE_REF(
226 counter->msg_iter, iterator);
227
228 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
229 end:
230 return status;
231 }
232
233 BT_HIDDEN
234 bt_component_class_sink_consume_method_status counter_consume(
235 bt_self_component_sink *comp)
236 {
237 bt_component_class_sink_consume_method_status status =
238 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
239 struct counter *counter;
240 bt_message_iterator_next_status next_status;
241 uint64_t msg_count;
242 bt_message_array_const msgs;
243
244 counter = bt_self_component_get_data(
245 bt_self_component_sink_as_self_component(comp));
246 BT_ASSERT_DBG(counter);
247
248 if (G_UNLIKELY(!counter->msg_iter)) {
249 try_print_last(counter);
250 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
251 goto end;
252 }
253
254 /* Consume messages */
255 next_status = bt_message_iterator_next(
256 counter->msg_iter, &msgs, &msg_count);
257 if (next_status < 0) {
258 status = (int) next_status;
259 goto end;
260 }
261
262 switch (next_status) {
263 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
264 {
265 uint64_t i;
266
267 for (i = 0; i < msg_count; i++) {
268 const bt_message *msg = msgs[i];
269
270 BT_ASSERT_DBG(msg);
271 switch (bt_message_get_type(msg)) {
272 case BT_MESSAGE_TYPE_EVENT:
273 counter->count.event++;
274 break;
275 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
276 counter->count.packet_begin++;
277 break;
278 case BT_MESSAGE_TYPE_PACKET_END:
279 counter->count.packet_end++;
280 break;
281 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
282 counter->count.msg_iter_inactivity++;
283 break;
284 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
285 counter->count.stream_begin++;
286 break;
287 case BT_MESSAGE_TYPE_STREAM_END:
288 counter->count.stream_end++;
289 break;
290 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
291 counter->count.disc_events++;
292 break;
293 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
294 counter->count.disc_packets++;
295 break;
296 default:
297 counter->count.other++;
298 }
299
300 bt_message_put_ref(msg);
301 }
302
303 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
304 break;
305 }
306 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
307 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
308 goto end;
309 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
310 try_print_last(counter);
311 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
312 goto end;
313 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
314 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
315 goto end;
316 default:
317 break;
318 }
319
320 try_print_count(counter, msg_count);
321
322 end:
323 return status;
324 }
This page took 0.035008 seconds and 4 git commands to generate.