Logging: standardize logging tags
[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_LOG_TAG "PLUGIN/FLT.UTILS.COUNTER"
24 #include "logging.h"
25
26 #include <babeltrace2/babeltrace.h>
27 #include "common/macros.h"
28 #include "common/common.h"
29 #include "common/assert.h"
30 #include <inttypes.h>
31 #include <stdint.h>
32
33 #include "counter.h"
34
35 #define PRINTF_COUNT(_what, _var, args...) \
36 do { \
37 if (counter->count._var != 0 || !counter->hide_zero) { \
38 printf("%15" PRIu64 " %s message%s\n", \
39 counter->count._var, \
40 (_what), \
41 counter->count._var == 1 ? "" : "s"); \
42 } \
43 } while (0)
44
45 static
46 const char * const in_port_name = "in";
47
48 static
49 uint64_t get_total_count(struct counter *counter)
50 {
51 return counter->count.event +
52 counter->count.stream_begin +
53 counter->count.stream_end +
54 counter->count.stream_activity_begin +
55 counter->count.stream_activity_end +
56 counter->count.packet_begin +
57 counter->count.packet_end +
58 counter->count.disc_events +
59 counter->count.disc_packets +
60 counter->count.msg_iter_inactivity +
61 counter->count.other;
62 }
63
64 static
65 void print_count(struct counter *counter)
66 {
67 uint64_t total = get_total_count(counter);
68
69 PRINTF_COUNT("Event", event);
70 PRINTF_COUNT("Stream beginning", stream_begin);
71 PRINTF_COUNT("Stream end", stream_end);
72 PRINTF_COUNT("Stream activity beginning", stream_activity_begin);
73 PRINTF_COUNT("Stream activity end", stream_activity_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 void destroy_private_counter_data(struct counter *counter)
118 {
119 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
120 g_free(counter);
121 }
122
123 BT_HIDDEN
124 void counter_finalize(bt_self_component_sink *comp)
125 {
126 struct counter *counter;
127
128 BT_ASSERT(comp);
129 counter = bt_self_component_get_data(
130 bt_self_component_sink_as_self_component(comp));
131 BT_ASSERT(counter);
132 try_print_last(counter);
133 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
134 g_free(counter);
135 }
136
137 BT_HIDDEN
138 bt_self_component_status counter_init(
139 bt_self_component_sink *component,
140 const bt_value *params,
141 __attribute__((unused)) void *init_method_data)
142 {
143 bt_self_component_status ret;
144 struct counter *counter = g_new0(struct counter, 1);
145 const bt_value *step = NULL;
146 const bt_value *hide_zero = NULL;
147
148 if (!counter) {
149 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
150 goto error;
151 }
152
153 ret = bt_self_component_sink_add_input_port(component,
154 "in", NULL, NULL);
155 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
156 goto error;
157 }
158
159 counter->last_printed_total = -1ULL;
160 counter->step = 10000;
161 step = bt_value_map_borrow_entry_value_const(params, "step");
162 if (step) {
163 if (!bt_value_is_unsigned_integer(step)) {
164 BT_LOGE("`step` parameter: expecting an unsigned integer value: "
165 "type=%s", bt_common_value_type_string(
166 bt_value_get_type(step)));
167 goto error;
168 }
169
170 counter->step = bt_value_unsigned_integer_get(step);
171 }
172
173 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
174 if (hide_zero) {
175 if (!bt_value_is_bool(hide_zero)) {
176 BT_LOGE("`hide-zero` parameter: expecting a boolean value: "
177 "type=%s", bt_common_value_type_string(
178 bt_value_get_type(hide_zero)));
179 goto error;
180 }
181
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 goto end;
189
190 error:
191 destroy_private_counter_data(counter);
192 ret = BT_SELF_COMPONENT_STATUS_ERROR;
193
194 end:
195 return ret;
196 }
197
198 BT_HIDDEN
199 bt_self_component_status counter_graph_is_configured(
200 bt_self_component_sink *comp)
201 {
202 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
203 struct counter *counter;
204 bt_self_component_port_input_message_iterator *iterator;
205
206 counter = bt_self_component_get_data(
207 bt_self_component_sink_as_self_component(comp));
208 BT_ASSERT(counter);
209 iterator = bt_self_component_port_input_message_iterator_create(
210 bt_self_component_sink_borrow_input_port_by_name(comp,
211 in_port_name));
212 if (!iterator) {
213 status = BT_SELF_COMPONENT_STATUS_NOMEM;
214 goto end;
215 }
216
217 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
218 counter->msg_iter, iterator);
219
220 end:
221 return status;
222 }
223
224 BT_HIDDEN
225 bt_self_component_status counter_consume(
226 bt_self_component_sink *comp)
227 {
228 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
229 struct counter *counter;
230 bt_message_iterator_status it_ret;
231 uint64_t msg_count;
232 bt_message_array_const msgs;
233
234 counter = bt_self_component_get_data(
235 bt_self_component_sink_as_self_component(comp));
236 BT_ASSERT(counter);
237
238 if (G_UNLIKELY(!counter->msg_iter)) {
239 try_print_last(counter);
240 ret = BT_SELF_COMPONENT_STATUS_END;
241 goto end;
242 }
243
244 /* Consume messages */
245 it_ret = bt_self_component_port_input_message_iterator_next(
246 counter->msg_iter, &msgs, &msg_count);
247 if (it_ret < 0) {
248 ret = BT_SELF_COMPONENT_STATUS_ERROR;
249 goto end;
250 }
251
252 switch (it_ret) {
253 case BT_MESSAGE_ITERATOR_STATUS_OK:
254 {
255 uint64_t i;
256
257 for (i = 0; i < msg_count; i++) {
258 const bt_message *msg = msgs[i];
259
260 BT_ASSERT(msg);
261 switch (bt_message_get_type(msg)) {
262 case BT_MESSAGE_TYPE_EVENT:
263 counter->count.event++;
264 break;
265 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
266 counter->count.packet_begin++;
267 break;
268 case BT_MESSAGE_TYPE_PACKET_END:
269 counter->count.packet_end++;
270 break;
271 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
272 counter->count.msg_iter_inactivity++;
273 break;
274 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
275 counter->count.stream_begin++;
276 break;
277 case BT_MESSAGE_TYPE_STREAM_END:
278 counter->count.stream_end++;
279 break;
280 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
281 counter->count.stream_activity_begin++;
282 break;
283 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
284 counter->count.stream_activity_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 ret = BT_SELF_COMPONENT_STATUS_OK;
300 break;
301 }
302 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
303 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
304 goto end;
305 case BT_MESSAGE_ITERATOR_STATUS_END:
306 try_print_last(counter);
307 ret = BT_SELF_COMPONENT_STATUS_END;
308 goto end;
309 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
310 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
311 goto end;
312 default:
313 break;
314 }
315
316 try_print_count(counter, msg_count);
317
318 end:
319 return ret;
320 }
This page took 0.034969 seconds and 4 git commands to generate.