be20f89538bede60a5bf4457a44eda80f82d237e
[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,
134 const bt_value *params,
135 __attribute__((unused)) void *init_method_data)
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 bt_component_class_sink_consume_method_status status =
234 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
235 struct counter *counter;
236 bt_message_iterator_next_status next_status;
237 uint64_t msg_count;
238 bt_message_array_const msgs;
239
240 counter = bt_self_component_get_data(
241 bt_self_component_sink_as_self_component(comp));
242 BT_ASSERT_DBG(counter);
243
244 if (G_UNLIKELY(!counter->msg_iter)) {
245 try_print_last(counter);
246 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
247 goto end;
248 }
249
250 /* Consume messages */
251 next_status = bt_message_iterator_next(
252 counter->msg_iter, &msgs, &msg_count);
253 if (next_status < 0) {
254 status = (int) next_status;
255 goto end;
256 }
257
258 switch (next_status) {
259 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
260 {
261 uint64_t i;
262
263 for (i = 0; i < msg_count; i++) {
264 const bt_message *msg = msgs[i];
265
266 BT_ASSERT_DBG(msg);
267 switch (bt_message_get_type(msg)) {
268 case BT_MESSAGE_TYPE_EVENT:
269 counter->count.event++;
270 break;
271 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
272 counter->count.packet_begin++;
273 break;
274 case BT_MESSAGE_TYPE_PACKET_END:
275 counter->count.packet_end++;
276 break;
277 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
278 counter->count.msg_iter_inactivity++;
279 break;
280 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
281 counter->count.stream_begin++;
282 break;
283 case BT_MESSAGE_TYPE_STREAM_END:
284 counter->count.stream_end++;
285 break;
286 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
287 counter->count.disc_events++;
288 break;
289 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
290 counter->count.disc_packets++;
291 break;
292 default:
293 counter->count.other++;
294 }
295
296 bt_message_put_ref(msg);
297 }
298
299 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
300 break;
301 }
302 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
303 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
304 goto end;
305 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
306 try_print_last(counter);
307 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
308 goto end;
309 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
310 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
311 goto end;
312 default:
313 break;
314 }
315
316 try_print_count(counter, msg_count);
317
318 end:
319 return status;
320 }
This page took 0.035728 seconds and 3 git commands to generate.