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