python: define _BaseObject._ptr property
[babeltrace.git] / src / plugins / utils / counter / counter.c
... / ...
CommitLineData
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
33static
34const char * const in_port_name = "in";
35
36bt_component_class_get_supported_mip_versions_method_status
37counter_supported_mip_versions(
38 bt_self_component_class_sink *self_component_class __attribute__((unused)),
39 const bt_value *params __attribute__((unused)),
40 void *initialize_method_data __attribute__((unused)),
41 bt_logging_level logging_level __attribute__((unused)),
42 bt_integer_range_set_unsigned *supported_versions) {
43 return (int) bt_integer_range_set_unsigned_add_range(supported_versions, 0, 1);
44}
45
46static
47uint64_t get_total_count(struct counter *counter)
48{
49 return counter->count.event +
50 counter->count.stream_begin +
51 counter->count.stream_end +
52 counter->count.packet_begin +
53 counter->count.packet_end +
54 counter->count.disc_events +
55 counter->count.disc_packets +
56 counter->count.msg_iter_inactivity +
57 counter->count.other;
58}
59
60static
61void print_count(struct counter *counter)
62{
63 uint64_t total = get_total_count(counter);
64
65 PRINTF_COUNT("Event", event);
66 PRINTF_COUNT("Stream beginning", stream_begin);
67 PRINTF_COUNT("Stream end", stream_end);
68 PRINTF_COUNT("Packet beginning", packet_begin);
69 PRINTF_COUNT("Packet end", packet_end);
70 PRINTF_COUNT("Discarded event", disc_events);
71 PRINTF_COUNT("Discarded packet", disc_packets);
72 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity);
73
74 if (counter->count.other > 0) {
75 PRINTF_COUNT("Other (unknown)", other);
76 }
77
78 printf("%s%15" PRIu64 " message%s (TOTAL)%s\n",
79 bt_common_color_bold(), total, total == 1 ? "" : "s",
80 bt_common_color_reset());
81 counter->last_printed_total = total;
82}
83
84static
85void try_print_count(struct counter *counter, uint64_t msg_count)
86{
87 if (counter->step == 0) {
88 /* No update */
89 return;
90 }
91
92 counter->at += msg_count;
93
94 if (counter->at >= counter->step) {
95 counter->at = 0;
96 print_count(counter);
97 putchar('\n');
98 }
99}
100
101static
102void try_print_last(struct counter *counter)
103{
104 const uint64_t total = get_total_count(counter);
105
106 if (total != counter->last_printed_total) {
107 print_count(counter);
108 }
109}
110
111static
112void destroy_private_counter_data(struct counter *counter)
113{
114 if (counter) {
115 bt_message_iterator_put_ref(
116 counter->msg_iter);
117 g_free(counter);
118 }
119}
120
121void counter_finalize(bt_self_component_sink *comp)
122{
123 struct counter *counter;
124
125 BT_ASSERT(comp);
126 counter = bt_self_component_get_data(
127 bt_self_component_sink_as_self_component(comp));
128 BT_ASSERT(counter);
129 try_print_last(counter);
130 bt_message_iterator_put_ref(counter->msg_iter);
131 g_free(counter);
132}
133
134static
135struct bt_param_validation_map_value_entry_descr counter_params[] = {
136 { "step", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_UNSIGNED_INTEGER } },
137 { "hide-zero", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
138 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
139};
140
141bt_component_class_initialize_method_status counter_init(
142 bt_self_component_sink *component,
143 bt_self_component_sink_configuration *config __attribute__((unused)),
144 const bt_value *params,
145 void *init_method_data __attribute__((unused)))
146{
147 bt_component_class_initialize_method_status status;
148 bt_self_component_add_port_status add_port_status;
149 struct counter *counter = g_new0(struct counter, 1);
150 const bt_value *step = NULL;
151 const bt_value *hide_zero = NULL;
152 enum bt_param_validation_status validation_status;
153 gchar *validate_error = NULL;
154
155 if (!counter) {
156 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
157 goto error;
158 }
159
160 counter->self_comp =
161 bt_self_component_sink_as_self_component(component);
162 counter->log_level = bt_component_get_logging_level(
163 bt_self_component_as_component(counter->self_comp));
164 add_port_status = bt_self_component_sink_add_input_port(component,
165 "in", NULL, NULL);
166 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
167 status = (int) add_port_status;
168 goto error;
169 }
170
171 validation_status = bt_param_validation_validate(params,
172 counter_params, &validate_error);
173 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
174 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
175 goto error;
176 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
177 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
178 BT_COMP_LOGE_APPEND_CAUSE(counter->self_comp,
179 "%s", validate_error);
180 goto error;
181 }
182
183 counter->last_printed_total = -1ULL;
184 counter->step = 10000;
185 step = bt_value_map_borrow_entry_value_const(params, "step");
186 if (step) {
187 counter->step = bt_value_integer_unsigned_get(step);
188 }
189
190 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
191 if (hide_zero) {
192 counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
193 }
194
195 bt_self_component_set_data(
196 bt_self_component_sink_as_self_component(component),
197 counter);
198
199 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
200 goto end;
201
202error:
203 destroy_private_counter_data(counter);
204
205end:
206 g_free(validate_error);
207 return status;
208}
209
210bt_component_class_sink_graph_is_configured_method_status
211counter_graph_is_configured(
212 bt_self_component_sink *comp)
213{
214 bt_component_class_sink_graph_is_configured_method_status status;
215 bt_message_iterator_create_from_sink_component_status
216 msg_iter_status;
217 struct counter *counter;
218 bt_message_iterator *iterator;
219
220 counter = bt_self_component_get_data(
221 bt_self_component_sink_as_self_component(comp));
222 BT_ASSERT(counter);
223
224 msg_iter_status = bt_message_iterator_create_from_sink_component(
225 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
226 in_port_name), &iterator);
227 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
228 status = (int) msg_iter_status;
229 goto end;
230 }
231
232 BT_MESSAGE_ITERATOR_MOVE_REF(
233 counter->msg_iter, iterator);
234
235 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
236end:
237 return status;
238}
239
240bt_component_class_sink_consume_method_status counter_consume(
241 bt_self_component_sink *comp)
242{
243 struct counter *counter;
244 bt_message_iterator_next_status next_status;
245 uint64_t msg_count;
246 bt_message_array_const msgs;
247 bt_self_component *self_comp =
248 bt_self_component_sink_as_self_component(comp);
249
250 counter = bt_self_component_get_data(self_comp);
251 BT_ASSERT_DBG(counter);
252 BT_ASSERT_DBG(counter->msg_iter);
253
254 /* Consume messages */
255 next_status = bt_message_iterator_next(
256 counter->msg_iter, &msgs, &msg_count);
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 try_print_count(counter, msg_count);
300 break;
301 }
302 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
303 try_print_last(counter);
304 break;
305 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
306 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
307 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(self_comp,
308 "Failed to get messages from upstream component");
309 break;
310 default:
311 break;
312 }
313
314 return (int) next_status;
315}
This page took 0.025253 seconds and 5 git commands to generate.